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 /crates/ra_text_edit | |
parent | 8d34262956059aca7e6fded351a9299b3581a5cf (diff) |
Cleanup TextEdit API
Diffstat (limited to 'crates/ra_text_edit')
-rw-r--r-- | crates/ra_text_edit/Cargo.toml | 7 | ||||
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 27 |
2 files changed, 22 insertions, 12 deletions
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)) |