tab_map.rs

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