Skip to content

Commit 952fc23

Browse files
committed
Replace SyntaxRewriter with ted in exppand_macro::expand_macro_recur
1 parent 617cd72 commit 952fc23

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

crates/ide/src/expand_macro.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use std::iter;
33
use hir::Semantics;
44
use ide_db::RootDatabase;
55
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::*,
97
SyntaxNode, WalkEvent, T,
108
};
119

@@ -46,26 +44,23 @@ fn expand_macro_recur(
4644
sema: &Semantics<RootDatabase>,
4745
macro_call: &ast::MacroCall,
4846
) -> Option<SyntaxNode> {
49-
let mut expanded = sema.expand(macro_call)?;
47+
let expanded = sema.expand(macro_call)?.clone_for_update();
5048

5149
let children = expanded.descendants().filter_map(ast::MacroCall::cast);
52-
let mut rewriter = SyntaxRewriter::default();
50+
let mut replacements = Vec::new();
5351

54-
for child in children.into_iter() {
52+
for child in children {
5553
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
5955
if expanded == *child.syntax() {
60-
expanded = new_node;
61-
} else {
62-
rewriter.replace(child.syntax(), &new_node)
56+
return Some(new_node);
6357
}
58+
replacements.push((child, new_node));
6459
}
6560
}
6661

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)
6964
}
7065

7166
// FIXME: It would also be cool to share logic here and in the mbe tests,

0 commit comments

Comments
 (0)