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