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