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