Skip to content

Commit a9557ee

Browse files
committed
Fix Tabs for Diagnostic Marks
1 parent 3562af1 commit a9557ee

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

liquidjava-verifier/src/main/java/liquidjava/diagnostics/LJDiagnostic.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,34 @@ public String getSnippet() {
108108

109109
for (int i = startLine; i <= endLine; i++) {
110110
String lineNumStr = String.format("%" + padding + "d", i);
111-
String line = lines.get(i - 1);
111+
String rawLine = lines.get(i - 1);
112+
String line = rawLine.replace("\t", " ");
112113

113114
// add line
114115
sb.append(Colors.GREY).append(lineNumStr).append(PIPE).append(line).append(Colors.RESET).append("\n");
115116

116117
// add error markers on the line(s) with the error
117118
if (i >= position.lineStart() && i <= position.lineEnd()) {
118119
int colStart = (i == position.lineStart()) ? position.colStart() : 1;
119-
int colEnd = (i == position.lineEnd()) ? position.colEnd() : line.length();
120+
int colEnd = (i == position.lineEnd()) ? position.colEnd() : rawLine.length();
120121

121122
if (colStart > 0 && colEnd > 0) {
123+
int tabsBeforeStart = (int) rawLine.substring(0, Math.max(0, colStart - 1)).chars()
124+
.filter(ch -> ch == '\t').count();
125+
int tabsBeforeEnd = (int) rawLine.substring(0, Math.max(0, colEnd)).chars()
126+
.filter(ch -> ch == '\t').count();
127+
int visualColStart = colStart + tabsBeforeStart * 3;
128+
int visualColEnd = colEnd + tabsBeforeEnd * 3;
129+
122130
// line number padding + pipe + column offset
123131
String indent = " ".repeat(padding) + Colors.GREY + PIPE + Colors.RESET
124-
+ " ".repeat(colStart - 1);
125-
String markers = accentColor + "^".repeat(Math.max(1, colEnd - colStart + 1));
132+
+ " ".repeat(visualColStart - 1);
133+
String markers = accentColor + "^".repeat(Math.max(1, visualColEnd - visualColStart + 1));
126134
sb.append(indent).append(markers);
127135

128136
// custom message
129137
if (customMessage != null && !customMessage.isBlank()) {
130-
String offset = " ".repeat(padding + colEnd + PIPE.length() + 1);
138+
String offset = " ".repeat(padding + visualColEnd + PIPE.length() + 1);
131139
sb.append(" " + customMessage.replace("\n", "\n" + offset));
132140
}
133141
sb.append(Colors.RESET).append("\n");

0 commit comments

Comments
 (0)