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