Skip to content

Commit cf22f02

Browse files
committed
fix: Fix focus range being discarded in attributes/derives when upmapping
1 parent 426d284 commit cf22f02

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

crates/hir-expand/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ pub enum MacroCallKind {
220220
},
221221
Attr {
222222
ast_id: AstId<ast::Item>,
223+
// FIXME: This is being interned, subtrees can very quickly differ just slightly causing
224+
// leakage problems here
223225
attr_args: Option<Arc<tt::Subtree>>,
224226
/// Syntactical index of the invoking `#[attribute]`.
225227
///

crates/ide/src/navigation_target.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,8 @@ fn orig_range_with_focus(
724724
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
725725
let Some(name) = name else { return orig_range(db, hir_file, value) };
726726

727-
let call_range = || {
728-
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
729-
.kind
730-
.original_call_range(db)
731-
};
727+
let call_kind =
728+
|| db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id).kind;
732729

733730
let def_range = || {
734731
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
@@ -755,7 +752,22 @@ fn orig_range_with_focus(
755752
}
756753
// name lies outside the node, so instead point to the macro call which
757754
// *should* contain the name
758-
_ => call_range(),
755+
_ => {
756+
let kind = call_kind();
757+
let range = kind.clone().original_call_range_with_body(db);
758+
//If the focus range is in the attribute/derive body, we
759+
// need to point the call site to the entire body, if not, fall back
760+
// to the name range of the attribute/derive call
761+
// FIXME: Do this differently, this is very inflexible the caller
762+
// should choose this behavior
763+
if range.file_id == focus_range.file_id
764+
&& range.range.contains_range(focus_range.range)
765+
{
766+
range
767+
} else {
768+
kind.original_call_range(db)
769+
}
770+
}
759771
},
760772
Some(focus_range),
761773
),
@@ -784,7 +796,7 @@ fn orig_range_with_focus(
784796
// node is in macro def, just show the focus
785797
_ => (
786798
// show the macro call
787-
(call_range(), None),
799+
(call_kind().original_call_range(db), None),
788800
Some((focus_range, Some(focus_range))),
789801
),
790802
}

0 commit comments

Comments
 (0)