tab_map.rs

  1use super::fold_map::{self, FoldEdit, FoldPoint, FoldSnapshot, ToFoldPoint};
  2use crate::MultiBufferSnapshot;
  3use language::{rope, Chunk};
  4use parking_lot::Mutex;
  5use std::{cmp, mem, ops::Range};
  6use sum_tree::Bias;
  7use text::Point;
  8use theme::SyntaxTheme;
  9
 10pub struct TabMap(Mutex<TabSnapshot>);
 11
 12impl TabMap {
 13    pub fn new(input: FoldSnapshot, tab_size: usize) -> (Self, TabSnapshot) {
 14        let snapshot = TabSnapshot {
 15            fold_snapshot: input,
 16            tab_size,
 17        };
 18        (Self(Mutex::new(snapshot.clone())), snapshot)
 19    }
 20
 21    pub fn sync(
 22        &self,
 23        fold_snapshot: FoldSnapshot,
 24        mut fold_edits: Vec<FoldEdit>,
 25    ) -> (TabSnapshot, Vec<TabEdit>) {
 26        let mut old_snapshot = self.0.lock();
 27        let max_offset = old_snapshot.fold_snapshot.len();
 28        let new_snapshot = TabSnapshot {
 29            fold_snapshot,
 30            tab_size: old_snapshot.tab_size,
 31        };
 32
 33        let mut tab_edits = Vec::with_capacity(fold_edits.len());
 34        for fold_edit in &mut fold_edits {
 35            let mut delta = 0;
 36            for chunk in old_snapshot
 37                .fold_snapshot
 38                .chunks(fold_edit.old.end..max_offset, None)
 39            {
 40                let patterns: &[_] = &['\t', '\n'];
 41                if let Some(ix) = chunk.text.find(patterns) {
 42                    if &chunk.text[ix..ix + 1] == "\t" {
 43                        fold_edit.old.end.0 += delta + ix + 1;
 44                        fold_edit.new.end.0 += delta + ix + 1;
 45                    }
 46
 47                    break;
 48                }
 49
 50                delta += chunk.text.len();
 51            }
 52        }
 53
 54        let mut ix = 1;
 55        while ix < fold_edits.len() {
 56            let (prev_edits, next_edits) = fold_edits.split_at_mut(ix);
 57            let prev_edit = prev_edits.last_mut().unwrap();
 58            let edit = &next_edits[0];
 59            if prev_edit.old.end >= edit.old.start {
 60                prev_edit.old.end = edit.old.end;
 61                prev_edit.new.end = edit.new.end;
 62                fold_edits.remove(ix);
 63            } else {
 64                ix += 1;
 65            }
 66        }
 67
 68        for fold_edit in fold_edits {
 69            let old_start = fold_edit.old.start.to_point(&old_snapshot.fold_snapshot);
 70            let old_end = fold_edit.old.end.to_point(&old_snapshot.fold_snapshot);
 71            let new_start = fold_edit.new.start.to_point(&new_snapshot.fold_snapshot);
 72            let new_end = fold_edit.new.end.to_point(&new_snapshot.fold_snapshot);
 73            tab_edits.push(TabEdit {
 74                old: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
 75                new: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
 76            });
 77        }
 78
 79        *old_snapshot = new_snapshot;
 80        (old_snapshot.clone(), tab_edits)
 81    }
 82}
 83
 84#[derive(Clone)]
 85pub struct TabSnapshot {
 86    pub fold_snapshot: FoldSnapshot,
 87    pub tab_size: usize,
 88}
 89
 90impl TabSnapshot {
 91    pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
 92        self.fold_snapshot.buffer_snapshot()
 93    }
 94
 95    pub fn text_summary(&self) -> TextSummary {
 96        self.text_summary_for_range(TabPoint::zero()..self.max_point())
 97    }
 98
 99    pub fn text_summary_for_range(&self, range: Range<TabPoint>) -> TextSummary {
100        let input_start = self.to_fold_point(range.start, Bias::Left).0;
101        let input_end = self.to_fold_point(range.end, Bias::Right).0;
102        let input_summary = self
103            .fold_snapshot
104            .text_summary_for_range(input_start..input_end);
105
106        let mut first_line_chars = 0;
107        let line_end = if range.start.row() == range.end.row() {
108            range.end
109        } else {
110            self.max_point()
111        };
112        for c in self
113            .chunks(range.start..line_end, None)
114            .flat_map(|chunk| chunk.text.chars())
115        {
116            if c == '\n' {
117                break;
118            }
119            first_line_chars += 1;
120        }
121
122        let mut last_line_chars = 0;
123        if range.start.row() == range.end.row() {
124            last_line_chars = first_line_chars;
125        } else {
126            for _ in self
127                .chunks(TabPoint::new(range.end.row(), 0)..range.end, None)
128                .flat_map(|chunk| chunk.text.chars())
129            {
130                last_line_chars += 1;
131            }
132        }
133
134        TextSummary {
135            lines: range.end.0 - range.start.0,
136            first_line_chars,
137            last_line_chars,
138            longest_row: input_summary.longest_row,
139            longest_row_chars: input_summary.longest_row_chars,
140        }
141    }
142
143    pub fn version(&self) -> usize {
144        self.fold_snapshot.version
145    }
146
147    pub fn chunks<'a>(
148        &'a self,
149        range: Range<TabPoint>,
150        theme: Option<&'a SyntaxTheme>,
151    ) -> TabChunks<'a> {
152        let (input_start, expanded_char_column, to_next_stop) =
153            self.to_fold_point(range.start, Bias::Left);
154        let input_start = input_start.to_offset(&self.fold_snapshot);
155        let input_end = self
156            .to_fold_point(range.end, Bias::Right)
157            .0
158            .to_offset(&self.fold_snapshot);
159        let to_next_stop = if range.start.0 + Point::new(0, to_next_stop as u32) > range.end.0 {
160            (range.end.column() - range.start.column()) as usize
161        } else {
162            to_next_stop
163        };
164
165        TabChunks {
166            fold_chunks: self.fold_snapshot.chunks(input_start..input_end, theme),
167            column: expanded_char_column,
168            output_position: range.start.0,
169            max_output_position: range.end.0,
170            tab_size: self.tab_size,
171            chunk: Chunk {
172                text: &SPACES[0..to_next_stop],
173                ..Default::default()
174            },
175            skip_leading_tab: to_next_stop > 0,
176        }
177    }
178
179    pub fn buffer_rows(&self, row: u32) -> fold_map::FoldBufferRows {
180        self.fold_snapshot.buffer_rows(row)
181    }
182
183    #[cfg(test)]
184    pub fn text(&self) -> String {
185        self.chunks(TabPoint::zero()..self.max_point(), None)
186            .map(|chunk| chunk.text)
187            .collect()
188    }
189
190    pub fn max_point(&self) -> TabPoint {
191        self.to_tab_point(self.fold_snapshot.max_point())
192    }
193
194    pub fn clip_point(&self, point: TabPoint, bias: Bias) -> TabPoint {
195        self.to_tab_point(
196            self.fold_snapshot
197                .clip_point(self.to_fold_point(point, bias).0, bias),
198        )
199    }
200
201    pub fn to_tab_point(&self, input: FoldPoint) -> TabPoint {
202        let chars = self.fold_snapshot.chars_at(FoldPoint::new(input.row(), 0));
203        let expanded = Self::expand_tabs(chars, input.column() as usize, self.tab_size);
204        TabPoint::new(input.row(), expanded as u32)
205    }
206
207    pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint {
208        self.to_tab_point(point.to_fold_point(&self.fold_snapshot, bias))
209    }
210
211    pub fn to_fold_point(&self, output: TabPoint, bias: Bias) -> (FoldPoint, usize, usize) {
212        let chars = self.fold_snapshot.chars_at(FoldPoint::new(output.row(), 0));
213        let expanded = output.column() as usize;
214        let (collapsed, expanded_char_column, to_next_stop) =
215            Self::collapse_tabs(chars, expanded, bias, self.tab_size);
216        (
217            FoldPoint::new(output.row(), collapsed as u32),
218            expanded_char_column,
219            to_next_stop,
220        )
221    }
222
223    pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
224        self.to_fold_point(point, bias)
225            .0
226            .to_buffer_point(&self.fold_snapshot)
227    }
228
229    fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
230        let mut expanded_chars = 0;
231        let mut expanded_bytes = 0;
232        let mut collapsed_bytes = 0;
233        for c in chars {
234            if collapsed_bytes == column {
235                break;
236            }
237            if c == '\t' {
238                let tab_len = tab_size - expanded_chars % tab_size;
239                expanded_bytes += tab_len;
240                expanded_chars += tab_len;
241            } else {
242                expanded_bytes += c.len_utf8();
243                expanded_chars += 1;
244            }
245            collapsed_bytes += c.len_utf8();
246        }
247        expanded_bytes
248    }
249
250    fn collapse_tabs(
251        mut chars: impl Iterator<Item = char>,
252        column: usize,
253        bias: Bias,
254        tab_size: usize,
255    ) -> (usize, usize, usize) {
256        let mut expanded_bytes = 0;
257        let mut expanded_chars = 0;
258        let mut collapsed_bytes = 0;
259        while let Some(c) = chars.next() {
260            if expanded_bytes >= column {
261                break;
262            }
263
264            if c == '\t' {
265                let tab_len = tab_size - (expanded_chars % tab_size);
266                expanded_chars += tab_len;
267                expanded_bytes += tab_len;
268                if expanded_bytes > column {
269                    expanded_chars -= expanded_bytes - column;
270                    return match bias {
271                        Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column),
272                        Bias::Right => (collapsed_bytes + 1, expanded_chars, 0),
273                    };
274                }
275            } else {
276                expanded_chars += 1;
277                expanded_bytes += c.len_utf8();
278            }
279
280            if expanded_bytes > column && matches!(bias, Bias::Left) {
281                expanded_chars -= 1;
282                break;
283            }
284
285            collapsed_bytes += c.len_utf8();
286        }
287        (collapsed_bytes, expanded_chars, 0)
288    }
289}
290
291#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
292pub struct TabPoint(pub super::Point);
293
294impl TabPoint {
295    pub fn new(row: u32, column: u32) -> Self {
296        Self(super::Point::new(row, column))
297    }
298
299    pub fn zero() -> Self {
300        Self::new(0, 0)
301    }
302
303    pub fn row(self) -> u32 {
304        self.0.row
305    }
306
307    pub fn column(self) -> u32 {
308        self.0.column
309    }
310}
311
312impl From<super::Point> for TabPoint {
313    fn from(point: super::Point) -> Self {
314        Self(point)
315    }
316}
317
318pub type TabEdit = text::Edit<TabPoint>;
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 TabChunks<'a> {
373    fold_chunks: fold_map::FoldChunks<'a>,
374    chunk: Chunk<'a>,
375    column: usize,
376    output_position: Point,
377    max_output_position: Point,
378    tab_size: usize,
379    skip_leading_tab: bool,
380}
381
382impl<'a> Iterator for TabChunks<'a> {
383    type Item = Chunk<'a>;
384
385    fn next(&mut self) -> Option<Self::Item> {
386        if self.chunk.text.is_empty() {
387            if let Some(chunk) = self.fold_chunks.next() {
388                self.chunk = chunk;
389                if self.skip_leading_tab {
390                    self.chunk.text = &self.chunk.text[1..];
391                    self.skip_leading_tab = false;
392                }
393            } else {
394                return None;
395            }
396        }
397
398        for (ix, c) in self.chunk.text.char_indices() {
399            match c {
400                '\t' => {
401                    if ix > 0 {
402                        let (prefix, suffix) = self.chunk.text.split_at(ix);
403                        self.chunk.text = suffix;
404                        return Some(Chunk {
405                            text: prefix,
406                            ..self.chunk
407                        });
408                    } else {
409                        self.chunk.text = &self.chunk.text[1..];
410                        let mut len = self.tab_size - self.column % self.tab_size;
411                        let next_output_position = cmp::min(
412                            self.output_position + Point::new(0, len as u32),
413                            self.max_output_position,
414                        );
415                        len = (next_output_position.column - self.output_position.column) as usize;
416                        self.column += len;
417                        self.output_position = next_output_position;
418                        return Some(Chunk {
419                            text: &SPACES[0..len],
420                            ..self.chunk
421                        });
422                    }
423                }
424                '\n' => {
425                    self.column = 0;
426                    self.output_position += Point::new(1, 0);
427                }
428                _ => {
429                    self.column += 1;
430                    self.output_position.column += c.len_utf8() as u32;
431                }
432            }
433        }
434
435        Some(mem::take(&mut self.chunk))
436    }
437}
438
439#[cfg(test)]
440mod tests {
441    use super::*;
442    use crate::{display_map::fold_map::FoldMap, MultiBuffer};
443    use rand::{prelude::StdRng, Rng};
444    use text::{RandomCharIter, Rope};
445
446    #[test]
447    fn test_expand_tabs() {
448        assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 0, 4), 0);
449        assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 1, 4), 4);
450        assert_eq!(TabSnapshot::expand_tabs("\ta".chars(), 2, 4), 5);
451    }
452
453    #[gpui::test(iterations = 100)]
454    fn test_random_tabs(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
455        let tab_size = rng.gen_range(1..=4);
456        let len = rng.gen_range(0..30);
457        let buffer = if rng.gen() {
458            let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
459            MultiBuffer::build_simple(&text, cx)
460        } else {
461            MultiBuffer::build_random(&mut rng, cx)
462        };
463        let buffer_snapshot = buffer.read(cx).snapshot(cx);
464        log::info!("Buffer text: {:?}", buffer_snapshot.text());
465
466        let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
467        fold_map.randomly_mutate(&mut rng);
468        let (folds_snapshot, _) = fold_map.read(buffer_snapshot.clone(), vec![]);
469        log::info!("FoldMap text: {:?}", folds_snapshot.text());
470
471        let (_, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
472        let text = Rope::from(tabs_snapshot.text().as_str());
473        log::info!(
474            "TabMap text (tab size: {}): {:?}",
475            tab_size,
476            tabs_snapshot.text(),
477        );
478
479        for _ in 0..5 {
480            let end_row = rng.gen_range(0..=text.max_point().row);
481            let end_column = rng.gen_range(0..=text.line_len(end_row));
482            let mut end = TabPoint(text.clip_point(Point::new(end_row, end_column), Bias::Right));
483            let start_row = rng.gen_range(0..=text.max_point().row);
484            let start_column = rng.gen_range(0..=text.line_len(start_row));
485            let mut start =
486                TabPoint(text.clip_point(Point::new(start_row, start_column), Bias::Left));
487            if start > end {
488                mem::swap(&mut start, &mut end);
489            }
490
491            let expected_text = text
492                .chunks_in_range(text.point_to_offset(start.0)..text.point_to_offset(end.0))
493                .collect::<String>();
494            let expected_summary = TextSummary::from(expected_text.as_str());
495            assert_eq!(
496                expected_text,
497                tabs_snapshot
498                    .chunks(start..end, None)
499                    .map(|c| c.text)
500                    .collect::<String>(),
501                "chunks({:?}..{:?})",
502                start,
503                end
504            );
505
506            let mut actual_summary = tabs_snapshot.text_summary_for_range(start..end);
507            if tab_size > 1 && folds_snapshot.text().contains('\t') {
508                actual_summary.longest_row = expected_summary.longest_row;
509                actual_summary.longest_row_chars = expected_summary.longest_row_chars;
510            }
511
512            assert_eq!(actual_summary, expected_summary,);
513        }
514    }
515}