Skip to content

Commit

Permalink
[lib/util] Add debugging utilities to TextReader
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Aug 12, 2024
1 parent ec07ee7 commit c20c246
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/util/TextReader.v3
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ class TextReader {
}
}
private def tabColumn() {
column = 1 + (((column + 8) >>> 3) << 3);
column = nextTabStop(column);
}
def nextTabStop(column: int) -> int {
return 1 + (((column + 8) >>> 3) << 3);
}
def renderCurrentLineWithCaret(buf: StringBuilder, pos: int) -> StringBuilder {
var start = pos - 1;
while (start > 0) {
if (data[start] == '\n') {
start++;
break;
}
start--;
}
var end = start;
while (end < limit) {
if (data[end] == '\n') break;
end++;
}
buf.putr(data[start ... end]);
var column = 1;
for (i = start; i < end; i++) {
if (i == pos) break;
var ch = data[i];
if (ch == '\t') column = nextTabStop(column);
else column++;
}
if (start <= pos && pos < end) {
buf.ln();
for (i < column - 1) buf.sp();
buf.putc('^');
}
return buf;
}
}

0 comments on commit c20c246

Please sign in to comment.