Skip to content

feat: Allow viewing the full compiler diagnostic in a readonly textview #13633

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 2 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions crates/rust-analyzer/src/diagnostics/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,15 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
.iter()
.flat_map(|primary_span| {
let primary_location = primary_location(config, workspace_root, primary_span, snap);

let mut message = message.clone();
if needs_primary_span_label {
if let Some(primary_span_label) = &primary_span.label {
format_to!(message, "\n{}", primary_span_label);
let message = {
let mut message = message.clone();
if needs_primary_span_label {
if let Some(primary_span_label) = &primary_span.label {
format_to!(message, "\n{}", primary_span_label);
}
}
}

message
};
// Each primary diagnostic span may result in multiple LSP diagnostics.
let mut diagnostics = Vec::new();

Expand Down Expand Up @@ -417,7 +418,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
message: message.clone(),
related_information: Some(information_for_additional_diagnostic),
tags: if tags.is_empty() { None } else { Some(tags.clone()) },
data: None,
data: Some(serde_json::json!({ "rendered": rd.rendered })),
};
diagnostics.push(MappedRustDiagnostic {
url: secondary_location.uri,
Expand Down Expand Up @@ -449,7 +450,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
}
},
tags: if tags.is_empty() { None } else { Some(tags.clone()) },
data: None,
data: Some(serde_json::json!({ "rendered": rd.rendered })),
},
fix: None,
});
Expand Down Expand Up @@ -534,7 +535,8 @@ mod tests {
Config::new(workspace_root.to_path_buf(), ClientCapabilities::default()),
);
let snap = state.snapshot();
let actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root, &snap);
let mut actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root, &snap);
actual.iter_mut().for_each(|diag| diag.diagnostic.data = None);
expect.assert_debug_eq(&actual)
}

Expand Down
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.diagnostics.previewRustcOutput": {
"markdownDescription": "Whether to show the main part of the rendered rustc output of a diagnostic message.",
"default": false,
"type": "boolean"
},
"$generated-start": {},
"rust-analyzer.assist.emitMustUse": {
"markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.",
Expand Down
42 changes: 40 additions & 2 deletions editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ra from "../src/lsp_ext";
import * as Is from "vscode-languageclient/lib/common/utils/is";
import { assert } from "./util";
import { WorkspaceEdit } from "vscode";
import { substituteVSCodeVariables } from "./config";
import { Config, substituteVSCodeVariables } from "./config";
import { randomUUID } from "crypto";

export interface Env {
Expand Down Expand Up @@ -66,7 +66,8 @@ export async function createClient(
traceOutputChannel: vscode.OutputChannel,
outputChannel: vscode.OutputChannel,
initializationOptions: vscode.WorkspaceConfiguration,
serverOptions: lc.ServerOptions
serverOptions: lc.ServerOptions,
config: Config
): Promise<lc.LanguageClient> {
const clientOptions: lc.LanguageClientOptions = {
documentSelector: [{ scheme: "file", language: "rust" }],
Expand Down Expand Up @@ -99,6 +100,43 @@ export async function createClient(
}
},
},
async handleDiagnostics(
uri: vscode.Uri,
diagnostics: vscode.Diagnostic[],
next: lc.HandleDiagnosticsSignature
) {
const preview = config.previewRustcOutput;
diagnostics.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:)

// We encode the rendered output of a rustc diagnostic in the rendered field of
// the data payload of the lsp diagnostic. If that field exists, overwrite the
// diagnostic code such that clicking it opens the diagnostic in a readonly
// text editor for easy inspection
const rendered = (diag as unknown as { data?: { rendered?: string } }).data
?.rendered;
if (rendered) {
if (preview) {
const index = rendered.match(/^(note|help):/m)?.index || 0;
diag.message = rendered
.substring(0, index)
.replace(/^ -->[^\n]+\n/m, "");
}
diag.code = {
target: vscode.Uri.from({
scheme: "rust-analyzer-diagnostics-view",
path: "/diagnostic message",
fragment: uri.toString(),
query: idx.toString(),
}),
value: "Click for full compiler diagnostic",
};
}
});
return next(uri, diagnostics);
},
async provideHover(
document: vscode.TextDocument,
position: vscode.Position,
Expand Down
3 changes: 3 additions & 0 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export class Config {
gotoTypeDef: this.get<boolean>("hover.actions.gotoTypeDef.enable"),
};
}
get previewRustcOutput() {
return this.get<boolean>("diagnostics.previewRustcOutput");
}
}

const VarRegex = new RegExp(/\$\{(.+?)\}/g);
Expand Down
3 changes: 2 additions & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export class Ctx {
this.traceOutputChannel,
this.outputChannel,
initializationOptions,
serverOptions
serverOptions,
this.config
);
this.pushClientCleanup(
this._client.onNotification(ra.serverStatus, (params) =>
Expand Down
24 changes: 24 additions & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
ctx.pushExtCleanup(activateTaskProvider(ctx.config));
}

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";
}
})()
)
);

vscode.workspace.onDidChangeWorkspaceFolders(
async (_) => ctx.onWorkspaceFolderChanges(),
null,
Expand Down