block_map.rs

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