Skip to content

Generate annotations for macro defined items if their name is in the input #19990

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
Jun 13, 2025
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
82 changes: 75 additions & 7 deletions crates/ide/src/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
NavigationTarget, RunnableKind,
annotations::fn_references::find_all_methods,
goto_implementation::goto_implementation,
navigation_target,
references::find_all_refs,
runnables::{Runnable, runnables},
};
Expand Down Expand Up @@ -148,15 +149,32 @@ pub(crate) fn annotations(
node: InFile<T>,
source_file_id: FileId,
) -> Option<(TextRange, Option<TextRange>)> {
if let Some(InRealFile { file_id, value }) = node.original_ast_node_rooted(db) {
if file_id.file_id(db) == source_file_id {
return Some((
value.syntax().text_range(),
value.name().map(|name| name.syntax().text_range()),
));
if let Some(name) = node.value.name().map(|name| name.syntax().text_range()) {
// if we have a name, try mapping that out of the macro expansion as we can put the
// annotation on that name token
// See `test_no_annotations_macro_struct_def` vs `test_annotations_macro_struct_def_call_site`
let res = navigation_target::orig_range_with_focus_r(
db,
node.file_id,
node.value.syntax().text_range(),
Some(name),
);
if res.call_site.0.file_id == source_file_id {
if let Some(name_range) = res.call_site.1 {
return Some((res.call_site.0.range, Some(name_range)));
}
}
};
// otherwise try upmapping the entire node out of attributes
let InRealFile { file_id, value } = node.original_ast_node_rooted(db)?;
if file_id.file_id(db) == source_file_id {
Some((
value.syntax().text_range(),
value.name().map(|name| name.syntax().text_range()),
))
} else {
None
}
None
}
});

Expand Down Expand Up @@ -913,6 +931,56 @@ m!();
);
}

#[test]
fn test_annotations_macro_struct_def_call_site() {
check(
r#"
//- /lib.rs
macro_rules! m {
($name:ident) => {
struct $name {}
};
}

m! {
Name
};
"#,
expect![[r#"
[
Annotation {
range: 83..87,
kind: HasImpls {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 83,
},
data: Some(
[],
),
},
},
Annotation {
range: 83..87,
kind: HasReferences {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 83,
},
data: Some(
[],
),
},
},
]
"#]],
);
}

#[test]
fn test_annotations_appear_above_whole_item_when_configured_to_do_so() {
check_with_config(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ pub(crate) fn orig_range_with_focus_r(
}

// def site name
// FIXME: This can be de improved
// FIXME: This can be improved
Some((focus_range, _ctxt)) => {
match value_range {
// but overall node is in macro input
Expand Down