char_map.rs

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