From 617cd7231c1aee5d7ca04892c6983c009b5ee60c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 19 Apr 2021 19:28:41 +0200 Subject: Remove SyntaxRewriter usage in eager::eager_macro_recur --- crates/hir_expand/src/eager.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index ef126e4ad..f413a63f7 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -29,7 +29,7 @@ use base_db::CrateId; use mbe::ExpandResult; use parser::FragmentKind; use std::sync::Arc; -use syntax::{algo::SyntaxRewriter, SyntaxNode}; +use syntax::{ted, SyntaxNode}; pub struct ErrorEmitted { _private: (), @@ -191,10 +191,10 @@ fn eager_macro_recur( macro_resolver: &dyn Fn(ast::Path) -> Option, mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), ) -> Result { - let original = curr.value.clone(); + let original = curr.value.clone().clone_for_update(); - let children = curr.value.descendants().filter_map(ast::MacroCall::cast); - let mut rewriter = SyntaxRewriter::default(); + let children = original.descendants().filter_map(ast::MacroCall::cast); + let mut replacements = Vec::new(); // Collect replacement for child in children { @@ -213,6 +213,7 @@ fn eager_macro_recur( .into(); db.parse_or_expand(id.as_file()) .expect("successful macro expansion should be parseable") + .clone_for_update() } MacroDefKind::Declarative(_) | MacroDefKind::BuiltIn(..) @@ -226,15 +227,14 @@ fn eager_macro_recur( } }; - // check if the whole original sytnax is replaced - // Note that SyntaxRewriter cannot replace the root node itself + // check if the whole original syntax is replaced if child.syntax() == &original { return Ok(insert); } - rewriter.replace(child.syntax(), &insert); + replacements.push((child, insert)); } - let res = rewriter.rewrite(&original); - Ok(res) + replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new)); + Ok(original) } -- cgit v1.2.3 From 952fc236948ce8a63e7f7ee49878aa2ffd011298 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 19 Apr 2021 19:43:26 +0200 Subject: Replace SyntaxRewriter with ted in exppand_macro::expand_macro_recur --- crates/ide/src/expand_macro.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index be0ee03bf..eebae5ebe 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -3,9 +3,7 @@ use std::iter; use hir::Semantics; use ide_db::RootDatabase; use syntax::{ - algo::{find_node_at_offset, SyntaxRewriter}, - ast, AstNode, NodeOrToken, SyntaxKind, - SyntaxKind::*, + algo::find_node_at_offset, ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T, }; @@ -46,26 +44,23 @@ fn expand_macro_recur( sema: &Semantics, macro_call: &ast::MacroCall, ) -> Option { - 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 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) { - // Replace the whole node if it is root - // `replace_descendants` will not replace the parent node - // but `SyntaxNode::descendants include itself + // check if the whole original syntax is replaced if expanded == *child.syntax() { - expanded = new_node; - } else { - rewriter.replace(child.syntax(), &new_node) + return Some(new_node); } + replacements.push((child, new_node)); } } - let res = rewriter.rewrite(&expanded); - Some(res) + replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new)); + Some(expanded) } // FIXME: It would also be cool to share logic here and in the mbe tests, -- cgit v1.2.3