tab_map.rs

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