Skip to content

Commit b6a7276

Browse files
bors[bot]Veykril
andauthored
Merge #8586
8586: Replace SyntaxRewriter usage with ted in eager::eager_macro_recur r=Veykril a=Veykril Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 9cfbb56 + 952fc23 commit b6a7276

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

crates/hir_expand/src/eager.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use base_db::CrateId;
2929
use mbe::ExpandResult;
3030
use parser::FragmentKind;
3131
use std::sync::Arc;
32-
use syntax::{algo::SyntaxRewriter, SyntaxNode};
32+
use syntax::{ted, SyntaxNode};
3333

3434
#[derive(Debug)]
3535
pub struct ErrorEmitted {
@@ -192,10 +192,10 @@ fn eager_macro_recur(
192192
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
193193
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
194194
) -> Result<SyntaxNode, ErrorEmitted> {
195-
let original = curr.value.clone();
195+
let original = curr.value.clone().clone_for_update();
196196

197-
let children = curr.value.descendants().filter_map(ast::MacroCall::cast);
198-
let mut rewriter = SyntaxRewriter::default();
197+
let children = original.descendants().filter_map(ast::MacroCall::cast);
198+
let mut replacements = Vec::new();
199199

200200
// Collect replacement
201201
for child in children {
@@ -214,6 +214,7 @@ fn eager_macro_recur(
214214
.into();
215215
db.parse_or_expand(id.as_file())
216216
.expect("successful macro expansion should be parseable")
217+
.clone_for_update()
217218
}
218219
MacroDefKind::Declarative(_)
219220
| MacroDefKind::BuiltIn(..)
@@ -227,15 +228,14 @@ fn eager_macro_recur(
227228
}
228229
};
229230

230-
// check if the whole original sytnax is replaced
231-
// Note that SyntaxRewriter cannot replace the root node itself
231+
// check if the whole original syntax is replaced
232232
if child.syntax() == &original {
233233
return Ok(insert);
234234
}
235235

236-
rewriter.replace(child.syntax(), &insert);
236+
replacements.push((child, insert));
237237
}
238238

239-
let res = rewriter.rewrite(&original);
240-
Ok(res)
239+
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
240+
Ok(original)
241241
}

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)