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