aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/ast_editor.rs27
1 files changed, 3 insertions, 24 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index 2936c094b..2a685f26e 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -7,7 +7,7 @@ use ra_fmt::leading_indent;
7use ra_syntax::{ 7use ra_syntax::{
8 algo, 8 algo,
9 ast::{self, TypeBoundsOwner}, 9 ast::{self, TypeBoundsOwner},
10 AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, 10 AstNode, Direction, InsertPosition, SyntaxElement,
11 SyntaxKind::*, 11 SyntaxKind::*,
12 T, 12 T,
13}; 13};
@@ -27,29 +27,8 @@ impl<N: AstNode> AstEditor<N> {
27 } 27 }
28 28
29 pub fn into_text_edit(self, builder: &mut TextEditBuilder) { 29 pub fn into_text_edit(self, builder: &mut TextEditBuilder) {
30 // FIXME: this is both horrible inefficient and gives larger than 30 for (from, to) in algo::diff(&self.original_ast.syntax(), self.ast().syntax()) {
31 // necessary diff. I bet there's a cool algorithm to diff trees properly. 31 builder.replace(from.text_range(), to.to_string())
32 go(builder, self.original_ast.syntax().clone().into(), self.ast().syntax().clone().into());
33
34 fn go(buf: &mut TextEditBuilder, lhs: SyntaxElement, rhs: SyntaxElement) {
35 if lhs.kind() == rhs.kind() && lhs.text_range().len() == rhs.text_range().len() {
36 if match (&lhs, &rhs) {
37 (NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => lhs.text() == rhs.text(),
38 (NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => lhs.text() == rhs.text(),
39 _ => false,
40 } {
41 return;
42 }
43 }
44 if let (Some(lhs), Some(rhs)) = (lhs.as_node(), rhs.as_node()) {
45 if lhs.children_with_tokens().count() == rhs.children_with_tokens().count() {
46 for (lhs, rhs) in lhs.children_with_tokens().zip(rhs.children_with_tokens()) {
47 go(buf, lhs, rhs)
48 }
49 return;
50 }
51 }
52 buf.replace(lhs.text_range(), rhs.to_string())
53 } 32 }
54 } 33 }
55 34