mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-29 19:17:12 +00:00
Merge pull request #19990 from Veykril/push-zkvrlrotmuzr
Generate annotations for macro defined items if their name is in the input
This commit is contained in:
commit
c0258d01ba
2 changed files with 76 additions and 8 deletions
|
|
@ -10,6 +10,7 @@ use crate::{
|
||||||
NavigationTarget, RunnableKind,
|
NavigationTarget, RunnableKind,
|
||||||
annotations::fn_references::find_all_methods,
|
annotations::fn_references::find_all_methods,
|
||||||
goto_implementation::goto_implementation,
|
goto_implementation::goto_implementation,
|
||||||
|
navigation_target,
|
||||||
references::find_all_refs,
|
references::find_all_refs,
|
||||||
runnables::{Runnable, runnables},
|
runnables::{Runnable, runnables},
|
||||||
};
|
};
|
||||||
|
|
@ -148,15 +149,32 @@ pub(crate) fn annotations(
|
||||||
node: InFile<T>,
|
node: InFile<T>,
|
||||||
source_file_id: FileId,
|
source_file_id: FileId,
|
||||||
) -> Option<(TextRange, Option<TextRange>)> {
|
) -> Option<(TextRange, Option<TextRange>)> {
|
||||||
if let Some(InRealFile { file_id, value }) = node.original_ast_node_rooted(db) {
|
if let Some(name) = node.value.name().map(|name| name.syntax().text_range()) {
|
||||||
if file_id.file_id(db) == source_file_id {
|
// if we have a name, try mapping that out of the macro expansion as we can put the
|
||||||
return Some((
|
// annotation on that name token
|
||||||
value.syntax().text_range(),
|
// See `test_no_annotations_macro_struct_def` vs `test_annotations_macro_struct_def_call_site`
|
||||||
value.name().map(|name| name.syntax().text_range()),
|
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
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -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]
|
#[test]
|
||||||
fn test_annotations_appear_above_whole_item_when_configured_to_do_so() {
|
fn test_annotations_appear_above_whole_item_when_configured_to_do_so() {
|
||||||
check_with_config(
|
check_with_config(
|
||||||
|
|
|
||||||
|
|
@ -867,7 +867,7 @@ pub(crate) fn orig_range_with_focus_r(
|
||||||
}
|
}
|
||||||
|
|
||||||
// def site name
|
// def site name
|
||||||
// FIXME: This can be de improved
|
// FIXME: This can be improved
|
||||||
Some((focus_range, _ctxt)) => {
|
Some((focus_range, _ctxt)) => {
|
||||||
match value_range {
|
match value_range {
|
||||||
// but overall node is in macro input
|
// but overall node is in macro input
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue