1use super::{
2 wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot},
3 TextHighlights,
4};
5use crate::{Anchor, Editor, ExcerptId, ExcerptRange, ToPoint as _};
6use collections::{Bound, HashMap, HashSet};
7use gpui::{fonts::HighlightStyle, AnyElement, ViewContext};
8use language::{BufferSnapshot, Chunk, Patch, Point};
9use parking_lot::Mutex;
10use std::{
11 cell::RefCell,
12 cmp::{self, Ordering},
13 fmt::Debug,
14 ops::{Deref, DerefMut, Range},
15 sync::{
16 atomic::{AtomicUsize, Ordering::SeqCst},
17 Arc,
18 },
19};
20use sum_tree::{Bias, SumTree};
21use text::Edit;
22
23const NEWLINES: &[u8] = &[b'\n'; u8::MAX as usize];
24
25pub struct BlockMap {
26 next_block_id: AtomicUsize,
27 wrap_snapshot: RefCell<WrapSnapshot>,
28 blocks: Vec<Arc<Block>>,
29 transforms: RefCell<SumTree<Transform>>,
30 buffer_header_height: u8,
31 excerpt_header_height: u8,
32}
33
34pub struct BlockMapWriter<'a>(&'a mut BlockMap);
35
36pub struct BlockSnapshot {
37 wrap_snapshot: WrapSnapshot,
38 transforms: SumTree<Transform>,
39}
40
41#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub struct BlockId(usize);
43
44#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
45pub struct BlockPoint(pub Point);
46
47#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
48struct BlockRow(u32);
49
50#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
51struct WrapRow(u32);
52
53pub type RenderBlock = Arc<dyn Fn(&mut BlockContext) -> AnyElement<Editor>>;
54
55pub struct Block {
56 id: BlockId,
57 position: Anchor,
58 height: u8,
59 style: BlockStyle,
60 render: Mutex<RenderBlock>,
61 disposition: BlockDisposition,
62}
63
64#[derive(Clone)]
65pub struct BlockProperties<P>
66where
67 P: Clone,
68{
69 pub position: P,
70 pub height: u8,
71 pub style: BlockStyle,
72 pub render: Arc<dyn Fn(&mut BlockContext) -> AnyElement<Editor>>,
73 pub disposition: BlockDisposition,
74}
75
76#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
77pub enum BlockStyle {
78 Fixed,
79 Flex,
80 Sticky,
81}
82
83pub struct BlockContext<'a, 'b, 'c> {
84 pub view_context: &'c mut ViewContext<'a, 'b, Editor>,
85 pub anchor_x: f32,
86 pub scroll_x: f32,
87 pub gutter_width: f32,
88 pub gutter_padding: f32,
89 pub em_width: f32,
90 pub line_height: f32,
91}
92
93#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
94pub enum BlockDisposition {
95 Above,
96 Below,
97}
98
99#[derive(Clone, Debug)]
100struct Transform {
101 summary: TransformSummary,
102 block: Option<TransformBlock>,
103}
104
105#[allow(clippy::large_enum_variant)]
106#[derive(Clone)]
107pub enum TransformBlock {
108 Custom(Arc<Block>),
109 ExcerptHeader {
110 id: ExcerptId,
111 buffer: BufferSnapshot,
112 range: ExcerptRange<text::Anchor>,
113 height: u8,
114 starts_new_buffer: bool,
115 },
116}
117
118impl TransformBlock {
119 fn disposition(&self) -> BlockDisposition {
120 match self {
121 TransformBlock::Custom(block) => block.disposition,
122 TransformBlock::ExcerptHeader { .. } => BlockDisposition::Above,
123 }
124 }
125
126 pub fn height(&self) -> u8 {
127 match self {
128 TransformBlock::Custom(block) => block.height,
129 TransformBlock::ExcerptHeader { height, .. } => *height,
130 }
131 }
132}
133
134impl Debug for TransformBlock {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 match self {
137 Self::Custom(block) => f.debug_struct("Custom").field("block", block).finish(),
138 Self::ExcerptHeader { buffer, .. } => f
139 .debug_struct("ExcerptHeader")
140 .field("path", &buffer.file().map(|f| f.path()))
141 .finish(),
142 }
143 }
144}
145
146#[derive(Clone, Debug, Default)]
147struct TransformSummary {
148 input_rows: u32,
149 output_rows: u32,
150}
151
152pub struct BlockChunks<'a> {
153 transforms: sum_tree::Cursor<'a, Transform, (BlockRow, WrapRow)>,
154 input_chunks: wrap_map::WrapChunks<'a>,
155 input_chunk: Chunk<'a>,
156 output_row: u32,
157 max_output_row: u32,
158}
159
160#[derive(Clone)]
161pub struct BlockBufferRows<'a> {
162 transforms: sum_tree::Cursor<'a, Transform, (BlockRow, WrapRow)>,
163 input_buffer_rows: wrap_map::WrapBufferRows<'a>,
164 output_row: u32,
165 started: bool,
166}
167
168impl BlockMap {
169 pub fn new(
170 wrap_snapshot: WrapSnapshot,
171 buffer_header_height: u8,
172 excerpt_header_height: u8,
173 ) -> Self {
174 let row_count = wrap_snapshot.max_point().row() + 1;
175 let map = Self {
176 next_block_id: AtomicUsize::new(0),
177 blocks: Vec::new(),
178 transforms: RefCell::new(SumTree::from_item(Transform::isomorphic(row_count), &())),
179 wrap_snapshot: RefCell::new(wrap_snapshot.clone()),
180 buffer_header_height,
181 excerpt_header_height,
182 };
183 map.sync(
184 &wrap_snapshot,
185 Patch::new(vec![Edit {
186 old: 0..row_count,
187 new: 0..row_count,
188 }]),
189 );
190 map
191 }
192
193 pub fn read(&self, wrap_snapshot: WrapSnapshot, edits: Patch<u32>) -> BlockSnapshot {
194 self.sync(&wrap_snapshot, edits);
195 *self.wrap_snapshot.borrow_mut() = wrap_snapshot.clone();
196 BlockSnapshot {
197 wrap_snapshot,
198 transforms: self.transforms.borrow().clone(),
199 }
200 }
201
202 pub fn write(&mut self, wrap_snapshot: WrapSnapshot, edits: Patch<u32>) -> BlockMapWriter {
203 self.sync(&wrap_snapshot, edits);
204 *self.wrap_snapshot.borrow_mut() = wrap_snapshot;
205 BlockMapWriter(self)
206 }
207
208 fn sync(&self, wrap_snapshot: &WrapSnapshot, mut edits: Patch<u32>) {
209 let buffer = wrap_snapshot.buffer_snapshot();
210
211 // Handle changing the last excerpt if it is empty.
212 if buffer.trailing_excerpt_update_count()
213 != self
214 .wrap_snapshot
215 .borrow()
216 .buffer_snapshot()
217 .trailing_excerpt_update_count()
218 {
219 let max_point = wrap_snapshot.max_point();
220 let edit_start = wrap_snapshot.prev_row_boundary(max_point);
221 let edit_end = max_point.row() + 1;
222 edits = edits.compose([WrapEdit {
223 old: edit_start..edit_end,
224 new: edit_start..edit_end,
225 }]);
226 }
227
228 let edits = edits.into_inner();
229 if edits.is_empty() {
230 return;
231 }
232
233 let mut transforms = self.transforms.borrow_mut();
234 let mut new_transforms = SumTree::new();
235 let old_row_count = transforms.summary().input_rows;
236 let new_row_count = wrap_snapshot.max_point().row() + 1;
237 let mut cursor = transforms.cursor::<WrapRow>();
238 let mut last_block_ix = 0;
239 let mut blocks_in_edit = Vec::new();
240 let mut edits = edits.into_iter().peekable();
241
242 while let Some(edit) = edits.next() {
243 // Preserve any old transforms that precede this edit.
244 let old_start = WrapRow(edit.old.start);
245 let new_start = WrapRow(edit.new.start);
246 new_transforms.push_tree(cursor.slice(&old_start, Bias::Left, &()), &());
247 if let Some(transform) = cursor.item() {
248 if transform.is_isomorphic() && old_start == cursor.end(&()) {
249 new_transforms.push(transform.clone(), &());
250 cursor.next(&());
251 while let Some(transform) = cursor.item() {
252 if transform
253 .block
254 .as_ref()
255 .map_or(false, |b| b.disposition().is_below())
256 {
257 new_transforms.push(transform.clone(), &());
258 cursor.next(&());
259 } else {
260 break;
261 }
262 }
263 }
264 }
265
266 // Preserve any portion of an old transform that precedes this edit.
267 let extent_before_edit = old_start.0 - cursor.start().0;
268 push_isomorphic(&mut new_transforms, extent_before_edit);
269
270 // Skip over any old transforms that intersect this edit.
271 let mut old_end = WrapRow(edit.old.end);
272 let mut new_end = WrapRow(edit.new.end);
273 cursor.seek(&old_end, Bias::Left, &());
274 cursor.next(&());
275 if old_end == *cursor.start() {
276 while let Some(transform) = cursor.item() {
277 if transform
278 .block
279 .as_ref()
280 .map_or(false, |b| b.disposition().is_below())
281 {
282 cursor.next(&());
283 } else {
284 break;
285 }
286 }
287 }
288
289 // Combine this edit with any subsequent edits that intersect the same transform.
290 while let Some(next_edit) = edits.peek() {
291 if next_edit.old.start <= cursor.start().0 {
292 old_end = WrapRow(next_edit.old.end);
293 new_end = WrapRow(next_edit.new.end);
294 cursor.seek(&old_end, Bias::Left, &());
295 cursor.next(&());
296 if old_end == *cursor.start() {
297 while let Some(transform) = cursor.item() {
298 if transform
299 .block
300 .as_ref()
301 .map_or(false, |b| b.disposition().is_below())
302 {
303 cursor.next(&());
304 } else {
305 break;
306 }
307 }
308 }
309 edits.next();
310 } else {
311 break;
312 }
313 }
314
315 // Find the blocks within this edited region.
316 let new_buffer_start =
317 wrap_snapshot.to_point(WrapPoint::new(new_start.0, 0), Bias::Left);
318 let start_bound = Bound::Included(new_buffer_start);
319 let start_block_ix = match self.blocks[last_block_ix..].binary_search_by(|probe| {
320 probe
321 .position
322 .to_point(buffer)
323 .cmp(&new_buffer_start)
324 .then(Ordering::Greater)
325 }) {
326 Ok(ix) | Err(ix) => last_block_ix + ix,
327 };
328
329 let end_bound;
330 let end_block_ix = if new_end.0 > wrap_snapshot.max_point().row() {
331 end_bound = Bound::Unbounded;
332 self.blocks.len()
333 } else {
334 let new_buffer_end =
335 wrap_snapshot.to_point(WrapPoint::new(new_end.0, 0), Bias::Left);
336 end_bound = Bound::Excluded(new_buffer_end);
337 match self.blocks[start_block_ix..].binary_search_by(|probe| {
338 probe
339 .position
340 .to_point(buffer)
341 .cmp(&new_buffer_end)
342 .then(Ordering::Greater)
343 }) {
344 Ok(ix) | Err(ix) => start_block_ix + ix,
345 }
346 };
347 last_block_ix = end_block_ix;
348
349 debug_assert!(blocks_in_edit.is_empty());
350 blocks_in_edit.extend(
351 self.blocks[start_block_ix..end_block_ix]
352 .iter()
353 .map(|block| {
354 let mut position = block.position.to_point(buffer);
355 match block.disposition {
356 BlockDisposition::Above => position.column = 0,
357 BlockDisposition::Below => {
358 position.column = buffer.line_len(position.row)
359 }
360 }
361 let position = wrap_snapshot.make_wrap_point(position, Bias::Left);
362 (position.row(), TransformBlock::Custom(block.clone()))
363 }),
364 );
365 blocks_in_edit.extend(
366 buffer
367 .excerpt_boundaries_in_range((start_bound, end_bound))
368 .map(|excerpt_boundary| {
369 (
370 wrap_snapshot
371 .make_wrap_point(Point::new(excerpt_boundary.row, 0), Bias::Left)
372 .row(),
373 TransformBlock::ExcerptHeader {
374 id: excerpt_boundary.id,
375 buffer: excerpt_boundary.buffer,
376 range: excerpt_boundary.range,
377 height: if excerpt_boundary.starts_new_buffer {
378 self.buffer_header_height
379 } else {
380 self.excerpt_header_height
381 },
382 starts_new_buffer: excerpt_boundary.starts_new_buffer,
383 },
384 )
385 }),
386 );
387
388 // Place excerpt headers above custom blocks on the same row.
389 blocks_in_edit.sort_unstable_by(|(row_a, block_a), (row_b, block_b)| {
390 row_a.cmp(row_b).then_with(|| match (block_a, block_b) {
391 (
392 TransformBlock::ExcerptHeader { .. },
393 TransformBlock::ExcerptHeader { .. },
394 ) => Ordering::Equal,
395 (TransformBlock::ExcerptHeader { .. }, _) => Ordering::Less,
396 (_, TransformBlock::ExcerptHeader { .. }) => Ordering::Greater,
397 (TransformBlock::Custom(block_a), TransformBlock::Custom(block_b)) => block_a
398 .disposition
399 .cmp(&block_b.disposition)
400 .then_with(|| block_a.id.cmp(&block_b.id)),
401 })
402 });
403
404 // For each of these blocks, insert a new isomorphic transform preceding the block,
405 // and then insert the block itself.
406 for (block_row, block) in blocks_in_edit.drain(..) {
407 let insertion_row = match block.disposition() {
408 BlockDisposition::Above => block_row,
409 BlockDisposition::Below => block_row + 1,
410 };
411 let extent_before_block = insertion_row - new_transforms.summary().input_rows;
412 push_isomorphic(&mut new_transforms, extent_before_block);
413 new_transforms.push(Transform::block(block), &());
414 }
415
416 old_end = WrapRow(old_end.0.min(old_row_count));
417 new_end = WrapRow(new_end.0.min(new_row_count));
418
419 // Insert an isomorphic transform after the final block.
420 let extent_after_last_block = new_end.0 - new_transforms.summary().input_rows;
421 push_isomorphic(&mut new_transforms, extent_after_last_block);
422
423 // Preserve any portion of the old transform after this edit.
424 let extent_after_edit = cursor.start().0 - old_end.0;
425 push_isomorphic(&mut new_transforms, extent_after_edit);
426 }
427
428 new_transforms.push_tree(cursor.suffix(&()), &());
429 debug_assert_eq!(
430 new_transforms.summary().input_rows,
431 wrap_snapshot.max_point().row() + 1
432 );
433
434 drop(cursor);
435 *transforms = new_transforms;
436 }
437
438 pub fn replace(&mut self, mut renderers: HashMap<BlockId, RenderBlock>) {
439 for block in &self.blocks {
440 if let Some(render) = renderers.remove(&block.id) {
441 *block.render.lock() = render;
442 }
443 }
444 }
445}
446
447fn push_isomorphic(tree: &mut SumTree<Transform>, rows: u32) {
448 if rows == 0 {
449 return;
450 }
451
452 let mut extent = Some(rows);
453 tree.update_last(
454 |last_transform| {
455 if last_transform.is_isomorphic() {
456 let extent = extent.take().unwrap();
457 last_transform.summary.input_rows += extent;
458 last_transform.summary.output_rows += extent;
459 }
460 },
461 &(),
462 );
463 if let Some(extent) = extent {
464 tree.push(Transform::isomorphic(extent), &());
465 }
466}
467
468impl BlockPoint {
469 pub fn new(row: u32, column: u32) -> Self {
470 Self(Point::new(row, column))
471 }
472}
473
474impl Deref for BlockPoint {
475 type Target = Point;
476
477 fn deref(&self) -> &Self::Target {
478 &self.0
479 }
480}
481
482impl std::ops::DerefMut for BlockPoint {
483 fn deref_mut(&mut self) -> &mut Self::Target {
484 &mut self.0
485 }
486}
487
488impl<'a> BlockMapWriter<'a> {
489 pub fn insert(
490 &mut self,
491 blocks: impl IntoIterator<Item = BlockProperties<Anchor>>,
492 ) -> Vec<BlockId> {
493 let mut ids = Vec::new();
494 let mut edits = Patch::default();
495 let wrap_snapshot = &*self.0.wrap_snapshot.borrow();
496 let buffer = wrap_snapshot.buffer_snapshot();
497
498 for block in blocks {
499 let id = BlockId(self.0.next_block_id.fetch_add(1, SeqCst));
500 ids.push(id);
501
502 let position = block.position;
503 let point = position.to_point(buffer);
504 let wrap_row = wrap_snapshot
505 .make_wrap_point(Point::new(point.row, 0), Bias::Left)
506 .row();
507 let start_row = wrap_snapshot.prev_row_boundary(WrapPoint::new(wrap_row, 0));
508 let end_row = wrap_snapshot
509 .next_row_boundary(WrapPoint::new(wrap_row, 0))
510 .unwrap_or(wrap_snapshot.max_point().row() + 1);
511
512 let block_ix = match self
513 .0
514 .blocks
515 .binary_search_by(|probe| probe.position.cmp(&position, buffer))
516 {
517 Ok(ix) | Err(ix) => ix,
518 };
519 self.0.blocks.insert(
520 block_ix,
521 Arc::new(Block {
522 id,
523 position,
524 height: block.height,
525 render: Mutex::new(block.render),
526 disposition: block.disposition,
527 style: block.style,
528 }),
529 );
530
531 edits = edits.compose([Edit {
532 old: start_row..end_row,
533 new: start_row..end_row,
534 }]);
535 }
536
537 self.0.sync(wrap_snapshot, edits);
538 ids
539 }
540
541 pub fn remove(&mut self, block_ids: HashSet<BlockId>) {
542 let wrap_snapshot = &*self.0.wrap_snapshot.borrow();
543 let buffer = wrap_snapshot.buffer_snapshot();
544 let mut edits = Patch::default();
545 let mut last_block_buffer_row = None;
546 self.0.blocks.retain(|block| {
547 if block_ids.contains(&block.id) {
548 let buffer_row = block.position.to_point(buffer).row;
549 if last_block_buffer_row != Some(buffer_row) {
550 last_block_buffer_row = Some(buffer_row);
551 let wrap_row = wrap_snapshot
552 .make_wrap_point(Point::new(buffer_row, 0), Bias::Left)
553 .row();
554 let start_row = wrap_snapshot.prev_row_boundary(WrapPoint::new(wrap_row, 0));
555 let end_row = wrap_snapshot
556 .next_row_boundary(WrapPoint::new(wrap_row, 0))
557 .unwrap_or(wrap_snapshot.max_point().row() + 1);
558 edits.push(Edit {
559 old: start_row..end_row,
560 new: start_row..end_row,
561 })
562 }
563 false
564 } else {
565 true
566 }
567 });
568 self.0.sync(wrap_snapshot, edits);
569 }
570}
571
572impl BlockSnapshot {
573 #[cfg(test)]
574 pub fn text(&self) -> String {
575 self.chunks(0..self.transforms.summary().output_rows, false, None, None)
576 .map(|chunk| chunk.text)
577 .collect()
578 }
579
580 pub fn chunks<'a>(
581 &'a self,
582 rows: Range<u32>,
583 language_aware: bool,
584 text_highlights: Option<&'a TextHighlights>,
585 suggestion_highlight: Option<HighlightStyle>,
586 ) -> BlockChunks<'a> {
587 let max_output_row = cmp::min(rows.end, self.transforms.summary().output_rows);
588 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
589 let input_end = {
590 cursor.seek(&BlockRow(rows.end), Bias::Right, &());
591 let overshoot = if cursor
592 .item()
593 .map_or(false, |transform| transform.is_isomorphic())
594 {
595 rows.end - cursor.start().0 .0
596 } else {
597 0
598 };
599 cursor.start().1 .0 + overshoot
600 };
601 let input_start = {
602 cursor.seek(&BlockRow(rows.start), Bias::Right, &());
603 let overshoot = if cursor
604 .item()
605 .map_or(false, |transform| transform.is_isomorphic())
606 {
607 rows.start - cursor.start().0 .0
608 } else {
609 0
610 };
611 cursor.start().1 .0 + overshoot
612 };
613 BlockChunks {
614 input_chunks: self.wrap_snapshot.chunks(
615 input_start..input_end,
616 language_aware,
617 text_highlights,
618 suggestion_highlight,
619 ),
620 input_chunk: Default::default(),
621 transforms: cursor,
622 output_row: rows.start,
623 max_output_row,
624 }
625 }
626
627 pub fn buffer_rows(&self, start_row: u32) -> BlockBufferRows {
628 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
629 cursor.seek(&BlockRow(start_row), Bias::Right, &());
630 let (output_start, input_start) = cursor.start();
631 let overshoot = if cursor.item().map_or(false, |t| t.is_isomorphic()) {
632 start_row - output_start.0
633 } else {
634 0
635 };
636 let input_start_row = input_start.0 + overshoot;
637 BlockBufferRows {
638 transforms: cursor,
639 input_buffer_rows: self.wrap_snapshot.buffer_rows(input_start_row),
640 output_row: start_row,
641 started: false,
642 }
643 }
644
645 pub fn blocks_in_range(
646 &self,
647 rows: Range<u32>,
648 ) -> impl Iterator<Item = (u32, &TransformBlock)> {
649 let mut cursor = self.transforms.cursor::<BlockRow>();
650 cursor.seek(&BlockRow(rows.start), Bias::Right, &());
651 std::iter::from_fn(move || {
652 while let Some(transform) = cursor.item() {
653 let start_row = cursor.start().0;
654 if start_row >= rows.end {
655 break;
656 }
657 if let Some(block) = &transform.block {
658 cursor.next(&());
659 return Some((start_row, block));
660 } else {
661 cursor.next(&());
662 }
663 }
664 None
665 })
666 }
667
668 pub fn max_point(&self) -> BlockPoint {
669 let row = self.transforms.summary().output_rows - 1;
670 BlockPoint::new(row, self.line_len(row))
671 }
672
673 pub fn longest_row(&self) -> u32 {
674 let input_row = self.wrap_snapshot.longest_row();
675 self.to_block_point(WrapPoint::new(input_row, 0)).row
676 }
677
678 pub fn line_len(&self, row: u32) -> u32 {
679 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
680 cursor.seek(&BlockRow(row), Bias::Right, &());
681 if let Some(transform) = cursor.item() {
682 let (output_start, input_start) = cursor.start();
683 let overshoot = row - output_start.0;
684 if transform.block.is_some() {
685 0
686 } else {
687 self.wrap_snapshot.line_len(input_start.0 + overshoot)
688 }
689 } else {
690 panic!("row out of range");
691 }
692 }
693
694 pub fn is_block_line(&self, row: u32) -> bool {
695 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
696 cursor.seek(&BlockRow(row), Bias::Right, &());
697 cursor.item().map_or(false, |t| t.block.is_some())
698 }
699
700 pub fn clip_point(&self, point: BlockPoint, bias: Bias) -> BlockPoint {
701 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
702 cursor.seek(&BlockRow(point.row), Bias::Right, &());
703
704 let max_input_row = WrapRow(self.transforms.summary().input_rows);
705 let mut search_left =
706 (bias == Bias::Left && cursor.start().1 .0 > 0) || cursor.end(&()).1 == max_input_row;
707 let mut reversed = false;
708
709 loop {
710 if let Some(transform) = cursor.item() {
711 if transform.is_isomorphic() {
712 let (output_start_row, input_start_row) = cursor.start();
713 let (output_end_row, input_end_row) = cursor.end(&());
714 let output_start = Point::new(output_start_row.0, 0);
715 let input_start = Point::new(input_start_row.0, 0);
716 let input_end = Point::new(input_end_row.0, 0);
717 let input_point = if point.row >= output_end_row.0 {
718 let line_len = self.wrap_snapshot.line_len(input_end_row.0 - 1);
719 self.wrap_snapshot
720 .clip_point(WrapPoint::new(input_end_row.0 - 1, line_len), bias)
721 } else {
722 let output_overshoot = point.0.saturating_sub(output_start);
723 self.wrap_snapshot
724 .clip_point(WrapPoint(input_start + output_overshoot), bias)
725 };
726
727 if (input_start..input_end).contains(&input_point.0) {
728 let input_overshoot = input_point.0.saturating_sub(input_start);
729 return BlockPoint(output_start + input_overshoot);
730 }
731 }
732
733 if search_left {
734 cursor.prev(&());
735 } else {
736 cursor.next(&());
737 }
738 } else if reversed {
739 return self.max_point();
740 } else {
741 reversed = true;
742 search_left = !search_left;
743 cursor.seek(&BlockRow(point.row), Bias::Right, &());
744 }
745 }
746 }
747
748 pub fn to_block_point(&self, wrap_point: WrapPoint) -> BlockPoint {
749 let mut cursor = self.transforms.cursor::<(WrapRow, BlockRow)>();
750 cursor.seek(&WrapRow(wrap_point.row()), Bias::Right, &());
751 if let Some(transform) = cursor.item() {
752 debug_assert!(transform.is_isomorphic());
753 } else {
754 return self.max_point();
755 }
756
757 let (input_start_row, output_start_row) = cursor.start();
758 let input_start = Point::new(input_start_row.0, 0);
759 let output_start = Point::new(output_start_row.0, 0);
760 let input_overshoot = wrap_point.0 - input_start;
761 BlockPoint(output_start + input_overshoot)
762 }
763
764 pub fn to_wrap_point(&self, block_point: BlockPoint) -> WrapPoint {
765 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
766 cursor.seek(&BlockRow(block_point.row), Bias::Right, &());
767 if let Some(transform) = cursor.item() {
768 match transform.block.as_ref().map(|b| b.disposition()) {
769 Some(BlockDisposition::Above) => WrapPoint::new(cursor.start().1 .0, 0),
770 Some(BlockDisposition::Below) => {
771 let wrap_row = cursor.start().1 .0 - 1;
772 WrapPoint::new(wrap_row, self.wrap_snapshot.line_len(wrap_row))
773 }
774 None => {
775 let overshoot = block_point.row - cursor.start().0 .0;
776 let wrap_row = cursor.start().1 .0 + overshoot;
777 WrapPoint::new(wrap_row, block_point.column)
778 }
779 }
780 } else {
781 self.wrap_snapshot.max_point()
782 }
783 }
784}
785
786impl Transform {
787 fn isomorphic(rows: u32) -> Self {
788 Self {
789 summary: TransformSummary {
790 input_rows: rows,
791 output_rows: rows,
792 },
793 block: None,
794 }
795 }
796
797 fn block(block: TransformBlock) -> Self {
798 Self {
799 summary: TransformSummary {
800 input_rows: 0,
801 output_rows: block.height() as u32,
802 },
803 block: Some(block),
804 }
805 }
806
807 fn is_isomorphic(&self) -> bool {
808 self.block.is_none()
809 }
810}
811
812impl<'a> Iterator for BlockChunks<'a> {
813 type Item = Chunk<'a>;
814
815 fn next(&mut self) -> Option<Self::Item> {
816 if self.output_row >= self.max_output_row {
817 return None;
818 }
819
820 let transform = self.transforms.item()?;
821 if transform.block.is_some() {
822 let block_start = self.transforms.start().0 .0;
823 let mut block_end = self.transforms.end(&()).0 .0;
824 self.transforms.next(&());
825 if self.transforms.item().is_none() {
826 block_end -= 1;
827 }
828
829 let start_in_block = self.output_row - block_start;
830 let end_in_block = cmp::min(self.max_output_row, block_end) - block_start;
831 let line_count = end_in_block - start_in_block;
832 self.output_row += line_count;
833
834 return Some(Chunk {
835 text: unsafe { std::str::from_utf8_unchecked(&NEWLINES[..line_count as usize]) },
836 ..Default::default()
837 });
838 }
839
840 if self.input_chunk.text.is_empty() {
841 if let Some(input_chunk) = self.input_chunks.next() {
842 self.input_chunk = input_chunk;
843 } else {
844 self.output_row += 1;
845 if self.output_row < self.max_output_row {
846 self.transforms.next(&());
847 return Some(Chunk {
848 text: "\n",
849 ..Default::default()
850 });
851 } else {
852 return None;
853 }
854 }
855 }
856
857 let transform_end = self.transforms.end(&()).0 .0;
858 let (prefix_rows, prefix_bytes) =
859 offset_for_row(self.input_chunk.text, transform_end - self.output_row);
860 self.output_row += prefix_rows;
861 let (prefix, suffix) = self.input_chunk.text.split_at(prefix_bytes);
862 self.input_chunk.text = suffix;
863 if self.output_row == transform_end {
864 self.transforms.next(&());
865 }
866
867 Some(Chunk {
868 text: prefix,
869 ..self.input_chunk
870 })
871 }
872}
873
874impl<'a> Iterator for BlockBufferRows<'a> {
875 type Item = Option<u32>;
876
877 fn next(&mut self) -> Option<Self::Item> {
878 if self.started {
879 self.output_row += 1;
880 } else {
881 self.started = true;
882 }
883
884 if self.output_row >= self.transforms.end(&()).0 .0 {
885 self.transforms.next(&());
886 }
887
888 let transform = self.transforms.item()?;
889 if transform.block.is_some() {
890 Some(None)
891 } else {
892 Some(self.input_buffer_rows.next().unwrap())
893 }
894 }
895}
896
897impl sum_tree::Item for Transform {
898 type Summary = TransformSummary;
899
900 fn summary(&self) -> Self::Summary {
901 self.summary.clone()
902 }
903}
904
905impl sum_tree::Summary for TransformSummary {
906 type Context = ();
907
908 fn add_summary(&mut self, summary: &Self, _: &()) {
909 self.input_rows += summary.input_rows;
910 self.output_rows += summary.output_rows;
911 }
912}
913
914impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapRow {
915 fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
916 self.0 += summary.input_rows;
917 }
918}
919
920impl<'a> sum_tree::Dimension<'a, TransformSummary> for BlockRow {
921 fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
922 self.0 += summary.output_rows;
923 }
924}
925
926impl BlockDisposition {
927 fn is_below(&self) -> bool {
928 matches!(self, BlockDisposition::Below)
929 }
930}
931
932impl<'a, 'b, 'c> Deref for BlockContext<'a, 'b, 'c> {
933 type Target = ViewContext<'a, 'b, Editor>;
934
935 fn deref(&self) -> &Self::Target {
936 self.view_context
937 }
938}
939
940impl DerefMut for BlockContext<'_, '_, '_> {
941 fn deref_mut(&mut self) -> &mut Self::Target {
942 self.view_context
943 }
944}
945
946impl Block {
947 pub fn render(&self, cx: &mut BlockContext) -> AnyElement<Editor> {
948 self.render.lock()(cx)
949 }
950
951 pub fn position(&self) -> &Anchor {
952 &self.position
953 }
954
955 pub fn style(&self) -> BlockStyle {
956 self.style
957 }
958}
959
960impl Debug for Block {
961 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
962 f.debug_struct("Block")
963 .field("id", &self.id)
964 .field("position", &self.position)
965 .field("disposition", &self.disposition)
966 .finish()
967 }
968}
969
970// Count the number of bytes prior to a target point. If the string doesn't contain the target
971// point, return its total extent. Otherwise return the target point itself.
972fn offset_for_row(s: &str, target: u32) -> (u32, usize) {
973 let mut row = 0;
974 let mut offset = 0;
975 for (ix, line) in s.split('\n').enumerate() {
976 if ix > 0 {
977 row += 1;
978 offset += 1;
979 }
980 if row >= target {
981 break;
982 }
983 offset += line.len() as usize;
984 }
985 (row, offset)
986}
987
988#[cfg(test)]
989mod tests {
990 use super::*;
991 use crate::display_map::suggestion_map::SuggestionMap;
992 use crate::display_map::{fold_map::FoldMap, tab_map::TabMap, wrap_map::WrapMap};
993 use crate::multi_buffer::MultiBuffer;
994 use gpui::{elements::Empty, Element};
995 use rand::prelude::*;
996 use settings::SettingsStore;
997 use std::env;
998 use util::RandomCharIter;
999
1000 #[gpui::test]
1001 fn test_offset_for_row() {
1002 assert_eq!(offset_for_row("", 0), (0, 0));
1003 assert_eq!(offset_for_row("", 1), (0, 0));
1004 assert_eq!(offset_for_row("abcd", 0), (0, 0));
1005 assert_eq!(offset_for_row("abcd", 1), (0, 4));
1006 assert_eq!(offset_for_row("\n", 0), (0, 0));
1007 assert_eq!(offset_for_row("\n", 1), (1, 1));
1008 assert_eq!(offset_for_row("abc\ndef\nghi", 0), (0, 0));
1009 assert_eq!(offset_for_row("abc\ndef\nghi", 1), (1, 4));
1010 assert_eq!(offset_for_row("abc\ndef\nghi", 2), (2, 8));
1011 assert_eq!(offset_for_row("abc\ndef\nghi", 3), (2, 11));
1012 }
1013
1014 #[gpui::test]
1015 fn test_basic_blocks(cx: &mut gpui::AppContext) {
1016 init_test(cx);
1017
1018 let family_id = cx
1019 .font_cache()
1020 .load_family(&["Helvetica"], &Default::default())
1021 .unwrap();
1022 let font_id = cx
1023 .font_cache()
1024 .select_font(family_id, &Default::default())
1025 .unwrap();
1026
1027 let text = "aaa\nbbb\nccc\nddd";
1028
1029 let buffer = MultiBuffer::build_simple(text, cx);
1030 let buffer_snapshot = buffer.read(cx).snapshot(cx);
1031 let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
1032 let (fold_map, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
1033 let (suggestion_map, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
1034 let (tab_map, tab_snapshot) = TabMap::new(suggestion_snapshot, 1.try_into().unwrap());
1035 let (wrap_map, wraps_snapshot) = WrapMap::new(tab_snapshot, font_id, 14.0, None, cx);
1036 let mut block_map = BlockMap::new(wraps_snapshot.clone(), 1, 1);
1037
1038 let mut writer = block_map.write(wraps_snapshot.clone(), Default::default());
1039 let block_ids = writer.insert(vec![
1040 BlockProperties {
1041 style: BlockStyle::Fixed,
1042 position: buffer_snapshot.anchor_after(Point::new(1, 0)),
1043 height: 1,
1044 disposition: BlockDisposition::Above,
1045 render: Arc::new(|_| Empty::new().into_any_named("block 1")),
1046 },
1047 BlockProperties {
1048 style: BlockStyle::Fixed,
1049 position: buffer_snapshot.anchor_after(Point::new(1, 2)),
1050 height: 2,
1051 disposition: BlockDisposition::Above,
1052 render: Arc::new(|_| Empty::new().into_any_named("block 2")),
1053 },
1054 BlockProperties {
1055 style: BlockStyle::Fixed,
1056 position: buffer_snapshot.anchor_after(Point::new(3, 3)),
1057 height: 3,
1058 disposition: BlockDisposition::Below,
1059 render: Arc::new(|_| Empty::new().into_any_named("block 3")),
1060 },
1061 ]);
1062
1063 let snapshot = block_map.read(wraps_snapshot, Default::default());
1064 assert_eq!(snapshot.text(), "aaa\n\n\n\nbbb\nccc\nddd\n\n\n");
1065
1066 let blocks = snapshot
1067 .blocks_in_range(0..8)
1068 .map(|(start_row, block)| {
1069 let block = block.as_custom().unwrap();
1070 (start_row..start_row + block.height as u32, block.id)
1071 })
1072 .collect::<Vec<_>>();
1073
1074 // When multiple blocks are on the same line, the newer blocks appear first.
1075 assert_eq!(
1076 blocks,
1077 &[
1078 (1..2, block_ids[0]),
1079 (2..4, block_ids[1]),
1080 (7..10, block_ids[2]),
1081 ]
1082 );
1083
1084 assert_eq!(
1085 snapshot.to_block_point(WrapPoint::new(0, 3)),
1086 BlockPoint::new(0, 3)
1087 );
1088 assert_eq!(
1089 snapshot.to_block_point(WrapPoint::new(1, 0)),
1090 BlockPoint::new(4, 0)
1091 );
1092 assert_eq!(
1093 snapshot.to_block_point(WrapPoint::new(3, 3)),
1094 BlockPoint::new(6, 3)
1095 );
1096
1097 assert_eq!(
1098 snapshot.to_wrap_point(BlockPoint::new(0, 3)),
1099 WrapPoint::new(0, 3)
1100 );
1101 assert_eq!(
1102 snapshot.to_wrap_point(BlockPoint::new(1, 0)),
1103 WrapPoint::new(1, 0)
1104 );
1105 assert_eq!(
1106 snapshot.to_wrap_point(BlockPoint::new(3, 0)),
1107 WrapPoint::new(1, 0)
1108 );
1109 assert_eq!(
1110 snapshot.to_wrap_point(BlockPoint::new(7, 0)),
1111 WrapPoint::new(3, 3)
1112 );
1113
1114 assert_eq!(
1115 snapshot.clip_point(BlockPoint::new(1, 0), Bias::Left),
1116 BlockPoint::new(0, 3)
1117 );
1118 assert_eq!(
1119 snapshot.clip_point(BlockPoint::new(1, 0), Bias::Right),
1120 BlockPoint::new(4, 0)
1121 );
1122 assert_eq!(
1123 snapshot.clip_point(BlockPoint::new(1, 1), Bias::Left),
1124 BlockPoint::new(0, 3)
1125 );
1126 assert_eq!(
1127 snapshot.clip_point(BlockPoint::new(1, 1), Bias::Right),
1128 BlockPoint::new(4, 0)
1129 );
1130 assert_eq!(
1131 snapshot.clip_point(BlockPoint::new(4, 0), Bias::Left),
1132 BlockPoint::new(4, 0)
1133 );
1134 assert_eq!(
1135 snapshot.clip_point(BlockPoint::new(4, 0), Bias::Right),
1136 BlockPoint::new(4, 0)
1137 );
1138 assert_eq!(
1139 snapshot.clip_point(BlockPoint::new(6, 3), Bias::Left),
1140 BlockPoint::new(6, 3)
1141 );
1142 assert_eq!(
1143 snapshot.clip_point(BlockPoint::new(6, 3), Bias::Right),
1144 BlockPoint::new(6, 3)
1145 );
1146 assert_eq!(
1147 snapshot.clip_point(BlockPoint::new(7, 0), Bias::Left),
1148 BlockPoint::new(6, 3)
1149 );
1150 assert_eq!(
1151 snapshot.clip_point(BlockPoint::new(7, 0), Bias::Right),
1152 BlockPoint::new(6, 3)
1153 );
1154
1155 assert_eq!(
1156 snapshot.buffer_rows(0).collect::<Vec<_>>(),
1157 &[
1158 Some(0),
1159 None,
1160 None,
1161 None,
1162 Some(1),
1163 Some(2),
1164 Some(3),
1165 None,
1166 None,
1167 None
1168 ]
1169 );
1170
1171 // Insert a line break, separating two block decorations into separate lines.
1172 let buffer_snapshot = buffer.update(cx, |buffer, cx| {
1173 buffer.edit([(Point::new(1, 1)..Point::new(1, 1), "!!!\n")], None, cx);
1174 buffer.snapshot(cx)
1175 });
1176
1177 let (fold_snapshot, fold_edits) =
1178 fold_map.read(buffer_snapshot, subscription.consume().into_inner());
1179 let (suggestion_snapshot, suggestion_edits) =
1180 suggestion_map.sync(fold_snapshot, fold_edits);
1181 let (tab_snapshot, tab_edits) =
1182 tab_map.sync(suggestion_snapshot, suggestion_edits, 4.try_into().unwrap());
1183 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1184 wrap_map.sync(tab_snapshot, tab_edits, cx)
1185 });
1186 let snapshot = block_map.read(wraps_snapshot, wrap_edits);
1187 assert_eq!(snapshot.text(), "aaa\n\nb!!!\n\n\nbb\nccc\nddd\n\n\n");
1188 }
1189
1190 #[gpui::test]
1191 fn test_blocks_on_wrapped_lines(cx: &mut gpui::AppContext) {
1192 init_test(cx);
1193
1194 let family_id = cx
1195 .font_cache()
1196 .load_family(&["Helvetica"], &Default::default())
1197 .unwrap();
1198 let font_id = cx
1199 .font_cache()
1200 .select_font(family_id, &Default::default())
1201 .unwrap();
1202
1203 let text = "one two three\nfour five six\nseven eight";
1204
1205 let buffer = MultiBuffer::build_simple(text, cx);
1206 let buffer_snapshot = buffer.read(cx).snapshot(cx);
1207 let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
1208 let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
1209 let (_, tab_snapshot) = TabMap::new(suggestion_snapshot, 1.try_into().unwrap());
1210 let (_, wraps_snapshot) = WrapMap::new(tab_snapshot, font_id, 14.0, Some(60.), cx);
1211 let mut block_map = BlockMap::new(wraps_snapshot.clone(), 1, 1);
1212
1213 let mut writer = block_map.write(wraps_snapshot.clone(), Default::default());
1214 writer.insert(vec![
1215 BlockProperties {
1216 style: BlockStyle::Fixed,
1217 position: buffer_snapshot.anchor_after(Point::new(1, 12)),
1218 disposition: BlockDisposition::Above,
1219 render: Arc::new(|_| Empty::new().into_any_named("block 1")),
1220 height: 1,
1221 },
1222 BlockProperties {
1223 style: BlockStyle::Fixed,
1224 position: buffer_snapshot.anchor_after(Point::new(1, 1)),
1225 disposition: BlockDisposition::Below,
1226 render: Arc::new(|_| Empty::new().into_any_named("block 2")),
1227 height: 1,
1228 },
1229 ]);
1230
1231 // Blocks with an 'above' disposition go above their corresponding buffer line.
1232 // Blocks with a 'below' disposition go below their corresponding buffer line.
1233 let snapshot = block_map.read(wraps_snapshot, Default::default());
1234 assert_eq!(
1235 snapshot.text(),
1236 "one two \nthree\n\nfour five \nsix\n\nseven \neight"
1237 );
1238 }
1239
1240 #[gpui::test(iterations = 100)]
1241 fn test_random_blocks(cx: &mut gpui::AppContext, mut rng: StdRng) {
1242 init_test(cx);
1243
1244 let operations = env::var("OPERATIONS")
1245 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1246 .unwrap_or(10);
1247
1248 let wrap_width = if rng.gen_bool(0.2) {
1249 None
1250 } else {
1251 Some(rng.gen_range(0.0..=100.0))
1252 };
1253 let tab_size = 1.try_into().unwrap();
1254 let family_id = cx
1255 .font_cache()
1256 .load_family(&["Helvetica"], &Default::default())
1257 .unwrap();
1258 let font_id = cx
1259 .font_cache()
1260 .select_font(family_id, &Default::default())
1261 .unwrap();
1262 let font_size = 14.0;
1263 let buffer_start_header_height = rng.gen_range(1..=5);
1264 let excerpt_header_height = rng.gen_range(1..=5);
1265
1266 log::info!("Wrap width: {:?}", wrap_width);
1267 log::info!("Excerpt Header Height: {:?}", excerpt_header_height);
1268
1269 let buffer = if rng.gen() {
1270 let len = rng.gen_range(0..10);
1271 let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
1272 log::info!("initial buffer text: {:?}", text);
1273 MultiBuffer::build_simple(&text, cx)
1274 } else {
1275 MultiBuffer::build_random(&mut rng, cx)
1276 };
1277
1278 let mut buffer_snapshot = buffer.read(cx).snapshot(cx);
1279 let (fold_map, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
1280 let (suggestion_map, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
1281 let (tab_map, tab_snapshot) = TabMap::new(suggestion_snapshot, tab_size);
1282 let (wrap_map, wraps_snapshot) =
1283 WrapMap::new(tab_snapshot, font_id, font_size, wrap_width, cx);
1284 let mut block_map = BlockMap::new(
1285 wraps_snapshot,
1286 buffer_start_header_height,
1287 excerpt_header_height,
1288 );
1289 let mut custom_blocks = Vec::new();
1290
1291 for _ in 0..operations {
1292 let mut buffer_edits = Vec::new();
1293 match rng.gen_range(0..=100) {
1294 0..=19 => {
1295 let wrap_width = if rng.gen_bool(0.2) {
1296 None
1297 } else {
1298 Some(rng.gen_range(0.0..=100.0))
1299 };
1300 log::info!("Setting wrap width to {:?}", wrap_width);
1301 wrap_map.update(cx, |map, cx| map.set_wrap_width(wrap_width, cx));
1302 }
1303 20..=39 => {
1304 let block_count = rng.gen_range(1..=5);
1305 let block_properties = (0..block_count)
1306 .map(|_| {
1307 let buffer = buffer.read(cx).read(cx);
1308 let position = buffer.anchor_after(
1309 buffer.clip_offset(rng.gen_range(0..=buffer.len()), Bias::Left),
1310 );
1311
1312 let disposition = if rng.gen() {
1313 BlockDisposition::Above
1314 } else {
1315 BlockDisposition::Below
1316 };
1317 let height = rng.gen_range(1..5);
1318 log::info!(
1319 "inserting block {:?} {:?} with height {}",
1320 disposition,
1321 position.to_point(&buffer),
1322 height
1323 );
1324 BlockProperties {
1325 style: BlockStyle::Fixed,
1326 position,
1327 height,
1328 disposition,
1329 render: Arc::new(|_| Empty::new().into_any()),
1330 }
1331 })
1332 .collect::<Vec<_>>();
1333
1334 let (fold_snapshot, fold_edits) =
1335 fold_map.read(buffer_snapshot.clone(), vec![]);
1336 let (suggestion_snapshot, suggestion_edits) =
1337 suggestion_map.sync(fold_snapshot, fold_edits);
1338 let (tab_snapshot, tab_edits) =
1339 tab_map.sync(suggestion_snapshot, suggestion_edits, tab_size);
1340 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1341 wrap_map.sync(tab_snapshot, tab_edits, cx)
1342 });
1343 let mut block_map = block_map.write(wraps_snapshot, wrap_edits);
1344 let block_ids = block_map.insert(block_properties.clone());
1345 for (block_id, props) in block_ids.into_iter().zip(block_properties) {
1346 custom_blocks.push((block_id, props));
1347 }
1348 }
1349 40..=59 if !custom_blocks.is_empty() => {
1350 let block_count = rng.gen_range(1..=4.min(custom_blocks.len()));
1351 let block_ids_to_remove = (0..block_count)
1352 .map(|_| {
1353 custom_blocks
1354 .remove(rng.gen_range(0..custom_blocks.len()))
1355 .0
1356 })
1357 .collect();
1358
1359 let (fold_snapshot, fold_edits) =
1360 fold_map.read(buffer_snapshot.clone(), vec![]);
1361 let (suggestion_snapshot, suggestion_edits) =
1362 suggestion_map.sync(fold_snapshot, fold_edits);
1363 let (tab_snapshot, tab_edits) =
1364 tab_map.sync(suggestion_snapshot, suggestion_edits, tab_size);
1365 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1366 wrap_map.sync(tab_snapshot, tab_edits, cx)
1367 });
1368 let mut block_map = block_map.write(wraps_snapshot, wrap_edits);
1369 block_map.remove(block_ids_to_remove);
1370 }
1371 _ => {
1372 buffer.update(cx, |buffer, cx| {
1373 let mutation_count = rng.gen_range(1..=5);
1374 let subscription = buffer.subscribe();
1375 buffer.randomly_mutate(&mut rng, mutation_count, cx);
1376 buffer_snapshot = buffer.snapshot(cx);
1377 buffer_edits.extend(subscription.consume());
1378 log::info!("buffer text: {:?}", buffer_snapshot.text());
1379 });
1380 }
1381 }
1382
1383 let (fold_snapshot, fold_edits) = fold_map.read(buffer_snapshot.clone(), buffer_edits);
1384 let (suggestion_snapshot, suggestion_edits) =
1385 suggestion_map.sync(fold_snapshot, fold_edits);
1386 let (tab_snapshot, tab_edits) =
1387 tab_map.sync(suggestion_snapshot, suggestion_edits, tab_size);
1388 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1389 wrap_map.sync(tab_snapshot, tab_edits, cx)
1390 });
1391 let blocks_snapshot = block_map.read(wraps_snapshot.clone(), wrap_edits);
1392 assert_eq!(
1393 blocks_snapshot.transforms.summary().input_rows,
1394 wraps_snapshot.max_point().row() + 1
1395 );
1396 log::info!("blocks text: {:?}", blocks_snapshot.text());
1397
1398 let mut expected_blocks = Vec::new();
1399 expected_blocks.extend(custom_blocks.iter().map(|(id, block)| {
1400 let mut position = block.position.to_point(&buffer_snapshot);
1401 match block.disposition {
1402 BlockDisposition::Above => {
1403 position.column = 0;
1404 }
1405 BlockDisposition::Below => {
1406 position.column = buffer_snapshot.line_len(position.row);
1407 }
1408 };
1409 let row = wraps_snapshot.make_wrap_point(position, Bias::Left).row();
1410 (
1411 row,
1412 ExpectedBlock::Custom {
1413 disposition: block.disposition,
1414 id: *id,
1415 height: block.height,
1416 },
1417 )
1418 }));
1419 expected_blocks.extend(buffer_snapshot.excerpt_boundaries_in_range(0..).map(
1420 |boundary| {
1421 let position =
1422 wraps_snapshot.make_wrap_point(Point::new(boundary.row, 0), Bias::Left);
1423 (
1424 position.row(),
1425 ExpectedBlock::ExcerptHeader {
1426 height: if boundary.starts_new_buffer {
1427 buffer_start_header_height
1428 } else {
1429 excerpt_header_height
1430 },
1431 starts_new_buffer: boundary.starts_new_buffer,
1432 },
1433 )
1434 },
1435 ));
1436 expected_blocks.sort_unstable();
1437 let mut sorted_blocks_iter = expected_blocks.into_iter().peekable();
1438
1439 let input_buffer_rows = buffer_snapshot.buffer_rows(0).collect::<Vec<_>>();
1440 let mut expected_buffer_rows = Vec::new();
1441 let mut expected_text = String::new();
1442 let mut expected_block_positions = Vec::new();
1443 let input_text = wraps_snapshot.text();
1444 for (row, input_line) in input_text.split('\n').enumerate() {
1445 let row = row as u32;
1446 if row > 0 {
1447 expected_text.push('\n');
1448 }
1449
1450 let buffer_row = input_buffer_rows[wraps_snapshot
1451 .to_point(WrapPoint::new(row, 0), Bias::Left)
1452 .row as usize];
1453
1454 while let Some((block_row, block)) = sorted_blocks_iter.peek() {
1455 if *block_row == row && block.disposition() == BlockDisposition::Above {
1456 let (_, block) = sorted_blocks_iter.next().unwrap();
1457 let height = block.height() as usize;
1458 expected_block_positions
1459 .push((expected_text.matches('\n').count() as u32, block));
1460 let text = "\n".repeat(height);
1461 expected_text.push_str(&text);
1462 for _ in 0..height {
1463 expected_buffer_rows.push(None);
1464 }
1465 } else {
1466 break;
1467 }
1468 }
1469
1470 let soft_wrapped = wraps_snapshot.to_tab_point(WrapPoint::new(row, 0)).column() > 0;
1471 expected_buffer_rows.push(if soft_wrapped { None } else { buffer_row });
1472 expected_text.push_str(input_line);
1473
1474 while let Some((block_row, block)) = sorted_blocks_iter.peek() {
1475 if *block_row == row && block.disposition() == BlockDisposition::Below {
1476 let (_, block) = sorted_blocks_iter.next().unwrap();
1477 let height = block.height() as usize;
1478 expected_block_positions
1479 .push((expected_text.matches('\n').count() as u32 + 1, block));
1480 let text = "\n".repeat(height);
1481 expected_text.push_str(&text);
1482 for _ in 0..height {
1483 expected_buffer_rows.push(None);
1484 }
1485 } else {
1486 break;
1487 }
1488 }
1489 }
1490
1491 let expected_lines = expected_text.split('\n').collect::<Vec<_>>();
1492 let expected_row_count = expected_lines.len();
1493 for start_row in 0..expected_row_count {
1494 let expected_text = expected_lines[start_row..].join("\n");
1495 let actual_text = blocks_snapshot
1496 .chunks(
1497 start_row as u32..blocks_snapshot.max_point().row + 1,
1498 false,
1499 None,
1500 None,
1501 )
1502 .map(|chunk| chunk.text)
1503 .collect::<String>();
1504 assert_eq!(
1505 actual_text, expected_text,
1506 "incorrect text starting from row {}",
1507 start_row
1508 );
1509 assert_eq!(
1510 blocks_snapshot
1511 .buffer_rows(start_row as u32)
1512 .collect::<Vec<_>>(),
1513 &expected_buffer_rows[start_row..]
1514 );
1515 }
1516
1517 assert_eq!(
1518 blocks_snapshot
1519 .blocks_in_range(0..(expected_row_count as u32))
1520 .map(|(row, block)| (row, block.clone().into()))
1521 .collect::<Vec<_>>(),
1522 expected_block_positions
1523 );
1524
1525 let mut expected_longest_rows = Vec::new();
1526 let mut longest_line_len = -1_isize;
1527 for (row, line) in expected_lines.iter().enumerate() {
1528 let row = row as u32;
1529
1530 assert_eq!(
1531 blocks_snapshot.line_len(row),
1532 line.len() as u32,
1533 "invalid line len for row {}",
1534 row
1535 );
1536
1537 let line_char_count = line.chars().count() as isize;
1538 match line_char_count.cmp(&longest_line_len) {
1539 Ordering::Less => {}
1540 Ordering::Equal => expected_longest_rows.push(row),
1541 Ordering::Greater => {
1542 longest_line_len = line_char_count;
1543 expected_longest_rows.clear();
1544 expected_longest_rows.push(row);
1545 }
1546 }
1547 }
1548
1549 let longest_row = blocks_snapshot.longest_row();
1550 assert!(
1551 expected_longest_rows.contains(&longest_row),
1552 "incorrect longest row {}. expected {:?} with length {}",
1553 longest_row,
1554 expected_longest_rows,
1555 longest_line_len,
1556 );
1557
1558 for row in 0..=blocks_snapshot.wrap_snapshot.max_point().row() {
1559 let wrap_point = WrapPoint::new(row, 0);
1560 let block_point = blocks_snapshot.to_block_point(wrap_point);
1561 assert_eq!(blocks_snapshot.to_wrap_point(block_point), wrap_point);
1562 }
1563
1564 let mut block_point = BlockPoint::new(0, 0);
1565 for c in expected_text.chars() {
1566 let left_point = blocks_snapshot.clip_point(block_point, Bias::Left);
1567 let left_buffer_point = blocks_snapshot.to_point(left_point, Bias::Left);
1568 assert_eq!(
1569 blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(left_point)),
1570 left_point
1571 );
1572 assert_eq!(
1573 left_buffer_point,
1574 buffer_snapshot.clip_point(left_buffer_point, Bias::Right),
1575 "{:?} is not valid in buffer coordinates",
1576 left_point
1577 );
1578
1579 let right_point = blocks_snapshot.clip_point(block_point, Bias::Right);
1580 let right_buffer_point = blocks_snapshot.to_point(right_point, Bias::Right);
1581 assert_eq!(
1582 blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(right_point)),
1583 right_point
1584 );
1585 assert_eq!(
1586 right_buffer_point,
1587 buffer_snapshot.clip_point(right_buffer_point, Bias::Left),
1588 "{:?} is not valid in buffer coordinates",
1589 right_point
1590 );
1591
1592 if c == '\n' {
1593 block_point.0 += Point::new(1, 0);
1594 } else {
1595 block_point.column += c.len_utf8() as u32;
1596 }
1597 }
1598 }
1599
1600 #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
1601 enum ExpectedBlock {
1602 ExcerptHeader {
1603 height: u8,
1604 starts_new_buffer: bool,
1605 },
1606 Custom {
1607 disposition: BlockDisposition,
1608 id: BlockId,
1609 height: u8,
1610 },
1611 }
1612
1613 impl ExpectedBlock {
1614 fn height(&self) -> u8 {
1615 match self {
1616 ExpectedBlock::ExcerptHeader { height, .. } => *height,
1617 ExpectedBlock::Custom { height, .. } => *height,
1618 }
1619 }
1620
1621 fn disposition(&self) -> BlockDisposition {
1622 match self {
1623 ExpectedBlock::ExcerptHeader { .. } => BlockDisposition::Above,
1624 ExpectedBlock::Custom { disposition, .. } => *disposition,
1625 }
1626 }
1627 }
1628
1629 impl From<TransformBlock> for ExpectedBlock {
1630 fn from(block: TransformBlock) -> Self {
1631 match block {
1632 TransformBlock::Custom(block) => ExpectedBlock::Custom {
1633 id: block.id,
1634 disposition: block.disposition,
1635 height: block.height,
1636 },
1637 TransformBlock::ExcerptHeader {
1638 height,
1639 starts_new_buffer,
1640 ..
1641 } => ExpectedBlock::ExcerptHeader {
1642 height,
1643 starts_new_buffer,
1644 },
1645 }
1646 }
1647 }
1648 }
1649
1650 fn init_test(cx: &mut gpui::AppContext) {
1651 cx.set_global(SettingsStore::test(cx));
1652 theme::init((), cx);
1653 }
1654
1655 impl TransformBlock {
1656 fn as_custom(&self) -> Option<&Block> {
1657 match self {
1658 TransformBlock::Custom(block) => Some(block),
1659 TransformBlock::ExcerptHeader { .. } => None,
1660 }
1661 }
1662 }
1663
1664 impl BlockSnapshot {
1665 fn to_point(&self, point: BlockPoint, bias: Bias) -> Point {
1666 self.wrap_snapshot.to_point(self.to_wrap_point(point), bias)
1667 }
1668 }
1669}