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