|
1 | 1 | use either::Either;
|
2 |
| -use hir::{HasSource, Semantics}; |
| 2 | +use hir::{HasSource, InFile, Semantics}; |
3 | 3 | use ide_db::{
|
4 | 4 | base_db::{FileId, FilePosition, FileRange},
|
5 | 5 | helpers::visit_file_defs,
|
@@ -80,19 +80,19 @@ pub(crate) fn annotations(
|
80 | 80 | Either::Left(def) => {
|
81 | 81 | let node = match def {
|
82 | 82 | hir::ModuleDef::Const(konst) => {
|
83 |
| - konst.source(db).and_then(|node| range_and_position_of(&node.value)) |
| 83 | + konst.source(db).and_then(|node| range_and_position_of(&node, file_id)) |
84 | 84 | }
|
85 | 85 | hir::ModuleDef::Trait(trait_) => {
|
86 |
| - trait_.source(db).and_then(|node| range_and_position_of(&node.value)) |
| 86 | + trait_.source(db).and_then(|node| range_and_position_of(&node, file_id)) |
87 | 87 | }
|
88 | 88 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
|
89 |
| - strukt.source(db).and_then(|node| range_and_position_of(&node.value)) |
| 89 | + strukt.source(db).and_then(|node| range_and_position_of(&node, file_id)) |
90 | 90 | }
|
91 | 91 | hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => {
|
92 |
| - enum_.source(db).and_then(|node| range_and_position_of(&node.value)) |
| 92 | + enum_.source(db).and_then(|node| range_and_position_of(&node, file_id)) |
93 | 93 | }
|
94 | 94 | hir::ModuleDef::Adt(hir::Adt::Union(union)) => {
|
95 |
| - union.source(db).and_then(|node| range_and_position_of(&node.value)) |
| 95 | + union.source(db).and_then(|node| range_and_position_of(&node, file_id)) |
96 | 96 | }
|
97 | 97 | _ => None,
|
98 | 98 | };
|
@@ -120,8 +120,19 @@ pub(crate) fn annotations(
|
120 | 120 | });
|
121 | 121 | }
|
122 | 122 |
|
123 |
| - fn range_and_position_of(node: &dyn NameOwner) -> Option<(TextSize, TextRange)> { |
124 |
| - Some((node.name()?.syntax().text_range().start(), node.syntax().text_range())) |
| 123 | + fn range_and_position_of<T: NameOwner>( |
| 124 | + node: &InFile<T>, |
| 125 | + file_id: FileId, |
| 126 | + ) -> Option<(TextSize, TextRange)> { |
| 127 | + if node.file_id != file_id.into() { |
| 128 | + // Node is outside the file we are adding annotations to (e.g. macros). |
| 129 | + None |
| 130 | + } else { |
| 131 | + Some(( |
| 132 | + node.value.name()?.syntax().text_range().start(), |
| 133 | + node.value.syntax().text_range(), |
| 134 | + )) |
| 135 | + } |
125 | 136 | }
|
126 | 137 | }
|
127 | 138 | Either::Right(_) => (),
|
@@ -961,6 +972,25 @@ mod tests {
|
961 | 972 | struct Foo;
|
962 | 973 | //- /lib.rs
|
963 | 974 | // this file comes last since `check` checks the first file only
|
| 975 | +"#, |
| 976 | + expect![[r#" |
| 977 | + [] |
| 978 | + "#]], |
| 979 | + ); |
| 980 | + } |
| 981 | + |
| 982 | + #[test] |
| 983 | + fn test_no_annotations_macro_struct_def() { |
| 984 | + check( |
| 985 | + r#" |
| 986 | +//- /lib.rs |
| 987 | +macro_rules! m { |
| 988 | + () => { |
| 989 | + struct A {} |
| 990 | + }; |
| 991 | +} |
| 992 | +
|
| 993 | +m!(); |
964 | 994 | "#,
|
965 | 995 | expect![[r#"
|
966 | 996 | []
|
|
0 commit comments