Auto merge of #13853 - veber-alex:diag_fix, r=Veykril

Use diagnostic code as link to full message

fixes #13823 by adding a vscode setting that will keeping the existing diagnostic code and use it as a link to the full compiler error message.
While I was there I also fixed `index` to fallback to `rendered.length` to make the previewRustcOutput feature work.
This commit is contained in:
bors 2023-01-03 09:40:20 +00:00
commit 50801b7d6a
3 changed files with 21 additions and 2 deletions

View File

@ -411,6 +411,11 @@
"default": false, "default": false,
"type": "boolean" "type": "boolean"
}, },
"rust-analyzer.diagnostics.useRustcErrorCode": {
"markdownDescription": "Whether to use the rustc error code.",
"default": false,
"type": "boolean"
},
"$generated-start": {}, "$generated-start": {},
"rust-analyzer.assist.emitMustUse": { "rust-analyzer.assist.emitMustUse": {
"markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.", "markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.",

View File

@ -106,6 +106,7 @@ export async function createClient(
next: lc.HandleDiagnosticsSignature next: lc.HandleDiagnosticsSignature
) { ) {
const preview = config.previewRustcOutput; const preview = config.previewRustcOutput;
const errorCode = config.useRustcErrorCode;
diagnostics.forEach((diag, idx) => { diagnostics.forEach((diag, idx) => {
// Abuse the fact that VSCode leaks the LSP diagnostics data field through the // 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 // Diagnostic class, if they ever break this we are out of luck and have to go
@ -119,11 +120,20 @@ export async function createClient(
?.rendered; ?.rendered;
if (rendered) { if (rendered) {
if (preview) { if (preview) {
const index = rendered.match(/^(note|help):/m)?.index || 0; const index =
rendered.match(/^(note|help):/m)?.index || rendered.length;
diag.message = rendered diag.message = rendered
.substring(0, index) .substring(0, index)
.replace(/^ -->[^\n]+\n/m, ""); .replace(/^ -->[^\n]+\n/m, "");
} }
let value;
if (errorCode) {
if (typeof diag.code === "string" || typeof diag.code === "number") {
value = diag.code;
} else {
value = diag.code?.value;
}
}
diag.code = { diag.code = {
target: vscode.Uri.from({ target: vscode.Uri.from({
scheme: "rust-analyzer-diagnostics-view", scheme: "rust-analyzer-diagnostics-view",
@ -131,7 +141,7 @@ export async function createClient(
fragment: uri.toString(), fragment: uri.toString(),
query: idx.toString(), query: idx.toString(),
}), }),
value: "Click for full compiler diagnostic", value: value ?? "Click for full compiler diagnostic",
}; };
} }
}); });

View File

@ -241,6 +241,10 @@ export class Config {
get previewRustcOutput() { get previewRustcOutput() {
return this.get<boolean>("diagnostics.previewRustcOutput"); return this.get<boolean>("diagnostics.previewRustcOutput");
} }
get useRustcErrorCode() {
return this.get<boolean>("diagnostics.useRustcErrorCode");
}
} }
const VarRegex = new RegExp(/\$\{(.+?)\}/g); const VarRegex = new RegExp(/\$\{(.+?)\}/g);