aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-30 08:05:12 +0100
committerAleksey Kladov <[email protected]>2019-09-30 08:05:12 +0100
commit05ca252fb51bbbf60433bdd3af55ce14bbd66bfd (patch)
tree3f1b50838de801f487c811df3c9a7e7b0c229ba7
parent054c53aeb9a9e29d1c06fa183da263037aa62572 (diff)
remove ast_editor.rs
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs9
-rw-r--r--crates/ra_assists/src/ast_editor.rs53
-rw-r--r--crates/ra_assists/src/lib.rs1
-rw-r--r--crates/ra_syntax/src/ast/edit.rs12
4 files changed, 16 insertions, 59 deletions
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs
index 1d27832a3..39ff51233 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -1,11 +1,11 @@
1use hir::db::HirDatabase; 1use hir::db::HirDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, make, AstNode, NameOwner, TypeBoundsOwner}, 3 ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner},
4 SyntaxElement, 4 SyntaxElement,
5 SyntaxKind::*, 5 SyntaxKind::*,
6}; 6};
7 7
8use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
9 9
10pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; 11 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
@@ -43,9 +43,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
43 (type_param, without_bounds) 43 (type_param, without_bounds)
44 }); 44 });
45 45
46 let mut ast_editor = AstEditor::new(type_param_list.clone()); 46 let new_type_param_list = edit::replace_descendants(&type_param_list, new_params);
47 ast_editor.replace_descendants(new_params); 47 edit.replace_ast(type_param_list.clone(), new_type_param_list);
48 ast_editor.into_text_edit(edit.text_edit_builder());
49 48
50 let where_clause = { 49 let where_clause = {
51 let predicates = type_param_list.type_params().filter_map(build_predicate); 50 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 69abf28a1..000000000
--- a/crates/ra_assists/src/ast_editor.rs
+++ /dev/null
@@ -1,53 +0,0 @@
1use std::{iter, ops::RangeInclusive};
2
3use ra_syntax::{
4 algo,
5 ast::{self, TypeBoundsOwner},
6 AstNode, SyntaxElement,
7};
8use ra_text_edit::TextEditBuilder;
9use rustc_hash::FxHashMap;
10
11pub struct AstEditor<N: AstNode> {
12 original_ast: N,
13 ast: N,
14}
15
16impl<N: AstNode> AstEditor<N> {
17 pub fn new(node: N) -> AstEditor<N>
18 where
19 N: Clone,
20 {
21 AstEditor { original_ast: node.clone(), ast: node }
22 }
23
24 pub fn into_text_edit(self, builder: &mut TextEditBuilder) {
25 algo::diff(&self.original_ast.syntax(), self.ast().syntax()).into_text_edit(builder)
26 }
27
28 pub fn ast(&self) -> &N {
29 &self.ast
30 }
31
32 pub fn replace_descendants<T: AstNode>(
33 &mut self,
34 replacement_map: impl Iterator<Item = (T, T)>,
35 ) -> &mut Self {
36 let map = replacement_map
37 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
38 .collect::<FxHashMap<_, _>>();
39 let new_syntax = algo::replace_descendants(self.ast.syntax(), &map);
40 self.ast = N::cast(new_syntax).unwrap();
41 self
42 }
43
44 #[must_use]
45 fn replace_children(
46 &self,
47 to_delete: RangeInclusive<SyntaxElement>,
48 mut to_insert: impl Iterator<Item = SyntaxElement>,
49 ) -> N {
50 let new_syntax = algo::replace_children(self.ast().syntax(), to_delete, &mut to_insert);
51 N::cast(new_syntax).unwrap()
52 }
53}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 3ca3320f7..91b2a1dce 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -7,7 +7,6 @@
7 7
8mod assist_ctx; 8mod assist_ctx;
9mod marks; 9mod marks;
10pub mod ast_editor;
11 10
12use hir::db::HirDatabase; 11use hir::db::HirDatabase;
13use itertools::Itertools; 12use itertools::Itertools;
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 9d0fd1383..d0857d88b 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -4,6 +4,7 @@
4use std::{iter, ops::RangeInclusive}; 4use std::{iter, ops::RangeInclusive};
5 5
6use arrayvec::ArrayVec; 6use arrayvec::ArrayVec;
7use rustc_hash::FxHashMap;
7 8
8use crate::{ 9use crate::{
9 algo, 10 algo,
@@ -216,6 +217,17 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
216 node 217 node
217} 218}
218 219
220pub fn replace_descendants<N: AstNode, D: AstNode>(
221 parent: &N,
222 replacement_map: impl Iterator<Item = (D, D)>,
223) -> N {
224 let map = replacement_map
225 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
226 .collect::<FxHashMap<_, _>>();
227 let new_syntax = algo::replace_descendants(parent.syntax(), &map);
228 N::cast(new_syntax).unwrap()
229}
230
219// Note this is copy-pasted from fmt. It seems like fmt should be a separate 231// Note this is copy-pasted from fmt. It seems like fmt should be a separate
220// crate, but basic tree building should be this crate. However, tree building 232// crate, but basic tree building should be this crate. However, tree building
221// might want to call into fmt... 233// might want to call into fmt...