@@ -4,7 +4,7 @@ import * as ra from "../src/lsp_ext";
4
4
import * as Is from "vscode-languageclient/lib/common/utils/is" ;
5
5
import { assert } from "./util" ;
6
6
import { WorkspaceEdit } from "vscode" ;
7
- import { substituteVSCodeVariables } from "./config" ;
7
+ import { Config , substituteVSCodeVariables } from "./config" ;
8
8
import { randomUUID } from "crypto" ;
9
9
10
10
export interface Env {
@@ -66,7 +66,8 @@ export async function createClient(
66
66
traceOutputChannel : vscode . OutputChannel ,
67
67
outputChannel : vscode . OutputChannel ,
68
68
initializationOptions : vscode . WorkspaceConfiguration ,
69
- serverOptions : lc . ServerOptions
69
+ serverOptions : lc . ServerOptions ,
70
+ config : Config
70
71
) : Promise < lc . LanguageClient > {
71
72
const clientOptions : lc . LanguageClientOptions = {
72
73
documentSelector : [ { scheme : "file" , language : "rust" } ] ,
@@ -99,6 +100,43 @@ export async function createClient(
99
100
}
100
101
} ,
101
102
} ,
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
+ } ,
102
140
async provideHover (
103
141
document : vscode . TextDocument ,
104
142
position : vscode . Position ,
0 commit comments