text.rs

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