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