Skip to content

Commit b9cef03

Browse files
committed
Updated search to expose some more functions and to make search take the search scope by reference.
1 parent 5f11c9a commit b9cef03

12 files changed

+27
-28
lines changed

crates/ide-assists/src/handlers/destructure_tuple_binding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
114114
let usages = ctx.sema.to_def(&ident_pat).map(|def| {
115115
Definition::Local(def)
116116
.usages(&ctx.sema)
117-
.in_scope(SearchScope::single_file(ctx.file_id()))
117+
.in_scope(&SearchScope::single_file(ctx.file_id()))
118118
.all()
119119
});
120120

crates/ide-assists/src/handlers/expand_glob_import.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn find_parent_and_path(
120120

121121
fn def_is_referenced_in(def: Definition, ctx: &AssistContext<'_>) -> bool {
122122
let search_scope = SearchScope::single_file(ctx.file_id());
123-
def.usages(&ctx.sema).in_scope(search_scope).at_least_one()
123+
def.usages(&ctx.sema).in_scope(&search_scope).at_least_one()
124124
}
125125

126126
#[derive(Debug, Clone)]

crates/ide-assists/src/handlers/extract_function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl LocalUsages {
384384
Self(
385385
Definition::Local(var)
386386
.usages(&ctx.sema)
387-
.in_scope(SearchScope::single_file(ctx.file_id()))
387+
.in_scope(&SearchScope::single_file(ctx.file_id()))
388388
.all(),
389389
)
390390
}

crates/ide-assists/src/handlers/extract_module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl Module {
478478
let selection_range = ctx.selection_trimmed();
479479
let curr_file_id = ctx.file_id();
480480
let search_scope = SearchScope::single_file(curr_file_id);
481-
let usage_res = def.usages(&ctx.sema).in_scope(search_scope).all();
481+
let usage_res = def.usages(&ctx.sema).in_scope(&search_scope).all();
482482
let file = ctx.sema.parse(curr_file_id);
483483

484484
let mut exists_inside_sel = false;

crates/ide-assists/src/handlers/inline_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
8080

8181
let is_recursive_fn = usages
8282
.clone()
83-
.in_scope(SearchScope::file_range(FileRange {
83+
.in_scope(&SearchScope::file_range(FileRange {
8484
file_id: def_file,
8585
range: func_body.syntax().text_range(),
8686
}))

crates/ide-assists/src/handlers/move_const_to_impl.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,19 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
8282
return None;
8383
}
8484

85-
let usages =
86-
Definition::Const(def).usages(&ctx.sema).in_scope(SearchScope::file_range(FileRange {
87-
file_id: ctx.file_id(),
88-
range: parent_fn.syntax().text_range(),
89-
}));
90-
9185
acc.add(
9286
AssistId("move_const_to_impl", crate::AssistKind::RefactorRewrite),
9387
"Move const to impl block",
9488
const_.syntax().text_range(),
9589
|builder| {
90+
let usages = Definition::Const(def)
91+
.usages(&ctx.sema)
92+
.in_scope(&SearchScope::file_range(FileRange {
93+
file_id: ctx.file_id(),
94+
range: parent_fn.syntax().text_range(),
95+
}))
96+
.all();
97+
9698
let range_to_delete = match const_.syntax().next_sibling_or_token() {
9799
Some(s) if matches!(s.kind(), SyntaxKind::WHITESPACE) => {
98100
// Remove following whitespaces too.
@@ -103,7 +105,7 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
103105
builder.delete(range_to_delete);
104106

105107
let const_ref = format!("Self::{}", name.display(ctx.db()));
106-
for range in usages.all().file_ranges().map(|it| it.range) {
108+
for range in usages.file_ranges().map(|it| it.range) {
107109
builder.replace(range, const_ref.clone());
108110
}
109111

crates/ide-assists/src/handlers/replace_named_generic_with_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn find_usages(
157157
file_id: FileId,
158158
) -> UsageSearchResult {
159159
let file_range = FileRange { file_id, range: fn_.syntax().text_range() };
160-
type_param_def.usages(sema).in_scope(SearchScope::file_range(file_range)).all()
160+
type_param_def.usages(sema).in_scope(&SearchScope::file_range(file_range)).all()
161161
}
162162

163163
fn check_valid_usages(usages: &UsageSearchResult, param_list_range: TextRange) -> bool {

crates/ide-db/src/search.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl SearchScope {
127127
}
128128

129129
/// Build a search scope spanning the given module and all its submodules.
130-
fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
130+
pub fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
131131
let mut entries = IntMap::default();
132132

133133
let (file_id, range) = {
@@ -329,7 +329,7 @@ impl Definition {
329329
pub struct FindUsages<'a> {
330330
def: Definition,
331331
sema: &'a Semantics<'a, RootDatabase>,
332-
scope: Option<SearchScope>,
332+
scope: Option<&'a SearchScope>,
333333
/// The container of our definition should it be an assoc item
334334
assoc_item_container: Option<hir::AssocItemContainer>,
335335
/// whether to search for the `Self` type of the definition
@@ -338,7 +338,7 @@ pub struct FindUsages<'a> {
338338
search_self_mod: bool,
339339
}
340340

341-
impl FindUsages<'_> {
341+
impl<'a> FindUsages<'a> {
342342
/// Enable searching for `Self` when the definition is a type or `self` for modules.
343343
pub fn include_self_refs(mut self) -> Self {
344344
self.include_self_kw_refs = def_to_ty(self.sema, &self.def);
@@ -347,12 +347,12 @@ impl FindUsages<'_> {
347347
}
348348

349349
/// Limit the search to a given [`SearchScope`].
350-
pub fn in_scope(self, scope: SearchScope) -> Self {
350+
pub fn in_scope(self, scope: &'a SearchScope) -> Self {
351351
self.set_scope(Some(scope))
352352
}
353353

354354
/// Limit the search to a given [`SearchScope`].
355-
pub fn set_scope(mut self, scope: Option<SearchScope>) -> Self {
355+
pub fn set_scope(mut self, scope: Option<&'a SearchScope>) -> Self {
356356
assert!(self.scope.is_none());
357357
self.scope = scope;
358358
self
@@ -376,7 +376,7 @@ impl FindUsages<'_> {
376376
res
377377
}
378378

379-
fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
379+
pub fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
380380
let _p = profile::span("FindUsages:search");
381381
let sema = self.sema;
382382

crates/ide-ssr/src/search.rs

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

crates/ide/src/highlight_related.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ fn highlight_closure_captures(
100100
.flat_map(|local| {
101101
let usages = Definition::Local(local)
102102
.usages(sema)
103-
.set_scope(Some(SearchScope::file_range(FileRange {
104-
file_id,
105-
range: search_range,
106-
})))
103+
.in_scope(&SearchScope::file_range(FileRange { file_id, range: search_range }))
107104
.include_self_refs()
108105
.all()
109106
.references
@@ -139,7 +136,7 @@ fn highlight_references(
139136
.iter()
140137
.filter_map(|&d| {
141138
d.usages(sema)
142-
.set_scope(Some(SearchScope::single_file(file_id)))
139+
.in_scope(&SearchScope::single_file(file_id))
143140
.include_self_refs()
144141
.all()
145142
.references
@@ -183,7 +180,7 @@ fn highlight_references(
183180
.filter_map(|item| {
184181
Definition::from(item)
185182
.usages(sema)
186-
.set_scope(Some(SearchScope::file_range(FileRange {
183+
.set_scope(Some(&SearchScope::file_range(FileRange {
187184
file_id,
188185
range: trait_item_use_scope.text_range(),
189186
})))

crates/ide/src/references.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn find_all_refs(
7474
}
7575
});
7676
let mut usages =
77-
def.usages(sema).set_scope(search_scope.clone()).include_self_refs().all();
77+
def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all();
7878

7979
if literal_search {
8080
retain_adt_literal_usages(&mut usages, def, sema);

crates/ide/src/runnables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn find_related_tests(
232232
for def in defs {
233233
let defs = def
234234
.usages(sema)
235-
.set_scope(search_scope.clone())
235+
.set_scope(search_scope.as_ref())
236236
.all()
237237
.references
238238
.into_values()

0 commit comments

Comments
 (0)