From e6e4417bbbcc7e135d1b372e4e278cd3efa2d4b8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 23 Apr 2021 18:36:43 +0200 Subject: Remove SyntaxRewriter::from_fn --- crates/ide_assists/src/ast_transform.rs | 33 +++++++++++++++++++-------------- crates/ide_assists/src/utils.rs | 3 ++- 2 files changed, 21 insertions(+), 15 deletions(-) (limited to 'crates/ide_assists/src') diff --git a/crates/ide_assists/src/ast_transform.rs b/crates/ide_assists/src/ast_transform.rs index 4a3ed7783..e5ae718c9 100644 --- a/crates/ide_assists/src/ast_transform.rs +++ b/crates/ide_assists/src/ast_transform.rs @@ -3,20 +3,27 @@ use hir::{HirDisplay, PathResolution, SemanticsScope}; use ide_db::helpers::mod_path_to_ast; use rustc_hash::FxHashMap; use syntax::{ - algo::SyntaxRewriter, ast::{self, AstNode}, - SyntaxNode, + ted, SyntaxNode, }; -pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N { - SyntaxRewriter::from_fn(|element| match element { - syntax::SyntaxElement::Node(n) => { - let replacement = transformer.get_substitution(&n, transformer)?; - Some(replacement.into()) +pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: &N) { + let mut skip_to = None; + for event in node.syntax().preorder() { + match event { + syntax::WalkEvent::Enter(node) if skip_to.is_none() => { + skip_to = transformer.get_substitution(&node, transformer).zip(Some(node)); + } + syntax::WalkEvent::Enter(_) => (), + syntax::WalkEvent::Leave(node) => match &skip_to { + Some((replacement, skip_target)) if *skip_target == node => { + ted::replace(node, replacement.clone_for_update()); + skip_to.take(); + } + _ => (), + }, } - _ => None, - }) - .rewrite_ast(&node) + } } /// `AstTransform` helps with applying bulk transformations to syntax nodes. @@ -191,11 +198,9 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> { let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?; let mut path = mod_path_to_ast(&found_path); - let type_args = p - .segment() - .and_then(|s| s.generic_arg_list()) - .map(|arg_list| apply(recur, arg_list)); + let type_args = p.segment().and_then(|s| s.generic_arg_list()); if let Some(type_args) = type_args { + apply(recur, &type_args); let last_segment = path.segment().unwrap(); path = path.with_segment(last_segment.with_generic_args(type_args)) } diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index d67524937..5a90ad715 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -140,7 +140,8 @@ pub fn add_trait_assoc_items_to_impl( let items = items .into_iter() - .map(|it| ast_transform::apply(&*ast_transform, it)) + .map(|it| it.clone_for_update()) + .inspect(|it| ast_transform::apply(&*ast_transform, it)) .map(|it| match it { ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)), ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()), -- cgit v1.2.3