@@ -3,9 +3,7 @@ use std::iter;
3
3
use hir:: Semantics ;
4
4
use ide_db:: RootDatabase ;
5
5
use syntax:: {
6
- algo:: { find_node_at_offset, SyntaxRewriter } ,
7
- ast, AstNode , NodeOrToken , SyntaxKind ,
8
- SyntaxKind :: * ,
6
+ algo:: find_node_at_offset, ast, ted, AstNode , NodeOrToken , SyntaxKind , SyntaxKind :: * ,
9
7
SyntaxNode , WalkEvent , T ,
10
8
} ;
11
9
@@ -46,26 +44,23 @@ fn expand_macro_recur(
46
44
sema : & Semantics < RootDatabase > ,
47
45
macro_call : & ast:: MacroCall ,
48
46
) -> Option < SyntaxNode > {
49
- let mut expanded = sema. expand ( macro_call) ?;
47
+ let expanded = sema. expand ( macro_call) ?. clone_for_update ( ) ;
50
48
51
49
let children = expanded. descendants ( ) . filter_map ( ast:: MacroCall :: cast) ;
52
- let mut rewriter = SyntaxRewriter :: default ( ) ;
50
+ let mut replacements = Vec :: new ( ) ;
53
51
54
- for child in children. into_iter ( ) {
52
+ for child in children {
55
53
if let Some ( new_node) = expand_macro_recur ( sema, & child) {
56
- // Replace the whole node if it is root
57
- // `replace_descendants` will not replace the parent node
58
- // but `SyntaxNode::descendants include itself
54
+ // check if the whole original syntax is replaced
59
55
if expanded == * child. syntax ( ) {
60
- expanded = new_node;
61
- } else {
62
- rewriter. replace ( child. syntax ( ) , & new_node)
56
+ return Some ( new_node) ;
63
57
}
58
+ replacements. push ( ( child, new_node) ) ;
64
59
}
65
60
}
66
61
67
- let res = rewriter . rewrite ( & expanded ) ;
68
- Some ( res )
62
+ replacements . into_iter ( ) . rev ( ) . for_each ( | ( old , new ) | ted :: replace ( old . syntax ( ) , new ) ) ;
63
+ Some ( expanded )
69
64
}
70
65
71
66
// FIXME: It would also be cool to share logic here and in the mbe tests,
0 commit comments