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