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