diff options
Diffstat (limited to 'crates/ide_db/src/search.rs')
-rw-r--r-- | crates/ide_db/src/search.rs | 113 |
1 files changed, 74 insertions, 39 deletions
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 436c59d2c..b5fa46642 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -18,9 +18,43 @@ use crate::{ | |||
18 | RootDatabase, | 18 | RootDatabase, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | #[derive(Debug, Default, Clone)] | ||
22 | pub struct UsageSearchResult { | ||
23 | pub references: FxHashMap<FileId, Vec<FileReference>>, | ||
24 | } | ||
25 | |||
26 | impl UsageSearchResult { | ||
27 | pub fn is_empty(&self) -> bool { | ||
28 | self.references.is_empty() | ||
29 | } | ||
30 | |||
31 | pub fn len(&self) -> usize { | ||
32 | self.references.len() | ||
33 | } | ||
34 | |||
35 | pub fn iter(&self) -> impl Iterator<Item = (&FileId, &Vec<FileReference>)> + '_ { | ||
36 | self.references.iter() | ||
37 | } | ||
38 | |||
39 | pub fn file_ranges(&self) -> impl Iterator<Item = FileRange> + '_ { | ||
40 | self.references.iter().flat_map(|(&file_id, refs)| { | ||
41 | refs.iter().map(move |&FileReference { range, .. }| FileRange { file_id, range }) | ||
42 | }) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl IntoIterator for UsageSearchResult { | ||
47 | type Item = (FileId, Vec<FileReference>); | ||
48 | type IntoIter = <FxHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter; | ||
49 | |||
50 | fn into_iter(self) -> Self::IntoIter { | ||
51 | self.references.into_iter() | ||
52 | } | ||
53 | } | ||
54 | |||
21 | #[derive(Debug, Clone)] | 55 | #[derive(Debug, Clone)] |
22 | pub struct Reference { | 56 | pub struct FileReference { |
23 | pub file_range: FileRange, | 57 | pub range: TextRange, |
24 | pub kind: ReferenceKind, | 58 | pub kind: ReferenceKind, |
25 | pub access: Option<ReferenceAccess>, | 59 | pub access: Option<ReferenceAccess>, |
26 | } | 60 | } |
@@ -136,8 +170,7 @@ impl Definition { | |||
136 | return SearchScope::new(res); | 170 | return SearchScope::new(res); |
137 | } | 171 | } |
138 | 172 | ||
139 | if let Definition::LifetimeParam(param) = self { | 173 | if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self { |
140 | #[allow(deprecated)] | ||
141 | let range = match param.parent(db) { | 174 | let range = match param.parent(db) { |
142 | hir::GenericDef::Function(it) => { | 175 | hir::GenericDef::Function(it) => { |
143 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | 176 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) |
@@ -253,23 +286,23 @@ impl<'a> FindUsages<'a> { | |||
253 | 286 | ||
254 | pub fn at_least_one(self) -> bool { | 287 | pub fn at_least_one(self) -> bool { |
255 | let mut found = false; | 288 | let mut found = false; |
256 | self.search(&mut |_reference| { | 289 | self.search(&mut |_, _| { |
257 | found = true; | 290 | found = true; |
258 | true | 291 | true |
259 | }); | 292 | }); |
260 | found | 293 | found |
261 | } | 294 | } |
262 | 295 | ||
263 | pub fn all(self) -> Vec<Reference> { | 296 | pub fn all(self) -> UsageSearchResult { |
264 | let mut res = Vec::new(); | 297 | let mut res = UsageSearchResult::default(); |
265 | self.search(&mut |reference| { | 298 | self.search(&mut |file_id, reference| { |
266 | res.push(reference); | 299 | res.references.entry(file_id).or_default().push(reference); |
267 | false | 300 | false |
268 | }); | 301 | }); |
269 | res | 302 | res |
270 | } | 303 | } |
271 | 304 | ||
272 | fn search(self, sink: &mut dyn FnMut(Reference) -> bool) { | 305 | fn search(self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) { |
273 | let _p = profile::span("FindUsages:search"); | 306 | let _p = profile::span("FindUsages:search"); |
274 | let sema = self.sema; | 307 | let sema = self.sema; |
275 | 308 | ||
@@ -321,16 +354,14 @@ impl<'a> FindUsages<'a> { | |||
321 | fn found_lifetime( | 354 | fn found_lifetime( |
322 | &self, | 355 | &self, |
323 | lifetime: &ast::Lifetime, | 356 | lifetime: &ast::Lifetime, |
324 | sink: &mut dyn FnMut(Reference) -> bool, | 357 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
325 | ) -> bool { | 358 | ) -> bool { |
326 | match NameRefClass::classify_lifetime(self.sema, lifetime) { | 359 | match NameRefClass::classify_lifetime(self.sema, lifetime) { |
327 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 360 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
328 | let reference = Reference { | 361 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); |
329 | file_range: self.sema.original_range(lifetime.syntax()), | 362 | let reference = |
330 | kind: ReferenceKind::Lifetime, | 363 | FileReference { range, kind: ReferenceKind::Lifetime, access: None }; |
331 | access: None, | 364 | sink(file_id, reference) |
332 | }; | ||
333 | sink(reference) | ||
334 | } | 365 | } |
335 | _ => false, // not a usage | 366 | _ => false, // not a usage |
336 | } | 367 | } |
@@ -339,7 +370,7 @@ impl<'a> FindUsages<'a> { | |||
339 | fn found_name_ref( | 370 | fn found_name_ref( |
340 | &self, | 371 | &self, |
341 | name_ref: &ast::NameRef, | 372 | name_ref: &ast::NameRef, |
342 | sink: &mut dyn FnMut(Reference) -> bool, | 373 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
343 | ) -> bool { | 374 | ) -> bool { |
344 | match NameRefClass::classify(self.sema, &name_ref) { | 375 | match NameRefClass::classify(self.sema, &name_ref) { |
345 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 376 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
@@ -353,46 +384,50 @@ impl<'a> FindUsages<'a> { | |||
353 | ReferenceKind::Other | 384 | ReferenceKind::Other |
354 | }; | 385 | }; |
355 | 386 | ||
356 | let reference = Reference { | 387 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); |
357 | file_range: self.sema.original_range(name_ref.syntax()), | 388 | let reference = |
358 | kind, | 389 | FileReference { range, kind, access: reference_access(&def, &name_ref) }; |
359 | access: reference_access(&def, &name_ref), | 390 | sink(file_id, reference) |
360 | }; | ||
361 | sink(reference) | ||
362 | } | 391 | } |
363 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { | 392 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { |
393 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); | ||
364 | let reference = match self.def { | 394 | let reference = match self.def { |
365 | Definition::Field(_) if &field == self.def => Reference { | 395 | Definition::Field(_) if &field == self.def => FileReference { |
366 | file_range: self.sema.original_range(name_ref.syntax()), | 396 | range, |
367 | kind: ReferenceKind::FieldShorthandForField, | 397 | kind: ReferenceKind::FieldShorthandForField, |
368 | access: reference_access(&field, &name_ref), | 398 | access: reference_access(&field, &name_ref), |
369 | }, | 399 | }, |
370 | Definition::Local(l) if &local == l => Reference { | 400 | Definition::Local(l) if &local == l => FileReference { |
371 | file_range: self.sema.original_range(name_ref.syntax()), | 401 | range, |
372 | kind: ReferenceKind::FieldShorthandForLocal, | 402 | kind: ReferenceKind::FieldShorthandForLocal, |
373 | access: reference_access(&Definition::Local(local), &name_ref), | 403 | access: reference_access(&Definition::Local(local), &name_ref), |
374 | }, | 404 | }, |
375 | _ => return false, // not a usage | 405 | _ => return false, // not a usage |
376 | }; | 406 | }; |
377 | sink(reference) | 407 | sink(file_id, reference) |
378 | } | 408 | } |
379 | _ => false, // not a usage | 409 | _ => false, // not a usage |
380 | } | 410 | } |
381 | } | 411 | } |
382 | 412 | ||
383 | fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { | 413 | fn found_name( |
414 | &self, | ||
415 | name: &ast::Name, | ||
416 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, | ||
417 | ) -> bool { | ||
384 | match NameClass::classify(self.sema, name) { | 418 | match NameClass::classify(self.sema, name) { |
385 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { | 419 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { |
386 | let reference = match self.def { | 420 | if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) { |
387 | Definition::Field(_) if &field_ref == self.def => Reference { | 421 | return false; |
388 | file_range: self.sema.original_range(name.syntax()), | 422 | } |
389 | kind: ReferenceKind::FieldShorthandForField, | 423 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
390 | // FIXME: mutable patterns should have `Write` access | 424 | let reference = FileReference { |
391 | access: Some(ReferenceAccess::Read), | 425 | range, |
392 | }, | 426 | kind: ReferenceKind::FieldShorthandForField, |
393 | _ => return false, // not a usage | 427 | // FIXME: mutable patterns should have `Write` access |
428 | access: Some(ReferenceAccess::Read), | ||
394 | }; | 429 | }; |
395 | sink(reference) | 430 | sink(file_id, reference) |
396 | } | 431 | } |
397 | _ => false, // not a usage | 432 | _ => false, // not a usage |
398 | } | 433 | } |