@@ -18,7 +18,7 @@ use smallvec::{smallvec, SmallVec};
18
18
use syntax:: {
19
19
algo:: skip_trivia_token,
20
20
ast:: { self , HasAttrs , HasGenericParams , HasLoopBody } ,
21
- match_ast, AstNode , Direction , SyntaxNode , SyntaxNodePtr , SyntaxToken , TextRange , TextSize ,
21
+ match_ast, AstNode , Direction , SyntaxNode , SyntaxNodePtr , SyntaxToken , TextSize ,
22
22
} ;
23
23
24
24
use crate :: {
@@ -556,25 +556,27 @@ impl<'db> SemanticsImpl<'db> {
556
556
None => return ,
557
557
} ;
558
558
let sa = self . analyze ( & parent) ;
559
- let mut stack: SmallVec < [ _ ; 1 ] > = smallvec ! [ InFile :: new( sa. file_id, token) ] ;
559
+ let mut stack: SmallVec < [ _ ; 4 ] > = smallvec ! [ InFile :: new( sa. file_id, token) ] ;
560
560
let mut cache = self . expansion_info_cache . borrow_mut ( ) ;
561
561
let mut mcache = self . macro_call_cache . borrow_mut ( ) ;
562
562
563
563
let mut process_expansion_for_token =
564
- |stack : & mut SmallVec < _ > , file_id, item, token : InFile < & _ > | {
565
- let mapped_tokens = cache
566
- . entry ( file_id)
567
- . or_insert_with ( || file_id. expansion_info ( self . db . upcast ( ) ) )
568
- . as_ref ( ) ?
569
- . map_token_down ( self . db . upcast ( ) , item, token) ?;
564
+ |stack : & mut SmallVec < _ > , macro_file, item, token : InFile < & _ > | {
565
+ let expansion_info = cache
566
+ . entry ( macro_file)
567
+ . or_insert_with ( || macro_file. expansion_info ( self . db . upcast ( ) ) )
568
+ . as_ref ( ) ?;
569
+
570
+ {
571
+ let InFile { file_id, value } = expansion_info. expanded ( ) ;
572
+ self . cache ( value, file_id) ;
573
+ }
574
+
575
+ let mapped_tokens = expansion_info. map_token_down ( self . db . upcast ( ) , item, token) ?;
570
576
571
577
let len = stack. len ( ) ;
572
578
// requeue the tokens we got from mapping our current token down
573
- stack. extend ( mapped_tokens. inspect ( |token| {
574
- if let Some ( parent) = token. value . parent ( ) {
575
- self . cache ( find_root ( & parent) , token. file_id ) ;
576
- }
577
- } ) ) ;
579
+ stack. extend ( mapped_tokens) ;
578
580
// if the length changed we have found a mapping for the token
579
581
( stack. len ( ) != len) . then ( || ( ) )
580
582
} ;
@@ -606,17 +608,15 @@ impl<'db> SemanticsImpl<'db> {
606
608
}
607
609
608
610
// or are we inside a function-like macro call
609
- if let Some ( macro_call) = token. value . ancestors ( ) . find_map ( ast:: MacroCall :: cast) {
610
- let tt = macro_call. token_tree ( ) ?;
611
- let l_delim = match tt. left_delimiter_token ( ) {
612
- Some ( it) => it. text_range ( ) . end ( ) ,
613
- None => tt. syntax ( ) . text_range ( ) . start ( ) ,
614
- } ;
615
- let r_delim = match tt. right_delimiter_token ( ) {
616
- Some ( it) => it. text_range ( ) . start ( ) ,
617
- None => tt. syntax ( ) . text_range ( ) . end ( ) ,
618
- } ;
619
- if !TextRange :: new ( l_delim, r_delim) . contains_range ( token. value . text_range ( ) ) {
611
+ if let Some ( tt) =
612
+ // FIXME replace map.while_some with take_while once stable
613
+ token. value . ancestors ( ) . map ( ast:: TokenTree :: cast) . while_some ( ) . last ( )
614
+ {
615
+ let macro_call = tt. syntax ( ) . parent ( ) . and_then ( ast:: MacroCall :: cast) ?;
616
+ if tt. left_delimiter_token ( ) . map_or ( false , |it| it == token. value ) {
617
+ return None ;
618
+ }
619
+ if tt. right_delimiter_token ( ) . map_or ( false , |it| it == token. value ) {
620
620
return None ;
621
621
}
622
622
0 commit comments