diff options
Diffstat (limited to 'editors/code/src/utils/diagnostics/rust.ts')
-rw-r--r-- | editors/code/src/utils/diagnostics/rust.ts | 58 |
1 files changed, 48 insertions, 10 deletions
diff --git a/editors/code/src/utils/diagnostics/rust.ts b/editors/code/src/utils/diagnostics/rust.ts index 0550d0372..1f0c0d3e4 100644 --- a/editors/code/src/utils/diagnostics/rust.ts +++ b/editors/code/src/utils/diagnostics/rust.ts | |||
@@ -7,7 +7,13 @@ export enum SuggestionApplicability { | |||
7 | MachineApplicable = 'MachineApplicable', | 7 | MachineApplicable = 'MachineApplicable', |
8 | HasPlaceholders = 'HasPlaceholders', | 8 | HasPlaceholders = 'HasPlaceholders', |
9 | MaybeIncorrect = 'MaybeIncorrect', | 9 | MaybeIncorrect = 'MaybeIncorrect', |
10 | Unspecified = 'Unspecified' | 10 | Unspecified = 'Unspecified', |
11 | } | ||
12 | |||
13 | export interface RustDiagnosticSpanMacroExpansion { | ||
14 | span: RustDiagnosticSpan; | ||
15 | macro_decl_name: string; | ||
16 | def_site_span?: RustDiagnosticSpan; | ||
11 | } | 17 | } |
12 | 18 | ||
13 | // Reference: | 19 | // Reference: |
@@ -20,6 +26,7 @@ export interface RustDiagnosticSpan { | |||
20 | is_primary: boolean; | 26 | is_primary: boolean; |
21 | file_name: string; | 27 | file_name: string; |
22 | label?: string; | 28 | label?: string; |
29 | expansion?: RustDiagnosticSpanMacroExpansion; | ||
23 | suggested_replacement?: string; | 30 | suggested_replacement?: string; |
24 | suggestion_applicability?: SuggestionApplicability; | 31 | suggestion_applicability?: SuggestionApplicability; |
25 | } | 32 | } |
@@ -61,15 +68,46 @@ function mapLevelToSeverity(s: string): vscode.DiagnosticSeverity { | |||
61 | } | 68 | } |
62 | 69 | ||
63 | /** | 70 | /** |
71 | * Check whether a file name is from macro invocation | ||
72 | */ | ||
73 | function isFromMacro(fileName: string): boolean { | ||
74 | return fileName.startsWith('<') && fileName.endsWith('>'); | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Converts a Rust macro span to a VsCode location recursively | ||
79 | */ | ||
80 | function mapMacroSpanToLocation( | ||
81 | spanMacro: RustDiagnosticSpanMacroExpansion, | ||
82 | ): vscode.Location | undefined { | ||
83 | if (!isFromMacro(spanMacro.span.file_name)) { | ||
84 | return mapSpanToLocation(spanMacro.span); | ||
85 | } | ||
86 | |||
87 | if (spanMacro.span.expansion) { | ||
88 | return mapMacroSpanToLocation(spanMacro.span.expansion); | ||
89 | } | ||
90 | |||
91 | return; | ||
92 | } | ||
93 | |||
94 | /** | ||
64 | * Converts a Rust span to a VsCode location | 95 | * Converts a Rust span to a VsCode location |
65 | */ | 96 | */ |
66 | function mapSpanToLocation(span: RustDiagnosticSpan): vscode.Location { | 97 | function mapSpanToLocation(span: RustDiagnosticSpan): vscode.Location { |
98 | if (isFromMacro(span.file_name) && span.expansion) { | ||
99 | const macroLoc = mapMacroSpanToLocation(span.expansion); | ||
100 | if (macroLoc) { | ||
101 | return macroLoc; | ||
102 | } | ||
103 | } | ||
104 | |||
67 | const fileName = path.join(vscode.workspace.rootPath || '', span.file_name); | 105 | const fileName = path.join(vscode.workspace.rootPath || '', span.file_name); |
68 | const fileUri = vscode.Uri.file(fileName); | 106 | const fileUri = vscode.Uri.file(fileName); |
69 | 107 | ||
70 | const range = new vscode.Range( | 108 | const range = new vscode.Range( |
71 | new vscode.Position(span.line_start - 1, span.column_start - 1), | 109 | new vscode.Position(span.line_start - 1, span.column_start - 1), |
72 | new vscode.Position(span.line_end - 1, span.column_end - 1) | 110 | new vscode.Position(span.line_end - 1, span.column_end - 1), |
73 | ); | 111 | ); |
74 | 112 | ||
75 | return new vscode.Location(fileUri, range); | 113 | return new vscode.Location(fileUri, range); |
@@ -81,7 +119,7 @@ function mapSpanToLocation(span: RustDiagnosticSpan): vscode.Location { | |||
81 | * If the span is unlabelled this will return `undefined`. | 119 | * If the span is unlabelled this will return `undefined`. |
82 | */ | 120 | */ |
83 | function mapSecondarySpanToRelated( | 121 | function mapSecondarySpanToRelated( |
84 | span: RustDiagnosticSpan | 122 | span: RustDiagnosticSpan, |
85 | ): vscode.DiagnosticRelatedInformation | undefined { | 123 | ): vscode.DiagnosticRelatedInformation | undefined { |
86 | if (!span.label) { | 124 | if (!span.label) { |
87 | // Nothing to label this with | 125 | // Nothing to label this with |
@@ -107,7 +145,7 @@ function isUnusedOrUnnecessary(rd: RustDiagnostic): boolean { | |||
107 | 'unused_attributes', | 145 | 'unused_attributes', |
108 | 'unused_imports', | 146 | 'unused_imports', |
109 | 'unused_macros', | 147 | 'unused_macros', |
110 | 'unused_variables' | 148 | 'unused_variables', |
111 | ].includes(rd.code.code); | 149 | ].includes(rd.code.code); |
112 | } | 150 | } |
113 | 151 | ||
@@ -157,13 +195,13 @@ function mapRustChildDiagnostic(rd: RustDiagnostic): MappedRustChildDiagnostic { | |||
157 | title, | 195 | title, |
158 | location, | 196 | location, |
159 | span.suggested_replacement, | 197 | span.suggested_replacement, |
160 | span.suggestion_applicability | 198 | span.suggestion_applicability, |
161 | ) | 199 | ), |
162 | }; | 200 | }; |
163 | } else { | 201 | } else { |
164 | const related = new vscode.DiagnosticRelatedInformation( | 202 | const related = new vscode.DiagnosticRelatedInformation( |
165 | location, | 203 | location, |
166 | rd.message | 204 | rd.message, |
167 | ); | 205 | ); |
168 | 206 | ||
169 | return { related }; | 207 | return { related }; |
@@ -183,7 +221,7 @@ function mapRustChildDiagnostic(rd: RustDiagnostic): MappedRustChildDiagnostic { | |||
183 | * If the diagnostic has no primary span this will return `undefined` | 221 | * If the diagnostic has no primary span this will return `undefined` |
184 | */ | 222 | */ |
185 | export function mapRustDiagnosticToVsCode( | 223 | export function mapRustDiagnosticToVsCode( |
186 | rd: RustDiagnostic | 224 | rd: RustDiagnostic, |
187 | ): MappedRustDiagnostic | undefined { | 225 | ): MappedRustDiagnostic | undefined { |
188 | const primarySpan = rd.spans.find(s => s.is_primary); | 226 | const primarySpan = rd.spans.find(s => s.is_primary); |
189 | if (!primarySpan) { | 227 | if (!primarySpan) { |
@@ -223,7 +261,7 @@ export function mapRustDiagnosticToVsCode( | |||
223 | const suggestedFixes = []; | 261 | const suggestedFixes = []; |
224 | for (const child of rd.children) { | 262 | for (const child of rd.children) { |
225 | const { related, suggestedFix, messageLine } = mapRustChildDiagnostic( | 263 | const { related, suggestedFix, messageLine } = mapRustChildDiagnostic( |
226 | child | 264 | child, |
227 | ); | 265 | ); |
228 | 266 | ||
229 | if (related) { | 267 | if (related) { |
@@ -256,6 +294,6 @@ export function mapRustDiagnosticToVsCode( | |||
256 | return { | 294 | return { |
257 | location, | 295 | location, |
258 | diagnostic: vd, | 296 | diagnostic: vd, |
259 | suggestedFixes | 297 | suggestedFixes, |
260 | }; | 298 | }; |
261 | } | 299 | } |