block_map.rs

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