Skip to content

Commit

Permalink
support pure source report building
Browse files Browse the repository at this point in the history
  • Loading branch information
ChAoSUnItY committed Jun 14, 2022
1 parent a1c56d8 commit 1b9021c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/main/java/chaos/unity/nenggao/FileReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@
import java.util.*;

public class FileReportBuilder {
private @NotNull String filePath;
private @Nullable String filePath;
/**
* The actual source file to retrieve, will be read by {@link Source#fromFile(File)}
*/
private final @NotNull File sourceFile;
private final @Nullable File sourceFile;
private final @Nullable String source;
private boolean enableColor = true;
private @NotNull CharacterSet characterSet = CharacterSet.UNICODE;
private @NotNull List<@NotNull Report> reports = new ArrayList<>();

private FileReportBuilder(@NotNull String sourceFilePath) {
this.filePath = sourceFilePath;
this.sourceFile = new File(sourceFilePath);
}

private FileReportBuilder(@NotNull File sourceFile) {
try {
this.filePath = sourceFile.getCanonicalPath();
Expand All @@ -40,16 +36,23 @@ private FileReportBuilder(@NotNull File sourceFile) {
this.filePath = sourceFile.getPath();
}
this.sourceFile = sourceFile;
this.source = null;
}

public static @NotNull FileReportBuilder sourceFile(@NotNull String sourceFilePath) {
return new FileReportBuilder(sourceFilePath);
private FileReportBuilder(@NotNull String source) {
this.filePath = null;
this.sourceFile = null;
this.source = source;
}

public static @NotNull FileReportBuilder sourceFile(@NotNull File sourceFile) {
return new FileReportBuilder(sourceFile);
}

public static @NotNull FileReportBuilder source(@NotNull String source) {
return new FileReportBuilder(source);
}

public @NotNull ReportBuilder warning(@NotNull Span span, @NotNull String message, @Nullable Object... args) {
return new ReportBuilder(this, new Warning(span, String.format(message, args)));
}
Expand All @@ -76,10 +79,12 @@ private FileReportBuilder(@NotNull File sourceFile) {
public void print(final @NotNull PrintStream printStream) {
enableWindows10AnsiSupport();

Source source = SourceCache.INSTANCE.getOrAdd(sourceFile);
Source source = sourceFile != null ? SourceCache.INSTANCE.getOrAdd(sourceFile) :
this.source != null ? Source.fromString(this.source) : null;

if (source == null)
return;
if (source == null) {
throw new IllegalStateException("Unable to fetch source to print");
}

for (Report report : reports) {
report.labels.sort((l1, l2) -> l1.span.startPosition.pos - l2.span.startPosition.pos); // Sort label's order so the render algorithm won't mess up
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/chaos/unity/nenggao/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -47,7 +48,11 @@ public Source(@NotNull List<@NotNull Line> lines) {
return source;
}

public static Source fromStrings(@NotNull List<@NotNull String> lines) {
public static @NotNull Source fromString(@NotNull String line) {
return new Source(Arrays.asList(new Line(1, 0, line.length(), line)));
}

public static @NotNull Source fromStrings(@NotNull List<@NotNull String> lines) {
AtomicInteger offset = new AtomicInteger();

return new Source(IntStream.range(0, lines.size()).mapToObj(i -> {
Expand Down

0 comments on commit 1b9021c

Please sign in to comment.