text.rs

   1mod anchor;
   2pub mod locator;
   3#[cfg(any(test, feature = "test-support"))]
   4pub mod network;
   5pub mod operation_queue;
   6mod patch;
   7mod selection;
   8pub mod subscription;
   9#[cfg(test)]
  10mod tests;
  11mod undo_map;
  12
  13pub use anchor::*;
  14use anyhow::{Context as _, Result};
  15use clock::Lamport;
  16pub use clock::ReplicaId;
  17use collections::{HashMap, HashSet};
  18use locator::Locator;
  19use operation_queue::OperationQueue;
  20pub use patch::Patch;
  21use postage::{oneshot, prelude::*};
  22
  23use regex::Regex;
  24pub use rope::*;
  25pub use selection::*;
  26use std::{
  27    borrow::Cow,
  28    cmp::{self, Ordering, Reverse},
  29    fmt::Display,
  30    future::Future,
  31    iter::Iterator,
  32    num::NonZeroU64,
  33    ops::{self, Deref, Range, Sub},
  34    str,
  35    sync::{Arc, LazyLock},
  36    time::{Duration, Instant},
  37};
  38pub use subscription::*;
  39pub use sum_tree::Bias;
  40use sum_tree::{Dimensions, FilterCursor, SumTree, TreeMap, TreeSet};
  41use undo_map::UndoMap;
  42use util::debug_panic;
  43
  44#[cfg(any(test, feature = "test-support"))]
  45use util::RandomCharIter;
  46
  47static LINE_SEPARATORS_REGEX: LazyLock<Regex> =
  48    LazyLock::new(|| Regex::new(r"\r\n|\r").expect("Failed to create LINE_SEPARATORS_REGEX"));
  49
  50pub type TransactionId = clock::Lamport;
  51
  52pub struct Buffer {
  53    snapshot: BufferSnapshot,
  54    history: History,
  55    deferred_ops: OperationQueue<Operation>,
  56    deferred_replicas: HashSet<ReplicaId>,
  57    pub lamport_clock: clock::Lamport,
  58    subscriptions: Topic<usize>,
  59    edit_id_resolvers: HashMap<clock::Lamport, Vec<oneshot::Sender<()>>>,
  60    wait_for_version_txs: Vec<(clock::Global, oneshot::Sender<()>)>,
  61}
  62
  63#[repr(transparent)]
  64#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq)]
  65pub struct BufferId(NonZeroU64);
  66
  67impl Display for BufferId {
  68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  69        write!(f, "{}", self.0)
  70    }
  71}
  72
  73impl From<NonZeroU64> for BufferId {
  74    fn from(id: NonZeroU64) -> Self {
  75        BufferId(id)
  76    }
  77}
  78
  79impl BufferId {
  80    /// Returns Err if `id` is outside of BufferId domain.
  81    pub fn new(id: u64) -> anyhow::Result<Self> {
  82        let id = NonZeroU64::new(id).context("Buffer id cannot be 0.")?;
  83        Ok(Self(id))
  84    }
  85
  86    /// Increments this buffer id, returning the old value.
  87    /// So that's a post-increment operator in disguise.
  88    pub fn next(&mut self) -> Self {
  89        let old = *self;
  90        self.0 = self.0.saturating_add(1);
  91        old
  92    }
  93
  94    pub fn to_proto(self) -> u64 {
  95        self.into()
  96    }
  97}
  98
  99impl From<BufferId> for u64 {
 100    fn from(id: BufferId) -> Self {
 101        id.0.get()
 102    }
 103}
 104
 105#[derive(Clone)]
 106pub struct BufferSnapshot {
 107    visible_text: Rope,
 108    deleted_text: Rope,
 109    fragments: SumTree<Fragment>,
 110    insertions: SumTree<InsertionFragment>,
 111    insertion_slices: TreeSet<InsertionSlice>,
 112    undo_map: UndoMap,
 113    pub version: clock::Global,
 114    remote_id: BufferId,
 115    replica_id: ReplicaId,
 116    line_ending: LineEnding,
 117}
 118
 119#[derive(Clone, Debug)]
 120pub struct HistoryEntry {
 121    transaction: Transaction,
 122    first_edit_at: Instant,
 123    last_edit_at: Instant,
 124    suppress_grouping: bool,
 125}
 126
 127#[derive(Clone, Debug)]
 128pub struct Transaction {
 129    pub id: TransactionId,
 130    pub edit_ids: Vec<clock::Lamport>,
 131    pub start: clock::Global,
 132}
 133
 134impl Transaction {
 135    pub fn merge_in(&mut self, other: Transaction) {
 136        self.edit_ids.extend(other.edit_ids);
 137    }
 138}
 139
 140impl HistoryEntry {
 141    pub fn transaction_id(&self) -> TransactionId {
 142        self.transaction.id
 143    }
 144}
 145
 146struct History {
 147    base_text: Rope,
 148    operations: TreeMap<clock::Lamport, Operation>,
 149    undo_stack: Vec<HistoryEntry>,
 150    redo_stack: Vec<HistoryEntry>,
 151    transaction_depth: usize,
 152    group_interval: Duration,
 153}
 154
 155#[derive(Clone, Debug, Eq, PartialEq)]
 156struct InsertionSlice {
 157    edit_id: clock::Lamport,
 158    insertion_id: clock::Lamport,
 159    range: Range<usize>,
 160}
 161
 162impl Ord for InsertionSlice {
 163    fn cmp(&self, other: &Self) -> Ordering {
 164        self.edit_id
 165            .cmp(&other.edit_id)
 166            .then_with(|| self.insertion_id.cmp(&other.insertion_id))
 167            .then_with(|| self.range.start.cmp(&other.range.start))
 168            .then_with(|| self.range.end.cmp(&other.range.end))
 169    }
 170}
 171
 172impl PartialOrd for InsertionSlice {
 173    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
 174        Some(self.cmp(other))
 175    }
 176}
 177
 178impl InsertionSlice {
 179    fn from_fragment(edit_id: clock::Lamport, fragment: &Fragment) -> Self {
 180        Self {
 181            edit_id,
 182            insertion_id: fragment.timestamp,
 183            range: fragment.insertion_offset..fragment.insertion_offset + fragment.len,
 184        }
 185    }
 186}
 187
 188impl History {
 189    pub fn new(base_text: Rope) -> Self {
 190        Self {
 191            base_text,
 192            operations: Default::default(),
 193            undo_stack: Vec::new(),
 194            redo_stack: Vec::new(),
 195            transaction_depth: 0,
 196            // Don't group transactions in tests unless we opt in, because it's a footgun.
 197            #[cfg(any(test, feature = "test-support"))]
 198            group_interval: Duration::ZERO,
 199            #[cfg(not(any(test, feature = "test-support")))]
 200            group_interval: Duration::from_millis(300),
 201        }
 202    }
 203
 204    fn push(&mut self, op: Operation) {
 205        self.operations.insert(op.timestamp(), op);
 206    }
 207
 208    fn start_transaction(
 209        &mut self,
 210        start: clock::Global,
 211        now: Instant,
 212        clock: &mut clock::Lamport,
 213    ) -> Option<TransactionId> {
 214        self.transaction_depth += 1;
 215        if self.transaction_depth == 1 {
 216            let id = clock.tick();
 217            self.undo_stack.push(HistoryEntry {
 218                transaction: Transaction {
 219                    id,
 220                    start,
 221                    edit_ids: Default::default(),
 222                },
 223                first_edit_at: now,
 224                last_edit_at: now,
 225                suppress_grouping: false,
 226            });
 227            Some(id)
 228        } else {
 229            None
 230        }
 231    }
 232
 233    fn end_transaction(&mut self, now: Instant) -> Option<&HistoryEntry> {
 234        assert_ne!(self.transaction_depth, 0);
 235        self.transaction_depth -= 1;
 236        if self.transaction_depth == 0 {
 237            if self
 238                .undo_stack
 239                .last()
 240                .unwrap()
 241                .transaction
 242                .edit_ids
 243                .is_empty()
 244            {
 245                self.undo_stack.pop();
 246                None
 247            } else {
 248                self.redo_stack.clear();
 249                let entry = self.undo_stack.last_mut().unwrap();
 250                entry.last_edit_at = now;
 251                Some(entry)
 252            }
 253        } else {
 254            None
 255        }
 256    }
 257
 258    fn group(&mut self) -> Option<TransactionId> {
 259        let mut count = 0;
 260        let mut entries = self.undo_stack.iter();
 261        if let Some(mut entry) = entries.next_back() {
 262            while let Some(prev_entry) = entries.next_back() {
 263                if !prev_entry.suppress_grouping
 264                    && entry.first_edit_at - prev_entry.last_edit_at < self.group_interval
 265                {
 266                    entry = prev_entry;
 267                    count += 1;
 268                } else {
 269                    break;
 270                }
 271            }
 272        }
 273        self.group_trailing(count)
 274    }
 275
 276    fn group_until(&mut self, transaction_id: TransactionId) {
 277        let mut count = 0;
 278        for entry in self.undo_stack.iter().rev() {
 279            if entry.transaction_id() == transaction_id {
 280                self.group_trailing(count);
 281                break;
 282            } else if entry.suppress_grouping {
 283                break;
 284            } else {
 285                count += 1;
 286            }
 287        }
 288    }
 289
 290    fn group_trailing(&mut self, n: usize) -> Option<TransactionId> {
 291        let new_len = self.undo_stack.len() - n;
 292        let (entries_to_keep, entries_to_merge) = self.undo_stack.split_at_mut(new_len);
 293        if let Some(last_entry) = entries_to_keep.last_mut() {
 294            for entry in &*entries_to_merge {
 295                for edit_id in &entry.transaction.edit_ids {
 296                    last_entry.transaction.edit_ids.push(*edit_id);
 297                }
 298            }
 299
 300            if let Some(entry) = entries_to_merge.last_mut() {
 301                last_entry.last_edit_at = entry.last_edit_at;
 302            }
 303        }
 304
 305        self.undo_stack.truncate(new_len);
 306        self.undo_stack.last().map(|e| e.transaction.id)
 307    }
 308
 309    fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
 310        self.undo_stack.last_mut().map(|entry| {
 311            entry.suppress_grouping = true;
 312            &entry.transaction
 313        })
 314    }
 315
 316    fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
 317        assert_eq!(self.transaction_depth, 0);
 318        self.undo_stack.push(HistoryEntry {
 319            transaction,
 320            first_edit_at: now,
 321            last_edit_at: now,
 322            suppress_grouping: false,
 323        });
 324    }
 325
 326    /// Differs from `push_transaction` in that it does not clear the redo
 327    /// stack. Intended to be used to create a parent transaction to merge
 328    /// potential child transactions into.
 329    ///
 330    /// The caller is responsible for removing it from the undo history using
 331    /// `forget_transaction` if no edits are merged into it. Otherwise, if edits
 332    /// are merged into this transaction, the caller is responsible for ensuring
 333    /// the redo stack is cleared. The easiest way to ensure the redo stack is
 334    /// cleared is to create transactions with the usual `start_transaction` and
 335    /// `end_transaction` methods and merging the resulting transactions into
 336    /// the transaction created by this method
 337    fn push_empty_transaction(
 338        &mut self,
 339        start: clock::Global,
 340        now: Instant,
 341        clock: &mut clock::Lamport,
 342    ) -> TransactionId {
 343        assert_eq!(self.transaction_depth, 0);
 344        let id = clock.tick();
 345        let transaction = Transaction {
 346            id,
 347            start,
 348            edit_ids: Vec::new(),
 349        };
 350        self.undo_stack.push(HistoryEntry {
 351            transaction,
 352            first_edit_at: now,
 353            last_edit_at: now,
 354            suppress_grouping: false,
 355        });
 356        id
 357    }
 358
 359    fn push_undo(&mut self, op_id: clock::Lamport) {
 360        assert_ne!(self.transaction_depth, 0);
 361        if let Some(Operation::Edit(_)) = self.operations.get(&op_id) {
 362            let last_transaction = self.undo_stack.last_mut().unwrap();
 363            last_transaction.transaction.edit_ids.push(op_id);
 364        }
 365    }
 366
 367    fn pop_undo(&mut self) -> Option<&HistoryEntry> {
 368        assert_eq!(self.transaction_depth, 0);
 369        if let Some(entry) = self.undo_stack.pop() {
 370            self.redo_stack.push(entry);
 371            self.redo_stack.last()
 372        } else {
 373            None
 374        }
 375    }
 376
 377    fn remove_from_undo(&mut self, transaction_id: TransactionId) -> Option<&HistoryEntry> {
 378        assert_eq!(self.transaction_depth, 0);
 379
 380        let entry_ix = self
 381            .undo_stack
 382            .iter()
 383            .rposition(|entry| entry.transaction.id == transaction_id)?;
 384        let entry = self.undo_stack.remove(entry_ix);
 385        self.redo_stack.push(entry);
 386        self.redo_stack.last()
 387    }
 388
 389    fn remove_from_undo_until(&mut self, transaction_id: TransactionId) -> &[HistoryEntry] {
 390        assert_eq!(self.transaction_depth, 0);
 391
 392        let redo_stack_start_len = self.redo_stack.len();
 393        if let Some(entry_ix) = self
 394            .undo_stack
 395            .iter()
 396            .rposition(|entry| entry.transaction.id == transaction_id)
 397        {
 398            self.redo_stack
 399                .extend(self.undo_stack.drain(entry_ix..).rev());
 400        }
 401        &self.redo_stack[redo_stack_start_len..]
 402    }
 403
 404    fn forget(&mut self, transaction_id: TransactionId) -> Option<Transaction> {
 405        assert_eq!(self.transaction_depth, 0);
 406        if let Some(entry_ix) = self
 407            .undo_stack
 408            .iter()
 409            .rposition(|entry| entry.transaction.id == transaction_id)
 410        {
 411            Some(self.undo_stack.remove(entry_ix).transaction)
 412        } else if let Some(entry_ix) = self
 413            .redo_stack
 414            .iter()
 415            .rposition(|entry| entry.transaction.id == transaction_id)
 416        {
 417            Some(self.redo_stack.remove(entry_ix).transaction)
 418        } else {
 419            None
 420        }
 421    }
 422
 423    fn transaction(&self, transaction_id: TransactionId) -> Option<&Transaction> {
 424        let entry = self
 425            .undo_stack
 426            .iter()
 427            .rfind(|entry| entry.transaction.id == transaction_id)
 428            .or_else(|| {
 429                self.redo_stack
 430                    .iter()
 431                    .rfind(|entry| entry.transaction.id == transaction_id)
 432            })?;
 433        Some(&entry.transaction)
 434    }
 435
 436    fn transaction_mut(&mut self, transaction_id: TransactionId) -> Option<&mut Transaction> {
 437        let entry = self
 438            .undo_stack
 439            .iter_mut()
 440            .rfind(|entry| entry.transaction.id == transaction_id)
 441            .or_else(|| {
 442                self.redo_stack
 443                    .iter_mut()
 444                    .rfind(|entry| entry.transaction.id == transaction_id)
 445            })?;
 446        Some(&mut entry.transaction)
 447    }
 448
 449    fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
 450        if let Some(transaction) = self.forget(transaction)
 451            && let Some(destination) = self.transaction_mut(destination)
 452        {
 453            destination.edit_ids.extend(transaction.edit_ids);
 454        }
 455    }
 456
 457    fn pop_redo(&mut self) -> Option<&HistoryEntry> {
 458        assert_eq!(self.transaction_depth, 0);
 459        if let Some(entry) = self.redo_stack.pop() {
 460            self.undo_stack.push(entry);
 461            self.undo_stack.last()
 462        } else {
 463            None
 464        }
 465    }
 466
 467    fn remove_from_redo(&mut self, transaction_id: TransactionId) -> &[HistoryEntry] {
 468        assert_eq!(self.transaction_depth, 0);
 469
 470        let undo_stack_start_len = self.undo_stack.len();
 471        if let Some(entry_ix) = self
 472            .redo_stack
 473            .iter()
 474            .rposition(|entry| entry.transaction.id == transaction_id)
 475        {
 476            self.undo_stack
 477                .extend(self.redo_stack.drain(entry_ix..).rev());
 478        }
 479        &self.undo_stack[undo_stack_start_len..]
 480    }
 481}
 482
 483struct Edits<'a, D: TextDimension, F: FnMut(&FragmentSummary) -> bool> {
 484    visible_cursor: rope::Cursor<'a>,
 485    deleted_cursor: rope::Cursor<'a>,
 486    fragments_cursor: Option<FilterCursor<'a, 'static, F, Fragment, FragmentTextSummary>>,
 487    undos: &'a UndoMap,
 488    since: &'a clock::Global,
 489    old_end: D,
 490    new_end: D,
 491    range: Range<(&'a Locator, usize)>,
 492    buffer_id: BufferId,
 493}
 494
 495#[derive(Clone, Debug, Default, Eq, PartialEq)]
 496pub struct Edit<D> {
 497    pub old: Range<D>,
 498    pub new: Range<D>,
 499}
 500impl<D> Edit<D>
 501where
 502    D: PartialEq,
 503{
 504    pub fn is_empty(&self) -> bool {
 505        self.old.start == self.old.end && self.new.start == self.new.end
 506    }
 507}
 508
 509impl<D, DDelta> Edit<D>
 510where
 511    D: Sub<D, Output = DDelta> + Copy,
 512{
 513    pub fn old_len(&self) -> DDelta {
 514        self.old.end - self.old.start
 515    }
 516
 517    pub fn new_len(&self) -> DDelta {
 518        self.new.end - self.new.start
 519    }
 520}
 521
 522impl<D1, D2> Edit<(D1, D2)> {
 523    pub fn flatten(self) -> (Edit<D1>, Edit<D2>) {
 524        (
 525            Edit {
 526                old: self.old.start.0..self.old.end.0,
 527                new: self.new.start.0..self.new.end.0,
 528            },
 529            Edit {
 530                old: self.old.start.1..self.old.end.1,
 531                new: self.new.start.1..self.new.end.1,
 532            },
 533        )
 534    }
 535}
 536
 537#[derive(Eq, PartialEq, Clone, Debug)]
 538pub struct Fragment {
 539    pub id: Locator,
 540    pub timestamp: clock::Lamport,
 541    pub insertion_offset: usize,
 542    pub len: usize,
 543    pub visible: bool,
 544    pub deletions: HashSet<clock::Lamport>,
 545    pub max_undos: clock::Global,
 546}
 547
 548#[derive(Eq, PartialEq, Clone, Debug)]
 549pub struct FragmentSummary {
 550    text: FragmentTextSummary,
 551    max_id: Locator,
 552    max_version: clock::Global,
 553    min_insertion_version: clock::Global,
 554    max_insertion_version: clock::Global,
 555}
 556
 557#[derive(Copy, Default, Clone, Debug, PartialEq, Eq)]
 558struct FragmentTextSummary {
 559    visible: usize,
 560    deleted: usize,
 561}
 562
 563impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentTextSummary {
 564    fn zero(_: &Option<clock::Global>) -> Self {
 565        Default::default()
 566    }
 567
 568    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
 569        self.visible += summary.text.visible;
 570        self.deleted += summary.text.deleted;
 571    }
 572}
 573
 574#[derive(Eq, PartialEq, Clone, Debug)]
 575struct InsertionFragment {
 576    timestamp: clock::Lamport,
 577    split_offset: usize,
 578    fragment_id: Locator,
 579}
 580
 581#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
 582struct InsertionFragmentKey {
 583    timestamp: clock::Lamport,
 584    split_offset: usize,
 585}
 586
 587#[derive(Clone, Debug, Eq, PartialEq)]
 588pub enum Operation {
 589    Edit(EditOperation),
 590    Undo(UndoOperation),
 591}
 592
 593#[derive(Clone, Debug, Eq, PartialEq)]
 594pub struct EditOperation {
 595    pub timestamp: clock::Lamport,
 596    pub version: clock::Global,
 597    pub ranges: Vec<Range<FullOffset>>,
 598    pub new_text: Vec<Arc<str>>,
 599}
 600
 601#[derive(Clone, Debug, Eq, PartialEq)]
 602pub struct UndoOperation {
 603    pub timestamp: clock::Lamport,
 604    pub version: clock::Global,
 605    pub counts: HashMap<clock::Lamport, u32>,
 606}
 607
 608/// Stores information about the indentation of a line (tabs and spaces).
 609#[derive(Clone, Copy, Debug, Eq, PartialEq)]
 610pub struct LineIndent {
 611    pub tabs: u32,
 612    pub spaces: u32,
 613    pub line_blank: bool,
 614}
 615
 616impl LineIndent {
 617    pub fn from_chunks(chunks: &mut Chunks) -> Self {
 618        let mut tabs = 0;
 619        let mut spaces = 0;
 620        let mut line_blank = true;
 621
 622        'outer: while let Some(chunk) = chunks.peek() {
 623            for ch in chunk.chars() {
 624                if ch == '\t' {
 625                    tabs += 1;
 626                } else if ch == ' ' {
 627                    spaces += 1;
 628                } else {
 629                    if ch != '\n' {
 630                        line_blank = false;
 631                    }
 632                    break 'outer;
 633                }
 634            }
 635
 636            chunks.next();
 637        }
 638
 639        Self {
 640            tabs,
 641            spaces,
 642            line_blank,
 643        }
 644    }
 645
 646    /// Constructs a new `LineIndent` which only contains spaces.
 647    pub fn spaces(spaces: u32) -> Self {
 648        Self {
 649            tabs: 0,
 650            spaces,
 651            line_blank: true,
 652        }
 653    }
 654
 655    /// Constructs a new `LineIndent` which only contains tabs.
 656    pub fn tabs(tabs: u32) -> Self {
 657        Self {
 658            tabs,
 659            spaces: 0,
 660            line_blank: true,
 661        }
 662    }
 663
 664    /// Indicates whether the line is empty.
 665    pub fn is_line_empty(&self) -> bool {
 666        self.tabs == 0 && self.spaces == 0 && self.line_blank
 667    }
 668
 669    /// Indicates whether the line is blank (contains only whitespace).
 670    pub fn is_line_blank(&self) -> bool {
 671        self.line_blank
 672    }
 673
 674    /// Returns the number of indentation characters (tabs or spaces).
 675    pub fn raw_len(&self) -> u32 {
 676        self.tabs + self.spaces
 677    }
 678
 679    /// Returns the number of indentation characters (tabs or spaces), taking tab size into account.
 680    pub fn len(&self, tab_size: u32) -> u32 {
 681        self.tabs * tab_size + self.spaces
 682    }
 683}
 684
 685impl From<&str> for LineIndent {
 686    fn from(value: &str) -> Self {
 687        Self::from_iter(value.chars())
 688    }
 689}
 690
 691impl FromIterator<char> for LineIndent {
 692    fn from_iter<T: IntoIterator<Item = char>>(chars: T) -> Self {
 693        let mut tabs = 0;
 694        let mut spaces = 0;
 695        let mut line_blank = true;
 696        for c in chars {
 697            if c == '\t' {
 698                tabs += 1;
 699            } else if c == ' ' {
 700                spaces += 1;
 701            } else {
 702                if c != '\n' {
 703                    line_blank = false;
 704                }
 705                break;
 706            }
 707        }
 708        Self {
 709            tabs,
 710            spaces,
 711            line_blank,
 712        }
 713    }
 714}
 715
 716impl Buffer {
 717    pub fn new(replica_id: ReplicaId, remote_id: BufferId, base_text: impl Into<String>) -> Buffer {
 718        let mut base_text = base_text.into();
 719        let line_ending = LineEnding::detect(&base_text);
 720        LineEnding::normalize(&mut base_text);
 721        Self::new_normalized(replica_id, remote_id, line_ending, Rope::from(&*base_text))
 722    }
 723
 724    pub fn new_normalized(
 725        replica_id: ReplicaId,
 726        remote_id: BufferId,
 727        line_ending: LineEnding,
 728        normalized: Rope,
 729    ) -> Buffer {
 730        let history = History::new(normalized);
 731        let mut fragments = SumTree::new(&None);
 732        let mut insertions = SumTree::default();
 733
 734        let mut lamport_clock = clock::Lamport::new(replica_id);
 735        let mut version = clock::Global::new();
 736
 737        let visible_text = history.base_text.clone();
 738        if !visible_text.is_empty() {
 739            let insertion_timestamp = clock::Lamport::new(ReplicaId::LOCAL);
 740            lamport_clock.observe(insertion_timestamp);
 741            version.observe(insertion_timestamp);
 742            let fragment_id = Locator::between(&Locator::min(), &Locator::max());
 743            let fragment = Fragment {
 744                id: fragment_id,
 745                timestamp: insertion_timestamp,
 746                insertion_offset: 0,
 747                len: visible_text.len(),
 748                visible: true,
 749                deletions: Default::default(),
 750                max_undos: Default::default(),
 751            };
 752            insertions.push(InsertionFragment::new(&fragment), ());
 753            fragments.push(fragment, &None);
 754        }
 755
 756        Buffer {
 757            snapshot: BufferSnapshot {
 758                replica_id,
 759                remote_id,
 760                visible_text,
 761                deleted_text: Rope::new(),
 762                line_ending,
 763                fragments,
 764                insertions,
 765                version,
 766                undo_map: Default::default(),
 767                insertion_slices: Default::default(),
 768            },
 769            history,
 770            deferred_ops: OperationQueue::new(),
 771            deferred_replicas: HashSet::default(),
 772            lamport_clock,
 773            subscriptions: Default::default(),
 774            edit_id_resolvers: Default::default(),
 775            wait_for_version_txs: Default::default(),
 776        }
 777    }
 778
 779    pub fn version(&self) -> clock::Global {
 780        self.version.clone()
 781    }
 782
 783    pub fn snapshot(&self) -> &BufferSnapshot {
 784        &self.snapshot
 785    }
 786
 787    pub fn into_snapshot(self) -> BufferSnapshot {
 788        self.snapshot
 789    }
 790
 791    pub fn branch(&self) -> Self {
 792        Self {
 793            snapshot: self.snapshot.clone(),
 794            history: History::new(self.base_text().clone()),
 795            deferred_ops: OperationQueue::new(),
 796            deferred_replicas: HashSet::default(),
 797            lamport_clock: clock::Lamport::new(ReplicaId::LOCAL_BRANCH),
 798            subscriptions: Default::default(),
 799            edit_id_resolvers: Default::default(),
 800            wait_for_version_txs: Default::default(),
 801        }
 802    }
 803
 804    pub fn replica_id(&self) -> ReplicaId {
 805        self.lamport_clock.replica_id
 806    }
 807
 808    pub fn remote_id(&self) -> BufferId {
 809        self.remote_id
 810    }
 811
 812    pub fn deferred_ops_len(&self) -> usize {
 813        self.deferred_ops.len()
 814    }
 815
 816    pub fn transaction_group_interval(&self) -> Duration {
 817        self.history.group_interval
 818    }
 819
 820    pub fn edit<R, I, S, T>(&mut self, edits: R) -> Operation
 821    where
 822        R: IntoIterator<IntoIter = I>,
 823        I: ExactSizeIterator<Item = (Range<S>, T)>,
 824        S: ToOffset,
 825        T: Into<Arc<str>>,
 826    {
 827        let edits = edits
 828            .into_iter()
 829            .map(|(range, new_text)| (range, new_text.into()));
 830
 831        self.start_transaction();
 832        let timestamp = self.lamport_clock.tick();
 833        let operation = Operation::Edit(self.apply_local_edit(edits, timestamp));
 834
 835        self.history.push(operation.clone());
 836        self.history.push_undo(operation.timestamp());
 837        self.snapshot.version.observe(operation.timestamp());
 838        self.end_transaction();
 839        operation
 840    }
 841
 842    fn apply_local_edit<S: ToOffset, T: Into<Arc<str>>>(
 843        &mut self,
 844        edits: impl ExactSizeIterator<Item = (Range<S>, T)>,
 845        timestamp: clock::Lamport,
 846    ) -> EditOperation {
 847        let mut edits_patch = Patch::default();
 848        let mut edit_op = EditOperation {
 849            timestamp,
 850            version: self.version(),
 851            ranges: Vec::with_capacity(edits.len()),
 852            new_text: Vec::with_capacity(edits.len()),
 853        };
 854        let mut new_insertions = Vec::new();
 855        let mut insertion_offset = 0;
 856        let mut insertion_slices = Vec::new();
 857
 858        let mut edits = edits
 859            .map(|(range, new_text)| (range.to_offset(&*self), new_text))
 860            .peekable();
 861
 862        let mut new_ropes =
 863            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
 864        let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>(&None);
 865        let mut new_fragments = old_fragments.slice(&edits.peek().unwrap().0.start, Bias::Right);
 866        new_ropes.append(new_fragments.summary().text);
 867
 868        let mut fragment_start = old_fragments.start().visible;
 869        for (range, new_text) in edits {
 870            let new_text = LineEnding::normalize_arc(new_text.into());
 871            let fragment_end = old_fragments.end().visible;
 872
 873            // If the current fragment ends before this range, then jump ahead to the first fragment
 874            // that extends past the start of this range, reusing any intervening fragments.
 875            if fragment_end < range.start {
 876                // If the current fragment has been partially consumed, then consume the rest of it
 877                // and advance to the next fragment before slicing.
 878                if fragment_start > old_fragments.start().visible {
 879                    if fragment_end > fragment_start {
 880                        let mut suffix = old_fragments.item().unwrap().clone();
 881                        suffix.len = fragment_end - fragment_start;
 882                        suffix.insertion_offset += fragment_start - old_fragments.start().visible;
 883                        new_insertions.push(InsertionFragment::insert_new(&suffix));
 884                        new_ropes.push_fragment(&suffix, suffix.visible);
 885                        new_fragments.push(suffix, &None);
 886                    }
 887                    old_fragments.next();
 888                }
 889
 890                let slice = old_fragments.slice(&range.start, Bias::Right);
 891                new_ropes.append(slice.summary().text);
 892                new_fragments.append(slice, &None);
 893                fragment_start = old_fragments.start().visible;
 894            }
 895
 896            let full_range_start = FullOffset(range.start + old_fragments.start().deleted);
 897
 898            // Preserve any portion of the current fragment that precedes this range.
 899            if fragment_start < range.start {
 900                let mut prefix = old_fragments.item().unwrap().clone();
 901                prefix.len = range.start - fragment_start;
 902                prefix.insertion_offset += fragment_start - old_fragments.start().visible;
 903                prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
 904                new_insertions.push(InsertionFragment::insert_new(&prefix));
 905                new_ropes.push_fragment(&prefix, prefix.visible);
 906                new_fragments.push(prefix, &None);
 907                fragment_start = range.start;
 908            }
 909
 910            // Insert the new text before any existing fragments within the range.
 911            if !new_text.is_empty() {
 912                let new_start = new_fragments.summary().text.visible;
 913
 914                let fragment = Fragment {
 915                    id: Locator::between(
 916                        &new_fragments.summary().max_id,
 917                        old_fragments
 918                            .item()
 919                            .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
 920                    ),
 921                    timestamp,
 922                    insertion_offset,
 923                    len: new_text.len(),
 924                    deletions: Default::default(),
 925                    max_undos: Default::default(),
 926                    visible: true,
 927                };
 928                edits_patch.push(Edit {
 929                    old: fragment_start..fragment_start,
 930                    new: new_start..new_start + new_text.len(),
 931                });
 932                insertion_slices.push(InsertionSlice::from_fragment(timestamp, &fragment));
 933                new_insertions.push(InsertionFragment::insert_new(&fragment));
 934                new_ropes.push_str(new_text.as_ref());
 935                new_fragments.push(fragment, &None);
 936                insertion_offset += new_text.len();
 937            }
 938
 939            // Advance through every fragment that intersects this range, marking the intersecting
 940            // portions as deleted.
 941            while fragment_start < range.end {
 942                let fragment = old_fragments.item().unwrap();
 943                let fragment_end = old_fragments.end().visible;
 944                let mut intersection = fragment.clone();
 945                let intersection_end = cmp::min(range.end, fragment_end);
 946                if fragment.visible {
 947                    intersection.len = intersection_end - fragment_start;
 948                    intersection.insertion_offset += fragment_start - old_fragments.start().visible;
 949                    intersection.id =
 950                        Locator::between(&new_fragments.summary().max_id, &intersection.id);
 951                    intersection.deletions.insert(timestamp);
 952                    intersection.visible = false;
 953                }
 954                if intersection.len > 0 {
 955                    if fragment.visible && !intersection.visible {
 956                        let new_start = new_fragments.summary().text.visible;
 957                        edits_patch.push(Edit {
 958                            old: fragment_start..intersection_end,
 959                            new: new_start..new_start,
 960                        });
 961                        insertion_slices
 962                            .push(InsertionSlice::from_fragment(timestamp, &intersection));
 963                    }
 964                    new_insertions.push(InsertionFragment::insert_new(&intersection));
 965                    new_ropes.push_fragment(&intersection, fragment.visible);
 966                    new_fragments.push(intersection, &None);
 967                    fragment_start = intersection_end;
 968                }
 969                if fragment_end <= range.end {
 970                    old_fragments.next();
 971                }
 972            }
 973
 974            let full_range_end = FullOffset(range.end + old_fragments.start().deleted);
 975            edit_op.ranges.push(full_range_start..full_range_end);
 976            edit_op.new_text.push(new_text);
 977        }
 978
 979        // If the current fragment has been partially consumed, then consume the rest of it
 980        // and advance to the next fragment before slicing.
 981        if fragment_start > old_fragments.start().visible {
 982            let fragment_end = old_fragments.end().visible;
 983            if fragment_end > fragment_start {
 984                let mut suffix = old_fragments.item().unwrap().clone();
 985                suffix.len = fragment_end - fragment_start;
 986                suffix.insertion_offset += fragment_start - old_fragments.start().visible;
 987                new_insertions.push(InsertionFragment::insert_new(&suffix));
 988                new_ropes.push_fragment(&suffix, suffix.visible);
 989                new_fragments.push(suffix, &None);
 990            }
 991            old_fragments.next();
 992        }
 993
 994        let suffix = old_fragments.suffix();
 995        new_ropes.append(suffix.summary().text);
 996        new_fragments.append(suffix, &None);
 997        let (visible_text, deleted_text) = new_ropes.finish();
 998        drop(old_fragments);
 999
1000        self.snapshot.fragments = new_fragments;
1001        self.snapshot.insertions.edit(new_insertions, ());
1002        self.snapshot.visible_text = visible_text;
1003        self.snapshot.deleted_text = deleted_text;
1004        self.subscriptions.publish_mut(&edits_patch);
1005        self.snapshot.insertion_slices.extend(insertion_slices);
1006        edit_op
1007    }
1008
1009    pub fn set_line_ending(&mut self, line_ending: LineEnding) {
1010        self.snapshot.line_ending = line_ending;
1011    }
1012
1013    pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I) {
1014        let mut deferred_ops = Vec::new();
1015        for op in ops {
1016            self.history.push(op.clone());
1017            if self.can_apply_op(&op) {
1018                self.apply_op(op);
1019            } else {
1020                self.deferred_replicas.insert(op.replica_id());
1021                deferred_ops.push(op);
1022            }
1023        }
1024        self.deferred_ops.insert(deferred_ops);
1025        self.flush_deferred_ops();
1026    }
1027
1028    fn apply_op(&mut self, op: Operation) {
1029        match op {
1030            Operation::Edit(edit) => {
1031                if !self.version.observed(edit.timestamp) {
1032                    self.apply_remote_edit(
1033                        &edit.version,
1034                        &edit.ranges,
1035                        &edit.new_text,
1036                        edit.timestamp,
1037                    );
1038                    self.snapshot.version.observe(edit.timestamp);
1039                    self.lamport_clock.observe(edit.timestamp);
1040                    self.resolve_edit(edit.timestamp);
1041                }
1042            }
1043            Operation::Undo(undo) => {
1044                if !self.version.observed(undo.timestamp) {
1045                    self.apply_undo(&undo);
1046                    self.snapshot.version.observe(undo.timestamp);
1047                    self.lamport_clock.observe(undo.timestamp);
1048                }
1049            }
1050        }
1051        self.wait_for_version_txs.retain_mut(|(version, tx)| {
1052            if self.snapshot.version().observed_all(version) {
1053                tx.try_send(()).ok();
1054                false
1055            } else {
1056                true
1057            }
1058        });
1059    }
1060
1061    fn apply_remote_edit(
1062        &mut self,
1063        version: &clock::Global,
1064        ranges: &[Range<FullOffset>],
1065        new_text: &[Arc<str>],
1066        timestamp: clock::Lamport,
1067    ) {
1068        if ranges.is_empty() {
1069            return;
1070        }
1071
1072        let edits = ranges.iter().zip(new_text.iter());
1073        let mut edits_patch = Patch::default();
1074        let mut insertion_slices = Vec::new();
1075        let cx = Some(version.clone());
1076        let mut new_insertions = Vec::new();
1077        let mut insertion_offset = 0;
1078        let mut new_ropes =
1079            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1080        let mut old_fragments = self
1081            .fragments
1082            .cursor::<Dimensions<VersionedFullOffset, usize>>(&cx);
1083        let mut new_fragments =
1084            old_fragments.slice(&VersionedFullOffset::Offset(ranges[0].start), Bias::Left);
1085        new_ropes.append(new_fragments.summary().text);
1086
1087        let mut fragment_start = old_fragments.start().0.full_offset();
1088        for (range, new_text) in edits {
1089            let fragment_end = old_fragments.end().0.full_offset();
1090
1091            // If the current fragment ends before this range, then jump ahead to the first fragment
1092            // that extends past the start of this range, reusing any intervening fragments.
1093            if fragment_end < range.start {
1094                // If the current fragment has been partially consumed, then consume the rest of it
1095                // and advance to the next fragment before slicing.
1096                if fragment_start > old_fragments.start().0.full_offset() {
1097                    if fragment_end > fragment_start {
1098                        let mut suffix = old_fragments.item().unwrap().clone();
1099                        suffix.len = fragment_end.0 - fragment_start.0;
1100                        suffix.insertion_offset +=
1101                            fragment_start - old_fragments.start().0.full_offset();
1102                        new_insertions.push(InsertionFragment::insert_new(&suffix));
1103                        new_ropes.push_fragment(&suffix, suffix.visible);
1104                        new_fragments.push(suffix, &None);
1105                    }
1106                    old_fragments.next();
1107                }
1108
1109                let slice =
1110                    old_fragments.slice(&VersionedFullOffset::Offset(range.start), Bias::Left);
1111                new_ropes.append(slice.summary().text);
1112                new_fragments.append(slice, &None);
1113                fragment_start = old_fragments.start().0.full_offset();
1114            }
1115
1116            // If we are at the end of a non-concurrent fragment, advance to the next one.
1117            let fragment_end = old_fragments.end().0.full_offset();
1118            if fragment_end == range.start && fragment_end > fragment_start {
1119                let mut fragment = old_fragments.item().unwrap().clone();
1120                fragment.len = fragment_end.0 - fragment_start.0;
1121                fragment.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
1122                new_insertions.push(InsertionFragment::insert_new(&fragment));
1123                new_ropes.push_fragment(&fragment, fragment.visible);
1124                new_fragments.push(fragment, &None);
1125                old_fragments.next();
1126                fragment_start = old_fragments.start().0.full_offset();
1127            }
1128
1129            // Skip over insertions that are concurrent to this edit, but have a lower lamport
1130            // timestamp.
1131            while let Some(fragment) = old_fragments.item() {
1132                if fragment_start == range.start && fragment.timestamp > timestamp {
1133                    new_ropes.push_fragment(fragment, fragment.visible);
1134                    new_fragments.push(fragment.clone(), &None);
1135                    old_fragments.next();
1136                    debug_assert_eq!(fragment_start, range.start);
1137                } else {
1138                    break;
1139                }
1140            }
1141            debug_assert!(fragment_start <= range.start);
1142
1143            // Preserve any portion of the current fragment that precedes this range.
1144            if fragment_start < range.start {
1145                let mut prefix = old_fragments.item().unwrap().clone();
1146                prefix.len = range.start.0 - fragment_start.0;
1147                prefix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
1148                prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
1149                new_insertions.push(InsertionFragment::insert_new(&prefix));
1150                fragment_start = range.start;
1151                new_ropes.push_fragment(&prefix, prefix.visible);
1152                new_fragments.push(prefix, &None);
1153            }
1154
1155            // Insert the new text before any existing fragments within the range.
1156            if !new_text.is_empty() {
1157                let mut old_start = old_fragments.start().1;
1158                if old_fragments.item().is_some_and(|f| f.visible) {
1159                    old_start += fragment_start.0 - old_fragments.start().0.full_offset().0;
1160                }
1161                let new_start = new_fragments.summary().text.visible;
1162                let fragment = Fragment {
1163                    id: Locator::between(
1164                        &new_fragments.summary().max_id,
1165                        old_fragments
1166                            .item()
1167                            .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
1168                    ),
1169                    timestamp,
1170                    insertion_offset,
1171                    len: new_text.len(),
1172                    deletions: Default::default(),
1173                    max_undos: Default::default(),
1174                    visible: true,
1175                };
1176                edits_patch.push(Edit {
1177                    old: old_start..old_start,
1178                    new: new_start..new_start + new_text.len(),
1179                });
1180                insertion_slices.push(InsertionSlice::from_fragment(timestamp, &fragment));
1181                new_insertions.push(InsertionFragment::insert_new(&fragment));
1182                new_ropes.push_str(new_text);
1183                new_fragments.push(fragment, &None);
1184                insertion_offset += new_text.len();
1185            }
1186
1187            // Advance through every fragment that intersects this range, marking the intersecting
1188            // portions as deleted.
1189            while fragment_start < range.end {
1190                let fragment = old_fragments.item().unwrap();
1191                let fragment_end = old_fragments.end().0.full_offset();
1192                let mut intersection = fragment.clone();
1193                let intersection_end = cmp::min(range.end, fragment_end);
1194                if fragment.was_visible(version, &self.undo_map) {
1195                    intersection.len = intersection_end.0 - fragment_start.0;
1196                    intersection.insertion_offset +=
1197                        fragment_start - old_fragments.start().0.full_offset();
1198                    intersection.id =
1199                        Locator::between(&new_fragments.summary().max_id, &intersection.id);
1200                    intersection.deletions.insert(timestamp);
1201                    intersection.visible = false;
1202                    insertion_slices.push(InsertionSlice::from_fragment(timestamp, &intersection));
1203                }
1204                if intersection.len > 0 {
1205                    if fragment.visible && !intersection.visible {
1206                        let old_start = old_fragments.start().1
1207                            + (fragment_start.0 - old_fragments.start().0.full_offset().0);
1208                        let new_start = new_fragments.summary().text.visible;
1209                        edits_patch.push(Edit {
1210                            old: old_start..old_start + intersection.len,
1211                            new: new_start..new_start,
1212                        });
1213                    }
1214                    new_insertions.push(InsertionFragment::insert_new(&intersection));
1215                    new_ropes.push_fragment(&intersection, fragment.visible);
1216                    new_fragments.push(intersection, &None);
1217                    fragment_start = intersection_end;
1218                }
1219                if fragment_end <= range.end {
1220                    old_fragments.next();
1221                }
1222            }
1223        }
1224
1225        // If the current fragment has been partially consumed, then consume the rest of it
1226        // and advance to the next fragment before slicing.
1227        if fragment_start > old_fragments.start().0.full_offset() {
1228            let fragment_end = old_fragments.end().0.full_offset();
1229            if fragment_end > fragment_start {
1230                let mut suffix = old_fragments.item().unwrap().clone();
1231                suffix.len = fragment_end.0 - fragment_start.0;
1232                suffix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
1233                new_insertions.push(InsertionFragment::insert_new(&suffix));
1234                new_ropes.push_fragment(&suffix, suffix.visible);
1235                new_fragments.push(suffix, &None);
1236            }
1237            old_fragments.next();
1238        }
1239
1240        let suffix = old_fragments.suffix();
1241        new_ropes.append(suffix.summary().text);
1242        new_fragments.append(suffix, &None);
1243        let (visible_text, deleted_text) = new_ropes.finish();
1244        drop(old_fragments);
1245
1246        self.snapshot.fragments = new_fragments;
1247        self.snapshot.visible_text = visible_text;
1248        self.snapshot.deleted_text = deleted_text;
1249        self.snapshot.insertions.edit(new_insertions, ());
1250        self.snapshot.insertion_slices.extend(insertion_slices);
1251        self.subscriptions.publish_mut(&edits_patch)
1252    }
1253
1254    fn fragment_ids_for_edits<'a>(
1255        &'a self,
1256        edit_ids: impl Iterator<Item = &'a clock::Lamport>,
1257    ) -> Vec<&'a Locator> {
1258        // Get all of the insertion slices changed by the given edits.
1259        let mut insertion_slices = Vec::new();
1260        for edit_id in edit_ids {
1261            let insertion_slice = InsertionSlice {
1262                edit_id: *edit_id,
1263                insertion_id: clock::Lamport::MIN,
1264                range: 0..0,
1265            };
1266            let slices = self
1267                .snapshot
1268                .insertion_slices
1269                .iter_from(&insertion_slice)
1270                .take_while(|slice| slice.edit_id == *edit_id);
1271            insertion_slices.extend(slices)
1272        }
1273        insertion_slices
1274            .sort_unstable_by_key(|s| (s.insertion_id, s.range.start, Reverse(s.range.end)));
1275
1276        // Get all of the fragments corresponding to these insertion slices.
1277        let mut fragment_ids = Vec::new();
1278        let mut insertions_cursor = self.insertions.cursor::<InsertionFragmentKey>(());
1279        for insertion_slice in &insertion_slices {
1280            if insertion_slice.insertion_id != insertions_cursor.start().timestamp
1281                || insertion_slice.range.start > insertions_cursor.start().split_offset
1282            {
1283                insertions_cursor.seek_forward(
1284                    &InsertionFragmentKey {
1285                        timestamp: insertion_slice.insertion_id,
1286                        split_offset: insertion_slice.range.start,
1287                    },
1288                    Bias::Left,
1289                );
1290            }
1291            while let Some(item) = insertions_cursor.item() {
1292                if item.timestamp != insertion_slice.insertion_id
1293                    || item.split_offset >= insertion_slice.range.end
1294                {
1295                    break;
1296                }
1297                fragment_ids.push(&item.fragment_id);
1298                insertions_cursor.next();
1299            }
1300        }
1301        fragment_ids.sort_unstable();
1302        fragment_ids
1303    }
1304
1305    fn apply_undo(&mut self, undo: &UndoOperation) {
1306        self.snapshot.undo_map.insert(undo);
1307
1308        let mut edits = Patch::default();
1309        let mut old_fragments = self
1310            .fragments
1311            .cursor::<Dimensions<Option<&Locator>, usize>>(&None);
1312        let mut new_fragments = SumTree::new(&None);
1313        let mut new_ropes =
1314            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1315
1316        for fragment_id in self.fragment_ids_for_edits(undo.counts.keys()) {
1317            let preceding_fragments = old_fragments.slice(&Some(fragment_id), Bias::Left);
1318            new_ropes.append(preceding_fragments.summary().text);
1319            new_fragments.append(preceding_fragments, &None);
1320
1321            if let Some(fragment) = old_fragments.item() {
1322                let mut fragment = fragment.clone();
1323                let fragment_was_visible = fragment.visible;
1324
1325                fragment.visible = fragment.is_visible(&self.undo_map);
1326                fragment.max_undos.observe(undo.timestamp);
1327
1328                let old_start = old_fragments.start().1;
1329                let new_start = new_fragments.summary().text.visible;
1330                if fragment_was_visible && !fragment.visible {
1331                    edits.push(Edit {
1332                        old: old_start..old_start + fragment.len,
1333                        new: new_start..new_start,
1334                    });
1335                } else if !fragment_was_visible && fragment.visible {
1336                    edits.push(Edit {
1337                        old: old_start..old_start,
1338                        new: new_start..new_start + fragment.len,
1339                    });
1340                }
1341                new_ropes.push_fragment(&fragment, fragment_was_visible);
1342                new_fragments.push(fragment, &None);
1343
1344                old_fragments.next();
1345            }
1346        }
1347
1348        let suffix = old_fragments.suffix();
1349        new_ropes.append(suffix.summary().text);
1350        new_fragments.append(suffix, &None);
1351
1352        drop(old_fragments);
1353        let (visible_text, deleted_text) = new_ropes.finish();
1354        self.snapshot.fragments = new_fragments;
1355        self.snapshot.visible_text = visible_text;
1356        self.snapshot.deleted_text = deleted_text;
1357        self.subscriptions.publish_mut(&edits);
1358    }
1359
1360    fn flush_deferred_ops(&mut self) {
1361        self.deferred_replicas.clear();
1362        let mut deferred_ops = Vec::new();
1363        for op in self.deferred_ops.drain().iter().cloned() {
1364            if self.can_apply_op(&op) {
1365                self.apply_op(op);
1366            } else {
1367                self.deferred_replicas.insert(op.replica_id());
1368                deferred_ops.push(op);
1369            }
1370        }
1371        self.deferred_ops.insert(deferred_ops);
1372    }
1373
1374    fn can_apply_op(&self, op: &Operation) -> bool {
1375        if self.deferred_replicas.contains(&op.replica_id()) {
1376            false
1377        } else {
1378            self.version.observed_all(match op {
1379                Operation::Edit(edit) => &edit.version,
1380                Operation::Undo(undo) => &undo.version,
1381            })
1382        }
1383    }
1384
1385    pub fn has_deferred_ops(&self) -> bool {
1386        !self.deferred_ops.is_empty()
1387    }
1388
1389    pub fn peek_undo_stack(&self) -> Option<&HistoryEntry> {
1390        self.history.undo_stack.last()
1391    }
1392
1393    pub fn peek_redo_stack(&self) -> Option<&HistoryEntry> {
1394        self.history.redo_stack.last()
1395    }
1396
1397    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1398        self.start_transaction_at(Instant::now())
1399    }
1400
1401    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1402        self.history
1403            .start_transaction(self.version.clone(), now, &mut self.lamport_clock)
1404    }
1405
1406    pub fn end_transaction(&mut self) -> Option<(TransactionId, clock::Global)> {
1407        self.end_transaction_at(Instant::now())
1408    }
1409
1410    pub fn end_transaction_at(&mut self, now: Instant) -> Option<(TransactionId, clock::Global)> {
1411        if let Some(entry) = self.history.end_transaction(now) {
1412            let since = entry.transaction.start.clone();
1413            let id = self.history.group().unwrap();
1414            Some((id, since))
1415        } else {
1416            None
1417        }
1418    }
1419
1420    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1421        self.history.finalize_last_transaction()
1422    }
1423
1424    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1425        self.history.group_until(transaction_id);
1426    }
1427
1428    pub fn base_text(&self) -> &Rope {
1429        &self.history.base_text
1430    }
1431
1432    pub fn operations(&self) -> &TreeMap<clock::Lamport, Operation> {
1433        &self.history.operations
1434    }
1435
1436    pub fn undo(&mut self) -> Option<(TransactionId, Operation)> {
1437        if let Some(entry) = self.history.pop_undo() {
1438            let transaction = entry.transaction.clone();
1439            let transaction_id = transaction.id;
1440            let op = self.undo_or_redo(transaction);
1441            Some((transaction_id, op))
1442        } else {
1443            None
1444        }
1445    }
1446
1447    pub fn undo_transaction(&mut self, transaction_id: TransactionId) -> Option<Operation> {
1448        let transaction = self
1449            .history
1450            .remove_from_undo(transaction_id)?
1451            .transaction
1452            .clone();
1453        Some(self.undo_or_redo(transaction))
1454    }
1455
1456    pub fn undo_to_transaction(&mut self, transaction_id: TransactionId) -> Vec<Operation> {
1457        let transactions = self
1458            .history
1459            .remove_from_undo_until(transaction_id)
1460            .iter()
1461            .map(|entry| entry.transaction.clone())
1462            .collect::<Vec<_>>();
1463
1464        transactions
1465            .into_iter()
1466            .map(|transaction| self.undo_or_redo(transaction))
1467            .collect()
1468    }
1469
1470    pub fn forget_transaction(&mut self, transaction_id: TransactionId) -> Option<Transaction> {
1471        self.history.forget(transaction_id)
1472    }
1473
1474    pub fn get_transaction(&self, transaction_id: TransactionId) -> Option<&Transaction> {
1475        self.history.transaction(transaction_id)
1476    }
1477
1478    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
1479        self.history.merge_transactions(transaction, destination);
1480    }
1481
1482    pub fn redo(&mut self) -> Option<(TransactionId, Operation)> {
1483        if let Some(entry) = self.history.pop_redo() {
1484            let transaction = entry.transaction.clone();
1485            let transaction_id = transaction.id;
1486            let op = self.undo_or_redo(transaction);
1487            Some((transaction_id, op))
1488        } else {
1489            None
1490        }
1491    }
1492
1493    pub fn redo_to_transaction(&mut self, transaction_id: TransactionId) -> Vec<Operation> {
1494        let transactions = self
1495            .history
1496            .remove_from_redo(transaction_id)
1497            .iter()
1498            .map(|entry| entry.transaction.clone())
1499            .collect::<Vec<_>>();
1500
1501        transactions
1502            .into_iter()
1503            .map(|transaction| self.undo_or_redo(transaction))
1504            .collect()
1505    }
1506
1507    fn undo_or_redo(&mut self, transaction: Transaction) -> Operation {
1508        let mut counts = HashMap::default();
1509        for edit_id in transaction.edit_ids {
1510            counts.insert(edit_id, self.undo_map.undo_count(edit_id).saturating_add(1));
1511        }
1512
1513        let operation = self.undo_operations(counts);
1514        self.history.push(operation.clone());
1515        operation
1516    }
1517
1518    pub fn undo_operations(&mut self, counts: HashMap<clock::Lamport, u32>) -> Operation {
1519        let timestamp = self.lamport_clock.tick();
1520        let version = self.version();
1521        self.snapshot.version.observe(timestamp);
1522        let undo = UndoOperation {
1523            timestamp,
1524            version,
1525            counts,
1526        };
1527        self.apply_undo(&undo);
1528        Operation::Undo(undo)
1529    }
1530
1531    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1532        self.history.push_transaction(transaction, now);
1533    }
1534
1535    /// Differs from `push_transaction` in that it does not clear the redo stack.
1536    /// The caller responsible for
1537    /// Differs from `push_transaction` in that it does not clear the redo
1538    /// stack. Intended to be used to create a parent transaction to merge
1539    /// potential child transactions into.
1540    ///
1541    /// The caller is responsible for removing it from the undo history using
1542    /// `forget_transaction` if no edits are merged into it. Otherwise, if edits
1543    /// are merged into this transaction, the caller is responsible for ensuring
1544    /// the redo stack is cleared. The easiest way to ensure the redo stack is
1545    /// cleared is to create transactions with the usual `start_transaction` and
1546    /// `end_transaction` methods and merging the resulting transactions into
1547    /// the transaction created by this method
1548    pub fn push_empty_transaction(&mut self, now: Instant) -> TransactionId {
1549        self.history
1550            .push_empty_transaction(self.version.clone(), now, &mut self.lamport_clock)
1551    }
1552
1553    pub fn edited_ranges_for_transaction_id<D>(
1554        &self,
1555        transaction_id: TransactionId,
1556    ) -> impl '_ + Iterator<Item = Range<D>>
1557    where
1558        D: TextDimension,
1559    {
1560        self.history
1561            .transaction(transaction_id)
1562            .into_iter()
1563            .flat_map(|transaction| self.edited_ranges_for_transaction(transaction))
1564    }
1565
1566    pub fn edited_ranges_for_edit_ids<'a, D>(
1567        &'a self,
1568        edit_ids: impl IntoIterator<Item = &'a clock::Lamport>,
1569    ) -> impl 'a + Iterator<Item = Range<D>>
1570    where
1571        D: TextDimension,
1572    {
1573        // get fragment ranges
1574        let mut cursor = self
1575            .fragments
1576            .cursor::<Dimensions<Option<&Locator>, usize>>(&None);
1577        let offset_ranges = self
1578            .fragment_ids_for_edits(edit_ids.into_iter())
1579            .into_iter()
1580            .filter_map(move |fragment_id| {
1581                cursor.seek_forward(&Some(fragment_id), Bias::Left);
1582                let fragment = cursor.item()?;
1583                let start_offset = cursor.start().1;
1584                let end_offset = start_offset + if fragment.visible { fragment.len } else { 0 };
1585                Some(start_offset..end_offset)
1586            });
1587
1588        // combine adjacent ranges
1589        let mut prev_range: Option<Range<usize>> = None;
1590        let disjoint_ranges = offset_ranges
1591            .map(Some)
1592            .chain([None])
1593            .filter_map(move |range| {
1594                if let Some((range, prev_range)) = range.as_ref().zip(prev_range.as_mut())
1595                    && prev_range.end == range.start
1596                {
1597                    prev_range.end = range.end;
1598                    return None;
1599                }
1600                let result = prev_range.clone();
1601                prev_range = range;
1602                result
1603            });
1604
1605        // convert to the desired text dimension.
1606        let mut position = D::zero(());
1607        let mut rope_cursor = self.visible_text.cursor(0);
1608        disjoint_ranges.map(move |range| {
1609            position.add_assign(&rope_cursor.summary(range.start));
1610            let start = position;
1611            position.add_assign(&rope_cursor.summary(range.end));
1612            let end = position;
1613            start..end
1614        })
1615    }
1616
1617    pub fn edited_ranges_for_transaction<'a, D>(
1618        &'a self,
1619        transaction: &'a Transaction,
1620    ) -> impl 'a + Iterator<Item = Range<D>>
1621    where
1622        D: TextDimension,
1623    {
1624        self.edited_ranges_for_edit_ids(&transaction.edit_ids)
1625    }
1626
1627    pub fn subscribe(&mut self) -> Subscription<usize> {
1628        self.subscriptions.subscribe()
1629    }
1630
1631    pub fn wait_for_edits<It: IntoIterator<Item = clock::Lamport>>(
1632        &mut self,
1633        edit_ids: It,
1634    ) -> impl 'static + Future<Output = Result<()>> + use<It> {
1635        let mut futures = Vec::new();
1636        for edit_id in edit_ids {
1637            if !self.version.observed(edit_id) {
1638                let (tx, rx) = oneshot::channel();
1639                self.edit_id_resolvers.entry(edit_id).or_default().push(tx);
1640                futures.push(rx);
1641            }
1642        }
1643
1644        async move {
1645            for mut future in futures {
1646                if future.recv().await.is_none() {
1647                    anyhow::bail!("gave up waiting for edits");
1648                }
1649            }
1650            Ok(())
1651        }
1652    }
1653
1654    pub fn wait_for_anchors<It: IntoIterator<Item = Anchor>>(
1655        &mut self,
1656        anchors: It,
1657    ) -> impl 'static + Future<Output = Result<()>> + use<It> {
1658        let mut futures = Vec::new();
1659        for anchor in anchors {
1660            if !self.version.observed(anchor.timestamp) && !anchor.is_max() && !anchor.is_min() {
1661                let (tx, rx) = oneshot::channel();
1662                self.edit_id_resolvers
1663                    .entry(anchor.timestamp)
1664                    .or_default()
1665                    .push(tx);
1666                futures.push(rx);
1667            }
1668        }
1669
1670        async move {
1671            for mut future in futures {
1672                if future.recv().await.is_none() {
1673                    anyhow::bail!("gave up waiting for anchors");
1674                }
1675            }
1676            Ok(())
1677        }
1678    }
1679
1680    pub fn wait_for_version(
1681        &mut self,
1682        version: clock::Global,
1683    ) -> impl Future<Output = Result<()>> + use<> {
1684        let mut rx = None;
1685        if !self.snapshot.version.observed_all(&version) {
1686            let channel = oneshot::channel();
1687            self.wait_for_version_txs.push((version, channel.0));
1688            rx = Some(channel.1);
1689        }
1690        async move {
1691            if let Some(mut rx) = rx
1692                && rx.recv().await.is_none()
1693            {
1694                anyhow::bail!("gave up waiting for version");
1695            }
1696            Ok(())
1697        }
1698    }
1699
1700    pub fn give_up_waiting(&mut self) {
1701        self.edit_id_resolvers.clear();
1702        self.wait_for_version_txs.clear();
1703    }
1704
1705    fn resolve_edit(&mut self, edit_id: clock::Lamport) {
1706        for mut tx in self
1707            .edit_id_resolvers
1708            .remove(&edit_id)
1709            .into_iter()
1710            .flatten()
1711        {
1712            tx.try_send(()).ok();
1713        }
1714    }
1715}
1716
1717#[cfg(any(test, feature = "test-support"))]
1718impl Buffer {
1719    #[track_caller]
1720    pub fn edit_via_marked_text(&mut self, marked_string: &str) {
1721        let edits = self.edits_for_marked_text(marked_string);
1722        self.edit(edits);
1723    }
1724
1725    #[track_caller]
1726    pub fn edits_for_marked_text(&self, marked_string: &str) -> Vec<(Range<usize>, String)> {
1727        let old_text = self.text();
1728        let (new_text, mut ranges) = util::test::marked_text_ranges(marked_string, false);
1729        if ranges.is_empty() {
1730            ranges.push(0..new_text.len());
1731        }
1732
1733        assert_eq!(
1734            old_text[..ranges[0].start],
1735            new_text[..ranges[0].start],
1736            "invalid edit"
1737        );
1738
1739        let mut delta = 0;
1740        let mut edits = Vec::new();
1741        let mut ranges = ranges.into_iter().peekable();
1742
1743        while let Some(inserted_range) = ranges.next() {
1744            let new_start = inserted_range.start;
1745            let old_start = (new_start as isize - delta) as usize;
1746
1747            let following_text = if let Some(next_range) = ranges.peek() {
1748                &new_text[inserted_range.end..next_range.start]
1749            } else {
1750                &new_text[inserted_range.end..]
1751            };
1752
1753            let inserted_len = inserted_range.len();
1754            let deleted_len = old_text[old_start..]
1755                .find(following_text)
1756                .expect("invalid edit");
1757
1758            let old_range = old_start..old_start + deleted_len;
1759            edits.push((old_range, new_text[inserted_range].to_string()));
1760            delta += inserted_len as isize - deleted_len as isize;
1761        }
1762
1763        assert_eq!(
1764            old_text.len() as isize + delta,
1765            new_text.len() as isize,
1766            "invalid edit"
1767        );
1768
1769        edits
1770    }
1771
1772    pub fn check_invariants(&self) {
1773        // Ensure every fragment is ordered by locator in the fragment tree and corresponds
1774        // to an insertion fragment in the insertions tree.
1775        let mut prev_fragment_id = Locator::min();
1776        for fragment in self.snapshot.fragments.items(&None) {
1777            assert!(fragment.id > prev_fragment_id);
1778            prev_fragment_id = fragment.id.clone();
1779
1780            let insertion_fragment = self
1781                .snapshot
1782                .insertions
1783                .get(
1784                    &InsertionFragmentKey {
1785                        timestamp: fragment.timestamp,
1786                        split_offset: fragment.insertion_offset,
1787                    },
1788                    (),
1789                )
1790                .unwrap();
1791            assert_eq!(
1792                insertion_fragment.fragment_id, fragment.id,
1793                "fragment: {:?}\ninsertion: {:?}",
1794                fragment, insertion_fragment
1795            );
1796        }
1797
1798        let mut cursor = self.snapshot.fragments.cursor::<Option<&Locator>>(&None);
1799        for insertion_fragment in self.snapshot.insertions.cursor::<()>(()) {
1800            cursor.seek(&Some(&insertion_fragment.fragment_id), Bias::Left);
1801            let fragment = cursor.item().unwrap();
1802            assert_eq!(insertion_fragment.fragment_id, fragment.id);
1803            assert_eq!(insertion_fragment.split_offset, fragment.insertion_offset);
1804        }
1805
1806        let fragment_summary = self.snapshot.fragments.summary();
1807        assert_eq!(
1808            fragment_summary.text.visible,
1809            self.snapshot.visible_text.len()
1810        );
1811        assert_eq!(
1812            fragment_summary.text.deleted,
1813            self.snapshot.deleted_text.len()
1814        );
1815
1816        assert!(!self.text().contains("\r\n"));
1817    }
1818
1819    pub fn set_group_interval(&mut self, group_interval: Duration) {
1820        self.history.group_interval = group_interval;
1821    }
1822
1823    pub fn random_byte_range(&self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
1824        let end = self.clip_offset(rng.random_range(start_offset..=self.len()), Bias::Right);
1825        let start = self.clip_offset(rng.random_range(start_offset..=end), Bias::Right);
1826        start..end
1827    }
1828
1829    pub fn get_random_edits<T>(
1830        &self,
1831        rng: &mut T,
1832        edit_count: usize,
1833    ) -> Vec<(Range<usize>, Arc<str>)>
1834    where
1835        T: rand::Rng,
1836    {
1837        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1838        let mut last_end = None;
1839        for _ in 0..edit_count {
1840            if last_end.is_some_and(|last_end| last_end >= self.len()) {
1841                break;
1842            }
1843            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1844            let range = self.random_byte_range(new_start, rng);
1845            last_end = Some(range.end);
1846
1847            let new_text_len = rng.random_range(0..10);
1848            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1849
1850            edits.push((range, new_text.into()));
1851        }
1852        edits
1853    }
1854
1855    pub fn randomly_edit<T>(
1856        &mut self,
1857        rng: &mut T,
1858        edit_count: usize,
1859    ) -> (Vec<(Range<usize>, Arc<str>)>, Operation)
1860    where
1861        T: rand::Rng,
1862    {
1863        let mut edits = self.get_random_edits(rng, edit_count);
1864        log::info!("mutating buffer {:?} with {:?}", self.replica_id, edits);
1865
1866        let op = self.edit(edits.iter().cloned());
1867        if let Operation::Edit(edit) = &op {
1868            assert_eq!(edits.len(), edit.new_text.len());
1869            for (edit, new_text) in edits.iter_mut().zip(&edit.new_text) {
1870                edit.1 = new_text.clone();
1871            }
1872        } else {
1873            unreachable!()
1874        }
1875
1876        (edits, op)
1877    }
1878
1879    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
1880        use rand::prelude::*;
1881
1882        let mut ops = Vec::new();
1883        for _ in 0..rng.random_range(1..=5) {
1884            if let Some(entry) = self.history.undo_stack.choose(rng) {
1885                let transaction = entry.transaction.clone();
1886                log::info!(
1887                    "undoing buffer {:?} transaction {:?}",
1888                    self.replica_id,
1889                    transaction
1890                );
1891                ops.push(self.undo_or_redo(transaction));
1892            }
1893        }
1894        ops
1895    }
1896}
1897
1898impl Deref for Buffer {
1899    type Target = BufferSnapshot;
1900
1901    fn deref(&self) -> &Self::Target {
1902        &self.snapshot
1903    }
1904}
1905
1906impl BufferSnapshot {
1907    pub fn as_rope(&self) -> &Rope {
1908        &self.visible_text
1909    }
1910
1911    pub fn rope_for_version(&self, version: &clock::Global) -> Rope {
1912        let mut rope = Rope::new();
1913
1914        let mut cursor = self
1915            .fragments
1916            .filter::<_, FragmentTextSummary>(&None, move |summary| {
1917                !version.observed_all(&summary.max_version)
1918            });
1919        cursor.next();
1920
1921        let mut visible_cursor = self.visible_text.cursor(0);
1922        let mut deleted_cursor = self.deleted_text.cursor(0);
1923
1924        while let Some(fragment) = cursor.item() {
1925            if cursor.start().visible > visible_cursor.offset() {
1926                let text = visible_cursor.slice(cursor.start().visible);
1927                rope.append(text);
1928            }
1929
1930            if fragment.was_visible(version, &self.undo_map) {
1931                if fragment.visible {
1932                    let text = visible_cursor.slice(cursor.end().visible);
1933                    rope.append(text);
1934                } else {
1935                    deleted_cursor.seek_forward(cursor.start().deleted);
1936                    let text = deleted_cursor.slice(cursor.end().deleted);
1937                    rope.append(text);
1938                }
1939            } else if fragment.visible {
1940                visible_cursor.seek_forward(cursor.end().visible);
1941            }
1942
1943            cursor.next();
1944        }
1945
1946        if cursor.start().visible > visible_cursor.offset() {
1947            let text = visible_cursor.slice(cursor.start().visible);
1948            rope.append(text);
1949        }
1950
1951        rope
1952    }
1953
1954    pub fn remote_id(&self) -> BufferId {
1955        self.remote_id
1956    }
1957
1958    pub fn replica_id(&self) -> ReplicaId {
1959        self.replica_id
1960    }
1961
1962    pub fn row_count(&self) -> u32 {
1963        self.max_point().row + 1
1964    }
1965
1966    pub fn len(&self) -> usize {
1967        self.visible_text.len()
1968    }
1969
1970    pub fn is_empty(&self) -> bool {
1971        self.len() == 0
1972    }
1973
1974    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1975        self.chars_at(0)
1976    }
1977
1978    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1979        self.text_for_range(range).flat_map(str::chars)
1980    }
1981
1982    pub fn reversed_chars_for_range<T: ToOffset>(
1983        &self,
1984        range: Range<T>,
1985    ) -> impl Iterator<Item = char> + '_ {
1986        self.reversed_chunks_in_range(range)
1987            .flat_map(|chunk| chunk.chars().rev())
1988    }
1989
1990    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1991    where
1992        T: ToOffset,
1993    {
1994        let position = position.to_offset(self);
1995        position == self.clip_offset(position, Bias::Left)
1996            && self
1997                .bytes_in_range(position..self.len())
1998                .flatten()
1999                .copied()
2000                .take(needle.len())
2001                .eq(needle.bytes())
2002    }
2003
2004    pub fn common_prefix_at<T>(&self, position: T, needle: &str) -> Range<T>
2005    where
2006        T: ToOffset + TextDimension,
2007    {
2008        let offset = position.to_offset(self);
2009        let common_prefix_len = needle
2010            .char_indices()
2011            .map(|(index, _)| index)
2012            .chain([needle.len()])
2013            .take_while(|&len| len <= offset)
2014            .filter(|&len| {
2015                let left = self
2016                    .chars_for_range(offset - len..offset)
2017                    .flat_map(char::to_lowercase);
2018                let right = needle[..len].chars().flat_map(char::to_lowercase);
2019                left.eq(right)
2020            })
2021            .last()
2022            .unwrap_or(0);
2023        let start_offset = offset - common_prefix_len;
2024        let start = self.text_summary_for_range(0..start_offset);
2025        start..position
2026    }
2027
2028    pub fn text(&self) -> String {
2029        self.visible_text.to_string()
2030    }
2031
2032    pub fn line_ending(&self) -> LineEnding {
2033        self.line_ending
2034    }
2035
2036    pub fn deleted_text(&self) -> String {
2037        self.deleted_text.to_string()
2038    }
2039
2040    pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
2041        self.fragments.iter()
2042    }
2043
2044    pub fn text_summary(&self) -> TextSummary {
2045        self.visible_text.summary()
2046    }
2047
2048    pub fn max_point(&self) -> Point {
2049        self.visible_text.max_point()
2050    }
2051
2052    pub fn max_point_utf16(&self) -> PointUtf16 {
2053        self.visible_text.max_point_utf16()
2054    }
2055
2056    pub fn point_to_offset(&self, point: Point) -> usize {
2057        self.visible_text.point_to_offset(point)
2058    }
2059
2060    pub fn point_to_offset_utf16(&self, point: Point) -> OffsetUtf16 {
2061        self.visible_text.point_to_offset_utf16(point)
2062    }
2063
2064    pub fn point_utf16_to_offset_utf16(&self, point: PointUtf16) -> OffsetUtf16 {
2065        self.visible_text.point_utf16_to_offset_utf16(point)
2066    }
2067
2068    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
2069        self.visible_text.point_utf16_to_offset(point)
2070    }
2071
2072    pub fn unclipped_point_utf16_to_offset(&self, point: Unclipped<PointUtf16>) -> usize {
2073        self.visible_text.unclipped_point_utf16_to_offset(point)
2074    }
2075
2076    pub fn unclipped_point_utf16_to_point(&self, point: Unclipped<PointUtf16>) -> Point {
2077        self.visible_text.unclipped_point_utf16_to_point(point)
2078    }
2079
2080    pub fn offset_utf16_to_offset(&self, offset: OffsetUtf16) -> usize {
2081        self.visible_text.offset_utf16_to_offset(offset)
2082    }
2083
2084    pub fn offset_to_offset_utf16(&self, offset: usize) -> OffsetUtf16 {
2085        self.visible_text.offset_to_offset_utf16(offset)
2086    }
2087
2088    pub fn offset_to_point(&self, offset: usize) -> Point {
2089        self.visible_text.offset_to_point(offset)
2090    }
2091
2092    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
2093        self.visible_text.offset_to_point_utf16(offset)
2094    }
2095
2096    pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
2097        self.visible_text.point_to_point_utf16(point)
2098    }
2099
2100    pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
2101        self.visible_text.point_utf16_to_point(point)
2102    }
2103
2104    pub fn version(&self) -> &clock::Global {
2105        &self.version
2106    }
2107
2108    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
2109        let offset = position.to_offset(self);
2110        self.visible_text.chars_at(offset)
2111    }
2112
2113    pub fn reversed_chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
2114        let offset = position.to_offset(self);
2115        self.visible_text.reversed_chars_at(offset)
2116    }
2117
2118    pub fn reversed_chunks_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Chunks<'_> {
2119        let range = range.start.to_offset(self)..range.end.to_offset(self);
2120        self.visible_text.reversed_chunks_in_range(range)
2121    }
2122
2123    pub fn bytes_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Bytes<'_> {
2124        let start = range.start.to_offset(self);
2125        let end = range.end.to_offset(self);
2126        self.visible_text.bytes_in_range(start..end)
2127    }
2128
2129    pub fn reversed_bytes_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Bytes<'_> {
2130        let start = range.start.to_offset(self);
2131        let end = range.end.to_offset(self);
2132        self.visible_text.reversed_bytes_in_range(start..end)
2133    }
2134
2135    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks<'_> {
2136        let start = range.start.to_offset(self);
2137        let end = range.end.to_offset(self);
2138        self.visible_text.chunks_in_range(start..end)
2139    }
2140
2141    pub fn line_len(&self, row: u32) -> u32 {
2142        let row_start_offset = Point::new(row, 0).to_offset(self);
2143        let row_end_offset = if row >= self.max_point().row {
2144            self.len()
2145        } else {
2146            Point::new(row + 1, 0).to_previous_offset(self)
2147        };
2148        (row_end_offset - row_start_offset) as u32
2149    }
2150
2151    pub fn line_indents_in_row_range(
2152        &self,
2153        row_range: Range<u32>,
2154    ) -> impl Iterator<Item = (u32, LineIndent)> + '_ {
2155        let start = Point::new(row_range.start, 0).to_offset(self);
2156        let end = Point::new(row_range.end, self.line_len(row_range.end)).to_offset(self);
2157
2158        let mut chunks = self.as_rope().chunks_in_range(start..end);
2159        let mut row = row_range.start;
2160        let mut done = false;
2161        std::iter::from_fn(move || {
2162            if done {
2163                None
2164            } else {
2165                let indent = (row, LineIndent::from_chunks(&mut chunks));
2166                done = !chunks.next_line();
2167                row += 1;
2168                Some(indent)
2169            }
2170        })
2171    }
2172
2173    /// Returns the line indents in the given row range, exclusive of end row, in reversed order.
2174    pub fn reversed_line_indents_in_row_range(
2175        &self,
2176        row_range: Range<u32>,
2177    ) -> impl Iterator<Item = (u32, LineIndent)> + '_ {
2178        let start = Point::new(row_range.start, 0).to_offset(self);
2179
2180        let end_point;
2181        let end;
2182        if row_range.end > row_range.start {
2183            end_point = Point::new(row_range.end - 1, self.line_len(row_range.end - 1));
2184            end = end_point.to_offset(self);
2185        } else {
2186            end_point = Point::new(row_range.start, 0);
2187            end = start;
2188        };
2189
2190        let mut chunks = self.as_rope().chunks_in_range(start..end);
2191        // Move the cursor to the start of the last line if it's not empty.
2192        chunks.seek(end);
2193        if end_point.column > 0 {
2194            chunks.prev_line();
2195        }
2196
2197        let mut row = end_point.row;
2198        let mut done = false;
2199        std::iter::from_fn(move || {
2200            if done {
2201                None
2202            } else {
2203                let initial_offset = chunks.offset();
2204                let indent = (row, LineIndent::from_chunks(&mut chunks));
2205                if chunks.offset() > initial_offset {
2206                    chunks.prev_line();
2207                }
2208                done = !chunks.prev_line();
2209                if !done {
2210                    row -= 1;
2211                }
2212
2213                Some(indent)
2214            }
2215        })
2216    }
2217
2218    pub fn line_indent_for_row(&self, row: u32) -> LineIndent {
2219        LineIndent::from_iter(self.chars_at(Point::new(row, 0)))
2220    }
2221
2222    pub fn is_line_blank(&self, row: u32) -> bool {
2223        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
2224            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
2225    }
2226
2227    pub fn text_summary_for_range<D, O: ToOffset>(&self, range: Range<O>) -> D
2228    where
2229        D: TextDimension,
2230    {
2231        self.visible_text
2232            .cursor(range.start.to_offset(self))
2233            .summary(range.end.to_offset(self))
2234    }
2235
2236    pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
2237    where
2238        D: 'a + TextDimension,
2239        A: 'a + IntoIterator<Item = &'a Anchor>,
2240    {
2241        let anchors = anchors.into_iter();
2242        self.summaries_for_anchors_with_payload::<D, _, ()>(anchors.map(|a| (a, ())))
2243            .map(|d| d.0)
2244    }
2245
2246    pub fn summaries_for_anchors_with_payload<'a, D, A, T>(
2247        &'a self,
2248        anchors: A,
2249    ) -> impl 'a + Iterator<Item = (D, T)>
2250    where
2251        D: 'a + TextDimension,
2252        A: 'a + IntoIterator<Item = (&'a Anchor, T)>,
2253    {
2254        let anchors = anchors.into_iter();
2255        let mut fragment_cursor = self
2256            .fragments
2257            .cursor::<Dimensions<Option<&Locator>, usize>>(&None);
2258        let mut text_cursor = self.visible_text.cursor(0);
2259        let mut position = D::zero(());
2260
2261        anchors.map(move |(anchor, payload)| {
2262            if anchor.is_min() {
2263                return (D::zero(()), payload);
2264            } else if anchor.is_max() {
2265                return (D::from_text_summary(&self.visible_text.summary()), payload);
2266            }
2267
2268            let Some(insertion) = self.try_find_fragment(anchor) else {
2269                panic!(
2270                    "invalid insertion for buffer {}@{:?} with anchor {:?}",
2271                    self.remote_id(),
2272                    self.version,
2273                    anchor
2274                );
2275            };
2276            assert_eq!(
2277                insertion.timestamp,
2278                anchor.timestamp,
2279                "invalid insertion for buffer {}@{:?} and anchor {:?}",
2280                self.remote_id(),
2281                self.version,
2282                anchor
2283            );
2284
2285            fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left);
2286            let fragment = fragment_cursor.item().unwrap();
2287            let mut fragment_offset = fragment_cursor.start().1;
2288            if fragment.visible {
2289                fragment_offset += anchor.offset - insertion.split_offset;
2290            }
2291
2292            position.add_assign(&text_cursor.summary(fragment_offset));
2293            (position, payload)
2294        })
2295    }
2296
2297    pub fn summary_for_anchor<D>(&self, anchor: &Anchor) -> D
2298    where
2299        D: TextDimension,
2300    {
2301        self.text_summary_for_range(0..self.offset_for_anchor(anchor))
2302    }
2303
2304    pub fn offset_for_anchor(&self, anchor: &Anchor) -> usize {
2305        if anchor.is_min() {
2306            0
2307        } else if anchor.is_max() {
2308            self.visible_text.len()
2309        } else {
2310            debug_assert_eq!(anchor.buffer_id, Some(self.remote_id));
2311            debug_assert!(
2312                self.version.observed(anchor.timestamp),
2313                "Anchor timestamp {:?} not observed by buffer {:?}",
2314                anchor.timestamp,
2315                self.version
2316            );
2317            let item = self.try_find_fragment(anchor);
2318            let Some(insertion) = item.filter(|insertion| insertion.timestamp == anchor.timestamp)
2319            else {
2320                self.panic_bad_anchor(anchor);
2321            };
2322
2323            let (start, _, item) = self
2324                .fragments
2325                .find::<Dimensions<Option<&Locator>, usize>, _>(
2326                    &None,
2327                    &Some(&insertion.fragment_id),
2328                    Bias::Left,
2329                );
2330            let fragment = item.unwrap();
2331            let mut fragment_offset = start.1;
2332            if fragment.visible {
2333                fragment_offset += anchor.offset - insertion.split_offset;
2334            }
2335            fragment_offset
2336        }
2337    }
2338
2339    #[cold]
2340    fn panic_bad_anchor(&self, anchor: &Anchor) -> ! {
2341        if anchor.buffer_id.is_some_and(|id| id != self.remote_id) {
2342            panic!(
2343                "invalid anchor - buffer id does not match: anchor {anchor:?}; buffer id: {}, version: {:?}",
2344                self.remote_id, self.version
2345            );
2346        } else if !self.version.observed(anchor.timestamp) {
2347            panic!(
2348                "invalid anchor - snapshot has not observed lamport: {:?}; version: {:?}",
2349                anchor, self.version
2350            );
2351        } else {
2352            panic!(
2353                "invalid anchor {:?}. buffer id: {}, version: {:?}",
2354                anchor, self.remote_id, self.version
2355            );
2356        }
2357    }
2358
2359    fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
2360        self.try_fragment_id_for_anchor(anchor)
2361            .unwrap_or_else(|| self.panic_bad_anchor(anchor))
2362    }
2363
2364    fn try_fragment_id_for_anchor(&self, anchor: &Anchor) -> Option<&Locator> {
2365        if anchor.is_min() {
2366            Some(Locator::min_ref())
2367        } else if anchor.is_max() {
2368            Some(Locator::max_ref())
2369        } else {
2370            let item = self.try_find_fragment(anchor);
2371            item.filter(|insertion| {
2372                !cfg!(debug_assertions) || insertion.timestamp == anchor.timestamp
2373            })
2374            .map(|insertion| &insertion.fragment_id)
2375        }
2376    }
2377
2378    fn try_find_fragment(&self, anchor: &Anchor) -> Option<&InsertionFragment> {
2379        let anchor_key = InsertionFragmentKey {
2380            timestamp: anchor.timestamp,
2381            split_offset: anchor.offset,
2382        };
2383        match self.insertions.find_with_prev::<InsertionFragmentKey, _>(
2384            (),
2385            &anchor_key,
2386            anchor.bias,
2387        ) {
2388            (_, _, Some((prev, insertion))) => {
2389                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
2390                if comparison == Ordering::Greater
2391                    || (anchor.bias == Bias::Left
2392                        && comparison == Ordering::Equal
2393                        && anchor.offset > 0)
2394                {
2395                    prev
2396                } else {
2397                    Some(insertion)
2398                }
2399            }
2400            _ => self.insertions.last(),
2401        }
2402    }
2403
2404    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2405        self.anchor_at(position, Bias::Left)
2406    }
2407
2408    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2409        self.anchor_at(position, Bias::Right)
2410    }
2411
2412    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
2413        self.anchor_at_offset(position.to_offset(self), bias)
2414    }
2415
2416    fn anchor_at_offset(&self, mut offset: usize, bias: Bias) -> Anchor {
2417        if bias == Bias::Left && offset == 0 {
2418            Anchor::min_for_buffer(self.remote_id)
2419        } else if bias == Bias::Right
2420            && ((!cfg!(debug_assertions) && offset >= self.len()) || offset == self.len())
2421        {
2422            Anchor::max_for_buffer(self.remote_id)
2423        } else {
2424            if !self
2425                .visible_text
2426                .assert_char_boundary::<{ cfg!(debug_assertions) }>(offset)
2427            {
2428                offset = match bias {
2429                    Bias::Left => self.visible_text.floor_char_boundary(offset),
2430                    Bias::Right => self.visible_text.ceil_char_boundary(offset),
2431                };
2432            }
2433            let (start, _, item) = self.fragments.find::<usize, _>(&None, &offset, bias);
2434            let Some(fragment) = item else {
2435                // We got a bad offset, likely out of bounds
2436                debug_panic!(
2437                    "Failed to find fragment at offset {} (len: {})",
2438                    offset,
2439                    self.len()
2440                );
2441                return Anchor::max_for_buffer(self.remote_id);
2442            };
2443            let overshoot = offset - start;
2444            Anchor {
2445                timestamp: fragment.timestamp,
2446                offset: fragment.insertion_offset + overshoot,
2447                bias,
2448                buffer_id: Some(self.remote_id),
2449            }
2450        }
2451    }
2452
2453    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
2454        anchor.is_min()
2455            || anchor.is_max()
2456            || (Some(self.remote_id) == anchor.buffer_id && self.version.observed(anchor.timestamp))
2457    }
2458
2459    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
2460        self.visible_text.clip_offset(offset, bias)
2461    }
2462
2463    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
2464        self.visible_text.clip_point(point, bias)
2465    }
2466
2467    pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
2468        self.visible_text.clip_offset_utf16(offset, bias)
2469    }
2470
2471    pub fn clip_point_utf16(&self, point: Unclipped<PointUtf16>, bias: Bias) -> PointUtf16 {
2472        self.visible_text.clip_point_utf16(point, bias)
2473    }
2474
2475    pub fn edits_since<'a, D>(
2476        &'a self,
2477        since: &'a clock::Global,
2478    ) -> impl 'a + Iterator<Item = Edit<D>>
2479    where
2480        D: TextDimension + Ord,
2481    {
2482        self.edits_since_in_range(since, Anchor::MIN..Anchor::MAX)
2483    }
2484
2485    pub fn anchored_edits_since<'a, D>(
2486        &'a self,
2487        since: &'a clock::Global,
2488    ) -> impl 'a + Iterator<Item = (Edit<D>, Range<Anchor>)>
2489    where
2490        D: TextDimension + Ord,
2491    {
2492        self.anchored_edits_since_in_range(since, Anchor::MIN..Anchor::MAX)
2493    }
2494
2495    pub fn edits_since_in_range<'a, D>(
2496        &'a self,
2497        since: &'a clock::Global,
2498        range: Range<Anchor>,
2499    ) -> impl 'a + Iterator<Item = Edit<D>>
2500    where
2501        D: TextDimension + Ord,
2502    {
2503        self.anchored_edits_since_in_range(since, range)
2504            .map(|item| item.0)
2505    }
2506
2507    pub fn anchored_edits_since_in_range<'a, D>(
2508        &'a self,
2509        since: &'a clock::Global,
2510        range: Range<Anchor>,
2511    ) -> impl 'a + Iterator<Item = (Edit<D>, Range<Anchor>)>
2512    where
2513        D: TextDimension + Ord,
2514    {
2515        if *since == self.version {
2516            return None.into_iter().flatten();
2517        }
2518        let mut cursor = self.fragments.filter(&None, move |summary| {
2519            !since.observed_all(&summary.max_version)
2520        });
2521        cursor.next();
2522        let fragments_cursor = Some(cursor);
2523        let start_fragment_id = self.fragment_id_for_anchor(&range.start);
2524        let (start, _, item) = self
2525            .fragments
2526            .find::<Dimensions<Option<&Locator>, FragmentTextSummary>, _>(
2527                &None,
2528                &Some(start_fragment_id),
2529                Bias::Left,
2530            );
2531        let mut visible_start = start.1.visible;
2532        let mut deleted_start = start.1.deleted;
2533        if let Some(fragment) = item {
2534            let overshoot = range.start.offset - fragment.insertion_offset;
2535            if fragment.visible {
2536                visible_start += overshoot;
2537            } else {
2538                deleted_start += overshoot;
2539            }
2540        }
2541        let end_fragment_id = self.fragment_id_for_anchor(&range.end);
2542
2543        Some(Edits {
2544            visible_cursor: self.visible_text.cursor(visible_start),
2545            deleted_cursor: self.deleted_text.cursor(deleted_start),
2546            fragments_cursor,
2547            undos: &self.undo_map,
2548            since,
2549            old_end: D::zero(()),
2550            new_end: D::zero(()),
2551            range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
2552            buffer_id: self.remote_id,
2553        })
2554        .into_iter()
2555        .flatten()
2556    }
2557
2558    pub fn has_edits_since_in_range(&self, since: &clock::Global, range: Range<Anchor>) -> bool {
2559        if *since != self.version {
2560            let start_fragment_id = self.fragment_id_for_anchor(&range.start);
2561            let end_fragment_id = self.fragment_id_for_anchor(&range.end);
2562            let mut cursor = self.fragments.filter::<_, usize>(&None, move |summary| {
2563                !since.observed_all(&summary.max_version)
2564            });
2565            cursor.next();
2566            while let Some(fragment) = cursor.item() {
2567                if fragment.id > *end_fragment_id {
2568                    break;
2569                }
2570                if fragment.id > *start_fragment_id {
2571                    let was_visible = fragment.was_visible(since, &self.undo_map);
2572                    let is_visible = fragment.visible;
2573                    if was_visible != is_visible {
2574                        return true;
2575                    }
2576                }
2577                cursor.next();
2578            }
2579        }
2580        false
2581    }
2582
2583    pub fn has_edits_since(&self, since: &clock::Global) -> bool {
2584        if *since != self.version {
2585            let mut cursor = self.fragments.filter::<_, usize>(&None, move |summary| {
2586                !since.observed_all(&summary.max_version)
2587            });
2588            cursor.next();
2589            while let Some(fragment) = cursor.item() {
2590                let was_visible = fragment.was_visible(since, &self.undo_map);
2591                let is_visible = fragment.visible;
2592                if was_visible != is_visible {
2593                    return true;
2594                }
2595                cursor.next();
2596            }
2597        }
2598        false
2599    }
2600
2601    pub fn range_to_version(&self, range: Range<usize>, version: &clock::Global) -> Range<usize> {
2602        let mut offsets = self.offsets_to_version([range.start, range.end], version);
2603        offsets.next().unwrap()..offsets.next().unwrap()
2604    }
2605
2606    /// Converts the given sequence of offsets into their corresponding offsets
2607    /// at a prior version of this buffer.
2608    pub fn offsets_to_version<'a>(
2609        &'a self,
2610        offsets: impl 'a + IntoIterator<Item = usize>,
2611        version: &'a clock::Global,
2612    ) -> impl 'a + Iterator<Item = usize> {
2613        let mut edits = self.edits_since(version).peekable();
2614        let mut last_old_end = 0;
2615        let mut last_new_end = 0;
2616        offsets.into_iter().map(move |new_offset| {
2617            while let Some(edit) = edits.peek() {
2618                if edit.new.start > new_offset {
2619                    break;
2620                }
2621
2622                if edit.new.end <= new_offset {
2623                    last_new_end = edit.new.end;
2624                    last_old_end = edit.old.end;
2625                    edits.next();
2626                    continue;
2627                }
2628
2629                let overshoot = new_offset - edit.new.start;
2630                return (edit.old.start + overshoot).min(edit.old.end);
2631            }
2632
2633            last_old_end + new_offset.saturating_sub(last_new_end)
2634        })
2635    }
2636
2637    /// Visually annotates a position or range with the `Debug` representation of a value. The
2638    /// callsite of this function is used as a key - previous annotations will be removed.
2639    #[cfg(debug_assertions)]
2640    #[track_caller]
2641    pub fn debug<R, V>(&self, ranges: &R, value: V)
2642    where
2643        R: debug::ToDebugRanges,
2644        V: std::fmt::Debug,
2645    {
2646        self.debug_with_key(std::panic::Location::caller(), ranges, value);
2647    }
2648
2649    /// Visually annotates a position or range with the `Debug` representation of a value. Previous
2650    /// debug annotations with the same key will be removed. The key is also used to determine the
2651    /// annotation's color.
2652    #[cfg(debug_assertions)]
2653    pub fn debug_with_key<K, R, V>(&self, key: &K, ranges: &R, value: V)
2654    where
2655        K: std::hash::Hash + 'static,
2656        R: debug::ToDebugRanges,
2657        V: std::fmt::Debug,
2658    {
2659        let ranges = ranges
2660            .to_debug_ranges(self)
2661            .into_iter()
2662            .map(|range| self.anchor_after(range.start)..self.anchor_before(range.end))
2663            .collect();
2664        debug::GlobalDebugRanges::with_locked(|debug_ranges| {
2665            debug_ranges.insert(key, ranges, format!("{value:?}").into());
2666        });
2667    }
2668}
2669
2670struct RopeBuilder<'a> {
2671    old_visible_cursor: rope::Cursor<'a>,
2672    old_deleted_cursor: rope::Cursor<'a>,
2673    new_visible: Rope,
2674    new_deleted: Rope,
2675}
2676
2677impl<'a> RopeBuilder<'a> {
2678    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
2679        Self {
2680            old_visible_cursor,
2681            old_deleted_cursor,
2682            new_visible: Rope::new(),
2683            new_deleted: Rope::new(),
2684        }
2685    }
2686
2687    fn append(&mut self, len: FragmentTextSummary) {
2688        self.push(len.visible, true, true);
2689        self.push(len.deleted, false, false);
2690    }
2691
2692    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
2693        debug_assert!(fragment.len > 0);
2694        self.push(fragment.len, was_visible, fragment.visible)
2695    }
2696
2697    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
2698        let text = if was_visible {
2699            self.old_visible_cursor
2700                .slice(self.old_visible_cursor.offset() + len)
2701        } else {
2702            self.old_deleted_cursor
2703                .slice(self.old_deleted_cursor.offset() + len)
2704        };
2705        if is_visible {
2706            self.new_visible.append(text);
2707        } else {
2708            self.new_deleted.append(text);
2709        }
2710    }
2711
2712    fn push_str(&mut self, text: &str) {
2713        self.new_visible.push(text);
2714    }
2715
2716    fn finish(mut self) -> (Rope, Rope) {
2717        self.new_visible.append(self.old_visible_cursor.suffix());
2718        self.new_deleted.append(self.old_deleted_cursor.suffix());
2719        (self.new_visible, self.new_deleted)
2720    }
2721}
2722
2723impl<D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'_, D, F> {
2724    type Item = (Edit<D>, Range<Anchor>);
2725
2726    fn next(&mut self) -> Option<Self::Item> {
2727        let mut pending_edit: Option<Self::Item> = None;
2728        let cursor = self.fragments_cursor.as_mut()?;
2729
2730        while let Some(fragment) = cursor.item() {
2731            if fragment.id < *self.range.start.0 {
2732                cursor.next();
2733                continue;
2734            } else if fragment.id > *self.range.end.0 {
2735                break;
2736            }
2737
2738            if cursor.start().visible > self.visible_cursor.offset() {
2739                let summary = self.visible_cursor.summary(cursor.start().visible);
2740                self.old_end.add_assign(&summary);
2741                self.new_end.add_assign(&summary);
2742            }
2743
2744            if pending_edit
2745                .as_ref()
2746                .is_some_and(|(change, _)| change.new.end < self.new_end)
2747            {
2748                break;
2749            }
2750
2751            let start_anchor = Anchor {
2752                timestamp: fragment.timestamp,
2753                offset: fragment.insertion_offset,
2754                bias: Bias::Right,
2755                buffer_id: Some(self.buffer_id),
2756            };
2757            let end_anchor = Anchor {
2758                timestamp: fragment.timestamp,
2759                offset: fragment.insertion_offset + fragment.len,
2760                bias: Bias::Left,
2761                buffer_id: Some(self.buffer_id),
2762            };
2763
2764            if !fragment.was_visible(self.since, self.undos) && fragment.visible {
2765                let mut visible_end = cursor.end().visible;
2766                if fragment.id == *self.range.end.0 {
2767                    visible_end = cmp::min(
2768                        visible_end,
2769                        cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
2770                    );
2771                }
2772
2773                let fragment_summary = self.visible_cursor.summary(visible_end);
2774                let mut new_end = self.new_end;
2775                new_end.add_assign(&fragment_summary);
2776                if let Some((edit, range)) = pending_edit.as_mut() {
2777                    edit.new.end = new_end;
2778                    range.end = end_anchor;
2779                } else {
2780                    pending_edit = Some((
2781                        Edit {
2782                            old: self.old_end..self.old_end,
2783                            new: self.new_end..new_end,
2784                        },
2785                        start_anchor..end_anchor,
2786                    ));
2787                }
2788
2789                self.new_end = new_end;
2790            } else if fragment.was_visible(self.since, self.undos) && !fragment.visible {
2791                let mut deleted_end = cursor.end().deleted;
2792                if fragment.id == *self.range.end.0 {
2793                    deleted_end = cmp::min(
2794                        deleted_end,
2795                        cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
2796                    );
2797                }
2798
2799                if cursor.start().deleted > self.deleted_cursor.offset() {
2800                    self.deleted_cursor.seek_forward(cursor.start().deleted);
2801                }
2802                let fragment_summary = self.deleted_cursor.summary(deleted_end);
2803                let mut old_end = self.old_end;
2804                old_end.add_assign(&fragment_summary);
2805                if let Some((edit, range)) = pending_edit.as_mut() {
2806                    edit.old.end = old_end;
2807                    range.end = end_anchor;
2808                } else {
2809                    pending_edit = Some((
2810                        Edit {
2811                            old: self.old_end..old_end,
2812                            new: self.new_end..self.new_end,
2813                        },
2814                        start_anchor..end_anchor,
2815                    ));
2816                }
2817
2818                self.old_end = old_end;
2819            }
2820
2821            cursor.next();
2822        }
2823
2824        pending_edit
2825    }
2826}
2827
2828impl Fragment {
2829    fn is_visible(&self, undos: &UndoMap) -> bool {
2830        !undos.is_undone(self.timestamp) && self.deletions.iter().all(|d| undos.is_undone(*d))
2831    }
2832
2833    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
2834        (version.observed(self.timestamp) && !undos.was_undone(self.timestamp, version))
2835            && self
2836                .deletions
2837                .iter()
2838                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
2839    }
2840}
2841
2842impl sum_tree::Item for Fragment {
2843    type Summary = FragmentSummary;
2844
2845    fn summary(&self, _cx: &Option<clock::Global>) -> Self::Summary {
2846        let mut max_version = clock::Global::new();
2847        max_version.observe(self.timestamp);
2848        for deletion in &self.deletions {
2849            max_version.observe(*deletion);
2850        }
2851        max_version.join(&self.max_undos);
2852
2853        let mut min_insertion_version = clock::Global::new();
2854        min_insertion_version.observe(self.timestamp);
2855        let max_insertion_version = min_insertion_version.clone();
2856        if self.visible {
2857            FragmentSummary {
2858                max_id: self.id.clone(),
2859                text: FragmentTextSummary {
2860                    visible: self.len,
2861                    deleted: 0,
2862                },
2863                max_version,
2864                min_insertion_version,
2865                max_insertion_version,
2866            }
2867        } else {
2868            FragmentSummary {
2869                max_id: self.id.clone(),
2870                text: FragmentTextSummary {
2871                    visible: 0,
2872                    deleted: self.len,
2873                },
2874                max_version,
2875                min_insertion_version,
2876                max_insertion_version,
2877            }
2878        }
2879    }
2880}
2881
2882impl sum_tree::Summary for FragmentSummary {
2883    type Context<'a> = &'a Option<clock::Global>;
2884
2885    fn zero(_cx: Self::Context<'_>) -> Self {
2886        Default::default()
2887    }
2888
2889    fn add_summary(&mut self, other: &Self, _: Self::Context<'_>) {
2890        self.max_id.assign(&other.max_id);
2891        self.text.visible += &other.text.visible;
2892        self.text.deleted += &other.text.deleted;
2893        self.max_version.join(&other.max_version);
2894        self.min_insertion_version
2895            .meet(&other.min_insertion_version);
2896        self.max_insertion_version
2897            .join(&other.max_insertion_version);
2898    }
2899}
2900
2901impl Default for FragmentSummary {
2902    fn default() -> Self {
2903        FragmentSummary {
2904            max_id: Locator::min(),
2905            text: FragmentTextSummary::default(),
2906            max_version: clock::Global::new(),
2907            min_insertion_version: clock::Global::new(),
2908            max_insertion_version: clock::Global::new(),
2909        }
2910    }
2911}
2912
2913impl sum_tree::Item for InsertionFragment {
2914    type Summary = InsertionFragmentKey;
2915
2916    fn summary(&self, _cx: ()) -> Self::Summary {
2917        InsertionFragmentKey {
2918            timestamp: self.timestamp,
2919            split_offset: self.split_offset,
2920        }
2921    }
2922}
2923
2924impl sum_tree::KeyedItem for InsertionFragment {
2925    type Key = InsertionFragmentKey;
2926
2927    fn key(&self) -> Self::Key {
2928        sum_tree::Item::summary(self, ())
2929    }
2930}
2931
2932impl InsertionFragment {
2933    fn new(fragment: &Fragment) -> Self {
2934        Self {
2935            timestamp: fragment.timestamp,
2936            split_offset: fragment.insertion_offset,
2937            fragment_id: fragment.id.clone(),
2938        }
2939    }
2940
2941    fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
2942        sum_tree::Edit::Insert(Self::new(fragment))
2943    }
2944}
2945
2946impl sum_tree::ContextLessSummary for InsertionFragmentKey {
2947    fn zero() -> Self {
2948        InsertionFragmentKey {
2949            timestamp: Lamport::MIN,
2950            split_offset: 0,
2951        }
2952    }
2953
2954    fn add_summary(&mut self, summary: &Self) {
2955        *self = *summary;
2956    }
2957}
2958
2959#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
2960pub struct FullOffset(pub usize);
2961
2962impl ops::AddAssign<usize> for FullOffset {
2963    fn add_assign(&mut self, rhs: usize) {
2964        self.0 += rhs;
2965    }
2966}
2967
2968impl ops::Add<usize> for FullOffset {
2969    type Output = Self;
2970
2971    fn add(mut self, rhs: usize) -> Self::Output {
2972        self += rhs;
2973        self
2974    }
2975}
2976
2977impl ops::Sub for FullOffset {
2978    type Output = usize;
2979
2980    fn sub(self, rhs: Self) -> Self::Output {
2981        self.0 - rhs.0
2982    }
2983}
2984
2985impl sum_tree::Dimension<'_, FragmentSummary> for usize {
2986    fn zero(_: &Option<clock::Global>) -> Self {
2987        Default::default()
2988    }
2989
2990    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2991        *self += summary.text.visible;
2992    }
2993}
2994
2995impl sum_tree::Dimension<'_, FragmentSummary> for FullOffset {
2996    fn zero(_: &Option<clock::Global>) -> Self {
2997        Default::default()
2998    }
2999
3000    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
3001        self.0 += summary.text.visible + summary.text.deleted;
3002    }
3003}
3004
3005impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
3006    fn zero(_: &Option<clock::Global>) -> Self {
3007        Default::default()
3008    }
3009
3010    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
3011        *self = Some(&summary.max_id);
3012    }
3013}
3014
3015impl sum_tree::SeekTarget<'_, FragmentSummary, FragmentTextSummary> for usize {
3016    fn cmp(
3017        &self,
3018        cursor_location: &FragmentTextSummary,
3019        _: &Option<clock::Global>,
3020    ) -> cmp::Ordering {
3021        Ord::cmp(self, &cursor_location.visible)
3022    }
3023}
3024
3025#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3026enum VersionedFullOffset {
3027    Offset(FullOffset),
3028    Invalid,
3029}
3030
3031impl VersionedFullOffset {
3032    fn full_offset(&self) -> FullOffset {
3033        if let Self::Offset(position) = self {
3034            *position
3035        } else {
3036            panic!("invalid version")
3037        }
3038    }
3039}
3040
3041impl Default for VersionedFullOffset {
3042    fn default() -> Self {
3043        Self::Offset(Default::default())
3044    }
3045}
3046
3047impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
3048    fn zero(_cx: &Option<clock::Global>) -> Self {
3049        Default::default()
3050    }
3051
3052    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
3053        if let Self::Offset(offset) = self {
3054            let version = cx.as_ref().unwrap();
3055            if version.observed_all(&summary.max_insertion_version) {
3056                *offset += summary.text.visible + summary.text.deleted;
3057            } else if version.observed_any(&summary.min_insertion_version) {
3058                *self = Self::Invalid;
3059            }
3060        }
3061    }
3062}
3063
3064impl sum_tree::SeekTarget<'_, FragmentSummary, Self> for VersionedFullOffset {
3065    fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
3066        match (self, cursor_position) {
3067            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
3068            (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
3069            (Self::Invalid, _) => unreachable!(),
3070        }
3071    }
3072}
3073
3074impl Operation {
3075    fn replica_id(&self) -> ReplicaId {
3076        operation_queue::Operation::lamport_timestamp(self).replica_id
3077    }
3078
3079    pub fn timestamp(&self) -> clock::Lamport {
3080        match self {
3081            Operation::Edit(edit) => edit.timestamp,
3082            Operation::Undo(undo) => undo.timestamp,
3083        }
3084    }
3085
3086    pub fn as_edit(&self) -> Option<&EditOperation> {
3087        match self {
3088            Operation::Edit(edit) => Some(edit),
3089            _ => None,
3090        }
3091    }
3092
3093    pub fn is_edit(&self) -> bool {
3094        matches!(self, Operation::Edit { .. })
3095    }
3096}
3097
3098impl operation_queue::Operation for Operation {
3099    fn lamport_timestamp(&self) -> clock::Lamport {
3100        match self {
3101            Operation::Edit(edit) => edit.timestamp,
3102            Operation::Undo(undo) => undo.timestamp,
3103        }
3104    }
3105}
3106
3107pub trait ToOffset {
3108    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize;
3109    /// Turns this point into the next offset in the buffer that comes after this, respecting utf8 boundaries.
3110    fn to_next_offset(&self, snapshot: &BufferSnapshot) -> usize {
3111        snapshot
3112            .visible_text
3113            .ceil_char_boundary(self.to_offset(snapshot) + 1)
3114    }
3115    /// Turns this point into the previous offset in the buffer that comes before this, respecting utf8 boundaries.
3116    fn to_previous_offset(&self, snapshot: &BufferSnapshot) -> usize {
3117        snapshot
3118            .visible_text
3119            .floor_char_boundary(self.to_offset(snapshot).saturating_sub(1))
3120    }
3121}
3122
3123impl ToOffset for Point {
3124    #[inline]
3125    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
3126        snapshot.point_to_offset(*self)
3127    }
3128}
3129
3130impl ToOffset for usize {
3131    #[track_caller]
3132    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
3133        if !snapshot
3134            .as_rope()
3135            .assert_char_boundary::<{ cfg!(debug_assertions) }>(*self)
3136        {
3137            snapshot.as_rope().floor_char_boundary(*self)
3138        } else {
3139            *self
3140        }
3141    }
3142}
3143
3144impl ToOffset for Anchor {
3145    #[inline]
3146    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
3147        snapshot.summary_for_anchor(self)
3148    }
3149}
3150
3151impl<T: ToOffset> ToOffset for &T {
3152    #[inline]
3153    fn to_offset(&self, content: &BufferSnapshot) -> usize {
3154        (*self).to_offset(content)
3155    }
3156}
3157
3158impl ToOffset for PointUtf16 {
3159    #[inline]
3160    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
3161        snapshot.point_utf16_to_offset(*self)
3162    }
3163}
3164
3165impl ToOffset for Unclipped<PointUtf16> {
3166    #[inline]
3167    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
3168        snapshot.unclipped_point_utf16_to_offset(*self)
3169    }
3170}
3171
3172pub trait ToPoint {
3173    fn to_point(&self, snapshot: &BufferSnapshot) -> Point;
3174}
3175
3176impl ToPoint for Anchor {
3177    #[inline]
3178    fn to_point(&self, snapshot: &BufferSnapshot) -> Point {
3179        snapshot.summary_for_anchor(self)
3180    }
3181}
3182
3183impl ToPoint for usize {
3184    #[inline]
3185    fn to_point(&self, snapshot: &BufferSnapshot) -> Point {
3186        snapshot.offset_to_point(*self)
3187    }
3188}
3189
3190impl ToPoint for Point {
3191    #[inline]
3192    fn to_point(&self, _: &BufferSnapshot) -> Point {
3193        *self
3194    }
3195}
3196
3197impl ToPoint for Unclipped<PointUtf16> {
3198    #[inline]
3199    fn to_point(&self, snapshot: &BufferSnapshot) -> Point {
3200        snapshot.unclipped_point_utf16_to_point(*self)
3201    }
3202}
3203
3204pub trait ToPointUtf16 {
3205    fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16;
3206}
3207
3208impl ToPointUtf16 for Anchor {
3209    #[inline]
3210    fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
3211        snapshot.summary_for_anchor(self)
3212    }
3213}
3214
3215impl ToPointUtf16 for usize {
3216    #[inline]
3217    fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
3218        snapshot.offset_to_point_utf16(*self)
3219    }
3220}
3221
3222impl ToPointUtf16 for PointUtf16 {
3223    #[inline]
3224    fn to_point_utf16(&self, _: &BufferSnapshot) -> PointUtf16 {
3225        *self
3226    }
3227}
3228
3229impl ToPointUtf16 for Point {
3230    #[inline]
3231    fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
3232        snapshot.point_to_point_utf16(*self)
3233    }
3234}
3235
3236pub trait ToOffsetUtf16 {
3237    fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16;
3238}
3239
3240impl ToOffsetUtf16 for Anchor {
3241    #[inline]
3242    fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 {
3243        snapshot.summary_for_anchor(self)
3244    }
3245}
3246
3247impl ToOffsetUtf16 for usize {
3248    #[inline]
3249    fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 {
3250        snapshot.offset_to_offset_utf16(*self)
3251    }
3252}
3253
3254impl ToOffsetUtf16 for OffsetUtf16 {
3255    #[inline]
3256    fn to_offset_utf16(&self, _snapshot: &BufferSnapshot) -> OffsetUtf16 {
3257        *self
3258    }
3259}
3260
3261pub trait FromAnchor {
3262    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
3263}
3264
3265impl FromAnchor for Anchor {
3266    #[inline]
3267    fn from_anchor(anchor: &Anchor, _snapshot: &BufferSnapshot) -> Self {
3268        *anchor
3269    }
3270}
3271
3272impl FromAnchor for Point {
3273    #[inline]
3274    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
3275        snapshot.summary_for_anchor(anchor)
3276    }
3277}
3278
3279impl FromAnchor for PointUtf16 {
3280    #[inline]
3281    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
3282        snapshot.summary_for_anchor(anchor)
3283    }
3284}
3285
3286impl FromAnchor for usize {
3287    #[inline]
3288    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
3289        snapshot.summary_for_anchor(anchor)
3290    }
3291}
3292
3293#[derive(Clone, Copy, Debug, PartialEq)]
3294pub enum LineEnding {
3295    Unix,
3296    Windows,
3297}
3298
3299impl Default for LineEnding {
3300    fn default() -> Self {
3301        #[cfg(unix)]
3302        return Self::Unix;
3303
3304        #[cfg(not(unix))]
3305        return Self::Windows;
3306    }
3307}
3308
3309impl LineEnding {
3310    pub fn as_str(&self) -> &'static str {
3311        match self {
3312            LineEnding::Unix => "\n",
3313            LineEnding::Windows => "\r\n",
3314        }
3315    }
3316
3317    pub fn label(&self) -> &'static str {
3318        match self {
3319            LineEnding::Unix => "LF",
3320            LineEnding::Windows => "CRLF",
3321        }
3322    }
3323
3324    pub fn detect(text: &str) -> Self {
3325        let mut max_ix = cmp::min(text.len(), 1000);
3326        while !text.is_char_boundary(max_ix) {
3327            max_ix -= 1;
3328        }
3329
3330        if let Some(ix) = text[..max_ix].find(['\n']) {
3331            if ix > 0 && text.as_bytes()[ix - 1] == b'\r' {
3332                Self::Windows
3333            } else {
3334                Self::Unix
3335            }
3336        } else {
3337            Self::default()
3338        }
3339    }
3340
3341    pub fn normalize(text: &mut String) {
3342        if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(text, "\n") {
3343            *text = replaced;
3344        }
3345    }
3346
3347    pub fn normalize_arc(text: Arc<str>) -> Arc<str> {
3348        if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") {
3349            replaced.into()
3350        } else {
3351            text
3352        }
3353    }
3354
3355    pub fn normalize_cow(text: Cow<str>) -> Cow<str> {
3356        if let Cow::Owned(replaced) = LINE_SEPARATORS_REGEX.replace_all(&text, "\n") {
3357            replaced.into()
3358        } else {
3359            text
3360        }
3361    }
3362}
3363
3364pub fn chunks_with_line_ending(rope: &Rope, line_ending: LineEnding) -> impl Iterator<Item = &str> {
3365    rope.chunks().flat_map(move |chunk| {
3366        let mut newline = false;
3367        let end_with_newline = chunk.ends_with('\n').then_some(line_ending.as_str());
3368        chunk
3369            .lines()
3370            .flat_map(move |line| {
3371                let ending = if newline {
3372                    Some(line_ending.as_str())
3373                } else {
3374                    None
3375                };
3376                newline = true;
3377                ending.into_iter().chain([line])
3378            })
3379            .chain(end_with_newline)
3380    })
3381}
3382
3383#[cfg(debug_assertions)]
3384pub mod debug {
3385    use super::*;
3386    use parking_lot::Mutex;
3387    use std::any::TypeId;
3388    use std::hash::{Hash, Hasher};
3389
3390    static GLOBAL_DEBUG_RANGES: Mutex<Option<GlobalDebugRanges>> = Mutex::new(None);
3391
3392    pub struct GlobalDebugRanges {
3393        pub ranges: Vec<DebugRange>,
3394        key_to_occurrence_index: HashMap<Key, usize>,
3395        next_occurrence_index: usize,
3396    }
3397
3398    pub struct DebugRange {
3399        key: Key,
3400        pub ranges: Vec<Range<Anchor>>,
3401        pub value: Arc<str>,
3402        pub occurrence_index: usize,
3403    }
3404
3405    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
3406    struct Key {
3407        type_id: TypeId,
3408        hash: u64,
3409    }
3410
3411    impl GlobalDebugRanges {
3412        pub fn with_locked<R>(f: impl FnOnce(&mut Self) -> R) -> R {
3413            let mut state = GLOBAL_DEBUG_RANGES.lock();
3414            if state.is_none() {
3415                *state = Some(GlobalDebugRanges {
3416                    ranges: Vec::new(),
3417                    key_to_occurrence_index: HashMap::default(),
3418                    next_occurrence_index: 0,
3419                });
3420            }
3421            if let Some(global_debug_ranges) = state.as_mut() {
3422                f(global_debug_ranges)
3423            } else {
3424                unreachable!()
3425            }
3426        }
3427
3428        pub fn insert<K: Hash + 'static>(
3429            &mut self,
3430            key: &K,
3431            ranges: Vec<Range<Anchor>>,
3432            value: Arc<str>,
3433        ) {
3434            let occurrence_index = *self
3435                .key_to_occurrence_index
3436                .entry(Key::new(key))
3437                .or_insert_with(|| {
3438                    let occurrence_index = self.next_occurrence_index;
3439                    self.next_occurrence_index += 1;
3440                    occurrence_index
3441                });
3442            let key = Key::new(key);
3443            let existing = self
3444                .ranges
3445                .iter()
3446                .enumerate()
3447                .rfind(|(_, existing)| existing.key == key);
3448            if let Some((existing_ix, _)) = existing {
3449                self.ranges.remove(existing_ix);
3450            }
3451            self.ranges.push(DebugRange {
3452                ranges,
3453                key,
3454                value,
3455                occurrence_index,
3456            });
3457        }
3458
3459        pub fn remove<K: Hash + 'static>(&mut self, key: &K) {
3460            self.remove_impl(&Key::new(key));
3461        }
3462
3463        fn remove_impl(&mut self, key: &Key) {
3464            let existing = self
3465                .ranges
3466                .iter()
3467                .enumerate()
3468                .rfind(|(_, existing)| &existing.key == key);
3469            if let Some((existing_ix, _)) = existing {
3470                self.ranges.remove(existing_ix);
3471            }
3472        }
3473
3474        pub fn remove_all_with_key_type<K: 'static>(&mut self) {
3475            self.ranges
3476                .retain(|item| item.key.type_id != TypeId::of::<K>());
3477        }
3478    }
3479
3480    impl Key {
3481        fn new<K: Hash + 'static>(key: &K) -> Self {
3482            let type_id = TypeId::of::<K>();
3483            let mut hasher = collections::FxHasher::default();
3484            key.hash(&mut hasher);
3485            Key {
3486                type_id,
3487                hash: hasher.finish(),
3488            }
3489        }
3490    }
3491
3492    pub trait ToDebugRanges {
3493        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>>;
3494    }
3495
3496    impl<T: ToOffset> ToDebugRanges for T {
3497        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3498            [self.to_offset(snapshot)].to_debug_ranges(snapshot)
3499        }
3500    }
3501
3502    impl<T: ToOffset + Clone> ToDebugRanges for Range<T> {
3503        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3504            [self.clone()].to_debug_ranges(snapshot)
3505        }
3506    }
3507
3508    impl<T: ToOffset> ToDebugRanges for Vec<T> {
3509        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3510            self.as_slice().to_debug_ranges(snapshot)
3511        }
3512    }
3513
3514    impl<T: ToOffset> ToDebugRanges for Vec<Range<T>> {
3515        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3516            self.as_slice().to_debug_ranges(snapshot)
3517        }
3518    }
3519
3520    impl<T: ToOffset> ToDebugRanges for [T] {
3521        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3522            self.iter()
3523                .map(|item| {
3524                    let offset = item.to_offset(snapshot);
3525                    offset..offset
3526                })
3527                .collect()
3528        }
3529    }
3530
3531    impl<T: ToOffset> ToDebugRanges for [Range<T>] {
3532        fn to_debug_ranges(&self, snapshot: &BufferSnapshot) -> Vec<Range<usize>> {
3533            self.iter()
3534                .map(|range| range.start.to_offset(snapshot)..range.end.to_offset(snapshot))
3535                .collect()
3536        }
3537    }
3538}