aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src
diff options
context:
space:
mode:
authorunexge <[email protected]>2021-04-21 14:42:47 +0100
committerunexge <[email protected]>2021-04-21 14:42:47 +0100
commit322cd1fa7fb870f44860bc42ef3b894e5a42bb70 (patch)
treea103767f932ae5b65383238acf73b765b7f11451 /crates/ide_db/src
parent6630266ce17112bb42f2fa62f975d53512ace682 (diff)
Use multiple loops instead of `Iterator::chain` in `FindUsages`
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r--crates/ide_db/src/search.rs35
1 files changed, 22 insertions, 13 deletions
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs
index 90e4e7b03..8f899ea56 100644
--- a/crates/ide_db/src/search.rs
+++ b/crates/ide_db/src/search.rs
@@ -4,10 +4,9 @@
4//! get a super-set of matches. Then, we we confirm each match using precise 4//! get a super-set of matches. Then, we we confirm each match using precise
5//! name resolution. 5//! name resolution.
6 6
7use std::{convert::TryInto, iter, mem}; 7use std::{convert::TryInto, mem};
8 8
9use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt}; 9use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
10use either::Either;
11use hir::{ 10use hir::{
12 DefWithBody, HasAttrs, HasSource, InFile, ModuleDef, ModuleSource, Semantics, Visibility, 11 DefWithBody, HasAttrs, HasSource, InFile, ModuleDef, ModuleSource, Semantics, Visibility,
13}; 12};
@@ -370,37 +369,47 @@ impl<'a> FindUsages<'a> {
370 369
371 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); 370 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
372 371
373 let matches = text.match_indices(pat).chain(if search_for_self { 372 let mut handle_match = |idx: usize| -> bool {
374 Either::Left(text.match_indices("Self"))
375 } else {
376 Either::Right(iter::empty())
377 });
378
379 for (idx, _) in matches {
380 let offset: TextSize = idx.try_into().unwrap(); 373 let offset: TextSize = idx.try_into().unwrap();
381 if !search_range.contains_inclusive(offset) { 374 if !search_range.contains_inclusive(offset) {
382 continue; 375 return false;
383 } 376 }
384 377
385 if let Some(name) = sema.find_node_at_offset_with_descend(&tree, offset) { 378 if let Some(name) = sema.find_node_at_offset_with_descend(&tree, offset) {
386 match name { 379 match name {
387 ast::NameLike::NameRef(name_ref) => { 380 ast::NameLike::NameRef(name_ref) => {
388 if self.found_name_ref(&name_ref, sink) { 381 if self.found_name_ref(&name_ref, sink) {
389 return; 382 return true;
390 } 383 }
391 } 384 }
392 ast::NameLike::Name(name) => { 385 ast::NameLike::Name(name) => {
393 if self.found_name(&name, sink) { 386 if self.found_name(&name, sink) {
394 return; 387 return true;
395 } 388 }
396 } 389 }
397 ast::NameLike::Lifetime(lifetime) => { 390 ast::NameLike::Lifetime(lifetime) => {
398 if self.found_lifetime(&lifetime, sink) { 391 if self.found_lifetime(&lifetime, sink) {
399 return; 392 return true;
400 } 393 }
401 } 394 }
402 } 395 }
403 } 396 }
397
398 return false;
399 };
400
401 for (idx, _) in text.match_indices(pat) {
402 if handle_match(idx) {
403 return;
404 }
405 }
406
407 if search_for_self {
408 for (idx, _) in text.match_indices("Self") {
409 if handle_match(idx) {
410 return;
411 }
412 }
404 } 413 }
405 } 414 }
406 } 415 }