@@ -208,6 +208,16 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
208
208
self . imp . descend_into_macros ( token)
209
209
}
210
210
211
+ /// Descend the token into macrocalls to all its mapped counterparts.
212
+ ///
213
+ /// Returns the original non descended token if none of the mapped counterparts have the same syntax kind.
214
+ pub fn descend_into_macros_with_same_kind (
215
+ & self ,
216
+ token : SyntaxToken ,
217
+ ) -> SmallVec < [ SyntaxToken ; 1 ] > {
218
+ self . imp . descend_into_macros_with_same_kind ( token)
219
+ }
220
+
211
221
/// Maps a node down by mapping its first and last token down.
212
222
pub fn descend_node_into_attributes < N : AstNode > ( & self , node : N ) -> SmallVec < [ N ; 1 ] > {
213
223
self . imp . descend_node_into_attributes ( node)
@@ -599,25 +609,19 @@ impl<'db> SemanticsImpl<'db> {
599
609
} ;
600
610
601
611
if first == last {
602
- self . descend_into_macros_impl (
603
- first,
604
- & mut |InFile { value, .. } | {
605
- if let Some ( node) = value. ancestors ( ) . find_map ( N :: cast) {
606
- res. push ( node)
607
- }
608
- } ,
609
- false ,
610
- ) ;
612
+ self . descend_into_macros_impl ( first, & mut |InFile { value, .. } | {
613
+ if let Some ( node) = value. ancestors ( ) . find_map ( N :: cast) {
614
+ res. push ( node)
615
+ }
616
+ false
617
+ } ) ;
611
618
} else {
612
619
// Descend first and last token, then zip them to look for the node they belong to
613
620
let mut scratch: SmallVec < [ _ ; 1 ] > = smallvec ! [ ] ;
614
- self . descend_into_macros_impl (
615
- first,
616
- & mut |token| {
617
- scratch. push ( token) ;
618
- } ,
619
- false ,
620
- ) ;
621
+ self . descend_into_macros_impl ( first, & mut |token| {
622
+ scratch. push ( token) ;
623
+ false
624
+ } ) ;
621
625
622
626
let mut scratch = scratch. into_iter ( ) ;
623
627
self . descend_into_macros_impl (
@@ -638,30 +642,50 @@ impl<'db> SemanticsImpl<'db> {
638
642
}
639
643
}
640
644
}
645
+ false
641
646
} ,
642
- false ,
643
647
) ;
644
648
}
645
649
res
646
650
}
647
651
648
652
fn descend_into_macros ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
649
653
let mut res = smallvec ! [ ] ;
650
- self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | res. push ( value) , false ) ;
654
+ self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | {
655
+ res. push ( value) ;
656
+ false
657
+ } ) ;
658
+ res
659
+ }
660
+
661
+ fn descend_into_macros_with_same_kind ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
662
+ let kind = token. kind ( ) ;
663
+ let mut res = smallvec ! [ ] ;
664
+ self . descend_into_macros_impl ( token. clone ( ) , & mut |InFile { value, .. } | {
665
+ if value. kind ( ) == kind {
666
+ res. push ( value) ;
667
+ }
668
+ false
669
+ } ) ;
670
+ if res. is_empty ( ) {
671
+ res. push ( token) ;
672
+ }
651
673
res
652
674
}
653
675
654
676
fn descend_into_macros_single ( & self , token : SyntaxToken ) -> SyntaxToken {
655
677
let mut res = token. clone ( ) ;
656
- self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | res = value, true ) ;
678
+ self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | {
679
+ res = value;
680
+ true
681
+ } ) ;
657
682
res
658
683
}
659
684
660
685
fn descend_into_macros_impl (
661
686
& self ,
662
687
token : SyntaxToken ,
663
- f : & mut dyn FnMut ( InFile < SyntaxToken > ) ,
664
- single : bool ,
688
+ f : & mut dyn FnMut ( InFile < SyntaxToken > ) -> bool ,
665
689
) {
666
690
let _p = profile:: span ( "descend_into_macros" ) ;
667
691
let parent = match token. parent ( ) {
@@ -688,16 +712,11 @@ impl<'db> SemanticsImpl<'db> {
688
712
self . cache ( value, file_id) ;
689
713
}
690
714
691
- let mut mapped_tokens =
692
- expansion_info. map_token_down ( self . db . upcast ( ) , item, token) ?;
693
-
715
+ let mapped_tokens = expansion_info. map_token_down ( self . db . upcast ( ) , item, token) ?;
694
716
let len = stack. len ( ) ;
717
+
695
718
// requeue the tokens we got from mapping our current token down
696
- if single {
697
- stack. extend ( mapped_tokens. next ( ) ) ;
698
- } else {
699
- stack. extend ( mapped_tokens) ;
700
- }
719
+ stack. extend ( mapped_tokens) ;
701
720
// if the length changed we have found a mapping for the token
702
721
( stack. len ( ) != len) . then ( || ( ) )
703
722
} ;
@@ -787,8 +806,8 @@ impl<'db> SemanticsImpl<'db> {
787
806
} ) ( )
788
807
. is_none ( ) ;
789
808
790
- if was_not_remapped {
791
- f ( token )
809
+ if was_not_remapped && f ( token ) {
810
+ break ;
792
811
}
793
812
}
794
813
}
0 commit comments