@@ -446,33 +446,47 @@ impl<'a> FindUsages<'a> {
446
446
} )
447
447
}
448
448
449
- // FIXME: There should be optimization potential here
450
- // Currently we try to descend everything we find which
451
- // means we call `Semantics::descend_into_macros` on
452
- // every textual hit. That function is notoriously
453
- // expensive even for things that do not get down mapped
454
- // into macros.
449
+ let find_nodes = move |name : & str , node : & syntax:: SyntaxNode , offset : TextSize | {
450
+ node. token_at_offset ( offset) . find ( |it| it. text ( ) == name) . map ( |token| {
451
+ // FIXME: There should be optimization potential here
452
+ // Currently we try to descend everything we find which
453
+ // means we call `Semantics::descend_into_macros` on
454
+ // every textual hit. That function is notoriously
455
+ // expensive even for things that do not get down mapped
456
+ // into macros.
457
+ sema. descend_into_macros ( token) . into_iter ( ) . filter_map ( |it| it. parent ( ) )
458
+ } )
459
+ } ;
460
+
455
461
for ( text, file_id, search_range) in scope_files ( sema, & search_scope) {
456
462
let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
457
463
458
464
// Search for occurrences of the items name
459
465
for offset in match_indices ( & text, finder, search_range) {
460
- for name in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
461
- if match name {
462
- ast:: NameLike :: NameRef ( name_ref) => self . found_name_ref ( & name_ref, sink) ,
463
- ast:: NameLike :: Name ( name) => self . found_name ( & name, sink) ,
464
- ast:: NameLike :: Lifetime ( lifetime) => self . found_lifetime ( & lifetime, sink) ,
465
- } {
466
- return ;
466
+ if let Some ( iter) = find_nodes ( name, & tree, offset) {
467
+ for name in iter. filter_map ( ast:: NameLike :: cast) {
468
+ if match name {
469
+ ast:: NameLike :: NameRef ( name_ref) => {
470
+ self . found_name_ref ( & name_ref, sink)
471
+ }
472
+ ast:: NameLike :: Name ( name) => self . found_name ( & name, sink) ,
473
+ ast:: NameLike :: Lifetime ( lifetime) => {
474
+ self . found_lifetime ( & lifetime, sink)
475
+ }
476
+ } {
477
+ return ;
478
+ }
467
479
}
468
480
}
469
481
}
470
482
// Search for occurrences of the `Self` referring to our type
471
483
if let Some ( ( self_ty, finder) ) = & include_self_kw_refs {
472
484
for offset in match_indices ( & text, finder, search_range) {
473
- for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
474
- if self . found_self_ty_name_ref ( self_ty, & name_ref, sink) {
475
- return ;
485
+ if let Some ( iter) = find_nodes ( "self" , & tree, offset) {
486
+ for name_ref in iter. filter_map ( ast:: NameRef :: cast) {
487
+ if self . found_self_ty_name_ref ( self_ty, & name_ref, sink) {
488
+ return ;
489
+ }
476
490
}
477
491
}
478
492
}
@@ -493,17 +507,21 @@ impl<'a> FindUsages<'a> {
493
507
let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
494
508
495
509
for offset in match_indices ( & text, finder, search_range) {
496
- for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
497
- if self . found_name_ref ( & name_ref, sink) {
498
- return ;
510
+ if let Some ( iter) = find_nodes ( "super" , & tree, offset) {
511
+ for name_ref in iter. filter_map ( ast:: NameRef :: cast) {
512
+ if self . found_name_ref ( & name_ref, sink) {
513
+ return ;
514
+ }
499
515
}
500
516
}
501
517
}
502
518
if let Some ( finder) = & is_crate_root {
503
519
for offset in match_indices ( & text, finder, search_range) {
504
- for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
505
- if self . found_name_ref ( & name_ref, sink) {
506
- return ;
520
+ if let Some ( iter) = find_nodes ( "crate" , & tree, offset) {
521
+ for name_ref in iter. filter_map ( ast:: NameRef :: cast) {
522
+ if self . found_name_ref ( & name_ref, sink) {
523
+ return ;
524
+ }
507
525
}
508
526
}
509
527
}
@@ -544,9 +562,11 @@ impl<'a> FindUsages<'a> {
544
562
let finder = & Finder :: new ( "self" ) ;
545
563
546
564
for offset in match_indices ( & text, finder, search_range) {
547
- for name_ref in sema. find_nodes_at_offset_with_descend ( & tree, offset) {
548
- if self . found_self_module_name_ref ( & name_ref, sink) {
549
- return ;
565
+ if let Some ( iter) = find_nodes ( "self" , & tree, offset) {
566
+ for name_ref in iter. filter_map ( ast:: NameRef :: cast) {
567
+ if self . found_self_module_name_ref ( & name_ref, sink) {
568
+ return ;
569
+ }
550
570
}
551
571
}
552
572
}
0 commit comments