Skip to content

fix: Fix focus range being discarded in attributes/derives when upmapping #16234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ pub enum MacroCallKind {
},
Attr {
ast_id: AstId<ast::Item>,
// FIXME: This is being interned, subtrees can very quickly differ just slightly causing
// leakage problems here
attr_args: Option<Arc<tt::Subtree>>,
/// Syntactical index of the invoking `#[attribute]`.
///
Expand Down
26 changes: 19 additions & 7 deletions crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,8 @@ fn orig_range_with_focus(
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
let Some(name) = name else { return orig_range(db, hir_file, value) };

let call_range = || {
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
.kind
.original_call_range(db)
};
let call_kind =
|| db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id).kind;

let def_range = || {
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
Expand All @@ -755,7 +752,22 @@ fn orig_range_with_focus(
}
// name lies outside the node, so instead point to the macro call which
// *should* contain the name
_ => call_range(),
_ => {
let kind = call_kind();
let range = kind.clone().original_call_range_with_body(db);
//If the focus range is in the attribute/derive body, we
// need to point the call site to the entire body, if not, fall back
// to the name range of the attribute/derive call
// FIXME: Do this differently, this is very inflexible the caller
// should choose this behavior
if range.file_id == focus_range.file_id
&& range.range.contains_range(focus_range.range)
{
range
} else {
kind.original_call_range(db)
}
}
},
Some(focus_range),
),
Expand Down Expand Up @@ -784,7 +796,7 @@ fn orig_range_with_focus(
// node is in macro def, just show the focus
_ => (
// show the macro call
(call_range(), None),
(call_kind().original_call_range(db), None),
Some((focus_range, Some(focus_range))),
),
}
Expand Down