1 //! Representation of a `TextEdit`.
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
6 pub use text_size::{TextRange, TextSize};
8 /// `InsertDelete` -- a single "atomic" change to text
10 /// Must not overlap with other `InDel`s
11 #[derive(Debug, Clone)]
14 /// Refers to offsets in the original text
15 pub delete: TextRange,
18 #[derive(Default, Debug, Clone)]
23 #[derive(Debug, Default, Clone)]
24 pub struct TextEditBuilder {
29 pub fn insert(offset: TextSize, text: String) -> Indel {
30 Indel::replace(TextRange::empty(offset), text)
32 pub fn delete(range: TextRange) -> Indel {
33 Indel::replace(range, String::new())
35 pub fn replace(range: TextRange, replace_with: String) -> Indel {
36 Indel { delete: range, insert: replace_with }
39 pub fn apply(&self, text: &mut String) {
40 let start: usize = self.delete.start().into();
41 let end: usize = self.delete.end().into();
42 text.replace_range(start..end, &self.insert);
47 pub fn builder() -> TextEditBuilder {
48 TextEditBuilder::default()
51 pub fn into_builder(self) -> TextEditBuilder {
52 TextEditBuilder { indels: self.indels }
55 pub fn insert(offset: TextSize, text: String) -> TextEdit {
56 let mut builder = TextEdit::builder();
57 builder.insert(offset, text);
61 pub fn delete(range: TextRange) -> TextEdit {
62 let mut builder = TextEdit::builder();
63 builder.delete(range);
67 pub fn replace(range: TextRange, replace_with: String) -> TextEdit {
68 let mut builder = TextEdit::builder();
69 builder.replace(range, replace_with);
73 pub fn len(&self) -> usize {
77 pub fn is_empty(&self) -> bool {
78 self.indels.is_empty()
81 pub fn iter(&self) -> std::slice::Iter<'_, Indel> {
85 pub fn apply(&self, text: &mut String) {
89 self.indels[0].apply(text);
95 let mut total_len = TextSize::of(&*text);
96 for indel in self.indels.iter() {
97 total_len += TextSize::of(&indel.insert);
98 total_len -= indel.delete.end() - indel.delete.start();
100 let mut buf = String::with_capacity(total_len.into());
102 for indel in self.indels.iter() {
103 let start: usize = indel.delete.start().into();
104 let end: usize = indel.delete.end().into();
106 buf.push_str(&text[prev..start]);
108 buf.push_str(&indel.insert);
111 buf.push_str(&text[prev..text.len()]);
112 assert_eq!(TextSize::of(&buf), total_len);
114 // FIXME: figure out a way to mutate the text in-place or reuse the
115 // memory in some other way
119 pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> {
120 // FIXME: can be done without allocating intermediate vector
121 let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>();
122 if !check_disjoint(&mut all) {
125 self.indels.extend(other.indels);
126 assert_disjoint(&mut self.indels);
130 pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {
131 let mut res = offset;
132 for indel in self.indels.iter() {
133 if indel.delete.start() >= offset {
136 if offset < indel.delete.end() {
139 res += TextSize::of(&indel.insert);
140 res -= indel.delete.len();
146 impl IntoIterator for TextEdit {
148 type IntoIter = std::vec::IntoIter<Indel>;
150 fn into_iter(self) -> Self::IntoIter {
151 self.indels.into_iter()
155 impl<'a> IntoIterator for &'a TextEdit {
156 type Item = &'a Indel;
157 type IntoIter = std::slice::Iter<'a, Indel>;
159 fn into_iter(self) -> Self::IntoIter {
164 impl TextEditBuilder {
165 pub fn replace(&mut self, range: TextRange, replace_with: String) {
166 self.indel(Indel::replace(range, replace_with))
168 pub fn delete(&mut self, range: TextRange) {
169 self.indel(Indel::delete(range))
171 pub fn insert(&mut self, offset: TextSize, text: String) {
172 self.indel(Indel::insert(offset, text))
174 pub fn finish(self) -> TextEdit {
175 let mut indels = self.indels;
176 assert_disjoint(&mut indels);
179 pub fn invalidates_offset(&self, offset: TextSize) -> bool {
180 self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))
182 fn indel(&mut self, indel: Indel) {
183 self.indels.push(indel);
184 if self.indels.len() <= 16 {
185 assert_disjoint(&mut self.indels);
190 fn assert_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) {
191 assert!(check_disjoint(indels));
193 fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {
194 indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end()));
197 .zip(indels.iter().skip(1))
198 .all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start())