@@ -446,33 +446,47 @@ impl<'a> FindUsages<'a> {
446446 } )
447447 }
448448
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+
455461 for ( text, file_id, search_range) in scope_files ( sema, & search_scope) {
456462 let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
457463
458464 // Search for occurrences of the items name
459465 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+ }
467479 }
468480 }
469481 }
470482 // Search for occurrences of the `Self` referring to our type
471483 if let Some ( ( self_ty, finder) ) = & include_self_kw_refs {
472484 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+ }
476490 }
477491 }
478492 }
@@ -493,17 +507,21 @@ impl<'a> FindUsages<'a> {
493507 let tree = Lazy :: new ( move || sema. parse ( file_id) . syntax ( ) . clone ( ) ) ;
494508
495509 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+ }
499515 }
500516 }
501517 }
502518 if let Some ( finder) = & is_crate_root {
503519 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+ }
507525 }
508526 }
509527 }
@@ -544,9 +562,11 @@ impl<'a> FindUsages<'a> {
544562 let finder = & Finder :: new ( "self" ) ;
545563
546564 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+ }
550570 }
551571 }
552572 }
0 commit comments