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 version(&self) -> &clock::Global {
1516        &self.version
1517    }
1518
1519    pub fn chars_at<'a, T: ToOffset>(&'a self, position: T) -> impl Iterator<Item = char> + 'a {
1520        let offset = position.to_offset(self);
1521        self.visible_text.chars_at(offset)
1522    }
1523
1524    pub fn reversed_chars_at<'a, T: ToOffset>(
1525        &'a self,
1526        position: T,
1527    ) -> impl Iterator<Item = char> + 'a {
1528        let offset = position.to_offset(self);
1529        self.visible_text.reversed_chars_at(offset)
1530    }
1531
1532    pub fn reversed_chunks_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Chunks {
1533        let range = range.start.to_offset(self)..range.end.to_offset(self);
1534        self.visible_text.reversed_chunks_in_range(range)
1535    }
1536
1537    pub fn bytes_in_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> rope::Bytes<'a> {
1538        let start = range.start.to_offset(self);
1539        let end = range.end.to_offset(self);
1540        self.visible_text.bytes_in_range(start..end)
1541    }
1542
1543    pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
1544        let start = range.start.to_offset(self);
1545        let end = range.end.to_offset(self);
1546        self.visible_text.chunks_in_range(start..end)
1547    }
1548
1549    pub fn line_len(&self, row: u32) -> u32 {
1550        let row_start_offset = Point::new(row, 0).to_offset(self);
1551        let row_end_offset = if row >= self.max_point().row {
1552            self.len()
1553        } else {
1554            Point::new(row + 1, 0).to_offset(self) - 1
1555        };
1556        (row_end_offset - row_start_offset) as u32
1557    }
1558
1559    pub fn is_line_blank(&self, row: u32) -> bool {
1560        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1561            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1562    }
1563
1564    pub fn indent_column_for_line(&self, row: u32) -> u32 {
1565        let mut result = 0;
1566        for c in self.chars_at(Point::new(row, 0)) {
1567            if c == ' ' {
1568                result += 1;
1569            } else {
1570                break;
1571            }
1572        }
1573        result
1574    }
1575
1576    pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
1577    where
1578        D: TextDimension,
1579    {
1580        self.visible_text
1581            .cursor(range.start.to_offset(self))
1582            .summary(range.end.to_offset(self))
1583    }
1584
1585    pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
1586    where
1587        D: 'a + TextDimension,
1588        A: 'a + IntoIterator<Item = &'a Anchor>,
1589    {
1590        let anchors = anchors.into_iter();
1591        let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1592        let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1593        let mut text_cursor = self.visible_text.cursor(0);
1594        let mut position = D::default();
1595
1596        anchors.map(move |anchor| {
1597            if *anchor == Anchor::min() {
1598                return D::default();
1599            } else if *anchor == Anchor::max() {
1600                return D::from_text_summary(&self.visible_text.summary());
1601            }
1602
1603            let anchor_key = InsertionFragmentKey {
1604                timestamp: anchor.timestamp,
1605                split_offset: anchor.offset,
1606            };
1607            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1608            if let Some(insertion) = insertion_cursor.item() {
1609                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1610                if comparison == Ordering::Greater
1611                    || (anchor.bias == Bias::Left
1612                        && comparison == Ordering::Equal
1613                        && anchor.offset > 0)
1614                {
1615                    insertion_cursor.prev(&());
1616                }
1617            } else {
1618                insertion_cursor.prev(&());
1619            }
1620            let insertion = insertion_cursor.item().expect("invalid insertion");
1621            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1622
1623            fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left, &None);
1624            let fragment = fragment_cursor.item().unwrap();
1625            let mut fragment_offset = fragment_cursor.start().1;
1626            if fragment.visible {
1627                fragment_offset += anchor.offset - insertion.split_offset;
1628            }
1629
1630            position.add_assign(&text_cursor.summary(fragment_offset));
1631            position.clone()
1632        })
1633    }
1634
1635    fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D
1636    where
1637        D: TextDimension,
1638    {
1639        if *anchor == Anchor::min() {
1640            D::default()
1641        } else if *anchor == Anchor::max() {
1642            D::from_text_summary(&self.visible_text.summary())
1643        } else {
1644            let anchor_key = InsertionFragmentKey {
1645                timestamp: anchor.timestamp,
1646                split_offset: anchor.offset,
1647            };
1648            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1649            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1650            if let Some(insertion) = insertion_cursor.item() {
1651                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1652                if comparison == Ordering::Greater
1653                    || (anchor.bias == Bias::Left
1654                        && comparison == Ordering::Equal
1655                        && anchor.offset > 0)
1656                {
1657                    insertion_cursor.prev(&());
1658                }
1659            } else {
1660                insertion_cursor.prev(&());
1661            }
1662            let insertion = insertion_cursor.item().expect("invalid insertion");
1663            assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1664
1665            let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1666            fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None);
1667            let fragment = fragment_cursor.item().unwrap();
1668            let mut fragment_offset = fragment_cursor.start().1;
1669            if fragment.visible {
1670                fragment_offset += anchor.offset - insertion.split_offset;
1671            }
1672            self.text_summary_for_range(0..fragment_offset)
1673        }
1674    }
1675
1676    fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
1677        if *anchor == Anchor::min() {
1678            &locator::MIN
1679        } else if *anchor == Anchor::max() {
1680            &locator::MAX
1681        } else {
1682            let anchor_key = InsertionFragmentKey {
1683                timestamp: anchor.timestamp,
1684                split_offset: anchor.offset,
1685            };
1686            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1687            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1688            if let Some(insertion) = insertion_cursor.item() {
1689                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1690                if comparison == Ordering::Greater
1691                    || (anchor.bias == Bias::Left
1692                        && comparison == Ordering::Equal
1693                        && anchor.offset > 0)
1694                {
1695                    insertion_cursor.prev(&());
1696                }
1697            } else {
1698                insertion_cursor.prev(&());
1699            }
1700            let insertion = insertion_cursor.item().expect("invalid insertion");
1701            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1702            &insertion.fragment_id
1703        }
1704    }
1705
1706    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
1707        self.anchor_at(position, Bias::Left)
1708    }
1709
1710    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
1711        self.anchor_at(position, Bias::Right)
1712    }
1713
1714    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
1715        let offset = position.to_offset(self);
1716        if bias == Bias::Left && offset == 0 {
1717            Anchor::min()
1718        } else if bias == Bias::Right && offset == self.len() {
1719            Anchor::max()
1720        } else {
1721            let mut fragment_cursor = self.fragments.cursor::<usize>();
1722            fragment_cursor.seek(&offset, bias, &None);
1723            let fragment = fragment_cursor.item().unwrap();
1724            let overshoot = offset - *fragment_cursor.start();
1725            Anchor {
1726                timestamp: fragment.insertion_timestamp.local(),
1727                offset: fragment.insertion_offset + overshoot,
1728                bias,
1729            }
1730        }
1731    }
1732
1733    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
1734        *anchor == Anchor::min()
1735            || *anchor == Anchor::max()
1736            || self.version.observed(anchor.timestamp)
1737    }
1738
1739    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1740        self.visible_text.clip_offset(offset, bias)
1741    }
1742
1743    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1744        self.visible_text.clip_point(point, bias)
1745    }
1746
1747    pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
1748        self.visible_text.clip_point_utf16(point, bias)
1749    }
1750
1751    // pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
1752    //     if offset <= self.len() {
1753    //         Ok(self.text_summary_for_range(0..offset))
1754    //     } else {
1755    //         Err(anyhow!("offset out of bounds"))
1756    //     }
1757    // }
1758
1759    pub fn edits_since<'a, D>(
1760        &'a self,
1761        since: &'a clock::Global,
1762    ) -> impl 'a + Iterator<Item = Edit<D>>
1763    where
1764        D: TextDimension + Ord,
1765    {
1766        self.edits_since_in_range(since, Anchor::min()..Anchor::max())
1767    }
1768
1769    pub fn edits_since_in_range<'a, D>(
1770        &'a self,
1771        since: &'a clock::Global,
1772        range: Range<Anchor>,
1773    ) -> impl 'a + Iterator<Item = Edit<D>>
1774    where
1775        D: TextDimension + Ord,
1776    {
1777        let fragments_cursor = if *since == self.version {
1778            None
1779        } else {
1780            Some(self.fragments.filter(
1781                move |summary| !since.observed_all(&summary.max_version),
1782                &None,
1783            ))
1784        };
1785        let mut cursor = self
1786            .fragments
1787            .cursor::<(Option<&Locator>, FragmentTextSummary)>();
1788
1789        let start_fragment_id = self.fragment_id_for_anchor(&range.start);
1790        cursor.seek(&Some(start_fragment_id), Bias::Left, &None);
1791        let mut visible_start = cursor.start().1.visible;
1792        let mut deleted_start = cursor.start().1.deleted;
1793        if let Some(fragment) = cursor.item() {
1794            let overshoot = range.start.offset - fragment.insertion_offset;
1795            if fragment.visible {
1796                visible_start += overshoot;
1797            } else {
1798                deleted_start += overshoot;
1799            }
1800        }
1801        let end_fragment_id = self.fragment_id_for_anchor(&range.end);
1802
1803        Edits {
1804            visible_cursor: self.visible_text.cursor(visible_start),
1805            deleted_cursor: self.deleted_text.cursor(deleted_start),
1806            fragments_cursor,
1807            undos: &self.undo_map,
1808            since,
1809            old_end: Default::default(),
1810            new_end: Default::default(),
1811            range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
1812        }
1813    }
1814}
1815
1816struct RopeBuilder<'a> {
1817    old_visible_cursor: rope::Cursor<'a>,
1818    old_deleted_cursor: rope::Cursor<'a>,
1819    new_visible: Rope,
1820    new_deleted: Rope,
1821}
1822
1823impl<'a> RopeBuilder<'a> {
1824    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
1825        Self {
1826            old_visible_cursor,
1827            old_deleted_cursor,
1828            new_visible: Rope::new(),
1829            new_deleted: Rope::new(),
1830        }
1831    }
1832
1833    fn push_tree(&mut self, len: FragmentTextSummary) {
1834        self.push(len.visible, true, true);
1835        self.push(len.deleted, false, false);
1836    }
1837
1838    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
1839        debug_assert!(fragment.len > 0);
1840        self.push(fragment.len, was_visible, fragment.visible)
1841    }
1842
1843    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
1844        let text = if was_visible {
1845            self.old_visible_cursor
1846                .slice(self.old_visible_cursor.offset() + len)
1847        } else {
1848            self.old_deleted_cursor
1849                .slice(self.old_deleted_cursor.offset() + len)
1850        };
1851        if is_visible {
1852            self.new_visible.append(text);
1853        } else {
1854            self.new_deleted.append(text);
1855        }
1856    }
1857
1858    fn push_str(&mut self, text: &str) {
1859        self.new_visible.push(text);
1860    }
1861
1862    fn finish(mut self) -> (Rope, Rope) {
1863        self.new_visible.append(self.old_visible_cursor.suffix());
1864        self.new_deleted.append(self.old_deleted_cursor.suffix());
1865        (self.new_visible, self.new_deleted)
1866    }
1867}
1868
1869impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
1870    type Item = Edit<D>;
1871
1872    fn next(&mut self) -> Option<Self::Item> {
1873        let mut pending_edit: Option<Edit<D>> = None;
1874        let cursor = self.fragments_cursor.as_mut()?;
1875
1876        while let Some(fragment) = cursor.item() {
1877            if fragment.id < *self.range.start.0 {
1878                cursor.next(&None);
1879                continue;
1880            } else if fragment.id > *self.range.end.0 {
1881                break;
1882            }
1883
1884            if cursor.start().visible > self.visible_cursor.offset() {
1885                let summary = self.visible_cursor.summary(cursor.start().visible);
1886                self.old_end.add_assign(&summary);
1887                self.new_end.add_assign(&summary);
1888            }
1889
1890            if pending_edit
1891                .as_ref()
1892                .map_or(false, |change| change.new.end < self.new_end)
1893            {
1894                break;
1895            }
1896
1897            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
1898                let mut visible_end = cursor.end(&None).visible;
1899                if fragment.id == *self.range.end.0 {
1900                    visible_end = cmp::min(
1901                        visible_end,
1902                        cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
1903                    );
1904                }
1905
1906                let fragment_summary = self.visible_cursor.summary(visible_end);
1907                let mut new_end = self.new_end.clone();
1908                new_end.add_assign(&fragment_summary);
1909                if let Some(pending_edit) = pending_edit.as_mut() {
1910                    pending_edit.new.end = new_end.clone();
1911                } else {
1912                    pending_edit = Some(Edit {
1913                        old: self.old_end.clone()..self.old_end.clone(),
1914                        new: self.new_end.clone()..new_end.clone(),
1915                    });
1916                }
1917
1918                self.new_end = new_end;
1919            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
1920                let mut deleted_end = cursor.end(&None).deleted;
1921                if fragment.id == *self.range.end.0 {
1922                    deleted_end = cmp::min(
1923                        deleted_end,
1924                        cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
1925                    );
1926                }
1927
1928                if cursor.start().deleted > self.deleted_cursor.offset() {
1929                    self.deleted_cursor.seek_forward(cursor.start().deleted);
1930                }
1931                let fragment_summary = self.deleted_cursor.summary(deleted_end);
1932                let mut old_end = self.old_end.clone();
1933                old_end.add_assign(&fragment_summary);
1934                if let Some(pending_edit) = pending_edit.as_mut() {
1935                    pending_edit.old.end = old_end.clone();
1936                } else {
1937                    pending_edit = Some(Edit {
1938                        old: self.old_end.clone()..old_end.clone(),
1939                        new: self.new_end.clone()..self.new_end.clone(),
1940                    });
1941                }
1942
1943                self.old_end = old_end;
1944            }
1945
1946            cursor.next(&None);
1947        }
1948
1949        pending_edit
1950    }
1951}
1952
1953impl Fragment {
1954    fn is_visible(&self, undos: &UndoMap) -> bool {
1955        !undos.is_undone(self.insertion_timestamp.local())
1956            && self.deletions.iter().all(|d| undos.is_undone(*d))
1957    }
1958
1959    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
1960        (version.observed(self.insertion_timestamp.local())
1961            && !undos.was_undone(self.insertion_timestamp.local(), version))
1962            && self
1963                .deletions
1964                .iter()
1965                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
1966    }
1967}
1968
1969impl sum_tree::Item for Fragment {
1970    type Summary = FragmentSummary;
1971
1972    fn summary(&self) -> Self::Summary {
1973        let mut max_version = clock::Global::new();
1974        max_version.observe(self.insertion_timestamp.local());
1975        for deletion in &self.deletions {
1976            max_version.observe(*deletion);
1977        }
1978        max_version.join(&self.max_undos);
1979
1980        let mut min_insertion_version = clock::Global::new();
1981        min_insertion_version.observe(self.insertion_timestamp.local());
1982        let max_insertion_version = min_insertion_version.clone();
1983        if self.visible {
1984            FragmentSummary {
1985                max_id: self.id.clone(),
1986                text: FragmentTextSummary {
1987                    visible: self.len,
1988                    deleted: 0,
1989                },
1990                max_version,
1991                min_insertion_version,
1992                max_insertion_version,
1993            }
1994        } else {
1995            FragmentSummary {
1996                max_id: self.id.clone(),
1997                text: FragmentTextSummary {
1998                    visible: 0,
1999                    deleted: self.len,
2000                },
2001                max_version,
2002                min_insertion_version,
2003                max_insertion_version,
2004            }
2005        }
2006    }
2007}
2008
2009impl sum_tree::Summary for FragmentSummary {
2010    type Context = Option<clock::Global>;
2011
2012    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
2013        self.max_id.assign(&other.max_id);
2014        self.text.visible += &other.text.visible;
2015        self.text.deleted += &other.text.deleted;
2016        self.max_version.join(&other.max_version);
2017        self.min_insertion_version
2018            .meet(&other.min_insertion_version);
2019        self.max_insertion_version
2020            .join(&other.max_insertion_version);
2021    }
2022}
2023
2024impl Default for FragmentSummary {
2025    fn default() -> Self {
2026        FragmentSummary {
2027            max_id: Locator::min(),
2028            text: FragmentTextSummary::default(),
2029            max_version: clock::Global::new(),
2030            min_insertion_version: clock::Global::new(),
2031            max_insertion_version: clock::Global::new(),
2032        }
2033    }
2034}
2035
2036impl sum_tree::Item for InsertionFragment {
2037    type Summary = InsertionFragmentKey;
2038
2039    fn summary(&self) -> Self::Summary {
2040        InsertionFragmentKey {
2041            timestamp: self.timestamp,
2042            split_offset: self.split_offset,
2043        }
2044    }
2045}
2046
2047impl sum_tree::KeyedItem for InsertionFragment {
2048    type Key = InsertionFragmentKey;
2049
2050    fn key(&self) -> Self::Key {
2051        sum_tree::Item::summary(self)
2052    }
2053}
2054
2055impl InsertionFragment {
2056    fn new(fragment: &Fragment) -> Self {
2057        Self {
2058            timestamp: fragment.insertion_timestamp.local(),
2059            split_offset: fragment.insertion_offset,
2060            fragment_id: fragment.id.clone(),
2061        }
2062    }
2063
2064    fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
2065        sum_tree::Edit::Insert(Self::new(fragment))
2066    }
2067}
2068
2069impl sum_tree::Summary for InsertionFragmentKey {
2070    type Context = ();
2071
2072    fn add_summary(&mut self, summary: &Self, _: &()) {
2073        *self = *summary;
2074    }
2075}
2076
2077#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
2078pub struct FullOffset(pub usize);
2079
2080impl ops::AddAssign<usize> for FullOffset {
2081    fn add_assign(&mut self, rhs: usize) {
2082        self.0 += rhs;
2083    }
2084}
2085
2086impl ops::Add<usize> for FullOffset {
2087    type Output = Self;
2088
2089    fn add(mut self, rhs: usize) -> Self::Output {
2090        self += rhs;
2091        self
2092    }
2093}
2094
2095impl ops::Sub for FullOffset {
2096    type Output = usize;
2097
2098    fn sub(self, rhs: Self) -> Self::Output {
2099        self.0 - rhs.0
2100    }
2101}
2102
2103impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
2104    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2105        *self += summary.text.visible;
2106    }
2107}
2108
2109impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
2110    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
2111        self.0 += summary.text.visible + summary.text.deleted;
2112    }
2113}
2114
2115impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
2116    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
2117        *self = Some(&summary.max_id);
2118    }
2119}
2120
2121impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
2122    fn cmp(
2123        &self,
2124        cursor_location: &FragmentTextSummary,
2125        _: &Option<clock::Global>,
2126    ) -> cmp::Ordering {
2127        Ord::cmp(self, &cursor_location.visible)
2128    }
2129}
2130
2131#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2132enum VersionedFullOffset {
2133    Offset(FullOffset),
2134    Invalid,
2135}
2136
2137impl VersionedFullOffset {
2138    fn full_offset(&self) -> FullOffset {
2139        if let Self::Offset(position) = self {
2140            *position
2141        } else {
2142            panic!("invalid version")
2143        }
2144    }
2145}
2146
2147impl Default for VersionedFullOffset {
2148    fn default() -> Self {
2149        Self::Offset(Default::default())
2150    }
2151}
2152
2153impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
2154    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
2155        if let Self::Offset(offset) = self {
2156            let version = cx.as_ref().unwrap();
2157            if version.observed_all(&summary.max_insertion_version) {
2158                *offset += summary.text.visible + summary.text.deleted;
2159            } else if version.observed_any(&summary.min_insertion_version) {
2160                *self = Self::Invalid;
2161            }
2162        }
2163    }
2164}
2165
2166impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedFullOffset {
2167    fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
2168        match (self, cursor_position) {
2169            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
2170            (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
2171            (Self::Invalid, _) => unreachable!(),
2172        }
2173    }
2174}
2175
2176impl Operation {
2177    fn replica_id(&self) -> ReplicaId {
2178        operation_queue::Operation::lamport_timestamp(self).replica_id
2179    }
2180
2181    pub fn is_edit(&self) -> bool {
2182        match self {
2183            Operation::Edit { .. } => true,
2184            _ => false,
2185        }
2186    }
2187}
2188
2189impl operation_queue::Operation for Operation {
2190    fn lamport_timestamp(&self) -> clock::Lamport {
2191        match self {
2192            Operation::Edit(edit) => edit.timestamp.lamport(),
2193            Operation::Undo {
2194                lamport_timestamp, ..
2195            } => *lamport_timestamp,
2196        }
2197    }
2198}
2199
2200pub trait ToOffset {
2201    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize;
2202}
2203
2204impl ToOffset for Point {
2205    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2206        snapshot.point_to_offset(*self)
2207    }
2208}
2209
2210impl ToOffset for PointUtf16 {
2211    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2212        snapshot.point_utf16_to_offset(*self)
2213    }
2214}
2215
2216impl ToOffset for usize {
2217    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2218        assert!(*self <= snapshot.len(), "offset is out of range");
2219        *self
2220    }
2221}
2222
2223impl ToOffset for Anchor {
2224    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2225        snapshot.summary_for_anchor(self)
2226    }
2227}
2228
2229impl<'a, T: ToOffset> ToOffset for &'a T {
2230    fn to_offset(&self, content: &BufferSnapshot) -> usize {
2231        (*self).to_offset(content)
2232    }
2233}
2234
2235pub trait ToPoint {
2236    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point;
2237}
2238
2239impl ToPoint for Anchor {
2240    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2241        snapshot.summary_for_anchor(self)
2242    }
2243}
2244
2245impl ToPoint for usize {
2246    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2247        snapshot.offset_to_point(*self)
2248    }
2249}
2250
2251impl ToPoint for PointUtf16 {
2252    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2253        snapshot.point_utf16_to_point(*self)
2254    }
2255}
2256
2257impl ToPoint for Point {
2258    fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
2259        *self
2260    }
2261}
2262
2263pub trait Clip {
2264    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self;
2265}
2266
2267impl Clip for usize {
2268    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2269        snapshot.clip_offset(*self, bias)
2270    }
2271}
2272
2273impl Clip for Point {
2274    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2275        snapshot.clip_point(*self, bias)
2276    }
2277}
2278
2279impl Clip for PointUtf16 {
2280    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2281        snapshot.clip_point_utf16(*self, bias)
2282    }
2283}
2284
2285pub trait FromAnchor {
2286    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
2287}
2288
2289impl FromAnchor for Point {
2290    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2291        snapshot.summary_for_anchor(anchor)
2292    }
2293}
2294
2295impl FromAnchor for PointUtf16 {
2296    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2297        snapshot.summary_for_anchor(anchor)
2298    }
2299}
2300
2301impl FromAnchor for usize {
2302    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2303        snapshot.summary_for_anchor(anchor)
2304    }
2305}