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