Skip to content

Commit b09016c

Browse files
committed
Print file info after source excerpt in diagnostics
1 parent e45eca6 commit b09016c

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

src/main/java/run/myCode/compiler/CompileDiagnosticListener.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,39 @@ private String formatDiagnostic(Diagnostic<? extends JavaFileObject> d) {
5050
StringBuilder sb = new StringBuilder();
5151
String sourceName = d.getSource() == null ? "Unknown Source"
5252
: new File(d.getSource().getName()).getName();
53-
sb.append(sourceName).append(':').append(d.getLineNumber()).append(':')
54-
.append(' ').append(d.getKind().toString().toLowerCase(Locale.ENGLISH))
55-
.append(':').append(' ').append(d.getMessage(Locale.ENGLISH));
5653

54+
boolean appendedSource = false;
5755
try {
5856
if (d.getSource() != null) {
5957
CharSequence content = d.getSource().getCharContent(true);
6058
String[] lines = content.toString().split("\r?\n");
6159
long lineNo = d.getLineNumber();
6260
if (lineNo > 0 && lineNo <= lines.length) {
6361
String line = lines[(int) lineNo - 1];
64-
sb.append(System.lineSeparator()).append(line)
65-
.append(System.lineSeparator());
62+
sb.append(line).append(System.lineSeparator());
6663
long col = d.getColumnNumber();
6764
if (col > 0) {
6865
for (int i = 1; i < col; i++) {
6966
sb.append(' ');
7067
}
7168
sb.append('^');
7269
}
70+
sb.append(System.lineSeparator());
71+
appendedSource = true;
7372
}
7473
}
7574
} catch (IOException e) {
7675
// ignore - if we can't read the source, just return the basic message
7776
}
7877

78+
if (!appendedSource && sb.length() > 0) {
79+
sb.append(System.lineSeparator());
80+
}
81+
82+
sb.append(sourceName).append(':').append(d.getLineNumber()).append(':')
83+
.append(' ').append(d.getKind().toString().toLowerCase(Locale.ENGLISH))
84+
.append(':').append(' ').append(d.getMessage(Locale.ENGLISH));
85+
7986
return sb.toString();
8087
}
8188
}

src/main/java/zss/compiler/CompileDiagnosticListener.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,39 @@ private String formatDiagnostic(Diagnostic<? extends JavaFileObject> d) {
5050
StringBuilder sb = new StringBuilder();
5151
String sourceName = d.getSource() == null ? "Unknown Source"
5252
: new File(d.getSource().getName()).getName();
53-
sb.append(sourceName).append(':').append(d.getLineNumber()).append(':')
54-
.append(' ').append(d.getKind().toString().toLowerCase(Locale.ENGLISH))
55-
.append(':').append(' ').append(d.getMessage(Locale.ENGLISH));
5653

54+
boolean appendedSource = false;
5755
try {
5856
if (d.getSource() != null) {
5957
CharSequence content = d.getSource().getCharContent(true);
6058
String[] lines = content.toString().split("\r?\n");
6159
long lineNo = d.getLineNumber();
6260
if (lineNo > 0 && lineNo <= lines.length) {
6361
String line = lines[(int) lineNo - 1];
64-
sb.append(System.lineSeparator()).append(line)
65-
.append(System.lineSeparator());
62+
sb.append(line).append(System.lineSeparator());
6663
long col = d.getColumnNumber();
6764
if (col > 0) {
6865
for (int i = 1; i < col; i++) {
6966
sb.append(' ');
7067
}
7168
sb.append('^');
7269
}
70+
sb.append(System.lineSeparator());
71+
appendedSource = true;
7372
}
7473
}
7574
} catch (IOException e) {
7675
// ignore - if we can't read the source, just return the basic message
7776
}
7877

78+
if (!appendedSource && sb.length() > 0) {
79+
sb.append(System.lineSeparator());
80+
}
81+
82+
sb.append(sourceName).append(':').append(d.getLineNumber()).append(':')
83+
.append(' ').append(d.getKind().toString().toLowerCase(Locale.ENGLISH))
84+
.append(':').append(' ').append(d.getMessage(Locale.ENGLISH));
85+
7986
return sb.toString();
8087
}
8188
}

src/test/java/example/HelloFailureTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ public void testCompilationFailureShowsDiagnostics() {
3232

3333
assertFalse("Compiler returned no response", resp == null);
3434
assertFalse("Compilation unexpectedly succeeded", resp.getSucceeded());
35-
assertTrue("Missing file/line in diagnostics", resp.getResult().contains("Broken.java:3"));
36-
assertTrue("Missing caret in diagnostics", resp.getResult().contains("^"));
37-
assertTrue("Missing source line", resp.getResult().contains("System.out.println(\"hi\")"));
35+
String diag = resp.getResult();
36+
assertTrue("Missing source line", diag.contains("System.out.println(\"hi\")"));
37+
assertTrue("Missing caret in diagnostics", diag.contains("^"));
38+
assertTrue("Missing file/line in diagnostics", diag.contains("Broken.java:3"));
3839

39-
System.out.println("Result: " + resp.getResult());
40+
int lineIdx = diag.indexOf("System.out.println(\"hi\")");
41+
int caretIdx = diag.indexOf("^");
42+
int fileIdx = diag.indexOf("Broken.java:3");
43+
assertTrue("Caret should follow source line", lineIdx >= 0 && caretIdx > lineIdx);
44+
assertTrue("File info should follow caret", fileIdx > caretIdx);
45+
46+
System.out.println("Result: " + diag);
4047
}
4148

4249
private CompileResponse doTest(String resourceName) {

0 commit comments

Comments
 (0)