@@ -8,6 +8,7 @@ use std::{mem, sync::Arc};
8
8
9
9
use base_db:: { FileId , FileRange , SourceDatabase , SourceDatabaseExt } ;
10
10
use hir:: { DefWithBody , HasAttrs , HasSource , InFile , ModuleSource , Semantics , Visibility } ;
11
+ use memchr:: memmem:: Finder ;
11
12
use once_cell:: unsync:: Lazy ;
12
13
use parser:: SyntaxKind ;
13
14
use stdx:: hash:: NoHashHashMap ;
@@ -411,14 +412,17 @@ impl<'a> FindUsages<'a> {
411
412
Some ( s) => s. as_str ( ) ,
412
413
None => return ,
413
414
} ;
415
+ let finder = & Finder :: new ( name) ;
416
+ let include_self_kw_refs =
417
+ self . include_self_kw_refs . as_ref ( ) . map ( |ty| ( ty, Finder :: new ( "Self" ) ) ) ;
414
418
415
- // these can't be closures because rust infers the lifetimes wrong ...
419
+ // for<'a> |text: &'a str, name: &'a str, search_range: TextRange| -> impl Iterator<Item = TextSize> + 'a { ... }
416
420
fn match_indices < ' a > (
417
421
text : & ' a str ,
418
- name : & ' a str ,
422
+ finder : & ' a Finder < ' a > ,
419
423
search_range : TextRange ,
420
424
) -> impl Iterator < Item = TextSize > + ' a {
421
- text. match_indices ( name ) . filter_map ( move |( idx, _ ) | {
425
+ finder . find_iter ( text. as_bytes ( ) ) . filter_map ( move |idx| {
422
426
let offset: TextSize = idx. try_into ( ) . unwrap ( ) ;
423
427
if !search_range. contains_inclusive ( offset) {
424
428
return None ;
@@ -427,6 +431,7 @@ impl<'a> FindUsages<'a> {
427
431
} )
428
432
}
429
433
434
+ // for<'a> |scope: &'a SearchScope| -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a { ... }
430
435
fn scope_files < ' a > (
431
436
sema : & ' a Semantics < ' _ , RootDatabase > ,
432
437
scope : & ' a SearchScope ,
@@ -450,7 +455,7 @@ impl<'a> FindUsages<'a> {
450
455
let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
451
456
452
457
// Search for occurrences of the items name
453
- for offset in match_indices ( & text, name , search_range) {
458
+ for offset in match_indices ( & text, finder , search_range) {
454
459
for name in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
455
460
if match name {
456
461
ast:: NameLike :: NameRef ( name_ref) => self . found_name_ref ( & name_ref, sink) ,
@@ -462,8 +467,8 @@ impl<'a> FindUsages<'a> {
462
467
}
463
468
}
464
469
// Search for occurrences of the `Self` referring to our type
465
- if let Some ( self_ty) = & self . include_self_kw_refs {
466
- for offset in match_indices ( & text, "Self" , search_range) {
470
+ if let Some ( ( self_ty, finder ) ) = & include_self_kw_refs {
471
+ for offset in match_indices ( & text, finder , search_range) {
467
472
for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
468
473
if self . found_self_ty_name_ref ( self_ty, & name_ref, sink) {
469
474
return ;
@@ -479,20 +484,22 @@ impl<'a> FindUsages<'a> {
479
484
let scope = search_scope
480
485
. intersection ( & SearchScope :: module_and_children ( self . sema . db , module) ) ;
481
486
482
- let is_crate_root = module. is_crate_root ( self . sema . db ) ;
487
+ let is_crate_root =
488
+ module. is_crate_root ( self . sema . db ) . then ( || Finder :: new ( "crate" ) ) ;
489
+ let finder = & Finder :: new ( "super" ) ;
483
490
484
491
for ( text, file_id, search_range) in scope_files ( sema, & scope) {
485
492
let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
486
493
487
- for offset in match_indices ( & text, "super" , search_range) {
494
+ for offset in match_indices ( & text, finder , search_range) {
488
495
for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
489
496
if self . found_name_ref ( & name_ref, sink) {
490
497
return ;
491
498
}
492
499
}
493
500
}
494
- if is_crate_root {
495
- for offset in match_indices ( & text, "crate" , search_range) {
501
+ if let Some ( finder ) = & is_crate_root {
502
+ for offset in match_indices ( & text, finder , search_range) {
496
503
for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
497
504
if self . found_name_ref ( & name_ref, sink) {
498
505
return ;
@@ -533,8 +540,9 @@ impl<'a> FindUsages<'a> {
533
540
search_range. unwrap_or_else ( || TextRange :: up_to ( TextSize :: of ( text. as_str ( ) ) ) ) ;
534
541
535
542
let tree = Lazy :: new ( || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
543
+ let finder = & Finder :: new ( "self" ) ;
536
544
537
- for offset in match_indices ( & text, "self" , search_range) {
545
+ for offset in match_indices ( & text, finder , search_range) {
538
546
for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
539
547
if self . found_self_module_name_ref ( & name_ref, sink) {
540
548
return ;
0 commit comments