tab_map.rs

  1use super::{
  2    suggestion_map::{self, SuggestionChunks, SuggestionEdit, SuggestionPoint, SuggestionSnapshot},
  3    TextHighlights,
  4};
  5use crate::MultiBufferSnapshot;
  6use language::{Chunk, Point};
  7use parking_lot::Mutex;
  8use std::{cmp, mem, num::NonZeroU32, ops::Range};
  9use sum_tree::Bias;
 10
 11const MAX_EXPANSION_COLUMN: u32 = 256;
 12
 13pub struct TabMap(Mutex<TabSnapshot>);
 14
 15impl TabMap {
 16    pub fn new(input: SuggestionSnapshot, tab_size: NonZeroU32) -> (Self, TabSnapshot) {
 17        let snapshot = TabSnapshot {
 18            suggestion_snapshot: input,
 19            tab_size,
 20            max_expansion_column: MAX_EXPANSION_COLUMN,
 21            version: 0,
 22        };
 23        (Self(Mutex::new(snapshot.clone())), snapshot)
 24    }
 25
 26    #[cfg(test)]
 27    pub fn set_max_expansion_column(&self, column: u32) -> TabSnapshot {
 28        self.0.lock().max_expansion_column = column;
 29        self.0.lock().clone()
 30    }
 31
 32    pub fn sync(
 33        &self,
 34        suggestion_snapshot: SuggestionSnapshot,
 35        mut suggestion_edits: Vec<SuggestionEdit>,
 36        tab_size: NonZeroU32,
 37    ) -> (TabSnapshot, Vec<TabEdit>) {
 38        let mut old_snapshot = self.0.lock();
 39        let mut new_snapshot = TabSnapshot {
 40            suggestion_snapshot,
 41            tab_size,
 42            max_expansion_column: old_snapshot.max_expansion_column,
 43            version: old_snapshot.version,
 44        };
 45
 46        if old_snapshot.suggestion_snapshot.version != new_snapshot.suggestion_snapshot.version {
 47            new_snapshot.version += 1;
 48        }
 49
 50        let old_max_offset = old_snapshot.suggestion_snapshot.len();
 51        let mut tab_edits = Vec::with_capacity(suggestion_edits.len());
 52
 53        if old_snapshot.tab_size == new_snapshot.tab_size {
 54            for suggestion_edit in &mut suggestion_edits {
 55                let mut delta = 0;
 56                for chunk in old_snapshot.suggestion_snapshot.chunks(
 57                    suggestion_edit.old.end..old_max_offset,
 58                    false,
 59                    None,
 60                ) {
 61                    let patterns: &[_] = &['\t', '\n'];
 62                    if let Some(ix) = chunk.text.find(patterns) {
 63                        if &chunk.text[ix..ix + 1] == "\t" {
 64                            suggestion_edit.old.end.0 += delta + ix + 1;
 65                            suggestion_edit.new.end.0 += delta + ix + 1;
 66                        }
 67
 68                        break;
 69                    }
 70
 71                    delta += chunk.text.len();
 72                }
 73            }
 74
 75            let mut ix = 1;
 76            while ix < suggestion_edits.len() {
 77                let (prev_edits, next_edits) = suggestion_edits.split_at_mut(ix);
 78                let prev_edit = prev_edits.last_mut().unwrap();
 79                let edit = &next_edits[0];
 80                if prev_edit.old.end >= edit.old.start {
 81                    prev_edit.old.end = edit.old.end;
 82                    prev_edit.new.end = edit.new.end;
 83                    suggestion_edits.remove(ix);
 84                } else {
 85                    ix += 1;
 86                }
 87            }
 88
 89            for suggestion_edit in suggestion_edits {
 90                let old_start = old_snapshot
 91                    .suggestion_snapshot
 92                    .to_point(suggestion_edit.old.start);
 93                let old_end = old_snapshot
 94                    .suggestion_snapshot
 95                    .to_point(suggestion_edit.old.end);
 96                let new_start = new_snapshot
 97                    .suggestion_snapshot
 98                    .to_point(suggestion_edit.new.start);
 99                let new_end = new_snapshot
100                    .suggestion_snapshot
101                    .to_point(suggestion_edit.new.end);
102                tab_edits.push(TabEdit {
103                    old: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
104                    new: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
105                });
106            }
107        } else {
108            new_snapshot.version += 1;
109            tab_edits.push(TabEdit {
110                old: TabPoint::zero()..old_snapshot.max_point(),
111                new: TabPoint::zero()..new_snapshot.max_point(),
112            });
113        }
114
115        *old_snapshot = new_snapshot;
116        (old_snapshot.clone(), tab_edits)
117    }
118}
119
120#[derive(Clone)]
121pub struct TabSnapshot {
122    pub suggestion_snapshot: SuggestionSnapshot,
123    pub tab_size: NonZeroU32,
124    pub max_expansion_column: u32,
125    pub version: usize,
126}
127
128impl TabSnapshot {
129    pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
130        self.suggestion_snapshot.buffer_snapshot()
131    }
132
133    pub fn line_len(&self, row: u32) -> u32 {
134        let max_point = self.max_point();
135        if row < max_point.row() {
136            self.to_tab_point(SuggestionPoint::new(
137                row,
138                self.suggestion_snapshot.line_len(row),
139            ))
140            .0
141            .column
142        } else {
143            max_point.column()
144        }
145    }
146
147    pub fn text_summary(&self) -> TextSummary {
148        self.text_summary_for_range(TabPoint::zero()..self.max_point())
149    }
150
151    pub fn text_summary_for_range(&self, range: Range<TabPoint>) -> TextSummary {
152        let input_start = self.to_suggestion_point(range.start, Bias::Left).0;
153        let input_end = self.to_suggestion_point(range.end, Bias::Right).0;
154        let input_summary = self
155            .suggestion_snapshot
156            .text_summary_for_range(input_start..input_end);
157
158        let mut first_line_chars = 0;
159        let line_end = if range.start.row() == range.end.row() {
160            range.end
161        } else {
162            self.max_point()
163        };
164        for c in self
165            .chunks(range.start..line_end, false, None)
166            .flat_map(|chunk| chunk.text.chars())
167        {
168            if c == '\n' {
169                break;
170            }
171            first_line_chars += 1;
172        }
173
174        let mut last_line_chars = 0;
175        if range.start.row() == range.end.row() {
176            last_line_chars = first_line_chars;
177        } else {
178            for _ in self
179                .chunks(TabPoint::new(range.end.row(), 0)..range.end, false, None)
180                .flat_map(|chunk| chunk.text.chars())
181            {
182                last_line_chars += 1;
183            }
184        }
185
186        TextSummary {
187            lines: range.end.0 - range.start.0,
188            first_line_chars,
189            last_line_chars,
190            longest_row: input_summary.longest_row,
191            longest_row_chars: input_summary.longest_row_chars,
192        }
193    }
194
195    pub fn chunks<'a>(
196        &'a self,
197        range: Range<TabPoint>,
198        language_aware: bool,
199        text_highlights: Option<&'a TextHighlights>,
200    ) -> TabChunks<'a> {
201        let (input_start, expanded_char_column, to_next_stop) =
202            self.to_suggestion_point(range.start, Bias::Left);
203        let input_column = input_start.column();
204        let input_start = self.suggestion_snapshot.to_offset(input_start);
205        let input_end = self
206            .suggestion_snapshot
207            .to_offset(self.to_suggestion_point(range.end, Bias::Right).0);
208        let to_next_stop = if range.start.0 + Point::new(0, to_next_stop) > range.end.0 {
209            range.end.column() - range.start.column()
210        } else {
211            to_next_stop
212        };
213
214        TabChunks {
215            suggestion_chunks: self.suggestion_snapshot.chunks(
216                input_start..input_end,
217                language_aware,
218                text_highlights,
219            ),
220            input_column,
221            column: expanded_char_column,
222            max_expansion_column: self.max_expansion_column,
223            output_position: range.start.0,
224            max_output_position: range.end.0,
225            tab_size: self.tab_size,
226            chunk: Chunk {
227                text: &SPACES[0..(to_next_stop as usize)],
228                ..Default::default()
229            },
230            skip_leading_tab: to_next_stop > 0,
231        }
232    }
233
234    pub fn buffer_rows(&self, row: u32) -> suggestion_map::SuggestionBufferRows {
235        self.suggestion_snapshot.buffer_rows(row)
236    }
237
238    #[cfg(test)]
239    pub fn text(&self) -> String {
240        self.chunks(TabPoint::zero()..self.max_point(), false, None)
241            .map(|chunk| chunk.text)
242            .collect()
243    }
244
245    pub fn max_point(&self) -> TabPoint {
246        self.to_tab_point(self.suggestion_snapshot.max_point())
247    }
248
249    pub fn clip_point(&self, point: TabPoint, bias: Bias) -> TabPoint {
250        self.to_tab_point(
251            self.suggestion_snapshot
252                .clip_point(self.to_suggestion_point(point, bias).0, bias),
253        )
254    }
255
256    pub fn to_tab_point(&self, input: SuggestionPoint) -> TabPoint {
257        let chars = self
258            .suggestion_snapshot
259            .chars_at(SuggestionPoint::new(input.row(), 0));
260        let expanded = self.expand_tabs(chars, input.column());
261        TabPoint::new(input.row(), expanded)
262    }
263
264    pub fn to_suggestion_point(&self, output: TabPoint, bias: Bias) -> (SuggestionPoint, u32, u32) {
265        let chars = self
266            .suggestion_snapshot
267            .chars_at(SuggestionPoint::new(output.row(), 0));
268        let expanded = output.column();
269        let (collapsed, expanded_char_column, to_next_stop) =
270            self.collapse_tabs(chars, expanded, bias);
271        (
272            SuggestionPoint::new(output.row(), collapsed as u32),
273            expanded_char_column,
274            to_next_stop,
275        )
276    }
277
278    pub fn make_tab_point(&self, point: Point, bias: Bias) -> TabPoint {
279        let fold_point = self
280            .suggestion_snapshot
281            .fold_snapshot
282            .to_fold_point(point, bias);
283        let suggestion_point = self.suggestion_snapshot.to_suggestion_point(fold_point);
284        self.to_tab_point(suggestion_point)
285    }
286
287    pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
288        let suggestion_point = self.to_suggestion_point(point, bias).0;
289        let fold_point = self.suggestion_snapshot.to_fold_point(suggestion_point);
290        fold_point.to_buffer_point(&self.suggestion_snapshot.fold_snapshot)
291    }
292
293    fn expand_tabs(&self, chars: impl Iterator<Item = char>, column: u32) -> u32 {
294        let tab_size = self.tab_size.get();
295
296        let mut expanded_chars = 0;
297        let mut expanded_bytes = 0;
298        let mut collapsed_bytes = 0;
299        let end_column = column.min(self.max_expansion_column);
300        for c in chars {
301            if collapsed_bytes >= end_column {
302                break;
303            }
304            if c == '\t' {
305                let tab_len = tab_size - expanded_chars % tab_size;
306                expanded_bytes += tab_len;
307                expanded_chars += tab_len;
308            } else {
309                expanded_bytes += c.len_utf8() as u32;
310                expanded_chars += 1;
311            }
312            collapsed_bytes += c.len_utf8() as u32;
313        }
314        expanded_bytes + column.saturating_sub(collapsed_bytes)
315    }
316
317    fn collapse_tabs(
318        &self,
319        chars: impl Iterator<Item = char>,
320        column: u32,
321        bias: Bias,
322    ) -> (u32, u32, u32) {
323        let tab_size = self.tab_size.get();
324
325        let mut expanded_bytes = 0;
326        let mut expanded_chars = 0;
327        let mut collapsed_bytes = 0;
328        for c in chars {
329            if expanded_bytes >= column {
330                break;
331            }
332            if collapsed_bytes >= self.max_expansion_column {
333                break;
334            }
335
336            if c == '\t' {
337                let tab_len = tab_size - (expanded_chars % tab_size);
338                expanded_chars += tab_len;
339                expanded_bytes += tab_len;
340                if expanded_bytes > column {
341                    expanded_chars -= expanded_bytes - column;
342                    return match bias {
343                        Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column),
344                        Bias::Right => (collapsed_bytes + 1, expanded_chars, 0),
345                    };
346                }
347            } else {
348                expanded_chars += 1;
349                expanded_bytes += c.len_utf8() as u32;
350            }
351
352            if expanded_bytes > column && matches!(bias, Bias::Left) {
353                expanded_chars -= 1;
354                break;
355            }
356
357            collapsed_bytes += c.len_utf8() as u32;
358        }
359        (
360            collapsed_bytes + column.saturating_sub(expanded_bytes),
361            expanded_chars,
362            0,
363        )
364    }
365}
366
367#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
368pub struct TabPoint(pub Point);
369
370impl TabPoint {
371    pub fn new(row: u32, column: u32) -> Self {
372        Self(Point::new(row, column))
373    }
374
375    pub fn zero() -> Self {
376        Self::new(0, 0)
377    }
378
379    pub fn row(self) -> u32 {
380        self.0.row
381    }
382
383    pub fn column(self) -> u32 {
384        self.0.column
385    }
386}
387
388impl From<Point> for TabPoint {
389    fn from(point: Point) -> Self {
390        Self(point)
391    }
392}
393
394pub type TabEdit = text::Edit<TabPoint>;
395
396#[derive(Clone, Debug, Default, Eq, PartialEq)]
397pub struct TextSummary {
398    pub lines: Point,
399    pub first_line_chars: u32,
400    pub last_line_chars: u32,
401    pub longest_row: u32,
402    pub longest_row_chars: u32,
403}
404
405impl<'a> From<&'a str> for TextSummary {
406    fn from(text: &'a str) -> Self {
407        let sum = text::TextSummary::from(text);
408
409        TextSummary {
410            lines: sum.lines,
411            first_line_chars: sum.first_line_chars,
412            last_line_chars: sum.last_line_chars,
413            longest_row: sum.longest_row,
414            longest_row_chars: sum.longest_row_chars,
415        }
416    }
417}
418
419impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
420    fn add_assign(&mut self, other: &'a Self) {
421        let joined_chars = self.last_line_chars + other.first_line_chars;
422        if joined_chars > self.longest_row_chars {
423            self.longest_row = self.lines.row;
424            self.longest_row_chars = joined_chars;
425        }
426        if other.longest_row_chars > self.longest_row_chars {
427            self.longest_row = self.lines.row + other.longest_row;
428            self.longest_row_chars = other.longest_row_chars;
429        }
430
431        if self.lines.row == 0 {
432            self.first_line_chars += other.first_line_chars;
433        }
434
435        if other.lines.row == 0 {
436            self.last_line_chars += other.first_line_chars;
437        } else {
438            self.last_line_chars = other.last_line_chars;
439        }
440
441        self.lines += &other.lines;
442    }
443}
444
445// Handles a tab width <= 16
446const SPACES: &str = "                ";
447
448pub struct TabChunks<'a> {
449    suggestion_chunks: SuggestionChunks<'a>,
450    chunk: Chunk<'a>,
451    column: u32,
452    max_expansion_column: u32,
453    output_position: Point,
454    input_column: u32,
455    max_output_position: Point,
456    tab_size: NonZeroU32,
457    skip_leading_tab: bool,
458}
459
460impl<'a> Iterator for TabChunks<'a> {
461    type Item = Chunk<'a>;
462
463    fn next(&mut self) -> Option<Self::Item> {
464        if self.chunk.text.is_empty() {
465            if let Some(chunk) = self.suggestion_chunks.next() {
466                self.chunk = chunk;
467                if self.skip_leading_tab {
468                    self.chunk.text = &self.chunk.text[1..];
469                    self.skip_leading_tab = false;
470                }
471            } else {
472                return None;
473            }
474        }
475
476        for (ix, c) in self.chunk.text.char_indices() {
477            match c {
478                '\t' => {
479                    if ix > 0 {
480                        let (prefix, suffix) = self.chunk.text.split_at(ix);
481                        self.chunk.text = suffix;
482                        return Some(Chunk {
483                            text: prefix,
484                            ..self.chunk
485                        });
486                    } else {
487                        self.chunk.text = &self.chunk.text[1..];
488                        let tab_size = if self.input_column < self.max_expansion_column {
489                            self.tab_size.get() as u32
490                        } else {
491                            1
492                        };
493                        let mut len = tab_size - self.column % tab_size;
494                        let next_output_position = cmp::min(
495                            self.output_position + Point::new(0, len),
496                            self.max_output_position,
497                        );
498                        len = next_output_position.column - self.output_position.column;
499                        self.column += len;
500                        self.input_column += 1;
501                        self.output_position = next_output_position;
502                        return Some(Chunk {
503                            text: &SPACES[0..len as usize],
504                            ..self.chunk
505                        });
506                    }
507                }
508                '\n' => {
509                    self.column = 0;
510                    self.input_column = 0;
511                    self.output_position += Point::new(1, 0);
512                }
513                _ => {
514                    self.column += 1;
515                    self.input_column += c.len_utf8() as u32;
516                    self.output_position.column += c.len_utf8() as u32;
517                }
518            }
519        }
520
521        Some(mem::take(&mut self.chunk))
522    }
523}
524
525#[cfg(test)]
526mod tests {
527    use super::*;
528    use crate::{
529        display_map::{fold_map::FoldMap, suggestion_map::SuggestionMap},
530        MultiBuffer,
531    };
532    use rand::{prelude::StdRng, Rng};
533
534    #[gpui::test]
535    fn test_expand_tabs(cx: &mut gpui::MutableAppContext) {
536        let buffer = MultiBuffer::build_simple("", cx);
537        let buffer_snapshot = buffer.read(cx).snapshot(cx);
538        let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
539        let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
540        let (_, tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
541
542        assert_eq!(tab_snapshot.expand_tabs("\t".chars(), 0), 0);
543        assert_eq!(tab_snapshot.expand_tabs("\t".chars(), 1), 4);
544        assert_eq!(tab_snapshot.expand_tabs("\ta".chars(), 2), 5);
545    }
546
547    #[gpui::test]
548    fn test_long_lines(cx: &mut gpui::MutableAppContext) {
549        let max_expansion_column = 12;
550        let input = "A\tBC\tDEF\tG\tHI\tJ\tK\tL\tM";
551        let output = "A   BC  DEF G   HI J K L M";
552
553        let buffer = MultiBuffer::build_simple(input, cx);
554        let buffer_snapshot = buffer.read(cx).snapshot(cx);
555        let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
556        let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
557        let (_, mut tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
558
559        tab_snapshot.max_expansion_column = max_expansion_column;
560        assert_eq!(tab_snapshot.text(), output);
561
562        for (ix, c) in input.char_indices() {
563            assert_eq!(
564                tab_snapshot
565                    .chunks(
566                        TabPoint::new(0, ix as u32)..tab_snapshot.max_point(),
567                        false,
568                        None
569                    )
570                    .map(|c| c.text)
571                    .collect::<String>(),
572                &output[ix..],
573                "text from index {ix}"
574            );
575
576            if c != '\t' {
577                let input_point = Point::new(0, ix as u32);
578                let output_point = Point::new(0, output.find(c).unwrap() as u32);
579                assert_eq!(
580                    tab_snapshot.to_tab_point(SuggestionPoint(input_point)),
581                    TabPoint(output_point),
582                    "to_tab_point({input_point:?})"
583                );
584                assert_eq!(
585                    tab_snapshot
586                        .to_suggestion_point(TabPoint(output_point), Bias::Left)
587                        .0,
588                    SuggestionPoint(input_point),
589                    "to_suggestion_point({output_point:?})"
590                );
591            }
592        }
593    }
594
595    #[gpui::test]
596    fn test_long_lines_with_character_spanning_max_expansion_column(
597        cx: &mut gpui::MutableAppContext,
598    ) {
599        let max_expansion_column = 8;
600        let input = "abcdefg⋯hij";
601
602        let buffer = MultiBuffer::build_simple(input, cx);
603        let buffer_snapshot = buffer.read(cx).snapshot(cx);
604        let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
605        let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
606        let (_, mut tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
607
608        tab_snapshot.max_expansion_column = max_expansion_column;
609        assert_eq!(tab_snapshot.text(), input);
610    }
611
612    #[gpui::test(iterations = 100)]
613    fn test_random_tabs(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
614        let tab_size = NonZeroU32::new(rng.gen_range(1..=4)).unwrap();
615        let len = rng.gen_range(0..30);
616        let buffer = if rng.gen() {
617            let text = util::RandomCharIter::new(&mut rng)
618                .take(len)
619                .collect::<String>();
620            MultiBuffer::build_simple(&text, cx)
621        } else {
622            MultiBuffer::build_random(&mut rng, cx)
623        };
624        let buffer_snapshot = buffer.read(cx).snapshot(cx);
625        log::info!("Buffer text: {:?}", buffer_snapshot.text());
626
627        let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
628        fold_map.randomly_mutate(&mut rng);
629        let (fold_snapshot, _) = fold_map.read(buffer_snapshot, vec![]);
630        log::info!("FoldMap text: {:?}", fold_snapshot.text());
631        let (suggestion_map, _) = SuggestionMap::new(fold_snapshot);
632        let (suggestion_snapshot, _) = suggestion_map.randomly_mutate(&mut rng);
633        log::info!("SuggestionMap text: {:?}", suggestion_snapshot.text());
634
635        let (tab_map, _) = TabMap::new(suggestion_snapshot.clone(), tab_size);
636        let tabs_snapshot = tab_map.set_max_expansion_column(32);
637
638        let text = text::Rope::from(tabs_snapshot.text().as_str());
639        log::info!(
640            "TabMap text (tab size: {}): {:?}",
641            tab_size,
642            tabs_snapshot.text(),
643        );
644
645        for _ in 0..5 {
646            let end_row = rng.gen_range(0..=text.max_point().row);
647            let end_column = rng.gen_range(0..=text.line_len(end_row));
648            let mut end = TabPoint(text.clip_point(Point::new(end_row, end_column), Bias::Right));
649            let start_row = rng.gen_range(0..=text.max_point().row);
650            let start_column = rng.gen_range(0..=text.line_len(start_row));
651            let mut start =
652                TabPoint(text.clip_point(Point::new(start_row, start_column), Bias::Left));
653            if start > end {
654                mem::swap(&mut start, &mut end);
655            }
656
657            let expected_text = text
658                .chunks_in_range(text.point_to_offset(start.0)..text.point_to_offset(end.0))
659                .collect::<String>();
660            let expected_summary = TextSummary::from(expected_text.as_str());
661            assert_eq!(
662                expected_text,
663                tabs_snapshot
664                    .chunks(start..end, false, None)
665                    .map(|c| c.text)
666                    .collect::<String>(),
667                "chunks({:?}..{:?})",
668                start,
669                end
670            );
671
672            let mut actual_summary = tabs_snapshot.text_summary_for_range(start..end);
673            if tab_size.get() > 1 && suggestion_snapshot.text().contains('\t') {
674                actual_summary.longest_row = expected_summary.longest_row;
675                actual_summary.longest_row_chars = expected_summary.longest_row_chars;
676            }
677            assert_eq!(actual_summary, expected_summary);
678        }
679
680        for row in 0..=text.max_point().row {
681            assert_eq!(
682                tabs_snapshot.line_len(row),
683                text.line_len(row),
684                "line_len({row})"
685            );
686        }
687    }
688}