mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Replace SyntaxRewriter with ted in exppand_macro::expand_macro_recur
This commit is contained in:
parent
617cd7231c
commit
952fc23694
1 changed files with 9 additions and 14 deletions
|
@ -3,9 +3,7 @@ use std::iter;
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::{find_node_at_offset, SyntaxRewriter},
|
algo::find_node_at_offset, ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*,
|
||||||
ast, AstNode, NodeOrToken, SyntaxKind,
|
|
||||||
SyntaxKind::*,
|
|
||||||
SyntaxNode, WalkEvent, T,
|
SyntaxNode, WalkEvent, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,26 +44,23 @@ fn expand_macro_recur(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
macro_call: &ast::MacroCall,
|
macro_call: &ast::MacroCall,
|
||||||
) -> Option<SyntaxNode> {
|
) -> 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 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) {
|
if let Some(new_node) = expand_macro_recur(sema, &child) {
|
||||||
// Replace the whole node if it is root
|
// check if the whole original syntax is replaced
|
||||||
// `replace_descendants` will not replace the parent node
|
|
||||||
// but `SyntaxNode::descendants include itself
|
|
||||||
if expanded == *child.syntax() {
|
if expanded == *child.syntax() {
|
||||||
expanded = new_node;
|
return Some(new_node);
|
||||||
} else {
|
|
||||||
rewriter.replace(child.syntax(), &new_node)
|
|
||||||
}
|
}
|
||||||
|
replacements.push((child, new_node));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = rewriter.rewrite(&expanded);
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
||||||
Some(res)
|
Some(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
// FIXME: It would also be cool to share logic here and in the mbe tests,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue