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 lc from "vscode-languageclient/node";
import * as commands from "./commands";
import { CommandFactory, Ctx, fetchWorkspace } from "./ctx";
import * as diagnostics from "./diagnostics";
import { activateTaskProvider } from "./tasks";
import { setContextValue } from "./util";
@ -48,30 +49,52 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
ctx.pushExtCleanup(activateTaskProvider(ctx.config));
}
const diagnosticProvider = new diagnostics.TextDocumentProvider(ctx);
ctx.pushExtCleanup(
vscode.workspace.registerTextDocumentContentProvider(
"rust-analyzer-diagnostics-view",
new (class implements vscode.TextDocumentContentProvider {
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
const diags = ctx.client?.diagnostics?.get(
vscode.Uri.parse(uri.fragment, true)
);
if (!diags) {
return "Unable to find original rustc diagnostic";
}
const diag = diags[parseInt(uri.query)];
if (!diag) {
return "Unable to find original rustc diagnostic";
}
const rendered = (diag as unknown as { data?: { rendered?: string } }).data
?.rendered;
return rendered ?? "Unable to find original rustc diagnostic";
}
})()
diagnostics.URI_SCHEME,
diagnosticProvider
)
);
const decorationProvider = new diagnostics.AnsiDecorationProvider(ctx);
ctx.pushExtCleanup(decorationProvider);
async function decorateVisibleEditors(document: vscode.TextDocument) {
for (const editor of vscode.window.visibleTextEditors) {
if (document === editor.document) {
await decorationProvider.provideDecorations(editor);
}
}
}
vscode.workspace.onDidChangeTextDocument(
async (event) => await decorateVisibleEditors(event.document),
null,
ctx.subscriptions
);
vscode.workspace.onDidOpenTextDocument(decorateVisibleEditors, null, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(
async (editor) => {
if (editor) {
diagnosticProvider.triggerUpdate(editor.document.uri);
await decorateVisibleEditors(editor.document);
}
},
null,
ctx.subscriptions
);
vscode.window.onDidChangeVisibleTextEditors(
async (visibleEditors) => {
for (const editor of visibleEditors) {
diagnosticProvider.triggerUpdate(editor.document.uri);
await decorationProvider.provideDecorations(editor);
}
},
null,
ctx.subscriptions
);
vscode.workspace.onDidChangeWorkspaceFolders(
async (_) => ctx.onWorkspaceFolderChanges(),
null,