1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
import * as path from 'path';
import * as vscode from 'vscode';
// Reference:
// https://github.com/rust-lang/rust/blob/master/src/libsyntax/json.rs
export interface RustDiagnosticSpan {
line_start: number;
line_end: number;
column_start: number;
column_end: number;
is_primary: boolean;
file_name: string;
label?: string;
suggested_replacement?: string;
suggestion_applicability?:
| 'MachineApplicable'
| 'HasPlaceholders'
| 'MaybeIncorrect'
| 'Unspecified';
}
export interface RustDiagnostic {
spans: RustDiagnosticSpan[];
rendered: string;
message: string;
level: string;
code?: {
code: string;
};
children: RustDiagnostic[];
}
export interface MappedRustDiagnostic {
location: vscode.Location;
diagnostic: vscode.Diagnostic;
codeActions: vscode.CodeAction[];
}
interface MappedRustChildDiagnostic {
related?: vscode.DiagnosticRelatedInformation;
codeAction?: vscode.CodeAction;
messageLine?: string;
}
/**
* Converts a Rust level string to a VsCode severity
*/
function mapLevelToSeverity(s: string): vscode.DiagnosticSeverity {
if (s === 'error') {
return vscode.DiagnosticSeverity.Error;
}
if (s.startsWith('warn')) {
return vscode.DiagnosticSeverity.Warning;
}
return vscode.DiagnosticSeverity.Information;
}
/**
* Converts a Rust span to a VsCode location
*/
function mapSpanToLocation(span: RustDiagnosticSpan): vscode.Location {
const fileName = path.join(vscode.workspace.rootPath!, span.file_name);
const fileUri = vscode.Uri.file(fileName);
const range = new vscode.Range(
new vscode.Position(span.line_start - 1, span.column_start - 1),
new vscode.Position(span.line_end - 1, span.column_end - 1)
);
return new vscode.Location(fileUri, range);
}
/**
* Converts a secondary Rust span to a VsCode related information
*
* If the span is unlabelled this will return `undefined`.
*/
function mapSecondarySpanToRelated(
span: RustDiagnosticSpan
): vscode.DiagnosticRelatedInformation | undefined {
if (!span.label) {
// Nothing to label this with
return;
}
const location = mapSpanToLocation(span);
return new vscode.DiagnosticRelatedInformation(location, span.label);
}
/**
* Determines if diagnostic is related to unused code
*/
function isUnusedOrUnnecessary(rd: RustDiagnostic): boolean {
if (!rd.code) {
return false;
}
return [
'dead_code',
'unknown_lints',
'unused_attributes',
'unused_imports',
'unused_macros',
'unused_variables'
].includes(rd.code.code);
}
/**
* Converts a Rust child diagnostic to a VsCode related information
*
* This can have three outcomes:
*
* 1. If this is no primary span this will return a `noteLine`
* 2. If there is a primary span with a suggested replacement it will return a
* `codeAction`.
* 3. If there is a primary span without a suggested replacement it will return
* a `related`.
*/
function mapRustChildDiagnostic(rd: RustDiagnostic): MappedRustChildDiagnostic {
const span = rd.spans.find(s => s.is_primary);
if (!span) {
// `rustc` uses these spanless children as a way to print multi-line
// messages
return { messageLine: rd.message };
}
// If we have a primary span use its location, otherwise use the parent
const location = mapSpanToLocation(span);
// We need to distinguish `null` from an empty string
if (span && typeof span.suggested_replacement === 'string') {
const edit = new vscode.WorkspaceEdit();
edit.replace(location.uri, location.range, span.suggested_replacement);
// Include our replacement in the label unless it's empty
const title = span.suggested_replacement
? `${rd.message}: \`${span.suggested_replacement}\``
: rd.message;
const codeAction = new vscode.CodeAction(
title,
vscode.CodeActionKind.QuickFix
);
codeAction.edit = edit;
codeAction.isPreferred =
span.suggestion_applicability === 'MachineApplicable';
return { codeAction };
} else {
const related = new vscode.DiagnosticRelatedInformation(
location,
rd.message
);
return { related };
}
}
/**
* Converts a Rust root diagnostic to VsCode form
*
* This flattens the Rust diagnostic by:
*
* 1. Creating a `vscode.Diagnostic` with the root message and primary span.
* 2. Adding any labelled secondary spans to `relatedInformation`
* 3. Categorising child diagnostics as either Quick Fix actions,
* `relatedInformation` or additional message lines.
*
* If the diagnostic has no primary span this will return `undefined`
*/
export function mapRustDiagnosticToVsCode(
rd: RustDiagnostic
): MappedRustDiagnostic | undefined {
const codeActions = [];
const primarySpan = rd.spans.find(s => s.is_primary);
if (!primarySpan) {
return;
}
const location = mapSpanToLocation(primarySpan);
const secondarySpans = rd.spans.filter(s => !s.is_primary);
const severity = mapLevelToSeverity(rd.level);
const vd = new vscode.Diagnostic(location.range, rd.message, severity);
vd.source = 'rustc';
vd.code = rd.code ? rd.code.code : undefined;
vd.relatedInformation = [];
for (const secondarySpan of secondarySpans) {
const related = mapSecondarySpanToRelated(secondarySpan);
if (related) {
vd.relatedInformation.push(related);
}
}
for (const child of rd.children) {
const { related, codeAction, messageLine } = mapRustChildDiagnostic(
child
);
if (related) {
vd.relatedInformation.push(related);
}
if (codeAction) {
codeActions.push(codeAction);
}
if (messageLine) {
vd.message += `\n${messageLine}`;
}
}
if (isUnusedOrUnnecessary(rd)) {
vd.tags = [vscode.DiagnosticTag.Unnecessary];
}
return {
location,
diagnostic: vd,
codeActions
};
}
|