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