Skip to content

Improve diagnostic messages printed to console #2381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import {

import {
isLineBreak,
isWhiteSpace,
COLOR_CYAN,
COLOR_YELLOW,
COLOR_RED,
COLOR_MAGENTA,
COLOR_RESET,
isColorsEnabled,
setColorsEnabled
setColorsEnabled,
CharCode
} from "./util";

export {
Expand Down Expand Up @@ -185,10 +187,10 @@ export function formatDiagnosticMessage(
if (showContext) {
sb.push("\n");
sb.push(formatDiagnosticContext(range));
} else {
sb.push("\n in ");
sb.push(source.normalizedPath);
}
sb.push("\n");
sb.push(" in ");
sb.push(source.normalizedPath);
sb.push("(");
sb.push(source.lineAt(range.start).toString());
sb.push(",");
Expand All @@ -201,10 +203,10 @@ export function formatDiagnosticMessage(
if (showContext) {
sb.push("\n");
sb.push(formatDiagnosticContext(relatedRange));
} else {
sb.push("\n in ");
sb.push(relatedSource.normalizedPath);
}
sb.push("\n");
sb.push(" in ");
sb.push(relatedSource.normalizedPath);
sb.push("(");
sb.push(relatedSource.lineAt(relatedRange.start).toString());
sb.push(",");
Expand All @@ -218,34 +220,59 @@ export function formatDiagnosticMessage(

/** Formats the diagnostic context for the specified range, optionally with terminal colors. */
function formatDiagnosticContext(range: Range): string {
var text = range.source.text;
var source = range.source;
var text = source.text;
var len = text.length;
var start = range.start;
var end = range.end;
var end = start;
var lineNumber = source.lineAt(start).toString();
var lineSpace = " ".repeat(lineNumber.length);
// Find preceeding line break
while (start > 0 && !isLineBreak(text.charCodeAt(start - 1))) start--;
// Skip leading whitespace
while (start < len && isWhiteSpace(text.charCodeAt(start))) start++;
// Find next line break
while (end < len && !isLineBreak(text.charCodeAt(end))) end++;
var sb: string[] = [
"\n ",
text.substring(start, end),
"\n "
lineSpace,
" :\n ",
lineNumber,
" │ ",
text.substring(start, end).replaceAll("\t", " "),
"\n ",
lineSpace,
" │ "
];
while (start < range.start) {
sb.push(" ");
start++;
if (text.charCodeAt(start) == CharCode.TAB) {
sb.push(" ");
start += 2;
} else {
sb.push(" ");
start++;
}
}
if (isColorsEnabled()) sb.push(COLOR_RED);
if (range.start == range.end) {
sb.push("^");
} else {
while (start++ < range.end) {
if (isLineBreak(text.charCodeAt(start))) {
let cc = text.charCodeAt(start);
if (cc == CharCode.TAB) {
sb.push("~~");
} else if (isLineBreak(cc)) {
sb.push(start == range.start + 1 ? "^" : "~");
break;
} else {
sb.push("~");
}
sb.push("~");
}
}
if (isColorsEnabled()) sb.push(COLOR_RESET);
sb.push("\n ");
sb.push(lineSpace);
sb.push(" └─ in ");
sb.push(source.normalizedPath);
return sb.join("");
}

Expand Down