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