1use super::{
2 fold_map::FoldBufferRows,
3 tab_map::{self, TabEdit, TabPoint, TabSnapshot},
4 TextHighlights,
5};
6use crate::MultiBufferSnapshot;
7use gpui::{
8 fonts::{FontId, HighlightStyle},
9 text_layout::LineWrapper,
10 AppContext, Entity, ModelContext, ModelHandle, Task,
11};
12use language::{Chunk, Point};
13use lazy_static::lazy_static;
14use smol::future::yield_now;
15use std::{cmp, collections::VecDeque, mem, ops::Range, time::Duration};
16use sum_tree::{Bias, Cursor, SumTree};
17use text::Patch;
18
19pub use super::tab_map::TextSummary;
20pub type WrapEdit = text::Edit<u32>;
21
22pub struct WrapMap {
23 snapshot: WrapSnapshot,
24 pending_edits: VecDeque<(TabSnapshot, Vec<TabEdit>)>,
25 interpolated_edits: Patch<u32>,
26 edits_since_sync: Patch<u32>,
27 wrap_width: Option<f32>,
28 background_task: Option<Task<()>>,
29 font: (FontId, f32),
30}
31
32impl Entity for WrapMap {
33 type Event = ();
34}
35
36#[derive(Clone)]
37pub struct WrapSnapshot {
38 tab_snapshot: TabSnapshot,
39 transforms: SumTree<Transform>,
40 interpolated: bool,
41}
42
43#[derive(Clone, Debug, Default, Eq, PartialEq)]
44struct Transform {
45 summary: TransformSummary,
46 display_text: Option<&'static str>,
47}
48
49#[derive(Clone, Debug, Default, Eq, PartialEq)]
50struct TransformSummary {
51 input: TextSummary,
52 output: TextSummary,
53}
54
55#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
56pub struct WrapPoint(pub Point);
57
58pub struct WrapChunks<'a> {
59 input_chunks: tab_map::TabChunks<'a>,
60 input_chunk: Chunk<'a>,
61 output_position: WrapPoint,
62 max_output_row: u32,
63 transforms: Cursor<'a, Transform, (WrapPoint, TabPoint)>,
64}
65
66#[derive(Clone)]
67pub struct WrapBufferRows<'a> {
68 input_buffer_rows: FoldBufferRows<'a>,
69 input_buffer_row: Option<u32>,
70 output_row: u32,
71 soft_wrapped: bool,
72 max_output_row: u32,
73 transforms: Cursor<'a, Transform, (WrapPoint, TabPoint)>,
74}
75
76impl WrapMap {
77 pub fn new(
78 tab_snapshot: TabSnapshot,
79 font_id: FontId,
80 font_size: f32,
81 wrap_width: Option<f32>,
82 cx: &mut AppContext,
83 ) -> (ModelHandle<Self>, WrapSnapshot) {
84 let handle = cx.add_model(|cx| {
85 let mut this = Self {
86 font: (font_id, font_size),
87 wrap_width: None,
88 pending_edits: Default::default(),
89 interpolated_edits: Default::default(),
90 edits_since_sync: Default::default(),
91 snapshot: WrapSnapshot::new(tab_snapshot),
92 background_task: None,
93 };
94 this.set_wrap_width(wrap_width, cx);
95 mem::take(&mut this.edits_since_sync);
96 this
97 });
98 let snapshot = handle.read(cx).snapshot.clone();
99 (handle, snapshot)
100 }
101
102 #[cfg(test)]
103 pub fn is_rewrapping(&self) -> bool {
104 self.background_task.is_some()
105 }
106
107 pub fn sync(
108 &mut self,
109 tab_snapshot: TabSnapshot,
110 edits: Vec<TabEdit>,
111 cx: &mut ModelContext<Self>,
112 ) -> (WrapSnapshot, Patch<u32>) {
113 if self.wrap_width.is_some() {
114 self.pending_edits.push_back((tab_snapshot, edits));
115 self.flush_edits(cx);
116 } else {
117 self.edits_since_sync = self
118 .edits_since_sync
119 .compose(&self.snapshot.interpolate(tab_snapshot, &edits));
120 self.snapshot.interpolated = false;
121 }
122
123 (self.snapshot.clone(), mem::take(&mut self.edits_since_sync))
124 }
125
126 pub fn set_font(
127 &mut self,
128 font_id: FontId,
129 font_size: f32,
130 cx: &mut ModelContext<Self>,
131 ) -> bool {
132 if (font_id, font_size) != self.font {
133 self.font = (font_id, font_size);
134 self.rewrap(cx);
135 true
136 } else {
137 false
138 }
139 }
140
141 pub fn set_wrap_width(&mut self, wrap_width: Option<f32>, cx: &mut ModelContext<Self>) -> bool {
142 if wrap_width == self.wrap_width {
143 return false;
144 }
145
146 self.wrap_width = wrap_width;
147 self.rewrap(cx);
148 true
149 }
150
151 fn rewrap(&mut self, cx: &mut ModelContext<Self>) {
152 self.background_task.take();
153 self.interpolated_edits.clear();
154 self.pending_edits.clear();
155
156 if let Some(wrap_width) = self.wrap_width {
157 let mut new_snapshot = self.snapshot.clone();
158 let font_cache = cx.font_cache().clone();
159 let (font_id, font_size) = self.font;
160 let task = cx.background().spawn(async move {
161 let mut line_wrapper = font_cache.line_wrapper(font_id, font_size);
162 let tab_snapshot = new_snapshot.tab_snapshot.clone();
163 let range = TabPoint::zero()..tab_snapshot.max_point();
164 let edits = new_snapshot
165 .update(
166 tab_snapshot,
167 &[TabEdit {
168 old: range.clone(),
169 new: range.clone(),
170 }],
171 wrap_width,
172 &mut line_wrapper,
173 )
174 .await;
175 (new_snapshot, edits)
176 });
177
178 match cx
179 .background()
180 .block_with_timeout(Duration::from_millis(5), task)
181 {
182 Ok((snapshot, edits)) => {
183 self.snapshot = snapshot;
184 self.edits_since_sync = self.edits_since_sync.compose(&edits);
185 cx.notify();
186 }
187 Err(wrap_task) => {
188 self.background_task = Some(cx.spawn(|this, mut cx| async move {
189 let (snapshot, edits) = wrap_task.await;
190 this.update(&mut cx, |this, cx| {
191 this.snapshot = snapshot;
192 this.edits_since_sync = this
193 .edits_since_sync
194 .compose(mem::take(&mut this.interpolated_edits).invert())
195 .compose(&edits);
196 this.background_task = None;
197 this.flush_edits(cx);
198 cx.notify();
199 });
200 }));
201 }
202 }
203 } else {
204 let old_rows = self.snapshot.transforms.summary().output.lines.row + 1;
205 self.snapshot.transforms = SumTree::new();
206 let summary = self.snapshot.tab_snapshot.text_summary();
207 if !summary.lines.is_zero() {
208 self.snapshot
209 .transforms
210 .push(Transform::isomorphic(summary), &());
211 }
212 let new_rows = self.snapshot.transforms.summary().output.lines.row + 1;
213 self.snapshot.interpolated = false;
214 self.edits_since_sync = self.edits_since_sync.compose(&Patch::new(vec![WrapEdit {
215 old: 0..old_rows,
216 new: 0..new_rows,
217 }]));
218 }
219 }
220
221 fn flush_edits(&mut self, cx: &mut ModelContext<Self>) {
222 if !self.snapshot.interpolated {
223 let mut to_remove_len = 0;
224 for (tab_snapshot, _) in &self.pending_edits {
225 if tab_snapshot.version <= self.snapshot.tab_snapshot.version {
226 to_remove_len += 1;
227 } else {
228 break;
229 }
230 }
231 self.pending_edits.drain(..to_remove_len);
232 }
233
234 if self.pending_edits.is_empty() {
235 return;
236 }
237
238 if let Some(wrap_width) = self.wrap_width {
239 if self.background_task.is_none() {
240 let pending_edits = self.pending_edits.clone();
241 let mut snapshot = self.snapshot.clone();
242 let font_cache = cx.font_cache().clone();
243 let (font_id, font_size) = self.font;
244 let update_task = cx.background().spawn(async move {
245 let mut line_wrapper = font_cache.line_wrapper(font_id, font_size);
246
247 let mut edits = Patch::default();
248 for (tab_snapshot, tab_edits) in pending_edits {
249 let wrap_edits = snapshot
250 .update(tab_snapshot, &tab_edits, wrap_width, &mut line_wrapper)
251 .await;
252 edits = edits.compose(&wrap_edits);
253 }
254 (snapshot, edits)
255 });
256
257 match cx
258 .background()
259 .block_with_timeout(Duration::from_millis(1), update_task)
260 {
261 Ok((snapshot, output_edits)) => {
262 self.snapshot = snapshot;
263 self.edits_since_sync = self.edits_since_sync.compose(&output_edits);
264 }
265 Err(update_task) => {
266 self.background_task = Some(cx.spawn(|this, mut cx| async move {
267 let (snapshot, edits) = update_task.await;
268 this.update(&mut cx, |this, cx| {
269 this.snapshot = snapshot;
270 this.edits_since_sync = this
271 .edits_since_sync
272 .compose(mem::take(&mut this.interpolated_edits).invert())
273 .compose(&edits);
274 this.background_task = None;
275 this.flush_edits(cx);
276 cx.notify();
277 });
278 }));
279 }
280 }
281 }
282 }
283
284 let was_interpolated = self.snapshot.interpolated;
285 let mut to_remove_len = 0;
286 for (tab_snapshot, edits) in &self.pending_edits {
287 if tab_snapshot.version <= self.snapshot.tab_snapshot.version {
288 to_remove_len += 1;
289 } else {
290 let interpolated_edits = self.snapshot.interpolate(tab_snapshot.clone(), edits);
291 self.edits_since_sync = self.edits_since_sync.compose(&interpolated_edits);
292 self.interpolated_edits = self.interpolated_edits.compose(&interpolated_edits);
293 }
294 }
295
296 if !was_interpolated {
297 self.pending_edits.drain(..to_remove_len);
298 }
299 }
300}
301
302impl WrapSnapshot {
303 fn new(tab_snapshot: TabSnapshot) -> Self {
304 let mut transforms = SumTree::new();
305 let extent = tab_snapshot.text_summary();
306 if !extent.lines.is_zero() {
307 transforms.push(Transform::isomorphic(extent), &());
308 }
309 Self {
310 transforms,
311 tab_snapshot,
312 interpolated: true,
313 }
314 }
315
316 pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
317 self.tab_snapshot.buffer_snapshot()
318 }
319
320 fn interpolate(&mut self, new_tab_snapshot: TabSnapshot, tab_edits: &[TabEdit]) -> Patch<u32> {
321 let mut new_transforms;
322 if tab_edits.is_empty() {
323 new_transforms = self.transforms.clone();
324 } else {
325 let mut old_cursor = self.transforms.cursor::<TabPoint>();
326
327 let mut tab_edits_iter = tab_edits.iter().peekable();
328 new_transforms =
329 old_cursor.slice(&tab_edits_iter.peek().unwrap().old.start, Bias::Right, &());
330
331 while let Some(edit) = tab_edits_iter.next() {
332 if edit.new.start > TabPoint::from(new_transforms.summary().input.lines) {
333 let summary = new_tab_snapshot.text_summary_for_range(
334 TabPoint::from(new_transforms.summary().input.lines)..edit.new.start,
335 );
336 new_transforms.push_or_extend(Transform::isomorphic(summary));
337 }
338
339 if !edit.new.is_empty() {
340 new_transforms.push_or_extend(Transform::isomorphic(
341 new_tab_snapshot.text_summary_for_range(edit.new.clone()),
342 ));
343 }
344
345 old_cursor.seek_forward(&edit.old.end, Bias::Right, &());
346 if let Some(next_edit) = tab_edits_iter.peek() {
347 if next_edit.old.start > old_cursor.end(&()) {
348 if old_cursor.end(&()) > edit.old.end {
349 let summary = self
350 .tab_snapshot
351 .text_summary_for_range(edit.old.end..old_cursor.end(&()));
352 new_transforms.push_or_extend(Transform::isomorphic(summary));
353 }
354
355 old_cursor.next(&());
356 new_transforms.append(
357 old_cursor.slice(&next_edit.old.start, Bias::Right, &()),
358 &(),
359 );
360 }
361 } else {
362 if old_cursor.end(&()) > edit.old.end {
363 let summary = self
364 .tab_snapshot
365 .text_summary_for_range(edit.old.end..old_cursor.end(&()));
366 new_transforms.push_or_extend(Transform::isomorphic(summary));
367 }
368 old_cursor.next(&());
369 new_transforms.append(old_cursor.suffix(&()), &());
370 }
371 }
372 }
373
374 let old_snapshot = mem::replace(
375 self,
376 WrapSnapshot {
377 tab_snapshot: new_tab_snapshot,
378 transforms: new_transforms,
379 interpolated: true,
380 },
381 );
382 self.check_invariants();
383 old_snapshot.compute_edits(tab_edits, self)
384 }
385
386 async fn update(
387 &mut self,
388 new_tab_snapshot: TabSnapshot,
389 tab_edits: &[TabEdit],
390 wrap_width: f32,
391 line_wrapper: &mut LineWrapper,
392 ) -> Patch<u32> {
393 #[derive(Debug)]
394 struct RowEdit {
395 old_rows: Range<u32>,
396 new_rows: Range<u32>,
397 }
398
399 let mut tab_edits_iter = tab_edits.iter().peekable();
400 let mut row_edits = Vec::new();
401 while let Some(edit) = tab_edits_iter.next() {
402 let mut row_edit = RowEdit {
403 old_rows: edit.old.start.row()..edit.old.end.row() + 1,
404 new_rows: edit.new.start.row()..edit.new.end.row() + 1,
405 };
406
407 while let Some(next_edit) = tab_edits_iter.peek() {
408 if next_edit.old.start.row() <= row_edit.old_rows.end {
409 row_edit.old_rows.end = next_edit.old.end.row() + 1;
410 row_edit.new_rows.end = next_edit.new.end.row() + 1;
411 tab_edits_iter.next();
412 } else {
413 break;
414 }
415 }
416
417 row_edits.push(row_edit);
418 }
419
420 let mut new_transforms;
421 if row_edits.is_empty() {
422 new_transforms = self.transforms.clone();
423 } else {
424 let mut row_edits = row_edits.into_iter().peekable();
425 let mut old_cursor = self.transforms.cursor::<TabPoint>();
426
427 new_transforms = old_cursor.slice(
428 &TabPoint::new(row_edits.peek().unwrap().old_rows.start, 0),
429 Bias::Right,
430 &(),
431 );
432
433 while let Some(edit) = row_edits.next() {
434 if edit.new_rows.start > new_transforms.summary().input.lines.row {
435 let summary = new_tab_snapshot.text_summary_for_range(
436 TabPoint(new_transforms.summary().input.lines)
437 ..TabPoint::new(edit.new_rows.start, 0),
438 );
439 new_transforms.push_or_extend(Transform::isomorphic(summary));
440 }
441
442 let mut line = String::new();
443 let mut remaining = None;
444 let mut chunks = new_tab_snapshot.chunks(
445 TabPoint::new(edit.new_rows.start, 0)..new_tab_snapshot.max_point(),
446 false,
447 None,
448 None,
449 None,
450 );
451 let mut edit_transforms = Vec::<Transform>::new();
452 for _ in edit.new_rows.start..edit.new_rows.end {
453 while let Some(chunk) =
454 remaining.take().or_else(|| chunks.next().map(|c| c.text))
455 {
456 if let Some(ix) = chunk.find('\n') {
457 line.push_str(&chunk[..ix + 1]);
458 remaining = Some(&chunk[ix + 1..]);
459 break;
460 } else {
461 line.push_str(chunk)
462 }
463 }
464
465 if line.is_empty() {
466 break;
467 }
468
469 let mut prev_boundary_ix = 0;
470 for boundary in line_wrapper.wrap_line(&line, wrap_width) {
471 let wrapped = &line[prev_boundary_ix..boundary.ix];
472 push_isomorphic(&mut edit_transforms, TextSummary::from(wrapped));
473 edit_transforms.push(Transform::wrap(boundary.next_indent));
474 prev_boundary_ix = boundary.ix;
475 }
476
477 if prev_boundary_ix < line.len() {
478 push_isomorphic(
479 &mut edit_transforms,
480 TextSummary::from(&line[prev_boundary_ix..]),
481 );
482 }
483
484 line.clear();
485 yield_now().await;
486 }
487
488 let mut edit_transforms = edit_transforms.into_iter();
489 if let Some(transform) = edit_transforms.next() {
490 new_transforms.push_or_extend(transform);
491 }
492 new_transforms.extend(edit_transforms, &());
493
494 old_cursor.seek_forward(&TabPoint::new(edit.old_rows.end, 0), Bias::Right, &());
495 if let Some(next_edit) = row_edits.peek() {
496 if next_edit.old_rows.start > old_cursor.end(&()).row() {
497 if old_cursor.end(&()) > TabPoint::new(edit.old_rows.end, 0) {
498 let summary = self.tab_snapshot.text_summary_for_range(
499 TabPoint::new(edit.old_rows.end, 0)..old_cursor.end(&()),
500 );
501 new_transforms.push_or_extend(Transform::isomorphic(summary));
502 }
503 old_cursor.next(&());
504 new_transforms.append(
505 old_cursor.slice(
506 &TabPoint::new(next_edit.old_rows.start, 0),
507 Bias::Right,
508 &(),
509 ),
510 &(),
511 );
512 }
513 } else {
514 if old_cursor.end(&()) > TabPoint::new(edit.old_rows.end, 0) {
515 let summary = self.tab_snapshot.text_summary_for_range(
516 TabPoint::new(edit.old_rows.end, 0)..old_cursor.end(&()),
517 );
518 new_transforms.push_or_extend(Transform::isomorphic(summary));
519 }
520 old_cursor.next(&());
521 new_transforms.append(old_cursor.suffix(&()), &());
522 }
523 }
524 }
525
526 let old_snapshot = mem::replace(
527 self,
528 WrapSnapshot {
529 tab_snapshot: new_tab_snapshot,
530 transforms: new_transforms,
531 interpolated: false,
532 },
533 );
534 self.check_invariants();
535 old_snapshot.compute_edits(tab_edits, self)
536 }
537
538 fn compute_edits(&self, tab_edits: &[TabEdit], new_snapshot: &WrapSnapshot) -> Patch<u32> {
539 let mut wrap_edits = Vec::new();
540 let mut old_cursor = self.transforms.cursor::<TransformSummary>();
541 let mut new_cursor = new_snapshot.transforms.cursor::<TransformSummary>();
542 for mut tab_edit in tab_edits.iter().cloned() {
543 tab_edit.old.start.0.column = 0;
544 tab_edit.old.end.0 += Point::new(1, 0);
545 tab_edit.new.start.0.column = 0;
546 tab_edit.new.end.0 += Point::new(1, 0);
547
548 old_cursor.seek(&tab_edit.old.start, Bias::Right, &());
549 let mut old_start = old_cursor.start().output.lines;
550 old_start += tab_edit.old.start.0 - old_cursor.start().input.lines;
551
552 old_cursor.seek(&tab_edit.old.end, Bias::Right, &());
553 let mut old_end = old_cursor.start().output.lines;
554 old_end += tab_edit.old.end.0 - old_cursor.start().input.lines;
555
556 new_cursor.seek(&tab_edit.new.start, Bias::Right, &());
557 let mut new_start = new_cursor.start().output.lines;
558 new_start += tab_edit.new.start.0 - new_cursor.start().input.lines;
559
560 new_cursor.seek(&tab_edit.new.end, Bias::Right, &());
561 let mut new_end = new_cursor.start().output.lines;
562 new_end += tab_edit.new.end.0 - new_cursor.start().input.lines;
563
564 wrap_edits.push(WrapEdit {
565 old: old_start.row..old_end.row,
566 new: new_start.row..new_end.row,
567 });
568 }
569
570 consolidate_wrap_edits(&mut wrap_edits);
571 Patch::new(wrap_edits)
572 }
573
574 pub fn chunks<'a>(
575 &'a self,
576 rows: Range<u32>,
577 language_aware: bool,
578 text_highlights: Option<&'a TextHighlights>,
579 hint_highlights: Option<HighlightStyle>,
580 suggestion_highlights: Option<HighlightStyle>,
581 ) -> WrapChunks<'a> {
582 let output_start = WrapPoint::new(rows.start, 0);
583 let output_end = WrapPoint::new(rows.end, 0);
584 let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
585 transforms.seek(&output_start, Bias::Right, &());
586 let mut input_start = TabPoint(transforms.start().1 .0);
587 if transforms.item().map_or(false, |t| t.is_isomorphic()) {
588 input_start.0 += output_start.0 - transforms.start().0 .0;
589 }
590 let input_end = self
591 .to_tab_point(output_end)
592 .min(self.tab_snapshot.max_point());
593 WrapChunks {
594 input_chunks: self.tab_snapshot.chunks(
595 input_start..input_end,
596 language_aware,
597 text_highlights,
598 hint_highlights,
599 suggestion_highlights,
600 ),
601 input_chunk: Default::default(),
602 output_position: output_start,
603 max_output_row: rows.end,
604 transforms,
605 }
606 }
607
608 pub fn max_point(&self) -> WrapPoint {
609 WrapPoint(self.transforms.summary().output.lines)
610 }
611
612 pub fn line_len(&self, row: u32) -> u32 {
613 let mut cursor = self.transforms.cursor::<(WrapPoint, TabPoint)>();
614 cursor.seek(&WrapPoint::new(row + 1, 0), Bias::Left, &());
615 if cursor
616 .item()
617 .map_or(false, |transform| transform.is_isomorphic())
618 {
619 let overshoot = row - cursor.start().0.row();
620 let tab_row = cursor.start().1.row() + overshoot;
621 let tab_line_len = self.tab_snapshot.line_len(tab_row);
622 if overshoot == 0 {
623 cursor.start().0.column() + (tab_line_len - cursor.start().1.column())
624 } else {
625 tab_line_len
626 }
627 } else {
628 cursor.start().0.column()
629 }
630 }
631
632 pub fn soft_wrap_indent(&self, row: u32) -> Option<u32> {
633 let mut cursor = self.transforms.cursor::<WrapPoint>();
634 cursor.seek(&WrapPoint::new(row + 1, 0), Bias::Right, &());
635 cursor.item().and_then(|transform| {
636 if transform.is_isomorphic() {
637 None
638 } else {
639 Some(transform.summary.output.lines.column)
640 }
641 })
642 }
643
644 pub fn longest_row(&self) -> u32 {
645 self.transforms.summary().output.longest_row
646 }
647
648 pub fn buffer_rows(&self, start_row: u32) -> WrapBufferRows {
649 let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
650 transforms.seek(&WrapPoint::new(start_row, 0), Bias::Left, &());
651 let mut input_row = transforms.start().1.row();
652 if transforms.item().map_or(false, |t| t.is_isomorphic()) {
653 input_row += start_row - transforms.start().0.row();
654 }
655 let soft_wrapped = transforms.item().map_or(false, |t| !t.is_isomorphic());
656 let mut input_buffer_rows = self.tab_snapshot.buffer_rows(input_row);
657 let input_buffer_row = input_buffer_rows.next().unwrap();
658 WrapBufferRows {
659 transforms,
660 input_buffer_row,
661 input_buffer_rows,
662 output_row: start_row,
663 soft_wrapped,
664 max_output_row: self.max_point().row(),
665 }
666 }
667
668 pub fn to_tab_point(&self, point: WrapPoint) -> TabPoint {
669 let mut cursor = self.transforms.cursor::<(WrapPoint, TabPoint)>();
670 cursor.seek(&point, Bias::Right, &());
671 let mut tab_point = cursor.start().1 .0;
672 if cursor.item().map_or(false, |t| t.is_isomorphic()) {
673 tab_point += point.0 - cursor.start().0 .0;
674 }
675 TabPoint(tab_point)
676 }
677
678 pub fn to_point(&self, point: WrapPoint, bias: Bias) -> Point {
679 self.tab_snapshot.to_point(self.to_tab_point(point), bias)
680 }
681
682 pub fn make_wrap_point(&self, point: Point, bias: Bias) -> WrapPoint {
683 self.tab_point_to_wrap_point(self.tab_snapshot.make_tab_point(point, bias))
684 }
685
686 pub fn tab_point_to_wrap_point(&self, point: TabPoint) -> WrapPoint {
687 let mut cursor = self.transforms.cursor::<(TabPoint, WrapPoint)>();
688 cursor.seek(&point, Bias::Right, &());
689 WrapPoint(cursor.start().1 .0 + (point.0 - cursor.start().0 .0))
690 }
691
692 pub fn clip_point(&self, mut point: WrapPoint, bias: Bias) -> WrapPoint {
693 if bias == Bias::Left {
694 let mut cursor = self.transforms.cursor::<WrapPoint>();
695 cursor.seek(&point, Bias::Right, &());
696 if cursor.item().map_or(false, |t| !t.is_isomorphic()) {
697 point = *cursor.start();
698 *point.column_mut() -= 1;
699 }
700 }
701
702 self.tab_point_to_wrap_point(self.tab_snapshot.clip_point(self.to_tab_point(point), bias))
703 }
704
705 pub fn prev_row_boundary(&self, mut point: WrapPoint) -> u32 {
706 if self.transforms.is_empty() {
707 return 0;
708 }
709
710 *point.column_mut() = 0;
711
712 let mut cursor = self.transforms.cursor::<(WrapPoint, TabPoint)>();
713 cursor.seek(&point, Bias::Right, &());
714 if cursor.item().is_none() {
715 cursor.prev(&());
716 }
717
718 while let Some(transform) = cursor.item() {
719 if transform.is_isomorphic() && cursor.start().1.column() == 0 {
720 return cmp::min(cursor.end(&()).0.row(), point.row());
721 } else {
722 cursor.prev(&());
723 }
724 }
725
726 unreachable!()
727 }
728
729 pub fn next_row_boundary(&self, mut point: WrapPoint) -> Option<u32> {
730 point.0 += Point::new(1, 0);
731
732 let mut cursor = self.transforms.cursor::<(WrapPoint, TabPoint)>();
733 cursor.seek(&point, Bias::Right, &());
734 while let Some(transform) = cursor.item() {
735 if transform.is_isomorphic() && cursor.start().1.column() == 0 {
736 return Some(cmp::max(cursor.start().0.row(), point.row()));
737 } else {
738 cursor.next(&());
739 }
740 }
741
742 None
743 }
744
745 fn check_invariants(&self) {
746 #[cfg(test)]
747 {
748 assert_eq!(
749 TabPoint::from(self.transforms.summary().input.lines),
750 self.tab_snapshot.max_point()
751 );
752
753 {
754 let mut transforms = self.transforms.cursor::<()>().peekable();
755 while let Some(transform) = transforms.next() {
756 if let Some(next_transform) = transforms.peek() {
757 assert!(transform.is_isomorphic() != next_transform.is_isomorphic());
758 }
759 }
760 }
761
762 let text = language::Rope::from(self.text().as_str());
763 let input_buffer_rows = self.buffer_snapshot().buffer_rows(0).collect::<Vec<_>>();
764 let mut expected_buffer_rows = Vec::new();
765 let mut prev_fold_row = 0;
766 for display_row in 0..=self.max_point().row() {
767 let tab_point = self.to_tab_point(WrapPoint::new(display_row, 0));
768 let fold_point = self.tab_snapshot.to_fold_point(tab_point, Bias::Left).0;
769 if fold_point.row() == prev_fold_row && display_row != 0 {
770 expected_buffer_rows.push(None);
771 } else {
772 let inlay_point = fold_point.to_inlay_point(&self.tab_snapshot.fold_snapshot);
773 let buffer_point = self
774 .tab_snapshot
775 .fold_snapshot
776 .inlay_snapshot
777 .to_buffer_point(inlay_point);
778 expected_buffer_rows.push(input_buffer_rows[buffer_point.row as usize]);
779 prev_fold_row = fold_point.row();
780 }
781
782 assert_eq!(self.line_len(display_row), text.line_len(display_row));
783 }
784
785 for start_display_row in 0..expected_buffer_rows.len() {
786 assert_eq!(
787 self.buffer_rows(start_display_row as u32)
788 .collect::<Vec<_>>(),
789 &expected_buffer_rows[start_display_row..],
790 "invalid buffer_rows({}..)",
791 start_display_row
792 );
793 }
794 }
795 }
796}
797
798impl<'a> Iterator for WrapChunks<'a> {
799 type Item = Chunk<'a>;
800
801 fn next(&mut self) -> Option<Self::Item> {
802 if self.output_position.row() >= self.max_output_row {
803 return None;
804 }
805
806 let transform = self.transforms.item()?;
807 if let Some(display_text) = transform.display_text {
808 let mut start_ix = 0;
809 let mut end_ix = display_text.len();
810 let mut summary = transform.summary.output.lines;
811
812 if self.output_position > self.transforms.start().0 {
813 // Exclude newline starting prior to the desired row.
814 start_ix = 1;
815 summary.row = 0;
816 } else if self.output_position.row() + 1 >= self.max_output_row {
817 // Exclude soft indentation ending after the desired row.
818 end_ix = 1;
819 summary.column = 0;
820 }
821
822 self.output_position.0 += summary;
823 self.transforms.next(&());
824 return Some(Chunk {
825 text: &display_text[start_ix..end_ix],
826 ..self.input_chunk
827 });
828 }
829
830 if self.input_chunk.text.is_empty() {
831 self.input_chunk = self.input_chunks.next().unwrap();
832 }
833
834 let mut input_len = 0;
835 let transform_end = self.transforms.end(&()).0;
836 for c in self.input_chunk.text.chars() {
837 let char_len = c.len_utf8();
838 input_len += char_len;
839 if c == '\n' {
840 *self.output_position.row_mut() += 1;
841 *self.output_position.column_mut() = 0;
842 } else {
843 *self.output_position.column_mut() += char_len as u32;
844 }
845
846 if self.output_position >= transform_end {
847 self.transforms.next(&());
848 break;
849 }
850 }
851
852 let (prefix, suffix) = self.input_chunk.text.split_at(input_len);
853 self.input_chunk.text = suffix;
854 Some(Chunk {
855 text: prefix,
856 ..self.input_chunk
857 })
858 }
859}
860
861impl<'a> Iterator for WrapBufferRows<'a> {
862 type Item = Option<u32>;
863
864 fn next(&mut self) -> Option<Self::Item> {
865 if self.output_row > self.max_output_row {
866 return None;
867 }
868
869 let buffer_row = self.input_buffer_row;
870 let soft_wrapped = self.soft_wrapped;
871
872 self.output_row += 1;
873 self.transforms
874 .seek_forward(&WrapPoint::new(self.output_row, 0), Bias::Left, &());
875 if self.transforms.item().map_or(false, |t| t.is_isomorphic()) {
876 self.input_buffer_row = self.input_buffer_rows.next().unwrap();
877 self.soft_wrapped = false;
878 } else {
879 self.soft_wrapped = true;
880 }
881
882 Some(if soft_wrapped { None } else { buffer_row })
883 }
884}
885
886impl Transform {
887 fn isomorphic(summary: TextSummary) -> Self {
888 #[cfg(test)]
889 assert!(!summary.lines.is_zero());
890
891 Self {
892 summary: TransformSummary {
893 input: summary.clone(),
894 output: summary,
895 },
896 display_text: None,
897 }
898 }
899
900 fn wrap(indent: u32) -> Self {
901 lazy_static! {
902 static ref WRAP_TEXT: String = {
903 let mut wrap_text = String::new();
904 wrap_text.push('\n');
905 wrap_text.extend((0..LineWrapper::MAX_INDENT as usize).map(|_| ' '));
906 wrap_text
907 };
908 }
909
910 Self {
911 summary: TransformSummary {
912 input: TextSummary::default(),
913 output: TextSummary {
914 lines: Point::new(1, indent),
915 first_line_chars: 0,
916 last_line_chars: indent,
917 longest_row: 1,
918 longest_row_chars: indent,
919 },
920 },
921 display_text: Some(&WRAP_TEXT[..1 + indent as usize]),
922 }
923 }
924
925 fn is_isomorphic(&self) -> bool {
926 self.display_text.is_none()
927 }
928}
929
930impl sum_tree::Item for Transform {
931 type Summary = TransformSummary;
932
933 fn summary(&self) -> Self::Summary {
934 self.summary.clone()
935 }
936}
937
938fn push_isomorphic(transforms: &mut Vec<Transform>, summary: TextSummary) {
939 if let Some(last_transform) = transforms.last_mut() {
940 if last_transform.is_isomorphic() {
941 last_transform.summary.input += &summary;
942 last_transform.summary.output += &summary;
943 return;
944 }
945 }
946 transforms.push(Transform::isomorphic(summary));
947}
948
949trait SumTreeExt {
950 fn push_or_extend(&mut self, transform: Transform);
951}
952
953impl SumTreeExt for SumTree<Transform> {
954 fn push_or_extend(&mut self, transform: Transform) {
955 let mut transform = Some(transform);
956 self.update_last(
957 |last_transform| {
958 if last_transform.is_isomorphic() && transform.as_ref().unwrap().is_isomorphic() {
959 let transform = transform.take().unwrap();
960 last_transform.summary.input += &transform.summary.input;
961 last_transform.summary.output += &transform.summary.output;
962 }
963 },
964 &(),
965 );
966
967 if let Some(transform) = transform {
968 self.push(transform, &());
969 }
970 }
971}
972
973impl WrapPoint {
974 pub fn new(row: u32, column: u32) -> Self {
975 Self(Point::new(row, column))
976 }
977
978 pub fn row(self) -> u32 {
979 self.0.row
980 }
981
982 pub fn row_mut(&mut self) -> &mut u32 {
983 &mut self.0.row
984 }
985
986 pub fn column(self) -> u32 {
987 self.0.column
988 }
989
990 pub fn column_mut(&mut self) -> &mut u32 {
991 &mut self.0.column
992 }
993}
994
995impl sum_tree::Summary for TransformSummary {
996 type Context = ();
997
998 fn add_summary(&mut self, other: &Self, _: &()) {
999 self.input += &other.input;
1000 self.output += &other.output;
1001 }
1002}
1003
1004impl<'a> sum_tree::Dimension<'a, TransformSummary> for TabPoint {
1005 fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
1006 self.0 += summary.input.lines;
1007 }
1008}
1009
1010impl<'a> sum_tree::SeekTarget<'a, TransformSummary, TransformSummary> for TabPoint {
1011 fn cmp(&self, cursor_location: &TransformSummary, _: &()) -> std::cmp::Ordering {
1012 Ord::cmp(&self.0, &cursor_location.input.lines)
1013 }
1014}
1015
1016impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapPoint {
1017 fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
1018 self.0 += summary.output.lines;
1019 }
1020}
1021
1022fn consolidate_wrap_edits(edits: &mut Vec<WrapEdit>) {
1023 let mut i = 1;
1024 while i < edits.len() {
1025 let edit = edits[i].clone();
1026 let prev_edit = &mut edits[i - 1];
1027 if prev_edit.old.end >= edit.old.start {
1028 prev_edit.old.end = edit.old.end;
1029 prev_edit.new.end = edit.new.end;
1030 edits.remove(i);
1031 continue;
1032 }
1033 i += 1;
1034 }
1035}
1036
1037#[cfg(test)]
1038mod tests {
1039 use super::*;
1040 use crate::{
1041 display_map::{fold_map::FoldMap, inlay_map::InlayMap, tab_map::TabMap},
1042 MultiBuffer,
1043 };
1044 use gpui::test::observe;
1045 use rand::prelude::*;
1046 use settings::SettingsStore;
1047 use smol::stream::StreamExt;
1048 use std::{cmp, env, num::NonZeroU32};
1049 use text::Rope;
1050
1051 #[gpui::test(iterations = 100)]
1052 async fn test_random_wraps(cx: &mut gpui::TestAppContext, mut rng: StdRng) {
1053 init_test(cx);
1054
1055 cx.foreground().set_block_on_ticks(0..=50);
1056 let operations = env::var("OPERATIONS")
1057 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1058 .unwrap_or(10);
1059
1060 let font_cache = cx.font_cache().clone();
1061 let font_system = cx.platform().fonts();
1062 let mut wrap_width = if rng.gen_bool(0.1) {
1063 None
1064 } else {
1065 Some(rng.gen_range(0.0..=1000.0))
1066 };
1067 let tab_size = NonZeroU32::new(rng.gen_range(1..=4)).unwrap();
1068 let family_id = font_cache
1069 .load_family(&["Helvetica"], &Default::default())
1070 .unwrap();
1071 let font_id = font_cache
1072 .select_font(family_id, &Default::default())
1073 .unwrap();
1074 let font_size = 14.0;
1075
1076 log::info!("Tab size: {}", tab_size);
1077 log::info!("Wrap width: {:?}", wrap_width);
1078
1079 let buffer = cx.update(|cx| {
1080 if rng.gen() {
1081 MultiBuffer::build_random(&mut rng, cx)
1082 } else {
1083 let len = rng.gen_range(0..10);
1084 let text = util::RandomCharIter::new(&mut rng)
1085 .take(len)
1086 .collect::<String>();
1087 MultiBuffer::build_simple(&text, cx)
1088 }
1089 });
1090 let mut buffer_snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx));
1091 log::info!("Buffer text: {:?}", buffer_snapshot.text());
1092 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
1093 log::info!("InlayMap text: {:?}", inlay_snapshot.text());
1094 let (mut fold_map, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
1095 log::info!("FoldMap text: {:?}", fold_snapshot.text());
1096 let (mut tab_map, _) = TabMap::new(fold_snapshot.clone(), tab_size);
1097 let tabs_snapshot = tab_map.set_max_expansion_column(32);
1098 log::info!("TabMap text: {:?}", tabs_snapshot.text());
1099
1100 let mut line_wrapper = LineWrapper::new(font_id, font_size, font_system);
1101 let unwrapped_text = tabs_snapshot.text();
1102 let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
1103
1104 let (wrap_map, _) =
1105 cx.update(|cx| WrapMap::new(tabs_snapshot.clone(), font_id, font_size, wrap_width, cx));
1106 let mut notifications = observe(&wrap_map, cx);
1107
1108 if wrap_map.read_with(cx, |map, _| map.is_rewrapping()) {
1109 notifications.next().await.unwrap();
1110 }
1111
1112 let (initial_snapshot, _) = wrap_map.update(cx, |map, cx| {
1113 assert!(!map.is_rewrapping());
1114 map.sync(tabs_snapshot.clone(), Vec::new(), cx)
1115 });
1116
1117 let actual_text = initial_snapshot.text();
1118 assert_eq!(
1119 actual_text, expected_text,
1120 "unwrapped text is: {:?}",
1121 unwrapped_text
1122 );
1123 log::info!("Wrapped text: {:?}", actual_text);
1124
1125 let mut next_inlay_id = 0;
1126 let mut edits = Vec::new();
1127 for _i in 0..operations {
1128 log::info!("{} ==============================================", _i);
1129
1130 let mut buffer_edits = Vec::new();
1131 match rng.gen_range(0..=100) {
1132 0..=19 => {
1133 wrap_width = if rng.gen_bool(0.2) {
1134 None
1135 } else {
1136 Some(rng.gen_range(0.0..=1000.0))
1137 };
1138 log::info!("Setting wrap width to {:?}", wrap_width);
1139 wrap_map.update(cx, |map, cx| map.set_wrap_width(wrap_width, cx));
1140 }
1141 20..=39 => {
1142 for (fold_snapshot, fold_edits) in fold_map.randomly_mutate(&mut rng) {
1143 let (tabs_snapshot, tab_edits) =
1144 tab_map.sync(fold_snapshot, fold_edits, tab_size);
1145 let (mut snapshot, wrap_edits) =
1146 wrap_map.update(cx, |map, cx| map.sync(tabs_snapshot, tab_edits, cx));
1147 snapshot.check_invariants();
1148 snapshot.verify_chunks(&mut rng);
1149 edits.push((snapshot, wrap_edits));
1150 }
1151 }
1152 40..=59 => {
1153 let (inlay_snapshot, inlay_edits) =
1154 inlay_map.randomly_mutate(&mut next_inlay_id, &mut rng);
1155 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1156 let (tabs_snapshot, tab_edits) =
1157 tab_map.sync(fold_snapshot, fold_edits, tab_size);
1158 let (mut snapshot, wrap_edits) =
1159 wrap_map.update(cx, |map, cx| map.sync(tabs_snapshot, tab_edits, cx));
1160 snapshot.check_invariants();
1161 snapshot.verify_chunks(&mut rng);
1162 edits.push((snapshot, wrap_edits));
1163 }
1164 _ => {
1165 buffer.update(cx, |buffer, cx| {
1166 let subscription = buffer.subscribe();
1167 let edit_count = rng.gen_range(1..=5);
1168 buffer.randomly_mutate(&mut rng, edit_count, cx);
1169 buffer_snapshot = buffer.snapshot(cx);
1170 buffer_edits.extend(subscription.consume());
1171 });
1172 }
1173 }
1174
1175 log::info!("Buffer text: {:?}", buffer_snapshot.text());
1176 let (inlay_snapshot, inlay_edits) =
1177 inlay_map.sync(buffer_snapshot.clone(), buffer_edits);
1178 log::info!("InlayMap text: {:?}", inlay_snapshot.text());
1179 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1180 log::info!("FoldMap text: {:?}", fold_snapshot.text());
1181 let (tabs_snapshot, tab_edits) = tab_map.sync(fold_snapshot, fold_edits, tab_size);
1182 log::info!("TabMap text: {:?}", tabs_snapshot.text());
1183
1184 let unwrapped_text = tabs_snapshot.text();
1185 let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
1186 let (mut snapshot, wrap_edits) =
1187 wrap_map.update(cx, |map, cx| map.sync(tabs_snapshot.clone(), tab_edits, cx));
1188 snapshot.check_invariants();
1189 snapshot.verify_chunks(&mut rng);
1190 edits.push((snapshot, wrap_edits));
1191
1192 if wrap_map.read_with(cx, |map, _| map.is_rewrapping()) && rng.gen_bool(0.4) {
1193 log::info!("Waiting for wrapping to finish");
1194 while wrap_map.read_with(cx, |map, _| map.is_rewrapping()) {
1195 notifications.next().await.unwrap();
1196 }
1197 wrap_map.read_with(cx, |map, _| assert!(map.pending_edits.is_empty()));
1198 }
1199
1200 if !wrap_map.read_with(cx, |map, _| map.is_rewrapping()) {
1201 let (mut wrapped_snapshot, wrap_edits) =
1202 wrap_map.update(cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
1203 let actual_text = wrapped_snapshot.text();
1204 let actual_longest_row = wrapped_snapshot.longest_row();
1205 log::info!("Wrapping finished: {:?}", actual_text);
1206 wrapped_snapshot.check_invariants();
1207 wrapped_snapshot.verify_chunks(&mut rng);
1208 edits.push((wrapped_snapshot.clone(), wrap_edits));
1209 assert_eq!(
1210 actual_text, expected_text,
1211 "unwrapped text is: {:?}",
1212 unwrapped_text
1213 );
1214
1215 let mut summary = TextSummary::default();
1216 for (ix, item) in wrapped_snapshot
1217 .transforms
1218 .items(&())
1219 .into_iter()
1220 .enumerate()
1221 {
1222 summary += &item.summary.output;
1223 log::info!("{} summary: {:?}", ix, item.summary.output,);
1224 }
1225
1226 if tab_size.get() == 1
1227 || !wrapped_snapshot
1228 .tab_snapshot
1229 .fold_snapshot
1230 .text()
1231 .contains('\t')
1232 {
1233 let mut expected_longest_rows = Vec::new();
1234 let mut longest_line_len = -1;
1235 for (row, line) in expected_text.split('\n').enumerate() {
1236 let line_char_count = line.chars().count() as isize;
1237 if line_char_count > longest_line_len {
1238 expected_longest_rows.clear();
1239 longest_line_len = line_char_count;
1240 }
1241 if line_char_count >= longest_line_len {
1242 expected_longest_rows.push(row as u32);
1243 }
1244 }
1245
1246 assert!(
1247 expected_longest_rows.contains(&actual_longest_row),
1248 "incorrect longest row {}. expected {:?} with length {}",
1249 actual_longest_row,
1250 expected_longest_rows,
1251 longest_line_len,
1252 )
1253 }
1254 }
1255 }
1256
1257 let mut initial_text = Rope::from(initial_snapshot.text().as_str());
1258 for (snapshot, patch) in edits {
1259 let snapshot_text = Rope::from(snapshot.text().as_str());
1260 for edit in &patch {
1261 let old_start = initial_text.point_to_offset(Point::new(edit.new.start, 0));
1262 let old_end = initial_text.point_to_offset(cmp::min(
1263 Point::new(edit.new.start + edit.old.len() as u32, 0),
1264 initial_text.max_point(),
1265 ));
1266 let new_start = snapshot_text.point_to_offset(Point::new(edit.new.start, 0));
1267 let new_end = snapshot_text.point_to_offset(cmp::min(
1268 Point::new(edit.new.end, 0),
1269 snapshot_text.max_point(),
1270 ));
1271 let new_text = snapshot_text
1272 .chunks_in_range(new_start..new_end)
1273 .collect::<String>();
1274
1275 initial_text.replace(old_start..old_end, &new_text);
1276 }
1277 assert_eq!(initial_text.to_string(), snapshot_text.to_string());
1278 }
1279
1280 if wrap_map.read_with(cx, |map, _| map.is_rewrapping()) {
1281 log::info!("Waiting for wrapping to finish");
1282 while wrap_map.read_with(cx, |map, _| map.is_rewrapping()) {
1283 notifications.next().await.unwrap();
1284 }
1285 }
1286 wrap_map.read_with(cx, |map, _| assert!(map.pending_edits.is_empty()));
1287 }
1288
1289 fn init_test(cx: &mut gpui::TestAppContext) {
1290 cx.foreground().forbid_parking();
1291 cx.update(|cx| {
1292 cx.set_global(SettingsStore::test(cx));
1293 theme::init((), cx);
1294 });
1295 }
1296
1297 fn wrap_text(
1298 unwrapped_text: &str,
1299 wrap_width: Option<f32>,
1300 line_wrapper: &mut LineWrapper,
1301 ) -> String {
1302 if let Some(wrap_width) = wrap_width {
1303 let mut wrapped_text = String::new();
1304 for (row, line) in unwrapped_text.split('\n').enumerate() {
1305 if row > 0 {
1306 wrapped_text.push('\n')
1307 }
1308
1309 let mut prev_ix = 0;
1310 for boundary in line_wrapper.wrap_line(line, wrap_width) {
1311 wrapped_text.push_str(&line[prev_ix..boundary.ix]);
1312 wrapped_text.push('\n');
1313 wrapped_text.push_str(&" ".repeat(boundary.next_indent as usize));
1314 prev_ix = boundary.ix;
1315 }
1316 wrapped_text.push_str(&line[prev_ix..]);
1317 }
1318 wrapped_text
1319 } else {
1320 unwrapped_text.to_string()
1321 }
1322 }
1323
1324 impl WrapSnapshot {
1325 pub fn text(&self) -> String {
1326 self.text_chunks(0).collect()
1327 }
1328
1329 pub fn text_chunks(&self, wrap_row: u32) -> impl Iterator<Item = &str> {
1330 self.chunks(
1331 wrap_row..self.max_point().row() + 1,
1332 false,
1333 None,
1334 None,
1335 None,
1336 )
1337 .map(|h| h.text)
1338 }
1339
1340 fn verify_chunks(&mut self, rng: &mut impl Rng) {
1341 for _ in 0..5 {
1342 let mut end_row = rng.gen_range(0..=self.max_point().row());
1343 let start_row = rng.gen_range(0..=end_row);
1344 end_row += 1;
1345
1346 let mut expected_text = self.text_chunks(start_row).collect::<String>();
1347 if expected_text.ends_with('\n') {
1348 expected_text.push('\n');
1349 }
1350 let mut expected_text = expected_text
1351 .lines()
1352 .take((end_row - start_row) as usize)
1353 .collect::<Vec<_>>()
1354 .join("\n");
1355 if end_row <= self.max_point().row() {
1356 expected_text.push('\n');
1357 }
1358
1359 let actual_text = self
1360 .chunks(start_row..end_row, true, None, None, None)
1361 .map(|c| c.text)
1362 .collect::<String>();
1363 assert_eq!(
1364 expected_text,
1365 actual_text,
1366 "chunks != highlighted_chunks for rows {:?}",
1367 start_row..end_row
1368 );
1369 }
1370 }
1371 }
1372}