Skip to content

Commit a8b8ee8

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

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-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

+21-7
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,12 @@ fn orig_range_with_focus(
722722
value: &SyntaxNode,
723723
name: Option<impl AstNode>,
724724
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
725+
value;
726+
name.as_ref().map(|it| it.syntax());
725727
let Some(name) = name else { return orig_range(db, hir_file, value) };
726728

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-
};
729+
let call_kind =
730+
|| db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id).kind;
732731

733732
let def_range = || {
734733
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
@@ -755,7 +754,22 @@ fn orig_range_with_focus(
755754
}
756755
// name lies outside the node, so instead point to the macro call which
757756
// *should* contain the name
758-
_ => call_range(),
757+
_ => {
758+
let kind = call_kind();
759+
let range = kind.clone().original_call_range_with_body(db);
760+
//If the focus range is in the attribute/derive body, we
761+
// need to point the call site to the entire body, if not, fall back
762+
// to the name range of the attribute/derive call
763+
// FIXME: Do this differently, this is very inflexible the caller
764+
// should choose this behavior
765+
if range.file_id == focus_range.file_id
766+
&& range.range.contains_range(focus_range.range)
767+
{
768+
range
769+
} else {
770+
kind.original_call_range(db)
771+
}
772+
}
759773
},
760774
Some(focus_range),
761775
),
@@ -784,7 +798,7 @@ fn orig_range_with_focus(
784798
// node is in macro def, just show the focus
785799
_ => (
786800
// show the macro call
787-
(call_range(), None),
801+
(call_kind().original_call_range(db), None),
788802
Some((focus_range, Some(focus_range))),
789803
),
790804
}

0 commit comments

Comments
 (0)