tab_map.rs

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