multi_buffer.rs

   1mod anchor;
   2
   3pub use anchor::{Anchor, AnchorRangeExt};
   4use anyhow::Result;
   5use clock::ReplicaId;
   6use collections::{BTreeMap, Bound, HashMap, HashSet};
   7use futures::{channel::mpsc, SinkExt};
   8use git::diff::DiffHunk;
   9use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
  10pub use language::Completion;
  11use language::{
  12    char_kind, AutoindentMode, Buffer, BufferChunks, BufferSnapshot, CharKind, Chunk, CursorShape,
  13    DiagnosticEntry, IndentSize, Language, LanguageScope, OffsetRangeExt, OffsetUtf16, Outline,
  14    OutlineItem, Point, PointUtf16, Selection, TextDimension, ToOffset as _, ToOffsetUtf16 as _,
  15    ToPoint as _, ToPointUtf16 as _, TransactionId, Unclipped,
  16};
  17use std::{
  18    borrow::Cow,
  19    cell::{Ref, RefCell},
  20    cmp, fmt, io,
  21    iter::{self, FromIterator},
  22    mem,
  23    ops::{Range, RangeBounds, Sub},
  24    str,
  25    sync::Arc,
  26    time::{Duration, Instant},
  27};
  28use sum_tree::{Bias, Cursor, SumTree};
  29use text::{
  30    locator::Locator,
  31    subscription::{Subscription, Topic},
  32    Edit, TextSummary,
  33};
  34use theme::SyntaxTheme;
  35use util::post_inc;
  36
  37const NEWLINES: &[u8] = &[b'\n'; u8::MAX as usize];
  38
  39#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
  40pub struct ExcerptId(usize);
  41
  42pub struct MultiBuffer {
  43    snapshot: RefCell<MultiBufferSnapshot>,
  44    buffers: RefCell<HashMap<usize, BufferState>>,
  45    next_excerpt_id: usize,
  46    subscriptions: Topic,
  47    singleton: bool,
  48    replica_id: ReplicaId,
  49    history: History,
  50    title: Option<String>,
  51}
  52
  53#[derive(Clone, Debug, PartialEq, Eq)]
  54pub enum Event {
  55    ExcerptsAdded {
  56        buffer: ModelHandle<Buffer>,
  57        predecessor: ExcerptId,
  58        excerpts: Vec<(ExcerptId, ExcerptRange<language::Anchor>)>,
  59    },
  60    ExcerptsRemoved {
  61        ids: Vec<ExcerptId>,
  62    },
  63    Edited,
  64    Reloaded,
  65    Reparsed,
  66    Saved,
  67    FileHandleChanged,
  68    Closed,
  69    DirtyChanged,
  70    DiagnosticsUpdated,
  71}
  72
  73#[derive(Clone)]
  74struct History {
  75    next_transaction_id: TransactionId,
  76    undo_stack: Vec<Transaction>,
  77    redo_stack: Vec<Transaction>,
  78    transaction_depth: usize,
  79    group_interval: Duration,
  80}
  81
  82#[derive(Clone)]
  83struct Transaction {
  84    id: TransactionId,
  85    buffer_transactions: HashMap<usize, text::TransactionId>,
  86    first_edit_at: Instant,
  87    last_edit_at: Instant,
  88    suppress_grouping: bool,
  89}
  90
  91pub trait ToOffset: 'static + fmt::Debug {
  92    fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize;
  93}
  94
  95pub trait ToOffsetUtf16: 'static + fmt::Debug {
  96    fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16;
  97}
  98
  99pub trait ToPoint: 'static + fmt::Debug {
 100    fn to_point(&self, snapshot: &MultiBufferSnapshot) -> Point;
 101}
 102
 103pub trait ToPointUtf16: 'static + fmt::Debug {
 104    fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> PointUtf16;
 105}
 106
 107struct BufferState {
 108    buffer: ModelHandle<Buffer>,
 109    last_version: clock::Global,
 110    last_parse_count: usize,
 111    last_selections_update_count: usize,
 112    last_diagnostics_update_count: usize,
 113    last_file_update_count: usize,
 114    last_git_diff_update_count: usize,
 115    excerpts: Vec<Locator>,
 116    _subscriptions: [gpui::Subscription; 2],
 117}
 118
 119#[derive(Clone, Default)]
 120pub struct MultiBufferSnapshot {
 121    singleton: bool,
 122    excerpts: SumTree<Excerpt>,
 123    excerpt_ids: SumTree<ExcerptIdMapping>,
 124    parse_count: usize,
 125    diagnostics_update_count: usize,
 126    trailing_excerpt_update_count: usize,
 127    git_diff_update_count: usize,
 128    edit_count: usize,
 129    is_dirty: bool,
 130    has_conflict: bool,
 131}
 132
 133pub struct ExcerptBoundary {
 134    pub id: ExcerptId,
 135    pub row: u32,
 136    pub buffer: BufferSnapshot,
 137    pub range: ExcerptRange<text::Anchor>,
 138    pub starts_new_buffer: bool,
 139}
 140
 141#[derive(Clone)]
 142struct Excerpt {
 143    id: ExcerptId,
 144    locator: Locator,
 145    buffer_id: usize,
 146    buffer: BufferSnapshot,
 147    range: ExcerptRange<text::Anchor>,
 148    max_buffer_row: u32,
 149    text_summary: TextSummary,
 150    has_trailing_newline: bool,
 151}
 152
 153#[derive(Clone, Debug)]
 154struct ExcerptIdMapping {
 155    id: ExcerptId,
 156    locator: Locator,
 157}
 158
 159#[derive(Clone, Debug, Eq, PartialEq)]
 160pub struct ExcerptRange<T> {
 161    pub context: Range<T>,
 162    pub primary: Option<Range<T>>,
 163}
 164
 165#[derive(Clone, Debug, Default)]
 166struct ExcerptSummary {
 167    excerpt_id: ExcerptId,
 168    excerpt_locator: Locator,
 169    max_buffer_row: u32,
 170    text: TextSummary,
 171}
 172
 173#[derive(Clone)]
 174pub struct MultiBufferRows<'a> {
 175    buffer_row_range: Range<u32>,
 176    excerpts: Cursor<'a, Excerpt, Point>,
 177}
 178
 179pub struct MultiBufferChunks<'a> {
 180    range: Range<usize>,
 181    excerpts: Cursor<'a, Excerpt, usize>,
 182    excerpt_chunks: Option<ExcerptChunks<'a>>,
 183    language_aware: bool,
 184}
 185
 186pub struct MultiBufferBytes<'a> {
 187    range: Range<usize>,
 188    excerpts: Cursor<'a, Excerpt, usize>,
 189    excerpt_bytes: Option<ExcerptBytes<'a>>,
 190    chunk: &'a [u8],
 191}
 192
 193struct ExcerptChunks<'a> {
 194    content_chunks: BufferChunks<'a>,
 195    footer_height: usize,
 196}
 197
 198struct ExcerptBytes<'a> {
 199    content_bytes: text::Bytes<'a>,
 200    footer_height: usize,
 201}
 202
 203impl MultiBuffer {
 204    pub fn new(replica_id: ReplicaId) -> Self {
 205        Self {
 206            snapshot: Default::default(),
 207            buffers: Default::default(),
 208            next_excerpt_id: 1,
 209            subscriptions: Default::default(),
 210            singleton: false,
 211            replica_id,
 212            history: History {
 213                next_transaction_id: Default::default(),
 214                undo_stack: Default::default(),
 215                redo_stack: Default::default(),
 216                transaction_depth: 0,
 217                group_interval: Duration::from_millis(300),
 218            },
 219            title: Default::default(),
 220        }
 221    }
 222
 223    pub fn clone(&self, new_cx: &mut ModelContext<Self>) -> Self {
 224        let mut buffers = HashMap::default();
 225        for (buffer_id, buffer_state) in self.buffers.borrow().iter() {
 226            buffers.insert(
 227                *buffer_id,
 228                BufferState {
 229                    buffer: buffer_state.buffer.clone(),
 230                    last_version: buffer_state.last_version.clone(),
 231                    last_parse_count: buffer_state.last_parse_count,
 232                    last_selections_update_count: buffer_state.last_selections_update_count,
 233                    last_diagnostics_update_count: buffer_state.last_diagnostics_update_count,
 234                    last_file_update_count: buffer_state.last_file_update_count,
 235                    last_git_diff_update_count: buffer_state.last_git_diff_update_count,
 236                    excerpts: buffer_state.excerpts.clone(),
 237                    _subscriptions: [
 238                        new_cx.observe(&buffer_state.buffer, |_, _, cx| cx.notify()),
 239                        new_cx.subscribe(&buffer_state.buffer, Self::on_buffer_event),
 240                    ],
 241                },
 242            );
 243        }
 244        Self {
 245            snapshot: RefCell::new(self.snapshot.borrow().clone()),
 246            buffers: RefCell::new(buffers),
 247            next_excerpt_id: 1,
 248            subscriptions: Default::default(),
 249            singleton: self.singleton,
 250            replica_id: self.replica_id,
 251            history: self.history.clone(),
 252            title: self.title.clone(),
 253        }
 254    }
 255
 256    pub fn with_title(mut self, title: String) -> Self {
 257        self.title = Some(title);
 258        self
 259    }
 260
 261    pub fn singleton(buffer: ModelHandle<Buffer>, cx: &mut ModelContext<Self>) -> Self {
 262        let mut this = Self::new(buffer.read(cx).replica_id());
 263        this.singleton = true;
 264        this.push_excerpts(
 265            buffer,
 266            [ExcerptRange {
 267                context: text::Anchor::MIN..text::Anchor::MAX,
 268                primary: None,
 269            }],
 270            cx,
 271        );
 272        this.snapshot.borrow_mut().singleton = true;
 273        this
 274    }
 275
 276    pub fn replica_id(&self) -> ReplicaId {
 277        self.replica_id
 278    }
 279
 280    pub fn snapshot(&self, cx: &AppContext) -> MultiBufferSnapshot {
 281        self.sync(cx);
 282        self.snapshot.borrow().clone()
 283    }
 284
 285    pub(crate) fn read(&self, cx: &AppContext) -> Ref<MultiBufferSnapshot> {
 286        self.sync(cx);
 287        self.snapshot.borrow()
 288    }
 289
 290    pub fn as_singleton(&self) -> Option<ModelHandle<Buffer>> {
 291        if self.singleton {
 292            return Some(
 293                self.buffers
 294                    .borrow()
 295                    .values()
 296                    .next()
 297                    .unwrap()
 298                    .buffer
 299                    .clone(),
 300            );
 301        } else {
 302            None
 303        }
 304    }
 305
 306    pub fn is_singleton(&self) -> bool {
 307        self.singleton
 308    }
 309
 310    pub fn subscribe(&mut self) -> Subscription {
 311        self.subscriptions.subscribe()
 312    }
 313
 314    pub fn is_dirty(&self, cx: &AppContext) -> bool {
 315        self.read(cx).is_dirty()
 316    }
 317
 318    pub fn has_conflict(&self, cx: &AppContext) -> bool {
 319        self.read(cx).has_conflict()
 320    }
 321
 322    // The `is_empty` signature doesn't match what clippy expects
 323    #[allow(clippy::len_without_is_empty)]
 324    pub fn len(&self, cx: &AppContext) -> usize {
 325        self.read(cx).len()
 326    }
 327
 328    pub fn is_empty(&self, cx: &AppContext) -> bool {
 329        self.len(cx) != 0
 330    }
 331
 332    pub fn symbols_containing<T: ToOffset>(
 333        &self,
 334        offset: T,
 335        theme: Option<&SyntaxTheme>,
 336        cx: &AppContext,
 337    ) -> Option<(usize, Vec<OutlineItem<Anchor>>)> {
 338        self.read(cx).symbols_containing(offset, theme)
 339    }
 340
 341    pub fn git_diff_recalc(&mut self, cx: &mut ModelContext<Self>) {
 342        let buffers = self.buffers.borrow();
 343        for buffer_state in buffers.values() {
 344            if buffer_state.buffer.read(cx).needs_git_diff_recalc() {
 345                buffer_state
 346                    .buffer
 347                    .update(cx, |buffer, cx| buffer.git_diff_recalc(cx))
 348            }
 349        }
 350    }
 351
 352    pub fn edit<I, S, T>(
 353        &mut self,
 354        edits: I,
 355        mut autoindent_mode: Option<AutoindentMode>,
 356        cx: &mut ModelContext<Self>,
 357    ) where
 358        I: IntoIterator<Item = (Range<S>, T)>,
 359        S: ToOffset,
 360        T: Into<Arc<str>>,
 361    {
 362        if self.buffers.borrow().is_empty() {
 363            return;
 364        }
 365
 366        let snapshot = self.read(cx);
 367        let edits = edits.into_iter().map(|(range, new_text)| {
 368            let mut range = range.start.to_offset(&snapshot)..range.end.to_offset(&snapshot);
 369            if range.start > range.end {
 370                mem::swap(&mut range.start, &mut range.end);
 371            }
 372            (range, new_text)
 373        });
 374
 375        if let Some(buffer) = self.as_singleton() {
 376            return buffer.update(cx, |buffer, cx| {
 377                buffer.edit(edits, autoindent_mode, cx);
 378            });
 379        }
 380
 381        let original_indent_columns = match &mut autoindent_mode {
 382            Some(AutoindentMode::Block {
 383                original_indent_columns,
 384            }) => mem::take(original_indent_columns),
 385            _ => Default::default(),
 386        };
 387
 388        #[allow(clippy::type_complexity)]
 389        let mut buffer_edits: HashMap<usize, Vec<(Range<usize>, Arc<str>, bool, u32)>> =
 390            Default::default();
 391        let mut cursor = snapshot.excerpts.cursor::<usize>();
 392        for (ix, (range, new_text)) in edits.enumerate() {
 393            let new_text: Arc<str> = new_text.into();
 394            let original_indent_column = original_indent_columns.get(ix).copied().unwrap_or(0);
 395            cursor.seek(&range.start, Bias::Right, &());
 396            if cursor.item().is_none() && range.start == *cursor.start() {
 397                cursor.prev(&());
 398            }
 399            let start_excerpt = cursor.item().expect("start offset out of bounds");
 400            let start_overshoot = range.start - cursor.start();
 401            let buffer_start = start_excerpt
 402                .range
 403                .context
 404                .start
 405                .to_offset(&start_excerpt.buffer)
 406                + start_overshoot;
 407
 408            cursor.seek(&range.end, Bias::Right, &());
 409            if cursor.item().is_none() && range.end == *cursor.start() {
 410                cursor.prev(&());
 411            }
 412            let end_excerpt = cursor.item().expect("end offset out of bounds");
 413            let end_overshoot = range.end - cursor.start();
 414            let buffer_end = end_excerpt
 415                .range
 416                .context
 417                .start
 418                .to_offset(&end_excerpt.buffer)
 419                + end_overshoot;
 420
 421            if start_excerpt.id == end_excerpt.id {
 422                buffer_edits
 423                    .entry(start_excerpt.buffer_id)
 424                    .or_insert(Vec::new())
 425                    .push((
 426                        buffer_start..buffer_end,
 427                        new_text,
 428                        true,
 429                        original_indent_column,
 430                    ));
 431            } else {
 432                let start_excerpt_range = buffer_start
 433                    ..start_excerpt
 434                        .range
 435                        .context
 436                        .end
 437                        .to_offset(&start_excerpt.buffer);
 438                let end_excerpt_range = end_excerpt
 439                    .range
 440                    .context
 441                    .start
 442                    .to_offset(&end_excerpt.buffer)
 443                    ..buffer_end;
 444                buffer_edits
 445                    .entry(start_excerpt.buffer_id)
 446                    .or_insert(Vec::new())
 447                    .push((
 448                        start_excerpt_range,
 449                        new_text.clone(),
 450                        true,
 451                        original_indent_column,
 452                    ));
 453                buffer_edits
 454                    .entry(end_excerpt.buffer_id)
 455                    .or_insert(Vec::new())
 456                    .push((
 457                        end_excerpt_range,
 458                        new_text.clone(),
 459                        false,
 460                        original_indent_column,
 461                    ));
 462
 463                cursor.seek(&range.start, Bias::Right, &());
 464                cursor.next(&());
 465                while let Some(excerpt) = cursor.item() {
 466                    if excerpt.id == end_excerpt.id {
 467                        break;
 468                    }
 469                    buffer_edits
 470                        .entry(excerpt.buffer_id)
 471                        .or_insert(Vec::new())
 472                        .push((
 473                            excerpt.range.context.to_offset(&excerpt.buffer),
 474                            new_text.clone(),
 475                            false,
 476                            original_indent_column,
 477                        ));
 478                    cursor.next(&());
 479                }
 480            }
 481        }
 482
 483        for (buffer_id, mut edits) in buffer_edits {
 484            edits.sort_unstable_by_key(|(range, _, _, _)| range.start);
 485            self.buffers.borrow()[&buffer_id]
 486                .buffer
 487                .update(cx, |buffer, cx| {
 488                    let mut edits = edits.into_iter().peekable();
 489                    let mut insertions = Vec::new();
 490                    let mut original_indent_columns = Vec::new();
 491                    let mut deletions = Vec::new();
 492                    let empty_str: Arc<str> = "".into();
 493                    while let Some((
 494                        mut range,
 495                        new_text,
 496                        mut is_insertion,
 497                        original_indent_column,
 498                    )) = edits.next()
 499                    {
 500                        while let Some((next_range, _, next_is_insertion, _)) = edits.peek() {
 501                            if range.end >= next_range.start {
 502                                range.end = cmp::max(next_range.end, range.end);
 503                                is_insertion |= *next_is_insertion;
 504                                edits.next();
 505                            } else {
 506                                break;
 507                            }
 508                        }
 509
 510                        if is_insertion {
 511                            original_indent_columns.push(original_indent_column);
 512                            insertions.push((
 513                                buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
 514                                new_text.clone(),
 515                            ));
 516                        } else if !range.is_empty() {
 517                            deletions.push((
 518                                buffer.anchor_before(range.start)..buffer.anchor_before(range.end),
 519                                empty_str.clone(),
 520                            ));
 521                        }
 522                    }
 523
 524                    let deletion_autoindent_mode =
 525                        if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
 526                            Some(AutoindentMode::Block {
 527                                original_indent_columns: Default::default(),
 528                            })
 529                        } else {
 530                            None
 531                        };
 532                    let insertion_autoindent_mode =
 533                        if let Some(AutoindentMode::Block { .. }) = autoindent_mode {
 534                            Some(AutoindentMode::Block {
 535                                original_indent_columns,
 536                            })
 537                        } else {
 538                            None
 539                        };
 540
 541                    buffer.edit(deletions, deletion_autoindent_mode, cx);
 542                    buffer.edit(insertions, insertion_autoindent_mode, cx);
 543                })
 544        }
 545    }
 546
 547    pub fn start_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
 548        self.start_transaction_at(Instant::now(), cx)
 549    }
 550
 551    pub(crate) fn start_transaction_at(
 552        &mut self,
 553        now: Instant,
 554        cx: &mut ModelContext<Self>,
 555    ) -> Option<TransactionId> {
 556        if let Some(buffer) = self.as_singleton() {
 557            return buffer.update(cx, |buffer, _| buffer.start_transaction_at(now));
 558        }
 559
 560        for BufferState { buffer, .. } in self.buffers.borrow().values() {
 561            buffer.update(cx, |buffer, _| buffer.start_transaction_at(now));
 562        }
 563        self.history.start_transaction(now)
 564    }
 565
 566    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
 567        self.end_transaction_at(Instant::now(), cx)
 568    }
 569
 570    pub(crate) fn end_transaction_at(
 571        &mut self,
 572        now: Instant,
 573        cx: &mut ModelContext<Self>,
 574    ) -> Option<TransactionId> {
 575        if let Some(buffer) = self.as_singleton() {
 576            return buffer.update(cx, |buffer, cx| buffer.end_transaction_at(now, cx));
 577        }
 578
 579        let mut buffer_transactions = HashMap::default();
 580        for BufferState { buffer, .. } in self.buffers.borrow().values() {
 581            if let Some(transaction_id) =
 582                buffer.update(cx, |buffer, cx| buffer.end_transaction_at(now, cx))
 583            {
 584                buffer_transactions.insert(buffer.id(), transaction_id);
 585            }
 586        }
 587
 588        if self.history.end_transaction(now, buffer_transactions) {
 589            let transaction_id = self.history.group().unwrap();
 590            Some(transaction_id)
 591        } else {
 592            None
 593        }
 594    }
 595
 596    pub fn finalize_last_transaction(&mut self, cx: &mut ModelContext<Self>) {
 597        self.history.finalize_last_transaction();
 598        for BufferState { buffer, .. } in self.buffers.borrow().values() {
 599            buffer.update(cx, |buffer, _| {
 600                buffer.finalize_last_transaction();
 601            });
 602        }
 603    }
 604
 605    pub fn push_transaction<'a, T>(&mut self, buffer_transactions: T)
 606    where
 607        T: IntoIterator<Item = (&'a ModelHandle<Buffer>, &'a language::Transaction)>,
 608    {
 609        self.history
 610            .push_transaction(buffer_transactions, Instant::now());
 611        self.history.finalize_last_transaction();
 612    }
 613
 614    pub fn group_until_transaction(
 615        &mut self,
 616        transaction_id: TransactionId,
 617        cx: &mut ModelContext<Self>,
 618    ) {
 619        if let Some(buffer) = self.as_singleton() {
 620            buffer.update(cx, |buffer, _| {
 621                buffer.group_until_transaction(transaction_id)
 622            });
 623        } else {
 624            self.history.group_until(transaction_id);
 625        }
 626    }
 627
 628    pub fn set_active_selections(
 629        &mut self,
 630        selections: &[Selection<Anchor>],
 631        line_mode: bool,
 632        cursor_shape: CursorShape,
 633        cx: &mut ModelContext<Self>,
 634    ) {
 635        let mut selections_by_buffer: HashMap<usize, Vec<Selection<text::Anchor>>> =
 636            Default::default();
 637        let snapshot = self.read(cx);
 638        let mut cursor = snapshot.excerpts.cursor::<Option<&Locator>>();
 639        for selection in selections {
 640            let start_locator = snapshot.excerpt_locator_for_id(selection.start.excerpt_id);
 641            let end_locator = snapshot.excerpt_locator_for_id(selection.end.excerpt_id);
 642
 643            cursor.seek(&Some(start_locator), Bias::Left, &());
 644            while let Some(excerpt) = cursor.item() {
 645                if excerpt.locator > *end_locator {
 646                    break;
 647                }
 648
 649                let mut start = excerpt.range.context.start;
 650                let mut end = excerpt.range.context.end;
 651                if excerpt.id == selection.start.excerpt_id {
 652                    start = selection.start.text_anchor;
 653                }
 654                if excerpt.id == selection.end.excerpt_id {
 655                    end = selection.end.text_anchor;
 656                }
 657                selections_by_buffer
 658                    .entry(excerpt.buffer_id)
 659                    .or_default()
 660                    .push(Selection {
 661                        id: selection.id,
 662                        start,
 663                        end,
 664                        reversed: selection.reversed,
 665                        goal: selection.goal,
 666                    });
 667
 668                cursor.next(&());
 669            }
 670        }
 671
 672        for (buffer_id, buffer_state) in self.buffers.borrow().iter() {
 673            if !selections_by_buffer.contains_key(buffer_id) {
 674                buffer_state
 675                    .buffer
 676                    .update(cx, |buffer, cx| buffer.remove_active_selections(cx));
 677            }
 678        }
 679
 680        for (buffer_id, mut selections) in selections_by_buffer {
 681            self.buffers.borrow()[&buffer_id]
 682                .buffer
 683                .update(cx, |buffer, cx| {
 684                    selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer));
 685                    let mut selections = selections.into_iter().peekable();
 686                    let merged_selections = Arc::from_iter(iter::from_fn(|| {
 687                        let mut selection = selections.next()?;
 688                        while let Some(next_selection) = selections.peek() {
 689                            if selection.end.cmp(&next_selection.start, buffer).is_ge() {
 690                                let next_selection = selections.next().unwrap();
 691                                if next_selection.end.cmp(&selection.end, buffer).is_ge() {
 692                                    selection.end = next_selection.end;
 693                                }
 694                            } else {
 695                                break;
 696                            }
 697                        }
 698                        Some(selection)
 699                    }));
 700                    buffer.set_active_selections(merged_selections, line_mode, cursor_shape, cx);
 701                });
 702        }
 703    }
 704
 705    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
 706        for buffer in self.buffers.borrow().values() {
 707            buffer
 708                .buffer
 709                .update(cx, |buffer, cx| buffer.remove_active_selections(cx));
 710        }
 711    }
 712
 713    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
 714        if let Some(buffer) = self.as_singleton() {
 715            return buffer.update(cx, |buffer, cx| buffer.undo(cx));
 716        }
 717
 718        while let Some(transaction) = self.history.pop_undo() {
 719            let mut undone = false;
 720            for (buffer_id, buffer_transaction_id) in &mut transaction.buffer_transactions {
 721                if let Some(BufferState { buffer, .. }) = self.buffers.borrow().get(buffer_id) {
 722                    undone |= buffer.update(cx, |buffer, cx| {
 723                        let undo_to = *buffer_transaction_id;
 724                        if let Some(entry) = buffer.peek_undo_stack() {
 725                            *buffer_transaction_id = entry.transaction_id();
 726                        }
 727                        buffer.undo_to_transaction(undo_to, cx)
 728                    });
 729                }
 730            }
 731
 732            if undone {
 733                return Some(transaction.id);
 734            }
 735        }
 736
 737        None
 738    }
 739
 740    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
 741        if let Some(buffer) = self.as_singleton() {
 742            return buffer.update(cx, |buffer, cx| buffer.redo(cx));
 743        }
 744
 745        while let Some(transaction) = self.history.pop_redo() {
 746            let mut redone = false;
 747            for (buffer_id, buffer_transaction_id) in &mut transaction.buffer_transactions {
 748                if let Some(BufferState { buffer, .. }) = self.buffers.borrow().get(buffer_id) {
 749                    redone |= buffer.update(cx, |buffer, cx| {
 750                        let redo_to = *buffer_transaction_id;
 751                        if let Some(entry) = buffer.peek_redo_stack() {
 752                            *buffer_transaction_id = entry.transaction_id();
 753                        }
 754                        buffer.redo_to_transaction(redo_to, cx)
 755                    });
 756                }
 757            }
 758
 759            if redone {
 760                return Some(transaction.id);
 761            }
 762        }
 763
 764        None
 765    }
 766
 767    pub fn stream_excerpts_with_context_lines(
 768        &mut self,
 769        excerpts: Vec<(ModelHandle<Buffer>, Vec<Range<text::Anchor>>)>,
 770        context_line_count: u32,
 771        cx: &mut ModelContext<Self>,
 772    ) -> (Task<()>, mpsc::Receiver<Range<Anchor>>) {
 773        let (mut tx, rx) = mpsc::channel(256);
 774        let task = cx.spawn(|this, mut cx| async move {
 775            for (buffer, ranges) in excerpts {
 776                let buffer_id = buffer.id();
 777                let buffer_snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
 778
 779                let mut excerpt_ranges = Vec::new();
 780                let mut range_counts = Vec::new();
 781                cx.background()
 782                    .scoped(|scope| {
 783                        scope.spawn(async {
 784                            let (ranges, counts) =
 785                                build_excerpt_ranges(&buffer_snapshot, &ranges, context_line_count);
 786                            excerpt_ranges = ranges;
 787                            range_counts = counts;
 788                        });
 789                    })
 790                    .await;
 791
 792                let mut ranges = ranges.into_iter();
 793                let mut range_counts = range_counts.into_iter();
 794                for excerpt_ranges in excerpt_ranges.chunks(100) {
 795                    let excerpt_ids = this.update(&mut cx, |this, cx| {
 796                        this.push_excerpts(buffer.clone(), excerpt_ranges.iter().cloned(), cx)
 797                    });
 798
 799                    for (excerpt_id, range_count) in
 800                        excerpt_ids.into_iter().zip(range_counts.by_ref())
 801                    {
 802                        for range in ranges.by_ref().take(range_count) {
 803                            let start = Anchor {
 804                                buffer_id: Some(buffer_id),
 805                                excerpt_id: excerpt_id.clone(),
 806                                text_anchor: range.start,
 807                            };
 808                            let end = Anchor {
 809                                buffer_id: Some(buffer_id),
 810                                excerpt_id: excerpt_id.clone(),
 811                                text_anchor: range.end,
 812                            };
 813                            if tx.send(start..end).await.is_err() {
 814                                break;
 815                            }
 816                        }
 817                    }
 818                }
 819            }
 820        });
 821        (task, rx)
 822    }
 823
 824    pub fn push_excerpts<O>(
 825        &mut self,
 826        buffer: ModelHandle<Buffer>,
 827        ranges: impl IntoIterator<Item = ExcerptRange<O>>,
 828        cx: &mut ModelContext<Self>,
 829    ) -> Vec<ExcerptId>
 830    where
 831        O: text::ToOffset,
 832    {
 833        self.insert_excerpts_after(ExcerptId::max(), buffer, ranges, cx)
 834    }
 835
 836    pub fn push_excerpts_with_context_lines<O>(
 837        &mut self,
 838        buffer: ModelHandle<Buffer>,
 839        ranges: Vec<Range<O>>,
 840        context_line_count: u32,
 841        cx: &mut ModelContext<Self>,
 842    ) -> Vec<Range<Anchor>>
 843    where
 844        O: text::ToPoint + text::ToOffset,
 845    {
 846        let buffer_id = buffer.id();
 847        let buffer_snapshot = buffer.read(cx).snapshot();
 848        let (excerpt_ranges, range_counts) =
 849            build_excerpt_ranges(&buffer_snapshot, &ranges, context_line_count);
 850
 851        let excerpt_ids = self.push_excerpts(buffer, excerpt_ranges, cx);
 852
 853        let mut anchor_ranges = Vec::new();
 854        let mut ranges = ranges.into_iter();
 855        for (excerpt_id, range_count) in excerpt_ids.into_iter().zip(range_counts.into_iter()) {
 856            anchor_ranges.extend(ranges.by_ref().take(range_count).map(|range| {
 857                let start = Anchor {
 858                    buffer_id: Some(buffer_id),
 859                    excerpt_id: excerpt_id.clone(),
 860                    text_anchor: buffer_snapshot.anchor_after(range.start),
 861                };
 862                let end = Anchor {
 863                    buffer_id: Some(buffer_id),
 864                    excerpt_id: excerpt_id.clone(),
 865                    text_anchor: buffer_snapshot.anchor_after(range.end),
 866                };
 867                start..end
 868            }))
 869        }
 870        anchor_ranges
 871    }
 872
 873    pub fn insert_excerpts_after<O>(
 874        &mut self,
 875        prev_excerpt_id: ExcerptId,
 876        buffer: ModelHandle<Buffer>,
 877        ranges: impl IntoIterator<Item = ExcerptRange<O>>,
 878        cx: &mut ModelContext<Self>,
 879    ) -> Vec<ExcerptId>
 880    where
 881        O: text::ToOffset,
 882    {
 883        let mut ids = Vec::new();
 884        let mut next_excerpt_id = self.next_excerpt_id;
 885        self.insert_excerpts_with_ids_after(
 886            prev_excerpt_id,
 887            buffer,
 888            ranges.into_iter().map(|range| {
 889                let id = ExcerptId(post_inc(&mut next_excerpt_id));
 890                ids.push(id);
 891                (id, range)
 892            }),
 893            cx,
 894        );
 895        ids
 896    }
 897
 898    pub fn insert_excerpts_with_ids_after<O>(
 899        &mut self,
 900        prev_excerpt_id: ExcerptId,
 901        buffer: ModelHandle<Buffer>,
 902        ranges: impl IntoIterator<Item = (ExcerptId, ExcerptRange<O>)>,
 903        cx: &mut ModelContext<Self>,
 904    ) where
 905        O: text::ToOffset,
 906    {
 907        assert_eq!(self.history.transaction_depth, 0);
 908        let mut ranges = ranges.into_iter().peekable();
 909        if ranges.peek().is_none() {
 910            return Default::default();
 911        }
 912
 913        self.sync(cx);
 914
 915        let buffer_id = buffer.id();
 916        let buffer_snapshot = buffer.read(cx).snapshot();
 917
 918        let mut buffers = self.buffers.borrow_mut();
 919        let buffer_state = buffers.entry(buffer_id).or_insert_with(|| BufferState {
 920            last_version: buffer_snapshot.version().clone(),
 921            last_parse_count: buffer_snapshot.parse_count(),
 922            last_selections_update_count: buffer_snapshot.selections_update_count(),
 923            last_diagnostics_update_count: buffer_snapshot.diagnostics_update_count(),
 924            last_file_update_count: buffer_snapshot.file_update_count(),
 925            last_git_diff_update_count: buffer_snapshot.git_diff_update_count(),
 926            excerpts: Default::default(),
 927            _subscriptions: [
 928                cx.observe(&buffer, |_, _, cx| cx.notify()),
 929                cx.subscribe(&buffer, Self::on_buffer_event),
 930            ],
 931            buffer: buffer.clone(),
 932        });
 933
 934        let mut snapshot = self.snapshot.borrow_mut();
 935
 936        let mut prev_locator = snapshot.excerpt_locator_for_id(prev_excerpt_id).clone();
 937        let mut new_excerpt_ids = mem::take(&mut snapshot.excerpt_ids);
 938        let mut cursor = snapshot.excerpts.cursor::<Option<&Locator>>();
 939        let mut new_excerpts = cursor.slice(&prev_locator, Bias::Right, &());
 940        prev_locator = cursor.start().unwrap_or(Locator::min_ref()).clone();
 941
 942        let edit_start = new_excerpts.summary().text.len;
 943        new_excerpts.update_last(
 944            |excerpt| {
 945                excerpt.has_trailing_newline = true;
 946            },
 947            &(),
 948        );
 949
 950        let next_locator = if let Some(excerpt) = cursor.item() {
 951            excerpt.locator.clone()
 952        } else {
 953            Locator::max()
 954        };
 955
 956        let mut excerpts = Vec::new();
 957        while let Some((id, range)) = ranges.next() {
 958            let locator = Locator::between(&prev_locator, &next_locator);
 959            if let Err(ix) = buffer_state.excerpts.binary_search(&locator) {
 960                buffer_state.excerpts.insert(ix, locator.clone());
 961            }
 962            let range = ExcerptRange {
 963                context: buffer_snapshot.anchor_before(&range.context.start)
 964                    ..buffer_snapshot.anchor_after(&range.context.end),
 965                primary: range.primary.map(|primary| {
 966                    buffer_snapshot.anchor_before(&primary.start)
 967                        ..buffer_snapshot.anchor_after(&primary.end)
 968                }),
 969            };
 970            if id.0 >= self.next_excerpt_id {
 971                self.next_excerpt_id = id.0 + 1;
 972            }
 973            excerpts.push((id, range.clone()));
 974            let excerpt = Excerpt::new(
 975                id,
 976                locator.clone(),
 977                buffer_id,
 978                buffer_snapshot.clone(),
 979                range,
 980                ranges.peek().is_some() || cursor.item().is_some(),
 981            );
 982            new_excerpts.push(excerpt, &());
 983            prev_locator = locator.clone();
 984            new_excerpt_ids.push(ExcerptIdMapping { id, locator }, &());
 985        }
 986
 987        let edit_end = new_excerpts.summary().text.len;
 988
 989        let suffix = cursor.suffix(&());
 990        let changed_trailing_excerpt = suffix.is_empty();
 991        new_excerpts.push_tree(suffix, &());
 992        drop(cursor);
 993        snapshot.excerpts = new_excerpts;
 994        snapshot.excerpt_ids = new_excerpt_ids;
 995        if changed_trailing_excerpt {
 996            snapshot.trailing_excerpt_update_count += 1;
 997        }
 998
 999        self.subscriptions.publish_mut([Edit {
1000            old: edit_start..edit_start,
1001            new: edit_start..edit_end,
1002        }]);
1003        cx.emit(Event::Edited);
1004        cx.emit(Event::ExcerptsAdded {
1005            buffer,
1006            predecessor: prev_excerpt_id,
1007            excerpts,
1008        });
1009        cx.notify();
1010    }
1011
1012    pub fn clear(&mut self, cx: &mut ModelContext<Self>) {
1013        self.sync(cx);
1014        let ids = self.excerpt_ids();
1015        self.buffers.borrow_mut().clear();
1016        let mut snapshot = self.snapshot.borrow_mut();
1017        let prev_len = snapshot.len();
1018        snapshot.excerpts = Default::default();
1019        snapshot.trailing_excerpt_update_count += 1;
1020        snapshot.is_dirty = false;
1021        snapshot.has_conflict = false;
1022
1023        self.subscriptions.publish_mut([Edit {
1024            old: 0..prev_len,
1025            new: 0..0,
1026        }]);
1027        cx.emit(Event::Edited);
1028        cx.emit(Event::ExcerptsRemoved { ids });
1029        cx.notify();
1030    }
1031
1032    pub fn excerpts_for_buffer(
1033        &self,
1034        buffer: &ModelHandle<Buffer>,
1035        cx: &AppContext,
1036    ) -> Vec<(ExcerptId, ExcerptRange<text::Anchor>)> {
1037        let mut excerpts = Vec::new();
1038        let snapshot = self.read(cx);
1039        let buffers = self.buffers.borrow();
1040        let mut cursor = snapshot.excerpts.cursor::<Option<&Locator>>();
1041        for locator in buffers
1042            .get(&buffer.id())
1043            .map(|state| &state.excerpts)
1044            .into_iter()
1045            .flatten()
1046        {
1047            cursor.seek_forward(&Some(locator), Bias::Left, &());
1048            if let Some(excerpt) = cursor.item() {
1049                if excerpt.locator == *locator {
1050                    excerpts.push((excerpt.id.clone(), excerpt.range.clone()));
1051                }
1052            }
1053        }
1054
1055        excerpts
1056    }
1057
1058    pub fn excerpt_ids(&self) -> Vec<ExcerptId> {
1059        self.snapshot
1060            .borrow()
1061            .excerpts
1062            .iter()
1063            .map(|entry| entry.id)
1064            .collect()
1065    }
1066
1067    pub fn excerpt_containing(
1068        &self,
1069        position: impl ToOffset,
1070        cx: &AppContext,
1071    ) -> Option<(ExcerptId, ModelHandle<Buffer>, Range<text::Anchor>)> {
1072        let snapshot = self.read(cx);
1073        let position = position.to_offset(&snapshot);
1074
1075        let mut cursor = snapshot.excerpts.cursor::<usize>();
1076        cursor.seek(&position, Bias::Right, &());
1077        cursor.item().map(|excerpt| {
1078            (
1079                excerpt.id.clone(),
1080                self.buffers
1081                    .borrow()
1082                    .get(&excerpt.buffer_id)
1083                    .unwrap()
1084                    .buffer
1085                    .clone(),
1086                excerpt.range.context.clone(),
1087            )
1088        })
1089    }
1090
1091    // If point is at the end of the buffer, the last excerpt is returned
1092    pub fn point_to_buffer_offset<T: ToOffset>(
1093        &self,
1094        point: T,
1095        cx: &AppContext,
1096    ) -> Option<(ModelHandle<Buffer>, usize)> {
1097        let snapshot = self.read(cx);
1098        let offset = point.to_offset(&snapshot);
1099        let mut cursor = snapshot.excerpts.cursor::<usize>();
1100        cursor.seek(&offset, Bias::Right, &());
1101        if cursor.item().is_none() {
1102            cursor.prev(&());
1103        }
1104
1105        cursor.item().map(|excerpt| {
1106            let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
1107            let buffer_point = excerpt_start + offset - *cursor.start();
1108            let buffer = self.buffers.borrow()[&excerpt.buffer_id].buffer.clone();
1109
1110            (buffer, buffer_point)
1111        })
1112    }
1113
1114    pub fn range_to_buffer_ranges<T: ToOffset>(
1115        &self,
1116        range: Range<T>,
1117        cx: &AppContext,
1118    ) -> Vec<(ModelHandle<Buffer>, Range<usize>)> {
1119        let snapshot = self.read(cx);
1120        let start = range.start.to_offset(&snapshot);
1121        let end = range.end.to_offset(&snapshot);
1122
1123        let mut result = Vec::new();
1124        let mut cursor = snapshot.excerpts.cursor::<usize>();
1125        cursor.seek(&start, Bias::Right, &());
1126        while let Some(excerpt) = cursor.item() {
1127            if *cursor.start() > end {
1128                break;
1129            }
1130
1131            let mut end_before_newline = cursor.end(&());
1132            if excerpt.has_trailing_newline {
1133                end_before_newline -= 1;
1134            }
1135            let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
1136            let start = excerpt_start + (cmp::max(start, *cursor.start()) - *cursor.start());
1137            let end = excerpt_start + (cmp::min(end, end_before_newline) - *cursor.start());
1138            let buffer = self.buffers.borrow()[&excerpt.buffer_id].buffer.clone();
1139            result.push((buffer, start..end));
1140            cursor.next(&());
1141        }
1142
1143        result
1144    }
1145
1146    pub fn remove_excerpts(
1147        &mut self,
1148        excerpt_ids: impl IntoIterator<Item = ExcerptId>,
1149        cx: &mut ModelContext<Self>,
1150    ) {
1151        self.sync(cx);
1152        let ids = excerpt_ids.into_iter().collect::<Vec<_>>();
1153
1154        let mut buffers = self.buffers.borrow_mut();
1155        let mut snapshot = self.snapshot.borrow_mut();
1156        let mut new_excerpts = SumTree::new();
1157        let mut cursor = snapshot.excerpts.cursor::<(Option<&Locator>, usize)>();
1158        let mut edits = Vec::new();
1159        let mut excerpt_ids = ids.iter().copied().peekable();
1160
1161        while let Some(excerpt_id) = excerpt_ids.next() {
1162            // Seek to the next excerpt to remove, preserving any preceding excerpts.
1163            let locator = snapshot.excerpt_locator_for_id(excerpt_id);
1164            new_excerpts.push_tree(cursor.slice(&Some(locator), Bias::Left, &()), &());
1165
1166            if let Some(mut excerpt) = cursor.item() {
1167                if excerpt.id != excerpt_id {
1168                    continue;
1169                }
1170                let mut old_start = cursor.start().1;
1171
1172                // Skip over the removed excerpt.
1173                'remove_excerpts: loop {
1174                    if let Some(buffer_state) = buffers.get_mut(&excerpt.buffer_id) {
1175                        buffer_state.excerpts.retain(|l| l != &excerpt.locator);
1176                        if buffer_state.excerpts.is_empty() {
1177                            buffers.remove(&excerpt.buffer_id);
1178                        }
1179                    }
1180                    cursor.next(&());
1181
1182                    // Skip over any subsequent excerpts that are also removed.
1183                    while let Some(&next_excerpt_id) = excerpt_ids.peek() {
1184                        let next_locator = snapshot.excerpt_locator_for_id(next_excerpt_id);
1185                        if let Some(next_excerpt) = cursor.item() {
1186                            if next_excerpt.locator == *next_locator {
1187                                excerpt_ids.next();
1188                                excerpt = next_excerpt;
1189                                continue 'remove_excerpts;
1190                            }
1191                        }
1192                        break;
1193                    }
1194
1195                    break;
1196                }
1197
1198                // When removing the last excerpt, remove the trailing newline from
1199                // the previous excerpt.
1200                if cursor.item().is_none() && old_start > 0 {
1201                    old_start -= 1;
1202                    new_excerpts.update_last(|e| e.has_trailing_newline = false, &());
1203                }
1204
1205                // Push an edit for the removal of this run of excerpts.
1206                let old_end = cursor.start().1;
1207                let new_start = new_excerpts.summary().text.len;
1208                edits.push(Edit {
1209                    old: old_start..old_end,
1210                    new: new_start..new_start,
1211                });
1212            }
1213        }
1214        let suffix = cursor.suffix(&());
1215        let changed_trailing_excerpt = suffix.is_empty();
1216        new_excerpts.push_tree(suffix, &());
1217        drop(cursor);
1218        snapshot.excerpts = new_excerpts;
1219
1220        if changed_trailing_excerpt {
1221            snapshot.trailing_excerpt_update_count += 1;
1222        }
1223
1224        self.subscriptions.publish_mut(edits);
1225        cx.emit(Event::Edited);
1226        cx.emit(Event::ExcerptsRemoved { ids });
1227        cx.notify();
1228    }
1229
1230    pub fn text_anchor_for_position<T: ToOffset>(
1231        &self,
1232        position: T,
1233        cx: &AppContext,
1234    ) -> Option<(ModelHandle<Buffer>, language::Anchor)> {
1235        let snapshot = self.read(cx);
1236        let anchor = snapshot.anchor_before(position);
1237        let buffer = self
1238            .buffers
1239            .borrow()
1240            .get(&anchor.buffer_id?)?
1241            .buffer
1242            .clone();
1243        Some((buffer, anchor.text_anchor))
1244    }
1245
1246    fn on_buffer_event(
1247        &mut self,
1248        _: ModelHandle<Buffer>,
1249        event: &language::Event,
1250        cx: &mut ModelContext<Self>,
1251    ) {
1252        cx.emit(match event {
1253            language::Event::Edited => Event::Edited,
1254            language::Event::DirtyChanged => Event::DirtyChanged,
1255            language::Event::Saved => Event::Saved,
1256            language::Event::FileHandleChanged => Event::FileHandleChanged,
1257            language::Event::Reloaded => Event::Reloaded,
1258            language::Event::Reparsed => Event::Reparsed,
1259            language::Event::DiagnosticsUpdated => Event::DiagnosticsUpdated,
1260            language::Event::Closed => Event::Closed,
1261
1262            //
1263            language::Event::Operation(_) => return,
1264        });
1265    }
1266
1267    pub fn all_buffers(&self) -> HashSet<ModelHandle<Buffer>> {
1268        self.buffers
1269            .borrow()
1270            .values()
1271            .map(|state| state.buffer.clone())
1272            .collect()
1273    }
1274
1275    pub fn buffer(&self, buffer_id: usize) -> Option<ModelHandle<Buffer>> {
1276        self.buffers
1277            .borrow()
1278            .get(&buffer_id)
1279            .map(|state| state.buffer.clone())
1280    }
1281
1282    pub fn save(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
1283        let mut save_tasks = Vec::new();
1284        for BufferState { buffer, .. } in self.buffers.borrow().values() {
1285            save_tasks.push(buffer.update(cx, |buffer, cx| buffer.save(cx)));
1286        }
1287
1288        cx.spawn(|_, _| async move {
1289            for save in save_tasks {
1290                save.await?;
1291            }
1292            Ok(())
1293        })
1294    }
1295
1296    pub fn is_completion_trigger<T>(&self, position: T, text: &str, cx: &AppContext) -> bool
1297    where
1298        T: ToOffset,
1299    {
1300        let mut chars = text.chars();
1301        let char = if let Some(char) = chars.next() {
1302            char
1303        } else {
1304            return false;
1305        };
1306        if chars.next().is_some() {
1307            return false;
1308        }
1309
1310        if char.is_alphanumeric() || char == '_' {
1311            return true;
1312        }
1313
1314        let snapshot = self.snapshot(cx);
1315        let anchor = snapshot.anchor_before(position);
1316        anchor
1317            .buffer_id
1318            .and_then(|buffer_id| {
1319                let buffer = self.buffers.borrow().get(&buffer_id)?.buffer.clone();
1320                Some(
1321                    buffer
1322                        .read(cx)
1323                        .completion_triggers()
1324                        .iter()
1325                        .any(|string| string == text),
1326                )
1327            })
1328            .unwrap_or(false)
1329    }
1330
1331    pub fn language_at<'a, T: ToOffset>(
1332        &self,
1333        point: T,
1334        cx: &'a AppContext,
1335    ) -> Option<Arc<Language>> {
1336        self.point_to_buffer_offset(point, cx)
1337            .and_then(|(buffer, offset)| buffer.read(cx).language_at(offset))
1338    }
1339
1340    pub fn for_each_buffer(&self, mut f: impl FnMut(&ModelHandle<Buffer>)) {
1341        self.buffers
1342            .borrow()
1343            .values()
1344            .for_each(|state| f(&state.buffer))
1345    }
1346
1347    pub fn title<'a>(&'a self, cx: &'a AppContext) -> Cow<'a, str> {
1348        if let Some(title) = self.title.as_ref() {
1349            return title.into();
1350        }
1351
1352        if let Some(buffer) = self.as_singleton() {
1353            if let Some(file) = buffer.read(cx).file() {
1354                return file.file_name(cx).to_string_lossy();
1355            }
1356        }
1357
1358        "untitled".into()
1359    }
1360
1361    #[cfg(test)]
1362    pub fn is_parsing(&self, cx: &AppContext) -> bool {
1363        self.as_singleton().unwrap().read(cx).is_parsing()
1364    }
1365
1366    fn sync(&self, cx: &AppContext) {
1367        let mut snapshot = self.snapshot.borrow_mut();
1368        let mut excerpts_to_edit = Vec::new();
1369        let mut reparsed = false;
1370        let mut diagnostics_updated = false;
1371        let mut git_diff_updated = false;
1372        let mut is_dirty = false;
1373        let mut has_conflict = false;
1374        let mut edited = false;
1375        let mut buffers = self.buffers.borrow_mut();
1376        for buffer_state in buffers.values_mut() {
1377            let buffer = buffer_state.buffer.read(cx);
1378            let version = buffer.version();
1379            let parse_count = buffer.parse_count();
1380            let selections_update_count = buffer.selections_update_count();
1381            let diagnostics_update_count = buffer.diagnostics_update_count();
1382            let file_update_count = buffer.file_update_count();
1383            let git_diff_update_count = buffer.git_diff_update_count();
1384
1385            let buffer_edited = version.changed_since(&buffer_state.last_version);
1386            let buffer_reparsed = parse_count > buffer_state.last_parse_count;
1387            let buffer_selections_updated =
1388                selections_update_count > buffer_state.last_selections_update_count;
1389            let buffer_diagnostics_updated =
1390                diagnostics_update_count > buffer_state.last_diagnostics_update_count;
1391            let buffer_file_updated = file_update_count > buffer_state.last_file_update_count;
1392            let buffer_git_diff_updated =
1393                git_diff_update_count > buffer_state.last_git_diff_update_count;
1394            if buffer_edited
1395                || buffer_reparsed
1396                || buffer_selections_updated
1397                || buffer_diagnostics_updated
1398                || buffer_file_updated
1399                || buffer_git_diff_updated
1400            {
1401                buffer_state.last_version = version;
1402                buffer_state.last_parse_count = parse_count;
1403                buffer_state.last_selections_update_count = selections_update_count;
1404                buffer_state.last_diagnostics_update_count = diagnostics_update_count;
1405                buffer_state.last_file_update_count = file_update_count;
1406                buffer_state.last_git_diff_update_count = git_diff_update_count;
1407                excerpts_to_edit.extend(
1408                    buffer_state
1409                        .excerpts
1410                        .iter()
1411                        .map(|locator| (locator, buffer_state.buffer.clone(), buffer_edited)),
1412                );
1413            }
1414
1415            edited |= buffer_edited;
1416            reparsed |= buffer_reparsed;
1417            diagnostics_updated |= buffer_diagnostics_updated;
1418            git_diff_updated |= buffer_git_diff_updated;
1419            is_dirty |= buffer.is_dirty();
1420            has_conflict |= buffer.has_conflict();
1421        }
1422        if edited {
1423            snapshot.edit_count += 1;
1424        }
1425        if reparsed {
1426            snapshot.parse_count += 1;
1427        }
1428        if diagnostics_updated {
1429            snapshot.diagnostics_update_count += 1;
1430        }
1431        if git_diff_updated {
1432            snapshot.git_diff_update_count += 1;
1433        }
1434        snapshot.is_dirty = is_dirty;
1435        snapshot.has_conflict = has_conflict;
1436
1437        excerpts_to_edit.sort_unstable_by_key(|(locator, _, _)| *locator);
1438
1439        let mut edits = Vec::new();
1440        let mut new_excerpts = SumTree::new();
1441        let mut cursor = snapshot.excerpts.cursor::<(Option<&Locator>, usize)>();
1442
1443        for (locator, buffer, buffer_edited) in excerpts_to_edit {
1444            new_excerpts.push_tree(cursor.slice(&Some(locator), Bias::Left, &()), &());
1445            let old_excerpt = cursor.item().unwrap();
1446            let buffer_id = buffer.id();
1447            let buffer = buffer.read(cx);
1448
1449            let mut new_excerpt;
1450            if buffer_edited {
1451                edits.extend(
1452                    buffer
1453                        .edits_since_in_range::<usize>(
1454                            old_excerpt.buffer.version(),
1455                            old_excerpt.range.context.clone(),
1456                        )
1457                        .map(|mut edit| {
1458                            let excerpt_old_start = cursor.start().1;
1459                            let excerpt_new_start = new_excerpts.summary().text.len;
1460                            edit.old.start += excerpt_old_start;
1461                            edit.old.end += excerpt_old_start;
1462                            edit.new.start += excerpt_new_start;
1463                            edit.new.end += excerpt_new_start;
1464                            edit
1465                        }),
1466                );
1467
1468                new_excerpt = Excerpt::new(
1469                    old_excerpt.id,
1470                    locator.clone(),
1471                    buffer_id,
1472                    buffer.snapshot(),
1473                    old_excerpt.range.clone(),
1474                    old_excerpt.has_trailing_newline,
1475                );
1476            } else {
1477                new_excerpt = old_excerpt.clone();
1478                new_excerpt.buffer = buffer.snapshot();
1479            }
1480
1481            new_excerpts.push(new_excerpt, &());
1482            cursor.next(&());
1483        }
1484        new_excerpts.push_tree(cursor.suffix(&()), &());
1485
1486        drop(cursor);
1487        snapshot.excerpts = new_excerpts;
1488
1489        self.subscriptions.publish(edits);
1490    }
1491}
1492
1493#[cfg(any(test, feature = "test-support"))]
1494impl MultiBuffer {
1495    pub fn build_simple(text: &str, cx: &mut gpui::MutableAppContext) -> ModelHandle<Self> {
1496        let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
1497        cx.add_model(|cx| Self::singleton(buffer, cx))
1498    }
1499
1500    pub fn build_random(
1501        rng: &mut impl rand::Rng,
1502        cx: &mut gpui::MutableAppContext,
1503    ) -> ModelHandle<Self> {
1504        cx.add_model(|cx| {
1505            let mut multibuffer = MultiBuffer::new(0);
1506            let mutation_count = rng.gen_range(1..=5);
1507            multibuffer.randomly_edit_excerpts(rng, mutation_count, cx);
1508            multibuffer
1509        })
1510    }
1511
1512    pub fn randomly_edit(
1513        &mut self,
1514        rng: &mut impl rand::Rng,
1515        edit_count: usize,
1516        cx: &mut ModelContext<Self>,
1517    ) {
1518        use util::RandomCharIter;
1519
1520        let snapshot = self.read(cx);
1521        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1522        let mut last_end = None;
1523        for _ in 0..edit_count {
1524            if last_end.map_or(false, |last_end| last_end >= snapshot.len()) {
1525                break;
1526            }
1527
1528            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1529            let end = snapshot.clip_offset(rng.gen_range(new_start..=snapshot.len()), Bias::Right);
1530            let start = snapshot.clip_offset(rng.gen_range(new_start..=end), Bias::Right);
1531            last_end = Some(end);
1532
1533            let mut range = start..end;
1534            if rng.gen_bool(0.2) {
1535                mem::swap(&mut range.start, &mut range.end);
1536            }
1537
1538            let new_text_len = rng.gen_range(0..10);
1539            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1540
1541            edits.push((range, new_text.into()));
1542        }
1543        log::info!("mutating multi-buffer with {:?}", edits);
1544        drop(snapshot);
1545
1546        self.edit(edits, None, cx);
1547    }
1548
1549    pub fn randomly_edit_excerpts(
1550        &mut self,
1551        rng: &mut impl rand::Rng,
1552        mutation_count: usize,
1553        cx: &mut ModelContext<Self>,
1554    ) {
1555        use rand::prelude::*;
1556        use std::env;
1557        use util::RandomCharIter;
1558
1559        let max_excerpts = env::var("MAX_EXCERPTS")
1560            .map(|i| i.parse().expect("invalid `MAX_EXCERPTS` variable"))
1561            .unwrap_or(5);
1562
1563        let mut buffers = Vec::new();
1564        for _ in 0..mutation_count {
1565            if rng.gen_bool(0.05) {
1566                log::info!("Clearing multi-buffer");
1567                self.clear(cx);
1568                continue;
1569            }
1570
1571            let excerpt_ids = self.excerpt_ids();
1572            if excerpt_ids.is_empty() || (rng.gen() && excerpt_ids.len() < max_excerpts) {
1573                let buffer_handle = if rng.gen() || self.buffers.borrow().is_empty() {
1574                    let text = RandomCharIter::new(&mut *rng).take(10).collect::<String>();
1575                    buffers.push(cx.add_model(|cx| Buffer::new(0, text, cx)));
1576                    let buffer = buffers.last().unwrap();
1577                    log::info!(
1578                        "Creating new buffer {} with text: {:?}",
1579                        buffer.id(),
1580                        buffer.read(cx).text()
1581                    );
1582                    buffers.last().unwrap().clone()
1583                } else {
1584                    self.buffers
1585                        .borrow()
1586                        .values()
1587                        .choose(rng)
1588                        .unwrap()
1589                        .buffer
1590                        .clone()
1591                };
1592
1593                let buffer = buffer_handle.read(cx);
1594                let buffer_text = buffer.text();
1595                let ranges = (0..rng.gen_range(0..5))
1596                    .map(|_| {
1597                        let end_ix =
1598                            buffer.clip_offset(rng.gen_range(0..=buffer.len()), Bias::Right);
1599                        let start_ix = buffer.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
1600                        ExcerptRange {
1601                            context: start_ix..end_ix,
1602                            primary: None,
1603                        }
1604                    })
1605                    .collect::<Vec<_>>();
1606                log::info!(
1607                    "Inserting excerpts from buffer {} and ranges {:?}: {:?}",
1608                    buffer_handle.id(),
1609                    ranges.iter().map(|r| &r.context).collect::<Vec<_>>(),
1610                    ranges
1611                        .iter()
1612                        .map(|r| &buffer_text[r.context.clone()])
1613                        .collect::<Vec<_>>()
1614                );
1615
1616                let excerpt_id = self.push_excerpts(buffer_handle.clone(), ranges, cx);
1617                log::info!("Inserted with ids: {:?}", excerpt_id);
1618            } else {
1619                let remove_count = rng.gen_range(1..=excerpt_ids.len());
1620                let mut excerpts_to_remove = excerpt_ids
1621                    .choose_multiple(rng, remove_count)
1622                    .cloned()
1623                    .collect::<Vec<_>>();
1624                let snapshot = self.snapshot.borrow();
1625                excerpts_to_remove.sort_unstable_by(|a, b| a.cmp(b, &*snapshot));
1626                drop(snapshot);
1627                log::info!("Removing excerpts {:?}", excerpts_to_remove);
1628                self.remove_excerpts(excerpts_to_remove, cx);
1629            }
1630        }
1631    }
1632
1633    pub fn randomly_mutate(
1634        &mut self,
1635        rng: &mut impl rand::Rng,
1636        mutation_count: usize,
1637        cx: &mut ModelContext<Self>,
1638    ) {
1639        use rand::prelude::*;
1640
1641        if rng.gen_bool(0.7) || self.singleton {
1642            let buffer = self
1643                .buffers
1644                .borrow()
1645                .values()
1646                .choose(rng)
1647                .map(|state| state.buffer.clone());
1648
1649            if let Some(buffer) = buffer {
1650                buffer.update(cx, |buffer, cx| {
1651                    if rng.gen() {
1652                        buffer.randomly_edit(rng, mutation_count, cx);
1653                    } else {
1654                        buffer.randomly_undo_redo(rng, cx);
1655                    }
1656                });
1657            } else {
1658                self.randomly_edit(rng, mutation_count, cx);
1659            }
1660        } else {
1661            self.randomly_edit_excerpts(rng, mutation_count, cx);
1662        }
1663
1664        self.check_invariants(cx);
1665    }
1666
1667    fn check_invariants(&self, cx: &mut ModelContext<Self>) {
1668        let snapshot = self.read(cx);
1669        let excerpts = snapshot.excerpts.items(&());
1670        let excerpt_ids = snapshot.excerpt_ids.items(&());
1671
1672        for (ix, excerpt) in excerpts.iter().enumerate() {
1673            if ix == 0 {
1674                if excerpt.locator <= Locator::min() {
1675                    panic!("invalid first excerpt locator {:?}", excerpt.locator);
1676                }
1677            } else {
1678                if excerpt.locator <= excerpts[ix - 1].locator {
1679                    panic!("excerpts are out-of-order: {:?}", excerpts);
1680                }
1681            }
1682        }
1683
1684        for (ix, entry) in excerpt_ids.iter().enumerate() {
1685            if ix == 0 {
1686                if entry.id.cmp(&ExcerptId::min(), &*snapshot).is_le() {
1687                    panic!("invalid first excerpt id {:?}", entry.id);
1688                }
1689            } else {
1690                if entry.id <= excerpt_ids[ix - 1].id {
1691                    panic!("excerpt ids are out-of-order: {:?}", excerpt_ids);
1692                }
1693            }
1694        }
1695    }
1696}
1697
1698impl Entity for MultiBuffer {
1699    type Event = Event;
1700}
1701
1702impl MultiBufferSnapshot {
1703    pub fn text(&self) -> String {
1704        self.chunks(0..self.len(), false)
1705            .map(|chunk| chunk.text)
1706            .collect()
1707    }
1708
1709    pub fn reversed_chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
1710        let mut offset = position.to_offset(self);
1711        let mut cursor = self.excerpts.cursor::<usize>();
1712        cursor.seek(&offset, Bias::Left, &());
1713        let mut excerpt_chunks = cursor.item().map(|excerpt| {
1714            let end_before_footer = cursor.start() + excerpt.text_summary.len;
1715            let start = excerpt.range.context.start.to_offset(&excerpt.buffer);
1716            let end = start + (cmp::min(offset, end_before_footer) - cursor.start());
1717            excerpt.buffer.reversed_chunks_in_range(start..end)
1718        });
1719        iter::from_fn(move || {
1720            if offset == *cursor.start() {
1721                cursor.prev(&());
1722                let excerpt = cursor.item()?;
1723                excerpt_chunks = Some(
1724                    excerpt
1725                        .buffer
1726                        .reversed_chunks_in_range(excerpt.range.context.clone()),
1727                );
1728            }
1729
1730            let excerpt = cursor.item().unwrap();
1731            if offset == cursor.end(&()) && excerpt.has_trailing_newline {
1732                offset -= 1;
1733                Some("\n")
1734            } else {
1735                let chunk = excerpt_chunks.as_mut().unwrap().next().unwrap();
1736                offset -= chunk.len();
1737                Some(chunk)
1738            }
1739        })
1740        .flat_map(|c| c.chars().rev())
1741    }
1742
1743    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
1744        let offset = position.to_offset(self);
1745        self.text_for_range(offset..self.len())
1746            .flat_map(|chunk| chunk.chars())
1747    }
1748
1749    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = &str> + '_ {
1750        self.chunks(range, false).map(|chunk| chunk.text)
1751    }
1752
1753    pub fn is_line_blank(&self, row: u32) -> bool {
1754        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1755            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1756    }
1757
1758    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1759    where
1760        T: ToOffset,
1761    {
1762        let position = position.to_offset(self);
1763        position == self.clip_offset(position, Bias::Left)
1764            && self
1765                .bytes_in_range(position..self.len())
1766                .flatten()
1767                .copied()
1768                .take(needle.len())
1769                .eq(needle.bytes())
1770    }
1771
1772    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
1773        let mut start = start.to_offset(self);
1774        let mut end = start;
1775        let mut next_chars = self.chars_at(start).peekable();
1776        let mut prev_chars = self.reversed_chars_at(start).peekable();
1777        let word_kind = cmp::max(
1778            prev_chars.peek().copied().map(char_kind),
1779            next_chars.peek().copied().map(char_kind),
1780        );
1781
1782        for ch in prev_chars {
1783            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1784                start -= ch.len_utf8();
1785            } else {
1786                break;
1787            }
1788        }
1789
1790        for ch in next_chars {
1791            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1792                end += ch.len_utf8();
1793            } else {
1794                break;
1795            }
1796        }
1797
1798        (start..end, word_kind)
1799    }
1800
1801    pub fn as_singleton(&self) -> Option<(&ExcerptId, usize, &BufferSnapshot)> {
1802        if self.singleton {
1803            self.excerpts
1804                .iter()
1805                .next()
1806                .map(|e| (&e.id, e.buffer_id, &e.buffer))
1807        } else {
1808            None
1809        }
1810    }
1811
1812    pub fn len(&self) -> usize {
1813        self.excerpts.summary().text.len
1814    }
1815
1816    pub fn is_empty(&self) -> bool {
1817        self.excerpts.summary().text.len == 0
1818    }
1819
1820    pub fn max_buffer_row(&self) -> u32 {
1821        self.excerpts.summary().max_buffer_row
1822    }
1823
1824    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1825        if let Some((_, _, buffer)) = self.as_singleton() {
1826            return buffer.clip_offset(offset, bias);
1827        }
1828
1829        let mut cursor = self.excerpts.cursor::<usize>();
1830        cursor.seek(&offset, Bias::Right, &());
1831        let overshoot = if let Some(excerpt) = cursor.item() {
1832            let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
1833            let buffer_offset = excerpt
1834                .buffer
1835                .clip_offset(excerpt_start + (offset - cursor.start()), bias);
1836            buffer_offset.saturating_sub(excerpt_start)
1837        } else {
1838            0
1839        };
1840        cursor.start() + overshoot
1841    }
1842
1843    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1844        if let Some((_, _, buffer)) = self.as_singleton() {
1845            return buffer.clip_point(point, bias);
1846        }
1847
1848        let mut cursor = self.excerpts.cursor::<Point>();
1849        cursor.seek(&point, Bias::Right, &());
1850        let overshoot = if let Some(excerpt) = cursor.item() {
1851            let excerpt_start = excerpt.range.context.start.to_point(&excerpt.buffer);
1852            let buffer_point = excerpt
1853                .buffer
1854                .clip_point(excerpt_start + (point - cursor.start()), bias);
1855            buffer_point.saturating_sub(excerpt_start)
1856        } else {
1857            Point::zero()
1858        };
1859        *cursor.start() + overshoot
1860    }
1861
1862    pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
1863        if let Some((_, _, buffer)) = self.as_singleton() {
1864            return buffer.clip_offset_utf16(offset, bias);
1865        }
1866
1867        let mut cursor = self.excerpts.cursor::<OffsetUtf16>();
1868        cursor.seek(&offset, Bias::Right, &());
1869        let overshoot = if let Some(excerpt) = cursor.item() {
1870            let excerpt_start = excerpt.range.context.start.to_offset_utf16(&excerpt.buffer);
1871            let buffer_offset = excerpt
1872                .buffer
1873                .clip_offset_utf16(excerpt_start + (offset - cursor.start()), bias);
1874            OffsetUtf16(buffer_offset.0.saturating_sub(excerpt_start.0))
1875        } else {
1876            OffsetUtf16(0)
1877        };
1878        *cursor.start() + overshoot
1879    }
1880
1881    pub fn clip_point_utf16(&self, point: Unclipped<PointUtf16>, bias: Bias) -> PointUtf16 {
1882        if let Some((_, _, buffer)) = self.as_singleton() {
1883            return buffer.clip_point_utf16(point, bias);
1884        }
1885
1886        let mut cursor = self.excerpts.cursor::<PointUtf16>();
1887        cursor.seek(&point.0, Bias::Right, &());
1888        let overshoot = if let Some(excerpt) = cursor.item() {
1889            let excerpt_start = excerpt
1890                .buffer
1891                .offset_to_point_utf16(excerpt.range.context.start.to_offset(&excerpt.buffer));
1892            let buffer_point = excerpt
1893                .buffer
1894                .clip_point_utf16(Unclipped(excerpt_start + (point.0 - cursor.start())), bias);
1895            buffer_point.saturating_sub(excerpt_start)
1896        } else {
1897            PointUtf16::zero()
1898        };
1899        *cursor.start() + overshoot
1900    }
1901
1902    pub fn bytes_in_range<T: ToOffset>(&self, range: Range<T>) -> MultiBufferBytes {
1903        let range = range.start.to_offset(self)..range.end.to_offset(self);
1904        let mut excerpts = self.excerpts.cursor::<usize>();
1905        excerpts.seek(&range.start, Bias::Right, &());
1906
1907        let mut chunk = &[][..];
1908        let excerpt_bytes = if let Some(excerpt) = excerpts.item() {
1909            let mut excerpt_bytes = excerpt
1910                .bytes_in_range(range.start - excerpts.start()..range.end - excerpts.start());
1911            chunk = excerpt_bytes.next().unwrap_or(&[][..]);
1912            Some(excerpt_bytes)
1913        } else {
1914            None
1915        };
1916
1917        MultiBufferBytes {
1918            range,
1919            excerpts,
1920            excerpt_bytes,
1921            chunk,
1922        }
1923    }
1924
1925    pub fn buffer_rows(&self, start_row: u32) -> MultiBufferRows {
1926        let mut result = MultiBufferRows {
1927            buffer_row_range: 0..0,
1928            excerpts: self.excerpts.cursor(),
1929        };
1930        result.seek(start_row);
1931        result
1932    }
1933
1934    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> MultiBufferChunks {
1935        let range = range.start.to_offset(self)..range.end.to_offset(self);
1936        let mut chunks = MultiBufferChunks {
1937            range: range.clone(),
1938            excerpts: self.excerpts.cursor(),
1939            excerpt_chunks: None,
1940            language_aware,
1941        };
1942        chunks.seek(range.start);
1943        chunks
1944    }
1945
1946    pub fn offset_to_point(&self, offset: usize) -> Point {
1947        if let Some((_, _, buffer)) = self.as_singleton() {
1948            return buffer.offset_to_point(offset);
1949        }
1950
1951        let mut cursor = self.excerpts.cursor::<(usize, Point)>();
1952        cursor.seek(&offset, Bias::Right, &());
1953        if let Some(excerpt) = cursor.item() {
1954            let (start_offset, start_point) = cursor.start();
1955            let overshoot = offset - start_offset;
1956            let excerpt_start_offset = excerpt.range.context.start.to_offset(&excerpt.buffer);
1957            let excerpt_start_point = excerpt.range.context.start.to_point(&excerpt.buffer);
1958            let buffer_point = excerpt
1959                .buffer
1960                .offset_to_point(excerpt_start_offset + overshoot);
1961            *start_point + (buffer_point - excerpt_start_point)
1962        } else {
1963            self.excerpts.summary().text.lines
1964        }
1965    }
1966
1967    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
1968        if let Some((_, _, buffer)) = self.as_singleton() {
1969            return buffer.offset_to_point_utf16(offset);
1970        }
1971
1972        let mut cursor = self.excerpts.cursor::<(usize, PointUtf16)>();
1973        cursor.seek(&offset, Bias::Right, &());
1974        if let Some(excerpt) = cursor.item() {
1975            let (start_offset, start_point) = cursor.start();
1976            let overshoot = offset - start_offset;
1977            let excerpt_start_offset = excerpt.range.context.start.to_offset(&excerpt.buffer);
1978            let excerpt_start_point = excerpt.range.context.start.to_point_utf16(&excerpt.buffer);
1979            let buffer_point = excerpt
1980                .buffer
1981                .offset_to_point_utf16(excerpt_start_offset + overshoot);
1982            *start_point + (buffer_point - excerpt_start_point)
1983        } else {
1984            self.excerpts.summary().text.lines_utf16()
1985        }
1986    }
1987
1988    pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
1989        if let Some((_, _, buffer)) = self.as_singleton() {
1990            return buffer.point_to_point_utf16(point);
1991        }
1992
1993        let mut cursor = self.excerpts.cursor::<(Point, PointUtf16)>();
1994        cursor.seek(&point, Bias::Right, &());
1995        if let Some(excerpt) = cursor.item() {
1996            let (start_offset, start_point) = cursor.start();
1997            let overshoot = point - start_offset;
1998            let excerpt_start_point = excerpt.range.context.start.to_point(&excerpt.buffer);
1999            let excerpt_start_point_utf16 =
2000                excerpt.range.context.start.to_point_utf16(&excerpt.buffer);
2001            let buffer_point = excerpt
2002                .buffer
2003                .point_to_point_utf16(excerpt_start_point + overshoot);
2004            *start_point + (buffer_point - excerpt_start_point_utf16)
2005        } else {
2006            self.excerpts.summary().text.lines_utf16()
2007        }
2008    }
2009
2010    pub fn point_to_offset(&self, point: Point) -> usize {
2011        if let Some((_, _, buffer)) = self.as_singleton() {
2012            return buffer.point_to_offset(point);
2013        }
2014
2015        let mut cursor = self.excerpts.cursor::<(Point, usize)>();
2016        cursor.seek(&point, Bias::Right, &());
2017        if let Some(excerpt) = cursor.item() {
2018            let (start_point, start_offset) = cursor.start();
2019            let overshoot = point - start_point;
2020            let excerpt_start_offset = excerpt.range.context.start.to_offset(&excerpt.buffer);
2021            let excerpt_start_point = excerpt.range.context.start.to_point(&excerpt.buffer);
2022            let buffer_offset = excerpt
2023                .buffer
2024                .point_to_offset(excerpt_start_point + overshoot);
2025            *start_offset + buffer_offset - excerpt_start_offset
2026        } else {
2027            self.excerpts.summary().text.len
2028        }
2029    }
2030
2031    pub fn offset_utf16_to_offset(&self, offset_utf16: OffsetUtf16) -> usize {
2032        if let Some((_, _, buffer)) = self.as_singleton() {
2033            return buffer.offset_utf16_to_offset(offset_utf16);
2034        }
2035
2036        let mut cursor = self.excerpts.cursor::<(OffsetUtf16, usize)>();
2037        cursor.seek(&offset_utf16, Bias::Right, &());
2038        if let Some(excerpt) = cursor.item() {
2039            let (start_offset_utf16, start_offset) = cursor.start();
2040            let overshoot = offset_utf16 - start_offset_utf16;
2041            let excerpt_start_offset = excerpt.range.context.start.to_offset(&excerpt.buffer);
2042            let excerpt_start_offset_utf16 =
2043                excerpt.buffer.offset_to_offset_utf16(excerpt_start_offset);
2044            let buffer_offset = excerpt
2045                .buffer
2046                .offset_utf16_to_offset(excerpt_start_offset_utf16 + overshoot);
2047            *start_offset + (buffer_offset - excerpt_start_offset)
2048        } else {
2049            self.excerpts.summary().text.len
2050        }
2051    }
2052
2053    pub fn offset_to_offset_utf16(&self, offset: usize) -> OffsetUtf16 {
2054        if let Some((_, _, buffer)) = self.as_singleton() {
2055            return buffer.offset_to_offset_utf16(offset);
2056        }
2057
2058        let mut cursor = self.excerpts.cursor::<(usize, OffsetUtf16)>();
2059        cursor.seek(&offset, Bias::Right, &());
2060        if let Some(excerpt) = cursor.item() {
2061            let (start_offset, start_offset_utf16) = cursor.start();
2062            let overshoot = offset - start_offset;
2063            let excerpt_start_offset_utf16 =
2064                excerpt.range.context.start.to_offset_utf16(&excerpt.buffer);
2065            let excerpt_start_offset = excerpt
2066                .buffer
2067                .offset_utf16_to_offset(excerpt_start_offset_utf16);
2068            let buffer_offset_utf16 = excerpt
2069                .buffer
2070                .offset_to_offset_utf16(excerpt_start_offset + overshoot);
2071            *start_offset_utf16 + (buffer_offset_utf16 - excerpt_start_offset_utf16)
2072        } else {
2073            self.excerpts.summary().text.len_utf16
2074        }
2075    }
2076
2077    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
2078        if let Some((_, _, buffer)) = self.as_singleton() {
2079            return buffer.point_utf16_to_offset(point);
2080        }
2081
2082        let mut cursor = self.excerpts.cursor::<(PointUtf16, usize)>();
2083        cursor.seek(&point, Bias::Right, &());
2084        if let Some(excerpt) = cursor.item() {
2085            let (start_point, start_offset) = cursor.start();
2086            let overshoot = point - start_point;
2087            let excerpt_start_offset = excerpt.range.context.start.to_offset(&excerpt.buffer);
2088            let excerpt_start_point = excerpt
2089                .buffer
2090                .offset_to_point_utf16(excerpt.range.context.start.to_offset(&excerpt.buffer));
2091            let buffer_offset = excerpt
2092                .buffer
2093                .point_utf16_to_offset(excerpt_start_point + overshoot);
2094            *start_offset + (buffer_offset - excerpt_start_offset)
2095        } else {
2096            self.excerpts.summary().text.len
2097        }
2098    }
2099
2100    pub fn point_to_buffer_offset<T: ToOffset>(
2101        &self,
2102        point: T,
2103    ) -> Option<(&BufferSnapshot, usize)> {
2104        let offset = point.to_offset(&self);
2105        let mut cursor = self.excerpts.cursor::<usize>();
2106        cursor.seek(&offset, Bias::Right, &());
2107        if cursor.item().is_none() {
2108            cursor.prev(&());
2109        }
2110
2111        cursor.item().map(|excerpt| {
2112            let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
2113            let buffer_point = excerpt_start + offset - *cursor.start();
2114            (&excerpt.buffer, buffer_point)
2115        })
2116    }
2117
2118    pub fn suggested_indents(
2119        &self,
2120        rows: impl IntoIterator<Item = u32>,
2121        cx: &AppContext,
2122    ) -> BTreeMap<u32, IndentSize> {
2123        let mut result = BTreeMap::new();
2124
2125        let mut rows_for_excerpt = Vec::new();
2126        let mut cursor = self.excerpts.cursor::<Point>();
2127        let mut rows = rows.into_iter().peekable();
2128        let mut prev_row = u32::MAX;
2129        let mut prev_language_indent_size = IndentSize::default();
2130
2131        while let Some(row) = rows.next() {
2132            cursor.seek(&Point::new(row, 0), Bias::Right, &());
2133            let excerpt = match cursor.item() {
2134                Some(excerpt) => excerpt,
2135                _ => continue,
2136            };
2137
2138            // Retrieve the language and indent size once for each disjoint region being indented.
2139            let single_indent_size = if row.saturating_sub(1) == prev_row {
2140                prev_language_indent_size
2141            } else {
2142                excerpt
2143                    .buffer
2144                    .language_indent_size_at(Point::new(row, 0), cx)
2145            };
2146            prev_language_indent_size = single_indent_size;
2147            prev_row = row;
2148
2149            let start_buffer_row = excerpt.range.context.start.to_point(&excerpt.buffer).row;
2150            let start_multibuffer_row = cursor.start().row;
2151
2152            rows_for_excerpt.push(row);
2153            while let Some(next_row) = rows.peek().copied() {
2154                if cursor.end(&()).row > next_row {
2155                    rows_for_excerpt.push(next_row);
2156                    rows.next();
2157                } else {
2158                    break;
2159                }
2160            }
2161
2162            let buffer_rows = rows_for_excerpt
2163                .drain(..)
2164                .map(|row| start_buffer_row + row - start_multibuffer_row);
2165            let buffer_indents = excerpt
2166                .buffer
2167                .suggested_indents(buffer_rows, single_indent_size);
2168            let multibuffer_indents = buffer_indents
2169                .into_iter()
2170                .map(|(row, indent)| (start_multibuffer_row + row - start_buffer_row, indent));
2171            result.extend(multibuffer_indents);
2172        }
2173
2174        result
2175    }
2176
2177    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
2178        if let Some((buffer, range)) = self.buffer_line_for_row(row) {
2179            let mut size = buffer.indent_size_for_line(range.start.row);
2180            size.len = size
2181                .len
2182                .min(range.end.column)
2183                .saturating_sub(range.start.column);
2184            size
2185        } else {
2186            IndentSize::spaces(0)
2187        }
2188    }
2189
2190    pub fn line_len(&self, row: u32) -> u32 {
2191        if let Some((_, range)) = self.buffer_line_for_row(row) {
2192            range.end.column - range.start.column
2193        } else {
2194            0
2195        }
2196    }
2197
2198    pub fn buffer_line_for_row(&self, row: u32) -> Option<(&BufferSnapshot, Range<Point>)> {
2199        let mut cursor = self.excerpts.cursor::<Point>();
2200        cursor.seek(&Point::new(row, 0), Bias::Right, &());
2201        if let Some(excerpt) = cursor.item() {
2202            let overshoot = row - cursor.start().row;
2203            let excerpt_start = excerpt.range.context.start.to_point(&excerpt.buffer);
2204            let excerpt_end = excerpt.range.context.end.to_point(&excerpt.buffer);
2205            let buffer_row = excerpt_start.row + overshoot;
2206            let line_start = Point::new(buffer_row, 0);
2207            let line_end = Point::new(buffer_row, excerpt.buffer.line_len(buffer_row));
2208            return Some((
2209                &excerpt.buffer,
2210                line_start.max(excerpt_start)..line_end.min(excerpt_end),
2211            ));
2212        }
2213        None
2214    }
2215
2216    pub fn max_point(&self) -> Point {
2217        self.text_summary().lines
2218    }
2219
2220    pub fn text_summary(&self) -> TextSummary {
2221        self.excerpts.summary().text.clone()
2222    }
2223
2224    pub fn text_summary_for_range<D, O>(&self, range: Range<O>) -> D
2225    where
2226        D: TextDimension,
2227        O: ToOffset,
2228    {
2229        let mut summary = D::default();
2230        let mut range = range.start.to_offset(self)..range.end.to_offset(self);
2231        let mut cursor = self.excerpts.cursor::<usize>();
2232        cursor.seek(&range.start, Bias::Right, &());
2233        if let Some(excerpt) = cursor.item() {
2234            let mut end_before_newline = cursor.end(&());
2235            if excerpt.has_trailing_newline {
2236                end_before_newline -= 1;
2237            }
2238
2239            let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
2240            let start_in_excerpt = excerpt_start + (range.start - cursor.start());
2241            let end_in_excerpt =
2242                excerpt_start + (cmp::min(end_before_newline, range.end) - cursor.start());
2243            summary.add_assign(
2244                &excerpt
2245                    .buffer
2246                    .text_summary_for_range(start_in_excerpt..end_in_excerpt),
2247            );
2248
2249            if range.end > end_before_newline {
2250                summary.add_assign(&D::from_text_summary(&TextSummary::from("\n")));
2251            }
2252
2253            cursor.next(&());
2254        }
2255
2256        if range.end > *cursor.start() {
2257            summary.add_assign(&D::from_text_summary(&cursor.summary::<_, TextSummary>(
2258                &range.end,
2259                Bias::Right,
2260                &(),
2261            )));
2262            if let Some(excerpt) = cursor.item() {
2263                range.end = cmp::max(*cursor.start(), range.end);
2264
2265                let excerpt_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
2266                let end_in_excerpt = excerpt_start + (range.end - cursor.start());
2267                summary.add_assign(
2268                    &excerpt
2269                        .buffer
2270                        .text_summary_for_range(excerpt_start..end_in_excerpt),
2271                );
2272            }
2273        }
2274
2275        summary
2276    }
2277
2278    pub fn summary_for_anchor<D>(&self, anchor: &Anchor) -> D
2279    where
2280        D: TextDimension + Ord + Sub<D, Output = D>,
2281    {
2282        let mut cursor = self.excerpts.cursor::<ExcerptSummary>();
2283        let locator = self.excerpt_locator_for_id(anchor.excerpt_id);
2284
2285        cursor.seek(locator, Bias::Left, &());
2286        if cursor.item().is_none() {
2287            cursor.next(&());
2288        }
2289
2290        let mut position = D::from_text_summary(&cursor.start().text);
2291        if let Some(excerpt) = cursor.item() {
2292            if excerpt.id == anchor.excerpt_id {
2293                let excerpt_buffer_start =
2294                    excerpt.range.context.start.summary::<D>(&excerpt.buffer);
2295                let excerpt_buffer_end = excerpt.range.context.end.summary::<D>(&excerpt.buffer);
2296                let buffer_position = cmp::min(
2297                    excerpt_buffer_end,
2298                    anchor.text_anchor.summary::<D>(&excerpt.buffer),
2299                );
2300                if buffer_position > excerpt_buffer_start {
2301                    position.add_assign(&(buffer_position - excerpt_buffer_start));
2302                }
2303            }
2304        }
2305        position
2306    }
2307
2308    pub fn summaries_for_anchors<'a, D, I>(&'a self, anchors: I) -> Vec<D>
2309    where
2310        D: TextDimension + Ord + Sub<D, Output = D>,
2311        I: 'a + IntoIterator<Item = &'a Anchor>,
2312    {
2313        if let Some((_, _, buffer)) = self.as_singleton() {
2314            return buffer
2315                .summaries_for_anchors(anchors.into_iter().map(|a| &a.text_anchor))
2316                .collect();
2317        }
2318
2319        let mut anchors = anchors.into_iter().peekable();
2320        let mut cursor = self.excerpts.cursor::<ExcerptSummary>();
2321        let mut summaries = Vec::new();
2322        while let Some(anchor) = anchors.peek() {
2323            let excerpt_id = anchor.excerpt_id;
2324            let excerpt_anchors = iter::from_fn(|| {
2325                let anchor = anchors.peek()?;
2326                if anchor.excerpt_id == excerpt_id {
2327                    Some(&anchors.next().unwrap().text_anchor)
2328                } else {
2329                    None
2330                }
2331            });
2332
2333            let locator = self.excerpt_locator_for_id(excerpt_id);
2334            cursor.seek_forward(locator, Bias::Left, &());
2335            if cursor.item().is_none() {
2336                cursor.next(&());
2337            }
2338
2339            let position = D::from_text_summary(&cursor.start().text);
2340            if let Some(excerpt) = cursor.item() {
2341                if excerpt.id == excerpt_id {
2342                    let excerpt_buffer_start =
2343                        excerpt.range.context.start.summary::<D>(&excerpt.buffer);
2344                    let excerpt_buffer_end =
2345                        excerpt.range.context.end.summary::<D>(&excerpt.buffer);
2346                    summaries.extend(
2347                        excerpt
2348                            .buffer
2349                            .summaries_for_anchors::<D, _>(excerpt_anchors)
2350                            .map(move |summary| {
2351                                let summary = cmp::min(excerpt_buffer_end.clone(), summary);
2352                                let mut position = position.clone();
2353                                let excerpt_buffer_start = excerpt_buffer_start.clone();
2354                                if summary > excerpt_buffer_start {
2355                                    position.add_assign(&(summary - excerpt_buffer_start));
2356                                }
2357                                position
2358                            }),
2359                    );
2360                    continue;
2361                }
2362            }
2363
2364            summaries.extend(excerpt_anchors.map(|_| position.clone()));
2365        }
2366
2367        summaries
2368    }
2369
2370    pub fn refresh_anchors<'a, I>(&'a self, anchors: I) -> Vec<(usize, Anchor, bool)>
2371    where
2372        I: 'a + IntoIterator<Item = &'a Anchor>,
2373    {
2374        let mut anchors = anchors.into_iter().enumerate().peekable();
2375        let mut cursor = self.excerpts.cursor::<Option<&Locator>>();
2376        cursor.next(&());
2377
2378        let mut result = Vec::new();
2379
2380        while let Some((_, anchor)) = anchors.peek() {
2381            let old_excerpt_id = anchor.excerpt_id;
2382
2383            // Find the location where this anchor's excerpt should be.
2384            let old_locator = self.excerpt_locator_for_id(old_excerpt_id);
2385            cursor.seek_forward(&Some(old_locator), Bias::Left, &());
2386
2387            if cursor.item().is_none() {
2388                cursor.next(&());
2389            }
2390
2391            let next_excerpt = cursor.item();
2392            let prev_excerpt = cursor.prev_item();
2393
2394            // Process all of the anchors for this excerpt.
2395            while let Some((_, anchor)) = anchors.peek() {
2396                if anchor.excerpt_id != old_excerpt_id {
2397                    break;
2398                }
2399                let (anchor_ix, anchor) = anchors.next().unwrap();
2400                let mut anchor = *anchor;
2401
2402                // Leave min and max anchors unchanged if invalid or
2403                // if the old excerpt still exists at this location
2404                let mut kept_position = next_excerpt
2405                    .map_or(false, |e| e.id == old_excerpt_id && e.contains(&anchor))
2406                    || old_excerpt_id == ExcerptId::max()
2407                    || old_excerpt_id == ExcerptId::min();
2408
2409                // If the old excerpt no longer exists at this location, then attempt to
2410                // find an equivalent position for this anchor in an adjacent excerpt.
2411                if !kept_position {
2412                    for excerpt in [next_excerpt, prev_excerpt].iter().filter_map(|e| *e) {
2413                        if excerpt.contains(&anchor) {
2414                            anchor.excerpt_id = excerpt.id.clone();
2415                            kept_position = true;
2416                            break;
2417                        }
2418                    }
2419                }
2420
2421                // If there's no adjacent excerpt that contains the anchor's position,
2422                // then report that the anchor has lost its position.
2423                if !kept_position {
2424                    anchor = if let Some(excerpt) = next_excerpt {
2425                        let mut text_anchor = excerpt
2426                            .range
2427                            .context
2428                            .start
2429                            .bias(anchor.text_anchor.bias, &excerpt.buffer);
2430                        if text_anchor
2431                            .cmp(&excerpt.range.context.end, &excerpt.buffer)
2432                            .is_gt()
2433                        {
2434                            text_anchor = excerpt.range.context.end;
2435                        }
2436                        Anchor {
2437                            buffer_id: Some(excerpt.buffer_id),
2438                            excerpt_id: excerpt.id.clone(),
2439                            text_anchor,
2440                        }
2441                    } else if let Some(excerpt) = prev_excerpt {
2442                        let mut text_anchor = excerpt
2443                            .range
2444                            .context
2445                            .end
2446                            .bias(anchor.text_anchor.bias, &excerpt.buffer);
2447                        if text_anchor
2448                            .cmp(&excerpt.range.context.start, &excerpt.buffer)
2449                            .is_lt()
2450                        {
2451                            text_anchor = excerpt.range.context.start;
2452                        }
2453                        Anchor {
2454                            buffer_id: Some(excerpt.buffer_id),
2455                            excerpt_id: excerpt.id.clone(),
2456                            text_anchor,
2457                        }
2458                    } else if anchor.text_anchor.bias == Bias::Left {
2459                        Anchor::min()
2460                    } else {
2461                        Anchor::max()
2462                    };
2463                }
2464
2465                result.push((anchor_ix, anchor, kept_position));
2466            }
2467        }
2468        result.sort_unstable_by(|a, b| a.1.cmp(&b.1, self));
2469        result
2470    }
2471
2472    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2473        self.anchor_at(position, Bias::Left)
2474    }
2475
2476    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2477        self.anchor_at(position, Bias::Right)
2478    }
2479
2480    pub fn anchor_at<T: ToOffset>(&self, position: T, mut bias: Bias) -> Anchor {
2481        let offset = position.to_offset(self);
2482        if let Some((excerpt_id, buffer_id, buffer)) = self.as_singleton() {
2483            return Anchor {
2484                buffer_id: Some(buffer_id),
2485                excerpt_id: excerpt_id.clone(),
2486                text_anchor: buffer.anchor_at(offset, bias),
2487            };
2488        }
2489
2490        let mut cursor = self.excerpts.cursor::<(usize, Option<ExcerptId>)>();
2491        cursor.seek(&offset, Bias::Right, &());
2492        if cursor.item().is_none() && offset == cursor.start().0 && bias == Bias::Left {
2493            cursor.prev(&());
2494        }
2495        if let Some(excerpt) = cursor.item() {
2496            let mut overshoot = offset.saturating_sub(cursor.start().0);
2497            if excerpt.has_trailing_newline && offset == cursor.end(&()).0 {
2498                overshoot -= 1;
2499                bias = Bias::Right;
2500            }
2501
2502            let buffer_start = excerpt.range.context.start.to_offset(&excerpt.buffer);
2503            let text_anchor =
2504                excerpt.clip_anchor(excerpt.buffer.anchor_at(buffer_start + overshoot, bias));
2505            Anchor {
2506                buffer_id: Some(excerpt.buffer_id),
2507                excerpt_id: excerpt.id.clone(),
2508                text_anchor,
2509            }
2510        } else if offset == 0 && bias == Bias::Left {
2511            Anchor::min()
2512        } else {
2513            Anchor::max()
2514        }
2515    }
2516
2517    pub fn anchor_in_excerpt(&self, excerpt_id: ExcerptId, text_anchor: text::Anchor) -> Anchor {
2518        let locator = self.excerpt_locator_for_id(excerpt_id);
2519        let mut cursor = self.excerpts.cursor::<Option<&Locator>>();
2520        cursor.seek(locator, Bias::Left, &());
2521        if let Some(excerpt) = cursor.item() {
2522            if excerpt.id == excerpt_id {
2523                let text_anchor = excerpt.clip_anchor(text_anchor);
2524                drop(cursor);
2525                return Anchor {
2526                    buffer_id: Some(excerpt.buffer_id),
2527                    excerpt_id,
2528                    text_anchor,
2529                };
2530            }
2531        }
2532        panic!("excerpt not found");
2533    }
2534
2535    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
2536        if anchor.excerpt_id == ExcerptId::min() || anchor.excerpt_id == ExcerptId::max() {
2537            true
2538        } else if let Some(excerpt) = self.excerpt(anchor.excerpt_id) {
2539            excerpt.buffer.can_resolve(&anchor.text_anchor)
2540        } else {
2541            false
2542        }
2543    }
2544
2545    pub fn excerpts(
2546        &self,
2547    ) -> impl Iterator<Item = (ExcerptId, &BufferSnapshot, ExcerptRange<text::Anchor>)> {
2548        self.excerpts
2549            .iter()
2550            .map(|excerpt| (excerpt.id, &excerpt.buffer, excerpt.range.clone()))
2551    }
2552
2553    pub fn excerpt_boundaries_in_range<R, T>(
2554        &self,
2555        range: R,
2556    ) -> impl Iterator<Item = ExcerptBoundary> + '_
2557    where
2558        R: RangeBounds<T>,
2559        T: ToOffset,
2560    {
2561        let start_offset;
2562        let start = match range.start_bound() {
2563            Bound::Included(start) => {
2564                start_offset = start.to_offset(self);
2565                Bound::Included(start_offset)
2566            }
2567            Bound::Excluded(start) => {
2568                start_offset = start.to_offset(self);
2569                Bound::Excluded(start_offset)
2570            }
2571            Bound::Unbounded => {
2572                start_offset = 0;
2573                Bound::Unbounded
2574            }
2575        };
2576        let end = match range.end_bound() {
2577            Bound::Included(end) => Bound::Included(end.to_offset(self)),
2578            Bound::Excluded(end) => Bound::Excluded(end.to_offset(self)),
2579            Bound::Unbounded => Bound::Unbounded,
2580        };
2581        let bounds = (start, end);
2582
2583        let mut cursor = self.excerpts.cursor::<(usize, Point)>();
2584        cursor.seek(&start_offset, Bias::Right, &());
2585        if cursor.item().is_none() {
2586            cursor.prev(&());
2587        }
2588        if !bounds.contains(&cursor.start().0) {
2589            cursor.next(&());
2590        }
2591
2592        let mut prev_buffer_id = cursor.prev_item().map(|excerpt| excerpt.buffer_id);
2593        std::iter::from_fn(move || {
2594            if self.singleton {
2595                None
2596            } else if bounds.contains(&cursor.start().0) {
2597                let excerpt = cursor.item()?;
2598                let starts_new_buffer = Some(excerpt.buffer_id) != prev_buffer_id;
2599                let boundary = ExcerptBoundary {
2600                    id: excerpt.id.clone(),
2601                    row: cursor.start().1.row,
2602                    buffer: excerpt.buffer.clone(),
2603                    range: excerpt.range.clone(),
2604                    starts_new_buffer,
2605                };
2606
2607                prev_buffer_id = Some(excerpt.buffer_id);
2608                cursor.next(&());
2609                Some(boundary)
2610            } else {
2611                None
2612            }
2613        })
2614    }
2615
2616    pub fn edit_count(&self) -> usize {
2617        self.edit_count
2618    }
2619
2620    pub fn parse_count(&self) -> usize {
2621        self.parse_count
2622    }
2623
2624    pub fn enclosing_bracket_ranges<T: ToOffset>(
2625        &self,
2626        range: Range<T>,
2627    ) -> Option<(Range<usize>, Range<usize>)> {
2628        let range = range.start.to_offset(self)..range.end.to_offset(self);
2629
2630        let mut cursor = self.excerpts.cursor::<usize>();
2631        cursor.seek(&range.start, Bias::Right, &());
2632        let start_excerpt = cursor.item();
2633
2634        cursor.seek(&range.end, Bias::Right, &());
2635        let end_excerpt = cursor.item();
2636
2637        start_excerpt
2638            .zip(end_excerpt)
2639            .and_then(|(start_excerpt, end_excerpt)| {
2640                if start_excerpt.id != end_excerpt.id {
2641                    return None;
2642                }
2643
2644                let excerpt_buffer_start = start_excerpt
2645                    .range
2646                    .context
2647                    .start
2648                    .to_offset(&start_excerpt.buffer);
2649                let excerpt_buffer_end = excerpt_buffer_start + start_excerpt.text_summary.len;
2650
2651                let start_in_buffer =
2652                    excerpt_buffer_start + range.start.saturating_sub(*cursor.start());
2653                let end_in_buffer =
2654                    excerpt_buffer_start + range.end.saturating_sub(*cursor.start());
2655                let (mut start_bracket_range, mut end_bracket_range) = start_excerpt
2656                    .buffer
2657                    .enclosing_bracket_ranges(start_in_buffer..end_in_buffer)?;
2658
2659                if start_bracket_range.start >= excerpt_buffer_start
2660                    && end_bracket_range.end <= excerpt_buffer_end
2661                {
2662                    start_bracket_range.start =
2663                        cursor.start() + (start_bracket_range.start - excerpt_buffer_start);
2664                    start_bracket_range.end =
2665                        cursor.start() + (start_bracket_range.end - excerpt_buffer_start);
2666                    end_bracket_range.start =
2667                        cursor.start() + (end_bracket_range.start - excerpt_buffer_start);
2668                    end_bracket_range.end =
2669                        cursor.start() + (end_bracket_range.end - excerpt_buffer_start);
2670                    Some((start_bracket_range, end_bracket_range))
2671                } else {
2672                    None
2673                }
2674            })
2675    }
2676
2677    pub fn diagnostics_update_count(&self) -> usize {
2678        self.diagnostics_update_count
2679    }
2680
2681    pub fn git_diff_update_count(&self) -> usize {
2682        self.git_diff_update_count
2683    }
2684
2685    pub fn trailing_excerpt_update_count(&self) -> usize {
2686        self.trailing_excerpt_update_count
2687    }
2688
2689    pub fn language_at<'a, T: ToOffset>(&'a self, point: T) -> Option<&'a Arc<Language>> {
2690        self.point_to_buffer_offset(point)
2691            .and_then(|(buffer, offset)| buffer.language_at(offset))
2692    }
2693
2694    pub fn language_scope_at<'a, T: ToOffset>(&'a self, point: T) -> Option<LanguageScope> {
2695        self.point_to_buffer_offset(point)
2696            .and_then(|(buffer, offset)| buffer.language_scope_at(offset))
2697    }
2698
2699    pub fn is_dirty(&self) -> bool {
2700        self.is_dirty
2701    }
2702
2703    pub fn has_conflict(&self) -> bool {
2704        self.has_conflict
2705    }
2706
2707    pub fn diagnostic_group<'a, O>(
2708        &'a self,
2709        group_id: usize,
2710    ) -> impl Iterator<Item = DiagnosticEntry<O>> + 'a
2711    where
2712        O: text::FromAnchor + 'a,
2713    {
2714        self.as_singleton()
2715            .into_iter()
2716            .flat_map(move |(_, _, buffer)| buffer.diagnostic_group(group_id))
2717    }
2718
2719    pub fn diagnostics_in_range<'a, T, O>(
2720        &'a self,
2721        range: Range<T>,
2722        reversed: bool,
2723    ) -> impl Iterator<Item = DiagnosticEntry<O>> + 'a
2724    where
2725        T: 'a + ToOffset,
2726        O: 'a + text::FromAnchor,
2727    {
2728        self.as_singleton()
2729            .into_iter()
2730            .flat_map(move |(_, _, buffer)| {
2731                buffer.diagnostics_in_range(
2732                    range.start.to_offset(self)..range.end.to_offset(self),
2733                    reversed,
2734                )
2735            })
2736    }
2737
2738    pub fn git_diff_hunks_in_range<'a>(
2739        &'a self,
2740        row_range: Range<u32>,
2741        reversed: bool,
2742    ) -> impl 'a + Iterator<Item = DiffHunk<u32>> {
2743        let mut cursor = self.excerpts.cursor::<Point>();
2744
2745        if reversed {
2746            cursor.seek(&Point::new(row_range.end, 0), Bias::Left, &());
2747            if cursor.item().is_none() {
2748                cursor.prev(&());
2749            }
2750        } else {
2751            cursor.seek(&Point::new(row_range.start, 0), Bias::Right, &());
2752        }
2753
2754        std::iter::from_fn(move || {
2755            let excerpt = cursor.item()?;
2756            let multibuffer_start = *cursor.start();
2757            let multibuffer_end = multibuffer_start + excerpt.text_summary.lines;
2758            if multibuffer_start.row >= row_range.end {
2759                return None;
2760            }
2761
2762            let mut buffer_start = excerpt.range.context.start;
2763            let mut buffer_end = excerpt.range.context.end;
2764            let excerpt_start_point = buffer_start.to_point(&excerpt.buffer);
2765            let excerpt_end_point = excerpt_start_point + excerpt.text_summary.lines;
2766
2767            if row_range.start > multibuffer_start.row {
2768                let buffer_start_point =
2769                    excerpt_start_point + Point::new(row_range.start - multibuffer_start.row, 0);
2770                buffer_start = excerpt.buffer.anchor_before(buffer_start_point);
2771            }
2772
2773            if row_range.end < multibuffer_end.row {
2774                let buffer_end_point =
2775                    excerpt_start_point + Point::new(row_range.end - multibuffer_start.row, 0);
2776                buffer_end = excerpt.buffer.anchor_before(buffer_end_point);
2777            }
2778
2779            let buffer_hunks = excerpt
2780                .buffer
2781                .git_diff_hunks_intersecting_range(buffer_start..buffer_end, reversed)
2782                .filter_map(move |hunk| {
2783                    let start = multibuffer_start.row
2784                        + hunk
2785                            .buffer_range
2786                            .start
2787                            .saturating_sub(excerpt_start_point.row);
2788                    let end = multibuffer_start.row
2789                        + hunk
2790                            .buffer_range
2791                            .end
2792                            .min(excerpt_end_point.row + 1)
2793                            .saturating_sub(excerpt_start_point.row);
2794
2795                    Some(DiffHunk {
2796                        buffer_range: start..end,
2797                        diff_base_byte_range: hunk.diff_base_byte_range.clone(),
2798                    })
2799                });
2800
2801            if reversed {
2802                cursor.prev(&());
2803            } else {
2804                cursor.next(&());
2805            }
2806
2807            Some(buffer_hunks)
2808        })
2809        .flatten()
2810    }
2811
2812    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2813        let range = range.start.to_offset(self)..range.end.to_offset(self);
2814
2815        let mut cursor = self.excerpts.cursor::<usize>();
2816        cursor.seek(&range.start, Bias::Right, &());
2817        let start_excerpt = cursor.item();
2818
2819        cursor.seek(&range.end, Bias::Right, &());
2820        let end_excerpt = cursor.item();
2821
2822        start_excerpt
2823            .zip(end_excerpt)
2824            .and_then(|(start_excerpt, end_excerpt)| {
2825                if start_excerpt.id != end_excerpt.id {
2826                    return None;
2827                }
2828
2829                let excerpt_buffer_start = start_excerpt
2830                    .range
2831                    .context
2832                    .start
2833                    .to_offset(&start_excerpt.buffer);
2834                let excerpt_buffer_end = excerpt_buffer_start + start_excerpt.text_summary.len;
2835
2836                let start_in_buffer =
2837                    excerpt_buffer_start + range.start.saturating_sub(*cursor.start());
2838                let end_in_buffer =
2839                    excerpt_buffer_start + range.end.saturating_sub(*cursor.start());
2840                let mut ancestor_buffer_range = start_excerpt
2841                    .buffer
2842                    .range_for_syntax_ancestor(start_in_buffer..end_in_buffer)?;
2843                ancestor_buffer_range.start =
2844                    cmp::max(ancestor_buffer_range.start, excerpt_buffer_start);
2845                ancestor_buffer_range.end = cmp::min(ancestor_buffer_range.end, excerpt_buffer_end);
2846
2847                let start = cursor.start() + (ancestor_buffer_range.start - excerpt_buffer_start);
2848                let end = cursor.start() + (ancestor_buffer_range.end - excerpt_buffer_start);
2849                Some(start..end)
2850            })
2851    }
2852
2853    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2854        let (excerpt_id, _, buffer) = self.as_singleton()?;
2855        let outline = buffer.outline(theme)?;
2856        Some(Outline::new(
2857            outline
2858                .items
2859                .into_iter()
2860                .map(|item| OutlineItem {
2861                    depth: item.depth,
2862                    range: self.anchor_in_excerpt(excerpt_id.clone(), item.range.start)
2863                        ..self.anchor_in_excerpt(excerpt_id.clone(), item.range.end),
2864                    text: item.text,
2865                    highlight_ranges: item.highlight_ranges,
2866                    name_ranges: item.name_ranges,
2867                })
2868                .collect(),
2869        ))
2870    }
2871
2872    pub fn symbols_containing<T: ToOffset>(
2873        &self,
2874        offset: T,
2875        theme: Option<&SyntaxTheme>,
2876    ) -> Option<(usize, Vec<OutlineItem<Anchor>>)> {
2877        let anchor = self.anchor_before(offset);
2878        let excerpt_id = anchor.excerpt_id();
2879        let excerpt = self.excerpt(excerpt_id)?;
2880        Some((
2881            excerpt.buffer_id,
2882            excerpt
2883                .buffer
2884                .symbols_containing(anchor.text_anchor, theme)
2885                .into_iter()
2886                .flatten()
2887                .map(|item| OutlineItem {
2888                    depth: item.depth,
2889                    range: self.anchor_in_excerpt(excerpt_id, item.range.start)
2890                        ..self.anchor_in_excerpt(excerpt_id, item.range.end),
2891                    text: item.text,
2892                    highlight_ranges: item.highlight_ranges,
2893                    name_ranges: item.name_ranges,
2894                })
2895                .collect(),
2896        ))
2897    }
2898
2899    fn excerpt_locator_for_id<'a>(&'a self, id: ExcerptId) -> &'a Locator {
2900        if id == ExcerptId::min() {
2901            Locator::min_ref()
2902        } else if id == ExcerptId::max() {
2903            Locator::max_ref()
2904        } else {
2905            let mut cursor = self.excerpt_ids.cursor::<ExcerptId>();
2906            cursor.seek(&id, Bias::Left, &());
2907            if let Some(entry) = cursor.item() {
2908                if entry.id == id {
2909                    return &entry.locator;
2910                }
2911            }
2912            panic!("invalid excerpt id {:?}", id)
2913        }
2914    }
2915
2916    pub fn buffer_id_for_excerpt(&self, excerpt_id: ExcerptId) -> Option<usize> {
2917        Some(self.excerpt(excerpt_id)?.buffer_id)
2918    }
2919
2920    fn excerpt<'a>(&'a self, excerpt_id: ExcerptId) -> Option<&'a Excerpt> {
2921        let mut cursor = self.excerpts.cursor::<Option<&Locator>>();
2922        let locator = self.excerpt_locator_for_id(excerpt_id);
2923        cursor.seek(&Some(locator), Bias::Left, &());
2924        if let Some(excerpt) = cursor.item() {
2925            if excerpt.id == excerpt_id {
2926                return Some(excerpt);
2927            }
2928        }
2929        None
2930    }
2931
2932    pub fn remote_selections_in_range<'a>(
2933        &'a self,
2934        range: &'a Range<Anchor>,
2935    ) -> impl 'a + Iterator<Item = (ReplicaId, bool, CursorShape, Selection<Anchor>)> {
2936        let mut cursor = self.excerpts.cursor::<ExcerptSummary>();
2937        let start_locator = self.excerpt_locator_for_id(range.start.excerpt_id);
2938        let end_locator = self.excerpt_locator_for_id(range.end.excerpt_id);
2939        cursor.seek(start_locator, Bias::Left, &());
2940        cursor
2941            .take_while(move |excerpt| excerpt.locator <= *end_locator)
2942            .flat_map(move |excerpt| {
2943                let mut query_range = excerpt.range.context.start..excerpt.range.context.end;
2944                if excerpt.id == range.start.excerpt_id {
2945                    query_range.start = range.start.text_anchor;
2946                }
2947                if excerpt.id == range.end.excerpt_id {
2948                    query_range.end = range.end.text_anchor;
2949                }
2950
2951                excerpt
2952                    .buffer
2953                    .remote_selections_in_range(query_range)
2954                    .flat_map(move |(replica_id, line_mode, cursor_shape, selections)| {
2955                        selections.map(move |selection| {
2956                            let mut start = Anchor {
2957                                buffer_id: Some(excerpt.buffer_id),
2958                                excerpt_id: excerpt.id.clone(),
2959                                text_anchor: selection.start,
2960                            };
2961                            let mut end = Anchor {
2962                                buffer_id: Some(excerpt.buffer_id),
2963                                excerpt_id: excerpt.id.clone(),
2964                                text_anchor: selection.end,
2965                            };
2966                            if range.start.cmp(&start, self).is_gt() {
2967                                start = range.start.clone();
2968                            }
2969                            if range.end.cmp(&end, self).is_lt() {
2970                                end = range.end.clone();
2971                            }
2972
2973                            (
2974                                replica_id,
2975                                line_mode,
2976                                cursor_shape,
2977                                Selection {
2978                                    id: selection.id,
2979                                    start,
2980                                    end,
2981                                    reversed: selection.reversed,
2982                                    goal: selection.goal,
2983                                },
2984                            )
2985                        })
2986                    })
2987            })
2988    }
2989}
2990
2991#[cfg(any(test, feature = "test-support"))]
2992impl MultiBufferSnapshot {
2993    pub fn random_byte_range(&self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
2994        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
2995        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
2996        start..end
2997    }
2998}
2999
3000impl History {
3001    fn start_transaction(&mut self, now: Instant) -> Option<TransactionId> {
3002        self.transaction_depth += 1;
3003        if self.transaction_depth == 1 {
3004            let id = self.next_transaction_id.tick();
3005            self.undo_stack.push(Transaction {
3006                id,
3007                buffer_transactions: Default::default(),
3008                first_edit_at: now,
3009                last_edit_at: now,
3010                suppress_grouping: false,
3011            });
3012            Some(id)
3013        } else {
3014            None
3015        }
3016    }
3017
3018    fn end_transaction(
3019        &mut self,
3020        now: Instant,
3021        buffer_transactions: HashMap<usize, TransactionId>,
3022    ) -> bool {
3023        assert_ne!(self.transaction_depth, 0);
3024        self.transaction_depth -= 1;
3025        if self.transaction_depth == 0 {
3026            if buffer_transactions.is_empty() {
3027                self.undo_stack.pop();
3028                false
3029            } else {
3030                self.redo_stack.clear();
3031                let transaction = self.undo_stack.last_mut().unwrap();
3032                transaction.last_edit_at = now;
3033                for (buffer_id, transaction_id) in buffer_transactions {
3034                    transaction
3035                        .buffer_transactions
3036                        .entry(buffer_id)
3037                        .or_insert(transaction_id);
3038                }
3039                true
3040            }
3041        } else {
3042            false
3043        }
3044    }
3045
3046    fn push_transaction<'a, T>(&mut self, buffer_transactions: T, now: Instant)
3047    where
3048        T: IntoIterator<Item = (&'a ModelHandle<Buffer>, &'a language::Transaction)>,
3049    {
3050        assert_eq!(self.transaction_depth, 0);
3051        let transaction = Transaction {
3052            id: self.next_transaction_id.tick(),
3053            buffer_transactions: buffer_transactions
3054                .into_iter()
3055                .map(|(buffer, transaction)| (buffer.id(), transaction.id))
3056                .collect(),
3057            first_edit_at: now,
3058            last_edit_at: now,
3059            suppress_grouping: false,
3060        };
3061        if !transaction.buffer_transactions.is_empty() {
3062            self.undo_stack.push(transaction);
3063            self.redo_stack.clear();
3064        }
3065    }
3066
3067    fn finalize_last_transaction(&mut self) {
3068        if let Some(transaction) = self.undo_stack.last_mut() {
3069            transaction.suppress_grouping = true;
3070        }
3071    }
3072
3073    fn pop_undo(&mut self) -> Option<&mut Transaction> {
3074        assert_eq!(self.transaction_depth, 0);
3075        if let Some(transaction) = self.undo_stack.pop() {
3076            self.redo_stack.push(transaction);
3077            self.redo_stack.last_mut()
3078        } else {
3079            None
3080        }
3081    }
3082
3083    fn pop_redo(&mut self) -> Option<&mut Transaction> {
3084        assert_eq!(self.transaction_depth, 0);
3085        if let Some(transaction) = self.redo_stack.pop() {
3086            self.undo_stack.push(transaction);
3087            self.undo_stack.last_mut()
3088        } else {
3089            None
3090        }
3091    }
3092
3093    fn group(&mut self) -> Option<TransactionId> {
3094        let mut count = 0;
3095        let mut transactions = self.undo_stack.iter();
3096        if let Some(mut transaction) = transactions.next_back() {
3097            while let Some(prev_transaction) = transactions.next_back() {
3098                if !prev_transaction.suppress_grouping
3099                    && transaction.first_edit_at - prev_transaction.last_edit_at
3100                        <= self.group_interval
3101                {
3102                    transaction = prev_transaction;
3103                    count += 1;
3104                } else {
3105                    break;
3106                }
3107            }
3108        }
3109        self.group_trailing(count)
3110    }
3111
3112    fn group_until(&mut self, transaction_id: TransactionId) {
3113        let mut count = 0;
3114        for transaction in self.undo_stack.iter().rev() {
3115            if transaction.id == transaction_id {
3116                self.group_trailing(count);
3117                break;
3118            } else if transaction.suppress_grouping {
3119                break;
3120            } else {
3121                count += 1;
3122            }
3123        }
3124    }
3125
3126    fn group_trailing(&mut self, n: usize) -> Option<TransactionId> {
3127        let new_len = self.undo_stack.len() - n;
3128        let (transactions_to_keep, transactions_to_merge) = self.undo_stack.split_at_mut(new_len);
3129        if let Some(last_transaction) = transactions_to_keep.last_mut() {
3130            if let Some(transaction) = transactions_to_merge.last() {
3131                last_transaction.last_edit_at = transaction.last_edit_at;
3132            }
3133            for to_merge in transactions_to_merge {
3134                for (buffer_id, transaction_id) in &to_merge.buffer_transactions {
3135                    last_transaction
3136                        .buffer_transactions
3137                        .entry(*buffer_id)
3138                        .or_insert(*transaction_id);
3139                }
3140            }
3141        }
3142
3143        self.undo_stack.truncate(new_len);
3144        self.undo_stack.last().map(|t| t.id)
3145    }
3146}
3147
3148impl Excerpt {
3149    fn new(
3150        id: ExcerptId,
3151        locator: Locator,
3152        buffer_id: usize,
3153        buffer: BufferSnapshot,
3154        range: ExcerptRange<text::Anchor>,
3155        has_trailing_newline: bool,
3156    ) -> Self {
3157        Excerpt {
3158            id,
3159            locator,
3160            max_buffer_row: range.context.end.to_point(&buffer).row,
3161            text_summary: buffer
3162                .text_summary_for_range::<TextSummary, _>(range.context.to_offset(&buffer)),
3163            buffer_id,
3164            buffer,
3165            range,
3166            has_trailing_newline,
3167        }
3168    }
3169
3170    fn chunks_in_range(&self, range: Range<usize>, language_aware: bool) -> ExcerptChunks {
3171        let content_start = self.range.context.start.to_offset(&self.buffer);
3172        let chunks_start = content_start + range.start;
3173        let chunks_end = content_start + cmp::min(range.end, self.text_summary.len);
3174
3175        let footer_height = if self.has_trailing_newline
3176            && range.start <= self.text_summary.len
3177            && range.end > self.text_summary.len
3178        {
3179            1
3180        } else {
3181            0
3182        };
3183
3184        let content_chunks = self.buffer.chunks(chunks_start..chunks_end, language_aware);
3185
3186        ExcerptChunks {
3187            content_chunks,
3188            footer_height,
3189        }
3190    }
3191
3192    fn bytes_in_range(&self, range: Range<usize>) -> ExcerptBytes {
3193        let content_start = self.range.context.start.to_offset(&self.buffer);
3194        let bytes_start = content_start + range.start;
3195        let bytes_end = content_start + cmp::min(range.end, self.text_summary.len);
3196        let footer_height = if self.has_trailing_newline
3197            && range.start <= self.text_summary.len
3198            && range.end > self.text_summary.len
3199        {
3200            1
3201        } else {
3202            0
3203        };
3204        let content_bytes = self.buffer.bytes_in_range(bytes_start..bytes_end);
3205
3206        ExcerptBytes {
3207            content_bytes,
3208            footer_height,
3209        }
3210    }
3211
3212    fn clip_anchor(&self, text_anchor: text::Anchor) -> text::Anchor {
3213        if text_anchor
3214            .cmp(&self.range.context.start, &self.buffer)
3215            .is_lt()
3216        {
3217            self.range.context.start
3218        } else if text_anchor
3219            .cmp(&self.range.context.end, &self.buffer)
3220            .is_gt()
3221        {
3222            self.range.context.end
3223        } else {
3224            text_anchor
3225        }
3226    }
3227
3228    fn contains(&self, anchor: &Anchor) -> bool {
3229        Some(self.buffer_id) == anchor.buffer_id
3230            && self
3231                .range
3232                .context
3233                .start
3234                .cmp(&anchor.text_anchor, &self.buffer)
3235                .is_le()
3236            && self
3237                .range
3238                .context
3239                .end
3240                .cmp(&anchor.text_anchor, &self.buffer)
3241                .is_ge()
3242    }
3243}
3244
3245impl ExcerptId {
3246    pub fn min() -> Self {
3247        Self(0)
3248    }
3249
3250    pub fn max() -> Self {
3251        Self(usize::MAX)
3252    }
3253
3254    pub fn to_proto(&self) -> u64 {
3255        self.0 as _
3256    }
3257
3258    pub fn from_proto(proto: u64) -> Self {
3259        Self(proto as _)
3260    }
3261
3262    pub fn cmp(&self, other: &Self, snapshot: &MultiBufferSnapshot) -> cmp::Ordering {
3263        let a = snapshot.excerpt_locator_for_id(*self);
3264        let b = snapshot.excerpt_locator_for_id(*other);
3265        a.cmp(&b).then_with(|| self.0.cmp(&other.0))
3266    }
3267}
3268
3269impl Into<usize> for ExcerptId {
3270    fn into(self) -> usize {
3271        self.0
3272    }
3273}
3274
3275impl fmt::Debug for Excerpt {
3276    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3277        f.debug_struct("Excerpt")
3278            .field("id", &self.id)
3279            .field("locator", &self.locator)
3280            .field("buffer_id", &self.buffer_id)
3281            .field("range", &self.range)
3282            .field("text_summary", &self.text_summary)
3283            .field("has_trailing_newline", &self.has_trailing_newline)
3284            .finish()
3285    }
3286}
3287
3288impl sum_tree::Item for Excerpt {
3289    type Summary = ExcerptSummary;
3290
3291    fn summary(&self) -> Self::Summary {
3292        let mut text = self.text_summary.clone();
3293        if self.has_trailing_newline {
3294            text += TextSummary::from("\n");
3295        }
3296        ExcerptSummary {
3297            excerpt_id: self.id,
3298            excerpt_locator: self.locator.clone(),
3299            max_buffer_row: self.max_buffer_row,
3300            text,
3301        }
3302    }
3303}
3304
3305impl sum_tree::Item for ExcerptIdMapping {
3306    type Summary = ExcerptId;
3307
3308    fn summary(&self) -> Self::Summary {
3309        self.id
3310    }
3311}
3312
3313impl sum_tree::KeyedItem for ExcerptIdMapping {
3314    type Key = ExcerptId;
3315
3316    fn key(&self) -> Self::Key {
3317        self.id
3318    }
3319}
3320
3321impl sum_tree::Summary for ExcerptId {
3322    type Context = ();
3323
3324    fn add_summary(&mut self, other: &Self, _: &()) {
3325        *self = *other;
3326    }
3327}
3328
3329impl sum_tree::Summary for ExcerptSummary {
3330    type Context = ();
3331
3332    fn add_summary(&mut self, summary: &Self, _: &()) {
3333        debug_assert!(summary.excerpt_locator > self.excerpt_locator);
3334        self.excerpt_locator = summary.excerpt_locator.clone();
3335        self.text.add_summary(&summary.text, &());
3336        self.max_buffer_row = cmp::max(self.max_buffer_row, summary.max_buffer_row);
3337    }
3338}
3339
3340impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for TextSummary {
3341    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3342        *self += &summary.text;
3343    }
3344}
3345
3346impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for usize {
3347    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3348        *self += summary.text.len;
3349    }
3350}
3351
3352impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, ExcerptSummary> for usize {
3353    fn cmp(&self, cursor_location: &ExcerptSummary, _: &()) -> cmp::Ordering {
3354        Ord::cmp(self, &cursor_location.text.len)
3355    }
3356}
3357
3358impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, Option<&'a Locator>> for Locator {
3359    fn cmp(&self, cursor_location: &Option<&'a Locator>, _: &()) -> cmp::Ordering {
3360        Ord::cmp(&Some(self), cursor_location)
3361    }
3362}
3363
3364impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, ExcerptSummary> for Locator {
3365    fn cmp(&self, cursor_location: &ExcerptSummary, _: &()) -> cmp::Ordering {
3366        Ord::cmp(self, &cursor_location.excerpt_locator)
3367    }
3368}
3369
3370impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for OffsetUtf16 {
3371    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3372        *self += summary.text.len_utf16;
3373    }
3374}
3375
3376impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for Point {
3377    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3378        *self += summary.text.lines;
3379    }
3380}
3381
3382impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for PointUtf16 {
3383    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3384        *self += summary.text.lines_utf16()
3385    }
3386}
3387
3388impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for Option<&'a Locator> {
3389    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3390        *self = Some(&summary.excerpt_locator);
3391    }
3392}
3393
3394impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for Option<ExcerptId> {
3395    fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
3396        *self = Some(summary.excerpt_id);
3397    }
3398}
3399
3400impl<'a> MultiBufferRows<'a> {
3401    pub fn seek(&mut self, row: u32) {
3402        self.buffer_row_range = 0..0;
3403
3404        self.excerpts
3405            .seek_forward(&Point::new(row, 0), Bias::Right, &());
3406        if self.excerpts.item().is_none() {
3407            self.excerpts.prev(&());
3408
3409            if self.excerpts.item().is_none() && row == 0 {
3410                self.buffer_row_range = 0..1;
3411                return;
3412            }
3413        }
3414
3415        if let Some(excerpt) = self.excerpts.item() {
3416            let overshoot = row - self.excerpts.start().row;
3417            let excerpt_start = excerpt.range.context.start.to_point(&excerpt.buffer).row;
3418            self.buffer_row_range.start = excerpt_start + overshoot;
3419            self.buffer_row_range.end = excerpt_start + excerpt.text_summary.lines.row + 1;
3420        }
3421    }
3422}
3423
3424impl<'a> Iterator for MultiBufferRows<'a> {
3425    type Item = Option<u32>;
3426
3427    fn next(&mut self) -> Option<Self::Item> {
3428        loop {
3429            if !self.buffer_row_range.is_empty() {
3430                let row = Some(self.buffer_row_range.start);
3431                self.buffer_row_range.start += 1;
3432                return Some(row);
3433            }
3434            self.excerpts.item()?;
3435            self.excerpts.next(&());
3436            let excerpt = self.excerpts.item()?;
3437            self.buffer_row_range.start = excerpt.range.context.start.to_point(&excerpt.buffer).row;
3438            self.buffer_row_range.end =
3439                self.buffer_row_range.start + excerpt.text_summary.lines.row + 1;
3440        }
3441    }
3442}
3443
3444impl<'a> MultiBufferChunks<'a> {
3445    pub fn offset(&self) -> usize {
3446        self.range.start
3447    }
3448
3449    pub fn seek(&mut self, offset: usize) {
3450        self.range.start = offset;
3451        self.excerpts.seek(&offset, Bias::Right, &());
3452        if let Some(excerpt) = self.excerpts.item() {
3453            self.excerpt_chunks = Some(excerpt.chunks_in_range(
3454                self.range.start - self.excerpts.start()..self.range.end - self.excerpts.start(),
3455                self.language_aware,
3456            ));
3457        } else {
3458            self.excerpt_chunks = None;
3459        }
3460    }
3461}
3462
3463impl<'a> Iterator for MultiBufferChunks<'a> {
3464    type Item = Chunk<'a>;
3465
3466    fn next(&mut self) -> Option<Self::Item> {
3467        if self.range.is_empty() {
3468            None
3469        } else if let Some(chunk) = self.excerpt_chunks.as_mut()?.next() {
3470            self.range.start += chunk.text.len();
3471            Some(chunk)
3472        } else {
3473            self.excerpts.next(&());
3474            let excerpt = self.excerpts.item()?;
3475            self.excerpt_chunks = Some(excerpt.chunks_in_range(
3476                0..self.range.end - self.excerpts.start(),
3477                self.language_aware,
3478            ));
3479            self.next()
3480        }
3481    }
3482}
3483
3484impl<'a> MultiBufferBytes<'a> {
3485    fn consume(&mut self, len: usize) {
3486        self.range.start += len;
3487        self.chunk = &self.chunk[len..];
3488
3489        if !self.range.is_empty() && self.chunk.is_empty() {
3490            if let Some(chunk) = self.excerpt_bytes.as_mut().and_then(|bytes| bytes.next()) {
3491                self.chunk = chunk;
3492            } else {
3493                self.excerpts.next(&());
3494                if let Some(excerpt) = self.excerpts.item() {
3495                    let mut excerpt_bytes =
3496                        excerpt.bytes_in_range(0..self.range.end - self.excerpts.start());
3497                    self.chunk = excerpt_bytes.next().unwrap();
3498                    self.excerpt_bytes = Some(excerpt_bytes);
3499                }
3500            }
3501        }
3502    }
3503}
3504
3505impl<'a> Iterator for MultiBufferBytes<'a> {
3506    type Item = &'a [u8];
3507
3508    fn next(&mut self) -> Option<Self::Item> {
3509        let chunk = self.chunk;
3510        if chunk.is_empty() {
3511            None
3512        } else {
3513            self.consume(chunk.len());
3514            Some(chunk)
3515        }
3516    }
3517}
3518
3519impl<'a> io::Read for MultiBufferBytes<'a> {
3520    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
3521        let len = cmp::min(buf.len(), self.chunk.len());
3522        buf[..len].copy_from_slice(&self.chunk[..len]);
3523        if len > 0 {
3524            self.consume(len);
3525        }
3526        Ok(len)
3527    }
3528}
3529
3530impl<'a> Iterator for ExcerptBytes<'a> {
3531    type Item = &'a [u8];
3532
3533    fn next(&mut self) -> Option<Self::Item> {
3534        if let Some(chunk) = self.content_bytes.next() {
3535            if !chunk.is_empty() {
3536                return Some(chunk);
3537            }
3538        }
3539
3540        if self.footer_height > 0 {
3541            let result = &NEWLINES[..self.footer_height];
3542            self.footer_height = 0;
3543            return Some(result);
3544        }
3545
3546        None
3547    }
3548}
3549
3550impl<'a> Iterator for ExcerptChunks<'a> {
3551    type Item = Chunk<'a>;
3552
3553    fn next(&mut self) -> Option<Self::Item> {
3554        if let Some(chunk) = self.content_chunks.next() {
3555            return Some(chunk);
3556        }
3557
3558        if self.footer_height > 0 {
3559            let text = unsafe { str::from_utf8_unchecked(&NEWLINES[..self.footer_height]) };
3560            self.footer_height = 0;
3561            return Some(Chunk {
3562                text,
3563                ..Default::default()
3564            });
3565        }
3566
3567        None
3568    }
3569}
3570
3571impl ToOffset for Point {
3572    fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
3573        snapshot.point_to_offset(*self)
3574    }
3575}
3576
3577impl ToOffset for usize {
3578    fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
3579        assert!(*self <= snapshot.len(), "offset is out of range");
3580        *self
3581    }
3582}
3583
3584impl ToOffset for OffsetUtf16 {
3585    fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
3586        snapshot.offset_utf16_to_offset(*self)
3587    }
3588}
3589
3590impl ToOffset for PointUtf16 {
3591    fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
3592        snapshot.point_utf16_to_offset(*self)
3593    }
3594}
3595
3596impl ToOffsetUtf16 for OffsetUtf16 {
3597    fn to_offset_utf16(&self, _snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
3598        *self
3599    }
3600}
3601
3602impl ToOffsetUtf16 for usize {
3603    fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
3604        snapshot.offset_to_offset_utf16(*self)
3605    }
3606}
3607
3608impl ToPoint for usize {
3609    fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
3610        snapshot.offset_to_point(*self)
3611    }
3612}
3613
3614impl ToPoint for Point {
3615    fn to_point<'a>(&self, _: &MultiBufferSnapshot) -> Point {
3616        *self
3617    }
3618}
3619
3620impl ToPointUtf16 for usize {
3621    fn to_point_utf16<'a>(&self, snapshot: &MultiBufferSnapshot) -> PointUtf16 {
3622        snapshot.offset_to_point_utf16(*self)
3623    }
3624}
3625
3626impl ToPointUtf16 for Point {
3627    fn to_point_utf16<'a>(&self, snapshot: &MultiBufferSnapshot) -> PointUtf16 {
3628        snapshot.point_to_point_utf16(*self)
3629    }
3630}
3631
3632impl ToPointUtf16 for PointUtf16 {
3633    fn to_point_utf16<'a>(&self, _: &MultiBufferSnapshot) -> PointUtf16 {
3634        *self
3635    }
3636}
3637
3638fn build_excerpt_ranges<T>(
3639    buffer: &BufferSnapshot,
3640    ranges: &[Range<T>],
3641    context_line_count: u32,
3642) -> (Vec<ExcerptRange<Point>>, Vec<usize>)
3643where
3644    T: text::ToPoint,
3645{
3646    let max_point = buffer.max_point();
3647    let mut range_counts = Vec::new();
3648    let mut excerpt_ranges = Vec::new();
3649    let mut range_iter = ranges
3650        .iter()
3651        .map(|range| range.start.to_point(buffer)..range.end.to_point(buffer))
3652        .peekable();
3653    while let Some(range) = range_iter.next() {
3654        let excerpt_start = Point::new(range.start.row.saturating_sub(context_line_count), 0);
3655        let mut excerpt_end = Point::new(range.end.row + 1 + context_line_count, 0).min(max_point);
3656        let mut ranges_in_excerpt = 1;
3657
3658        while let Some(next_range) = range_iter.peek() {
3659            if next_range.start.row <= excerpt_end.row + context_line_count {
3660                excerpt_end =
3661                    Point::new(next_range.end.row + 1 + context_line_count, 0).min(max_point);
3662                ranges_in_excerpt += 1;
3663                range_iter.next();
3664            } else {
3665                break;
3666            }
3667        }
3668
3669        excerpt_ranges.push(ExcerptRange {
3670            context: excerpt_start..excerpt_end,
3671            primary: Some(range),
3672        });
3673        range_counts.push(ranges_in_excerpt);
3674    }
3675
3676    (excerpt_ranges, range_counts)
3677}
3678
3679#[cfg(test)]
3680mod tests {
3681    use super::*;
3682    use futures::StreamExt;
3683    use gpui::{MutableAppContext, TestAppContext};
3684    use language::{Buffer, Rope};
3685    use rand::prelude::*;
3686    use settings::Settings;
3687    use std::{env, rc::Rc};
3688    use unindent::Unindent;
3689
3690    use util::test::sample_text;
3691
3692    #[gpui::test]
3693    fn test_singleton(cx: &mut MutableAppContext) {
3694        let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'a'), cx));
3695        let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx));
3696
3697        let snapshot = multibuffer.read(cx).snapshot(cx);
3698        assert_eq!(snapshot.text(), buffer.read(cx).text());
3699
3700        assert_eq!(
3701            snapshot.buffer_rows(0).collect::<Vec<_>>(),
3702            (0..buffer.read(cx).row_count())
3703                .map(Some)
3704                .collect::<Vec<_>>()
3705        );
3706
3707        buffer.update(cx, |buffer, cx| buffer.edit([(1..3, "XXX\n")], None, cx));
3708        let snapshot = multibuffer.read(cx).snapshot(cx);
3709
3710        assert_eq!(snapshot.text(), buffer.read(cx).text());
3711        assert_eq!(
3712            snapshot.buffer_rows(0).collect::<Vec<_>>(),
3713            (0..buffer.read(cx).row_count())
3714                .map(Some)
3715                .collect::<Vec<_>>()
3716        );
3717    }
3718
3719    #[gpui::test]
3720    fn test_remote(cx: &mut MutableAppContext) {
3721        let host_buffer = cx.add_model(|cx| Buffer::new(0, "a", cx));
3722        let guest_buffer = cx.add_model(|cx| {
3723            let state = host_buffer.read(cx).to_proto();
3724            let ops = cx
3725                .background()
3726                .block(host_buffer.read(cx).serialize_ops(None, cx));
3727            let mut buffer = Buffer::from_proto(1, state, None).unwrap();
3728            buffer
3729                .apply_ops(
3730                    ops.into_iter()
3731                        .map(|op| language::proto::deserialize_operation(op).unwrap()),
3732                    cx,
3733                )
3734                .unwrap();
3735            buffer
3736        });
3737        let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(guest_buffer.clone(), cx));
3738        let snapshot = multibuffer.read(cx).snapshot(cx);
3739        assert_eq!(snapshot.text(), "a");
3740
3741        guest_buffer.update(cx, |buffer, cx| buffer.edit([(1..1, "b")], None, cx));
3742        let snapshot = multibuffer.read(cx).snapshot(cx);
3743        assert_eq!(snapshot.text(), "ab");
3744
3745        guest_buffer.update(cx, |buffer, cx| buffer.edit([(2..2, "c")], None, cx));
3746        let snapshot = multibuffer.read(cx).snapshot(cx);
3747        assert_eq!(snapshot.text(), "abc");
3748    }
3749
3750    #[gpui::test]
3751    fn test_excerpt_boundaries_and_clipping(cx: &mut MutableAppContext) {
3752        let buffer_1 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'a'), cx));
3753        let buffer_2 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'g'), cx));
3754        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
3755
3756        let events = Rc::new(RefCell::new(Vec::<Event>::new()));
3757        multibuffer.update(cx, |_, cx| {
3758            let events = events.clone();
3759            cx.subscribe(&multibuffer, move |_, _, event, _| {
3760                if let Event::Edited = event {
3761                    events.borrow_mut().push(event.clone())
3762                }
3763            })
3764            .detach();
3765        });
3766
3767        let subscription = multibuffer.update(cx, |multibuffer, cx| {
3768            let subscription = multibuffer.subscribe();
3769            multibuffer.push_excerpts(
3770                buffer_1.clone(),
3771                [ExcerptRange {
3772                    context: Point::new(1, 2)..Point::new(2, 5),
3773                    primary: None,
3774                }],
3775                cx,
3776            );
3777            assert_eq!(
3778                subscription.consume().into_inner(),
3779                [Edit {
3780                    old: 0..0,
3781                    new: 0..10
3782                }]
3783            );
3784
3785            multibuffer.push_excerpts(
3786                buffer_1.clone(),
3787                [ExcerptRange {
3788                    context: Point::new(3, 3)..Point::new(4, 4),
3789                    primary: None,
3790                }],
3791                cx,
3792            );
3793            multibuffer.push_excerpts(
3794                buffer_2.clone(),
3795                [ExcerptRange {
3796                    context: Point::new(3, 1)..Point::new(3, 3),
3797                    primary: None,
3798                }],
3799                cx,
3800            );
3801            assert_eq!(
3802                subscription.consume().into_inner(),
3803                [Edit {
3804                    old: 10..10,
3805                    new: 10..22
3806                }]
3807            );
3808
3809            subscription
3810        });
3811
3812        // Adding excerpts emits an edited event.
3813        assert_eq!(
3814            events.borrow().as_slice(),
3815            &[Event::Edited, Event::Edited, Event::Edited]
3816        );
3817
3818        let snapshot = multibuffer.read(cx).snapshot(cx);
3819        assert_eq!(
3820            snapshot.text(),
3821            concat!(
3822                "bbbb\n",  // Preserve newlines
3823                "ccccc\n", //
3824                "ddd\n",   //
3825                "eeee\n",  //
3826                "jj"       //
3827            )
3828        );
3829        assert_eq!(
3830            snapshot.buffer_rows(0).collect::<Vec<_>>(),
3831            [Some(1), Some(2), Some(3), Some(4), Some(3)]
3832        );
3833        assert_eq!(
3834            snapshot.buffer_rows(2).collect::<Vec<_>>(),
3835            [Some(3), Some(4), Some(3)]
3836        );
3837        assert_eq!(snapshot.buffer_rows(4).collect::<Vec<_>>(), [Some(3)]);
3838        assert_eq!(snapshot.buffer_rows(5).collect::<Vec<_>>(), []);
3839
3840        assert_eq!(
3841            boundaries_in_range(Point::new(0, 0)..Point::new(4, 2), &snapshot),
3842            &[
3843                (0, "bbbb\nccccc".to_string(), true),
3844                (2, "ddd\neeee".to_string(), false),
3845                (4, "jj".to_string(), true),
3846            ]
3847        );
3848        assert_eq!(
3849            boundaries_in_range(Point::new(0, 0)..Point::new(2, 0), &snapshot),
3850            &[(0, "bbbb\nccccc".to_string(), true)]
3851        );
3852        assert_eq!(
3853            boundaries_in_range(Point::new(1, 0)..Point::new(1, 5), &snapshot),
3854            &[]
3855        );
3856        assert_eq!(
3857            boundaries_in_range(Point::new(1, 0)..Point::new(2, 0), &snapshot),
3858            &[]
3859        );
3860        assert_eq!(
3861            boundaries_in_range(Point::new(1, 0)..Point::new(4, 0), &snapshot),
3862            &[(2, "ddd\neeee".to_string(), false)]
3863        );
3864        assert_eq!(
3865            boundaries_in_range(Point::new(1, 0)..Point::new(4, 0), &snapshot),
3866            &[(2, "ddd\neeee".to_string(), false)]
3867        );
3868        assert_eq!(
3869            boundaries_in_range(Point::new(2, 0)..Point::new(3, 0), &snapshot),
3870            &[(2, "ddd\neeee".to_string(), false)]
3871        );
3872        assert_eq!(
3873            boundaries_in_range(Point::new(4, 0)..Point::new(4, 2), &snapshot),
3874            &[(4, "jj".to_string(), true)]
3875        );
3876        assert_eq!(
3877            boundaries_in_range(Point::new(4, 2)..Point::new(4, 2), &snapshot),
3878            &[]
3879        );
3880
3881        buffer_1.update(cx, |buffer, cx| {
3882            let text = "\n";
3883            buffer.edit(
3884                [
3885                    (Point::new(0, 0)..Point::new(0, 0), text),
3886                    (Point::new(2, 1)..Point::new(2, 3), text),
3887                ],
3888                None,
3889                cx,
3890            );
3891        });
3892
3893        let snapshot = multibuffer.read(cx).snapshot(cx);
3894        assert_eq!(
3895            snapshot.text(),
3896            concat!(
3897                "bbbb\n", // Preserve newlines
3898                "c\n",    //
3899                "cc\n",   //
3900                "ddd\n",  //
3901                "eeee\n", //
3902                "jj"      //
3903            )
3904        );
3905
3906        assert_eq!(
3907            subscription.consume().into_inner(),
3908            [Edit {
3909                old: 6..8,
3910                new: 6..7
3911            }]
3912        );
3913
3914        let snapshot = multibuffer.read(cx).snapshot(cx);
3915        assert_eq!(
3916            snapshot.clip_point(Point::new(0, 5), Bias::Left),
3917            Point::new(0, 4)
3918        );
3919        assert_eq!(
3920            snapshot.clip_point(Point::new(0, 5), Bias::Right),
3921            Point::new(0, 4)
3922        );
3923        assert_eq!(
3924            snapshot.clip_point(Point::new(5, 1), Bias::Right),
3925            Point::new(5, 1)
3926        );
3927        assert_eq!(
3928            snapshot.clip_point(Point::new(5, 2), Bias::Right),
3929            Point::new(5, 2)
3930        );
3931        assert_eq!(
3932            snapshot.clip_point(Point::new(5, 3), Bias::Right),
3933            Point::new(5, 2)
3934        );
3935
3936        let snapshot = multibuffer.update(cx, |multibuffer, cx| {
3937            let (buffer_2_excerpt_id, _) =
3938                multibuffer.excerpts_for_buffer(&buffer_2, cx)[0].clone();
3939            multibuffer.remove_excerpts([buffer_2_excerpt_id], cx);
3940            multibuffer.snapshot(cx)
3941        });
3942
3943        assert_eq!(
3944            snapshot.text(),
3945            concat!(
3946                "bbbb\n", // Preserve newlines
3947                "c\n",    //
3948                "cc\n",   //
3949                "ddd\n",  //
3950                "eeee",   //
3951            )
3952        );
3953
3954        fn boundaries_in_range(
3955            range: Range<Point>,
3956            snapshot: &MultiBufferSnapshot,
3957        ) -> Vec<(u32, String, bool)> {
3958            snapshot
3959                .excerpt_boundaries_in_range(range)
3960                .map(|boundary| {
3961                    (
3962                        boundary.row,
3963                        boundary
3964                            .buffer
3965                            .text_for_range(boundary.range.context)
3966                            .collect::<String>(),
3967                        boundary.starts_new_buffer,
3968                    )
3969                })
3970                .collect::<Vec<_>>()
3971        }
3972    }
3973
3974    #[gpui::test]
3975    fn test_excerpt_events(cx: &mut MutableAppContext) {
3976        let buffer_1 = cx.add_model(|cx| Buffer::new(0, sample_text(10, 3, 'a'), cx));
3977        let buffer_2 = cx.add_model(|cx| Buffer::new(0, sample_text(10, 3, 'm'), cx));
3978
3979        let leader_multibuffer = cx.add_model(|_| MultiBuffer::new(0));
3980        let follower_multibuffer = cx.add_model(|_| MultiBuffer::new(0));
3981
3982        follower_multibuffer.update(cx, |_, cx| {
3983            cx.subscribe(&leader_multibuffer, |follower, _, event, cx| {
3984                match event.clone() {
3985                    Event::ExcerptsAdded {
3986                        buffer,
3987                        predecessor,
3988                        excerpts,
3989                    } => follower.insert_excerpts_with_ids_after(predecessor, buffer, excerpts, cx),
3990                    Event::ExcerptsRemoved { ids } => follower.remove_excerpts(ids, cx),
3991                    _ => {}
3992                }
3993            })
3994            .detach();
3995        });
3996
3997        leader_multibuffer.update(cx, |leader, cx| {
3998            leader.push_excerpts(
3999                buffer_1.clone(),
4000                [
4001                    ExcerptRange {
4002                        context: 0..8,
4003                        primary: None,
4004                    },
4005                    ExcerptRange {
4006                        context: 12..16,
4007                        primary: None,
4008                    },
4009                ],
4010                cx,
4011            );
4012            leader.insert_excerpts_after(
4013                leader.excerpt_ids()[0],
4014                buffer_2.clone(),
4015                [
4016                    ExcerptRange {
4017                        context: 0..5,
4018                        primary: None,
4019                    },
4020                    ExcerptRange {
4021                        context: 10..15,
4022                        primary: None,
4023                    },
4024                ],
4025                cx,
4026            )
4027        });
4028        assert_eq!(
4029            leader_multibuffer.read(cx).snapshot(cx).text(),
4030            follower_multibuffer.read(cx).snapshot(cx).text(),
4031        );
4032
4033        leader_multibuffer.update(cx, |leader, cx| {
4034            let excerpt_ids = leader.excerpt_ids();
4035            leader.remove_excerpts([excerpt_ids[1], excerpt_ids[3]], cx);
4036        });
4037        assert_eq!(
4038            leader_multibuffer.read(cx).snapshot(cx).text(),
4039            follower_multibuffer.read(cx).snapshot(cx).text(),
4040        );
4041
4042        leader_multibuffer.update(cx, |leader, cx| {
4043            leader.clear(cx);
4044        });
4045        assert_eq!(
4046            leader_multibuffer.read(cx).snapshot(cx).text(),
4047            follower_multibuffer.read(cx).snapshot(cx).text(),
4048        );
4049    }
4050
4051    #[gpui::test]
4052    fn test_push_excerpts_with_context_lines(cx: &mut MutableAppContext) {
4053        let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(20, 3, 'a'), cx));
4054        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4055        let anchor_ranges = multibuffer.update(cx, |multibuffer, cx| {
4056            multibuffer.push_excerpts_with_context_lines(
4057                buffer.clone(),
4058                vec![
4059                    Point::new(3, 2)..Point::new(4, 2),
4060                    Point::new(7, 1)..Point::new(7, 3),
4061                    Point::new(15, 0)..Point::new(15, 0),
4062                ],
4063                2,
4064                cx,
4065            )
4066        });
4067
4068        let snapshot = multibuffer.read(cx).snapshot(cx);
4069        assert_eq!(
4070            snapshot.text(),
4071            "bbb\nccc\nddd\neee\nfff\nggg\nhhh\niii\njjj\n\nnnn\nooo\nppp\nqqq\nrrr\n"
4072        );
4073
4074        assert_eq!(
4075            anchor_ranges
4076                .iter()
4077                .map(|range| range.to_point(&snapshot))
4078                .collect::<Vec<_>>(),
4079            vec![
4080                Point::new(2, 2)..Point::new(3, 2),
4081                Point::new(6, 1)..Point::new(6, 3),
4082                Point::new(12, 0)..Point::new(12, 0)
4083            ]
4084        );
4085    }
4086
4087    #[gpui::test]
4088    async fn test_stream_excerpts_with_context_lines(cx: &mut TestAppContext) {
4089        let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(20, 3, 'a'), cx));
4090        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4091        let (task, anchor_ranges) = multibuffer.update(cx, |multibuffer, cx| {
4092            let snapshot = buffer.read(cx);
4093            let ranges = vec![
4094                snapshot.anchor_before(Point::new(3, 2))..snapshot.anchor_before(Point::new(4, 2)),
4095                snapshot.anchor_before(Point::new(7, 1))..snapshot.anchor_before(Point::new(7, 3)),
4096                snapshot.anchor_before(Point::new(15, 0))
4097                    ..snapshot.anchor_before(Point::new(15, 0)),
4098            ];
4099            multibuffer.stream_excerpts_with_context_lines(vec![(buffer.clone(), ranges)], 2, cx)
4100        });
4101
4102        let anchor_ranges = anchor_ranges.collect::<Vec<_>>().await;
4103        // Ensure task is finished when stream completes.
4104        task.await;
4105
4106        let snapshot = multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
4107        assert_eq!(
4108            snapshot.text(),
4109            "bbb\nccc\nddd\neee\nfff\nggg\nhhh\niii\njjj\n\nnnn\nooo\nppp\nqqq\nrrr\n"
4110        );
4111
4112        assert_eq!(
4113            anchor_ranges
4114                .iter()
4115                .map(|range| range.to_point(&snapshot))
4116                .collect::<Vec<_>>(),
4117            vec![
4118                Point::new(2, 2)..Point::new(3, 2),
4119                Point::new(6, 1)..Point::new(6, 3),
4120                Point::new(12, 0)..Point::new(12, 0)
4121            ]
4122        );
4123    }
4124
4125    #[gpui::test]
4126    fn test_empty_multibuffer(cx: &mut MutableAppContext) {
4127        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4128
4129        let snapshot = multibuffer.read(cx).snapshot(cx);
4130        assert_eq!(snapshot.text(), "");
4131        assert_eq!(snapshot.buffer_rows(0).collect::<Vec<_>>(), &[Some(0)]);
4132        assert_eq!(snapshot.buffer_rows(1).collect::<Vec<_>>(), &[]);
4133    }
4134
4135    #[gpui::test]
4136    fn test_singleton_multibuffer_anchors(cx: &mut MutableAppContext) {
4137        let buffer = cx.add_model(|cx| Buffer::new(0, "abcd", cx));
4138        let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4139        let old_snapshot = multibuffer.read(cx).snapshot(cx);
4140        buffer.update(cx, |buffer, cx| {
4141            buffer.edit([(0..0, "X")], None, cx);
4142            buffer.edit([(5..5, "Y")], None, cx);
4143        });
4144        let new_snapshot = multibuffer.read(cx).snapshot(cx);
4145
4146        assert_eq!(old_snapshot.text(), "abcd");
4147        assert_eq!(new_snapshot.text(), "XabcdY");
4148
4149        assert_eq!(old_snapshot.anchor_before(0).to_offset(&new_snapshot), 0);
4150        assert_eq!(old_snapshot.anchor_after(0).to_offset(&new_snapshot), 1);
4151        assert_eq!(old_snapshot.anchor_before(4).to_offset(&new_snapshot), 5);
4152        assert_eq!(old_snapshot.anchor_after(4).to_offset(&new_snapshot), 6);
4153    }
4154
4155    #[gpui::test]
4156    fn test_multibuffer_anchors(cx: &mut MutableAppContext) {
4157        let buffer_1 = cx.add_model(|cx| Buffer::new(0, "abcd", cx));
4158        let buffer_2 = cx.add_model(|cx| Buffer::new(0, "efghi", cx));
4159        let multibuffer = cx.add_model(|cx| {
4160            let mut multibuffer = MultiBuffer::new(0);
4161            multibuffer.push_excerpts(
4162                buffer_1.clone(),
4163                [ExcerptRange {
4164                    context: 0..4,
4165                    primary: None,
4166                }],
4167                cx,
4168            );
4169            multibuffer.push_excerpts(
4170                buffer_2.clone(),
4171                [ExcerptRange {
4172                    context: 0..5,
4173                    primary: None,
4174                }],
4175                cx,
4176            );
4177            multibuffer
4178        });
4179        let old_snapshot = multibuffer.read(cx).snapshot(cx);
4180
4181        assert_eq!(old_snapshot.anchor_before(0).to_offset(&old_snapshot), 0);
4182        assert_eq!(old_snapshot.anchor_after(0).to_offset(&old_snapshot), 0);
4183        assert_eq!(Anchor::min().to_offset(&old_snapshot), 0);
4184        assert_eq!(Anchor::min().to_offset(&old_snapshot), 0);
4185        assert_eq!(Anchor::max().to_offset(&old_snapshot), 10);
4186        assert_eq!(Anchor::max().to_offset(&old_snapshot), 10);
4187
4188        buffer_1.update(cx, |buffer, cx| {
4189            buffer.edit([(0..0, "W")], None, cx);
4190            buffer.edit([(5..5, "X")], None, cx);
4191        });
4192        buffer_2.update(cx, |buffer, cx| {
4193            buffer.edit([(0..0, "Y")], None, cx);
4194            buffer.edit([(6..6, "Z")], None, cx);
4195        });
4196        let new_snapshot = multibuffer.read(cx).snapshot(cx);
4197
4198        assert_eq!(old_snapshot.text(), "abcd\nefghi");
4199        assert_eq!(new_snapshot.text(), "WabcdX\nYefghiZ");
4200
4201        assert_eq!(old_snapshot.anchor_before(0).to_offset(&new_snapshot), 0);
4202        assert_eq!(old_snapshot.anchor_after(0).to_offset(&new_snapshot), 1);
4203        assert_eq!(old_snapshot.anchor_before(1).to_offset(&new_snapshot), 2);
4204        assert_eq!(old_snapshot.anchor_after(1).to_offset(&new_snapshot), 2);
4205        assert_eq!(old_snapshot.anchor_before(2).to_offset(&new_snapshot), 3);
4206        assert_eq!(old_snapshot.anchor_after(2).to_offset(&new_snapshot), 3);
4207        assert_eq!(old_snapshot.anchor_before(5).to_offset(&new_snapshot), 7);
4208        assert_eq!(old_snapshot.anchor_after(5).to_offset(&new_snapshot), 8);
4209        assert_eq!(old_snapshot.anchor_before(10).to_offset(&new_snapshot), 13);
4210        assert_eq!(old_snapshot.anchor_after(10).to_offset(&new_snapshot), 14);
4211    }
4212
4213    #[gpui::test]
4214    fn test_resolving_anchors_after_replacing_their_excerpts(cx: &mut MutableAppContext) {
4215        let buffer_1 = cx.add_model(|cx| Buffer::new(0, "abcd", cx));
4216        let buffer_2 = cx.add_model(|cx| Buffer::new(0, "ABCDEFGHIJKLMNOP", cx));
4217        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4218
4219        // Create an insertion id in buffer 1 that doesn't exist in buffer 2.
4220        // Add an excerpt from buffer 1 that spans this new insertion.
4221        buffer_1.update(cx, |buffer, cx| buffer.edit([(4..4, "123")], None, cx));
4222        let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
4223            multibuffer
4224                .push_excerpts(
4225                    buffer_1.clone(),
4226                    [ExcerptRange {
4227                        context: 0..7,
4228                        primary: None,
4229                    }],
4230                    cx,
4231                )
4232                .pop()
4233                .unwrap()
4234        });
4235
4236        let snapshot_1 = multibuffer.read(cx).snapshot(cx);
4237        assert_eq!(snapshot_1.text(), "abcd123");
4238
4239        // Replace the buffer 1 excerpt with new excerpts from buffer 2.
4240        let (excerpt_id_2, excerpt_id_3) = multibuffer.update(cx, |multibuffer, cx| {
4241            multibuffer.remove_excerpts([excerpt_id_1], cx);
4242            let mut ids = multibuffer
4243                .push_excerpts(
4244                    buffer_2.clone(),
4245                    [
4246                        ExcerptRange {
4247                            context: 0..4,
4248                            primary: None,
4249                        },
4250                        ExcerptRange {
4251                            context: 6..10,
4252                            primary: None,
4253                        },
4254                        ExcerptRange {
4255                            context: 12..16,
4256                            primary: None,
4257                        },
4258                    ],
4259                    cx,
4260                )
4261                .into_iter();
4262            (ids.next().unwrap(), ids.next().unwrap())
4263        });
4264        let snapshot_2 = multibuffer.read(cx).snapshot(cx);
4265        assert_eq!(snapshot_2.text(), "ABCD\nGHIJ\nMNOP");
4266
4267        // The old excerpt id doesn't get reused.
4268        assert_ne!(excerpt_id_2, excerpt_id_1);
4269
4270        // Resolve some anchors from the previous snapshot in the new snapshot.
4271        // The current excerpts are from a different buffer, so we don't attempt to
4272        // resolve the old text anchor in the new buffer.
4273        assert_eq!(
4274            snapshot_2.summary_for_anchor::<usize>(&snapshot_1.anchor_before(2)),
4275            0
4276        );
4277        assert_eq!(
4278            snapshot_2.summaries_for_anchors::<usize, _>(&[
4279                snapshot_1.anchor_before(2),
4280                snapshot_1.anchor_after(3)
4281            ]),
4282            vec![0, 0]
4283        );
4284
4285        // Refresh anchors from the old snapshot. The return value indicates that both
4286        // anchors lost their original excerpt.
4287        let refresh =
4288            snapshot_2.refresh_anchors(&[snapshot_1.anchor_before(2), snapshot_1.anchor_after(3)]);
4289        assert_eq!(
4290            refresh,
4291            &[
4292                (0, snapshot_2.anchor_before(0), false),
4293                (1, snapshot_2.anchor_after(0), false),
4294            ]
4295        );
4296
4297        // Replace the middle excerpt with a smaller excerpt in buffer 2,
4298        // that intersects the old excerpt.
4299        let excerpt_id_5 = multibuffer.update(cx, |multibuffer, cx| {
4300            multibuffer.remove_excerpts([excerpt_id_3], cx);
4301            multibuffer
4302                .insert_excerpts_after(
4303                    excerpt_id_2,
4304                    buffer_2.clone(),
4305                    [ExcerptRange {
4306                        context: 5..8,
4307                        primary: None,
4308                    }],
4309                    cx,
4310                )
4311                .pop()
4312                .unwrap()
4313        });
4314
4315        let snapshot_3 = multibuffer.read(cx).snapshot(cx);
4316        assert_eq!(snapshot_3.text(), "ABCD\nFGH\nMNOP");
4317        assert_ne!(excerpt_id_5, excerpt_id_3);
4318
4319        // Resolve some anchors from the previous snapshot in the new snapshot.
4320        // The third anchor can't be resolved, since its excerpt has been removed,
4321        // so it resolves to the same position as its predecessor.
4322        let anchors = [
4323            snapshot_2.anchor_before(0),
4324            snapshot_2.anchor_after(2),
4325            snapshot_2.anchor_after(6),
4326            snapshot_2.anchor_after(14),
4327        ];
4328        assert_eq!(
4329            snapshot_3.summaries_for_anchors::<usize, _>(&anchors),
4330            &[0, 2, 9, 13]
4331        );
4332
4333        let new_anchors = snapshot_3.refresh_anchors(&anchors);
4334        assert_eq!(
4335            new_anchors.iter().map(|a| (a.0, a.2)).collect::<Vec<_>>(),
4336            &[(0, true), (1, true), (2, true), (3, true)]
4337        );
4338        assert_eq!(
4339            snapshot_3.summaries_for_anchors::<usize, _>(new_anchors.iter().map(|a| &a.1)),
4340            &[0, 2, 7, 13]
4341        );
4342    }
4343
4344    #[gpui::test]
4345    async fn test_diff_hunks_in_range(cx: &mut TestAppContext) {
4346        use git::diff::DiffHunkStatus;
4347
4348        // buffer has two modified hunks with two rows each
4349        let buffer_1 = cx.add_model(|cx| {
4350            let mut buffer = Buffer::new(
4351                0,
4352                "
4353                1.zero
4354                1.ONE
4355                1.TWO
4356                1.three
4357                1.FOUR
4358                1.FIVE
4359                1.six
4360            "
4361                .unindent(),
4362                cx,
4363            );
4364            buffer.set_diff_base(
4365                Some(
4366                    "
4367                1.zero
4368                1.one
4369                1.two
4370                1.three
4371                1.four
4372                1.five
4373                1.six
4374            "
4375                    .unindent(),
4376                ),
4377                cx,
4378            );
4379            buffer
4380        });
4381
4382        // buffer has a deletion hunk and an insertion hunk
4383        let buffer_2 = cx.add_model(|cx| {
4384            let mut buffer = Buffer::new(
4385                0,
4386                "
4387                2.zero
4388                2.one
4389                2.two
4390                2.three
4391                2.four
4392                2.five
4393                2.six
4394            "
4395                .unindent(),
4396                cx,
4397            );
4398            buffer.set_diff_base(
4399                Some(
4400                    "
4401                2.zero
4402                2.one
4403                2.one-and-a-half
4404                2.two
4405                2.three
4406                2.four
4407                2.six
4408            "
4409                    .unindent(),
4410                ),
4411                cx,
4412            );
4413            buffer
4414        });
4415
4416        cx.foreground().run_until_parked();
4417
4418        let multibuffer = cx.add_model(|cx| {
4419            let mut multibuffer = MultiBuffer::new(0);
4420            multibuffer.push_excerpts(
4421                buffer_1.clone(),
4422                [
4423                    // excerpt ends in the middle of a modified hunk
4424                    ExcerptRange {
4425                        context: Point::new(0, 0)..Point::new(1, 5),
4426                        primary: Default::default(),
4427                    },
4428                    // excerpt begins in the middle of a modified hunk
4429                    ExcerptRange {
4430                        context: Point::new(5, 0)..Point::new(6, 5),
4431                        primary: Default::default(),
4432                    },
4433                ],
4434                cx,
4435            );
4436            multibuffer.push_excerpts(
4437                buffer_2.clone(),
4438                [
4439                    // excerpt ends at a deletion
4440                    ExcerptRange {
4441                        context: Point::new(0, 0)..Point::new(1, 5),
4442                        primary: Default::default(),
4443                    },
4444                    // excerpt starts at a deletion
4445                    ExcerptRange {
4446                        context: Point::new(2, 0)..Point::new(2, 5),
4447                        primary: Default::default(),
4448                    },
4449                    // excerpt fully contains a deletion hunk
4450                    ExcerptRange {
4451                        context: Point::new(1, 0)..Point::new(2, 5),
4452                        primary: Default::default(),
4453                    },
4454                    // excerpt fully contains an insertion hunk
4455                    ExcerptRange {
4456                        context: Point::new(4, 0)..Point::new(6, 5),
4457                        primary: Default::default(),
4458                    },
4459                ],
4460                cx,
4461            );
4462            multibuffer
4463        });
4464
4465        let snapshot = multibuffer.read_with(cx, |b, cx| b.snapshot(cx));
4466
4467        assert_eq!(
4468            snapshot.text(),
4469            "
4470                1.zero
4471                1.ONE
4472                1.FIVE
4473                1.six
4474                2.zero
4475                2.one
4476                2.two
4477                2.one
4478                2.two
4479                2.four
4480                2.five
4481                2.six"
4482                .unindent()
4483        );
4484
4485        let expected = [
4486            (DiffHunkStatus::Modified, 1..2),
4487            (DiffHunkStatus::Modified, 2..3),
4488            //TODO: Define better when and where removed hunks show up at range extremities
4489            (DiffHunkStatus::Removed, 6..6),
4490            (DiffHunkStatus::Removed, 8..8),
4491            (DiffHunkStatus::Added, 10..11),
4492        ];
4493
4494        assert_eq!(
4495            snapshot
4496                .git_diff_hunks_in_range(0..12, false)
4497                .map(|hunk| (hunk.status(), hunk.buffer_range))
4498                .collect::<Vec<_>>(),
4499            &expected,
4500        );
4501
4502        assert_eq!(
4503            snapshot
4504                .git_diff_hunks_in_range(0..12, true)
4505                .map(|hunk| (hunk.status(), hunk.buffer_range))
4506                .collect::<Vec<_>>(),
4507            expected
4508                .iter()
4509                .rev()
4510                .cloned()
4511                .collect::<Vec<_>>()
4512                .as_slice(),
4513        );
4514    }
4515
4516    #[gpui::test(iterations = 100)]
4517    fn test_random_multibuffer(cx: &mut MutableAppContext, mut rng: StdRng) {
4518        let operations = env::var("OPERATIONS")
4519            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
4520            .unwrap_or(10);
4521
4522        let mut buffers: Vec<ModelHandle<Buffer>> = Vec::new();
4523        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4524        let mut excerpt_ids = Vec::<ExcerptId>::new();
4525        let mut expected_excerpts = Vec::<(ModelHandle<Buffer>, Range<text::Anchor>)>::new();
4526        let mut anchors = Vec::new();
4527        let mut old_versions = Vec::new();
4528
4529        for _ in 0..operations {
4530            match rng.gen_range(0..100) {
4531                0..=19 if !buffers.is_empty() => {
4532                    let buffer = buffers.choose(&mut rng).unwrap();
4533                    buffer.update(cx, |buf, cx| buf.randomly_edit(&mut rng, 5, cx));
4534                }
4535                20..=29 if !expected_excerpts.is_empty() => {
4536                    let mut ids_to_remove = vec![];
4537                    for _ in 0..rng.gen_range(1..=3) {
4538                        if expected_excerpts.is_empty() {
4539                            break;
4540                        }
4541
4542                        let ix = rng.gen_range(0..expected_excerpts.len());
4543                        ids_to_remove.push(excerpt_ids.remove(ix));
4544                        let (buffer, range) = expected_excerpts.remove(ix);
4545                        let buffer = buffer.read(cx);
4546                        log::info!(
4547                            "Removing excerpt {}: {:?}",
4548                            ix,
4549                            buffer
4550                                .text_for_range(range.to_offset(buffer))
4551                                .collect::<String>(),
4552                        );
4553                    }
4554                    let snapshot = multibuffer.read(cx).read(cx);
4555                    ids_to_remove.sort_unstable_by(|a, b| a.cmp(&b, &snapshot));
4556                    drop(snapshot);
4557                    multibuffer.update(cx, |multibuffer, cx| {
4558                        multibuffer.remove_excerpts(ids_to_remove, cx)
4559                    });
4560                }
4561                30..=39 if !expected_excerpts.is_empty() => {
4562                    let multibuffer = multibuffer.read(cx).read(cx);
4563                    let offset =
4564                        multibuffer.clip_offset(rng.gen_range(0..=multibuffer.len()), Bias::Left);
4565                    let bias = if rng.gen() { Bias::Left } else { Bias::Right };
4566                    log::info!("Creating anchor at {} with bias {:?}", offset, bias);
4567                    anchors.push(multibuffer.anchor_at(offset, bias));
4568                    anchors.sort_by(|a, b| a.cmp(b, &multibuffer));
4569                }
4570                40..=44 if !anchors.is_empty() => {
4571                    let multibuffer = multibuffer.read(cx).read(cx);
4572                    let prev_len = anchors.len();
4573                    anchors = multibuffer
4574                        .refresh_anchors(&anchors)
4575                        .into_iter()
4576                        .map(|a| a.1)
4577                        .collect();
4578
4579                    // Ensure the newly-refreshed anchors point to a valid excerpt and don't
4580                    // overshoot its boundaries.
4581                    assert_eq!(anchors.len(), prev_len);
4582                    for anchor in &anchors {
4583                        if anchor.excerpt_id == ExcerptId::min()
4584                            || anchor.excerpt_id == ExcerptId::max()
4585                        {
4586                            continue;
4587                        }
4588
4589                        let excerpt = multibuffer.excerpt(anchor.excerpt_id).unwrap();
4590                        assert_eq!(excerpt.id, anchor.excerpt_id);
4591                        assert!(excerpt.contains(anchor));
4592                    }
4593                }
4594                _ => {
4595                    let buffer_handle = if buffers.is_empty() || rng.gen_bool(0.4) {
4596                        let base_text = util::RandomCharIter::new(&mut rng)
4597                            .take(10)
4598                            .collect::<String>();
4599                        buffers.push(cx.add_model(|cx| Buffer::new(0, base_text, cx)));
4600                        buffers.last().unwrap()
4601                    } else {
4602                        buffers.choose(&mut rng).unwrap()
4603                    };
4604
4605                    let buffer = buffer_handle.read(cx);
4606                    let end_ix = buffer.clip_offset(rng.gen_range(0..=buffer.len()), Bias::Right);
4607                    let start_ix = buffer.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
4608                    let anchor_range = buffer.anchor_before(start_ix)..buffer.anchor_after(end_ix);
4609                    let prev_excerpt_ix = rng.gen_range(0..=expected_excerpts.len());
4610                    let prev_excerpt_id = excerpt_ids
4611                        .get(prev_excerpt_ix)
4612                        .cloned()
4613                        .unwrap_or_else(ExcerptId::max);
4614                    let excerpt_ix = (prev_excerpt_ix + 1).min(expected_excerpts.len());
4615
4616                    log::info!(
4617                        "Inserting excerpt at {} of {} for buffer {}: {:?}[{:?}] = {:?}",
4618                        excerpt_ix,
4619                        expected_excerpts.len(),
4620                        buffer_handle.id(),
4621                        buffer.text(),
4622                        start_ix..end_ix,
4623                        &buffer.text()[start_ix..end_ix]
4624                    );
4625
4626                    let excerpt_id = multibuffer.update(cx, |multibuffer, cx| {
4627                        multibuffer
4628                            .insert_excerpts_after(
4629                                prev_excerpt_id,
4630                                buffer_handle.clone(),
4631                                [ExcerptRange {
4632                                    context: start_ix..end_ix,
4633                                    primary: None,
4634                                }],
4635                                cx,
4636                            )
4637                            .pop()
4638                            .unwrap()
4639                    });
4640
4641                    excerpt_ids.insert(excerpt_ix, excerpt_id);
4642                    expected_excerpts.insert(excerpt_ix, (buffer_handle.clone(), anchor_range));
4643                }
4644            }
4645
4646            if rng.gen_bool(0.3) {
4647                multibuffer.update(cx, |multibuffer, cx| {
4648                    old_versions.push((multibuffer.snapshot(cx), multibuffer.subscribe()));
4649                })
4650            }
4651
4652            let snapshot = multibuffer.read(cx).snapshot(cx);
4653
4654            let mut excerpt_starts = Vec::new();
4655            let mut expected_text = String::new();
4656            let mut expected_buffer_rows = Vec::new();
4657            for (buffer, range) in &expected_excerpts {
4658                let buffer = buffer.read(cx);
4659                let buffer_range = range.to_offset(buffer);
4660
4661                excerpt_starts.push(TextSummary::from(expected_text.as_str()));
4662                expected_text.extend(buffer.text_for_range(buffer_range.clone()));
4663                expected_text.push('\n');
4664
4665                let buffer_row_range = buffer.offset_to_point(buffer_range.start).row
4666                    ..=buffer.offset_to_point(buffer_range.end).row;
4667                for row in buffer_row_range {
4668                    expected_buffer_rows.push(Some(row));
4669                }
4670            }
4671            // Remove final trailing newline.
4672            if !expected_excerpts.is_empty() {
4673                expected_text.pop();
4674            }
4675
4676            // Always report one buffer row
4677            if expected_buffer_rows.is_empty() {
4678                expected_buffer_rows.push(Some(0));
4679            }
4680
4681            assert_eq!(snapshot.text(), expected_text);
4682            log::info!("MultiBuffer text: {:?}", expected_text);
4683
4684            assert_eq!(
4685                snapshot.buffer_rows(0).collect::<Vec<_>>(),
4686                expected_buffer_rows,
4687            );
4688
4689            for _ in 0..5 {
4690                let start_row = rng.gen_range(0..=expected_buffer_rows.len());
4691                assert_eq!(
4692                    snapshot.buffer_rows(start_row as u32).collect::<Vec<_>>(),
4693                    &expected_buffer_rows[start_row..],
4694                    "buffer_rows({})",
4695                    start_row
4696                );
4697            }
4698
4699            assert_eq!(
4700                snapshot.max_buffer_row(),
4701                expected_buffer_rows.into_iter().flatten().max().unwrap()
4702            );
4703
4704            let mut excerpt_starts = excerpt_starts.into_iter();
4705            for (buffer, range) in &expected_excerpts {
4706                let buffer_id = buffer.id();
4707                let buffer = buffer.read(cx);
4708                let buffer_range = range.to_offset(buffer);
4709                let buffer_start_point = buffer.offset_to_point(buffer_range.start);
4710                let buffer_start_point_utf16 =
4711                    buffer.text_summary_for_range::<PointUtf16, _>(0..buffer_range.start);
4712
4713                let excerpt_start = excerpt_starts.next().unwrap();
4714                let mut offset = excerpt_start.len;
4715                let mut buffer_offset = buffer_range.start;
4716                let mut point = excerpt_start.lines;
4717                let mut buffer_point = buffer_start_point;
4718                let mut point_utf16 = excerpt_start.lines_utf16();
4719                let mut buffer_point_utf16 = buffer_start_point_utf16;
4720                for ch in buffer
4721                    .snapshot()
4722                    .chunks(buffer_range.clone(), false)
4723                    .flat_map(|c| c.text.chars())
4724                {
4725                    for _ in 0..ch.len_utf8() {
4726                        let left_offset = snapshot.clip_offset(offset, Bias::Left);
4727                        let right_offset = snapshot.clip_offset(offset, Bias::Right);
4728                        let buffer_left_offset = buffer.clip_offset(buffer_offset, Bias::Left);
4729                        let buffer_right_offset = buffer.clip_offset(buffer_offset, Bias::Right);
4730                        assert_eq!(
4731                            left_offset,
4732                            excerpt_start.len + (buffer_left_offset - buffer_range.start),
4733                            "clip_offset({:?}, Left). buffer: {:?}, buffer offset: {:?}",
4734                            offset,
4735                            buffer_id,
4736                            buffer_offset,
4737                        );
4738                        assert_eq!(
4739                            right_offset,
4740                            excerpt_start.len + (buffer_right_offset - buffer_range.start),
4741                            "clip_offset({:?}, Right). buffer: {:?}, buffer offset: {:?}",
4742                            offset,
4743                            buffer_id,
4744                            buffer_offset,
4745                        );
4746
4747                        let left_point = snapshot.clip_point(point, Bias::Left);
4748                        let right_point = snapshot.clip_point(point, Bias::Right);
4749                        let buffer_left_point = buffer.clip_point(buffer_point, Bias::Left);
4750                        let buffer_right_point = buffer.clip_point(buffer_point, Bias::Right);
4751                        assert_eq!(
4752                            left_point,
4753                            excerpt_start.lines + (buffer_left_point - buffer_start_point),
4754                            "clip_point({:?}, Left). buffer: {:?}, buffer point: {:?}",
4755                            point,
4756                            buffer_id,
4757                            buffer_point,
4758                        );
4759                        assert_eq!(
4760                            right_point,
4761                            excerpt_start.lines + (buffer_right_point - buffer_start_point),
4762                            "clip_point({:?}, Right). buffer: {:?}, buffer point: {:?}",
4763                            point,
4764                            buffer_id,
4765                            buffer_point,
4766                        );
4767
4768                        assert_eq!(
4769                            snapshot.point_to_offset(left_point),
4770                            left_offset,
4771                            "point_to_offset({:?})",
4772                            left_point,
4773                        );
4774                        assert_eq!(
4775                            snapshot.offset_to_point(left_offset),
4776                            left_point,
4777                            "offset_to_point({:?})",
4778                            left_offset,
4779                        );
4780
4781                        offset += 1;
4782                        buffer_offset += 1;
4783                        if ch == '\n' {
4784                            point += Point::new(1, 0);
4785                            buffer_point += Point::new(1, 0);
4786                        } else {
4787                            point += Point::new(0, 1);
4788                            buffer_point += Point::new(0, 1);
4789                        }
4790                    }
4791
4792                    for _ in 0..ch.len_utf16() {
4793                        let left_point_utf16 =
4794                            snapshot.clip_point_utf16(Unclipped(point_utf16), Bias::Left);
4795                        let right_point_utf16 =
4796                            snapshot.clip_point_utf16(Unclipped(point_utf16), Bias::Right);
4797                        let buffer_left_point_utf16 =
4798                            buffer.clip_point_utf16(Unclipped(buffer_point_utf16), Bias::Left);
4799                        let buffer_right_point_utf16 =
4800                            buffer.clip_point_utf16(Unclipped(buffer_point_utf16), Bias::Right);
4801                        assert_eq!(
4802                            left_point_utf16,
4803                            excerpt_start.lines_utf16()
4804                                + (buffer_left_point_utf16 - buffer_start_point_utf16),
4805                            "clip_point_utf16({:?}, Left). buffer: {:?}, buffer point_utf16: {:?}",
4806                            point_utf16,
4807                            buffer_id,
4808                            buffer_point_utf16,
4809                        );
4810                        assert_eq!(
4811                            right_point_utf16,
4812                            excerpt_start.lines_utf16()
4813                                + (buffer_right_point_utf16 - buffer_start_point_utf16),
4814                            "clip_point_utf16({:?}, Right). buffer: {:?}, buffer point_utf16: {:?}",
4815                            point_utf16,
4816                            buffer_id,
4817                            buffer_point_utf16,
4818                        );
4819
4820                        if ch == '\n' {
4821                            point_utf16 += PointUtf16::new(1, 0);
4822                            buffer_point_utf16 += PointUtf16::new(1, 0);
4823                        } else {
4824                            point_utf16 += PointUtf16::new(0, 1);
4825                            buffer_point_utf16 += PointUtf16::new(0, 1);
4826                        }
4827                    }
4828                }
4829            }
4830
4831            for (row, line) in expected_text.split('\n').enumerate() {
4832                assert_eq!(
4833                    snapshot.line_len(row as u32),
4834                    line.len() as u32,
4835                    "line_len({}).",
4836                    row
4837                );
4838            }
4839
4840            let text_rope = Rope::from(expected_text.as_str());
4841            for _ in 0..10 {
4842                let end_ix = text_rope.clip_offset(rng.gen_range(0..=text_rope.len()), Bias::Right);
4843                let start_ix = text_rope.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
4844
4845                let text_for_range = snapshot
4846                    .text_for_range(start_ix..end_ix)
4847                    .collect::<String>();
4848                assert_eq!(
4849                    text_for_range,
4850                    &expected_text[start_ix..end_ix],
4851                    "incorrect text for range {:?}",
4852                    start_ix..end_ix
4853                );
4854
4855                let excerpted_buffer_ranges = multibuffer
4856                    .read(cx)
4857                    .range_to_buffer_ranges(start_ix..end_ix, cx);
4858                let excerpted_buffers_text = excerpted_buffer_ranges
4859                    .into_iter()
4860                    .map(|(buffer, buffer_range)| {
4861                        buffer
4862                            .read(cx)
4863                            .text_for_range(buffer_range)
4864                            .collect::<String>()
4865                    })
4866                    .collect::<Vec<_>>()
4867                    .join("\n");
4868                assert_eq!(excerpted_buffers_text, text_for_range);
4869
4870                let expected_summary = TextSummary::from(&expected_text[start_ix..end_ix]);
4871                assert_eq!(
4872                    snapshot.text_summary_for_range::<TextSummary, _>(start_ix..end_ix),
4873                    expected_summary,
4874                    "incorrect summary for range {:?}",
4875                    start_ix..end_ix
4876                );
4877            }
4878
4879            // Anchor resolution
4880            let summaries = snapshot.summaries_for_anchors::<usize, _>(&anchors);
4881            assert_eq!(anchors.len(), summaries.len());
4882            for (anchor, resolved_offset) in anchors.iter().zip(summaries) {
4883                assert!(resolved_offset <= snapshot.len());
4884                assert_eq!(
4885                    snapshot.summary_for_anchor::<usize>(anchor),
4886                    resolved_offset
4887                );
4888            }
4889
4890            for _ in 0..10 {
4891                let end_ix = text_rope.clip_offset(rng.gen_range(0..=text_rope.len()), Bias::Right);
4892                assert_eq!(
4893                    snapshot.reversed_chars_at(end_ix).collect::<String>(),
4894                    expected_text[..end_ix].chars().rev().collect::<String>(),
4895                );
4896            }
4897
4898            for _ in 0..10 {
4899                let end_ix = rng.gen_range(0..=text_rope.len());
4900                let start_ix = rng.gen_range(0..=end_ix);
4901                assert_eq!(
4902                    snapshot
4903                        .bytes_in_range(start_ix..end_ix)
4904                        .flatten()
4905                        .copied()
4906                        .collect::<Vec<_>>(),
4907                    expected_text.as_bytes()[start_ix..end_ix].to_vec(),
4908                    "bytes_in_range({:?})",
4909                    start_ix..end_ix,
4910                );
4911            }
4912        }
4913
4914        let snapshot = multibuffer.read(cx).snapshot(cx);
4915        for (old_snapshot, subscription) in old_versions {
4916            let edits = subscription.consume().into_inner();
4917
4918            log::info!(
4919                "applying subscription edits to old text: {:?}: {:?}",
4920                old_snapshot.text(),
4921                edits,
4922            );
4923
4924            let mut text = old_snapshot.text();
4925            for edit in edits {
4926                let new_text: String = snapshot.text_for_range(edit.new.clone()).collect();
4927                text.replace_range(edit.new.start..edit.new.start + edit.old.len(), &new_text);
4928            }
4929            assert_eq!(text.to_string(), snapshot.text());
4930        }
4931    }
4932
4933    #[gpui::test]
4934    fn test_history(cx: &mut MutableAppContext) {
4935        cx.set_global(Settings::test(cx));
4936        let buffer_1 = cx.add_model(|cx| Buffer::new(0, "1234", cx));
4937        let buffer_2 = cx.add_model(|cx| Buffer::new(0, "5678", cx));
4938        let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
4939        let group_interval = multibuffer.read(cx).history.group_interval;
4940        multibuffer.update(cx, |multibuffer, cx| {
4941            multibuffer.push_excerpts(
4942                buffer_1.clone(),
4943                [ExcerptRange {
4944                    context: 0..buffer_1.read(cx).len(),
4945                    primary: None,
4946                }],
4947                cx,
4948            );
4949            multibuffer.push_excerpts(
4950                buffer_2.clone(),
4951                [ExcerptRange {
4952                    context: 0..buffer_2.read(cx).len(),
4953                    primary: None,
4954                }],
4955                cx,
4956            );
4957        });
4958
4959        let mut now = Instant::now();
4960
4961        multibuffer.update(cx, |multibuffer, cx| {
4962            let transaction_1 = multibuffer.start_transaction_at(now, cx).unwrap();
4963            multibuffer.edit(
4964                [
4965                    (Point::new(0, 0)..Point::new(0, 0), "A"),
4966                    (Point::new(1, 0)..Point::new(1, 0), "A"),
4967                ],
4968                None,
4969                cx,
4970            );
4971            multibuffer.edit(
4972                [
4973                    (Point::new(0, 1)..Point::new(0, 1), "B"),
4974                    (Point::new(1, 1)..Point::new(1, 1), "B"),
4975                ],
4976                None,
4977                cx,
4978            );
4979            multibuffer.end_transaction_at(now, cx);
4980            assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
4981
4982            // Edit buffer 1 through the multibuffer
4983            now += 2 * group_interval;
4984            multibuffer.start_transaction_at(now, cx);
4985            multibuffer.edit([(2..2, "C")], None, cx);
4986            multibuffer.end_transaction_at(now, cx);
4987            assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678");
4988
4989            // Edit buffer 1 independently
4990            buffer_1.update(cx, |buffer_1, cx| {
4991                buffer_1.start_transaction_at(now);
4992                buffer_1.edit([(3..3, "D")], None, cx);
4993                buffer_1.end_transaction_at(now, cx);
4994
4995                now += 2 * group_interval;
4996                buffer_1.start_transaction_at(now);
4997                buffer_1.edit([(4..4, "E")], None, cx);
4998                buffer_1.end_transaction_at(now, cx);
4999            });
5000            assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
5001
5002            // An undo in the multibuffer undoes the multibuffer transaction
5003            // and also any individual buffer edits that have occured since
5004            // that transaction.
5005            multibuffer.undo(cx);
5006            assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
5007
5008            multibuffer.undo(cx);
5009            assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
5010
5011            multibuffer.redo(cx);
5012            assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
5013
5014            multibuffer.redo(cx);
5015            assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
5016
5017            // Undo buffer 2 independently.
5018            buffer_2.update(cx, |buffer_2, cx| buffer_2.undo(cx));
5019            assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\n5678");
5020
5021            // An undo in the multibuffer undoes the components of the
5022            // the last multibuffer transaction that are not already undone.
5023            multibuffer.undo(cx);
5024            assert_eq!(multibuffer.read(cx).text(), "AB1234\n5678");
5025
5026            multibuffer.undo(cx);
5027            assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
5028
5029            multibuffer.redo(cx);
5030            assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
5031
5032            buffer_1.update(cx, |buffer_1, cx| buffer_1.redo(cx));
5033            assert_eq!(multibuffer.read(cx).text(), "ABCD1234\nAB5678");
5034
5035            // Redo stack gets cleared after an edit.
5036            now += 2 * group_interval;
5037            multibuffer.start_transaction_at(now, cx);
5038            multibuffer.edit([(0..0, "X")], None, cx);
5039            multibuffer.end_transaction_at(now, cx);
5040            assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
5041            multibuffer.redo(cx);
5042            assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
5043            multibuffer.undo(cx);
5044            assert_eq!(multibuffer.read(cx).text(), "ABCD1234\nAB5678");
5045            multibuffer.undo(cx);
5046            assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
5047
5048            // Transactions can be grouped manually.
5049            multibuffer.redo(cx);
5050            multibuffer.redo(cx);
5051            assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
5052            multibuffer.group_until_transaction(transaction_1, cx);
5053            multibuffer.undo(cx);
5054            assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
5055            multibuffer.redo(cx);
5056            assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
5057        });
5058    }
5059}