internal: code review tweak

This commit is contained in:
Young-Flash
2026-04-18 20:40:30 +08:00
parent 67fa3a112c
commit cdc1d7c058
2 changed files with 51 additions and 37 deletions
@@ -449,7 +449,7 @@ pub fn usages<'a>(self, sema: &'a Semantics<'_, RootDatabase>) -> FindUsages<'a>
scope: None,
include_self_kw_refs: None,
search_self_mod: false,
excluded_categories: ReferenceCategory::empty(),
included_categories: ReferenceCategory::all(),
exclude_library_files: false,
}
}
@@ -467,8 +467,8 @@ pub struct FindUsages<'a> {
include_self_kw_refs: Option<hir::Type<'a>>,
/// whether to search for the `self` module
search_self_mod: bool,
/// categories to exclude while collecting usages
excluded_categories: ReferenceCategory,
/// categories to include while collecting usages
included_categories: ReferenceCategory,
/// whether to skip files from library source roots
exclude_library_files: bool,
}
@@ -501,8 +501,8 @@ pub fn with_rename(mut self, rename: Option<&'a Rename>) -> Self {
self
}
pub fn set_excluded_categories(mut self, categories: ReferenceCategory) -> Self {
self.excluded_categories = categories;
pub fn set_included_categories(mut self, categories: ReferenceCategory) -> Self {
self.included_categories = categories;
self
}
@@ -532,14 +532,21 @@ pub fn all(self) -> UsageSearchResult {
fn scope_files<'b>(
db: &'b RootDatabase,
scope: &'b SearchScope,
exclude_library_files: bool,
) -> impl Iterator<Item = (Arc<str>, EditionedFileId, TextRange)> + 'b {
scope.entries.iter().map(|(&file_id, &search_range)| {
let text = db.file_text(file_id.file_id(db)).text(db);
let search_range =
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&**text)));
scope
.entries
.iter()
.filter(move |(file_id, _)| {
!exclude_library_files || !is_library_file(db, file_id.file_id(db))
})
.map(|(&file_id, &search_range)| {
let text = db.file_text(file_id.file_id(db)).text(db);
let search_range =
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&**text)));
(text.clone(), file_id, search_range)
})
(text.clone(), file_id, search_range)
})
}
fn match_indices<'b>(
@@ -665,6 +672,7 @@ fn has_any_name(node: &SyntaxNode, mut predicate: impl FnMut(&str) -> bool) -> b
fn collect_possible_aliases(
sema: &Semantics<'_, RootDatabase>,
container: Adt,
exclude_library_files: bool,
) -> Option<(FxHashSet<SmolStr>, Vec<FileRangeWrapper<EditionedFileId>>)> {
fn insert_type_alias(
db: &RootDatabase,
@@ -698,9 +706,11 @@ fn insert_type_alias(
};
let finder = Finder::new(current_to_process.as_bytes());
for (file_text, file_id, search_range) in
FindUsages::scope_files(db, &current_to_process_search_scope)
{
for (file_text, file_id, search_range) in FindUsages::scope_files(
db,
&current_to_process_search_scope,
exclude_library_files,
) {
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
for offset in FindUsages::match_indices(&file_text, &finder, search_range) {
@@ -885,7 +895,7 @@ fn search(
}
let Some((container_possible_aliases, is_possibly_self)) =
collect_possible_aliases(self.sema, container)
collect_possible_aliases(self.sema, container, self.exclude_library_files)
else {
return false;
};
@@ -897,16 +907,12 @@ fn search(
let finder = Finder::new(name.as_bytes());
// The search for `Self` may return duplicate results with `ContainerName`, so deduplicate them.
let mut self_positions = FxHashSet::default();
let is_possibly_self = is_possibly_self.into_iter().filter(|position| {
!self.exclude_library_files
|| !is_library_file(self.sema.db, position.file_id.file_id(self.sema.db))
});
tracing::info_span!("Self_search").in_scope(|| {
search(
self,
&finder,
name,
is_possibly_self.map(|position| {
is_possibly_self.into_iter().map(|position| {
(position.file_text(self.sema.db).clone(), position.file_id, position.range)
}),
|path, name_position| {
@@ -926,7 +932,7 @@ fn search(
self,
&finder,
name,
FindUsages::scope_files(self.sema.db, search_scope),
FindUsages::scope_files(self.sema.db, search_scope, self.exclude_library_files),
|path, name_position| {
has_any_name(path, |name| container_possible_aliases.contains(name))
&& !self_positions.contains(&name_position)
@@ -942,7 +948,7 @@ pub fn search(&self, sink: &mut dyn FnMut(EditionedFileId, FileReference) -> boo
let _p = tracing::info_span!("FindUsages:search").entered();
let sema = self.sema;
let mut search_scope = {
let search_scope = {
// FIXME: Is the trait scope needed for trait impl assoc items?
let base =
as_trait_assoc_def(sema.db, self.def).unwrap_or(self.def).search_scope(sema.db);
@@ -951,11 +957,6 @@ pub fn search(&self, sink: &mut dyn FnMut(EditionedFileId, FileReference) -> boo
Some(scope) => base.intersection(scope),
}
};
if self.exclude_library_files {
search_scope
.entries
.retain(|&file_id, _| !is_library_file(sema.db, file_id.file_id(sema.db)));
}
if search_scope.entries.is_empty() {
return;
}
@@ -1010,7 +1011,9 @@ pub fn search(&self, sink: &mut dyn FnMut(EditionedFileId, FileReference) -> boo
let finder = &Finder::new(name);
let include_self_kw_refs =
self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self")));
for (text, file_id, search_range) in Self::scope_files(sema.db, &search_scope) {
for (text, file_id, search_range) in
Self::scope_files(sema.db, &search_scope, self.exclude_library_files)
{
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
// Search for occurrences of the items name
@@ -1067,7 +1070,9 @@ pub fn search(&self, sink: &mut dyn FnMut(EditionedFileId, FileReference) -> boo
let is_crate_root = module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
let finder = &Finder::new("super");
for (text, file_id, search_range) in Self::scope_files(sema.db, &scope) {
for (text, file_id, search_range) in
Self::scope_files(sema.db, &scope, self.exclude_library_files)
{
self.sema.db.unwind_if_revision_cancelled();
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
@@ -1324,9 +1329,9 @@ fn found_name_ref(
}
fn is_excluded_name_ref(&self, name_ref: &ast::NameRef) -> bool {
(self.excluded_categories.contains(ReferenceCategory::TEST)
(!self.included_categories.contains(ReferenceCategory::TEST)
&& is_name_ref_in_test(self.sema, name_ref))
|| (self.excluded_categories.contains(ReferenceCategory::IMPORT)
|| (!self.included_categories.contains(ReferenceCategory::IMPORT)
&& is_name_ref_in_import(name_ref))
}
@@ -131,17 +131,17 @@ pub(crate) fn find_all_refs(
let exclude_library_refs = !is_library_file(sema.db, position.file_id);
let make_searcher = |literal_search: bool| {
move |def: Definition| {
let mut excluded_categories = ReferenceCategory::empty();
let mut included_categories = ReferenceCategory::all();
if config.exclude_imports {
excluded_categories |= ReferenceCategory::IMPORT;
included_categories.remove(ReferenceCategory::IMPORT);
}
if config.exclude_tests {
excluded_categories |= ReferenceCategory::TEST;
included_categories.remove(ReferenceCategory::TEST);
}
let mut usages = def
.usages(sema)
.set_scope(config.search_scope.as_ref())
.set_excluded_categories(excluded_categories)
.set_included_categories(included_categories)
.set_exclude_library_files(exclude_library_refs)
.include_self_refs()
.all();
@@ -182,8 +182,7 @@ pub(crate) fn find_all_refs(
is_mut: matches!(def, Definition::Local(l) if l.is_mut(sema.db)),
nav,
}
})
.filter(|decl| !(exclude_library_refs && is_library_file(sema.db, decl.nav.file_id)));
});
ReferenceSearchResult { declaration, references }
}
};
@@ -582,6 +581,8 @@ pub fn also_calls_foo() {
false,
false,
expect![[r#"
foo Function FileId(1) 0..15 7..10
FileId(0) 9..12 import
FileId(0) 31..34
"#]],
@@ -598,6 +599,8 @@ fn main() {
false,
false,
expect![[r#"
Some Variant FileId(1) 5999..6031 6024..6028
FileId(0) 46..50
"#]],
);
@@ -2244,6 +2247,8 @@ fn attr() {
fn func() {}
"#,
expect![[r#"
identity Attribute FileId(1) 1..107 32..40
FileId(0) 17..25 import
FileId(0) 43..51
"#]],
@@ -2273,6 +2278,8 @@ fn proc_macro() {
mirror$0! {}
"#,
expect![[r#"
mirror ProcMacro FileId(1) 1..77 22..28
FileId(0) 17..23 import
FileId(0) 26..32
"#]],
@@ -2291,6 +2298,8 @@ fn derive() {
struct Foo;
"#,
expect![[r#"
derive_identity Derive FileId(2) 1..107 45..60
FileId(0) 17..31 import
FileId(0) 56..70
"#]],