From e158dc72465d79084184b34054239544f5bab095 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 9 Mar 2023 15:10:26 +0100 Subject: [PATCH 1/2] Remove unnecessary special local handling in search --- crates/ide-db/src/search.rs | 43 +++++-------------------------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs index 6298ea1927..77d694b57f 100644 --- a/crates/ide-db/src/search.rs +++ b/crates/ide-db/src/search.rs @@ -319,10 +319,6 @@ impl Definition { sema, scope: None, include_self_kw_refs: None, - local_repr: match self { - Definition::Local(local) => Some(local), - _ => None, - }, search_self_mod: false, } } @@ -337,9 +333,6 @@ pub struct FindUsages<'a> { assoc_item_container: Option, /// whether to search for the `Self` type of the definition include_self_kw_refs: Option, - /// the local representative for the local definition we are searching for - /// (this is required for finding all local declarations in a or-pattern) - local_repr: Option, /// whether to search for the `self` module search_self_mod: bool, } @@ -644,19 +637,6 @@ impl<'a> FindUsages<'a> { sink: &mut dyn FnMut(FileId, FileReference) -> bool, ) -> bool { match NameRefClass::classify(self.sema, name_ref) { - Some(NameRefClass::Definition(def @ Definition::Local(local))) - if matches!( - self.local_repr, Some(repr) if repr == local - ) => - { - let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); - let reference = FileReference { - range, - name: ast::NameLike::NameRef(name_ref.clone()), - category: ReferenceCategory::new(&def, name_ref), - }; - sink(file_id, reference) - } Some(NameRefClass::Definition(def)) if self.def == def // is our def a trait assoc item? then we want to find all assoc items from trait impls of our trait @@ -701,14 +681,16 @@ impl<'a> FindUsages<'a> { } } Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { - let field = Definition::Field(field); let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); + + let field = Definition::Field(field); + let local = Definition::Local(local); let access = match self.def { Definition::Field(_) if field == self.def => { ReferenceCategory::new(&field, name_ref) } - Definition::Local(_) if matches!(self.local_repr, Some(repr) if repr == local) => { - ReferenceCategory::new(&Definition::Local(local), name_ref) + Definition::Local(_) if local == self.def => { + ReferenceCategory::new(&local, name_ref) } _ => return false, }; @@ -752,21 +734,6 @@ impl<'a> FindUsages<'a> { }; sink(file_id, reference) } - Some(NameClass::Definition(def @ Definition::Local(local))) if def != self.def => { - if matches!( - self.local_repr, - Some(repr) if local == repr - ) { - let FileRange { file_id, range } = self.sema.original_range(name.syntax()); - let reference = FileReference { - range, - name: ast::NameLike::Name(name.clone()), - category: None, - }; - return sink(file_id, reference); - } - false - } Some(NameClass::Definition(def)) if def != self.def => { match (&self.assoc_item_container, self.def) { // for type aliases we always want to reference the trait def and all the trait impl counterparts From 3427d36d0e47a5266feb2e501876c60584600974 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 9 Mar 2023 15:30:17 +0100 Subject: [PATCH 2/2] fix: Fix search not searching bodies of attributed items --- crates/hir-expand/src/lib.rs | 17 ++++++++++++++++- crates/ide-db/src/search.rs | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index e4719237a2..39fc08ecdc 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -815,7 +815,7 @@ impl<'a> InFile<&'a SyntaxNode> { /// Falls back to the macro call range if the node cannot be mapped up fully. /// /// For attributes and derives, this will point back to the attribute only. - /// For the entire item `InFile::use original_file_range_full`. + /// For the entire item use [`InFile::original_file_range_full`]. pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange { match self.file_id.repr() { HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() }, @@ -830,6 +830,21 @@ impl<'a> InFile<&'a SyntaxNode> { } } + /// Falls back to the macro call range if the node cannot be mapped up fully. + pub fn original_file_range_full(self, db: &dyn db::AstDatabase) -> FileRange { + match self.file_id.repr() { + HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() }, + HirFileIdRepr::MacroFile(mac_file) => { + if let Some(res) = self.original_file_range_opt(db) { + return res; + } + // Fall back to whole macro call. + let loc = db.lookup_intern_macro_call(mac_file.macro_call_id); + loc.kind.original_call_range_with_body(db) + } + } + } + /// Attempts to map the syntax node back up its macro calls. pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option { match ascend_node_border_tokens(db, self) { diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs index 77d694b57f..12f5e4e2a2 100644 --- a/crates/ide-db/src/search.rs +++ b/crates/ide-db/src/search.rs @@ -244,14 +244,14 @@ impl Definition { DefWithBody::Variant(v) => v.source(db).map(|src| src.syntax().cloned()), }; return match def { - Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)), + Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)), None => SearchScope::single_file(file_id), }; } if let Definition::SelfType(impl_) = self { return match impl_.source(db).map(|src| src.syntax().cloned()) { - Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)), + Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)), None => SearchScope::single_file(file_id), }; } @@ -268,7 +268,7 @@ impl Definition { hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()), }; return match def { - Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)), + Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)), None => SearchScope::single_file(file_id), }; }