From c6645f2eb69db0e2ce5483fc1c60316320f050d9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Feb 2022 18:07:47 +0100 Subject: [PATCH] fix: Make code lenses work on attributed items --- crates/ide/src/annotations.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index a7c12c16361..986db75c610 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -65,10 +65,10 @@ pub(crate) fn annotations( visit_file_defs(&Semantics::new(db), file_id, &mut |def| { let range = match def { Definition::Const(konst) if config.annotate_references => { - konst.source(db).and_then(|node| name_range(&node, file_id)) + konst.source(db).and_then(|node| name_range(db, node, file_id)) } Definition::Trait(trait_) if config.annotate_references || config.annotate_impls => { - trait_.source(db).and_then(|node| name_range(&node, file_id)) + trait_.source(db).and_then(|node| name_range(db, node, file_id)) } Definition::Adt(adt) => match adt { hir::Adt::Enum(enum_) => { @@ -77,7 +77,7 @@ pub(crate) fn annotations( .variants(db) .into_iter() .map(|variant| { - variant.source(db).and_then(|node| name_range(&node, file_id)) + variant.source(db).and_then(|node| name_range(db, node, file_id)) }) .filter_map(std::convert::identity) .for_each(|range| { @@ -91,14 +91,14 @@ pub(crate) fn annotations( }) } if config.annotate_references || config.annotate_impls { - enum_.source(db).and_then(|node| name_range(&node, file_id)) + enum_.source(db).and_then(|node| name_range(db, node, file_id)) } else { None } } _ => { if config.annotate_references || config.annotate_impls { - adt.source(db).and_then(|node| name_range(&node, file_id)) + adt.source(db).and_then(|node| name_range(db, node, file_id)) } else { None } @@ -131,13 +131,17 @@ pub(crate) fn annotations( }); } - fn name_range(node: &InFile, file_id: FileId) -> Option { - if node.file_id == file_id.into() { - node.value.name().map(|it| it.syntax().text_range()) - } else { - // Node is outside the file we are adding annotations to (e.g. macros). - None + fn name_range( + db: &RootDatabase, + node: InFile, + source_file_id: FileId, + ) -> Option { + if let Some(InFile { file_id, value }) = node.original_ast_node(db) { + if file_id == source_file_id.into() { + return value.name().map(|it| it.syntax().text_range()); + } } + None } });