@@ -170,6 +170,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
170170 self . imp . is_derive_annotated ( item)
171171 }
172172
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.
173175 pub fn speculative_expand (
174176 & self ,
175177 actual_macro_call : & ast:: MacroCall ,
@@ -179,6 +181,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
179181 self . imp . speculative_expand ( actual_macro_call, speculative_args, token_to_map)
180182 }
181183
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.
182186 pub fn speculative_expand_attr_macro (
183187 & self ,
184188 actual_macro_call : & ast:: Item ,
@@ -201,14 +205,22 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
201205 )
202206 }
203207
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)
207213 }
208214
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)
212224 }
213225
214226 /// 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> {
217229 pub fn descend_into_macros_with_same_text (
218230 & self ,
219231 token : SyntaxToken ,
232+ offset : TextSize ,
220233 ) -> SmallVec < [ SyntaxToken ; 1 ] > {
221- self . imp . descend_into_macros_with_same_text ( token)
234+ self . imp . descend_into_macros_with_same_text ( token, offset )
222235 }
223236
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)
226243 }
227244
228245 /// Maps a node down by mapping its first and last token down.
@@ -665,7 +682,7 @@ impl<'db> SemanticsImpl<'db> {
665682 } ;
666683
667684 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, .. } | {
669686 if let Some ( node) = value. parent_ancestors ( ) . find_map ( N :: cast) {
670687 res. push ( node)
671688 }
@@ -674,14 +691,15 @@ impl<'db> SemanticsImpl<'db> {
674691 } else {
675692 // Descend first and last token, then zip them to look for the node they belong to
676693 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| {
678695 scratch. push ( token) ;
679696 false
680697 } ) ;
681698
682699 let mut scratch = scratch. into_iter ( ) ;
683700 self . descend_into_macros_impl (
684701 last,
702+ 0 . into ( ) ,
685703 & mut |InFile { value : last, file_id : last_fid } | {
686704 if let Some ( InFile { value : first, file_id : first_fid } ) = scratch. next ( ) {
687705 if first_fid == last_fid {
@@ -705,19 +723,27 @@ impl<'db> SemanticsImpl<'db> {
705723 res
706724 }
707725
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 ] > {
709731 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, .. } | {
711733 res. push ( value) ;
712734 false
713735 } ) ;
714736 res
715737 }
716738
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 ] > {
718744 let text = token. text ( ) ;
719745 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, .. } | {
721747 if value. text ( ) == text {
722748 res. push ( value) ;
723749 }
@@ -729,7 +755,11 @@ impl<'db> SemanticsImpl<'db> {
729755 res
730756 }
731757
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 {
733763 let fetch_kind = |token : & SyntaxToken | match token. parent ( ) {
734764 Some ( node) => match node. kind ( ) {
735765 kind @ ( SyntaxKind :: NAME | SyntaxKind :: NAME_REF ) => {
@@ -741,7 +771,7 @@ impl<'db> SemanticsImpl<'db> {
741771 } ;
742772 let preferred_kind = fetch_kind ( & token) ;
743773 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, .. } | {
745775 if fetch_kind ( & value) == preferred_kind {
746776 res = Some ( value) ;
747777 true
@@ -755,9 +785,9 @@ impl<'db> SemanticsImpl<'db> {
755785 res. unwrap_or ( token)
756786 }
757787
758- fn descend_into_macros_single ( & self , token : SyntaxToken ) -> SyntaxToken {
788+ fn descend_into_macros_single ( & self , token : SyntaxToken , offset : TextSize ) -> SyntaxToken {
759789 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, .. } | {
761791 res = value;
762792 true
763793 } ) ;
@@ -767,9 +797,13 @@ impl<'db> SemanticsImpl<'db> {
767797 fn descend_into_macros_impl (
768798 & self ,
769799 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 ,
770803 f : & mut dyn FnMut ( InFile < SyntaxToken > ) -> bool ,
771804 ) {
772805 let _p = profile:: span ( "descend_into_macros" ) ;
806+ let relative_token_offset = token. text_range ( ) . start ( ) . checked_sub ( offset) ;
773807 let parent = match token. parent ( ) {
774808 Some ( it) => it,
775809 None => return ,
@@ -796,7 +830,12 @@ impl<'db> SemanticsImpl<'db> {
796830 self . cache ( value, file_id) ;
797831 }
798832
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+ ) ?;
800839 let len = stack. len ( ) ;
801840
802841 // requeue the tokens we got from mapping our current token down
@@ -943,7 +982,7 @@ impl<'db> SemanticsImpl<'db> {
943982 offset : TextSize ,
944983 ) -> impl Iterator < Item = impl Iterator < Item = SyntaxNode > + ' _ > + ' _ {
945984 node. token_at_offset ( offset)
946- . map ( move |token| self . descend_into_macros ( token) )
985+ . map ( move |token| self . descend_into_macros ( token, offset ) )
947986 . map ( |descendants| {
948987 descendants. into_iter ( ) . map ( move |it| self . token_ancestors_with_macros ( it) )
949988 } )
0 commit comments