@@ -181,22 +181,44 @@ impl Definition {
181
181
SearchScope :: new ( res)
182
182
}
183
183
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 > {
189
210
let _p = profile:: span ( "Definition::find_usages" ) ;
211
+ let sema = self . sema ;
190
212
191
213
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 {
194
216
None => base,
195
217
Some ( scope) => base. intersection ( & scope) ,
196
218
}
197
219
} ;
198
220
199
- let name = match self . name ( sema. db ) {
221
+ let name = match self . def . name ( sema. db ) {
200
222
None => return Vec :: new ( ) ,
201
223
Some ( it) => it. to_string ( ) ,
202
224
} ;
@@ -225,7 +247,7 @@ impl Definition {
225
247
} ;
226
248
227
249
match classify_name_ref ( & sema, & name_ref) {
228
- Some ( NameRefClass :: Definition ( def) ) if & def == self => {
250
+ Some ( NameRefClass :: Definition ( def) ) if & def == self . def => {
229
251
let kind = if is_record_lit_name_ref ( & name_ref)
230
252
|| is_call_expr_name_ref ( & name_ref)
231
253
{
@@ -242,14 +264,14 @@ impl Definition {
242
264
} ) ;
243
265
}
244
266
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 ( ) ) ,
248
270
kind : ReferenceKind :: FieldShorthandForField ,
249
271
access : reference_access ( & field, & name_ref) ,
250
272
} ) ,
251
273
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 ( ) ) ,
253
275
kind : ReferenceKind :: FieldShorthandForLocal ,
254
276
access : reference_access ( & Definition :: Local ( local) , & name_ref) ,
255
277
} ) ,
0 commit comments