Generalise syntax rewriting infrastructure to allow removal of nodes

This commit is contained in:
Aleksey Kladov
2020-03-24 17:03:05 +01:00
parent 3bd119a4c1
commit 062f6e3bbe
7 changed files with 253 additions and 122 deletions

View File

@@ -26,8 +26,8 @@ use crate::{
};
use ra_parser::FragmentKind;
use ra_syntax::{algo::replace_descendants, SyntaxElement, SyntaxNode};
use std::{collections::HashMap, sync::Arc};
use ra_syntax::{algo::SyntaxRewriter, SyntaxNode};
use std::sync::Arc;
pub fn expand_eager_macro(
db: &dyn AstDatabase,
@@ -95,10 +95,10 @@ fn eager_macro_recur(
curr: InFile<SyntaxNode>,
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
) -> Option<SyntaxNode> {
let mut original = curr.value.clone();
let original = curr.value.clone();
let children = curr.value.descendants().filter_map(ast::MacroCall::cast);
let mut replaces: HashMap<SyntaxElement, SyntaxElement> = HashMap::default();
let mut rewriter = SyntaxRewriter::default();
// Collect replacement
for child in children {
@@ -119,12 +119,9 @@ fn eager_macro_recur(
}
};
replaces.insert(child.syntax().clone().into(), insert.into());
rewriter.replace(child.syntax(), &insert);
}
if !replaces.is_empty() {
original = replace_descendants(&original, |n| replaces.get(n).cloned());
}
Some(original)
let res = rewriter.rewrite(&original);
Some(res)
}