diff options
author | Aleksey Kladov <[email protected]> | 2020-08-12 15:58:56 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-12 15:58:56 +0100 |
commit | 7510048ec0a5d5e7136e3ea258954eb244d15baf (patch) | |
tree | 97a472bf38e4e3e079db27abf1f2c7b523a0d0b1 | |
parent | 8d34262956059aca7e6fded351a9299b3581a5cf (diff) |
Cleanup TextEdit API
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/assist_context.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/join_lines.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/references/rename.rs | 2 | ||||
-rw-r--r-- | crates/ra_ssr/src/replacing.rs | 2 | ||||
-rw-r--r-- | crates/ra_text_edit/Cargo.toml | 7 | ||||
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 27 |
9 files changed, 34 insertions, 24 deletions
diff --git a/Cargo.lock b/Cargo.lock index c48c6a597..daa872546 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1205,7 +1205,7 @@ dependencies = [ | |||
1205 | 1205 | ||
1206 | [[package]] | 1206 | [[package]] |
1207 | name = "ra_text_edit" | 1207 | name = "ra_text_edit" |
1208 | version = "0.1.0" | 1208 | version = "0.0.0" |
1209 | dependencies = [ | 1209 | dependencies = [ |
1210 | "text-size", | 1210 | "text-size", |
1211 | ] | 1211 | ] |
diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index afd3fd4b9..afba860d1 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs | |||
@@ -15,7 +15,7 @@ use ra_syntax::{ | |||
15 | AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, | 15 | AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, |
16 | TokenAtOffset, | 16 | TokenAtOffset, |
17 | }; | 17 | }; |
18 | use ra_text_edit::TextEditBuilder; | 18 | use ra_text_edit::{TextEdit, TextEditBuilder}; |
19 | 19 | ||
20 | use crate::{ | 20 | use crate::{ |
21 | assist_config::{AssistConfig, SnippetCap}, | 21 | assist_config::{AssistConfig, SnippetCap}, |
@@ -214,7 +214,7 @@ pub(crate) struct AssistBuilder { | |||
214 | impl AssistBuilder { | 214 | impl AssistBuilder { |
215 | pub(crate) fn new(file_id: FileId) -> AssistBuilder { | 215 | pub(crate) fn new(file_id: FileId) -> AssistBuilder { |
216 | AssistBuilder { | 216 | AssistBuilder { |
217 | edit: TextEditBuilder::default(), | 217 | edit: TextEdit::builder(), |
218 | file_id, | 218 | file_id, |
219 | is_snippet: false, | 219 | is_snippet: false, |
220 | change: SourceChange::default(), | 220 | change: SourceChange::default(), |
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 07bf133bd..e006c7775 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -14,7 +14,7 @@ use ra_syntax::{ | |||
14 | ast::{self, AstNode}, | 14 | ast::{self, AstNode}, |
15 | SyntaxNode, TextRange, T, | 15 | SyntaxNode, TextRange, T, |
16 | }; | 16 | }; |
17 | use ra_text_edit::{TextEdit, TextEditBuilder}; | 17 | use ra_text_edit::TextEdit; |
18 | 18 | ||
19 | use crate::{Diagnostic, FileId, Fix, SourceFileEdit}; | 19 | use crate::{Diagnostic, FileId, Fix, SourceFileEdit}; |
20 | 20 | ||
@@ -103,7 +103,7 @@ fn check_unnecessary_braces_in_use_statement( | |||
103 | text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree) | 103 | text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree) |
104 | .unwrap_or_else(|| { | 104 | .unwrap_or_else(|| { |
105 | let to_replace = single_use_tree.syntax().text().to_string(); | 105 | let to_replace = single_use_tree.syntax().text().to_string(); |
106 | let mut edit_builder = TextEditBuilder::default(); | 106 | let mut edit_builder = TextEdit::builder(); |
107 | edit_builder.delete(use_range); | 107 | edit_builder.delete(use_range); |
108 | edit_builder.insert(use_range.start(), to_replace); | 108 | edit_builder.insert(use_range.start(), to_replace); |
109 | edit_builder.finish() | 109 | edit_builder.finish() |
@@ -149,7 +149,7 @@ fn check_struct_shorthand_initialization( | |||
149 | let field_expr = expr.syntax().text().to_string(); | 149 | let field_expr = expr.syntax().text().to_string(); |
150 | let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); | 150 | let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); |
151 | if field_name == field_expr && !field_name_is_tup_index { | 151 | if field_name == field_expr && !field_name_is_tup_index { |
152 | let mut edit_builder = TextEditBuilder::default(); | 152 | let mut edit_builder = TextEdit::builder(); |
153 | edit_builder.delete(record_field.syntax().text_range()); | 153 | edit_builder.delete(record_field.syntax().text_range()); |
154 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); | 154 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); |
155 | let edit = edit_builder.finish(); | 155 | let edit = edit_builder.finish(); |
diff --git a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs index f7c73773f..88e593e00 100644 --- a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs +++ b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs | |||
@@ -13,7 +13,7 @@ use ra_ide_db::{ | |||
13 | RootDatabase, | 13 | RootDatabase, |
14 | }; | 14 | }; |
15 | use ra_syntax::{algo, ast, AstNode}; | 15 | use ra_syntax::{algo, ast, AstNode}; |
16 | use ra_text_edit::{TextEdit, TextEditBuilder}; | 16 | use ra_text_edit::TextEdit; |
17 | 17 | ||
18 | /// A [Diagnostic] that potentially has a fix available. | 18 | /// A [Diagnostic] that potentially has a fix available. |
19 | /// | 19 | /// |
@@ -70,7 +70,7 @@ impl DiagnosticWithFix for MissingFields { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | let edit = { | 72 | let edit = { |
73 | let mut builder = TextEditBuilder::default(); | 73 | let mut builder = TextEdit::builder(); |
74 | algo::diff(&old_field_list.syntax(), &new_field_list.syntax()) | 74 | algo::diff(&old_field_list.syntax(), &new_field_list.syntax()) |
75 | .into_text_edit(&mut builder); | 75 | .into_text_edit(&mut builder); |
76 | builder.finish() | 76 | builder.finish() |
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 6907c09e8..1c881386f 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs | |||
@@ -23,7 +23,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { | |||
23 | let syntax = file.syntax(); | 23 | let syntax = file.syntax(); |
24 | let text = syntax.text().slice(range.start()..); | 24 | let text = syntax.text().slice(range.start()..); |
25 | let pos = match text.find_char('\n') { | 25 | let pos = match text.find_char('\n') { |
26 | None => return TextEditBuilder::default().finish(), | 26 | None => return TextEdit::builder().finish(), |
27 | Some(pos) => pos, | 27 | Some(pos) => pos, |
28 | }; | 28 | }; |
29 | TextRange::at(range.start() + pos, TextSize::of('\n')) | 29 | TextRange::at(range.start() + pos, TextSize::of('\n')) |
@@ -35,7 +35,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { | |||
35 | NodeOrToken::Node(node) => node, | 35 | NodeOrToken::Node(node) => node, |
36 | NodeOrToken::Token(token) => token.parent(), | 36 | NodeOrToken::Token(token) => token.parent(), |
37 | }; | 37 | }; |
38 | let mut edit = TextEditBuilder::default(); | 38 | let mut edit = TextEdit::builder(); |
39 | for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { | 39 | for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { |
40 | let range = match range.intersect(token.text_range()) { | 40 | let range = match range.intersect(token.text_range()) { |
41 | Some(range) => range, | 41 | Some(range) => range, |
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index c8d80fcf7..8c1ac3c56 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs | |||
@@ -281,7 +281,7 @@ mod tests { | |||
281 | let ra_fixture_after = &trim_indent(ra_fixture_after); | 281 | let ra_fixture_after = &trim_indent(ra_fixture_after); |
282 | let (analysis, position) = analysis_and_position(ra_fixture_before); | 282 | let (analysis, position) = analysis_and_position(ra_fixture_before); |
283 | let source_change = analysis.rename(position, new_name).unwrap(); | 283 | let source_change = analysis.rename(position, new_name).unwrap(); |
284 | let mut text_edit_builder = TextEditBuilder::default(); | 284 | let mut text_edit_builder = TextEdit::builder(); |
285 | let mut file_id: Option<FileId> = None; | 285 | let mut file_id: Option<FileId> = None; |
286 | if let Some(change) = source_change { | 286 | if let Some(change) = source_change { |
287 | for edit in change.info.source_file_edits { | 287 | for edit in change.info.source_file_edits { |
diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs index 0943244ff..36ced3842 100644 --- a/crates/ra_ssr/src/replacing.rs +++ b/crates/ra_ssr/src/replacing.rs | |||
@@ -24,7 +24,7 @@ fn matches_to_edit_at_offset( | |||
24 | relative_start: TextSize, | 24 | relative_start: TextSize, |
25 | rules: &[ResolvedRule], | 25 | rules: &[ResolvedRule], |
26 | ) -> TextEdit { | 26 | ) -> TextEdit { |
27 | let mut edit_builder = ra_text_edit::TextEditBuilder::default(); | 27 | let mut edit_builder = TextEdit::builder(); |
28 | for m in &matches.matches { | 28 | for m in &matches.matches { |
29 | edit_builder.replace( | 29 | edit_builder.replace( |
30 | m.range.range.checked_sub(relative_start).unwrap(), | 30 | m.range.range.checked_sub(relative_start).unwrap(), |
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index dbb223350..427862a5c 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml | |||
@@ -1,10 +1,9 @@ | |||
1 | [package] | 1 | [package] |
2 | edition = "2018" | ||
3 | name = "ra_text_edit" | 2 | name = "ra_text_edit" |
4 | version = "0.1.0" | 3 | version = "0.0.0" |
5 | authors = ["rust-analyzer developers"] | ||
6 | publish = false | ||
7 | license = "MIT OR Apache-2.0" | 4 | license = "MIT OR Apache-2.0" |
5 | authors = ["rust-analyzer developers"] | ||
6 | edition = "2018" | ||
8 | 7 | ||
9 | [lib] | 8 | [lib] |
10 | doctest = false | 9 | doctest = false |
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index d68791cf1..ab8cd7fd1 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs | |||
@@ -3,8 +3,6 @@ | |||
3 | //! `rust-analyzer` never mutates text itself and only sends diffs to clients, | 3 | //! `rust-analyzer` never mutates text itself and only sends diffs to clients, |
4 | //! so `TextEdit` is the ultimate representation of the work done by | 4 | //! so `TextEdit` is the ultimate representation of the work done by |
5 | //! rust-analyzer. | 5 | //! rust-analyzer. |
6 | use std::{slice, vec}; | ||
7 | |||
8 | pub use text_size::{TextRange, TextSize}; | 6 | pub use text_size::{TextRange, TextSize}; |
9 | 7 | ||
10 | /// `InsertDelete` -- a single "atomic" change to text | 8 | /// `InsertDelete` -- a single "atomic" change to text |
@@ -46,20 +44,24 @@ impl Indel { | |||
46 | } | 44 | } |
47 | 45 | ||
48 | impl TextEdit { | 46 | impl TextEdit { |
47 | pub fn builder() -> TextEditBuilder { | ||
48 | TextEditBuilder::default() | ||
49 | } | ||
50 | |||
49 | pub fn insert(offset: TextSize, text: String) -> TextEdit { | 51 | pub fn insert(offset: TextSize, text: String) -> TextEdit { |
50 | let mut builder = TextEditBuilder::default(); | 52 | let mut builder = TextEdit::builder(); |
51 | builder.insert(offset, text); | 53 | builder.insert(offset, text); |
52 | builder.finish() | 54 | builder.finish() |
53 | } | 55 | } |
54 | 56 | ||
55 | pub fn delete(range: TextRange) -> TextEdit { | 57 | pub fn delete(range: TextRange) -> TextEdit { |
56 | let mut builder = TextEditBuilder::default(); | 58 | let mut builder = TextEdit::builder(); |
57 | builder.delete(range); | 59 | builder.delete(range); |
58 | builder.finish() | 60 | builder.finish() |
59 | } | 61 | } |
60 | 62 | ||
61 | pub fn replace(range: TextRange, replace_with: String) -> TextEdit { | 63 | pub fn replace(range: TextRange, replace_with: String) -> TextEdit { |
62 | let mut builder = TextEditBuilder::default(); | 64 | let mut builder = TextEdit::builder(); |
63 | builder.replace(range, replace_with); | 65 | builder.replace(range, replace_with); |
64 | builder.finish() | 66 | builder.finish() |
65 | } | 67 | } |
@@ -72,8 +74,8 @@ impl TextEdit { | |||
72 | self.indels.is_empty() | 74 | self.indels.is_empty() |
73 | } | 75 | } |
74 | 76 | ||
75 | pub fn iter(&self) -> slice::Iter<'_, Indel> { | 77 | pub fn iter(&self) -> std::slice::Iter<'_, Indel> { |
76 | self.indels.iter() | 78 | self.into_iter() |
77 | } | 79 | } |
78 | 80 | ||
79 | pub fn apply(&self, text: &mut String) { | 81 | pub fn apply(&self, text: &mut String) { |
@@ -139,13 +141,22 @@ impl TextEdit { | |||
139 | 141 | ||
140 | impl IntoIterator for TextEdit { | 142 | impl IntoIterator for TextEdit { |
141 | type Item = Indel; | 143 | type Item = Indel; |
142 | type IntoIter = vec::IntoIter<Self::Item>; | 144 | type IntoIter = std::vec::IntoIter<Indel>; |
143 | 145 | ||
144 | fn into_iter(self) -> Self::IntoIter { | 146 | fn into_iter(self) -> Self::IntoIter { |
145 | self.indels.into_iter() | 147 | self.indels.into_iter() |
146 | } | 148 | } |
147 | } | 149 | } |
148 | 150 | ||
151 | impl<'a> IntoIterator for &'a TextEdit { | ||
152 | type Item = &'a Indel; | ||
153 | type IntoIter = std::slice::Iter<'a, Indel>; | ||
154 | |||
155 | fn into_iter(self) -> Self::IntoIter { | ||
156 | self.indels.iter() | ||
157 | } | ||
158 | } | ||
159 | |||
149 | impl TextEditBuilder { | 160 | impl TextEditBuilder { |
150 | pub fn replace(&mut self, range: TextRange, replace_with: String) { | 161 | pub fn replace(&mut self, range: TextRange, replace_with: String) { |
151 | self.indels.push(Indel::replace(range, replace_with)) | 162 | self.indels.push(Indel::replace(range, replace_with)) |