Skip to content

Commit 8b707e3

Browse files
committed
internal: code review tweak
1 parent 7490a86 commit 8b707e3

2 files changed

Lines changed: 51 additions & 37 deletions

File tree

crates/ide-db/src/search.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl Definition {
449449
scope: None,
450450
include_self_kw_refs: None,
451451
search_self_mod: false,
452-
excluded_categories: ReferenceCategory::empty(),
452+
included_categories: ReferenceCategory::all(),
453453
exclude_library_files: false,
454454
}
455455
}
@@ -467,8 +467,8 @@ pub struct FindUsages<'a> {
467467
include_self_kw_refs: Option<hir::Type<'a>>,
468468
/// whether to search for the `self` module
469469
search_self_mod: bool,
470-
/// categories to exclude while collecting usages
471-
excluded_categories: ReferenceCategory,
470+
/// categories to include while collecting usages
471+
included_categories: ReferenceCategory,
472472
/// whether to skip files from library source roots
473473
exclude_library_files: bool,
474474
}
@@ -501,8 +501,8 @@ impl<'a> FindUsages<'a> {
501501
self
502502
}
503503

504-
pub fn set_excluded_categories(mut self, categories: ReferenceCategory) -> Self {
505-
self.excluded_categories = categories;
504+
pub fn set_included_categories(mut self, categories: ReferenceCategory) -> Self {
505+
self.included_categories = categories;
506506
self
507507
}
508508

@@ -532,14 +532,21 @@ impl<'a> FindUsages<'a> {
532532
fn scope_files<'b>(
533533
db: &'b RootDatabase,
534534
scope: &'b SearchScope,
535+
exclude_library_files: bool,
535536
) -> impl Iterator<Item = (Arc<str>, EditionedFileId, TextRange)> + 'b {
536-
scope.entries.iter().map(|(&file_id, &search_range)| {
537-
let text = db.file_text(file_id.file_id(db)).text(db);
538-
let search_range =
539-
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&**text)));
537+
scope
538+
.entries
539+
.iter()
540+
.filter(move |(file_id, _)| {
541+
!exclude_library_files || !is_library_file(db, file_id.file_id(db))
542+
})
543+
.map(|(&file_id, &search_range)| {
544+
let text = db.file_text(file_id.file_id(db)).text(db);
545+
let search_range =
546+
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&**text)));
540547

541-
(text.clone(), file_id, search_range)
542-
})
548+
(text.clone(), file_id, search_range)
549+
})
543550
}
544551

545552
fn match_indices<'b>(
@@ -665,6 +672,7 @@ impl<'a> FindUsages<'a> {
665672
fn collect_possible_aliases(
666673
sema: &Semantics<'_, RootDatabase>,
667674
container: Adt,
675+
exclude_library_files: bool,
668676
) -> Option<(FxHashSet<SmolStr>, Vec<FileRangeWrapper<EditionedFileId>>)> {
669677
fn insert_type_alias(
670678
db: &RootDatabase,
@@ -698,9 +706,11 @@ impl<'a> FindUsages<'a> {
698706
};
699707

700708
let finder = Finder::new(current_to_process.as_bytes());
701-
for (file_text, file_id, search_range) in
702-
FindUsages::scope_files(db, &current_to_process_search_scope)
703-
{
709+
for (file_text, file_id, search_range) in FindUsages::scope_files(
710+
db,
711+
&current_to_process_search_scope,
712+
exclude_library_files,
713+
) {
704714
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
705715

706716
for offset in FindUsages::match_indices(&file_text, &finder, search_range) {
@@ -885,7 +895,7 @@ impl<'a> FindUsages<'a> {
885895
}
886896

887897
let Some((container_possible_aliases, is_possibly_self)) =
888-
collect_possible_aliases(self.sema, container)
898+
collect_possible_aliases(self.sema, container, self.exclude_library_files)
889899
else {
890900
return false;
891901
};
@@ -897,16 +907,12 @@ impl<'a> FindUsages<'a> {
897907
let finder = Finder::new(name.as_bytes());
898908
// The search for `Self` may return duplicate results with `ContainerName`, so deduplicate them.
899909
let mut self_positions = FxHashSet::default();
900-
let is_possibly_self = is_possibly_self.into_iter().filter(|position| {
901-
!self.exclude_library_files
902-
|| !is_library_file(self.sema.db, position.file_id.file_id(self.sema.db))
903-
});
904910
tracing::info_span!("Self_search").in_scope(|| {
905911
search(
906912
self,
907913
&finder,
908914
name,
909-
is_possibly_self.map(|position| {
915+
is_possibly_self.into_iter().map(|position| {
910916
(position.file_text(self.sema.db).clone(), position.file_id, position.range)
911917
}),
912918
|path, name_position| {
@@ -926,7 +932,7 @@ impl<'a> FindUsages<'a> {
926932
self,
927933
&finder,
928934
name,
929-
FindUsages::scope_files(self.sema.db, search_scope),
935+
FindUsages::scope_files(self.sema.db, search_scope, self.exclude_library_files),
930936
|path, name_position| {
931937
has_any_name(path, |name| container_possible_aliases.contains(name))
932938
&& !self_positions.contains(&name_position)
@@ -942,7 +948,7 @@ impl<'a> FindUsages<'a> {
942948
let _p = tracing::info_span!("FindUsages:search").entered();
943949
let sema = self.sema;
944950

945-
let mut search_scope = {
951+
let search_scope = {
946952
// FIXME: Is the trait scope needed for trait impl assoc items?
947953
let base =
948954
as_trait_assoc_def(sema.db, self.def).unwrap_or(self.def).search_scope(sema.db);
@@ -951,11 +957,6 @@ impl<'a> FindUsages<'a> {
951957
Some(scope) => base.intersection(scope),
952958
}
953959
};
954-
if self.exclude_library_files {
955-
search_scope
956-
.entries
957-
.retain(|&file_id, _| !is_library_file(sema.db, file_id.file_id(sema.db)));
958-
}
959960
if search_scope.entries.is_empty() {
960961
return;
961962
}
@@ -1010,7 +1011,9 @@ impl<'a> FindUsages<'a> {
10101011
let finder = &Finder::new(name);
10111012
let include_self_kw_refs =
10121013
self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self")));
1013-
for (text, file_id, search_range) in Self::scope_files(sema.db, &search_scope) {
1014+
for (text, file_id, search_range) in
1015+
Self::scope_files(sema.db, &search_scope, self.exclude_library_files)
1016+
{
10141017
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
10151018

10161019
// Search for occurrences of the items name
@@ -1067,7 +1070,9 @@ impl<'a> FindUsages<'a> {
10671070
let is_crate_root = module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
10681071
let finder = &Finder::new("super");
10691072

1070-
for (text, file_id, search_range) in Self::scope_files(sema.db, &scope) {
1073+
for (text, file_id, search_range) in
1074+
Self::scope_files(sema.db, &scope, self.exclude_library_files)
1075+
{
10711076
self.sema.db.unwind_if_revision_cancelled();
10721077

10731078
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
@@ -1324,9 +1329,9 @@ impl<'a> FindUsages<'a> {
13241329
}
13251330

13261331
fn is_excluded_name_ref(&self, name_ref: &ast::NameRef) -> bool {
1327-
(self.excluded_categories.contains(ReferenceCategory::TEST)
1332+
(!self.included_categories.contains(ReferenceCategory::TEST)
13281333
&& is_name_ref_in_test(self.sema, name_ref))
1329-
|| (self.excluded_categories.contains(ReferenceCategory::IMPORT)
1334+
|| (!self.included_categories.contains(ReferenceCategory::IMPORT)
13301335
&& is_name_ref_in_import(name_ref))
13311336
}
13321337

crates/ide/src/references.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ pub(crate) fn find_all_refs(
131131
let exclude_library_refs = !is_library_file(sema.db, position.file_id);
132132
let make_searcher = |literal_search: bool| {
133133
move |def: Definition| {
134-
let mut excluded_categories = ReferenceCategory::empty();
134+
let mut included_categories = ReferenceCategory::all();
135135
if config.exclude_imports {
136-
excluded_categories |= ReferenceCategory::IMPORT;
136+
included_categories.remove(ReferenceCategory::IMPORT);
137137
}
138138
if config.exclude_tests {
139-
excluded_categories |= ReferenceCategory::TEST;
139+
included_categories.remove(ReferenceCategory::TEST);
140140
}
141141
let mut usages = def
142142
.usages(sema)
143143
.set_scope(config.search_scope.as_ref())
144-
.set_excluded_categories(excluded_categories)
144+
.set_included_categories(included_categories)
145145
.set_exclude_library_files(exclude_library_refs)
146146
.include_self_refs()
147147
.all();
@@ -182,8 +182,7 @@ pub(crate) fn find_all_refs(
182182
is_mut: matches!(def, Definition::Local(l) if l.is_mut(sema.db)),
183183
nav,
184184
}
185-
})
186-
.filter(|decl| !(exclude_library_refs && is_library_file(sema.db, decl.nav.file_id)));
185+
});
187186
ReferenceSearchResult { declaration, references }
188187
}
189188
};
@@ -582,6 +581,8 @@ pub fn also_calls_foo() {
582581
false,
583582
false,
584583
expect![[r#"
584+
foo Function FileId(1) 0..15 7..10
585+
585586
FileId(0) 9..12 import
586587
FileId(0) 31..34
587588
"#]],
@@ -598,6 +599,8 @@ fn main() {
598599
false,
599600
false,
600601
expect![[r#"
602+
Some Variant FileId(1) 5999..6031 6024..6028
603+
601604
FileId(0) 46..50
602605
"#]],
603606
);
@@ -2244,6 +2247,8 @@ use proc_macros::identity;
22442247
fn func() {}
22452248
"#,
22462249
expect![[r#"
2250+
identity Attribute FileId(1) 1..107 32..40
2251+
22472252
FileId(0) 17..25 import
22482253
FileId(0) 43..51
22492254
"#]],
@@ -2273,6 +2278,8 @@ use proc_macros::mirror;
22732278
mirror$0! {}
22742279
"#,
22752280
expect![[r#"
2281+
mirror ProcMacro FileId(1) 1..77 22..28
2282+
22762283
FileId(0) 17..23 import
22772284
FileId(0) 26..32
22782285
"#]],
@@ -2291,6 +2298,8 @@ use proc_macros::DeriveIdentity;
22912298
struct Foo;
22922299
"#,
22932300
expect![[r#"
2301+
derive_identity Derive FileId(2) 1..107 45..60
2302+
22942303
FileId(0) 17..31 import
22952304
FileId(0) 56..70
22962305
"#]],

0 commit comments

Comments
 (0)