aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-13 10:38:30 +0000
committerGitHub <[email protected]>2021-01-13 10:38:30 +0000
commite41b36302784fc2661b4be2e13cb9a54cfa08ac9 (patch)
tree34e38f7297ab93686f270bb03607e9e3590a6597 /crates/ide_db
parent52fa926f005890f07dffc789c84c2be57a6bdccc (diff)
parentaff9102afb2b6756b3935dedb9be30401975b262 (diff)
Merge #7251
7251: Group references by FileId r=matklad a=Veykril Fixes #4901 This doesn't address https://github.com/rust-analyzer/rust-analyzer/pull/7032/files#diff-a7e1e771e911237bb893e1b0f5e0f2c2a856b54ca06f95ef0818a922f1a8b5ebR266 Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/search.rs110
1 files changed, 73 insertions, 37 deletions
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs
index 773bfbc2c..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)]
22pub struct UsageSearchResult {
23 pub references: FxHashMap<FileId, Vec<FileReference>>,
24}
25
26impl 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
46impl 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)]
22pub struct Reference { 56pub 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}
@@ -252,23 +286,23 @@ impl<'a> FindUsages<'a> {
252 286
253 pub fn at_least_one(self) -> bool { 287 pub fn at_least_one(self) -> bool {
254 let mut found = false; 288 let mut found = false;
255 self.search(&mut |_reference| { 289 self.search(&mut |_, _| {
256 found = true; 290 found = true;
257 true 291 true
258 }); 292 });
259 found 293 found
260 } 294 }
261 295
262 pub fn all(self) -> Vec<Reference> { 296 pub fn all(self) -> UsageSearchResult {
263 let mut res = Vec::new(); 297 let mut res = UsageSearchResult::default();
264 self.search(&mut |reference| { 298 self.search(&mut |file_id, reference| {
265 res.push(reference); 299 res.references.entry(file_id).or_default().push(reference);
266 false 300 false
267 }); 301 });
268 res 302 res
269 } 303 }
270 304
271 fn search(self, sink: &mut dyn FnMut(Reference) -> bool) { 305 fn search(self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
272 let _p = profile::span("FindUsages:search"); 306 let _p = profile::span("FindUsages:search");
273 let sema = self.sema; 307 let sema = self.sema;
274 308
@@ -320,16 +354,14 @@ impl<'a> FindUsages<'a> {
320 fn found_lifetime( 354 fn found_lifetime(
321 &self, 355 &self,
322 lifetime: &ast::Lifetime, 356 lifetime: &ast::Lifetime,
323 sink: &mut dyn FnMut(Reference) -> bool, 357 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
324 ) -> bool { 358 ) -> bool {
325 match NameRefClass::classify_lifetime(self.sema, lifetime) { 359 match NameRefClass::classify_lifetime(self.sema, lifetime) {
326 Some(NameRefClass::Definition(def)) if &def == self.def => { 360 Some(NameRefClass::Definition(def)) if &def == self.def => {
327 let reference = Reference { 361 let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax());
328 file_range: self.sema.original_range(lifetime.syntax()), 362 let reference =
329 kind: ReferenceKind::Lifetime, 363 FileReference { range, kind: ReferenceKind::Lifetime, access: None };
330 access: None, 364 sink(file_id, reference)
331 };
332 sink(reference)
333 } 365 }
334 _ => false, // not a usage 366 _ => false, // not a usage
335 } 367 }
@@ -338,7 +370,7 @@ impl<'a> FindUsages<'a> {
338 fn found_name_ref( 370 fn found_name_ref(
339 &self, 371 &self,
340 name_ref: &ast::NameRef, 372 name_ref: &ast::NameRef,
341 sink: &mut dyn FnMut(Reference) -> bool, 373 sink: &mut dyn FnMut(FileId, FileReference) -> bool,
342 ) -> bool { 374 ) -> bool {
343 match NameRefClass::classify(self.sema, &name_ref) { 375 match NameRefClass::classify(self.sema, &name_ref) {
344 Some(NameRefClass::Definition(def)) if &def == self.def => { 376 Some(NameRefClass::Definition(def)) if &def == self.def => {
@@ -352,46 +384,50 @@ impl<'a> FindUsages<'a> {
352 ReferenceKind::Other 384 ReferenceKind::Other
353 }; 385 };
354 386
355 let reference = Reference { 387 let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
356 file_range: self.sema.original_range(name_ref.syntax()), 388 let reference =
357 kind, 389 FileReference { range, kind, access: reference_access(&def, &name_ref) };
358 access: reference_access(&def, &name_ref), 390 sink(file_id, reference)
359 };
360 sink(reference)
361 } 391 }
362 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());
363 let reference = match self.def { 394 let reference = match self.def {
364 Definition::Field(_) if &field == self.def => Reference { 395 Definition::Field(_) if &field == self.def => FileReference {
365 file_range: self.sema.original_range(name_ref.syntax()), 396 range,
366 kind: ReferenceKind::FieldShorthandForField, 397 kind: ReferenceKind::FieldShorthandForField,
367 access: reference_access(&field, &name_ref), 398 access: reference_access(&field, &name_ref),
368 }, 399 },
369 Definition::Local(l) if &local == l => Reference { 400 Definition::Local(l) if &local == l => FileReference {
370 file_range: self.sema.original_range(name_ref.syntax()), 401 range,
371 kind: ReferenceKind::FieldShorthandForLocal, 402 kind: ReferenceKind::FieldShorthandForLocal,
372 access: reference_access(&Definition::Local(local), &name_ref), 403 access: reference_access(&Definition::Local(local), &name_ref),
373 }, 404 },
374 _ => return false, // not a usage 405 _ => return false, // not a usage
375 }; 406 };
376 sink(reference) 407 sink(file_id, reference)
377 } 408 }
378 _ => false, // not a usage 409 _ => false, // not a usage
379 } 410 }
380 } 411 }
381 412
382 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 {
383 match NameClass::classify(self.sema, name) { 418 match NameClass::classify(self.sema, name) {
384 Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { 419 Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => {
385 let reference = match self.def { 420 if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) {
386 Definition::Field(_) if &field_ref == self.def => Reference { 421 return false;
387 file_range: self.sema.original_range(name.syntax()), 422 }
388 kind: ReferenceKind::FieldShorthandForField, 423 let FileRange { file_id, range } = self.sema.original_range(name.syntax());
389 // FIXME: mutable patterns should have `Write` access 424 let reference = FileReference {
390 access: Some(ReferenceAccess::Read), 425 range,
391 }, 426 kind: ReferenceKind::FieldShorthandForField,
392 _ => return false, // not a usage 427 // FIXME: mutable patterns should have `Write` access
428 access: Some(ReferenceAccess::Read),
393 }; 429 };
394 sink(reference) 430 sink(file_id, reference)
395 } 431 }
396 _ => false, // not a usage 432 _ => false, // not a usage
397 } 433 }