text.rs

   1mod anchor;
   2pub mod locator;
   3#[cfg(any(test, feature = "test-support"))]
   4pub mod network;
   5mod offset_utf16;
   6pub mod operation_queue;
   7mod patch;
   8mod point;
   9mod point_utf16;
  10#[cfg(any(test, feature = "test-support"))]
  11pub mod random_char_iter;
  12pub mod rope;
  13mod selection;
  14pub mod subscription;
  15#[cfg(test)]
  16mod tests;
  17
  18pub use anchor::*;
  19use anyhow::Result;
  20use clock::ReplicaId;
  21use collections::{HashMap, HashSet};
  22use lazy_static::lazy_static;
  23use locator::Locator;
  24pub use offset_utf16::*;
  25use operation_queue::OperationQueue;
  26pub use patch::Patch;
  27pub use point::*;
  28pub use point_utf16::*;
  29use postage::{barrier, oneshot, prelude::*};
  30#[cfg(any(test, feature = "test-support"))]
  31pub use random_char_iter::*;
  32use regex::Regex;
  33use rope::TextDimension;
  34pub use rope::{Chunks, Rope, TextSummary};
  35pub use selection::*;
  36use std::{
  37    borrow::Cow,
  38    cmp::{self, Ordering, Reverse},
  39    future::Future,
  40    iter::Iterator,
  41    ops::{self, Deref, Range, Sub},
  42    str,
  43    sync::Arc,
  44    time::{Duration, Instant},
  45};
  46pub use subscription::*;
  47pub use sum_tree::Bias;
  48use sum_tree::{FilterCursor, SumTree, TreeMap};
  49
  50lazy_static! {
  51    static ref CARRIAGE_RETURNS_REGEX: Regex = Regex::new("\r\n|\r").unwrap();
  52}
  53
  54pub type TransactionId = clock::Local;
  55
  56pub struct Buffer {
  57    snapshot: BufferSnapshot,
  58    history: History,
  59    deferred_ops: OperationQueue<Operation>,
  60    deferred_replicas: HashSet<ReplicaId>,
  61    replica_id: ReplicaId,
  62    local_clock: clock::Local,
  63    pub lamport_clock: clock::Lamport,
  64    subscriptions: Topic,
  65    edit_id_resolvers: HashMap<clock::Local, Vec<oneshot::Sender<()>>>,
  66    version_barriers: Vec<(clock::Global, barrier::Sender)>,
  67}
  68
  69#[derive(Clone, Debug)]
  70pub struct BufferSnapshot {
  71    replica_id: ReplicaId,
  72    remote_id: u64,
  73    visible_text: Rope,
  74    deleted_text: Rope,
  75    line_ending: LineEnding,
  76    undo_map: UndoMap,
  77    fragments: SumTree<Fragment>,
  78    insertions: SumTree<InsertionFragment>,
  79    pub version: clock::Global,
  80}
  81
  82#[derive(Clone, Debug)]
  83pub struct HistoryEntry {
  84    transaction: Transaction,
  85    first_edit_at: Instant,
  86    last_edit_at: Instant,
  87    suppress_grouping: bool,
  88}
  89
  90#[derive(Clone, Debug)]
  91pub struct Transaction {
  92    pub id: TransactionId,
  93    pub edit_ids: Vec<clock::Local>,
  94    pub start: clock::Global,
  95}
  96
  97#[derive(Clone, Copy, Debug, PartialEq)]
  98pub enum LineEnding {
  99    Unix,
 100    Windows,
 101}
 102
 103impl HistoryEntry {
 104    pub fn transaction_id(&self) -> TransactionId {
 105        self.transaction.id
 106    }
 107}
 108
 109struct History {
 110    // TODO: Turn this into a String or Rope, maybe.
 111    base_text: Arc<str>,
 112    operations: TreeMap<clock::Local, Operation>,
 113    insertion_slices: HashMap<clock::Local, Vec<InsertionSlice>>,
 114    undo_stack: Vec<HistoryEntry>,
 115    redo_stack: Vec<HistoryEntry>,
 116    transaction_depth: usize,
 117    group_interval: Duration,
 118}
 119
 120#[derive(Clone, Debug)]
 121struct InsertionSlice {
 122    insertion_id: clock::Local,
 123    range: Range<usize>,
 124}
 125
 126impl History {
 127    pub fn new(base_text: Arc<str>) -> Self {
 128        Self {
 129            base_text,
 130            operations: Default::default(),
 131            insertion_slices: Default::default(),
 132            undo_stack: Vec::new(),
 133            redo_stack: Vec::new(),
 134            transaction_depth: 0,
 135            group_interval: Duration::from_millis(300),
 136        }
 137    }
 138
 139    fn push(&mut self, op: Operation) {
 140        self.operations.insert(op.local_timestamp(), op);
 141    }
 142
 143    fn start_transaction(
 144        &mut self,
 145        start: clock::Global,
 146        now: Instant,
 147        local_clock: &mut clock::Local,
 148    ) -> Option<TransactionId> {
 149        self.transaction_depth += 1;
 150        if self.transaction_depth == 1 {
 151            let id = local_clock.tick();
 152            self.undo_stack.push(HistoryEntry {
 153                transaction: Transaction {
 154                    id,
 155                    start,
 156                    edit_ids: Default::default(),
 157                },
 158                first_edit_at: now,
 159                last_edit_at: now,
 160                suppress_grouping: false,
 161            });
 162            Some(id)
 163        } else {
 164            None
 165        }
 166    }
 167
 168    fn end_transaction(&mut self, now: Instant) -> Option<&HistoryEntry> {
 169        assert_ne!(self.transaction_depth, 0);
 170        self.transaction_depth -= 1;
 171        if self.transaction_depth == 0 {
 172            if self
 173                .undo_stack
 174                .last()
 175                .unwrap()
 176                .transaction
 177                .edit_ids
 178                .is_empty()
 179            {
 180                self.undo_stack.pop();
 181                None
 182            } else {
 183                self.redo_stack.clear();
 184                let entry = self.undo_stack.last_mut().unwrap();
 185                entry.last_edit_at = now;
 186                Some(entry)
 187            }
 188        } else {
 189            None
 190        }
 191    }
 192
 193    fn group(&mut self) -> Option<TransactionId> {
 194        let mut count = 0;
 195        let mut entries = self.undo_stack.iter();
 196        if let Some(mut entry) = entries.next_back() {
 197            while let Some(prev_entry) = entries.next_back() {
 198                if !prev_entry.suppress_grouping
 199                    && entry.first_edit_at - prev_entry.last_edit_at <= self.group_interval
 200                {
 201                    entry = prev_entry;
 202                    count += 1;
 203                } else {
 204                    break;
 205                }
 206            }
 207        }
 208        self.group_trailing(count)
 209    }
 210
 211    fn group_until(&mut self, transaction_id: TransactionId) {
 212        let mut count = 0;
 213        for entry in self.undo_stack.iter().rev() {
 214            if entry.transaction_id() == transaction_id {
 215                self.group_trailing(count);
 216                break;
 217            } else if entry.suppress_grouping {
 218                break;
 219            } else {
 220                count += 1;
 221            }
 222        }
 223    }
 224
 225    fn group_trailing(&mut self, n: usize) -> Option<TransactionId> {
 226        let new_len = self.undo_stack.len() - n;
 227        let (entries_to_keep, entries_to_merge) = self.undo_stack.split_at_mut(new_len);
 228        if let Some(last_entry) = entries_to_keep.last_mut() {
 229            for entry in &*entries_to_merge {
 230                for edit_id in &entry.transaction.edit_ids {
 231                    last_entry.transaction.edit_ids.push(*edit_id);
 232                }
 233            }
 234
 235            if let Some(entry) = entries_to_merge.last_mut() {
 236                last_entry.last_edit_at = entry.last_edit_at;
 237            }
 238        }
 239
 240        self.undo_stack.truncate(new_len);
 241        self.undo_stack.last().map(|e| e.transaction.id)
 242    }
 243
 244    fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
 245        self.undo_stack.last_mut().map(|entry| {
 246            entry.suppress_grouping = true;
 247            &entry.transaction
 248        })
 249    }
 250
 251    fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
 252        assert_eq!(self.transaction_depth, 0);
 253        self.undo_stack.push(HistoryEntry {
 254            transaction,
 255            first_edit_at: now,
 256            last_edit_at: now,
 257            suppress_grouping: false,
 258        });
 259        self.redo_stack.clear();
 260    }
 261
 262    fn push_undo(&mut self, op_id: clock::Local) {
 263        assert_ne!(self.transaction_depth, 0);
 264        if let Some(Operation::Edit(_)) = self.operations.get(&op_id) {
 265            let last_transaction = self.undo_stack.last_mut().unwrap();
 266            last_transaction.transaction.edit_ids.push(op_id);
 267        }
 268    }
 269
 270    fn pop_undo(&mut self) -> Option<&HistoryEntry> {
 271        assert_eq!(self.transaction_depth, 0);
 272        if let Some(entry) = self.undo_stack.pop() {
 273            self.redo_stack.push(entry);
 274            self.redo_stack.last()
 275        } else {
 276            None
 277        }
 278    }
 279
 280    fn remove_from_undo(&mut self, transaction_id: TransactionId) -> &[HistoryEntry] {
 281        assert_eq!(self.transaction_depth, 0);
 282
 283        let redo_stack_start_len = self.redo_stack.len();
 284        if let Some(entry_ix) = self
 285            .undo_stack
 286            .iter()
 287            .rposition(|entry| entry.transaction.id == transaction_id)
 288        {
 289            self.redo_stack
 290                .extend(self.undo_stack.drain(entry_ix..).rev());
 291        }
 292        &self.redo_stack[redo_stack_start_len..]
 293    }
 294
 295    fn forget(&mut self, transaction_id: TransactionId) {
 296        assert_eq!(self.transaction_depth, 0);
 297        if let Some(entry_ix) = self
 298            .undo_stack
 299            .iter()
 300            .rposition(|entry| entry.transaction.id == transaction_id)
 301        {
 302            self.undo_stack.remove(entry_ix);
 303        } else if let Some(entry_ix) = self
 304            .redo_stack
 305            .iter()
 306            .rposition(|entry| entry.transaction.id == transaction_id)
 307        {
 308            self.undo_stack.remove(entry_ix);
 309        }
 310    }
 311
 312    fn pop_redo(&mut self) -> Option<&HistoryEntry> {
 313        assert_eq!(self.transaction_depth, 0);
 314        if let Some(entry) = self.redo_stack.pop() {
 315            self.undo_stack.push(entry);
 316            self.undo_stack.last()
 317        } else {
 318            None
 319        }
 320    }
 321
 322    fn remove_from_redo(&mut self, transaction_id: TransactionId) -> &[HistoryEntry] {
 323        assert_eq!(self.transaction_depth, 0);
 324
 325        let undo_stack_start_len = self.undo_stack.len();
 326        if let Some(entry_ix) = self
 327            .redo_stack
 328            .iter()
 329            .rposition(|entry| entry.transaction.id == transaction_id)
 330        {
 331            self.undo_stack
 332                .extend(self.redo_stack.drain(entry_ix..).rev());
 333        }
 334        &self.undo_stack[undo_stack_start_len..]
 335    }
 336}
 337
 338#[derive(Clone, Default, Debug)]
 339struct UndoMap(HashMap<clock::Local, Vec<(clock::Local, u32)>>);
 340
 341impl UndoMap {
 342    fn insert(&mut self, undo: &UndoOperation) {
 343        for (edit_id, count) in &undo.counts {
 344            self.0.entry(*edit_id).or_default().push((undo.id, *count));
 345        }
 346    }
 347
 348    fn is_undone(&self, edit_id: clock::Local) -> bool {
 349        self.undo_count(edit_id) % 2 == 1
 350    }
 351
 352    fn was_undone(&self, edit_id: clock::Local, version: &clock::Global) -> bool {
 353        let undo_count = self
 354            .0
 355            .get(&edit_id)
 356            .unwrap_or(&Vec::new())
 357            .iter()
 358            .filter(|(undo_id, _)| version.observed(*undo_id))
 359            .map(|(_, undo_count)| *undo_count)
 360            .max()
 361            .unwrap_or(0);
 362        undo_count % 2 == 1
 363    }
 364
 365    fn undo_count(&self, edit_id: clock::Local) -> u32 {
 366        self.0
 367            .get(&edit_id)
 368            .unwrap_or(&Vec::new())
 369            .iter()
 370            .map(|(_, undo_count)| *undo_count)
 371            .max()
 372            .unwrap_or(0)
 373    }
 374}
 375
 376struct Edits<'a, D: TextDimension, F: FnMut(&FragmentSummary) -> bool> {
 377    visible_cursor: rope::Cursor<'a>,
 378    deleted_cursor: rope::Cursor<'a>,
 379    fragments_cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
 380    undos: &'a UndoMap,
 381    since: &'a clock::Global,
 382    old_end: D,
 383    new_end: D,
 384    range: Range<(&'a Locator, usize)>,
 385    buffer_id: u64,
 386}
 387
 388#[derive(Clone, Debug, Default, Eq, PartialEq)]
 389pub struct Edit<D> {
 390    pub old: Range<D>,
 391    pub new: Range<D>,
 392}
 393
 394impl<D> Edit<D>
 395where
 396    D: Sub<D, Output = D> + PartialEq + Copy,
 397{
 398    pub fn old_len(&self) -> D {
 399        self.old.end - self.old.start
 400    }
 401
 402    pub fn new_len(&self) -> D {
 403        self.new.end - self.new.start
 404    }
 405
 406    pub fn is_empty(&self) -> bool {
 407        self.old.start == self.old.end && self.new.start == self.new.end
 408    }
 409}
 410
 411impl<D1, D2> Edit<(D1, D2)> {
 412    pub fn flatten(self) -> (Edit<D1>, Edit<D2>) {
 413        (
 414            Edit {
 415                old: self.old.start.0..self.old.end.0,
 416                new: self.new.start.0..self.new.end.0,
 417            },
 418            Edit {
 419                old: self.old.start.1..self.old.end.1,
 420                new: self.new.start.1..self.new.end.1,
 421            },
 422        )
 423    }
 424}
 425
 426#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
 427pub struct InsertionTimestamp {
 428    pub replica_id: ReplicaId,
 429    pub local: clock::Seq,
 430    pub lamport: clock::Seq,
 431}
 432
 433impl InsertionTimestamp {
 434    pub fn local(&self) -> clock::Local {
 435        clock::Local {
 436            replica_id: self.replica_id,
 437            value: self.local,
 438        }
 439    }
 440
 441    pub fn lamport(&self) -> clock::Lamport {
 442        clock::Lamport {
 443            replica_id: self.replica_id,
 444            value: self.lamport,
 445        }
 446    }
 447}
 448
 449#[derive(Eq, PartialEq, Clone, Debug)]
 450pub struct Fragment {
 451    pub id: Locator,
 452    pub insertion_timestamp: InsertionTimestamp,
 453    pub insertion_offset: usize,
 454    pub len: usize,
 455    pub visible: bool,
 456    pub deletions: HashSet<clock::Local>,
 457    pub max_undos: clock::Global,
 458}
 459
 460#[derive(Eq, PartialEq, Clone, Debug)]
 461pub struct FragmentSummary {
 462    text: FragmentTextSummary,
 463    max_id: Locator,
 464    max_version: clock::Global,
 465    min_insertion_version: clock::Global,
 466    max_insertion_version: clock::Global,
 467}
 468
 469#[derive(Copy, Default, Clone, Debug, PartialEq, Eq)]
 470struct FragmentTextSummary {
 471    visible: usize,
 472    deleted: usize,
 473}
 474
 475impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentTextSummary {
 476    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
 477        self.visible += summary.text.visible;
 478        self.deleted += summary.text.deleted;
 479    }
 480}
 481
 482#[derive(Eq, PartialEq, Clone, Debug)]
 483struct InsertionFragment {
 484    timestamp: clock::Local,
 485    split_offset: usize,
 486    fragment_id: Locator,
 487}
 488
 489#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
 490struct InsertionFragmentKey {
 491    timestamp: clock::Local,
 492    split_offset: usize,
 493}
 494
 495#[derive(Clone, Debug, Eq, PartialEq)]
 496pub enum Operation {
 497    Edit(EditOperation),
 498    Undo {
 499        undo: UndoOperation,
 500        lamport_timestamp: clock::Lamport,
 501    },
 502}
 503
 504#[derive(Clone, Debug, Eq, PartialEq)]
 505pub struct EditOperation {
 506    pub timestamp: InsertionTimestamp,
 507    pub version: clock::Global,
 508    pub ranges: Vec<Range<FullOffset>>,
 509    pub new_text: Vec<Arc<str>>,
 510}
 511
 512#[derive(Clone, Debug, Eq, PartialEq)]
 513pub struct UndoOperation {
 514    pub id: clock::Local,
 515    pub counts: HashMap<clock::Local, u32>,
 516    pub version: clock::Global,
 517}
 518
 519impl Buffer {
 520    pub fn new(replica_id: u16, remote_id: u64, mut base_text: String) -> Buffer {
 521        let line_ending = LineEnding::detect(&base_text);
 522        LineEnding::normalize(&mut base_text);
 523
 524        let history = History::new(base_text.into());
 525        let mut fragments = SumTree::new();
 526        let mut insertions = SumTree::new();
 527
 528        let mut local_clock = clock::Local::new(replica_id);
 529        let mut lamport_clock = clock::Lamport::new(replica_id);
 530        let mut version = clock::Global::new();
 531
 532        let visible_text = Rope::from(history.base_text.as_ref());
 533        if !visible_text.is_empty() {
 534            let insertion_timestamp = InsertionTimestamp {
 535                replica_id: 0,
 536                local: 1,
 537                lamport: 1,
 538            };
 539            local_clock.observe(insertion_timestamp.local());
 540            lamport_clock.observe(insertion_timestamp.lamport());
 541            version.observe(insertion_timestamp.local());
 542            let fragment_id = Locator::between(&Locator::min(), &Locator::max());
 543            let fragment = Fragment {
 544                id: fragment_id,
 545                insertion_timestamp,
 546                insertion_offset: 0,
 547                len: visible_text.len(),
 548                visible: true,
 549                deletions: Default::default(),
 550                max_undos: Default::default(),
 551            };
 552            insertions.push(InsertionFragment::new(&fragment), &());
 553            fragments.push(fragment, &None);
 554        }
 555
 556        Buffer {
 557            snapshot: BufferSnapshot {
 558                replica_id,
 559                remote_id,
 560                visible_text,
 561                deleted_text: Rope::new(),
 562                line_ending,
 563                fragments,
 564                insertions,
 565                version,
 566                undo_map: Default::default(),
 567            },
 568            history,
 569            deferred_ops: OperationQueue::new(),
 570            deferred_replicas: HashSet::default(),
 571            replica_id,
 572            local_clock,
 573            lamport_clock,
 574            subscriptions: Default::default(),
 575            edit_id_resolvers: Default::default(),
 576            version_barriers: Default::default(),
 577        }
 578    }
 579
 580    pub fn version(&self) -> clock::Global {
 581        self.version.clone()
 582    }
 583
 584    pub fn snapshot(&self) -> BufferSnapshot {
 585        self.snapshot.clone()
 586    }
 587
 588    pub fn replica_id(&self) -> ReplicaId {
 589        self.local_clock.replica_id
 590    }
 591
 592    pub fn remote_id(&self) -> u64 {
 593        self.remote_id
 594    }
 595
 596    pub fn deferred_ops_len(&self) -> usize {
 597        self.deferred_ops.len()
 598    }
 599
 600    pub fn transaction_group_interval(&self) -> Duration {
 601        self.history.group_interval
 602    }
 603
 604    pub fn edit<R, I, S, T>(&mut self, edits: R) -> Operation
 605    where
 606        R: IntoIterator<IntoIter = I>,
 607        I: ExactSizeIterator<Item = (Range<S>, T)>,
 608        S: ToOffset,
 609        T: Into<Arc<str>>,
 610    {
 611        let edits = edits
 612            .into_iter()
 613            .map(|(range, new_text)| (range, new_text.into()));
 614
 615        self.start_transaction();
 616        let timestamp = InsertionTimestamp {
 617            replica_id: self.replica_id,
 618            local: self.local_clock.tick().value,
 619            lamport: self.lamport_clock.tick().value,
 620        };
 621        let operation = Operation::Edit(self.apply_local_edit(edits, timestamp));
 622
 623        self.history.push(operation.clone());
 624        self.history.push_undo(operation.local_timestamp());
 625        self.snapshot.version.observe(operation.local_timestamp());
 626        self.end_transaction();
 627        operation
 628    }
 629
 630    fn apply_local_edit<S: ToOffset, T: Into<Arc<str>>>(
 631        &mut self,
 632        edits: impl ExactSizeIterator<Item = (Range<S>, T)>,
 633        timestamp: InsertionTimestamp,
 634    ) -> EditOperation {
 635        let mut edits_patch = Patch::default();
 636        let mut edit_op = EditOperation {
 637            timestamp,
 638            version: self.version(),
 639            ranges: Vec::with_capacity(edits.len()),
 640            new_text: Vec::with_capacity(edits.len()),
 641        };
 642        let mut new_insertions = Vec::new();
 643        let mut insertion_offset = 0;
 644        let mut insertion_slices = Vec::new();
 645
 646        let mut edits = edits
 647            .map(|(range, new_text)| (range.to_offset(&*self), new_text))
 648            .peekable();
 649
 650        let mut new_ropes =
 651            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
 652        let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>();
 653        let mut new_fragments =
 654            old_fragments.slice(&edits.peek().unwrap().0.start, Bias::Right, &None);
 655        new_ropes.push_tree(new_fragments.summary().text);
 656
 657        let mut fragment_start = old_fragments.start().visible;
 658        for (range, new_text) in edits {
 659            let new_text = LineEnding::normalize_arc(new_text.into());
 660            let fragment_end = old_fragments.end(&None).visible;
 661
 662            // If the current fragment ends before this range, then jump ahead to the first fragment
 663            // that extends past the start of this range, reusing any intervening fragments.
 664            if fragment_end < range.start {
 665                // If the current fragment has been partially consumed, then consume the rest of it
 666                // and advance to the next fragment before slicing.
 667                if fragment_start > old_fragments.start().visible {
 668                    if fragment_end > fragment_start {
 669                        let mut suffix = old_fragments.item().unwrap().clone();
 670                        suffix.len = fragment_end - fragment_start;
 671                        suffix.insertion_offset += fragment_start - old_fragments.start().visible;
 672                        new_insertions.push(InsertionFragment::insert_new(&suffix));
 673                        new_ropes.push_fragment(&suffix, suffix.visible);
 674                        new_fragments.push(suffix, &None);
 675                    }
 676                    old_fragments.next(&None);
 677                }
 678
 679                let slice = old_fragments.slice(&range.start, Bias::Right, &None);
 680                new_ropes.push_tree(slice.summary().text);
 681                new_fragments.push_tree(slice, &None);
 682                fragment_start = old_fragments.start().visible;
 683            }
 684
 685            let full_range_start = FullOffset(range.start + old_fragments.start().deleted);
 686
 687            // Preserve any portion of the current fragment that precedes this range.
 688            if fragment_start < range.start {
 689                let mut prefix = old_fragments.item().unwrap().clone();
 690                prefix.len = range.start - fragment_start;
 691                prefix.insertion_offset += fragment_start - old_fragments.start().visible;
 692                prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
 693                new_insertions.push(InsertionFragment::insert_new(&prefix));
 694                new_ropes.push_fragment(&prefix, prefix.visible);
 695                new_fragments.push(prefix, &None);
 696                fragment_start = range.start;
 697            }
 698
 699            // Insert the new text before any existing fragments within the range.
 700            if !new_text.is_empty() {
 701                let new_start = new_fragments.summary().text.visible;
 702
 703                let fragment = Fragment {
 704                    id: Locator::between(
 705                        &new_fragments.summary().max_id,
 706                        old_fragments
 707                            .item()
 708                            .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
 709                    ),
 710                    insertion_timestamp: timestamp,
 711                    insertion_offset,
 712                    len: new_text.len(),
 713                    deletions: Default::default(),
 714                    max_undos: Default::default(),
 715                    visible: true,
 716                };
 717                edits_patch.push(Edit {
 718                    old: fragment_start..fragment_start,
 719                    new: new_start..new_start + new_text.len(),
 720                });
 721                insertion_slices.push(fragment.insertion_slice());
 722                new_insertions.push(InsertionFragment::insert_new(&fragment));
 723                new_ropes.push_str(new_text.as_ref());
 724                new_fragments.push(fragment, &None);
 725                insertion_offset += new_text.len();
 726            }
 727
 728            // Advance through every fragment that intersects this range, marking the intersecting
 729            // portions as deleted.
 730            while fragment_start < range.end {
 731                let fragment = old_fragments.item().unwrap();
 732                let fragment_end = old_fragments.end(&None).visible;
 733                let mut intersection = fragment.clone();
 734                let intersection_end = cmp::min(range.end, fragment_end);
 735                if fragment.visible {
 736                    intersection.len = intersection_end - fragment_start;
 737                    intersection.insertion_offset += fragment_start - old_fragments.start().visible;
 738                    intersection.id =
 739                        Locator::between(&new_fragments.summary().max_id, &intersection.id);
 740                    intersection.deletions.insert(timestamp.local());
 741                    intersection.visible = false;
 742                }
 743                if intersection.len > 0 {
 744                    if fragment.visible && !intersection.visible {
 745                        let new_start = new_fragments.summary().text.visible;
 746                        edits_patch.push(Edit {
 747                            old: fragment_start..intersection_end,
 748                            new: new_start..new_start,
 749                        });
 750                        insertion_slices.push(intersection.insertion_slice());
 751                    }
 752                    new_insertions.push(InsertionFragment::insert_new(&intersection));
 753                    new_ropes.push_fragment(&intersection, fragment.visible);
 754                    new_fragments.push(intersection, &None);
 755                    fragment_start = intersection_end;
 756                }
 757                if fragment_end <= range.end {
 758                    old_fragments.next(&None);
 759                }
 760            }
 761
 762            let full_range_end = FullOffset(range.end + old_fragments.start().deleted);
 763            edit_op.ranges.push(full_range_start..full_range_end);
 764            edit_op.new_text.push(new_text);
 765        }
 766
 767        // If the current fragment has been partially consumed, then consume the rest of it
 768        // and advance to the next fragment before slicing.
 769        if fragment_start > old_fragments.start().visible {
 770            let fragment_end = old_fragments.end(&None).visible;
 771            if fragment_end > fragment_start {
 772                let mut suffix = old_fragments.item().unwrap().clone();
 773                suffix.len = fragment_end - fragment_start;
 774                suffix.insertion_offset += fragment_start - old_fragments.start().visible;
 775                new_insertions.push(InsertionFragment::insert_new(&suffix));
 776                new_ropes.push_fragment(&suffix, suffix.visible);
 777                new_fragments.push(suffix, &None);
 778            }
 779            old_fragments.next(&None);
 780        }
 781
 782        let suffix = old_fragments.suffix(&None);
 783        new_ropes.push_tree(suffix.summary().text);
 784        new_fragments.push_tree(suffix, &None);
 785        let (visible_text, deleted_text) = new_ropes.finish();
 786        drop(old_fragments);
 787
 788        self.snapshot.fragments = new_fragments;
 789        self.snapshot.insertions.edit(new_insertions, &());
 790        self.snapshot.visible_text = visible_text;
 791        self.snapshot.deleted_text = deleted_text;
 792        self.subscriptions.publish_mut(&edits_patch);
 793        self.history
 794            .insertion_slices
 795            .insert(timestamp.local(), insertion_slices);
 796        edit_op
 797    }
 798
 799    pub fn set_line_ending(&mut self, line_ending: LineEnding) {
 800        self.snapshot.line_ending = line_ending;
 801    }
 802
 803    pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I) -> Result<()> {
 804        let mut deferred_ops = Vec::new();
 805        for op in ops {
 806            self.history.push(op.clone());
 807            if self.can_apply_op(&op) {
 808                self.apply_op(op)?;
 809            } else {
 810                self.deferred_replicas.insert(op.replica_id());
 811                deferred_ops.push(op);
 812            }
 813        }
 814        self.deferred_ops.insert(deferred_ops);
 815        self.flush_deferred_ops()?;
 816        Ok(())
 817    }
 818
 819    fn apply_op(&mut self, op: Operation) -> Result<()> {
 820        match op {
 821            Operation::Edit(edit) => {
 822                if !self.version.observed(edit.timestamp.local()) {
 823                    self.apply_remote_edit(
 824                        &edit.version,
 825                        &edit.ranges,
 826                        &edit.new_text,
 827                        edit.timestamp,
 828                    );
 829                    self.snapshot.version.observe(edit.timestamp.local());
 830                    self.local_clock.observe(edit.timestamp.local());
 831                    self.lamport_clock.observe(edit.timestamp.lamport());
 832                    self.resolve_edit(edit.timestamp.local());
 833                }
 834            }
 835            Operation::Undo {
 836                undo,
 837                lamport_timestamp,
 838            } => {
 839                if !self.version.observed(undo.id) {
 840                    self.apply_undo(&undo)?;
 841                    self.snapshot.version.observe(undo.id);
 842                    self.local_clock.observe(undo.id);
 843                    self.lamport_clock.observe(lamport_timestamp);
 844                }
 845            }
 846        }
 847        self.version_barriers
 848            .retain(|(version, _)| !self.snapshot.version().observed_all(version));
 849        Ok(())
 850    }
 851
 852    fn apply_remote_edit(
 853        &mut self,
 854        version: &clock::Global,
 855        ranges: &[Range<FullOffset>],
 856        new_text: &[Arc<str>],
 857        timestamp: InsertionTimestamp,
 858    ) {
 859        if ranges.is_empty() {
 860            return;
 861        }
 862
 863        let edits = ranges.iter().zip(new_text.iter());
 864        let mut edits_patch = Patch::default();
 865        let mut insertion_slices = Vec::new();
 866        let cx = Some(version.clone());
 867        let mut new_insertions = Vec::new();
 868        let mut insertion_offset = 0;
 869        let mut new_ropes =
 870            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
 871        let mut old_fragments = self.fragments.cursor::<(VersionedFullOffset, usize)>();
 872        let mut new_fragments = old_fragments.slice(
 873            &VersionedFullOffset::Offset(ranges[0].start),
 874            Bias::Left,
 875            &cx,
 876        );
 877        new_ropes.push_tree(new_fragments.summary().text);
 878
 879        let mut fragment_start = old_fragments.start().0.full_offset();
 880        for (range, new_text) in edits {
 881            let fragment_end = old_fragments.end(&cx).0.full_offset();
 882
 883            // If the current fragment ends before this range, then jump ahead to the first fragment
 884            // that extends past the start of this range, reusing any intervening fragments.
 885            if fragment_end < range.start {
 886                // If the current fragment has been partially consumed, then consume the rest of it
 887                // and advance to the next fragment before slicing.
 888                if fragment_start > old_fragments.start().0.full_offset() {
 889                    if fragment_end > fragment_start {
 890                        let mut suffix = old_fragments.item().unwrap().clone();
 891                        suffix.len = fragment_end.0 - fragment_start.0;
 892                        suffix.insertion_offset +=
 893                            fragment_start - old_fragments.start().0.full_offset();
 894                        new_insertions.push(InsertionFragment::insert_new(&suffix));
 895                        new_ropes.push_fragment(&suffix, suffix.visible);
 896                        new_fragments.push(suffix, &None);
 897                    }
 898                    old_fragments.next(&cx);
 899                }
 900
 901                let slice =
 902                    old_fragments.slice(&VersionedFullOffset::Offset(range.start), Bias::Left, &cx);
 903                new_ropes.push_tree(slice.summary().text);
 904                new_fragments.push_tree(slice, &None);
 905                fragment_start = old_fragments.start().0.full_offset();
 906            }
 907
 908            // If we are at the end of a non-concurrent fragment, advance to the next one.
 909            let fragment_end = old_fragments.end(&cx).0.full_offset();
 910            if fragment_end == range.start && fragment_end > fragment_start {
 911                let mut fragment = old_fragments.item().unwrap().clone();
 912                fragment.len = fragment_end.0 - fragment_start.0;
 913                fragment.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
 914                new_insertions.push(InsertionFragment::insert_new(&fragment));
 915                new_ropes.push_fragment(&fragment, fragment.visible);
 916                new_fragments.push(fragment, &None);
 917                old_fragments.next(&cx);
 918                fragment_start = old_fragments.start().0.full_offset();
 919            }
 920
 921            // Skip over insertions that are concurrent to this edit, but have a lower lamport
 922            // timestamp.
 923            while let Some(fragment) = old_fragments.item() {
 924                if fragment_start == range.start
 925                    && fragment.insertion_timestamp.lamport() > timestamp.lamport()
 926                {
 927                    new_ropes.push_fragment(fragment, fragment.visible);
 928                    new_fragments.push(fragment.clone(), &None);
 929                    old_fragments.next(&cx);
 930                    debug_assert_eq!(fragment_start, range.start);
 931                } else {
 932                    break;
 933                }
 934            }
 935            debug_assert!(fragment_start <= range.start);
 936
 937            // Preserve any portion of the current fragment that precedes this range.
 938            if fragment_start < range.start {
 939                let mut prefix = old_fragments.item().unwrap().clone();
 940                prefix.len = range.start.0 - fragment_start.0;
 941                prefix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
 942                prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
 943                new_insertions.push(InsertionFragment::insert_new(&prefix));
 944                fragment_start = range.start;
 945                new_ropes.push_fragment(&prefix, prefix.visible);
 946                new_fragments.push(prefix, &None);
 947            }
 948
 949            // Insert the new text before any existing fragments within the range.
 950            if !new_text.is_empty() {
 951                let mut old_start = old_fragments.start().1;
 952                if old_fragments.item().map_or(false, |f| f.visible) {
 953                    old_start += fragment_start.0 - old_fragments.start().0.full_offset().0;
 954                }
 955                let new_start = new_fragments.summary().text.visible;
 956                let fragment = Fragment {
 957                    id: Locator::between(
 958                        &new_fragments.summary().max_id,
 959                        old_fragments
 960                            .item()
 961                            .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
 962                    ),
 963                    insertion_timestamp: timestamp,
 964                    insertion_offset,
 965                    len: new_text.len(),
 966                    deletions: Default::default(),
 967                    max_undos: Default::default(),
 968                    visible: true,
 969                };
 970                edits_patch.push(Edit {
 971                    old: old_start..old_start,
 972                    new: new_start..new_start + new_text.len(),
 973                });
 974                insertion_slices.push(fragment.insertion_slice());
 975                new_insertions.push(InsertionFragment::insert_new(&fragment));
 976                new_ropes.push_str(new_text);
 977                new_fragments.push(fragment, &None);
 978                insertion_offset += new_text.len();
 979            }
 980
 981            // Advance through every fragment that intersects this range, marking the intersecting
 982            // portions as deleted.
 983            while fragment_start < range.end {
 984                let fragment = old_fragments.item().unwrap();
 985                let fragment_end = old_fragments.end(&cx).0.full_offset();
 986                let mut intersection = fragment.clone();
 987                let intersection_end = cmp::min(range.end, fragment_end);
 988                if fragment.was_visible(version, &self.undo_map) {
 989                    intersection.len = intersection_end.0 - fragment_start.0;
 990                    intersection.insertion_offset +=
 991                        fragment_start - old_fragments.start().0.full_offset();
 992                    intersection.id =
 993                        Locator::between(&new_fragments.summary().max_id, &intersection.id);
 994                    intersection.deletions.insert(timestamp.local());
 995                    intersection.visible = false;
 996                    insertion_slices.push(intersection.insertion_slice());
 997                }
 998                if intersection.len > 0 {
 999                    if fragment.visible && !intersection.visible {
1000                        let old_start = old_fragments.start().1
1001                            + (fragment_start.0 - old_fragments.start().0.full_offset().0);
1002                        let new_start = new_fragments.summary().text.visible;
1003                        edits_patch.push(Edit {
1004                            old: old_start..old_start + intersection.len,
1005                            new: new_start..new_start,
1006                        });
1007                    }
1008                    new_insertions.push(InsertionFragment::insert_new(&intersection));
1009                    new_ropes.push_fragment(&intersection, fragment.visible);
1010                    new_fragments.push(intersection, &None);
1011                    fragment_start = intersection_end;
1012                }
1013                if fragment_end <= range.end {
1014                    old_fragments.next(&cx);
1015                }
1016            }
1017        }
1018
1019        // If the current fragment has been partially consumed, then consume the rest of it
1020        // and advance to the next fragment before slicing.
1021        if fragment_start > old_fragments.start().0.full_offset() {
1022            let fragment_end = old_fragments.end(&cx).0.full_offset();
1023            if fragment_end > fragment_start {
1024                let mut suffix = old_fragments.item().unwrap().clone();
1025                suffix.len = fragment_end.0 - fragment_start.0;
1026                suffix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
1027                new_insertions.push(InsertionFragment::insert_new(&suffix));
1028                new_ropes.push_fragment(&suffix, suffix.visible);
1029                new_fragments.push(suffix, &None);
1030            }
1031            old_fragments.next(&cx);
1032        }
1033
1034        let suffix = old_fragments.suffix(&cx);
1035        new_ropes.push_tree(suffix.summary().text);
1036        new_fragments.push_tree(suffix, &None);
1037        let (visible_text, deleted_text) = new_ropes.finish();
1038        drop(old_fragments);
1039
1040        self.snapshot.fragments = new_fragments;
1041        self.snapshot.visible_text = visible_text;
1042        self.snapshot.deleted_text = deleted_text;
1043        self.snapshot.insertions.edit(new_insertions, &());
1044        self.history
1045            .insertion_slices
1046            .insert(timestamp.local(), insertion_slices);
1047        self.subscriptions.publish_mut(&edits_patch)
1048    }
1049
1050    fn fragment_ids_for_edits<'a>(
1051        &'a self,
1052        edit_ids: impl Iterator<Item = &'a clock::Local>,
1053    ) -> Vec<&'a Locator> {
1054        // Get all of the insertion slices changed by the given edits.
1055        let mut insertion_slices = Vec::new();
1056        for edit_id in edit_ids {
1057            if let Some(slices) = self.history.insertion_slices.get(edit_id) {
1058                insertion_slices.extend_from_slice(slices)
1059            }
1060        }
1061        insertion_slices
1062            .sort_unstable_by_key(|s| (s.insertion_id, s.range.start, Reverse(s.range.end)));
1063
1064        // Get all of the fragments corresponding to these insertion slices.
1065        let mut fragment_ids = Vec::new();
1066        let mut insertions_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1067        for insertion_slice in &insertion_slices {
1068            if insertion_slice.insertion_id != insertions_cursor.start().timestamp
1069                || insertion_slice.range.start > insertions_cursor.start().split_offset
1070            {
1071                insertions_cursor.seek_forward(
1072                    &InsertionFragmentKey {
1073                        timestamp: insertion_slice.insertion_id,
1074                        split_offset: insertion_slice.range.start,
1075                    },
1076                    Bias::Left,
1077                    &(),
1078                );
1079            }
1080            while let Some(item) = insertions_cursor.item() {
1081                if item.timestamp != insertion_slice.insertion_id
1082                    || item.split_offset >= insertion_slice.range.end
1083                {
1084                    break;
1085                }
1086                fragment_ids.push(&item.fragment_id);
1087                insertions_cursor.next(&());
1088            }
1089        }
1090        fragment_ids.sort_unstable();
1091        fragment_ids
1092    }
1093
1094    fn apply_undo(&mut self, undo: &UndoOperation) -> Result<()> {
1095        self.snapshot.undo_map.insert(undo);
1096
1097        let mut edits = Patch::default();
1098        let mut old_fragments = self.fragments.cursor::<(Option<&Locator>, usize)>();
1099        let mut new_fragments = SumTree::new();
1100        let mut new_ropes =
1101            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1102
1103        for fragment_id in self.fragment_ids_for_edits(undo.counts.keys()) {
1104            let preceding_fragments = old_fragments.slice(&Some(fragment_id), Bias::Left, &None);
1105            new_ropes.push_tree(preceding_fragments.summary().text);
1106            new_fragments.push_tree(preceding_fragments, &None);
1107
1108            if let Some(fragment) = old_fragments.item() {
1109                let mut fragment = fragment.clone();
1110                let fragment_was_visible = fragment.visible;
1111
1112                fragment.visible = fragment.is_visible(&self.undo_map);
1113                fragment.max_undos.observe(undo.id);
1114
1115                let old_start = old_fragments.start().1;
1116                let new_start = new_fragments.summary().text.visible;
1117                if fragment_was_visible && !fragment.visible {
1118                    edits.push(Edit {
1119                        old: old_start..old_start + fragment.len,
1120                        new: new_start..new_start,
1121                    });
1122                } else if !fragment_was_visible && fragment.visible {
1123                    edits.push(Edit {
1124                        old: old_start..old_start,
1125                        new: new_start..new_start + fragment.len,
1126                    });
1127                }
1128                new_ropes.push_fragment(&fragment, fragment_was_visible);
1129                new_fragments.push(fragment, &None);
1130
1131                old_fragments.next(&None);
1132            }
1133        }
1134
1135        let suffix = old_fragments.suffix(&None);
1136        new_ropes.push_tree(suffix.summary().text);
1137        new_fragments.push_tree(suffix, &None);
1138
1139        drop(old_fragments);
1140        let (visible_text, deleted_text) = new_ropes.finish();
1141        self.snapshot.fragments = new_fragments;
1142        self.snapshot.visible_text = visible_text;
1143        self.snapshot.deleted_text = deleted_text;
1144        self.subscriptions.publish_mut(&edits);
1145        Ok(())
1146    }
1147
1148    fn flush_deferred_ops(&mut self) -> Result<()> {
1149        self.deferred_replicas.clear();
1150        let mut deferred_ops = Vec::new();
1151        for op in self.deferred_ops.drain().iter().cloned() {
1152            if self.can_apply_op(&op) {
1153                self.apply_op(op)?;
1154            } else {
1155                self.deferred_replicas.insert(op.replica_id());
1156                deferred_ops.push(op);
1157            }
1158        }
1159        self.deferred_ops.insert(deferred_ops);
1160        Ok(())
1161    }
1162
1163    fn can_apply_op(&self, op: &Operation) -> bool {
1164        if self.deferred_replicas.contains(&op.replica_id()) {
1165            false
1166        } else {
1167            match op {
1168                Operation::Edit(edit) => self.version.observed_all(&edit.version),
1169                Operation::Undo { undo, .. } => self.version.observed_all(&undo.version),
1170            }
1171        }
1172    }
1173
1174    pub fn peek_undo_stack(&self) -> Option<&HistoryEntry> {
1175        self.history.undo_stack.last()
1176    }
1177
1178    pub fn peek_redo_stack(&self) -> Option<&HistoryEntry> {
1179        self.history.redo_stack.last()
1180    }
1181
1182    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1183        self.start_transaction_at(Instant::now())
1184    }
1185
1186    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1187        self.history
1188            .start_transaction(self.version.clone(), now, &mut self.local_clock)
1189    }
1190
1191    pub fn end_transaction(&mut self) -> Option<(TransactionId, clock::Global)> {
1192        self.end_transaction_at(Instant::now())
1193    }
1194
1195    pub fn end_transaction_at(&mut self, now: Instant) -> Option<(TransactionId, clock::Global)> {
1196        if let Some(entry) = self.history.end_transaction(now) {
1197            let since = entry.transaction.start.clone();
1198            let id = self.history.group().unwrap();
1199            Some((id, since))
1200        } else {
1201            None
1202        }
1203    }
1204
1205    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1206        self.history.finalize_last_transaction()
1207    }
1208
1209    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1210        self.history.group_until(transaction_id);
1211    }
1212
1213    pub fn base_text(&self) -> &Arc<str> {
1214        &self.history.base_text
1215    }
1216
1217    pub fn operations(&self) -> &TreeMap<clock::Local, Operation> {
1218        &self.history.operations
1219    }
1220
1221    pub fn undo_history(&self) -> impl Iterator<Item = (&clock::Local, &[(clock::Local, u32)])> {
1222        self.undo_map
1223            .0
1224            .iter()
1225            .map(|(edit_id, undo_counts)| (edit_id, undo_counts.as_slice()))
1226    }
1227
1228    pub fn undo(&mut self) -> Option<(TransactionId, Operation)> {
1229        if let Some(entry) = self.history.pop_undo() {
1230            let transaction = entry.transaction.clone();
1231            let transaction_id = transaction.id;
1232            let op = self.undo_or_redo(transaction).unwrap();
1233            Some((transaction_id, op))
1234        } else {
1235            None
1236        }
1237    }
1238
1239    #[allow(clippy::needless_collect)]
1240    pub fn undo_to_transaction(&mut self, transaction_id: TransactionId) -> Vec<Operation> {
1241        let transactions = self
1242            .history
1243            .remove_from_undo(transaction_id)
1244            .iter()
1245            .map(|entry| entry.transaction.clone())
1246            .collect::<Vec<_>>();
1247
1248        transactions
1249            .into_iter()
1250            .map(|transaction| self.undo_or_redo(transaction).unwrap())
1251            .collect()
1252    }
1253
1254    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1255        self.history.forget(transaction_id);
1256    }
1257
1258    pub fn redo(&mut self) -> Option<(TransactionId, Operation)> {
1259        if let Some(entry) = self.history.pop_redo() {
1260            let transaction = entry.transaction.clone();
1261            let transaction_id = transaction.id;
1262            let op = self.undo_or_redo(transaction).unwrap();
1263            Some((transaction_id, op))
1264        } else {
1265            None
1266        }
1267    }
1268
1269    #[allow(clippy::needless_collect)]
1270    pub fn redo_to_transaction(&mut self, transaction_id: TransactionId) -> Vec<Operation> {
1271        let transactions = self
1272            .history
1273            .remove_from_redo(transaction_id)
1274            .iter()
1275            .map(|entry| entry.transaction.clone())
1276            .collect::<Vec<_>>();
1277
1278        transactions
1279            .into_iter()
1280            .map(|transaction| self.undo_or_redo(transaction).unwrap())
1281            .collect()
1282    }
1283
1284    fn undo_or_redo(&mut self, transaction: Transaction) -> Result<Operation> {
1285        let mut counts = HashMap::default();
1286        for edit_id in transaction.edit_ids {
1287            counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1288        }
1289
1290        let undo = UndoOperation {
1291            id: self.local_clock.tick(),
1292            version: self.version(),
1293            counts,
1294        };
1295        self.apply_undo(&undo)?;
1296        let operation = Operation::Undo {
1297            undo,
1298            lamport_timestamp: self.lamport_clock.tick(),
1299        };
1300        self.snapshot.version.observe(operation.local_timestamp());
1301        self.history.push(operation.clone());
1302        Ok(operation)
1303    }
1304
1305    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1306        self.history.push_transaction(transaction, now);
1307        self.history.finalize_last_transaction();
1308    }
1309
1310    pub fn edited_ranges_for_transaction<'a, D>(
1311        &'a self,
1312        transaction: &'a Transaction,
1313    ) -> impl 'a + Iterator<Item = Range<D>>
1314    where
1315        D: TextDimension,
1316    {
1317        // get fragment ranges
1318        let mut cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1319        let offset_ranges = self
1320            .fragment_ids_for_edits(transaction.edit_ids.iter())
1321            .into_iter()
1322            .filter_map(move |fragment_id| {
1323                cursor.seek_forward(&Some(fragment_id), Bias::Left, &None);
1324                let fragment = cursor.item()?;
1325                let start_offset = cursor.start().1;
1326                let end_offset = start_offset + if fragment.visible { fragment.len } else { 0 };
1327                Some(start_offset..end_offset)
1328            });
1329
1330        // combine adjacent ranges
1331        let mut prev_range: Option<Range<usize>> = None;
1332        let disjoint_ranges = offset_ranges
1333            .map(Some)
1334            .chain([None])
1335            .filter_map(move |range| {
1336                if let Some((range, prev_range)) = range.as_ref().zip(prev_range.as_mut()) {
1337                    if prev_range.end == range.start {
1338                        prev_range.end = range.end;
1339                        return None;
1340                    }
1341                }
1342                let result = prev_range.clone();
1343                prev_range = range;
1344                result
1345            });
1346
1347        // convert to the desired text dimension.
1348        let mut position = D::default();
1349        let mut rope_cursor = self.visible_text.cursor(0);
1350        disjoint_ranges.map(move |range| {
1351            position.add_assign(&rope_cursor.summary(range.start));
1352            let start = position.clone();
1353            position.add_assign(&rope_cursor.summary(range.end));
1354            let end = position.clone();
1355            start..end
1356        })
1357    }
1358
1359    pub fn subscribe(&mut self) -> Subscription {
1360        self.subscriptions.subscribe()
1361    }
1362
1363    pub fn wait_for_edits(
1364        &mut self,
1365        edit_ids: impl IntoIterator<Item = clock::Local>,
1366    ) -> impl 'static + Future<Output = ()> {
1367        let mut futures = Vec::new();
1368        for edit_id in edit_ids {
1369            if !self.version.observed(edit_id) {
1370                let (tx, rx) = oneshot::channel();
1371                self.edit_id_resolvers.entry(edit_id).or_default().push(tx);
1372                futures.push(rx);
1373            }
1374        }
1375
1376        async move {
1377            for mut future in futures {
1378                future.recv().await;
1379            }
1380        }
1381    }
1382
1383    pub fn wait_for_anchors<'a>(
1384        &mut self,
1385        anchors: impl IntoIterator<Item = &'a Anchor>,
1386    ) -> impl 'static + Future<Output = ()> {
1387        let mut futures = Vec::new();
1388        for anchor in anchors {
1389            if !self.version.observed(anchor.timestamp)
1390                && *anchor != Anchor::MAX
1391                && *anchor != Anchor::MIN
1392            {
1393                let (tx, rx) = oneshot::channel();
1394                self.edit_id_resolvers
1395                    .entry(anchor.timestamp)
1396                    .or_default()
1397                    .push(tx);
1398                futures.push(rx);
1399            }
1400        }
1401
1402        async move {
1403            for mut future in futures {
1404                future.recv().await;
1405            }
1406        }
1407    }
1408
1409    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1410        let (tx, mut rx) = barrier::channel();
1411        if !self.snapshot.version.observed_all(&version) {
1412            self.version_barriers.push((version, tx));
1413        }
1414        async move {
1415            rx.recv().await;
1416        }
1417    }
1418
1419    fn resolve_edit(&mut self, edit_id: clock::Local) {
1420        for mut tx in self
1421            .edit_id_resolvers
1422            .remove(&edit_id)
1423            .into_iter()
1424            .flatten()
1425        {
1426            let _ = tx.try_send(());
1427        }
1428    }
1429}
1430
1431#[cfg(any(test, feature = "test-support"))]
1432impl Buffer {
1433    pub fn check_invariants(&self) {
1434        // Ensure every fragment is ordered by locator in the fragment tree and corresponds
1435        // to an insertion fragment in the insertions tree.
1436        let mut prev_fragment_id = Locator::min();
1437        for fragment in self.snapshot.fragments.items(&None) {
1438            assert!(fragment.id > prev_fragment_id);
1439            prev_fragment_id = fragment.id.clone();
1440
1441            let insertion_fragment = self
1442                .snapshot
1443                .insertions
1444                .get(
1445                    &InsertionFragmentKey {
1446                        timestamp: fragment.insertion_timestamp.local(),
1447                        split_offset: fragment.insertion_offset,
1448                    },
1449                    &(),
1450                )
1451                .unwrap();
1452            assert_eq!(
1453                insertion_fragment.fragment_id, fragment.id,
1454                "fragment: {:?}\ninsertion: {:?}",
1455                fragment, insertion_fragment
1456            );
1457        }
1458
1459        let mut cursor = self.snapshot.fragments.cursor::<Option<&Locator>>();
1460        for insertion_fragment in self.snapshot.insertions.cursor::<()>() {
1461            cursor.seek(&Some(&insertion_fragment.fragment_id), Bias::Left, &None);
1462            let fragment = cursor.item().unwrap();
1463            assert_eq!(insertion_fragment.fragment_id, fragment.id);
1464            assert_eq!(insertion_fragment.split_offset, fragment.insertion_offset);
1465        }
1466
1467        let fragment_summary = self.snapshot.fragments.summary();
1468        assert_eq!(
1469            fragment_summary.text.visible,
1470            self.snapshot.visible_text.len()
1471        );
1472        assert_eq!(
1473            fragment_summary.text.deleted,
1474            self.snapshot.deleted_text.len()
1475        );
1476
1477        assert!(!self.text().contains("\r\n"));
1478    }
1479
1480    pub fn set_group_interval(&mut self, group_interval: Duration) {
1481        self.history.group_interval = group_interval;
1482    }
1483
1484    pub fn random_byte_range(&self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
1485        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
1486        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
1487        start..end
1488    }
1489
1490    #[allow(clippy::type_complexity)]
1491    pub fn randomly_edit<T>(
1492        &mut self,
1493        rng: &mut T,
1494        edit_count: usize,
1495    ) -> (Vec<(Range<usize>, Arc<str>)>, Operation)
1496    where
1497        T: rand::Rng,
1498    {
1499        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1500        let mut last_end = None;
1501        for _ in 0..edit_count {
1502            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1503                break;
1504            }
1505            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1506            let range = self.random_byte_range(new_start, rng);
1507            last_end = Some(range.end);
1508
1509            let new_text_len = rng.gen_range(0..10);
1510            let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1511                .take(new_text_len)
1512                .collect();
1513
1514            edits.push((range, new_text.into()));
1515        }
1516
1517        log::info!("mutating buffer {} with {:?}", self.replica_id, edits);
1518        let op = self.edit(edits.iter().cloned());
1519        if let Operation::Edit(edit) = &op {
1520            assert_eq!(edits.len(), edit.new_text.len());
1521            for (edit, new_text) in edits.iter_mut().zip(&edit.new_text) {
1522                edit.1 = new_text.clone();
1523            }
1524        } else {
1525            unreachable!()
1526        }
1527
1528        (edits, op)
1529    }
1530
1531    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
1532        use rand::prelude::*;
1533
1534        let mut ops = Vec::new();
1535        for _ in 0..rng.gen_range(1..=5) {
1536            if let Some(entry) = self.history.undo_stack.choose(rng) {
1537                let transaction = entry.transaction.clone();
1538                log::info!(
1539                    "undoing buffer {} transaction {:?}",
1540                    self.replica_id,
1541                    transaction
1542                );
1543                ops.push(self.undo_or_redo(transaction).unwrap());
1544            }
1545        }
1546        ops
1547    }
1548}
1549
1550impl Deref for Buffer {
1551    type Target = BufferSnapshot;
1552
1553    fn deref(&self) -> &Self::Target {
1554        &self.snapshot
1555    }
1556}
1557
1558impl BufferSnapshot {
1559    pub fn as_rope(&self) -> &Rope {
1560        &self.visible_text
1561    }
1562
1563    pub fn replica_id(&self) -> ReplicaId {
1564        self.replica_id
1565    }
1566
1567    pub fn row_count(&self) -> u32 {
1568        self.max_point().row + 1
1569    }
1570
1571    pub fn len(&self) -> usize {
1572        self.visible_text.len()
1573    }
1574
1575    pub fn is_empty(&self) -> bool {
1576        self.len() == 0
1577    }
1578
1579    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1580        self.chars_at(0)
1581    }
1582
1583    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1584        self.text_for_range(range).flat_map(str::chars)
1585    }
1586
1587    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1588    where
1589        T: ToOffset,
1590    {
1591        let position = position.to_offset(self);
1592        position == self.clip_offset(position, Bias::Left)
1593            && self
1594                .bytes_in_range(position..self.len())
1595                .flatten()
1596                .copied()
1597                .take(needle.len())
1598                .eq(needle.bytes())
1599    }
1600
1601    pub fn common_prefix_at<T>(&self, position: T, needle: &str) -> Range<T>
1602    where
1603        T: ToOffset + TextDimension,
1604    {
1605        let offset = position.to_offset(self);
1606        let common_prefix_len = needle
1607            .char_indices()
1608            .map(|(index, _)| index)
1609            .chain([needle.len()])
1610            .take_while(|&len| len <= offset)
1611            .filter(|&len| {
1612                let left = self
1613                    .chars_for_range(offset - len..offset)
1614                    .flat_map(char::to_lowercase);
1615                let right = needle[..len].chars().flat_map(char::to_lowercase);
1616                left.eq(right)
1617            })
1618            .last()
1619            .unwrap_or(0);
1620        let start_offset = offset - common_prefix_len;
1621        let start = self.text_summary_for_range(0..start_offset);
1622        start..position
1623    }
1624
1625    pub fn text(&self) -> String {
1626        self.visible_text.to_string()
1627    }
1628
1629    pub fn line_ending(&self) -> LineEnding {
1630        self.line_ending
1631    }
1632
1633    pub fn deleted_text(&self) -> String {
1634        self.deleted_text.to_string()
1635    }
1636
1637    pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
1638        self.fragments.iter()
1639    }
1640
1641    pub fn text_summary(&self) -> TextSummary {
1642        self.visible_text.summary()
1643    }
1644
1645    pub fn max_point(&self) -> Point {
1646        self.visible_text.max_point()
1647    }
1648
1649    pub fn max_point_utf16(&self) -> PointUtf16 {
1650        self.visible_text.max_point_utf16()
1651    }
1652
1653    pub fn point_to_offset(&self, point: Point) -> usize {
1654        self.visible_text.point_to_offset(point)
1655    }
1656
1657    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
1658        self.visible_text.point_utf16_to_offset(point)
1659    }
1660
1661    pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
1662        self.visible_text.point_utf16_to_point(point)
1663    }
1664
1665    pub fn offset_utf16_to_offset(&self, offset: OffsetUtf16) -> usize {
1666        self.visible_text.offset_utf16_to_offset(offset)
1667    }
1668
1669    pub fn offset_to_offset_utf16(&self, offset: usize) -> OffsetUtf16 {
1670        self.visible_text.offset_to_offset_utf16(offset)
1671    }
1672
1673    pub fn offset_to_point(&self, offset: usize) -> Point {
1674        self.visible_text.offset_to_point(offset)
1675    }
1676
1677    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
1678        self.visible_text.offset_to_point_utf16(offset)
1679    }
1680
1681    pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
1682        self.visible_text.point_to_point_utf16(point)
1683    }
1684
1685    pub fn version(&self) -> &clock::Global {
1686        &self.version
1687    }
1688
1689    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
1690        let offset = position.to_offset(self);
1691        self.visible_text.chars_at(offset)
1692    }
1693
1694    pub fn reversed_chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + '_ {
1695        let offset = position.to_offset(self);
1696        self.visible_text.reversed_chars_at(offset)
1697    }
1698
1699    pub fn reversed_chunks_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Chunks {
1700        let range = range.start.to_offset(self)..range.end.to_offset(self);
1701        self.visible_text.reversed_chunks_in_range(range)
1702    }
1703
1704    pub fn bytes_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Bytes<'_> {
1705        let start = range.start.to_offset(self);
1706        let end = range.end.to_offset(self);
1707        self.visible_text.bytes_in_range(start..end)
1708    }
1709
1710    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks<'_> {
1711        let start = range.start.to_offset(self);
1712        let end = range.end.to_offset(self);
1713        self.visible_text.chunks_in_range(start..end)
1714    }
1715
1716    pub fn line_len(&self, row: u32) -> u32 {
1717        let row_start_offset = Point::new(row, 0).to_offset(self);
1718        let row_end_offset = if row >= self.max_point().row {
1719            self.len()
1720        } else {
1721            Point::new(row + 1, 0).to_offset(self) - 1
1722        };
1723        (row_end_offset - row_start_offset) as u32
1724    }
1725
1726    pub fn is_line_blank(&self, row: u32) -> bool {
1727        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1728            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1729    }
1730
1731    pub fn text_summary_for_range<D, O: ToOffset>(&self, range: Range<O>) -> D
1732    where
1733        D: TextDimension,
1734    {
1735        self.visible_text
1736            .cursor(range.start.to_offset(self))
1737            .summary(range.end.to_offset(self))
1738    }
1739
1740    pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
1741    where
1742        D: 'a + TextDimension,
1743        A: 'a + IntoIterator<Item = &'a Anchor>,
1744    {
1745        let anchors = anchors.into_iter();
1746        let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1747        let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1748        let mut text_cursor = self.visible_text.cursor(0);
1749        let mut position = D::default();
1750
1751        anchors.map(move |anchor| {
1752            if *anchor == Anchor::MIN {
1753                return D::default();
1754            } else if *anchor == Anchor::MAX {
1755                return D::from_text_summary(&self.visible_text.summary());
1756            }
1757
1758            let anchor_key = InsertionFragmentKey {
1759                timestamp: anchor.timestamp,
1760                split_offset: anchor.offset,
1761            };
1762            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1763            if let Some(insertion) = insertion_cursor.item() {
1764                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1765                if comparison == Ordering::Greater
1766                    || (anchor.bias == Bias::Left
1767                        && comparison == Ordering::Equal
1768                        && anchor.offset > 0)
1769                {
1770                    insertion_cursor.prev(&());
1771                }
1772            } else {
1773                insertion_cursor.prev(&());
1774            }
1775            let insertion = insertion_cursor.item().expect("invalid insertion");
1776            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1777
1778            fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left, &None);
1779            let fragment = fragment_cursor.item().unwrap();
1780            let mut fragment_offset = fragment_cursor.start().1;
1781            if fragment.visible {
1782                fragment_offset += anchor.offset - insertion.split_offset;
1783            }
1784
1785            position.add_assign(&text_cursor.summary(fragment_offset));
1786            position.clone()
1787        })
1788    }
1789
1790    fn summary_for_anchor<D>(&self, anchor: &Anchor) -> D
1791    where
1792        D: TextDimension,
1793    {
1794        if *anchor == Anchor::MIN {
1795            D::default()
1796        } else if *anchor == Anchor::MAX {
1797            D::from_text_summary(&self.visible_text.summary())
1798        } else {
1799            let anchor_key = InsertionFragmentKey {
1800                timestamp: anchor.timestamp,
1801                split_offset: anchor.offset,
1802            };
1803            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1804            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1805            if let Some(insertion) = insertion_cursor.item() {
1806                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1807                if comparison == Ordering::Greater
1808                    || (anchor.bias == Bias::Left
1809                        && comparison == Ordering::Equal
1810                        && anchor.offset > 0)
1811                {
1812                    insertion_cursor.prev(&());
1813                }
1814            } else {
1815                insertion_cursor.prev(&());
1816            }
1817            let insertion = insertion_cursor.item().expect("invalid insertion");
1818            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1819
1820            let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1821            fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None);
1822            let fragment = fragment_cursor.item().unwrap();
1823            let mut fragment_offset = fragment_cursor.start().1;
1824            if fragment.visible {
1825                fragment_offset += anchor.offset - insertion.split_offset;
1826            }
1827            self.text_summary_for_range(0..fragment_offset)
1828        }
1829    }
1830
1831    fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
1832        if *anchor == Anchor::MIN {
1833            &locator::MIN
1834        } else if *anchor == Anchor::MAX {
1835            &locator::MAX
1836        } else {
1837            let anchor_key = InsertionFragmentKey {
1838                timestamp: anchor.timestamp,
1839                split_offset: anchor.offset,
1840            };
1841            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1842            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1843            if let Some(insertion) = insertion_cursor.item() {
1844                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1845                if comparison == Ordering::Greater
1846                    || (anchor.bias == Bias::Left
1847                        && comparison == Ordering::Equal
1848                        && anchor.offset > 0)
1849                {
1850                    insertion_cursor.prev(&());
1851                }
1852            } else {
1853                insertion_cursor.prev(&());
1854            }
1855            let insertion = insertion_cursor.item().expect("invalid insertion");
1856            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1857            &insertion.fragment_id
1858        }
1859    }
1860
1861    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
1862        self.anchor_at(position, Bias::Left)
1863    }
1864
1865    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
1866        self.anchor_at(position, Bias::Right)
1867    }
1868
1869    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
1870        let offset = position.to_offset(self);
1871        if bias == Bias::Left && offset == 0 {
1872            Anchor::MIN
1873        } else if bias == Bias::Right && offset == self.len() {
1874            Anchor::MAX
1875        } else {
1876            let mut fragment_cursor = self.fragments.cursor::<usize>();
1877            fragment_cursor.seek(&offset, bias, &None);
1878            let fragment = fragment_cursor.item().unwrap();
1879            let overshoot = offset - *fragment_cursor.start();
1880            Anchor {
1881                timestamp: fragment.insertion_timestamp.local(),
1882                offset: fragment.insertion_offset + overshoot,
1883                bias,
1884                buffer_id: Some(self.remote_id),
1885            }
1886        }
1887    }
1888
1889    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
1890        *anchor == Anchor::MIN
1891            || *anchor == Anchor::MAX
1892            || (Some(self.remote_id) == anchor.buffer_id && self.version.observed(anchor.timestamp))
1893    }
1894
1895    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1896        self.visible_text.clip_offset(offset, bias)
1897    }
1898
1899    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1900        self.visible_text.clip_point(point, bias)
1901    }
1902
1903    pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
1904        self.visible_text.clip_offset_utf16(offset, bias)
1905    }
1906
1907    pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
1908        self.visible_text.clip_point_utf16(point, bias)
1909    }
1910
1911    pub fn edits_since<'a, D>(
1912        &'a self,
1913        since: &'a clock::Global,
1914    ) -> impl 'a + Iterator<Item = Edit<D>>
1915    where
1916        D: TextDimension + Ord,
1917    {
1918        self.edits_since_in_range(since, Anchor::MIN..Anchor::MAX)
1919    }
1920
1921    pub fn anchored_edits_since<'a, D>(
1922        &'a self,
1923        since: &'a clock::Global,
1924    ) -> impl 'a + Iterator<Item = (Edit<D>, Range<Anchor>)>
1925    where
1926        D: TextDimension + Ord,
1927    {
1928        self.anchored_edits_since_in_range(since, Anchor::MIN..Anchor::MAX)
1929    }
1930
1931    pub fn edits_since_in_range<'a, D>(
1932        &'a self,
1933        since: &'a clock::Global,
1934        range: Range<Anchor>,
1935    ) -> impl 'a + Iterator<Item = Edit<D>>
1936    where
1937        D: TextDimension + Ord,
1938    {
1939        self.anchored_edits_since_in_range(since, range)
1940            .map(|item| item.0)
1941    }
1942
1943    pub fn anchored_edits_since_in_range<'a, D>(
1944        &'a self,
1945        since: &'a clock::Global,
1946        range: Range<Anchor>,
1947    ) -> impl 'a + Iterator<Item = (Edit<D>, Range<Anchor>)>
1948    where
1949        D: TextDimension + Ord,
1950    {
1951        let fragments_cursor = if *since == self.version {
1952            None
1953        } else {
1954            let mut cursor = self
1955                .fragments
1956                .filter(move |summary| !since.observed_all(&summary.max_version));
1957            cursor.next(&None);
1958            Some(cursor)
1959        };
1960        let mut cursor = self
1961            .fragments
1962            .cursor::<(Option<&Locator>, FragmentTextSummary)>();
1963
1964        let start_fragment_id = self.fragment_id_for_anchor(&range.start);
1965        cursor.seek(&Some(start_fragment_id), Bias::Left, &None);
1966        let mut visible_start = cursor.start().1.visible;
1967        let mut deleted_start = cursor.start().1.deleted;
1968        if let Some(fragment) = cursor.item() {
1969            let overshoot = range.start.offset - fragment.insertion_offset;
1970            if fragment.visible {
1971                visible_start += overshoot;
1972            } else {
1973                deleted_start += overshoot;
1974            }
1975        }
1976        let end_fragment_id = self.fragment_id_for_anchor(&range.end);
1977
1978        Edits {
1979            visible_cursor: self.visible_text.cursor(visible_start),
1980            deleted_cursor: self.deleted_text.cursor(deleted_start),
1981            fragments_cursor,
1982            undos: &self.undo_map,
1983            since,
1984            old_end: Default::default(),
1985            new_end: Default::default(),
1986            range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
1987            buffer_id: self.remote_id,
1988        }
1989    }
1990}
1991
1992struct RopeBuilder<'a> {
1993    old_visible_cursor: rope::Cursor<'a>,
1994    old_deleted_cursor: rope::Cursor<'a>,
1995    new_visible: Rope,
1996    new_deleted: Rope,
1997}
1998
1999impl<'a> RopeBuilder<'a> {
2000    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
2001        Self {
2002            old_visible_cursor,
2003            old_deleted_cursor,
2004            new_visible: Rope::new(),
2005            new_deleted: Rope::new(),
2006        }
2007    }
2008
2009    fn push_tree(&mut self, len: FragmentTextSummary) {
2010        self.push(len.visible, true, true);
2011        self.push(len.deleted, false, false);
2012    }
2013
2014    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
2015        debug_assert!(fragment.len > 0);
2016        self.push(fragment.len, was_visible, fragment.visible)
2017    }
2018
2019    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
2020        let text = if was_visible {
2021            self.old_visible_cursor
2022                .slice(self.old_visible_cursor.offset() + len)
2023        } else {
2024            self.old_deleted_cursor
2025                .slice(self.old_deleted_cursor.offset() + len)
2026        };
2027        if is_visible {
2028            self.new_visible.append(text);
2029        } else {
2030            self.new_deleted.append(text);
2031        }
2032    }
2033
2034    fn push_str(&mut self, text: &str) {
2035        self.new_visible.push(text);
2036    }
2037
2038    fn finish(mut self) -> (Rope, Rope) {
2039        self.new_visible.append(self.old_visible_cursor.suffix());
2040        self.new_deleted.append(self.old_deleted_cursor.suffix());
2041        (self.new_visible, self.new_deleted)
2042    }
2043}
2044
2045impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
2046    type Item = (Edit<D>, Range<Anchor>);
2047
2048    fn next(&mut self) -> Option<Self::Item> {
2049        let mut pending_edit: Option<Self::Item> = None;
2050        let cursor = self.fragments_cursor.as_mut()?;
2051
2052        while let Some(fragment) = cursor.item() {
2053            if fragment.id < *self.range.start.0 {
2054                cursor.next(&None);
2055                continue;
2056            } else if fragment.id > *self.range.end.0 {
2057                break;
2058            }
2059
2060            if cursor.start().visible > self.visible_cursor.offset() {
2061                let summary = self.visible_cursor.summary(cursor.start().visible);
2062                self.old_end.add_assign(&summary);
2063                self.new_end.add_assign(&summary);
2064            }
2065
2066            if pending_edit
2067                .as_ref()
2068                .map_or(false, |(change, _)| change.new.end < self.new_end)
2069            {
2070                break;
2071            }
2072
2073            let timestamp = fragment.insertion_timestamp.local();
2074            let start_anchor = Anchor {
2075                timestamp,
2076                offset: fragment.insertion_offset,
2077                bias: Bias::Right,
2078                buffer_id: Some(self.buffer_id),
2079            };
2080            let end_anchor = Anchor {
2081                timestamp,
2082                offset: fragment.insertion_offset + fragment.len,
2083                bias: Bias::Left,
2084                buffer_id: Some(self.buffer_id),
2085            };
2086
2087            if !fragment.was_visible(self.since, self.undos) && fragment.visible {
2088                let mut visible_end = cursor.end(&None).visible;
2089                if fragment.id == *self.range.end.0 {
2090                    visible_end = cmp::min(
2091                        visible_end,
2092                        cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
2093                    );
2094                }
2095
2096                let fragment_summary = self.visible_cursor.summary(visible_end);
2097                let mut new_end = self.new_end.clone();
2098                new_end.add_assign(&fragment_summary);
2099                if let Some((edit, range)) = pending_edit.as_mut() {
2100                    edit.new.end = new_end.clone();
2101                    range.end = end_anchor;
2102                } else {
2103                    pending_edit = Some((
2104                        Edit {
2105                            old: self.old_end.clone()..self.old_end.clone(),
2106                            new: self.new_end.clone()..new_end.clone(),
2107                        },
2108                        start_anchor..end_anchor,
2109                    ));
2110                }
2111
2112                self.new_end = new_end;
2113            } else if fragment.was_visible(self.since, self.undos) && !fragment.visible {
2114                let mut deleted_end = cursor.end(&None).deleted;
2115                if fragment.id == *self.range.end.0 {
2116                    deleted_end = cmp::min(
2117                        deleted_end,
2118                        cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
2119                    );
2120                }
2121
2122                if cursor.start().deleted > self.deleted_cursor.offset() {
2123                    self.deleted_cursor.seek_forward(cursor.start().deleted);
2124                }
2125                let fragment_summary = self.deleted_cursor.summary(deleted_end);
2126                let mut old_end = self.old_end.clone();
2127                old_end.add_assign(&fragment_summary);
2128                if let Some((edit, range)) = pending_edit.as_mut() {
2129                    edit.old.end = old_end.clone();
2130                    range.end = end_anchor;
2131                } else {
2132                    pending_edit = Some((
2133                        Edit {
2134                            old: self.old_end.clone()..old_end.clone(),
2135                            new: self.new_end.clone()..self.new_end.clone(),
2136                        },
2137                        start_anchor..end_anchor,
2138                    ));
2139                }
2140
2141                self.old_end = old_end;
2142            }
2143
2144            cursor.next(&None);
2145        }
2146
2147        pending_edit
2148    }
2149}
2150
2151impl Fragment {
2152    fn insertion_slice(&self) -> InsertionSlice {
2153        InsertionSlice {
2154            insertion_id: self.insertion_timestamp.local(),
2155            range: self.insertion_offset..self.insertion_offset + self.len,
2156        }
2157    }
2158
2159    fn is_visible(&self, undos: &UndoMap) -> bool {
2160        !undos.is_undone(self.insertion_timestamp.local())
2161            && self.deletions.iter().all(|d| undos.is_undone(*d))
2162    }
2163
2164    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
2165        (version.observed(self.insertion_timestamp.local())
2166            && !undos.was_undone(self.insertion_timestamp.local(), version))
2167            && self
2168                .deletions
2169                .iter()
2170                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
2171    }
2172}
2173
2174impl sum_tree::Item for Fragment {
2175    type Summary = FragmentSummary;
2176
2177    fn summary(&self) -> Self::Summary {
2178        let mut max_version = clock::Global::new();
2179        max_version.observe(self.insertion_timestamp.local());
2180        for deletion in &self.deletions {
2181            max_version.observe(*deletion);
2182        }
2183        max_version.join(&self.max_undos);
2184
2185        let mut min_insertion_version = clock::Global::new();
2186        min_insertion_version.observe(self.insertion_timestamp.local());
2187        let max_insertion_version = min_insertion_version.clone();
2188        if self.visible {
2189            FragmentSummary {
2190                max_id: self.id.clone(),
2191                text: FragmentTextSummary {
2192                    visible: self.len,
2193                    deleted: 0,
2194                },
2195                max_version,
2196                min_insertion_version,
2197                max_insertion_version,
2198            }
2199        } else {
2200            FragmentSummary {
2201                max_id: self.id.clone(),
2202                text: FragmentTextSummary {
2203                    visible: 0,
2204                    deleted: self.len,
2205                },
2206                max_version,
2207                min_insertion_version,
2208                max_insertion_version,
2209            }
2210        }
2211    }
2212}
2213
2214impl sum_tree::Summary for FragmentSummary {
2215    type Context = Option<clock::Global>;
2216
2217    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
2218        self.max_id.assign(&other.max_id);
2219        self.text.visible += &other.text.visible;
2220        self.text.deleted += &other.text.deleted;
2221        self.max_version.join(&other.max_version);
2222        self.min_insertion_version
2223            .meet(&other.min_insertion_version);
2224        self.max_insertion_version
2225            .join(&other.max_insertion_version);
2226    }
2227}
2228
2229impl Default for FragmentSummary {
2230    fn default() -> Self {
2231        FragmentSummary {
2232            max_id: Locator::min(),
2233            text: FragmentTextSummary::default(),
2234            max_version: clock::Global::new(),
2235            min_insertion_version: clock::Global::new(),
2236            max_insertion_version: clock::Global::new(),
2237        }
2238    }
2239}
2240
2241impl sum_tree::Item for InsertionFragment {
2242    type Summary = InsertionFragmentKey;
2243
2244    fn summary(&self) -> Self::Summary {
2245        InsertionFragmentKey {
2246            timestamp: self.timestamp,
2247            split_offset: self.split_offset,
2248        }
2249    }
2250}
2251
2252impl sum_tree::KeyedItem for InsertionFragment {
2253    type Key = InsertionFragmentKey;
2254
2255    fn key(&self) -> Self::Key {
2256        sum_tree::Item::summary(self)
2257    }
2258}
2259
2260impl InsertionFragment {
2261    fn new(fragment: &Fragment) -> Self {
2262        Self {
2263            timestamp: fragment.insertion_timestamp.local(),
2264            split_offset: fragment.insertion_offset,
2265            fragment_id: fragment.id.clone(),
2266        }
2267    }
2268
2269    fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
2270        sum_tree::Edit::Insert(Self::new(fragment))
2271    }
2272}
2273
2274impl sum_tree::Summary for InsertionFragmentKey {
2275    type Context = ();
2276
2277    fn add_summary(&mut self, summary: &Self, _: &()) {
2278        *self = *summary;
2279    }
2280}
2281
2282#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
2283pub struct FullOffset(pub usize);
2284
2285impl ops::AddAssign<usize> for FullOffset {
2286    fn add_assign(&mut self, rhs: usize) {
2287        self.0 += rhs;
2288    }
2289}
2290
2291impl ops::Add<usize> for FullOffset {
2292    type Output = Self;
2293
2294    fn add(mut self, rhs: usize) -> Self::Output {
2295        self += rhs;
2296        self
2297    }
2298}
2299
2300impl ops::Sub for FullOffset {
2301    type Output = usize;
2302
2303    fn sub(self, rhs: Self) -> Self::Output {
2304        self.0 - rhs.0
2305    }
2306}
2307
2308impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
2309    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2310        *self += summary.text.visible;
2311    }
2312}
2313
2314impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
2315    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2316        self.0 += summary.text.visible + summary.text.deleted;
2317    }
2318}
2319
2320impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
2321    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
2322        *self = Some(&summary.max_id);
2323    }
2324}
2325
2326impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
2327    fn cmp(
2328        &self,
2329        cursor_location: &FragmentTextSummary,
2330        _: &Option<clock::Global>,
2331    ) -> cmp::Ordering {
2332        Ord::cmp(self, &cursor_location.visible)
2333    }
2334}
2335
2336#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2337enum VersionedFullOffset {
2338    Offset(FullOffset),
2339    Invalid,
2340}
2341
2342impl VersionedFullOffset {
2343    fn full_offset(&self) -> FullOffset {
2344        if let Self::Offset(position) = self {
2345            *position
2346        } else {
2347            panic!("invalid version")
2348        }
2349    }
2350}
2351
2352impl Default for VersionedFullOffset {
2353    fn default() -> Self {
2354        Self::Offset(Default::default())
2355    }
2356}
2357
2358impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
2359    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
2360        if let Self::Offset(offset) = self {
2361            let version = cx.as_ref().unwrap();
2362            if version.observed_all(&summary.max_insertion_version) {
2363                *offset += summary.text.visible + summary.text.deleted;
2364            } else if version.observed_any(&summary.min_insertion_version) {
2365                *self = Self::Invalid;
2366            }
2367        }
2368    }
2369}
2370
2371impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedFullOffset {
2372    fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
2373        match (self, cursor_position) {
2374            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
2375            (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
2376            (Self::Invalid, _) => unreachable!(),
2377        }
2378    }
2379}
2380
2381impl Operation {
2382    fn replica_id(&self) -> ReplicaId {
2383        operation_queue::Operation::lamport_timestamp(self).replica_id
2384    }
2385
2386    pub fn local_timestamp(&self) -> clock::Local {
2387        match self {
2388            Operation::Edit(edit) => edit.timestamp.local(),
2389            Operation::Undo { undo, .. } => undo.id,
2390        }
2391    }
2392
2393    pub fn as_edit(&self) -> Option<&EditOperation> {
2394        match self {
2395            Operation::Edit(edit) => Some(edit),
2396            _ => None,
2397        }
2398    }
2399
2400    pub fn is_edit(&self) -> bool {
2401        matches!(self, Operation::Edit { .. })
2402    }
2403}
2404
2405impl operation_queue::Operation for Operation {
2406    fn lamport_timestamp(&self) -> clock::Lamport {
2407        match self {
2408            Operation::Edit(edit) => edit.timestamp.lamport(),
2409            Operation::Undo {
2410                lamport_timestamp, ..
2411            } => *lamport_timestamp,
2412        }
2413    }
2414}
2415
2416impl Default for LineEnding {
2417    fn default() -> Self {
2418        #[cfg(unix)]
2419        return Self::Unix;
2420
2421        #[cfg(not(unix))]
2422        return Self::CRLF;
2423    }
2424}
2425
2426impl LineEnding {
2427    pub fn as_str(&self) -> &'static str {
2428        match self {
2429            LineEnding::Unix => "\n",
2430            LineEnding::Windows => "\r\n",
2431        }
2432    }
2433
2434    pub fn detect(text: &str) -> Self {
2435        let mut max_ix = cmp::min(text.len(), 1000);
2436        while !text.is_char_boundary(max_ix) {
2437            max_ix -= 1;
2438        }
2439
2440        if let Some(ix) = text[..max_ix].find(&['\n']) {
2441            if ix > 0 && text.as_bytes()[ix - 1] == b'\r' {
2442                Self::Windows
2443            } else {
2444                Self::Unix
2445            }
2446        } else {
2447            Self::default()
2448        }
2449    }
2450
2451    pub fn normalize(text: &mut String) {
2452        if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(text, "\n") {
2453            *text = replaced;
2454        }
2455    }
2456
2457    fn normalize_arc(text: Arc<str>) -> Arc<str> {
2458        if let Cow::Owned(replaced) = CARRIAGE_RETURNS_REGEX.replace_all(&text, "\n") {
2459            replaced.into()
2460        } else {
2461            text
2462        }
2463    }
2464}
2465
2466pub trait ToOffset {
2467    fn to_offset(&self, snapshot: &BufferSnapshot) -> usize;
2468}
2469
2470impl ToOffset for Point {
2471    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2472        snapshot.point_to_offset(*self)
2473    }
2474}
2475
2476impl ToOffset for PointUtf16 {
2477    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2478        snapshot.point_utf16_to_offset(*self)
2479    }
2480}
2481
2482impl ToOffset for usize {
2483    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2484        assert!(*self <= snapshot.len(), "offset {self} is out of range");
2485        *self
2486    }
2487}
2488
2489impl ToOffset for OffsetUtf16 {
2490    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2491        snapshot.offset_utf16_to_offset(*self)
2492    }
2493}
2494
2495impl ToOffset for Anchor {
2496    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2497        snapshot.summary_for_anchor(self)
2498    }
2499}
2500
2501impl<'a, T: ToOffset> ToOffset for &'a T {
2502    fn to_offset(&self, content: &BufferSnapshot) -> usize {
2503        (*self).to_offset(content)
2504    }
2505}
2506
2507pub trait ToPoint {
2508    fn to_point(&self, snapshot: &BufferSnapshot) -> Point;
2509}
2510
2511impl ToPoint for Anchor {
2512    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2513        snapshot.summary_for_anchor(self)
2514    }
2515}
2516
2517impl ToPoint for usize {
2518    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2519        snapshot.offset_to_point(*self)
2520    }
2521}
2522
2523impl ToPoint for PointUtf16 {
2524    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2525        snapshot.point_utf16_to_point(*self)
2526    }
2527}
2528
2529impl ToPoint for Point {
2530    fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
2531        *self
2532    }
2533}
2534
2535pub trait ToPointUtf16 {
2536    fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> PointUtf16;
2537}
2538
2539impl ToPointUtf16 for Anchor {
2540    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2541        snapshot.summary_for_anchor(self)
2542    }
2543}
2544
2545impl ToPointUtf16 for usize {
2546    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2547        snapshot.offset_to_point_utf16(*self)
2548    }
2549}
2550
2551impl ToPointUtf16 for PointUtf16 {
2552    fn to_point_utf16<'a>(&self, _: &BufferSnapshot) -> PointUtf16 {
2553        *self
2554    }
2555}
2556
2557impl ToPointUtf16 for Point {
2558    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2559        snapshot.point_to_point_utf16(*self)
2560    }
2561}
2562
2563pub trait ToOffsetUtf16 {
2564    fn to_offset_utf16(&self, snapshot: &BufferSnapshot) -> OffsetUtf16;
2565}
2566
2567impl ToOffsetUtf16 for Anchor {
2568    fn to_offset_utf16<'a>(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 {
2569        snapshot.summary_for_anchor(self)
2570    }
2571}
2572
2573impl ToOffsetUtf16 for usize {
2574    fn to_offset_utf16<'a>(&self, snapshot: &BufferSnapshot) -> OffsetUtf16 {
2575        snapshot.offset_to_offset_utf16(*self)
2576    }
2577}
2578
2579impl ToOffsetUtf16 for OffsetUtf16 {
2580    fn to_offset_utf16<'a>(&self, _snapshot: &BufferSnapshot) -> OffsetUtf16 {
2581        *self
2582    }
2583}
2584
2585pub trait Clip {
2586    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self;
2587}
2588
2589impl Clip for usize {
2590    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2591        snapshot.clip_offset(*self, bias)
2592    }
2593}
2594
2595impl Clip for Point {
2596    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2597        snapshot.clip_point(*self, bias)
2598    }
2599}
2600
2601impl Clip for PointUtf16 {
2602    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2603        snapshot.clip_point_utf16(*self, bias)
2604    }
2605}
2606
2607pub trait FromAnchor {
2608    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
2609}
2610
2611impl FromAnchor for Point {
2612    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2613        snapshot.summary_for_anchor(anchor)
2614    }
2615}
2616
2617impl FromAnchor for PointUtf16 {
2618    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2619        snapshot.summary_for_anchor(anchor)
2620    }
2621}
2622
2623impl FromAnchor for usize {
2624    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2625        snapshot.summary_for_anchor(anchor)
2626    }
2627}