From 062f6e3bbeffaa880d7f1c0b59dfad86b40a57a1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 24 Mar 2020 17:03:05 +0100 Subject: Generalise syntax rewriting infrastructure to allow removal of nodes --- crates/ra_hir_expand/src/eager.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/eager.rs b/crates/ra_hir_expand/src/eager.rs index 4cbce4df5..687d40294 100644 --- a/crates/ra_hir_expand/src/eager.rs +++ b/crates/ra_hir_expand/src/eager.rs @@ -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, macro_resolver: &dyn Fn(ast::Path) -> Option, ) -> Option { - 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 = 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) } -- cgit v1.2.3