Skip to content

Commit f5b7540

Browse files
bors[bot]matklad
andauthored
Merge #5820
5820: Future proof find-usages API r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 686a6a2 + 81b0976 commit f5b7540

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

crates/assists/src/handlers/extract_struct_from_enum_variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn extract_struct_from_enum_variant(
5353
target,
5454
|builder| {
5555
let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir));
56-
let res = definition.find_usages(&ctx.sema, None);
56+
let res = definition.usages(&ctx.sema).all();
5757
let start_offset = variant.parent_enum().syntax().text_range().start();
5858
let mut visited_modules_set = FxHashSet::default();
5959
visited_modules_set.insert(current_module);

crates/assists/src/handlers/inline_local_variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
4444

4545
let def = ctx.sema.to_def(&bind_pat)?;
4646
let def = Definition::Local(def);
47-
let refs = def.find_usages(&ctx.sema, None);
47+
let refs = def.usages(&ctx.sema).all();
4848
if refs.is_empty() {
4949
mark::hit!(test_not_applicable_if_variable_unused);
5050
return None;

crates/ide/src/references.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ pub(crate) fn find_all_refs(
106106
let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
107107

108108
let references = def
109-
.find_usages(sema, search_scope)
109+
.usages(sema)
110+
.set_scope(search_scope)
111+
.all()
110112
.into_iter()
111113
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
112114
.collect();

crates/ide_db/src/search.rs

+35-13
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,44 @@ impl Definition {
181181
SearchScope::new(res)
182182
}
183183

184-
pub fn find_usages(
185-
&self,
186-
sema: &Semantics<RootDatabase>,
187-
search_scope: Option<SearchScope>,
188-
) -> Vec<Reference> {
184+
pub fn usages<'a>(&'a self, sema: &'a Semantics<RootDatabase>) -> FindUsages<'a> {
185+
FindUsages { def: self, sema, scope: None }
186+
}
187+
}
188+
189+
pub struct FindUsages<'a> {
190+
def: &'a Definition,
191+
sema: &'a Semantics<'a, RootDatabase>,
192+
scope: Option<SearchScope>,
193+
}
194+
195+
impl<'a> FindUsages<'a> {
196+
pub fn in_scope(self, scope: SearchScope) -> FindUsages<'a> {
197+
self.set_scope(Some(scope))
198+
}
199+
pub fn set_scope(mut self, scope: Option<SearchScope>) -> FindUsages<'a> {
200+
assert!(self.scope.is_none());
201+
self.scope = scope;
202+
self
203+
}
204+
205+
pub fn at_least_one(self) -> bool {
206+
self.all().is_empty()
207+
}
208+
209+
pub fn all(self) -> Vec<Reference> {
189210
let _p = profile::span("Definition::find_usages");
211+
let sema = self.sema;
190212

191213
let search_scope = {
192-
let base = self.search_scope(sema.db);
193-
match search_scope {
214+
let base = self.def.search_scope(sema.db);
215+
match self.scope {
194216
None => base,
195217
Some(scope) => base.intersection(&scope),
196218
}
197219
};
198220

199-
let name = match self.name(sema.db) {
221+
let name = match self.def.name(sema.db) {
200222
None => return Vec::new(),
201223
Some(it) => it.to_string(),
202224
};
@@ -225,7 +247,7 @@ impl Definition {
225247
};
226248

227249
match classify_name_ref(&sema, &name_ref) {
228-
Some(NameRefClass::Definition(def)) if &def == self => {
250+
Some(NameRefClass::Definition(def)) if &def == self.def => {
229251
let kind = if is_record_lit_name_ref(&name_ref)
230252
|| is_call_expr_name_ref(&name_ref)
231253
{
@@ -242,14 +264,14 @@ impl Definition {
242264
});
243265
}
244266
Some(NameRefClass::FieldShorthand { local, field }) => {
245-
match self {
246-
Definition::Field(_) if &field == self => refs.push(Reference {
247-
file_range: sema.original_range(name_ref.syntax()),
267+
match self.def {
268+
Definition::Field(_) if &field == self.def => refs.push(Reference {
269+
file_range: self.sema.original_range(name_ref.syntax()),
248270
kind: ReferenceKind::FieldShorthandForField,
249271
access: reference_access(&field, &name_ref),
250272
}),
251273
Definition::Local(l) if &local == l => refs.push(Reference {
252-
file_range: sema.original_range(name_ref.syntax()),
274+
file_range: self.sema.original_range(name_ref.syntax()),
253275
kind: ReferenceKind::FieldShorthandForLocal,
254276
access: reference_access(&Definition::Local(local), &name_ref),
255277
}),

crates/ssr/src/search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'db> MatchFinder<'db> {
114114
// cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two
115115
// lookups in the case of a cache hit.
116116
if usage_cache.find(&definition).is_none() {
117-
let usages = definition.find_usages(&self.sema, Some(self.search_scope()));
117+
let usages = definition.usages(&self.sema).in_scope(self.search_scope()).all();
118118
usage_cache.usages.push((definition, usages));
119119
return &usage_cache.usages.last().unwrap().1;
120120
}

0 commit comments

Comments
 (0)