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, 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)]
53struct BlockRow(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: u32,
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(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),
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 fn buffer_rows(&self, start_row: u32) -> BlockBufferRows {
637 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
638 cursor.seek(&BlockRow(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 - 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(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 fn line_len(&self, row: u32) -> u32 {
688 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
689 cursor.seek(&BlockRow(row), Bias::Right, &());
690 if let Some(transform) = cursor.item() {
691 let (output_start, input_start) = cursor.start();
692 let overshoot = row - 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 fn is_block_line(&self, row: u32) -> bool {
704 let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
705 cursor.seek(&BlockRow(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<u32>;
885
886 fn next(&mut self) -> Option<Self::Item> {
887 if self.started {
888 self.output_row += 1;
889 } else {
890 self.started = true;
891 }
892
893 if self.output_row >= 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())
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.buffer_rows(0).collect::<Vec<_>>(),
1158 &[
1159 Some(0),
1160 None,
1161 None,
1162 None,
1163 Some(1),
1164 Some(2),
1165 Some(3),
1166 None,
1167 None,
1168 None
1169 ]
1170 );
1171
1172 // Insert a line break, separating two block decorations into separate lines.
1173 let buffer_snapshot = buffer.update(cx, |buffer, cx| {
1174 buffer.edit([(Point::new(1, 1)..Point::new(1, 1), "!!!\n")], None, cx);
1175 buffer.snapshot(cx)
1176 });
1177
1178 let (inlay_snapshot, inlay_edits) =
1179 inlay_map.sync(buffer_snapshot, subscription.consume().into_inner());
1180 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1181 let (tab_snapshot, tab_edits) =
1182 tab_map.sync(fold_snapshot, fold_edits, 4.try_into().unwrap());
1183 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1184 wrap_map.sync(tab_snapshot, tab_edits, cx)
1185 });
1186 let snapshot = block_map.read(wraps_snapshot, wrap_edits);
1187 assert_eq!(snapshot.text(), "aaa\n\nb!!!\n\n\nbb\nccc\nddd\n\n\n");
1188 }
1189
1190 #[gpui::test]
1191 fn test_blocks_on_wrapped_lines(cx: &mut gpui::TestAppContext) {
1192 cx.update(|cx| init_test(cx));
1193
1194 let _font_id = cx.text_system().font_id(&font("Helvetica")).unwrap();
1195
1196 let text = "one two three\nfour five six\nseven eight";
1197
1198 let buffer = cx.update(|cx| MultiBuffer::build_simple(text, cx));
1199 let buffer_snapshot = cx.update(|cx| buffer.read(cx).snapshot(cx));
1200 let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
1201 let (_, fold_snapshot) = FoldMap::new(inlay_snapshot);
1202 let (_, tab_snapshot) = TabMap::new(fold_snapshot, 4.try_into().unwrap());
1203 let (_, wraps_snapshot) = cx.update(|cx| {
1204 WrapMap::new(tab_snapshot, font("Helvetica"), px(14.0), Some(px(60.)), cx)
1205 });
1206 let mut block_map = BlockMap::new(wraps_snapshot.clone(), 1, 1);
1207
1208 let mut writer = block_map.write(wraps_snapshot.clone(), Default::default());
1209 writer.insert(vec![
1210 BlockProperties {
1211 style: BlockStyle::Fixed,
1212 position: buffer_snapshot.anchor_after(Point::new(1, 12)),
1213 disposition: BlockDisposition::Above,
1214 render: Box::new(|_| div().into_any()),
1215 height: 1,
1216 },
1217 BlockProperties {
1218 style: BlockStyle::Fixed,
1219 position: buffer_snapshot.anchor_after(Point::new(1, 1)),
1220 disposition: BlockDisposition::Below,
1221 render: Box::new(|_| div().into_any()),
1222 height: 1,
1223 },
1224 ]);
1225
1226 // Blocks with an 'above' disposition go above their corresponding buffer line.
1227 // Blocks with a 'below' disposition go below their corresponding buffer line.
1228 let snapshot = block_map.read(wraps_snapshot, Default::default());
1229 assert_eq!(
1230 snapshot.text(),
1231 "one two \nthree\n\nfour five \nsix\n\nseven \neight"
1232 );
1233 }
1234
1235 #[gpui::test(iterations = 100)]
1236 fn test_random_blocks(cx: &mut gpui::TestAppContext, mut rng: StdRng) {
1237 cx.update(|cx| init_test(cx));
1238
1239 let operations = env::var("OPERATIONS")
1240 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1241 .unwrap_or(10);
1242
1243 let wrap_width = if rng.gen_bool(0.2) {
1244 None
1245 } else {
1246 Some(px(rng.gen_range(0.0..=100.0)))
1247 };
1248 let tab_size = 1.try_into().unwrap();
1249 let font_size = px(14.0);
1250 let buffer_start_header_height = rng.gen_range(1..=5);
1251 let excerpt_header_height = rng.gen_range(1..=5);
1252
1253 log::info!("Wrap width: {:?}", wrap_width);
1254 log::info!("Excerpt Header Height: {:?}", excerpt_header_height);
1255
1256 let buffer = if rng.gen() {
1257 let len = rng.gen_range(0..10);
1258 let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
1259 log::info!("initial buffer text: {:?}", text);
1260 cx.update(|cx| MultiBuffer::build_simple(&text, cx))
1261 } else {
1262 cx.update(|cx| MultiBuffer::build_random(&mut rng, cx))
1263 };
1264
1265 let mut buffer_snapshot = cx.update(|cx| buffer.read(cx).snapshot(cx));
1266 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
1267 let (mut fold_map, fold_snapshot) = FoldMap::new(inlay_snapshot);
1268 let (mut tab_map, tab_snapshot) = TabMap::new(fold_snapshot, 4.try_into().unwrap());
1269 let (wrap_map, wraps_snapshot) = cx
1270 .update(|cx| WrapMap::new(tab_snapshot, font("Helvetica"), font_size, wrap_width, cx));
1271 let mut block_map = BlockMap::new(
1272 wraps_snapshot,
1273 buffer_start_header_height,
1274 excerpt_header_height,
1275 );
1276 let mut custom_blocks = Vec::new();
1277
1278 for _ in 0..operations {
1279 let mut buffer_edits = Vec::new();
1280 match rng.gen_range(0..=100) {
1281 0..=19 => {
1282 let wrap_width = if rng.gen_bool(0.2) {
1283 None
1284 } else {
1285 Some(px(rng.gen_range(0.0..=100.0)))
1286 };
1287 log::info!("Setting wrap width to {:?}", wrap_width);
1288 wrap_map.update(cx, |map, cx| map.set_wrap_width(wrap_width, cx));
1289 }
1290 20..=39 => {
1291 let block_count = rng.gen_range(1..=5);
1292 let block_properties = (0..block_count)
1293 .map(|_| {
1294 let buffer = cx.update(|cx| buffer.read(cx).read(cx).clone());
1295 let position = buffer.anchor_after(
1296 buffer.clip_offset(rng.gen_range(0..=buffer.len()), Bias::Left),
1297 );
1298
1299 let disposition = if rng.gen() {
1300 BlockDisposition::Above
1301 } else {
1302 BlockDisposition::Below
1303 };
1304 let height = rng.gen_range(1..5);
1305 log::info!(
1306 "inserting block {:?} {:?} with height {}",
1307 disposition,
1308 position.to_point(&buffer),
1309 height
1310 );
1311 BlockProperties {
1312 style: BlockStyle::Fixed,
1313 position,
1314 height,
1315 disposition,
1316 render: Box::new(|_| div().into_any()),
1317 }
1318 })
1319 .collect::<Vec<_>>();
1320
1321 let (inlay_snapshot, inlay_edits) =
1322 inlay_map.sync(buffer_snapshot.clone(), vec![]);
1323 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1324 let (tab_snapshot, tab_edits) =
1325 tab_map.sync(fold_snapshot, fold_edits, tab_size);
1326 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1327 wrap_map.sync(tab_snapshot, tab_edits, cx)
1328 });
1329 let mut block_map = block_map.write(wraps_snapshot, wrap_edits);
1330 let block_ids =
1331 block_map.insert(block_properties.iter().map(|props| BlockProperties {
1332 position: props.position,
1333 height: props.height,
1334 style: props.style,
1335 render: Box::new(|_| div().into_any()),
1336 disposition: props.disposition,
1337 }));
1338 for (block_id, props) in block_ids.into_iter().zip(block_properties) {
1339 custom_blocks.push((block_id, props));
1340 }
1341 }
1342 40..=59 if !custom_blocks.is_empty() => {
1343 let block_count = rng.gen_range(1..=4.min(custom_blocks.len()));
1344 let block_ids_to_remove = (0..block_count)
1345 .map(|_| {
1346 custom_blocks
1347 .remove(rng.gen_range(0..custom_blocks.len()))
1348 .0
1349 })
1350 .collect();
1351
1352 let (inlay_snapshot, inlay_edits) =
1353 inlay_map.sync(buffer_snapshot.clone(), vec![]);
1354 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1355 let (tab_snapshot, tab_edits) =
1356 tab_map.sync(fold_snapshot, fold_edits, tab_size);
1357 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1358 wrap_map.sync(tab_snapshot, tab_edits, cx)
1359 });
1360 let mut block_map = block_map.write(wraps_snapshot, wrap_edits);
1361 block_map.remove(block_ids_to_remove);
1362 }
1363 _ => {
1364 buffer.update(cx, |buffer, cx| {
1365 let mutation_count = rng.gen_range(1..=5);
1366 let subscription = buffer.subscribe();
1367 buffer.randomly_mutate(&mut rng, mutation_count, cx);
1368 buffer_snapshot = buffer.snapshot(cx);
1369 buffer_edits.extend(subscription.consume());
1370 log::info!("buffer text: {:?}", buffer_snapshot.text());
1371 });
1372 }
1373 }
1374
1375 let (inlay_snapshot, inlay_edits) =
1376 inlay_map.sync(buffer_snapshot.clone(), buffer_edits);
1377 let (fold_snapshot, fold_edits) = fold_map.read(inlay_snapshot, inlay_edits);
1378 let (tab_snapshot, tab_edits) = tab_map.sync(fold_snapshot, fold_edits, tab_size);
1379 let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
1380 wrap_map.sync(tab_snapshot, tab_edits, cx)
1381 });
1382 let blocks_snapshot = block_map.read(wraps_snapshot.clone(), wrap_edits);
1383 assert_eq!(
1384 blocks_snapshot.transforms.summary().input_rows,
1385 wraps_snapshot.max_point().row() + 1
1386 );
1387 log::info!("blocks text: {:?}", blocks_snapshot.text());
1388
1389 let mut expected_blocks = Vec::new();
1390 expected_blocks.extend(custom_blocks.iter().map(|(id, block)| {
1391 let mut position = block.position.to_point(&buffer_snapshot);
1392 match block.disposition {
1393 BlockDisposition::Above => {
1394 position.column = 0;
1395 }
1396 BlockDisposition::Below => {
1397 position.column = buffer_snapshot.line_len(position.row);
1398 }
1399 };
1400 let row = wraps_snapshot.make_wrap_point(position, Bias::Left).row();
1401 (
1402 row,
1403 ExpectedBlock::Custom {
1404 disposition: block.disposition,
1405 id: *id,
1406 height: block.height,
1407 },
1408 )
1409 }));
1410 expected_blocks.extend(buffer_snapshot.excerpt_boundaries_in_range(0..).map(
1411 |boundary| {
1412 let position =
1413 wraps_snapshot.make_wrap_point(Point::new(boundary.row, 0), Bias::Left);
1414 (
1415 position.row(),
1416 ExpectedBlock::ExcerptHeader {
1417 height: if boundary.starts_new_buffer {
1418 buffer_start_header_height
1419 } else {
1420 excerpt_header_height
1421 },
1422 starts_new_buffer: boundary.starts_new_buffer,
1423 },
1424 )
1425 },
1426 ));
1427 expected_blocks.sort_unstable();
1428 let mut sorted_blocks_iter = expected_blocks.into_iter().peekable();
1429
1430 let input_buffer_rows = buffer_snapshot.buffer_rows(0).collect::<Vec<_>>();
1431 let mut expected_buffer_rows = Vec::new();
1432 let mut expected_text = String::new();
1433 let mut expected_block_positions = Vec::new();
1434 let input_text = wraps_snapshot.text();
1435 for (row, input_line) in input_text.split('\n').enumerate() {
1436 let row = row as u32;
1437 if row > 0 {
1438 expected_text.push('\n');
1439 }
1440
1441 let buffer_row = input_buffer_rows[wraps_snapshot
1442 .to_point(WrapPoint::new(row, 0), Bias::Left)
1443 .row as usize];
1444
1445 while let Some((block_row, block)) = sorted_blocks_iter.peek() {
1446 if *block_row == row && block.disposition() == BlockDisposition::Above {
1447 let (_, block) = sorted_blocks_iter.next().unwrap();
1448 let height = block.height() as usize;
1449 expected_block_positions
1450 .push((expected_text.matches('\n').count() as u32, block));
1451 let text = "\n".repeat(height);
1452 expected_text.push_str(&text);
1453 for _ in 0..height {
1454 expected_buffer_rows.push(None);
1455 }
1456 } else {
1457 break;
1458 }
1459 }
1460
1461 let soft_wrapped = wraps_snapshot.to_tab_point(WrapPoint::new(row, 0)).column() > 0;
1462 expected_buffer_rows.push(if soft_wrapped { None } else { buffer_row });
1463 expected_text.push_str(input_line);
1464
1465 while let Some((block_row, block)) = sorted_blocks_iter.peek() {
1466 if *block_row == row && block.disposition() == BlockDisposition::Below {
1467 let (_, block) = sorted_blocks_iter.next().unwrap();
1468 let height = block.height() as usize;
1469 expected_block_positions
1470 .push((expected_text.matches('\n').count() as u32 + 1, block));
1471 let text = "\n".repeat(height);
1472 expected_text.push_str(&text);
1473 for _ in 0..height {
1474 expected_buffer_rows.push(None);
1475 }
1476 } else {
1477 break;
1478 }
1479 }
1480 }
1481
1482 let expected_lines = expected_text.split('\n').collect::<Vec<_>>();
1483 let expected_row_count = expected_lines.len();
1484 for start_row in 0..expected_row_count {
1485 let expected_text = expected_lines[start_row..].join("\n");
1486 let actual_text = blocks_snapshot
1487 .chunks(
1488 start_row as u32..blocks_snapshot.max_point().row + 1,
1489 false,
1490 Highlights::default(),
1491 )
1492 .map(|chunk| chunk.text)
1493 .collect::<String>();
1494 assert_eq!(
1495 actual_text, expected_text,
1496 "incorrect text starting from row {}",
1497 start_row
1498 );
1499 assert_eq!(
1500 blocks_snapshot
1501 .buffer_rows(start_row as u32)
1502 .collect::<Vec<_>>(),
1503 &expected_buffer_rows[start_row..]
1504 );
1505 }
1506
1507 assert_eq!(
1508 blocks_snapshot
1509 .blocks_in_range(0..(expected_row_count as u32))
1510 .map(|(row, block)| (row, block.clone().into()))
1511 .collect::<Vec<_>>(),
1512 expected_block_positions
1513 );
1514
1515 let mut expected_longest_rows = Vec::new();
1516 let mut longest_line_len = -1_isize;
1517 for (row, line) in expected_lines.iter().enumerate() {
1518 let row = row as u32;
1519
1520 assert_eq!(
1521 blocks_snapshot.line_len(row),
1522 line.len() as u32,
1523 "invalid line len for row {}",
1524 row
1525 );
1526
1527 let line_char_count = line.chars().count() as isize;
1528 match line_char_count.cmp(&longest_line_len) {
1529 Ordering::Less => {}
1530 Ordering::Equal => expected_longest_rows.push(row),
1531 Ordering::Greater => {
1532 longest_line_len = line_char_count;
1533 expected_longest_rows.clear();
1534 expected_longest_rows.push(row);
1535 }
1536 }
1537 }
1538
1539 let longest_row = blocks_snapshot.longest_row();
1540 assert!(
1541 expected_longest_rows.contains(&longest_row),
1542 "incorrect longest row {}. expected {:?} with length {}",
1543 longest_row,
1544 expected_longest_rows,
1545 longest_line_len,
1546 );
1547
1548 for row in 0..=blocks_snapshot.wrap_snapshot.max_point().row() {
1549 let wrap_point = WrapPoint::new(row, 0);
1550 let block_point = blocks_snapshot.to_block_point(wrap_point);
1551 assert_eq!(blocks_snapshot.to_wrap_point(block_point), wrap_point);
1552 }
1553
1554 let mut block_point = BlockPoint::new(0, 0);
1555 for c in expected_text.chars() {
1556 let left_point = blocks_snapshot.clip_point(block_point, Bias::Left);
1557 let left_buffer_point = blocks_snapshot.to_point(left_point, Bias::Left);
1558 assert_eq!(
1559 blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(left_point)),
1560 left_point
1561 );
1562 assert_eq!(
1563 left_buffer_point,
1564 buffer_snapshot.clip_point(left_buffer_point, Bias::Right),
1565 "{:?} is not valid in buffer coordinates",
1566 left_point
1567 );
1568
1569 let right_point = blocks_snapshot.clip_point(block_point, Bias::Right);
1570 let right_buffer_point = blocks_snapshot.to_point(right_point, Bias::Right);
1571 assert_eq!(
1572 blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(right_point)),
1573 right_point
1574 );
1575 assert_eq!(
1576 right_buffer_point,
1577 buffer_snapshot.clip_point(right_buffer_point, Bias::Left),
1578 "{:?} is not valid in buffer coordinates",
1579 right_point
1580 );
1581
1582 if c == '\n' {
1583 block_point.0 += Point::new(1, 0);
1584 } else {
1585 block_point.column += c.len_utf8() as u32;
1586 }
1587 }
1588 }
1589
1590 #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
1591 enum ExpectedBlock {
1592 ExcerptHeader {
1593 height: u8,
1594 starts_new_buffer: bool,
1595 },
1596 Custom {
1597 disposition: BlockDisposition,
1598 id: BlockId,
1599 height: u8,
1600 },
1601 }
1602
1603 impl ExpectedBlock {
1604 fn height(&self) -> u8 {
1605 match self {
1606 ExpectedBlock::ExcerptHeader { height, .. } => *height,
1607 ExpectedBlock::Custom { height, .. } => *height,
1608 }
1609 }
1610
1611 fn disposition(&self) -> BlockDisposition {
1612 match self {
1613 ExpectedBlock::ExcerptHeader { .. } => BlockDisposition::Above,
1614 ExpectedBlock::Custom { disposition, .. } => *disposition,
1615 }
1616 }
1617 }
1618
1619 impl From<TransformBlock> for ExpectedBlock {
1620 fn from(block: TransformBlock) -> Self {
1621 match block {
1622 TransformBlock::Custom(block) => ExpectedBlock::Custom {
1623 id: block.id,
1624 disposition: block.disposition,
1625 height: block.height,
1626 },
1627 TransformBlock::ExcerptHeader {
1628 height,
1629 starts_new_buffer,
1630 ..
1631 } => ExpectedBlock::ExcerptHeader {
1632 height,
1633 starts_new_buffer,
1634 },
1635 }
1636 }
1637 }
1638 }
1639
1640 fn init_test(cx: &mut gpui::AppContext) {
1641 let settings = SettingsStore::test(cx);
1642 cx.set_global(settings);
1643 theme::init(theme::LoadThemes::JustBase, cx);
1644 }
1645
1646 impl TransformBlock {
1647 fn as_custom(&self) -> Option<&Block> {
1648 match self {
1649 TransformBlock::Custom(block) => Some(block),
1650 TransformBlock::ExcerptHeader { .. } => None,
1651 }
1652 }
1653 }
1654
1655 impl BlockSnapshot {
1656 fn to_point(&self, point: BlockPoint, bias: Bias) -> Point {
1657 self.wrap_snapshot.to_point(self.to_wrap_point(point), bias)
1658 }
1659 }
1660}