block_map.rs

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