diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index 1d27832a37e..39ff512335b 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs @@ -1,11 +1,11 @@ use hir::db::HirDatabase; use ra_syntax::{ - ast::{self, make, AstNode, NameOwner, TypeBoundsOwner}, + ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, SyntaxElement, SyntaxKind::*, }; -use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; +use crate::{Assist, AssistCtx, AssistId}; pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) -> Option { let type_param_list = ctx.node_at_offset::()?; @@ -43,9 +43,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) (type_param, without_bounds) }); - let mut ast_editor = AstEditor::new(type_param_list.clone()); - ast_editor.replace_descendants(new_params); - ast_editor.into_text_edit(edit.text_edit_builder()); + let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); + edit.replace_ast(type_param_list.clone(), new_type_param_list); let where_clause = { let predicates = type_param_list.type_params().filter_map(build_predicate); diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs deleted file mode 100644 index 69abf28a1e6..00000000000 --- a/crates/ra_assists/src/ast_editor.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::{iter, ops::RangeInclusive}; - -use ra_syntax::{ - algo, - ast::{self, TypeBoundsOwner}, - AstNode, SyntaxElement, -}; -use ra_text_edit::TextEditBuilder; -use rustc_hash::FxHashMap; - -pub struct AstEditor { - original_ast: N, - ast: N, -} - -impl AstEditor { - pub fn new(node: N) -> AstEditor - where - N: Clone, - { - AstEditor { original_ast: node.clone(), ast: node } - } - - pub fn into_text_edit(self, builder: &mut TextEditBuilder) { - algo::diff(&self.original_ast.syntax(), self.ast().syntax()).into_text_edit(builder) - } - - pub fn ast(&self) -> &N { - &self.ast - } - - pub fn replace_descendants( - &mut self, - replacement_map: impl Iterator, - ) -> &mut Self { - let map = replacement_map - .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) - .collect::>(); - let new_syntax = algo::replace_descendants(self.ast.syntax(), &map); - self.ast = N::cast(new_syntax).unwrap(); - self - } - - #[must_use] - fn replace_children( - &self, - to_delete: RangeInclusive, - mut to_insert: impl Iterator, - ) -> N { - let new_syntax = algo::replace_children(self.ast().syntax(), to_delete, &mut to_insert); - N::cast(new_syntax).unwrap() - } -} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 3ca3320f7aa..91b2a1dcefb 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -7,7 +7,6 @@ mod assist_ctx; mod marks; -pub mod ast_editor; use hir::db::HirDatabase; use itertools::Itertools; diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 9d0fd13838c..d0857d88b28 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -4,6 +4,7 @@ use std::{iter, ops::RangeInclusive}; use arrayvec::ArrayVec; +use rustc_hash::FxHashMap; use crate::{ algo, @@ -216,6 +217,17 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { node } +pub fn replace_descendants( + parent: &N, + replacement_map: impl Iterator, +) -> N { + let map = replacement_map + .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) + .collect::>(); + let new_syntax = algo::replace_descendants(parent.syntax(), &map); + N::cast(new_syntax).unwrap() +} + // Note this is copy-pasted from fmt. It seems like fmt should be a separate // crate, but basic tree building should be this crate. However, tree building // might want to call into fmt...