diff options
Diffstat (limited to 'crates/ra_text_edit/src')
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 27 |
1 files changed, 19 insertions, 8 deletions
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)) |