Skip to content

Commit d2c1f8e

Browse files
committed
Add macro span handling
1 parent ee93fac commit d2c1f8e

File tree

1 file changed

+38
-0
lines changed
  • editors/code/src/utils/diagnostics

1 file changed

+38
-0
lines changed

editors/code/src/utils/diagnostics/rust.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export enum SuggestionApplicability {
1010
Unspecified = 'Unspecified',
1111
}
1212

13+
export interface RustDiagnosticSpanMacroExpansion {
14+
span: RustDiagnosticSpan;
15+
macro_decl_name: string;
16+
def_site_span?: RustDiagnosticSpan;
17+
}
18+
1319
// Reference:
1420
// https://github.com/rust-lang/rust/blob/master/src/libsyntax/json.rs
1521
export interface RustDiagnosticSpan {
@@ -20,6 +26,7 @@ export interface RustDiagnosticSpan {
2026
is_primary: boolean;
2127
file_name: string;
2228
label?: string;
29+
expansion?: RustDiagnosticSpanMacroExpansion;
2330
suggested_replacement?: string;
2431
suggestion_applicability?: SuggestionApplicability;
2532
}
@@ -60,10 +67,41 @@ function mapLevelToSeverity(s: string): vscode.DiagnosticSeverity {
6067
return vscode.DiagnosticSeverity.Information;
6168
}
6269

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+
6394
/**
6495
* Converts a Rust span to a VsCode location
6596
*/
6697
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+
67105
const fileName = path.join(vscode.workspace.rootPath || '', span.file_name);
68106
const fileUri = vscode.Uri.file(fileName);
69107

0 commit comments

Comments
 (0)