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 insert(offset: TextSize, text: String) -> TextEdit {
52 let mut builder = TextEdit::builder();
53 builder.insert(offset, text);
57 pub fn delete(range: TextRange) -> TextEdit {
58 let mut builder = TextEdit::builder();
59 builder.delete(range);
63 pub fn replace(range: TextRange, replace_with: String) -> TextEdit {
64 let mut builder = TextEdit::builder();
65 builder.replace(range, replace_with);
69 pub fn len(&self) -> usize {
73 pub fn is_empty(&self) -> bool {
74 self.indels.is_empty()
77 pub fn iter(&self) -> std::slice::Iter<'_, Indel> {
81 pub fn apply(&self, text: &mut String) {
85 self.indels[0].apply(text);
91 let mut total_len = TextSize::of(&*text);
92 for indel in self.indels.iter() {
93 total_len += TextSize::of(&indel.insert);
94 total_len -= indel.delete.end() - indel.delete.start();
96 let mut buf = String::with_capacity(total_len.into());
98 for indel in self.indels.iter() {
99 let start: usize = indel.delete.start().into();
100 let end: usize = indel.delete.end().into();
102 buf.push_str(&text[prev..start]);
104 buf.push_str(&indel.insert);
107 buf.push_str(&text[prev..text.len()]);
108 assert_eq!(TextSize::of(&buf), total_len);
110 // FIXME: figure out a way to mutate the text in-place or reuse the
111 // memory in some other way
115 pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> {
116 // FIXME: can be done without allocating intermediate vector
117 let mut all = self.iter().chain(other.iter()).collect::<Vec<_>>();
118 if !check_disjoint(&mut all) {
121 self.indels.extend(other.indels);
122 assert_disjoint(&mut self.indels);
126 pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {
127 let mut res = offset;
128 for indel in self.indels.iter() {
129 if indel.delete.start() >= offset {
132 if offset < indel.delete.end() {
135 res += TextSize::of(&indel.insert);
136 res -= indel.delete.len();
142 impl IntoIterator for TextEdit {
144 type IntoIter = std::vec::IntoIter<Indel>;
146 fn into_iter(self) -> Self::IntoIter {
147 self.indels.into_iter()
151 impl<'a> IntoIterator for &'a TextEdit {
152 type Item = &'a Indel;
153 type IntoIter = std::slice::Iter<'a, Indel>;
155 fn into_iter(self) -> Self::IntoIter {
160 impl TextEditBuilder {
161 pub fn replace(&mut self, range: TextRange, replace_with: String) {
162 self.indel(Indel::replace(range, replace_with))
164 pub fn delete(&mut self, range: TextRange) {
165 self.indel(Indel::delete(range))
167 pub fn insert(&mut self, offset: TextSize, text: String) {
168 self.indel(Indel::insert(offset, text))
170 pub fn finish(self) -> TextEdit {
171 let mut indels = self.indels;
172 assert_disjoint(&mut indels);
175 pub fn invalidates_offset(&self, offset: TextSize) -> bool {
176 self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset))
178 fn indel(&mut self, indel: Indel) {
179 self.indels.push(indel);
180 if self.indels.len() <= 16 {
181 assert_disjoint(&mut self.indels);
186 fn assert_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) {
187 assert!(check_disjoint(indels));
189 fn check_disjoint(indels: &mut [impl std::borrow::Borrow<Indel>]) -> bool {
190 indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end()));
193 .zip(indels.iter().skip(1))
194 .all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start())