Skip to content

Commit dc8254c

Browse files
committed
fix: Fix nested macro diagnostics pointing at macro expansion files
1 parent 52bc15f commit dc8254c

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

crates/ide-diagnostics/src/handlers/macro_error.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::{Diagnostic, DiagnosticsContext};
55
// This diagnostic is shown for macro expansion errors.
66
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
77
// Use more accurate position if available.
8-
let display_range = d
9-
.precise_location
10-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
11-
8+
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
129
Diagnostic::new("macro-error", d.message.clone(), display_range).experimental()
1310
}
1411

crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pub(crate) fn unresolved_macro_call(
99
d: &hir::UnresolvedMacroCall,
1010
) -> Diagnostic {
1111
// Use more accurate position if available.
12-
let display_range = d
13-
.precise_location
14-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.macro_call.clone()).range);
15-
12+
let display_range = ctx.resolve_precise_location(&d.macro_call, d.precise_location);
1613
let bang = if d.is_bang { "!" } else { "" };
1714
Diagnostic::new(
1815
"unresolved-macro-call",

crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use hir::db::DefDatabase;
2-
use syntax::NodeOrToken;
32

43
use crate::{Diagnostic, DiagnosticsContext, Severity};
54

@@ -19,16 +18,7 @@ pub(crate) fn unresolved_proc_macro(
1918
proc_attr_macros_enabled: bool,
2019
) -> Diagnostic {
2120
// Use more accurate position if available.
22-
let display_range = (|| {
23-
let precise_location = d.precise_location?;
24-
let root = ctx.sema.parse_or_expand(d.node.file_id)?;
25-
match root.covering_element(precise_location) {
26-
NodeOrToken::Node(it) => Some(ctx.sema.original_range(&it)),
27-
NodeOrToken::Token(it) => d.node.with_value(it).original_file_range_opt(ctx.sema.db),
28-
}
29-
})()
30-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()))
31-
.range;
21+
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
3222

3323
let config_enabled = match d.kind {
3424
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,

crates/ide-diagnostics/src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ struct DiagnosticsContext<'a> {
182182
resolve: &'a AssistResolveStrategy,
183183
}
184184

185+
impl<'a> DiagnosticsContext<'a> {
186+
fn resolve_precise_location(
187+
&self,
188+
node: &InFile<SyntaxNodePtr>,
189+
precise_location: Option<TextRange>,
190+
) -> TextRange {
191+
let sema = &self.sema;
192+
(|| {
193+
let precise_location = precise_location?;
194+
let root = sema.parse_or_expand(node.file_id)?;
195+
match root.covering_element(precise_location) {
196+
syntax::NodeOrToken::Node(it) => Some(sema.original_range(&it)),
197+
syntax::NodeOrToken::Token(it) => {
198+
node.with_value(it).original_file_range_opt(sema.db)
199+
}
200+
}
201+
})()
202+
.unwrap_or_else(|| sema.diagnostics_display_range(node.clone()))
203+
.range
204+
}
205+
}
206+
185207
pub fn diagnostics(
186208
db: &RootDatabase,
187209
config: &DiagnosticsConfig,

0 commit comments

Comments
 (0)