Parse + decorate rendered ANSI cargo output

Use ANSI control characters to display text decorations matching the
VScode terminal theme, and strip them out when providing text content
for rustc diagnostics.

This adds the small `anser` library to parse the control codes, and it
also supports HTML output so it should be fairly easy to switch to a
rendered HTML/webview implementation if desired.
This commit is contained in:
Ian Chamberlain 2023-01-03 10:16:16 -05:00
parent f32e20edb9
commit 1b8141b54c
No known key found for this signature in database
GPG key ID: BD7B25D58170E124
5 changed files with 272 additions and 24 deletions

View file

@ -3,6 +3,7 @@ import * as vscode from "vscode";
import * as ra from "../src/lsp_ext";
import * as Is from "vscode-languageclient/lib/common/utils/is";
import { assert } from "./util";
import * as diagnostics from "./diagnostics";
import { WorkspaceEdit } from "vscode";
import { Config, substituteVSCodeVariables } from "./config";
import { randomUUID } from "crypto";
@ -120,12 +121,12 @@ export async function createClient(
},
async handleDiagnostics(
uri: vscode.Uri,
diagnostics: vscode.Diagnostic[],
diagnosticList: vscode.Diagnostic[],
next: lc.HandleDiagnosticsSignature
) {
const preview = config.previewRustcOutput;
const errorCode = config.useRustcErrorCode;
diagnostics.forEach((diag, idx) => {
diagnosticList.forEach((diag, idx) => {
// Abuse the fact that VSCode leaks the LSP diagnostics data field through the
// Diagnostic class, if they ever break this we are out of luck and have to go
// back to the worst diagnostics experience ever:)
@ -154,8 +155,8 @@ export async function createClient(
}
diag.code = {
target: vscode.Uri.from({
scheme: "rust-analyzer-diagnostics-view",
path: "/diagnostic message",
scheme: diagnostics.URI_SCHEME,
path: `/diagnostic message [${idx.toString()}]`,
fragment: uri.toString(),
query: idx.toString(),
}),
@ -163,7 +164,7 @@ export async function createClient(
};
}
});
return next(uri, diagnostics);
return next(uri, diagnosticList);
},
async provideHover(
document: vscode.TextDocument,