1use super::fold_map::{self, FoldEdit, FoldPoint, Snapshot as FoldSnapshot, ToFoldPoint};
2use buffer::Point;
3use language::{rope, Chunk};
4use parking_lot::Mutex;
5use std::{mem, ops::Range};
6use sum_tree::Bias;
7
8pub struct TabMap(Mutex<Snapshot>);
9
10impl TabMap {
11 pub fn new(input: FoldSnapshot, tab_size: usize) -> (Self, Snapshot) {
12 let snapshot = Snapshot {
13 fold_snapshot: input,
14 tab_size,
15 };
16 (Self(Mutex::new(snapshot.clone())), snapshot)
17 }
18
19 pub fn sync(
20 &self,
21 fold_snapshot: FoldSnapshot,
22 mut fold_edits: Vec<FoldEdit>,
23 ) -> (Snapshot, Vec<Edit>) {
24 let mut old_snapshot = self.0.lock();
25 let max_offset = old_snapshot.fold_snapshot.len();
26 let new_snapshot = Snapshot {
27 fold_snapshot,
28 tab_size: old_snapshot.tab_size,
29 };
30
31 let mut tab_edits = Vec::with_capacity(fold_edits.len());
32 for fold_edit in &mut fold_edits {
33 let mut delta = 0;
34 for chunk in old_snapshot
35 .fold_snapshot
36 .chunks(fold_edit.old_bytes.end..max_offset, false)
37 {
38 let patterns: &[_] = &['\t', '\n'];
39 if let Some(ix) = chunk.text.find(patterns) {
40 if &chunk.text[ix..ix + 1] == "\t" {
41 fold_edit.old_bytes.end.0 += delta + ix + 1;
42 fold_edit.new_bytes.end.0 += delta + ix + 1;
43 }
44
45 break;
46 }
47
48 delta += chunk.text.len();
49 }
50 }
51
52 let mut ix = 1;
53 while ix < fold_edits.len() {
54 let (prev_edits, next_edits) = fold_edits.split_at_mut(ix);
55 let prev_edit = prev_edits.last_mut().unwrap();
56 let edit = &next_edits[0];
57 if prev_edit.old_bytes.end >= edit.old_bytes.start {
58 prev_edit.old_bytes.end = edit.old_bytes.end;
59 prev_edit.new_bytes.end = edit.new_bytes.end;
60 fold_edits.remove(ix);
61 } else {
62 ix += 1;
63 }
64 }
65
66 for fold_edit in fold_edits {
67 let old_start = fold_edit
68 .old_bytes
69 .start
70 .to_point(&old_snapshot.fold_snapshot);
71 let old_end = fold_edit
72 .old_bytes
73 .end
74 .to_point(&old_snapshot.fold_snapshot);
75 let new_start = fold_edit
76 .new_bytes
77 .start
78 .to_point(&new_snapshot.fold_snapshot);
79 let new_end = fold_edit
80 .new_bytes
81 .end
82 .to_point(&new_snapshot.fold_snapshot);
83 tab_edits.push(Edit {
84 old_lines: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
85 new_lines: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
86 });
87 }
88
89 *old_snapshot = new_snapshot;
90 (old_snapshot.clone(), tab_edits)
91 }
92}
93
94#[derive(Clone)]
95pub struct Snapshot {
96 pub fold_snapshot: FoldSnapshot,
97 pub tab_size: usize,
98}
99
100impl Snapshot {
101 pub fn text_summary(&self) -> TextSummary {
102 self.text_summary_for_range(TabPoint::zero()..self.max_point())
103 }
104
105 pub fn text_summary_for_range(&self, range: Range<TabPoint>) -> TextSummary {
106 let input_start = self.to_fold_point(range.start, Bias::Left).0;
107 let input_end = self.to_fold_point(range.end, Bias::Right).0;
108 let input_summary = self
109 .fold_snapshot
110 .text_summary_for_range(input_start..input_end);
111
112 let mut first_line_chars = 0;
113 let line_end = if range.start.row() == range.end.row() {
114 range.end
115 } else {
116 self.max_point()
117 };
118 for c in self
119 .chunks(range.start..line_end, false)
120 .flat_map(|chunk| chunk.text.chars())
121 {
122 if c == '\n' {
123 break;
124 }
125 first_line_chars += 1;
126 }
127
128 let mut last_line_chars = 0;
129 if range.start.row() == range.end.row() {
130 last_line_chars = first_line_chars;
131 } else {
132 for _ in self
133 .chunks(TabPoint::new(range.end.row(), 0)..range.end, false)
134 .flat_map(|chunk| chunk.text.chars())
135 {
136 last_line_chars += 1;
137 }
138 }
139
140 TextSummary {
141 lines: range.end.0 - range.start.0,
142 first_line_chars,
143 last_line_chars,
144 longest_row: input_summary.longest_row,
145 longest_row_chars: input_summary.longest_row_chars,
146 }
147 }
148
149 pub fn version(&self) -> usize {
150 self.fold_snapshot.version
151 }
152
153 pub fn chunks(&self, range: Range<TabPoint>, highlights: bool) -> Chunks {
154 let (input_start, expanded_char_column, to_next_stop) =
155 self.to_fold_point(range.start, Bias::Left);
156 let input_start = input_start.to_offset(&self.fold_snapshot);
157 let input_end = self
158 .to_fold_point(range.end, Bias::Right)
159 .0
160 .to_offset(&self.fold_snapshot);
161 Chunks {
162 fold_chunks: self
163 .fold_snapshot
164 .chunks(input_start..input_end, highlights),
165 column: expanded_char_column,
166 tab_size: self.tab_size,
167 chunk: Chunk {
168 text: &SPACES[0..to_next_stop],
169 ..Default::default()
170 },
171 skip_leading_tab: to_next_stop > 0,
172 }
173 }
174
175 pub fn buffer_rows(&self, row: u32) -> fold_map::BufferRows {
176 self.fold_snapshot.buffer_rows(row)
177 }
178
179 #[cfg(test)]
180 pub fn text(&self) -> String {
181 self.chunks(TabPoint::zero()..self.max_point(), false)
182 .map(|chunk| chunk.text)
183 .collect()
184 }
185
186 pub fn max_point(&self) -> TabPoint {
187 self.to_tab_point(self.fold_snapshot.max_point())
188 }
189
190 pub fn clip_point(&self, point: TabPoint, bias: Bias) -> TabPoint {
191 self.to_tab_point(
192 self.fold_snapshot
193 .clip_point(self.to_fold_point(point, bias).0, bias),
194 )
195 }
196
197 pub fn to_tab_point(&self, input: FoldPoint) -> TabPoint {
198 let chars = self.fold_snapshot.chars_at(FoldPoint::new(input.row(), 0));
199 let expanded = Self::expand_tabs(chars, input.column() as usize, self.tab_size);
200 TabPoint::new(input.row(), expanded as u32)
201 }
202
203 pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint {
204 self.to_tab_point(point.to_fold_point(&self.fold_snapshot, bias))
205 }
206
207 pub fn to_fold_point(&self, output: TabPoint, bias: Bias) -> (FoldPoint, usize, usize) {
208 let chars = self.fold_snapshot.chars_at(FoldPoint::new(output.row(), 0));
209 let expanded = output.column() as usize;
210 let (collapsed, expanded_char_column, to_next_stop) =
211 Self::collapse_tabs(chars, expanded, bias, self.tab_size);
212 (
213 FoldPoint::new(output.row(), collapsed as u32),
214 expanded_char_column,
215 to_next_stop,
216 )
217 }
218
219 pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
220 self.to_fold_point(point, bias)
221 .0
222 .to_buffer_point(&self.fold_snapshot)
223 }
224
225 fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
226 let mut expanded_chars = 0;
227 let mut expanded_bytes = 0;
228 let mut collapsed_bytes = 0;
229 for c in chars {
230 if collapsed_bytes == column {
231 break;
232 }
233 if c == '\t' {
234 let tab_len = tab_size - expanded_chars % tab_size;
235 expanded_bytes += tab_len;
236 expanded_chars += tab_len;
237 } else {
238 expanded_bytes += c.len_utf8();
239 expanded_chars += 1;
240 }
241 collapsed_bytes += c.len_utf8();
242 }
243 expanded_bytes
244 }
245
246 fn collapse_tabs(
247 mut chars: impl Iterator<Item = char>,
248 column: usize,
249 bias: Bias,
250 tab_size: usize,
251 ) -> (usize, usize, usize) {
252 let mut expanded_bytes = 0;
253 let mut expanded_chars = 0;
254 let mut collapsed_bytes = 0;
255 while let Some(c) = chars.next() {
256 if expanded_bytes >= column {
257 break;
258 }
259
260 if c == '\t' {
261 let tab_len = tab_size - (expanded_chars % tab_size);
262 expanded_chars += tab_len;
263 expanded_bytes += tab_len;
264 if expanded_bytes > column {
265 expanded_chars -= expanded_bytes - column;
266 return match bias {
267 Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column),
268 Bias::Right => (collapsed_bytes + 1, expanded_chars, 0),
269 };
270 }
271 } else {
272 expanded_chars += 1;
273 expanded_bytes += c.len_utf8();
274 }
275
276 if expanded_bytes > column && matches!(bias, Bias::Left) {
277 expanded_chars -= 1;
278 break;
279 }
280
281 collapsed_bytes += c.len_utf8();
282 }
283 (collapsed_bytes, expanded_chars, 0)
284 }
285}
286
287#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
288pub struct TabPoint(pub super::Point);
289
290impl TabPoint {
291 pub fn new(row: u32, column: u32) -> Self {
292 Self(super::Point::new(row, column))
293 }
294
295 pub fn zero() -> Self {
296 Self::new(0, 0)
297 }
298
299 pub fn row(self) -> u32 {
300 self.0.row
301 }
302
303 pub fn column(self) -> u32 {
304 self.0.column
305 }
306}
307
308impl From<super::Point> for TabPoint {
309 fn from(point: super::Point) -> Self {
310 Self(point)
311 }
312}
313
314#[derive(Clone, Debug, PartialEq, Eq)]
315pub struct Edit {
316 pub old_lines: Range<TabPoint>,
317 pub new_lines: Range<TabPoint>,
318}
319
320#[derive(Clone, Debug, Default, Eq, PartialEq)]
321pub struct TextSummary {
322 pub lines: super::Point,
323 pub first_line_chars: u32,
324 pub last_line_chars: u32,
325 pub longest_row: u32,
326 pub longest_row_chars: u32,
327}
328
329impl<'a> From<&'a str> for TextSummary {
330 fn from(text: &'a str) -> Self {
331 let sum = rope::TextSummary::from(text);
332
333 TextSummary {
334 lines: sum.lines,
335 first_line_chars: sum.first_line_chars,
336 last_line_chars: sum.last_line_chars,
337 longest_row: sum.longest_row,
338 longest_row_chars: sum.longest_row_chars,
339 }
340 }
341}
342
343impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
344 fn add_assign(&mut self, other: &'a Self) {
345 let joined_chars = self.last_line_chars + other.first_line_chars;
346 if joined_chars > self.longest_row_chars {
347 self.longest_row = self.lines.row;
348 self.longest_row_chars = joined_chars;
349 }
350 if other.longest_row_chars > self.longest_row_chars {
351 self.longest_row = self.lines.row + other.longest_row;
352 self.longest_row_chars = other.longest_row_chars;
353 }
354
355 if self.lines.row == 0 {
356 self.first_line_chars += other.first_line_chars;
357 }
358
359 if other.lines.row == 0 {
360 self.last_line_chars += other.first_line_chars;
361 } else {
362 self.last_line_chars = other.last_line_chars;
363 }
364
365 self.lines += &other.lines;
366 }
367}
368
369// Handles a tab width <= 16
370const SPACES: &'static str = " ";
371
372pub struct Chunks<'a> {
373 fold_chunks: fold_map::Chunks<'a>,
374 chunk: Chunk<'a>,
375 column: usize,
376 tab_size: usize,
377 skip_leading_tab: bool,
378}
379
380impl<'a> Iterator for Chunks<'a> {
381 type Item = Chunk<'a>;
382
383 fn next(&mut self) -> Option<Self::Item> {
384 if self.chunk.text.is_empty() {
385 if let Some(chunk) = self.fold_chunks.next() {
386 self.chunk = chunk;
387 if self.skip_leading_tab {
388 self.chunk.text = &self.chunk.text[1..];
389 self.skip_leading_tab = false;
390 }
391 } else {
392 return None;
393 }
394 }
395
396 for (ix, c) in self.chunk.text.char_indices() {
397 match c {
398 '\t' => {
399 if ix > 0 {
400 let (prefix, suffix) = self.chunk.text.split_at(ix);
401 self.chunk.text = suffix;
402 return Some(Chunk {
403 text: prefix,
404 ..self.chunk
405 });
406 } else {
407 self.chunk.text = &self.chunk.text[1..];
408 let len = self.tab_size - self.column % self.tab_size;
409 self.column += len;
410 return Some(Chunk {
411 text: &SPACES[0..len],
412 ..self.chunk
413 });
414 }
415 }
416 '\n' => self.column = 0,
417 _ => self.column += 1,
418 }
419 }
420
421 Some(mem::take(&mut self.chunk))
422 }
423}
424
425#[cfg(test)]
426mod tests {
427 use buffer::RandomCharIter;
428 use language::Buffer;
429 use rand::{prelude::StdRng, Rng};
430
431 use crate::display_map::fold_map::FoldMap;
432
433 use super::*;
434
435 #[test]
436 fn test_expand_tabs() {
437 assert_eq!(Snapshot::expand_tabs("\t".chars(), 0, 4), 0);
438 assert_eq!(Snapshot::expand_tabs("\t".chars(), 1, 4), 4);
439 assert_eq!(Snapshot::expand_tabs("\ta".chars(), 2, 4), 5);
440 }
441
442 #[gpui::test(iterations = 100)]
443 fn test_text_summary_for_range(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
444 let tab_size = rng.gen_range(1..=4);
445 let buffer = cx.add_model(|cx| {
446 let len = rng.gen_range(0..30);
447 let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
448 Buffer::new(0, text, cx)
449 });
450 let (_, folds_snapshot) = FoldMap::new(buffer.clone(), cx);
451 let (_, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
452
453 println!("{:?}", tabs_snapshot.text());
454 // TODO: Test text_summary_for_range with random ranges
455 }
456}