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