@@ -170,6 +170,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
170
170
self . imp . is_derive_annotated ( item)
171
171
}
172
172
173
+ /// Expand the macro call with a different token tree, mapping the `token_to_map` down into the
174
+ /// expansion. `token_to_map` should be a token from the `speculative args` node.
173
175
pub fn speculative_expand (
174
176
& self ,
175
177
actual_macro_call : & ast:: MacroCall ,
@@ -179,6 +181,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
179
181
self . imp . speculative_expand ( actual_macro_call, speculative_args, token_to_map)
180
182
}
181
183
184
+ /// Expand the macro call with a different item as the input, mapping the `token_to_map` down into the
185
+ /// expansion. `token_to_map` should be a token from the `speculative args` node.
182
186
pub fn speculative_expand_attr_macro (
183
187
& self ,
184
188
actual_macro_call : & ast:: Item ,
@@ -201,14 +205,22 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
201
205
)
202
206
}
203
207
204
- /// Descend the token into macrocalls to its first mapped counterpart.
205
- pub fn descend_into_macros_single ( & self , token : SyntaxToken ) -> SyntaxToken {
206
- self . imp . descend_into_macros_single ( token)
208
+ /// Descend the token into its macro call if it is part of one, returning the token in the
209
+ /// expansion that it is associated with. If `offset` points into the token's range, it will
210
+ /// be considered for the mapping in case of inline format args.
211
+ pub fn descend_into_macros_single ( & self , token : SyntaxToken , offset : TextSize ) -> SyntaxToken {
212
+ self . imp . descend_into_macros_single ( token, offset)
207
213
}
208
214
209
- /// Descend the token into macrocalls to all its mapped counterparts.
210
- pub fn descend_into_macros ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
211
- self . imp . descend_into_macros ( token)
215
+ /// Descend the token into its macro call if it is part of one, returning the tokens in the
216
+ /// expansion that it is associated with. If `offset` points into the token's range, it will
217
+ /// be considered for the mapping in case of inline format args.
218
+ pub fn descend_into_macros (
219
+ & self ,
220
+ token : SyntaxToken ,
221
+ offset : TextSize ,
222
+ ) -> SmallVec < [ SyntaxToken ; 1 ] > {
223
+ self . imp . descend_into_macros ( token, offset)
212
224
}
213
225
214
226
/// Descend the token into macrocalls to all its mapped counterparts that have the same text as the input token.
@@ -217,12 +229,17 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
217
229
pub fn descend_into_macros_with_same_text (
218
230
& self ,
219
231
token : SyntaxToken ,
232
+ offset : TextSize ,
220
233
) -> SmallVec < [ SyntaxToken ; 1 ] > {
221
- self . imp . descend_into_macros_with_same_text ( token)
234
+ self . imp . descend_into_macros_with_same_text ( token, offset )
222
235
}
223
236
224
- pub fn descend_into_macros_with_kind_preference ( & self , token : SyntaxToken ) -> SyntaxToken {
225
- self . imp . descend_into_macros_with_kind_preference ( token)
237
+ pub fn descend_into_macros_with_kind_preference (
238
+ & self ,
239
+ token : SyntaxToken ,
240
+ offset : TextSize ,
241
+ ) -> SyntaxToken {
242
+ self . imp . descend_into_macros_with_kind_preference ( token, offset)
226
243
}
227
244
228
245
/// Maps a node down by mapping its first and last token down.
@@ -665,7 +682,7 @@ impl<'db> SemanticsImpl<'db> {
665
682
} ;
666
683
667
684
if first == last {
668
- self . descend_into_macros_impl ( first, & mut |InFile { value, .. } | {
685
+ self . descend_into_macros_impl ( first, 0 . into ( ) , & mut |InFile { value, .. } | {
669
686
if let Some ( node) = value. parent_ancestors ( ) . find_map ( N :: cast) {
670
687
res. push ( node)
671
688
}
@@ -674,14 +691,15 @@ impl<'db> SemanticsImpl<'db> {
674
691
} else {
675
692
// Descend first and last token, then zip them to look for the node they belong to
676
693
let mut scratch: SmallVec < [ _ ; 1 ] > = smallvec ! [ ] ;
677
- self . descend_into_macros_impl ( first, & mut |token| {
694
+ self . descend_into_macros_impl ( first, 0 . into ( ) , & mut |token| {
678
695
scratch. push ( token) ;
679
696
false
680
697
} ) ;
681
698
682
699
let mut scratch = scratch. into_iter ( ) ;
683
700
self . descend_into_macros_impl (
684
701
last,
702
+ 0 . into ( ) ,
685
703
& mut |InFile { value : last, file_id : last_fid } | {
686
704
if let Some ( InFile { value : first, file_id : first_fid } ) = scratch. next ( ) {
687
705
if first_fid == last_fid {
@@ -705,19 +723,27 @@ impl<'db> SemanticsImpl<'db> {
705
723
res
706
724
}
707
725
708
- fn descend_into_macros ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
726
+ fn descend_into_macros (
727
+ & self ,
728
+ token : SyntaxToken ,
729
+ offset : TextSize ,
730
+ ) -> SmallVec < [ SyntaxToken ; 1 ] > {
709
731
let mut res = smallvec ! [ ] ;
710
- self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | {
732
+ self . descend_into_macros_impl ( token, offset , & mut |InFile { value, .. } | {
711
733
res. push ( value) ;
712
734
false
713
735
} ) ;
714
736
res
715
737
}
716
738
717
- fn descend_into_macros_with_same_text ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
739
+ fn descend_into_macros_with_same_text (
740
+ & self ,
741
+ token : SyntaxToken ,
742
+ offset : TextSize ,
743
+ ) -> SmallVec < [ SyntaxToken ; 1 ] > {
718
744
let text = token. text ( ) ;
719
745
let mut res = smallvec ! [ ] ;
720
- self . descend_into_macros_impl ( token. clone ( ) , & mut |InFile { value, .. } | {
746
+ self . descend_into_macros_impl ( token. clone ( ) , offset , & mut |InFile { value, .. } | {
721
747
if value. text ( ) == text {
722
748
res. push ( value) ;
723
749
}
@@ -729,7 +755,11 @@ impl<'db> SemanticsImpl<'db> {
729
755
res
730
756
}
731
757
732
- fn descend_into_macros_with_kind_preference ( & self , token : SyntaxToken ) -> SyntaxToken {
758
+ fn descend_into_macros_with_kind_preference (
759
+ & self ,
760
+ token : SyntaxToken ,
761
+ offset : TextSize ,
762
+ ) -> SyntaxToken {
733
763
let fetch_kind = |token : & SyntaxToken | match token. parent ( ) {
734
764
Some ( node) => match node. kind ( ) {
735
765
kind @ ( SyntaxKind :: NAME | SyntaxKind :: NAME_REF ) => {
@@ -741,7 +771,7 @@ impl<'db> SemanticsImpl<'db> {
741
771
} ;
742
772
let preferred_kind = fetch_kind ( & token) ;
743
773
let mut res = None ;
744
- self . descend_into_macros_impl ( token. clone ( ) , & mut |InFile { value, .. } | {
774
+ self . descend_into_macros_impl ( token. clone ( ) , offset , & mut |InFile { value, .. } | {
745
775
if fetch_kind ( & value) == preferred_kind {
746
776
res = Some ( value) ;
747
777
true
@@ -755,9 +785,9 @@ impl<'db> SemanticsImpl<'db> {
755
785
res. unwrap_or ( token)
756
786
}
757
787
758
- fn descend_into_macros_single ( & self , token : SyntaxToken ) -> SyntaxToken {
788
+ fn descend_into_macros_single ( & self , token : SyntaxToken , offset : TextSize ) -> SyntaxToken {
759
789
let mut res = token. clone ( ) ;
760
- self . descend_into_macros_impl ( token, & mut |InFile { value, .. } | {
790
+ self . descend_into_macros_impl ( token, offset , & mut |InFile { value, .. } | {
761
791
res = value;
762
792
true
763
793
} ) ;
@@ -767,9 +797,13 @@ impl<'db> SemanticsImpl<'db> {
767
797
fn descend_into_macros_impl (
768
798
& self ,
769
799
token : SyntaxToken ,
800
+ // FIXME: We might want this to be Option<TextSize> to be able to opt out of subrange
801
+ // mapping, specifically for node downmapping
802
+ offset : TextSize ,
770
803
f : & mut dyn FnMut ( InFile < SyntaxToken > ) -> bool ,
771
804
) {
772
805
let _p = profile:: span ( "descend_into_macros" ) ;
806
+ let relative_token_offset = token. text_range ( ) . start ( ) . checked_sub ( offset) ;
773
807
let parent = match token. parent ( ) {
774
808
Some ( it) => it,
775
809
None => return ,
@@ -796,7 +830,12 @@ impl<'db> SemanticsImpl<'db> {
796
830
self . cache ( value, file_id) ;
797
831
}
798
832
799
- let mapped_tokens = expansion_info. map_token_down ( self . db . upcast ( ) , item, token) ?;
833
+ let mapped_tokens = expansion_info. map_token_down (
834
+ self . db . upcast ( ) ,
835
+ item,
836
+ token,
837
+ relative_token_offset,
838
+ ) ?;
800
839
let len = stack. len ( ) ;
801
840
802
841
// requeue the tokens we got from mapping our current token down
@@ -943,7 +982,7 @@ impl<'db> SemanticsImpl<'db> {
943
982
offset : TextSize ,
944
983
) -> impl Iterator < Item = impl Iterator < Item = SyntaxNode > + ' _ > + ' _ {
945
984
node. token_at_offset ( offset)
946
- . map ( move |token| self . descend_into_macros ( token) )
985
+ . map ( move |token| self . descend_into_macros ( token, offset ) )
947
986
. map ( |descendants| {
948
987
descendants. into_iter ( ) . map ( move |it| self . token_ancestors_with_macros ( it) )
949
988
} )
0 commit comments