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) -> Option<&HistoryEntry> {
 284        assert_eq!(self.transaction_depth, 0);
 285        if let Some(entry_ix) = self
 286            .undo_stack
 287            .iter()
 288            .rposition(|entry| entry.transaction.id == transaction_id)
 289        {
 290            let entry = self.undo_stack.remove(entry_ix);
 291            self.redo_stack.push(entry);
 292            self.redo_stack.last()
 293        } else {
 294            None
 295        }
 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) -> Option<&HistoryEntry> {
 326        assert_eq!(self.transaction_depth, 0);
 327        if let Some(entry_ix) = self
 328            .redo_stack
 329            .iter()
 330            .rposition(|entry| entry.transaction.id == transaction_id)
 331        {
 332            let entry = self.redo_stack.remove(entry_ix);
 333            self.undo_stack.push(entry);
 334            self.undo_stack.last()
 335        } else {
 336            None
 337        }
 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_transaction(&mut self, transaction_id: TransactionId) -> Option<Operation> {
1207        if let Some(entry) = self.history.remove_from_undo(transaction_id) {
1208            let transaction = entry.transaction.clone();
1209            let op = self.undo_or_redo(transaction).unwrap();
1210            Some(op)
1211        } else {
1212            None
1213        }
1214    }
1215
1216    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1217        self.history.forget(transaction_id);
1218    }
1219
1220    pub fn redo(&mut self) -> Option<(TransactionId, Operation)> {
1221        if let Some(entry) = self.history.pop_redo() {
1222            let transaction = entry.transaction.clone();
1223            let transaction_id = transaction.id;
1224            let op = self.undo_or_redo(transaction).unwrap();
1225            Some((transaction_id, op))
1226        } else {
1227            None
1228        }
1229    }
1230
1231    pub fn redo_transaction(&mut self, transaction_id: TransactionId) -> Option<Operation> {
1232        if let Some(entry) = self.history.remove_from_redo(transaction_id) {
1233            let transaction = entry.transaction.clone();
1234            let op = self.undo_or_redo(transaction).unwrap();
1235            Some(op)
1236        } else {
1237            None
1238        }
1239    }
1240
1241    fn undo_or_redo(&mut self, transaction: Transaction) -> Result<Operation> {
1242        let mut counts = HashMap::default();
1243        for edit_id in transaction.edit_ids {
1244            counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1245        }
1246
1247        let undo = UndoOperation {
1248            id: self.local_clock.tick(),
1249            counts,
1250            ranges: transaction.ranges,
1251            version: transaction.start.clone(),
1252        };
1253        self.apply_undo(&undo)?;
1254        let operation = Operation::Undo {
1255            undo,
1256            lamport_timestamp: self.lamport_clock.tick(),
1257        };
1258        self.snapshot.version.observe(operation.local_timestamp());
1259        self.history.push(operation.clone());
1260        Ok(operation)
1261    }
1262
1263    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1264        self.history.push_transaction(transaction, now);
1265        self.history.finalize_last_transaction();
1266    }
1267
1268    pub fn subscribe(&mut self) -> Subscription {
1269        self.subscriptions.subscribe()
1270    }
1271
1272    pub fn wait_for_edits(
1273        &mut self,
1274        edit_ids: impl IntoIterator<Item = clock::Local>,
1275    ) -> impl 'static + Future<Output = ()> {
1276        let mut futures = Vec::new();
1277        for edit_id in edit_ids {
1278            if !self.version.observed(edit_id) {
1279                let (tx, rx) = oneshot::channel();
1280                self.edit_id_resolvers.entry(edit_id).or_default().push(tx);
1281                futures.push(rx);
1282            }
1283        }
1284
1285        async move {
1286            for mut future in futures {
1287                future.recv().await;
1288            }
1289        }
1290    }
1291
1292    fn resolve_edit(&mut self, edit_id: clock::Local) {
1293        for mut tx in self
1294            .edit_id_resolvers
1295            .remove(&edit_id)
1296            .into_iter()
1297            .flatten()
1298        {
1299            let _ = tx.try_send(());
1300        }
1301    }
1302}
1303
1304#[cfg(any(test, feature = "test-support"))]
1305impl Buffer {
1306    pub fn check_invariants(&self) {
1307        // Ensure every fragment is ordered by locator in the fragment tree and corresponds
1308        // to an insertion fragment in the insertions tree.
1309        let mut prev_fragment_id = Locator::min();
1310        for fragment in self.snapshot.fragments.items(&None) {
1311            assert!(fragment.id > prev_fragment_id);
1312            prev_fragment_id = fragment.id.clone();
1313
1314            let insertion_fragment = self
1315                .snapshot
1316                .insertions
1317                .get(
1318                    &InsertionFragmentKey {
1319                        timestamp: fragment.insertion_timestamp.local(),
1320                        split_offset: fragment.insertion_offset,
1321                    },
1322                    &(),
1323                )
1324                .unwrap();
1325            assert_eq!(insertion_fragment.fragment_id, fragment.id);
1326        }
1327
1328        let mut cursor = self.snapshot.fragments.cursor::<Option<&Locator>>();
1329        for insertion_fragment in self.snapshot.insertions.cursor::<()>() {
1330            cursor.seek(&Some(&insertion_fragment.fragment_id), Bias::Left, &None);
1331            let fragment = cursor.item().unwrap();
1332            assert_eq!(insertion_fragment.fragment_id, fragment.id);
1333            assert_eq!(insertion_fragment.split_offset, fragment.insertion_offset);
1334        }
1335
1336        let fragment_summary = self.snapshot.fragments.summary();
1337        assert_eq!(
1338            fragment_summary.text.visible,
1339            self.snapshot.visible_text.len()
1340        );
1341        assert_eq!(
1342            fragment_summary.text.deleted,
1343            self.snapshot.deleted_text.len()
1344        );
1345    }
1346
1347    pub fn set_group_interval(&mut self, group_interval: Duration) {
1348        self.history.group_interval = group_interval;
1349    }
1350
1351    pub fn random_byte_range(&self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
1352        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
1353        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
1354        start..end
1355    }
1356
1357    pub fn randomly_edit<T>(
1358        &mut self,
1359        rng: &mut T,
1360        old_range_count: usize,
1361    ) -> (Vec<Range<usize>>, String, Operation)
1362    where
1363        T: rand::Rng,
1364    {
1365        let mut old_ranges: Vec<Range<usize>> = Vec::new();
1366        for _ in 0..old_range_count {
1367            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
1368            if last_end > self.len() {
1369                break;
1370            }
1371            old_ranges.push(self.random_byte_range(last_end, rng));
1372        }
1373        let new_text_len = rng.gen_range(0..10);
1374        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1375            .take(new_text_len)
1376            .collect();
1377        log::info!(
1378            "mutating buffer {} at {:?}: {:?}",
1379            self.replica_id,
1380            old_ranges,
1381            new_text
1382        );
1383        let op = self.edit(old_ranges.iter().cloned(), new_text.as_str());
1384        (old_ranges, new_text, op)
1385    }
1386
1387    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
1388        use rand::prelude::*;
1389
1390        let mut ops = Vec::new();
1391        for _ in 0..rng.gen_range(1..=5) {
1392            if let Some(entry) = self.history.undo_stack.choose(rng) {
1393                let transaction = entry.transaction.clone();
1394                log::info!(
1395                    "undoing buffer {} transaction {:?}",
1396                    self.replica_id,
1397                    transaction
1398                );
1399                ops.push(self.undo_or_redo(transaction).unwrap());
1400            }
1401        }
1402        ops
1403    }
1404}
1405
1406impl Deref for Buffer {
1407    type Target = BufferSnapshot;
1408
1409    fn deref(&self) -> &Self::Target {
1410        &self.snapshot
1411    }
1412}
1413
1414impl BufferSnapshot {
1415    pub fn as_rope(&self) -> &Rope {
1416        &self.visible_text
1417    }
1418
1419    pub fn replica_id(&self) -> ReplicaId {
1420        self.replica_id
1421    }
1422
1423    pub fn row_count(&self) -> u32 {
1424        self.max_point().row + 1
1425    }
1426
1427    pub fn len(&self) -> usize {
1428        self.visible_text.len()
1429    }
1430
1431    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1432        self.chars_at(0)
1433    }
1434
1435    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1436        self.text_for_range(range).flat_map(str::chars)
1437    }
1438
1439    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1440    where
1441        T: ToOffset,
1442    {
1443        let position = position.to_offset(self);
1444        position == self.clip_offset(position, Bias::Left)
1445            && self
1446                .bytes_in_range(position..self.len())
1447                .flatten()
1448                .copied()
1449                .take(needle.len())
1450                .eq(needle.bytes())
1451    }
1452
1453    pub fn text(&self) -> String {
1454        self.visible_text.to_string()
1455    }
1456
1457    pub fn deleted_text(&self) -> String {
1458        self.deleted_text.to_string()
1459    }
1460
1461    pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
1462        self.fragments.iter()
1463    }
1464
1465    pub fn text_summary(&self) -> TextSummary {
1466        self.visible_text.summary()
1467    }
1468
1469    pub fn max_point(&self) -> Point {
1470        self.visible_text.max_point()
1471    }
1472
1473    pub fn point_to_offset(&self, point: Point) -> usize {
1474        self.visible_text.point_to_offset(point)
1475    }
1476
1477    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
1478        self.visible_text.point_utf16_to_offset(point)
1479    }
1480
1481    pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
1482        self.visible_text.point_utf16_to_point(point)
1483    }
1484
1485    pub fn offset_to_point(&self, offset: usize) -> Point {
1486        self.visible_text.offset_to_point(offset)
1487    }
1488
1489    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
1490        self.visible_text.offset_to_point_utf16(offset)
1491    }
1492
1493    pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
1494        self.visible_text.point_to_point_utf16(point)
1495    }
1496
1497    pub fn version(&self) -> &clock::Global {
1498        &self.version
1499    }
1500
1501    pub fn chars_at<'a, T: ToOffset>(&'a self, position: T) -> impl Iterator<Item = char> + 'a {
1502        let offset = position.to_offset(self);
1503        self.visible_text.chars_at(offset)
1504    }
1505
1506    pub fn reversed_chars_at<'a, T: ToOffset>(
1507        &'a self,
1508        position: T,
1509    ) -> impl Iterator<Item = char> + 'a {
1510        let offset = position.to_offset(self);
1511        self.visible_text.reversed_chars_at(offset)
1512    }
1513
1514    pub fn reversed_chunks_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Chunks {
1515        let range = range.start.to_offset(self)..range.end.to_offset(self);
1516        self.visible_text.reversed_chunks_in_range(range)
1517    }
1518
1519    pub fn bytes_in_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> rope::Bytes<'a> {
1520        let start = range.start.to_offset(self);
1521        let end = range.end.to_offset(self);
1522        self.visible_text.bytes_in_range(start..end)
1523    }
1524
1525    pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
1526        let start = range.start.to_offset(self);
1527        let end = range.end.to_offset(self);
1528        self.visible_text.chunks_in_range(start..end)
1529    }
1530
1531    pub fn line_len(&self, row: u32) -> u32 {
1532        let row_start_offset = Point::new(row, 0).to_offset(self);
1533        let row_end_offset = if row >= self.max_point().row {
1534            self.len()
1535        } else {
1536            Point::new(row + 1, 0).to_offset(self) - 1
1537        };
1538        (row_end_offset - row_start_offset) as u32
1539    }
1540
1541    pub fn is_line_blank(&self, row: u32) -> bool {
1542        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1543            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1544    }
1545
1546    pub fn indent_column_for_line(&self, row: u32) -> u32 {
1547        let mut result = 0;
1548        for c in self.chars_at(Point::new(row, 0)) {
1549            if c == ' ' {
1550                result += 1;
1551            } else {
1552                break;
1553            }
1554        }
1555        result
1556    }
1557
1558    pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
1559    where
1560        D: TextDimension,
1561    {
1562        self.visible_text
1563            .cursor(range.start.to_offset(self))
1564            .summary(range.end.to_offset(self))
1565    }
1566
1567    pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
1568    where
1569        D: 'a + TextDimension,
1570        A: 'a + IntoIterator<Item = &'a Anchor>,
1571    {
1572        let anchors = anchors.into_iter();
1573        let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1574        let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1575        let mut text_cursor = self.visible_text.cursor(0);
1576        let mut position = D::default();
1577
1578        anchors.map(move |anchor| {
1579            if *anchor == Anchor::min() {
1580                return D::default();
1581            } else if *anchor == Anchor::max() {
1582                return D::from_text_summary(&self.visible_text.summary());
1583            }
1584
1585            let anchor_key = InsertionFragmentKey {
1586                timestamp: anchor.timestamp,
1587                split_offset: anchor.offset,
1588            };
1589            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1590            if let Some(insertion) = insertion_cursor.item() {
1591                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1592                if comparison == Ordering::Greater
1593                    || (anchor.bias == Bias::Left
1594                        && comparison == Ordering::Equal
1595                        && anchor.offset > 0)
1596                {
1597                    insertion_cursor.prev(&());
1598                }
1599            } else {
1600                insertion_cursor.prev(&());
1601            }
1602            let insertion = insertion_cursor.item().expect("invalid insertion");
1603            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1604
1605            fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left, &None);
1606            let fragment = fragment_cursor.item().unwrap();
1607            let mut fragment_offset = fragment_cursor.start().1;
1608            if fragment.visible {
1609                fragment_offset += anchor.offset - insertion.split_offset;
1610            }
1611
1612            position.add_assign(&text_cursor.summary(fragment_offset));
1613            position.clone()
1614        })
1615    }
1616
1617    fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D
1618    where
1619        D: TextDimension,
1620    {
1621        if *anchor == Anchor::min() {
1622            D::default()
1623        } else if *anchor == Anchor::max() {
1624            D::from_text_summary(&self.visible_text.summary())
1625        } else {
1626            let anchor_key = InsertionFragmentKey {
1627                timestamp: anchor.timestamp,
1628                split_offset: anchor.offset,
1629            };
1630            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1631            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1632            if let Some(insertion) = insertion_cursor.item() {
1633                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1634                if comparison == Ordering::Greater
1635                    || (anchor.bias == Bias::Left
1636                        && comparison == Ordering::Equal
1637                        && anchor.offset > 0)
1638                {
1639                    insertion_cursor.prev(&());
1640                }
1641            } else {
1642                insertion_cursor.prev(&());
1643            }
1644            let insertion = insertion_cursor.item().expect("invalid insertion");
1645            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1646
1647            let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1648            fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None);
1649            let fragment = fragment_cursor.item().unwrap();
1650            let mut fragment_offset = fragment_cursor.start().1;
1651            if fragment.visible {
1652                fragment_offset += anchor.offset - insertion.split_offset;
1653            }
1654            self.text_summary_for_range(0..fragment_offset)
1655        }
1656    }
1657
1658    fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
1659        if *anchor == Anchor::min() {
1660            &locator::MIN
1661        } else if *anchor == Anchor::max() {
1662            &locator::MAX
1663        } else {
1664            let anchor_key = InsertionFragmentKey {
1665                timestamp: anchor.timestamp,
1666                split_offset: anchor.offset,
1667            };
1668            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1669            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1670            if let Some(insertion) = insertion_cursor.item() {
1671                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1672                if comparison == Ordering::Greater
1673                    || (anchor.bias == Bias::Left
1674                        && comparison == Ordering::Equal
1675                        && anchor.offset > 0)
1676                {
1677                    insertion_cursor.prev(&());
1678                }
1679            } else {
1680                insertion_cursor.prev(&());
1681            }
1682            let insertion = insertion_cursor.item().expect("invalid insertion");
1683            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1684            &insertion.fragment_id
1685        }
1686    }
1687
1688    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
1689        self.anchor_at(position, Bias::Left)
1690    }
1691
1692    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
1693        self.anchor_at(position, Bias::Right)
1694    }
1695
1696    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
1697        let offset = position.to_offset(self);
1698        if bias == Bias::Left && offset == 0 {
1699            Anchor::min()
1700        } else if bias == Bias::Right && offset == self.len() {
1701            Anchor::max()
1702        } else {
1703            let mut fragment_cursor = self.fragments.cursor::<usize>();
1704            fragment_cursor.seek(&offset, bias, &None);
1705            let fragment = fragment_cursor.item().unwrap();
1706            let overshoot = offset - *fragment_cursor.start();
1707            Anchor {
1708                timestamp: fragment.insertion_timestamp.local(),
1709                offset: fragment.insertion_offset + overshoot,
1710                bias,
1711            }
1712        }
1713    }
1714
1715    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
1716        *anchor == Anchor::min()
1717            || *anchor == Anchor::max()
1718            || self.version.observed(anchor.timestamp)
1719    }
1720
1721    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1722        self.visible_text.clip_offset(offset, bias)
1723    }
1724
1725    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1726        self.visible_text.clip_point(point, bias)
1727    }
1728
1729    pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
1730        self.visible_text.clip_point_utf16(point, bias)
1731    }
1732
1733    pub fn edits_since<'a, D>(
1734        &'a self,
1735        since: &'a clock::Global,
1736    ) -> impl 'a + Iterator<Item = Edit<D>>
1737    where
1738        D: TextDimension + Ord,
1739    {
1740        self.edits_since_in_range(since, Anchor::min()..Anchor::max())
1741    }
1742
1743    pub fn edited_ranges_for_transaction<'a, D>(
1744        &'a self,
1745        transaction: &'a Transaction,
1746    ) -> impl 'a + Iterator<Item = Range<D>>
1747    where
1748        D: TextDimension,
1749    {
1750        let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>();
1751        let mut rope_cursor = self.visible_text.cursor(0);
1752        let cx = Some(transaction.end.clone());
1753        let mut position = D::default();
1754        transaction.ranges.iter().map(move |range| {
1755            cursor.seek_forward(&VersionedFullOffset::Offset(range.start), Bias::Right, &cx);
1756            let mut start_offset = cursor.start().1;
1757            if cursor
1758                .item()
1759                .map_or(false, |fragment| fragment.is_visible(&self.undo_map))
1760            {
1761                start_offset += range.start - cursor.start().0.full_offset()
1762            }
1763            position.add_assign(&rope_cursor.summary(start_offset));
1764            let start = position.clone();
1765
1766            cursor.seek_forward(&VersionedFullOffset::Offset(range.end), Bias::Left, &cx);
1767            let mut end_offset = cursor.start().1;
1768            if cursor
1769                .item()
1770                .map_or(false, |fragment| fragment.is_visible(&self.undo_map))
1771            {
1772                end_offset += range.end - cursor.start().0.full_offset();
1773            }
1774            position.add_assign(&rope_cursor.summary(end_offset));
1775            start..position.clone()
1776        })
1777    }
1778
1779    pub fn edits_since_in_range<'a, D>(
1780        &'a self,
1781        since: &'a clock::Global,
1782        range: Range<Anchor>,
1783    ) -> impl 'a + Iterator<Item = Edit<D>>
1784    where
1785        D: TextDimension + Ord,
1786    {
1787        let fragments_cursor = if *since == self.version {
1788            None
1789        } else {
1790            Some(self.fragments.filter(
1791                move |summary| !since.observed_all(&summary.max_version),
1792                &None,
1793            ))
1794        };
1795        let mut cursor = self
1796            .fragments
1797            .cursor::<(Option<&Locator>, FragmentTextSummary)>();
1798
1799        let start_fragment_id = self.fragment_id_for_anchor(&range.start);
1800        cursor.seek(&Some(start_fragment_id), Bias::Left, &None);
1801        let mut visible_start = cursor.start().1.visible;
1802        let mut deleted_start = cursor.start().1.deleted;
1803        if let Some(fragment) = cursor.item() {
1804            let overshoot = range.start.offset - fragment.insertion_offset;
1805            if fragment.visible {
1806                visible_start += overshoot;
1807            } else {
1808                deleted_start += overshoot;
1809            }
1810        }
1811        let end_fragment_id = self.fragment_id_for_anchor(&range.end);
1812
1813        Edits {
1814            visible_cursor: self.visible_text.cursor(visible_start),
1815            deleted_cursor: self.deleted_text.cursor(deleted_start),
1816            fragments_cursor,
1817            undos: &self.undo_map,
1818            since,
1819            old_end: Default::default(),
1820            new_end: Default::default(),
1821            range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
1822        }
1823    }
1824}
1825
1826struct RopeBuilder<'a> {
1827    old_visible_cursor: rope::Cursor<'a>,
1828    old_deleted_cursor: rope::Cursor<'a>,
1829    new_visible: Rope,
1830    new_deleted: Rope,
1831}
1832
1833impl<'a> RopeBuilder<'a> {
1834    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
1835        Self {
1836            old_visible_cursor,
1837            old_deleted_cursor,
1838            new_visible: Rope::new(),
1839            new_deleted: Rope::new(),
1840        }
1841    }
1842
1843    fn push_tree(&mut self, len: FragmentTextSummary) {
1844        self.push(len.visible, true, true);
1845        self.push(len.deleted, false, false);
1846    }
1847
1848    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
1849        debug_assert!(fragment.len > 0);
1850        self.push(fragment.len, was_visible, fragment.visible)
1851    }
1852
1853    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
1854        let text = if was_visible {
1855            self.old_visible_cursor
1856                .slice(self.old_visible_cursor.offset() + len)
1857        } else {
1858            self.old_deleted_cursor
1859                .slice(self.old_deleted_cursor.offset() + len)
1860        };
1861        if is_visible {
1862            self.new_visible.append(text);
1863        } else {
1864            self.new_deleted.append(text);
1865        }
1866    }
1867
1868    fn push_str(&mut self, text: &str) {
1869        self.new_visible.push(text);
1870    }
1871
1872    fn finish(mut self) -> (Rope, Rope) {
1873        self.new_visible.append(self.old_visible_cursor.suffix());
1874        self.new_deleted.append(self.old_deleted_cursor.suffix());
1875        (self.new_visible, self.new_deleted)
1876    }
1877}
1878
1879impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
1880    type Item = Edit<D>;
1881
1882    fn next(&mut self) -> Option<Self::Item> {
1883        let mut pending_edit: Option<Edit<D>> = None;
1884        let cursor = self.fragments_cursor.as_mut()?;
1885
1886        while let Some(fragment) = cursor.item() {
1887            if fragment.id < *self.range.start.0 {
1888                cursor.next(&None);
1889                continue;
1890            } else if fragment.id > *self.range.end.0 {
1891                break;
1892            }
1893
1894            if cursor.start().visible > self.visible_cursor.offset() {
1895                let summary = self.visible_cursor.summary(cursor.start().visible);
1896                self.old_end.add_assign(&summary);
1897                self.new_end.add_assign(&summary);
1898            }
1899
1900            if pending_edit
1901                .as_ref()
1902                .map_or(false, |change| change.new.end < self.new_end)
1903            {
1904                break;
1905            }
1906
1907            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
1908                let mut visible_end = cursor.end(&None).visible;
1909                if fragment.id == *self.range.end.0 {
1910                    visible_end = cmp::min(
1911                        visible_end,
1912                        cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
1913                    );
1914                }
1915
1916                let fragment_summary = self.visible_cursor.summary(visible_end);
1917                let mut new_end = self.new_end.clone();
1918                new_end.add_assign(&fragment_summary);
1919                if let Some(pending_edit) = pending_edit.as_mut() {
1920                    pending_edit.new.end = new_end.clone();
1921                } else {
1922                    pending_edit = Some(Edit {
1923                        old: self.old_end.clone()..self.old_end.clone(),
1924                        new: self.new_end.clone()..new_end.clone(),
1925                    });
1926                }
1927
1928                self.new_end = new_end;
1929            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
1930                let mut deleted_end = cursor.end(&None).deleted;
1931                if fragment.id == *self.range.end.0 {
1932                    deleted_end = cmp::min(
1933                        deleted_end,
1934                        cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
1935                    );
1936                }
1937
1938                if cursor.start().deleted > self.deleted_cursor.offset() {
1939                    self.deleted_cursor.seek_forward(cursor.start().deleted);
1940                }
1941                let fragment_summary = self.deleted_cursor.summary(deleted_end);
1942                let mut old_end = self.old_end.clone();
1943                old_end.add_assign(&fragment_summary);
1944                if let Some(pending_edit) = pending_edit.as_mut() {
1945                    pending_edit.old.end = old_end.clone();
1946                } else {
1947                    pending_edit = Some(Edit {
1948                        old: self.old_end.clone()..old_end.clone(),
1949                        new: self.new_end.clone()..self.new_end.clone(),
1950                    });
1951                }
1952
1953                self.old_end = old_end;
1954            }
1955
1956            cursor.next(&None);
1957        }
1958
1959        pending_edit
1960    }
1961}
1962
1963impl Fragment {
1964    fn is_visible(&self, undos: &UndoMap) -> bool {
1965        !undos.is_undone(self.insertion_timestamp.local())
1966            && self.deletions.iter().all(|d| undos.is_undone(*d))
1967    }
1968
1969    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
1970        (version.observed(self.insertion_timestamp.local())
1971            && !undos.was_undone(self.insertion_timestamp.local(), version))
1972            && self
1973                .deletions
1974                .iter()
1975                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
1976    }
1977}
1978
1979impl sum_tree::Item for Fragment {
1980    type Summary = FragmentSummary;
1981
1982    fn summary(&self) -> Self::Summary {
1983        let mut max_version = clock::Global::new();
1984        max_version.observe(self.insertion_timestamp.local());
1985        for deletion in &self.deletions {
1986            max_version.observe(*deletion);
1987        }
1988        max_version.join(&self.max_undos);
1989
1990        let mut min_insertion_version = clock::Global::new();
1991        min_insertion_version.observe(self.insertion_timestamp.local());
1992        let max_insertion_version = min_insertion_version.clone();
1993        if self.visible {
1994            FragmentSummary {
1995                max_id: self.id.clone(),
1996                text: FragmentTextSummary {
1997                    visible: self.len,
1998                    deleted: 0,
1999                },
2000                max_version,
2001                min_insertion_version,
2002                max_insertion_version,
2003            }
2004        } else {
2005            FragmentSummary {
2006                max_id: self.id.clone(),
2007                text: FragmentTextSummary {
2008                    visible: 0,
2009                    deleted: self.len,
2010                },
2011                max_version,
2012                min_insertion_version,
2013                max_insertion_version,
2014            }
2015        }
2016    }
2017}
2018
2019impl sum_tree::Summary for FragmentSummary {
2020    type Context = Option<clock::Global>;
2021
2022    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
2023        self.max_id.assign(&other.max_id);
2024        self.text.visible += &other.text.visible;
2025        self.text.deleted += &other.text.deleted;
2026        self.max_version.join(&other.max_version);
2027        self.min_insertion_version
2028            .meet(&other.min_insertion_version);
2029        self.max_insertion_version
2030            .join(&other.max_insertion_version);
2031    }
2032}
2033
2034impl Default for FragmentSummary {
2035    fn default() -> Self {
2036        FragmentSummary {
2037            max_id: Locator::min(),
2038            text: FragmentTextSummary::default(),
2039            max_version: clock::Global::new(),
2040            min_insertion_version: clock::Global::new(),
2041            max_insertion_version: clock::Global::new(),
2042        }
2043    }
2044}
2045
2046impl sum_tree::Item for InsertionFragment {
2047    type Summary = InsertionFragmentKey;
2048
2049    fn summary(&self) -> Self::Summary {
2050        InsertionFragmentKey {
2051            timestamp: self.timestamp,
2052            split_offset: self.split_offset,
2053        }
2054    }
2055}
2056
2057impl sum_tree::KeyedItem for InsertionFragment {
2058    type Key = InsertionFragmentKey;
2059
2060    fn key(&self) -> Self::Key {
2061        sum_tree::Item::summary(self)
2062    }
2063}
2064
2065impl InsertionFragment {
2066    fn new(fragment: &Fragment) -> Self {
2067        Self {
2068            timestamp: fragment.insertion_timestamp.local(),
2069            split_offset: fragment.insertion_offset,
2070            fragment_id: fragment.id.clone(),
2071        }
2072    }
2073
2074    fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
2075        sum_tree::Edit::Insert(Self::new(fragment))
2076    }
2077}
2078
2079impl sum_tree::Summary for InsertionFragmentKey {
2080    type Context = ();
2081
2082    fn add_summary(&mut self, summary: &Self, _: &()) {
2083        *self = *summary;
2084    }
2085}
2086
2087#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
2088pub struct FullOffset(pub usize);
2089
2090impl ops::AddAssign<usize> for FullOffset {
2091    fn add_assign(&mut self, rhs: usize) {
2092        self.0 += rhs;
2093    }
2094}
2095
2096impl ops::Add<usize> for FullOffset {
2097    type Output = Self;
2098
2099    fn add(mut self, rhs: usize) -> Self::Output {
2100        self += rhs;
2101        self
2102    }
2103}
2104
2105impl ops::Sub for FullOffset {
2106    type Output = usize;
2107
2108    fn sub(self, rhs: Self) -> Self::Output {
2109        self.0 - rhs.0
2110    }
2111}
2112
2113impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
2114    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2115        *self += summary.text.visible;
2116    }
2117}
2118
2119impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
2120    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2121        self.0 += summary.text.visible + summary.text.deleted;
2122    }
2123}
2124
2125impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
2126    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
2127        *self = Some(&summary.max_id);
2128    }
2129}
2130
2131impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
2132    fn cmp(
2133        &self,
2134        cursor_location: &FragmentTextSummary,
2135        _: &Option<clock::Global>,
2136    ) -> cmp::Ordering {
2137        Ord::cmp(self, &cursor_location.visible)
2138    }
2139}
2140
2141#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2142enum VersionedFullOffset {
2143    Offset(FullOffset),
2144    Invalid,
2145}
2146
2147impl VersionedFullOffset {
2148    fn full_offset(&self) -> FullOffset {
2149        if let Self::Offset(position) = self {
2150            *position
2151        } else {
2152            panic!("invalid version")
2153        }
2154    }
2155}
2156
2157impl Default for VersionedFullOffset {
2158    fn default() -> Self {
2159        Self::Offset(Default::default())
2160    }
2161}
2162
2163impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
2164    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
2165        if let Self::Offset(offset) = self {
2166            let version = cx.as_ref().unwrap();
2167            if version.observed_all(&summary.max_insertion_version) {
2168                *offset += summary.text.visible + summary.text.deleted;
2169            } else if version.observed_any(&summary.min_insertion_version) {
2170                *self = Self::Invalid;
2171            }
2172        }
2173    }
2174}
2175
2176impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedFullOffset {
2177    fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
2178        match (self, cursor_position) {
2179            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
2180            (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
2181            (Self::Invalid, _) => unreachable!(),
2182        }
2183    }
2184}
2185
2186impl Operation {
2187    fn replica_id(&self) -> ReplicaId {
2188        operation_queue::Operation::lamport_timestamp(self).replica_id
2189    }
2190
2191    pub fn local_timestamp(&self) -> clock::Local {
2192        match self {
2193            Operation::Edit(edit) => edit.timestamp.local(),
2194            Operation::Undo { undo, .. } => undo.id,
2195        }
2196    }
2197
2198    pub fn as_edit(&self) -> Option<&EditOperation> {
2199        match self {
2200            Operation::Edit(edit) => Some(edit),
2201            _ => None,
2202        }
2203    }
2204
2205    pub fn is_edit(&self) -> bool {
2206        match self {
2207            Operation::Edit { .. } => true,
2208            _ => false,
2209        }
2210    }
2211}
2212
2213impl operation_queue::Operation for Operation {
2214    fn lamport_timestamp(&self) -> clock::Lamport {
2215        match self {
2216            Operation::Edit(edit) => edit.timestamp.lamport(),
2217            Operation::Undo {
2218                lamport_timestamp, ..
2219            } => *lamport_timestamp,
2220        }
2221    }
2222}
2223
2224pub trait ToOffset {
2225    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize;
2226}
2227
2228impl ToOffset for Point {
2229    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2230        snapshot.point_to_offset(*self)
2231    }
2232}
2233
2234impl ToOffset for PointUtf16 {
2235    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2236        snapshot.point_utf16_to_offset(*self)
2237    }
2238}
2239
2240impl ToOffset for usize {
2241    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2242        assert!(*self <= snapshot.len(), "offset is out of range");
2243        *self
2244    }
2245}
2246
2247impl ToOffset for Anchor {
2248    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2249        snapshot.summary_for_anchor(self)
2250    }
2251}
2252
2253impl<'a, T: ToOffset> ToOffset for &'a T {
2254    fn to_offset(&self, content: &BufferSnapshot) -> usize {
2255        (*self).to_offset(content)
2256    }
2257}
2258
2259pub trait ToPoint {
2260    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point;
2261}
2262
2263impl ToPoint for Anchor {
2264    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2265        snapshot.summary_for_anchor(self)
2266    }
2267}
2268
2269impl ToPoint for usize {
2270    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2271        snapshot.offset_to_point(*self)
2272    }
2273}
2274
2275impl ToPoint for PointUtf16 {
2276    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2277        snapshot.point_utf16_to_point(*self)
2278    }
2279}
2280
2281impl ToPoint for Point {
2282    fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
2283        *self
2284    }
2285}
2286
2287pub trait ToPointUtf16 {
2288    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16;
2289}
2290
2291impl ToPointUtf16 for Anchor {
2292    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2293        snapshot.summary_for_anchor(self)
2294    }
2295}
2296
2297impl ToPointUtf16 for usize {
2298    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2299        snapshot.offset_to_point_utf16(*self)
2300    }
2301}
2302
2303impl ToPointUtf16 for PointUtf16 {
2304    fn to_point_utf16<'a>(&self, _: &BufferSnapshot) -> PointUtf16 {
2305        *self
2306    }
2307}
2308
2309impl ToPointUtf16 for Point {
2310    fn to_point_utf16<'a>(&self, snapshot: &BufferSnapshot) -> PointUtf16 {
2311        snapshot.point_to_point_utf16(*self)
2312    }
2313}
2314
2315pub trait Clip {
2316    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self;
2317}
2318
2319impl Clip for usize {
2320    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2321        snapshot.clip_offset(*self, bias)
2322    }
2323}
2324
2325impl Clip for Point {
2326    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2327        snapshot.clip_point(*self, bias)
2328    }
2329}
2330
2331impl Clip for PointUtf16 {
2332    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2333        snapshot.clip_point_utf16(*self, bias)
2334    }
2335}
2336
2337pub trait FromAnchor {
2338    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
2339}
2340
2341impl FromAnchor for Point {
2342    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2343        snapshot.summary_for_anchor(anchor)
2344    }
2345}
2346
2347impl FromAnchor for PointUtf16 {
2348    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2349        snapshot.summary_for_anchor(anchor)
2350    }
2351}
2352
2353impl FromAnchor for usize {
2354    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2355        snapshot.summary_for_anchor(anchor)
2356    }
2357}