From 0d063b8d212dd6c54da0aa8608154c980107bd07 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 24 Mar 2021 00:00:38 -0700 Subject: Fix MISSING: command error with macros --- crates/ide/src/annotations.rs | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'crates/ide/src') 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 @@ use either::Either; -use hir::{HasSource, Semantics}; +use hir::{HasSource, InFile, Semantics}; use ide_db::{ base_db::{FileId, FilePosition, FileRange}, helpers::visit_file_defs, @@ -80,19 +80,19 @@ pub(crate) fn annotations( Either::Left(def) => { let node = match def { hir::ModuleDef::Const(konst) => { - konst.source(db).and_then(|node| range_and_position_of(&node.value)) + konst.source(db).and_then(|node| range_and_position_of(&node)) } hir::ModuleDef::Trait(trait_) => { - trait_.source(db).and_then(|node| range_and_position_of(&node.value)) + trait_.source(db).and_then(|node| range_and_position_of(&node)) } hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { - strukt.source(db).and_then(|node| range_and_position_of(&node.value)) + strukt.source(db).and_then(|node| range_and_position_of(&node)) } hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { - enum_.source(db).and_then(|node| range_and_position_of(&node.value)) + enum_.source(db).and_then(|node| range_and_position_of(&node)) } hir::ModuleDef::Adt(hir::Adt::Union(union)) => { - union.source(db).and_then(|node| range_and_position_of(&node.value)) + union.source(db).and_then(|node| range_and_position_of(&node)) } _ => None, }; @@ -120,8 +120,18 @@ pub(crate) fn annotations( }); } - fn range_and_position_of(node: &dyn NameOwner) -> Option<(TextSize, TextRange)> { - Some((node.name()?.syntax().text_range().start(), node.syntax().text_range())) + fn range_and_position_of( + node: &InFile, + ) -> Option<(TextSize, TextRange)> { + if node.file_id.is_macro_file() { + // Macro generated files should not contain annotations. + None + } else { + Some(( + node.value.name()?.syntax().text_range().start(), + node.value.syntax().text_range(), + )) + } } } Either::Right(_) => (), @@ -961,6 +971,25 @@ mod tests { struct Foo; //- /lib.rs // this file comes last since `check` checks the first file only +"#, + expect![[r#" + [] + "#]], + ); + } + + #[test] + fn test_no_annotations_macro_struct_def() { + check( + r#" +//- /lib.rs +macro_rules! m { + () => { + struct A {} + }; +} + +m!(); "#, expect![[r#" [] -- cgit v1.2.3 From 903a2e98f93df87af19375e951c56e7c285989d4 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 24 Mar 2021 00:47:55 -0700 Subject: Clean up implementation --- crates/ide/src/annotations.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 9e78ed6a0..64bc926f1 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -80,19 +80,19 @@ pub(crate) fn annotations( Either::Left(def) => { let node = match def { hir::ModuleDef::Const(konst) => { - konst.source(db).and_then(|node| range_and_position_of(&node)) + konst.source(db).and_then(|node| range_and_position_of(&node, file_id)) } hir::ModuleDef::Trait(trait_) => { - trait_.source(db).and_then(|node| range_and_position_of(&node)) + trait_.source(db).and_then(|node| range_and_position_of(&node, file_id)) } hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { - strukt.source(db).and_then(|node| range_and_position_of(&node)) + strukt.source(db).and_then(|node| range_and_position_of(&node, file_id)) } hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { - enum_.source(db).and_then(|node| range_and_position_of(&node)) + enum_.source(db).and_then(|node| range_and_position_of(&node, file_id)) } hir::ModuleDef::Adt(hir::Adt::Union(union)) => { - union.source(db).and_then(|node| range_and_position_of(&node)) + union.source(db).and_then(|node| range_and_position_of(&node, file_id)) } _ => None, }; @@ -122,9 +122,10 @@ pub(crate) fn annotations( fn range_and_position_of( node: &InFile, + file_id: FileId, ) -> Option<(TextSize, TextRange)> { - if node.file_id.is_macro_file() { - // Macro generated files should not contain annotations. + if node.file_id != file_id.into() { + // Node is outside the file we are adding annotations to (e.g. macros). None } else { Some(( -- cgit v1.2.3