diff options
Diffstat (limited to 'crates/ra_text_edit/src/lib.rs')
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs deleted file mode 100644 index d68791cf1..000000000 --- a/crates/ra_text_edit/src/lib.rs +++ /dev/null | |||
@@ -1,175 +0,0 @@ | |||
1 | //! Representation of a `TextEdit`. | ||
2 | //! | ||
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 | ||
5 | //! rust-analyzer. | ||
6 | use std::{slice, vec}; | ||
7 | |||
8 | pub use text_size::{TextRange, TextSize}; | ||
9 | |||
10 | /// `InsertDelete` -- a single "atomic" change to text | ||
11 | /// | ||
12 | /// Must not overlap with other `InDel`s | ||
13 | #[derive(Debug, Clone)] | ||
14 | pub struct Indel { | ||
15 | pub insert: String, | ||
16 | /// Refers to offsets in the original text | ||
17 | pub delete: TextRange, | ||
18 | } | ||
19 | |||
20 | #[derive(Default, Debug, Clone)] | ||
21 | pub struct TextEdit { | ||
22 | indels: Vec<Indel>, | ||
23 | } | ||
24 | |||
25 | #[derive(Debug, Default, Clone)] | ||
26 | pub struct TextEditBuilder { | ||
27 | indels: Vec<Indel>, | ||
28 | } | ||
29 | |||
30 | impl Indel { | ||
31 | pub fn insert(offset: TextSize, text: String) -> Indel { | ||
32 | Indel::replace(TextRange::empty(offset), text) | ||
33 | } | ||
34 | pub fn delete(range: TextRange) -> Indel { | ||
35 | Indel::replace(range, String::new()) | ||
36 | } | ||
37 | pub fn replace(range: TextRange, replace_with: String) -> Indel { | ||
38 | Indel { delete: range, insert: replace_with } | ||
39 | } | ||
40 | |||
41 | pub fn apply(&self, text: &mut String) { | ||
42 | let start: usize = self.delete.start().into(); | ||
43 | let end: usize = self.delete.end().into(); | ||
44 | text.replace_range(start..end, &self.insert); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl TextEdit { | ||
49 | pub fn insert(offset: TextSize, text: String) -> TextEdit { | ||
50 | let mut builder = TextEditBuilder::default(); | ||
51 | builder.insert(offset, text); | ||
52 | builder.finish() | ||
53 | } | ||
54 | |||
55 | pub fn delete(range: TextRange) -> TextEdit { | ||
56 | let mut builder = TextEditBuilder::default(); | ||
57 | builder.delete(range); | ||
58 | builder.finish() | ||
59 | } | ||
60 | |||
61 | pub fn replace(range: TextRange, replace_with: String) -> TextEdit { | ||
62 | let mut builder = TextEditBuilder::default(); | ||
63 | builder.replace(range, replace_with); | ||
64 | builder.finish() | ||
65 | } | ||
66 | |||
67 | pub fn len(&self) -> usize { | ||
68 | self.indels.len() | ||
69 | } | ||
70 | |||
71 | pub fn is_empty(&self) -> bool { | ||
72 | self.indels.is_empty() | ||
73 | } | ||
74 | |||
75 | pub fn iter(&self) -> slice::Iter<'_, Indel> { | ||
76 | self.indels.iter() | ||
77 | } | ||
78 | |||
79 | pub fn apply(&self, text: &mut String) { | ||
80 | match self.len() { | ||
81 | 0 => return, | ||
82 | 1 => { | ||
83 | self.indels[0].apply(text); | ||
84 | return; | ||
85 | } | ||
86 | _ => (), | ||
87 | } | ||
88 | |||
89 | let mut total_len = TextSize::of(&*text); | ||
90 | for indel in self.indels.iter() { | ||
91 | total_len += TextSize::of(&indel.insert); | ||
92 | total_len -= indel.delete.end() - indel.delete.start(); | ||
93 | } | ||
94 | let mut buf = String::with_capacity(total_len.into()); | ||
95 | let mut prev = 0; | ||
96 | for indel in self.indels.iter() { | ||
97 | let start: usize = indel.delete.start().into(); | ||
98 | let end: usize = indel.delete.end().into(); | ||
99 | if start > prev { | ||
100 | buf.push_str(&text[prev..start]); | ||
101 | } | ||
102 | buf.push_str(&indel.insert); | ||
103 | prev = end; | ||
104 | } | ||
105 | buf.push_str(&text[prev..text.len()]); | ||
106 | assert_eq!(TextSize::of(&buf), total_len); | ||
107 | |||
108 | // FIXME: figure out a way to mutate the text in-place or reuse the | ||
109 | // memory in some other way | ||
110 | *text = buf | ||
111 | } | ||
112 | |||
113 | pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> { | ||
114 | // FIXME: can be done without allocating intermediate vector | ||
115 | let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>(); | ||
116 | if !check_disjoint(&mut all) { | ||
117 | return Err(other); | ||
118 | } | ||
119 | self.indels.extend(other.indels); | ||
120 | assert!(check_disjoint(&mut self.indels)); | ||
121 | Ok(()) | ||
122 | } | ||
123 | |||
124 | pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> { | ||
125 | let mut res = offset; | ||
126 | for indel in self.indels.iter() { | ||
127 | if indel.delete.start() >= offset { | ||
128 | break; | ||
129 | } | ||
130 | if offset < indel.delete.end() { | ||
131 | return None; | ||
132 | } | ||
133 | res += TextSize::of(&indel.insert); | ||
134 | res -= indel.delete.len(); | ||
135 | } | ||
136 | Some(res) | ||
137 | } | ||
138 | } | ||
139 | |||
140 | impl IntoIterator for TextEdit { | ||
141 | type Item = Indel; | ||
142 | type IntoIter = vec::IntoIter<Self::Item>; | ||
143 | |||
144 | fn into_iter(self) -> Self::IntoIter { | ||
145 | self.indels.into_iter() | ||
146 | } | ||
147 | } | ||
148 | |||
149 | impl TextEditBuilder { | ||
150 | pub fn replace(&mut self, range: TextRange, replace_with: String) { | ||
151 | self.indels.push(Indel::replace(range, replace_with)) | ||
152 | } | ||
153 | pub fn delete(&mut self, range: TextRange) { | ||
154 | self.indels.push(Indel::delete(range)) | ||
155 | } | ||
156 | pub fn insert(&mut self, offset: TextSize, text: String) { | ||
157 | self.indels.push(Indel::insert(offset, text)) | ||
158 | } | ||
159 | pub fn finish(self) -> TextEdit { | ||
160 | let mut indels = self.indels; | ||
161 | assert!(check_disjoint(&mut indels)); | ||
162 | TextEdit { indels } | ||
163 | } | ||
164 | pub fn invalidates_offset(&self, offset: TextSize) -> bool { | ||
165 | self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset)) | ||
166 | } | ||
167 | } | ||
168 | |||
169 | fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool { | ||
170 | indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end())); | ||
171 | indels | ||
172 | .iter() | ||
173 | .zip(indels.iter().skip(1)) | ||
174 | .all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start()) | ||
175 | } | ||