Skip to content

Commit

Permalink
Print the problem line as context in error messages
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 675207795
  • Loading branch information
cushon authored and google-java-format Team committed Sep 16, 2024
1 parent 40ac75f commit 8c652ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

import static java.util.Locale.ENGLISH;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.googlejavaformat.FormatterDiagnostic;
import java.util.List;
import java.util.regex.Pattern;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

Expand Down Expand Up @@ -55,4 +58,22 @@ private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
return FormatterDiagnostic.create(
(int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
}

public String formatDiagnostics(String path, String input) {
List<String> lines = Splitter.on(NEWLINE_PATTERN).splitToList(input);
StringBuilder sb = new StringBuilder();
for (FormatterDiagnostic diagnostic : diagnostics()) {
sb.append(path).append(":").append(diagnostic).append(System.lineSeparator());
int line = diagnostic.line();
int column = diagnostic.column();
if (line != -1 && column != -1) {
sb.append(CharMatcher.breakingWhitespace().trimTrailingFrom(lines.get(line - 1)))
.append(System.lineSeparator());
sb.append(" ".repeat(column)).append('^').append(System.lineSeparator());
}
}
return sb.toString();
}

private static final Pattern NEWLINE_PATTERN = Pattern.compile("\\R");
}
9 changes: 2 additions & 7 deletions core/src/main/java/com/google/googlejavaformat/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.googlejavaformat.FormatterDiagnostic;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.io.IOError;
import java.io.IOException;
Expand Down Expand Up @@ -175,9 +174,7 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
for (FormatFileCallable.Result result : results) {
Path path = result.path();
if (result.exception() != null) {
for (FormatterDiagnostic diagnostic : result.exception().diagnostics()) {
errWriter.println(path + ":" + diagnostic);
}
errWriter.print(result.exception().formatDiagnostics(path.toString(), result.input()));
allOk = false;
continue;
}
Expand Down Expand Up @@ -224,9 +221,7 @@ private int formatStdin(CommandLineOptions parameters, JavaFormatterOptions opti
FormatFileCallable.Result result =
new FormatFileCallable(parameters, null, input, options).call();
if (result.exception() != null) {
for (FormatterDiagnostic diagnostic : result.exception().diagnostics()) {
errWriter.println(stdinFilename + ":" + diagnostic);
}
errWriter.print(result.exception().formatDiagnostics(stdinFilename, input));
ok = false;
} else {
String output = result.output();
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/com/google/googlejavaformat/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,29 @@ public void reorderModifiersOptionTest() throws Exception {
.formatSource(source))
.isEqualTo(source);
}

@Test
public void badIdentifier() throws Exception {
Path path = testFolder.newFile("Test.java").toPath();
String[] input = {
"class Test {", //
" void f(int package) {}",
"}",
"",
};
String source = joiner.join(input);
Files.writeString(path, source, UTF_8);
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
int errorCode = main.format(path.toAbsolutePath().toString());
assertWithMessage("Error Code").that(errorCode).isEqualTo(1);
String[] expected = {
path + ":2:14: error: <identifier> expected", //
" void f(int package) {}",
" ^",
"",
};
assertThat(err.toString()).isEqualTo(joiner.join(expected));
}
}

0 comments on commit 8c652ed

Please sign in to comment.