aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-12 15:58:56 +0100
committerAleksey Kladov <[email protected]>2020-08-12 15:58:56 +0100
commit7510048ec0a5d5e7136e3ea258954eb244d15baf (patch)
tree97a472bf38e4e3e079db27abf1f2c7b523a0d0b1 /crates/ra_text_edit
parent8d34262956059aca7e6fded351a9299b3581a5cf (diff)
Cleanup TextEdit API
Diffstat (limited to 'crates/ra_text_edit')
-rw-r--r--crates/ra_text_edit/Cargo.toml7
-rw-r--r--crates/ra_text_edit/src/lib.rs27
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]
2edition = "2018"
3name = "ra_text_edit" 2name = "ra_text_edit"
4version = "0.1.0" 3version = "0.0.0"
5authors = ["rust-analyzer developers"]
6publish = false
7license = "MIT OR Apache-2.0" 4license = "MIT OR Apache-2.0"
5authors = ["rust-analyzer developers"]
6edition = "2018"
8 7
9[lib] 8[lib]
10doctest = false 9doctest = 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.
6use std::{slice, vec};
7
8pub use text_size::{TextRange, TextSize}; 6pub 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
48impl TextEdit { 46impl 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
140impl IntoIterator for TextEdit { 142impl 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
151impl<'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
149impl TextEditBuilder { 160impl 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))