diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-26 13:58:54 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-26 13:58:54 +0100 |
commit | 53a30d9e69ee5149e4fdb1c6fe4081281e879d0e (patch) | |
tree | acb80dc5a8a91853f49ddf632a461a66df180ad4 /crates/ra_assists | |
parent | 3882231f3231db03144107f72c6052f773fe2375 (diff) | |
parent | 1a4b42400544a652a053a34263967689d47f554b (diff) |
Merge #1919
1919: move diff to ra_syntax r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 27 |
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; | |||
7 | use ra_syntax::{ | 7 | use 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 | ||