]> git.lizzy.rs Git - rust.git/commitdiff
Replace SyntaxRewriter with ted in exppand_macro::expand_macro_recur
authorLukas Wirth <lukastw97@gmail.com>
Mon, 19 Apr 2021 17:43:26 +0000 (19:43 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Mon, 19 Apr 2021 17:43:26 +0000 (19:43 +0200)
crates/ide/src/expand_macro.rs

index be0ee03bf8f8cf09dbc8bcae5c80b0061cbe547b..eebae5ebea50bd15e73c1e7f67c6155fec89d1d7 100644 (file)
@@ -3,9 +3,7 @@
 use hir::Semantics;
 use ide_db::RootDatabase;
 use syntax::{
-    algo::{find_node_at_offset, SyntaxRewriter},
-    ast, AstNode, NodeOrToken, SyntaxKind,
-    SyntaxKind::*,
+    algo::find_node_at_offset, ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*,
     SyntaxNode, WalkEvent, T,
 };
 
@@ -46,26 +44,23 @@ fn expand_macro_recur(
     sema: &Semantics<RootDatabase>,
     macro_call: &ast::MacroCall,
 ) -> Option<SyntaxNode> {
-    let mut expanded = sema.expand(macro_call)?;
+    let expanded = sema.expand(macro_call)?.clone_for_update();
 
     let children = expanded.descendants().filter_map(ast::MacroCall::cast);
-    let mut rewriter = SyntaxRewriter::default();
+    let mut replacements = Vec::new();
 
-    for child in children.into_iter() {
+    for child in children {
         if let Some(new_node) = expand_macro_recur(sema, &child) {
-            // Replace the whole node if it is root
-            // `replace_descendants` will not replace the parent node
-            // but `SyntaxNode::descendants include itself
+            // check if the whole original syntax is replaced
             if expanded == *child.syntax() {
-                expanded = new_node;
-            } else {
-                rewriter.replace(child.syntax(), &new_node)
+                return Some(new_node);
             }
+            replacements.push((child, new_node));
         }
     }
 
-    let res = rewriter.rewrite(&expanded);
-    Some(res)
+    replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
+    Some(expanded)
 }
 
 // FIXME: It would also be cool to share logic here and in the mbe tests,