@@ -4,7 +4,7 @@ import * as ra from "../src/lsp_ext";
44import * as Is from "vscode-languageclient/lib/common/utils/is" ;
55import { assert } from "./util" ;
66import { WorkspaceEdit } from "vscode" ;
7- import { substituteVSCodeVariables } from "./config" ;
7+ import { Config , substituteVSCodeVariables } from "./config" ;
88import { randomUUID } from "crypto" ;
99
1010export interface Env {
@@ -66,7 +66,8 @@ export async function createClient(
6666 traceOutputChannel : vscode . OutputChannel ,
6767 outputChannel : vscode . OutputChannel ,
6868 initializationOptions : vscode . WorkspaceConfiguration ,
69- serverOptions : lc . ServerOptions
69+ serverOptions : lc . ServerOptions ,
70+ config : Config
7071) : Promise < lc . LanguageClient > {
7172 const clientOptions : lc . LanguageClientOptions = {
7273 documentSelector : [ { scheme : "file" , language : "rust" } ] ,
@@ -99,6 +100,43 @@ export async function createClient(
99100 }
100101 } ,
101102 } ,
103+ async handleDiagnostics (
104+ uri : vscode . Uri ,
105+ diagnostics : vscode . Diagnostic [ ] ,
106+ next : lc . HandleDiagnosticsSignature
107+ ) {
108+ const preview = config . previewRustcOutput ;
109+ diagnostics . forEach ( ( diag , idx ) => {
110+ // Abuse the fact that VSCode leaks the LSP diagnostics data field through the
111+ // Diagnostic class, if they ever break this we are out of luck and have to go
112+ // back to the worst diagnostics experience ever:)
113+
114+ // We encode the rendered output of a rustc diagnostic in the rendered field of
115+ // the data payload of the lsp diagnostic. If that field exists, overwrite the
116+ // diagnostic code such that clicking it opens the diagnostic in a readonly
117+ // text editor for easy inspection
118+ const rendered = ( diag as unknown as { data ?: { rendered ?: string } } ) . data
119+ ?. rendered ;
120+ if ( rendered ) {
121+ if ( preview ) {
122+ const index = rendered . match ( / ^ ( n o t e | h e l p ) : / m) ?. index || 0 ;
123+ diag . message = rendered
124+ . substring ( 0 , index )
125+ . replace ( / ^ - - > [ ^ \n ] + \n / m, "" ) ;
126+ }
127+ diag . code = {
128+ target : vscode . Uri . from ( {
129+ scheme : "rust-analyzer-diagnostics-view" ,
130+ path : "/diagnostic message" ,
131+ fragment : uri . toString ( ) ,
132+ query : idx . toString ( ) ,
133+ } ) ,
134+ value : "Click for full compiler diagnostic" ,
135+ } ;
136+ }
137+ } ) ;
138+ return next ( uri , diagnostics ) ;
139+ } ,
102140 async provideHover (
103141 document : vscode . TextDocument ,
104142 position : vscode . Position ,
0 commit comments