Updated search to expose some more functions and to make search take the search scope by reference.

This commit is contained in:
Josiah Bills 2023-07-09 17:20:18 -04:00
parent 5f11c9a1c3
commit b9cef03230
12 changed files with 27 additions and 28 deletions

View file

@ -114,7 +114,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
let usages = ctx.sema.to_def(&ident_pat).map(|def| {
Definition::Local(def)
.usages(&ctx.sema)
.in_scope(SearchScope::single_file(ctx.file_id()))
.in_scope(&SearchScope::single_file(ctx.file_id()))
.all()
});

View file

@ -120,7 +120,7 @@ fn find_parent_and_path(
fn def_is_referenced_in(def: Definition, ctx: &AssistContext<'_>) -> bool {
let search_scope = SearchScope::single_file(ctx.file_id());
def.usages(&ctx.sema).in_scope(search_scope).at_least_one()
def.usages(&ctx.sema).in_scope(&search_scope).at_least_one()
}
#[derive(Debug, Clone)]

View file

@ -384,7 +384,7 @@ impl LocalUsages {
Self(
Definition::Local(var)
.usages(&ctx.sema)
.in_scope(SearchScope::single_file(ctx.file_id()))
.in_scope(&SearchScope::single_file(ctx.file_id()))
.all(),
)
}

View file

@ -478,7 +478,7 @@ impl Module {
let selection_range = ctx.selection_trimmed();
let curr_file_id = ctx.file_id();
let search_scope = SearchScope::single_file(curr_file_id);
let usage_res = def.usages(&ctx.sema).in_scope(search_scope).all();
let usage_res = def.usages(&ctx.sema).in_scope(&search_scope).all();
let file = ctx.sema.parse(curr_file_id);
let mut exists_inside_sel = false;

View file

@ -80,7 +80,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let is_recursive_fn = usages
.clone()
.in_scope(SearchScope::file_range(FileRange {
.in_scope(&SearchScope::file_range(FileRange {
file_id: def_file,
range: func_body.syntax().text_range(),
}))

View file

@ -82,17 +82,19 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
return None;
}
let usages =
Definition::Const(def).usages(&ctx.sema).in_scope(SearchScope::file_range(FileRange {
file_id: ctx.file_id(),
range: parent_fn.syntax().text_range(),
}));
acc.add(
AssistId("move_const_to_impl", crate::AssistKind::RefactorRewrite),
"Move const to impl block",
const_.syntax().text_range(),
|builder| {
let usages = Definition::Const(def)
.usages(&ctx.sema)
.in_scope(&SearchScope::file_range(FileRange {
file_id: ctx.file_id(),
range: parent_fn.syntax().text_range(),
}))
.all();
let range_to_delete = match const_.syntax().next_sibling_or_token() {
Some(s) if matches!(s.kind(), SyntaxKind::WHITESPACE) => {
// Remove following whitespaces too.
@ -103,7 +105,7 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
builder.delete(range_to_delete);
let const_ref = format!("Self::{}", name.display(ctx.db()));
for range in usages.all().file_ranges().map(|it| it.range) {
for range in usages.file_ranges().map(|it| it.range) {
builder.replace(range, const_ref.clone());
}

View file

@ -157,7 +157,7 @@ fn find_usages(
file_id: FileId,
) -> UsageSearchResult {
let file_range = FileRange { file_id, range: fn_.syntax().text_range() };
type_param_def.usages(sema).in_scope(SearchScope::file_range(file_range)).all()
type_param_def.usages(sema).in_scope(&SearchScope::file_range(file_range)).all()
}
fn check_valid_usages(usages: &UsageSearchResult, param_list_range: TextRange) -> bool {

View file

@ -127,7 +127,7 @@ impl SearchScope {
}
/// Build a search scope spanning the given module and all its submodules.
fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
pub fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
let mut entries = IntMap::default();
let (file_id, range) = {
@ -329,7 +329,7 @@ impl Definition {
pub struct FindUsages<'a> {
def: Definition,
sema: &'a Semantics<'a, RootDatabase>,
scope: Option<SearchScope>,
scope: Option<&'a SearchScope>,
/// The container of our definition should it be an assoc item
assoc_item_container: Option<hir::AssocItemContainer>,
/// whether to search for the `Self` type of the definition
@ -338,7 +338,7 @@ pub struct FindUsages<'a> {
search_self_mod: bool,
}
impl FindUsages<'_> {
impl<'a> FindUsages<'a> {
/// Enable searching for `Self` when the definition is a type or `self` for modules.
pub fn include_self_refs(mut self) -> Self {
self.include_self_kw_refs = def_to_ty(self.sema, &self.def);
@ -347,12 +347,12 @@ impl FindUsages<'_> {
}
/// Limit the search to a given [`SearchScope`].
pub fn in_scope(self, scope: SearchScope) -> Self {
pub fn in_scope(self, scope: &'a SearchScope) -> Self {
self.set_scope(Some(scope))
}
/// Limit the search to a given [`SearchScope`].
pub fn set_scope(mut self, scope: Option<SearchScope>) -> Self {
pub fn set_scope(mut self, scope: Option<&'a SearchScope>) -> Self {
assert!(self.scope.is_none());
self.scope = scope;
self
@ -376,7 +376,7 @@ impl FindUsages<'_> {
res
}
fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
pub fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
let _p = profile::span("FindUsages:search");
let sema = self.sema;

View file

@ -121,7 +121,7 @@ impl MatchFinder<'_> {
// cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two
// lookups in the case of a cache hit.
if usage_cache.find(&definition).is_none() {
let usages = definition.usages(&self.sema).in_scope(self.search_scope()).all();
let usages = definition.usages(&self.sema).in_scope(&self.search_scope()).all();
usage_cache.usages.push((definition, usages));
return &usage_cache.usages.last().unwrap().1;
}

View file

@ -100,10 +100,7 @@ fn highlight_closure_captures(
.flat_map(|local| {
let usages = Definition::Local(local)
.usages(sema)
.set_scope(Some(SearchScope::file_range(FileRange {
file_id,
range: search_range,
})))
.in_scope(&SearchScope::file_range(FileRange { file_id, range: search_range }))
.include_self_refs()
.all()
.references
@ -139,7 +136,7 @@ fn highlight_references(
.iter()
.filter_map(|&d| {
d.usages(sema)
.set_scope(Some(SearchScope::single_file(file_id)))
.in_scope(&SearchScope::single_file(file_id))
.include_self_refs()
.all()
.references
@ -183,7 +180,7 @@ fn highlight_references(
.filter_map(|item| {
Definition::from(item)
.usages(sema)
.set_scope(Some(SearchScope::file_range(FileRange {
.set_scope(Some(&SearchScope::file_range(FileRange {
file_id,
range: trait_item_use_scope.text_range(),
})))

View file

@ -74,7 +74,7 @@ pub(crate) fn find_all_refs(
}
});
let mut usages =
def.usages(sema).set_scope(search_scope.clone()).include_self_refs().all();
def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all();
if literal_search {
retain_adt_literal_usages(&mut usages, def, sema);

View file

@ -232,7 +232,7 @@ fn find_related_tests(
for def in defs {
let defs = def
.usages(sema)
.set_scope(search_scope.clone())
.set_scope(search_scope.as_ref())
.all()
.references
.into_values()