]> git.lizzy.rs Git - rust.git/commitdiff
Fix diagnostic code
authorAlex Veber <alexveber@gmail.com>
Tue, 27 Dec 2022 06:13:38 +0000 (08:13 +0200)
committerAlex Veber <alexveber@gmail.com>
Tue, 3 Jan 2023 06:33:27 +0000 (08:33 +0200)
editors/code/package.json
editors/code/src/client.ts
editors/code/src/config.ts

index fad67ce80310fe94abb86c05bc9f593d8712aafb..91532b3e96a8e8f9788d7221c87b45cb87d5ffe7 100644 (file)
                     "default": false,
                     "type": "boolean"
                 },
+                "rust-analyzer.diagnostics.useRustcErrorCode": {
+                    "markdownDescription": "Whether to use the rustc error code.",
+                    "default": false,
+                    "type": "boolean"
+                },
                 "$generated-start": {},
                 "rust-analyzer.assist.emitMustUse": {
                     "markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.",
index 23e039722ee334ac1634d2c5a0093e2ce1a4ea26..1470c1f581d41af3f3d547c1008a31a0535004d8 100644 (file)
@@ -106,6 +106,7 @@ export async function createClient(
                 next: lc.HandleDiagnosticsSignature
             ) {
                 const preview = config.previewRustcOutput;
+                const errorCode = config.useRustcErrorCode;
                 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
@@ -119,11 +120,20 @@ export async function createClient(
                         ?.rendered;
                     if (rendered) {
                         if (preview) {
-                            const index = rendered.match(/^(note|help):/m)?.index || 0;
+                            const index =
+                                rendered.match(/^(note|help):/m)?.index || rendered.length;
                             diag.message = rendered
                                 .substring(0, index)
                                 .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 = {
                             target: vscode.Uri.from({
                                 scheme: "rust-analyzer-diagnostics-view",
@@ -131,7 +141,7 @@ export async function createClient(
                                 fragment: uri.toString(),
                                 query: idx.toString(),
                             }),
-                            value: "Click for full compiler diagnostic",
+                            value: value ?? "Click for full compiler diagnostic",
                         };
                     }
                 });
index d8dbd1df16dfbceecee1e7249dec38046ccbe41d..eb4f965291fe59bfb907fcef06d912bdfa96b372 100644 (file)
@@ -241,6 +241,10 @@ export class Config {
     get previewRustcOutput() {
         return this.get<boolean>("diagnostics.previewRustcOutput");
     }
+
+    get useRustcErrorCode() {
+        return this.get<boolean>("diagnostics.useRustcErrorCode");
+    }
 }
 
 const VarRegex = new RegExp(/\$\{(.+?)\}/g);