diff options
-rw-r--r-- | crates/hir_expand/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/ide/src/annotations.rs | 45 |
2 files changed, 44 insertions, 8 deletions
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index b8045fda9..d7391ebad 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -207,6 +207,13 @@ impl HirFileId { | |||
207 | } | 207 | } |
208 | false | 208 | false |
209 | } | 209 | } |
210 | |||
211 | pub fn is_macro_file(&self) -> bool { | ||
212 | match self.0 { | ||
213 | HirFileIdRepr::MacroFile(_) => true, | ||
214 | HirFileIdRepr::FileId(_) => false, | ||
215 | } | ||
216 | } | ||
210 | } | 217 | } |
211 | 218 | ||
212 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 219 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 72492f826..9e78ed6a0 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use either::Either; | 1 | use either::Either; |
2 | use hir::{HasSource, Semantics}; | 2 | use hir::{HasSource, InFile, Semantics}; |
3 | use ide_db::{ | 3 | use ide_db::{ |
4 | base_db::{FileId, FilePosition, FileRange}, | 4 | base_db::{FileId, FilePosition, FileRange}, |
5 | helpers::visit_file_defs, | 5 | helpers::visit_file_defs, |
@@ -80,19 +80,19 @@ pub(crate) fn annotations( | |||
80 | Either::Left(def) => { | 80 | Either::Left(def) => { |
81 | let node = match def { | 81 | let node = match def { |
82 | hir::ModuleDef::Const(konst) => { | 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)) |
84 | } | 84 | } |
85 | hir::ModuleDef::Trait(trait_) => { | 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)) |
87 | } | 87 | } |
88 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { | 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)) |
90 | } | 90 | } |
91 | hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { | 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)) |
93 | } | 93 | } |
94 | hir::ModuleDef::Adt(hir::Adt::Union(union)) => { | 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)) |
96 | } | 96 | } |
97 | _ => None, | 97 | _ => None, |
98 | }; | 98 | }; |
@@ -120,8 +120,18 @@ pub(crate) fn annotations( | |||
120 | }); | 120 | }); |
121 | } | 121 | } |
122 | 122 | ||
123 | fn range_and_position_of(node: &dyn NameOwner) -> Option<(TextSize, TextRange)> { | 123 | fn range_and_position_of<T: NameOwner>( |
124 | Some((node.name()?.syntax().text_range().start(), node.syntax().text_range())) | 124 | node: &InFile<T>, |
125 | ) -> Option<(TextSize, TextRange)> { | ||
126 | if node.file_id.is_macro_file() { | ||
127 | // Macro generated files should not contain annotations. | ||
128 | None | ||
129 | } else { | ||
130 | Some(( | ||
131 | node.value.name()?.syntax().text_range().start(), | ||
132 | node.value.syntax().text_range(), | ||
133 | )) | ||
134 | } | ||
125 | } | 135 | } |
126 | } | 136 | } |
127 | Either::Right(_) => (), | 137 | Either::Right(_) => (), |
@@ -967,4 +977,23 @@ struct Foo; | |||
967 | "#]], | 977 | "#]], |
968 | ); | 978 | ); |
969 | } | 979 | } |
980 | |||
981 | #[test] | ||
982 | fn test_no_annotations_macro_struct_def() { | ||
983 | check( | ||
984 | r#" | ||
985 | //- /lib.rs | ||
986 | macro_rules! m { | ||
987 | () => { | ||
988 | struct A {} | ||
989 | }; | ||
990 | } | ||
991 | |||
992 | m!(); | ||
993 | "#, | ||
994 | expect![[r#" | ||
995 | [] | ||
996 | "#]], | ||
997 | ); | ||
998 | } | ||
970 | } | 999 | } |