Skip to content

Commit de3b12d

Browse files
committed
Auto merge of rust-lang#14299 - Veykril:local-search, r=Veykril
fix: Fix search not searching bodies of attributed items Closes rust-lang/rust-analyzer#14229
2 parents a738737 + 3427d36 commit de3b12d

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

crates/hir-expand/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ impl<'a> InFile<&'a SyntaxNode> {
815815
/// Falls back to the macro call range if the node cannot be mapped up fully.
816816
///
817817
/// For attributes and derives, this will point back to the attribute only.
818-
/// For the entire item `InFile::use original_file_range_full`.
818+
/// For the entire item use [`InFile::original_file_range_full`].
819819
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
820820
match self.file_id.repr() {
821821
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
@@ -830,6 +830,21 @@ impl<'a> InFile<&'a SyntaxNode> {
830830
}
831831
}
832832

833+
/// Falls back to the macro call range if the node cannot be mapped up fully.
834+
pub fn original_file_range_full(self, db: &dyn db::AstDatabase) -> FileRange {
835+
match self.file_id.repr() {
836+
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
837+
HirFileIdRepr::MacroFile(mac_file) => {
838+
if let Some(res) = self.original_file_range_opt(db) {
839+
return res;
840+
}
841+
// Fall back to whole macro call.
842+
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
843+
loc.kind.original_call_range_with_body(db)
844+
}
845+
}
846+
}
847+
833848
/// Attempts to map the syntax node back up its macro calls.
834849
pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
835850
match ascend_node_border_tokens(db, self) {

crates/ide-db/src/search.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ impl Definition {
244244
DefWithBody::Variant(v) => v.source(db).map(|src| src.syntax().cloned()),
245245
};
246246
return match def {
247-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
247+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
248248
None => SearchScope::single_file(file_id),
249249
};
250250
}
251251

252252
if let Definition::SelfType(impl_) = self {
253253
return match impl_.source(db).map(|src| src.syntax().cloned()) {
254-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
254+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
255255
None => SearchScope::single_file(file_id),
256256
};
257257
}
@@ -268,7 +268,7 @@ impl Definition {
268268
hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
269269
};
270270
return match def {
271-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
271+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
272272
None => SearchScope::single_file(file_id),
273273
};
274274
}
@@ -319,10 +319,6 @@ impl Definition {
319319
sema,
320320
scope: None,
321321
include_self_kw_refs: None,
322-
local_repr: match self {
323-
Definition::Local(local) => Some(local),
324-
_ => None,
325-
},
326322
search_self_mod: false,
327323
}
328324
}
@@ -337,9 +333,6 @@ pub struct FindUsages<'a> {
337333
assoc_item_container: Option<hir::AssocItemContainer>,
338334
/// whether to search for the `Self` type of the definition
339335
include_self_kw_refs: Option<hir::Type>,
340-
/// the local representative for the local definition we are searching for
341-
/// (this is required for finding all local declarations in a or-pattern)
342-
local_repr: Option<hir::Local>,
343336
/// whether to search for the `self` module
344337
search_self_mod: bool,
345338
}
@@ -644,19 +637,6 @@ impl<'a> FindUsages<'a> {
644637
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
645638
) -> bool {
646639
match NameRefClass::classify(self.sema, name_ref) {
647-
Some(NameRefClass::Definition(def @ Definition::Local(local)))
648-
if matches!(
649-
self.local_repr, Some(repr) if repr == local
650-
) =>
651-
{
652-
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
653-
let reference = FileReference {
654-
range,
655-
name: ast::NameLike::NameRef(name_ref.clone()),
656-
category: ReferenceCategory::new(&def, name_ref),
657-
};
658-
sink(file_id, reference)
659-
}
660640
Some(NameRefClass::Definition(def))
661641
if self.def == def
662642
// 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> {
701681
}
702682
}
703683
Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => {
704-
let field = Definition::Field(field);
705684
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
685+
686+
let field = Definition::Field(field);
687+
let local = Definition::Local(local);
706688
let access = match self.def {
707689
Definition::Field(_) if field == self.def => {
708690
ReferenceCategory::new(&field, name_ref)
709691
}
710-
Definition::Local(_) if matches!(self.local_repr, Some(repr) if repr == local) => {
711-
ReferenceCategory::new(&Definition::Local(local), name_ref)
692+
Definition::Local(_) if local == self.def => {
693+
ReferenceCategory::new(&local, name_ref)
712694
}
713695
_ => return false,
714696
};
@@ -752,21 +734,6 @@ impl<'a> FindUsages<'a> {
752734
};
753735
sink(file_id, reference)
754736
}
755-
Some(NameClass::Definition(def @ Definition::Local(local))) if def != self.def => {
756-
if matches!(
757-
self.local_repr,
758-
Some(repr) if local == repr
759-
) {
760-
let FileRange { file_id, range } = self.sema.original_range(name.syntax());
761-
let reference = FileReference {
762-
range,
763-
name: ast::NameLike::Name(name.clone()),
764-
category: None,
765-
};
766-
return sink(file_id, reference);
767-
}
768-
false
769-
}
770737
Some(NameClass::Definition(def)) if def != self.def => {
771738
match (&self.assoc_item_container, self.def) {
772739
// for type aliases we always want to reference the trait def and all the trait impl counterparts

0 commit comments

Comments
 (0)