aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/ast_editor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/ast_editor.rs')
-rw-r--r--crates/ra_assists/src/ast_editor.rs53
1 files changed, 0 insertions, 53 deletions
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}