-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathLJDiagnostic.java
More file actions
164 lines (134 loc) · 5.47 KB
/
LJDiagnostic.java
File metadata and controls
164 lines (134 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package liquidjava.diagnostics;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import spoon.reflect.cu.SourcePosition;
public class LJDiagnostic extends RuntimeException {
private final String title;
private final String message;
private final String accentColor;
private final String customMessage;
private String file;
private ErrorPosition position;
private static final String PIPE = " | ";
public LJDiagnostic(String title, String message, SourcePosition pos, String accentColor, String customMessage) {
this.title = title;
this.message = message;
this.file = (pos != null && pos.getFile() != null) ? pos.getFile().getPath() : null;
this.position = ErrorPosition.fromSpoonPosition(pos);
this.accentColor = accentColor;
this.customMessage = customMessage;
}
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
public String getCustomMessage() {
return customMessage;
}
public String getDetails() {
return ""; // to be overridden by subclasses
}
public ErrorPosition getPosition() {
return position;
}
public void setPosition(SourcePosition pos) {
if (pos == null)
return;
this.position = ErrorPosition.fromSpoonPosition(pos);
this.file = pos.getFile().getPath();
}
public String getFile() {
return file;
}
public String getAccentColor() {
return accentColor;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
// title
sb.append("\n").append(accentColor).append(title).append(": ").append(Colors.RESET).append(message)
.append("\n");
// snippet
String snippet = getSnippet();
if (snippet != null) {
sb.append(snippet);
}
// details
String details = getDetails();
if (!details.isEmpty()) {
sb.append(" --> ").append(String.join("\n ", details.split("\n"))).append("\n");
}
// location
if (file != null && position != null) {
sb.append("\n").append(file).append(":").append(position.lineStart()).append(Colors.RESET).append("\n");
}
return sb.toString();
}
public String getSnippet() {
if (file == null || position == null)
return null;
Path path = Path.of(file);
try {
List<String> lines = Files.readAllLines(path);
StringBuilder sb = new StringBuilder();
// before and after lines for context
int contextBefore = 2;
int contextAfter = 2;
int startLine = Math.max(1, position.lineStart() - contextBefore);
int endLine = Math.min(lines.size(), position.lineEnd() + contextAfter);
// calculate padding for line numbers
int padding = String.valueOf(endLine).length();
for (int i = startLine; i <= endLine; i++) {
String lineNumStr = String.format("%" + padding + "d", i);
String line = lines.get(i - 1);
// add line
sb.append(Colors.GREY).append(lineNumStr).append(PIPE).append(line).append(Colors.RESET).append("\n");
// add error markers on the line(s) with the error
if (i >= position.lineStart() && i <= position.lineEnd()) {
int colStart = (i == position.lineStart()) ? position.colStart() : 1;
int colEnd = (i == position.lineEnd()) ? position.colEnd() : line.length();
if (colStart > 0 && colEnd > 0) {
// line number padding + pipe + column offset
String indent = " ".repeat(padding) + Colors.GREY + PIPE + Colors.RESET
+ " ".repeat(colStart - 1);
String markers = accentColor + "^".repeat(Math.max(1, colEnd - colStart + 1));
sb.append(indent).append(markers);
// custom message
if (customMessage != null && !customMessage.isBlank()) {
String offset = " ".repeat(padding + colEnd + PIPE.length() + 1);
sb.append(" " + customMessage.replace("\n", "\n" + offset));
}
sb.append(Colors.RESET).append("\n");
}
}
}
return sb.toString();
} catch (Exception e) {
return null;
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
LJDiagnostic other = (LJDiagnostic) obj;
return title.equals(other.title) && message.equals(other.message)
&& ((file == null && other.file == null) || (file != null && file.equals(other.file)))
&& ((position == null && other.position == null)
|| (position != null && position.equals(other.position)));
}
@Override
public int hashCode() {
int result = title.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + (file != null ? file.hashCode() : 0);
result = 31 * result + (position != null ? position.hashCode() : 0);
return result;
}
}