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