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    pub fn deferred_ops(&self) -> impl Iterator<Item = &Operation> {
1105        self.deferred_ops.iter()
1106    }
1107
1108    fn flush_deferred_ops(&mut self) -> Result<()> {
1109        self.deferred_replicas.clear();
1110        let mut deferred_ops = Vec::new();
1111        for op in self.deferred_ops.drain().iter().cloned() {
1112            if self.can_apply_op(&op) {
1113                self.apply_op(op)?;
1114            } else {
1115                self.deferred_replicas.insert(op.replica_id());
1116                deferred_ops.push(op);
1117            }
1118        }
1119        self.deferred_ops.insert(deferred_ops);
1120        Ok(())
1121    }
1122
1123    fn can_apply_op(&self, op: &Operation) -> bool {
1124        if self.deferred_replicas.contains(&op.replica_id()) {
1125            false
1126        } else {
1127            match op {
1128                Operation::Edit(edit) => self.version.ge(&edit.version),
1129                Operation::Undo { undo, .. } => self.version.ge(&undo.version),
1130            }
1131        }
1132    }
1133
1134    pub fn can_resolve(&self, anchor: &Anchor) -> bool {
1135        *anchor == Anchor::min()
1136            || *anchor == Anchor::max()
1137            || self.version.observed(anchor.timestamp)
1138    }
1139
1140    pub fn peek_undo_stack(&self) -> Option<&Transaction> {
1141        self.history.undo_stack.last()
1142    }
1143
1144    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1145        self.start_transaction_at(Instant::now())
1146    }
1147
1148    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1149        self.history.start_transaction(self.version.clone(), now)
1150    }
1151
1152    pub fn end_transaction(&mut self) -> Option<(TransactionId, clock::Global)> {
1153        self.end_transaction_at(Instant::now())
1154    }
1155
1156    pub fn end_transaction_at(&mut self, now: Instant) -> Option<(TransactionId, clock::Global)> {
1157        if let Some(transaction) = self.history.end_transaction(now) {
1158            let since = transaction.start.clone();
1159            let id = self.history.group().unwrap();
1160            Some((id, since))
1161        } else {
1162            None
1163        }
1164    }
1165
1166    pub fn base_text(&self) -> &Arc<str> {
1167        &self.history.base_text
1168    }
1169
1170    pub fn history(&self) -> impl Iterator<Item = &EditOperation> {
1171        self.history.ops.values()
1172    }
1173
1174    pub fn undo_history(&self) -> impl Iterator<Item = (&clock::Local, &[(clock::Local, u32)])> {
1175        self.undo_map
1176            .0
1177            .iter()
1178            .map(|(edit_id, undo_counts)| (edit_id, undo_counts.as_slice()))
1179    }
1180
1181    pub fn undo(&mut self) -> Option<(TransactionId, Operation)> {
1182        if let Some(transaction) = self.history.pop_undo().cloned() {
1183            let transaction_id = transaction.id;
1184            let op = self.undo_or_redo(transaction).unwrap();
1185            Some((transaction_id, op))
1186        } else {
1187            None
1188        }
1189    }
1190
1191    pub fn undo_transaction(&mut self, transaction_id: TransactionId) -> Option<Operation> {
1192        if let Some(transaction) = self.history.remove_from_undo(transaction_id).cloned() {
1193            let op = self.undo_or_redo(transaction).unwrap();
1194            Some(op)
1195        } else {
1196            None
1197        }
1198    }
1199
1200    pub fn redo(&mut self) -> Option<(TransactionId, Operation)> {
1201        if let Some(transaction) = self.history.pop_redo().cloned() {
1202            let transaction_id = transaction.id;
1203            let op = self.undo_or_redo(transaction).unwrap();
1204            Some((transaction_id, op))
1205        } else {
1206            None
1207        }
1208    }
1209
1210    pub fn redo_transaction(&mut self, transaction_id: TransactionId) -> Option<Operation> {
1211        if let Some(transaction) = self.history.remove_from_redo(transaction_id).cloned() {
1212            let op = self.undo_or_redo(transaction).unwrap();
1213            Some(op)
1214        } else {
1215            None
1216        }
1217    }
1218
1219    fn undo_or_redo(&mut self, transaction: Transaction) -> Result<Operation> {
1220        let mut counts = HashMap::default();
1221        for edit_id in transaction.edits {
1222            counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1223        }
1224
1225        let undo = UndoOperation {
1226            id: self.local_clock.tick(),
1227            counts,
1228            ranges: transaction.ranges,
1229            version: transaction.start.clone(),
1230        };
1231        self.apply_undo(&undo)?;
1232        self.snapshot.version.observe(undo.id);
1233
1234        Ok(Operation::Undo {
1235            undo,
1236            lamport_timestamp: self.lamport_clock.tick(),
1237        })
1238    }
1239
1240    pub fn subscribe(&mut self) -> Subscription {
1241        self.subscriptions.subscribe()
1242    }
1243}
1244
1245#[cfg(any(test, feature = "test-support"))]
1246impl Buffer {
1247    pub fn set_group_interval(&mut self, group_interval: Duration) {
1248        self.history.group_interval = group_interval;
1249    }
1250
1251    pub fn random_byte_range(&self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
1252        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
1253        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
1254        start..end
1255    }
1256
1257    pub fn randomly_edit<T>(
1258        &mut self,
1259        rng: &mut T,
1260        old_range_count: usize,
1261    ) -> (Vec<Range<usize>>, String, Operation)
1262    where
1263        T: rand::Rng,
1264    {
1265        let mut old_ranges: Vec<Range<usize>> = Vec::new();
1266        for _ in 0..old_range_count {
1267            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
1268            if last_end > self.len() {
1269                break;
1270            }
1271            old_ranges.push(self.random_byte_range(last_end, rng));
1272        }
1273        let new_text_len = rng.gen_range(0..10);
1274        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1275            .take(new_text_len)
1276            .collect();
1277        log::info!(
1278            "mutating buffer {} at {:?}: {:?}",
1279            self.replica_id,
1280            old_ranges,
1281            new_text
1282        );
1283        let op = self.edit(old_ranges.iter().cloned(), new_text.as_str());
1284        (old_ranges, new_text, Operation::Edit(op))
1285    }
1286
1287    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
1288        use rand::prelude::*;
1289
1290        let mut ops = Vec::new();
1291        for _ in 0..rng.gen_range(1..=5) {
1292            if let Some(transaction) = self.history.undo_stack.choose(rng).cloned() {
1293                log::info!(
1294                    "undoing buffer {} transaction {:?}",
1295                    self.replica_id,
1296                    transaction
1297                );
1298                ops.push(self.undo_or_redo(transaction).unwrap());
1299            }
1300        }
1301        ops
1302    }
1303}
1304
1305impl Deref for Buffer {
1306    type Target = BufferSnapshot;
1307
1308    fn deref(&self) -> &Self::Target {
1309        &self.snapshot
1310    }
1311}
1312
1313impl BufferSnapshot {
1314    pub fn as_rope(&self) -> &Rope {
1315        &self.visible_text
1316    }
1317
1318    pub fn replica_id(&self) -> ReplicaId {
1319        self.replica_id
1320    }
1321
1322    pub fn row_count(&self) -> u32 {
1323        self.max_point().row + 1
1324    }
1325
1326    pub fn len(&self) -> usize {
1327        self.visible_text.len()
1328    }
1329
1330    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1331        self.chars_at(0)
1332    }
1333
1334    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1335        self.text_for_range(range).flat_map(str::chars)
1336    }
1337
1338    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1339    where
1340        T: ToOffset,
1341    {
1342        let position = position.to_offset(self);
1343        position == self.clip_offset(position, Bias::Left)
1344            && self
1345                .bytes_in_range(position..self.len())
1346                .flatten()
1347                .copied()
1348                .take(needle.len())
1349                .eq(needle.bytes())
1350    }
1351
1352    pub fn text(&self) -> String {
1353        self.visible_text.to_string()
1354    }
1355
1356    pub fn deleted_text(&self) -> String {
1357        self.deleted_text.to_string()
1358    }
1359
1360    pub fn fragments(&self) -> impl Iterator<Item = &Fragment> {
1361        self.fragments.iter()
1362    }
1363
1364    pub fn text_summary(&self) -> TextSummary {
1365        self.visible_text.summary()
1366    }
1367
1368    pub fn max_point(&self) -> Point {
1369        self.visible_text.max_point()
1370    }
1371
1372    pub fn point_to_offset(&self, point: Point) -> usize {
1373        self.visible_text.point_to_offset(point)
1374    }
1375
1376    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
1377        self.visible_text.point_utf16_to_offset(point)
1378    }
1379
1380    pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
1381        self.visible_text.point_utf16_to_point(point)
1382    }
1383
1384    pub fn offset_to_point(&self, offset: usize) -> Point {
1385        self.visible_text.offset_to_point(offset)
1386    }
1387
1388    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
1389        self.visible_text.offset_to_point_utf16(offset)
1390    }
1391
1392    pub fn version(&self) -> &clock::Global {
1393        &self.version
1394    }
1395
1396    pub fn chars_at<'a, T: ToOffset>(&'a self, position: T) -> impl Iterator<Item = char> + 'a {
1397        let offset = position.to_offset(self);
1398        self.visible_text.chars_at(offset)
1399    }
1400
1401    pub fn reversed_chars_at<'a, T: ToOffset>(
1402        &'a self,
1403        position: T,
1404    ) -> impl Iterator<Item = char> + 'a {
1405        let offset = position.to_offset(self);
1406        self.visible_text.reversed_chars_at(offset)
1407    }
1408
1409    pub fn reversed_chunks_in_range<T: ToOffset>(&self, range: Range<T>) -> rope::Chunks {
1410        let range = range.start.to_offset(self)..range.end.to_offset(self);
1411        self.visible_text.reversed_chunks_in_range(range)
1412    }
1413
1414    pub fn bytes_in_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> rope::Bytes<'a> {
1415        let start = range.start.to_offset(self);
1416        let end = range.end.to_offset(self);
1417        self.visible_text.bytes_in_range(start..end)
1418    }
1419
1420    pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
1421        let start = range.start.to_offset(self);
1422        let end = range.end.to_offset(self);
1423        self.visible_text.chunks_in_range(start..end)
1424    }
1425
1426    pub fn line_len(&self, row: u32) -> u32 {
1427        let row_start_offset = Point::new(row, 0).to_offset(self);
1428        let row_end_offset = if row >= self.max_point().row {
1429            self.len()
1430        } else {
1431            Point::new(row + 1, 0).to_offset(self) - 1
1432        };
1433        (row_end_offset - row_start_offset) as u32
1434    }
1435
1436    pub fn is_line_blank(&self, row: u32) -> bool {
1437        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1438            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1439    }
1440
1441    pub fn indent_column_for_line(&self, row: u32) -> u32 {
1442        let mut result = 0;
1443        for c in self.chars_at(Point::new(row, 0)) {
1444            if c == ' ' {
1445                result += 1;
1446            } else {
1447                break;
1448            }
1449        }
1450        result
1451    }
1452
1453    pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
1454    where
1455        D: TextDimension,
1456    {
1457        self.visible_text
1458            .cursor(range.start.to_offset(self))
1459            .summary(range.end.to_offset(self))
1460    }
1461
1462    pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
1463    where
1464        D: 'a + TextDimension,
1465        A: 'a + IntoIterator<Item = &'a Anchor>,
1466    {
1467        let anchors = anchors.into_iter();
1468        let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1469        let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1470        let mut text_cursor = self.visible_text.cursor(0);
1471        let mut position = D::default();
1472
1473        anchors.map(move |anchor| {
1474            if *anchor == Anchor::min() {
1475                return D::default();
1476            } else if *anchor == Anchor::max() {
1477                return D::from_text_summary(&self.visible_text.summary());
1478            }
1479
1480            let anchor_key = InsertionFragmentKey {
1481                timestamp: anchor.timestamp,
1482                split_offset: anchor.offset,
1483            };
1484            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1485            if let Some(insertion) = insertion_cursor.item() {
1486                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1487                if comparison == Ordering::Greater
1488                    || (anchor.bias == Bias::Left
1489                        && comparison == Ordering::Equal
1490                        && anchor.offset > 0)
1491                {
1492                    insertion_cursor.prev(&());
1493                }
1494            } else {
1495                insertion_cursor.prev(&());
1496            }
1497            let insertion = insertion_cursor.item().expect("invalid insertion");
1498            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1499
1500            fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left, &None);
1501            let fragment = fragment_cursor.item().unwrap();
1502            let mut fragment_offset = fragment_cursor.start().1;
1503            if fragment.visible {
1504                fragment_offset += anchor.offset - insertion.split_offset;
1505            }
1506
1507            position.add_assign(&text_cursor.summary(fragment_offset));
1508            position.clone()
1509        })
1510    }
1511
1512    fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D
1513    where
1514        D: TextDimension,
1515    {
1516        if *anchor == Anchor::min() {
1517            D::default()
1518        } else if *anchor == Anchor::max() {
1519            D::from_text_summary(&self.visible_text.summary())
1520        } else {
1521            let anchor_key = InsertionFragmentKey {
1522                timestamp: anchor.timestamp,
1523                split_offset: anchor.offset,
1524            };
1525            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1526            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1527            if let Some(insertion) = insertion_cursor.item() {
1528                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1529                if comparison == Ordering::Greater
1530                    || (anchor.bias == Bias::Left
1531                        && comparison == Ordering::Equal
1532                        && anchor.offset > 0)
1533                {
1534                    insertion_cursor.prev(&());
1535                }
1536            } else {
1537                insertion_cursor.prev(&());
1538            }
1539            let insertion = insertion_cursor.item().expect("invalid insertion");
1540            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1541
1542            let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1543            fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None);
1544            let fragment = fragment_cursor.item().unwrap();
1545            let mut fragment_offset = fragment_cursor.start().1;
1546            if fragment.visible {
1547                fragment_offset += anchor.offset - insertion.split_offset;
1548            }
1549            self.text_summary_for_range(0..fragment_offset)
1550        }
1551    }
1552
1553    fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
1554        if *anchor == Anchor::min() {
1555            &locator::MIN
1556        } else if *anchor == Anchor::max() {
1557            &locator::MAX
1558        } else {
1559            let anchor_key = InsertionFragmentKey {
1560                timestamp: anchor.timestamp,
1561                split_offset: anchor.offset,
1562            };
1563            let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1564            insertion_cursor.seek(&anchor_key, anchor.bias, &());
1565            if let Some(insertion) = insertion_cursor.item() {
1566                let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1567                if comparison == Ordering::Greater
1568                    || (anchor.bias == Bias::Left
1569                        && comparison == Ordering::Equal
1570                        && anchor.offset > 0)
1571                {
1572                    insertion_cursor.prev(&());
1573                }
1574            } else {
1575                insertion_cursor.prev(&());
1576            }
1577            let insertion = insertion_cursor.item().expect("invalid insertion");
1578            debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1579            &insertion.fragment_id
1580        }
1581    }
1582
1583    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
1584        self.anchor_at(position, Bias::Left)
1585    }
1586
1587    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
1588        self.anchor_at(position, Bias::Right)
1589    }
1590
1591    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
1592        let offset = position.to_offset(self);
1593        if bias == Bias::Left && offset == 0 {
1594            Anchor::min()
1595        } else if bias == Bias::Right && offset == self.len() {
1596            Anchor::max()
1597        } else {
1598            let mut fragment_cursor = self.fragments.cursor::<usize>();
1599            fragment_cursor.seek(&offset, bias, &None);
1600            let fragment = fragment_cursor.item().unwrap();
1601            let overshoot = offset - *fragment_cursor.start();
1602            Anchor {
1603                timestamp: fragment.insertion_timestamp.local(),
1604                offset: fragment.insertion_offset + overshoot,
1605                bias,
1606            }
1607        }
1608    }
1609
1610    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1611        self.visible_text.clip_offset(offset, bias)
1612    }
1613
1614    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1615        self.visible_text.clip_point(point, bias)
1616    }
1617
1618    pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
1619        self.visible_text.clip_point_utf16(point, bias)
1620    }
1621
1622    // pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
1623    //     if offset <= self.len() {
1624    //         Ok(self.text_summary_for_range(0..offset))
1625    //     } else {
1626    //         Err(anyhow!("offset out of bounds"))
1627    //     }
1628    // }
1629
1630    pub fn edits_since<'a, D>(
1631        &'a self,
1632        since: &'a clock::Global,
1633    ) -> impl 'a + Iterator<Item = Edit<D>>
1634    where
1635        D: TextDimension + Ord,
1636    {
1637        self.edits_since_in_range(since, Anchor::min()..Anchor::max())
1638    }
1639
1640    pub fn edits_since_in_range<'a, D>(
1641        &'a self,
1642        since: &'a clock::Global,
1643        range: Range<Anchor>,
1644    ) -> impl 'a + Iterator<Item = Edit<D>>
1645    where
1646        D: TextDimension + Ord,
1647    {
1648        let fragments_cursor = if *since == self.version {
1649            None
1650        } else {
1651            Some(
1652                self.fragments
1653                    .filter(move |summary| !since.ge(&summary.max_version), &None),
1654            )
1655        };
1656        let mut cursor = self
1657            .fragments
1658            .cursor::<(Option<&Locator>, FragmentTextSummary)>();
1659
1660        let start_fragment_id = self.fragment_id_for_anchor(&range.start);
1661        cursor.seek(&Some(start_fragment_id), Bias::Left, &None);
1662        let mut visible_start = cursor.start().1.visible;
1663        let mut deleted_start = cursor.start().1.deleted;
1664        if let Some(fragment) = cursor.item() {
1665            let overshoot = range.start.offset - fragment.insertion_offset;
1666            if fragment.visible {
1667                visible_start += overshoot;
1668            } else {
1669                deleted_start += overshoot;
1670            }
1671        }
1672        let end_fragment_id = self.fragment_id_for_anchor(&range.end);
1673
1674        Edits {
1675            visible_cursor: self.visible_text.cursor(visible_start),
1676            deleted_cursor: self.deleted_text.cursor(deleted_start),
1677            fragments_cursor,
1678            undos: &self.undo_map,
1679            since,
1680            old_end: Default::default(),
1681            new_end: Default::default(),
1682            range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
1683        }
1684    }
1685}
1686
1687struct RopeBuilder<'a> {
1688    old_visible_cursor: rope::Cursor<'a>,
1689    old_deleted_cursor: rope::Cursor<'a>,
1690    new_visible: Rope,
1691    new_deleted: Rope,
1692}
1693
1694impl<'a> RopeBuilder<'a> {
1695    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
1696        Self {
1697            old_visible_cursor,
1698            old_deleted_cursor,
1699            new_visible: Rope::new(),
1700            new_deleted: Rope::new(),
1701        }
1702    }
1703
1704    fn push_tree(&mut self, len: FragmentTextSummary) {
1705        self.push(len.visible, true, true);
1706        self.push(len.deleted, false, false);
1707    }
1708
1709    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
1710        debug_assert!(fragment.len > 0);
1711        self.push(fragment.len, was_visible, fragment.visible)
1712    }
1713
1714    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
1715        let text = if was_visible {
1716            self.old_visible_cursor
1717                .slice(self.old_visible_cursor.offset() + len)
1718        } else {
1719            self.old_deleted_cursor
1720                .slice(self.old_deleted_cursor.offset() + len)
1721        };
1722        if is_visible {
1723            self.new_visible.append(text);
1724        } else {
1725            self.new_deleted.append(text);
1726        }
1727    }
1728
1729    fn push_str(&mut self, text: &str) {
1730        self.new_visible.push(text);
1731    }
1732
1733    fn finish(mut self) -> (Rope, Rope) {
1734        self.new_visible.append(self.old_visible_cursor.suffix());
1735        self.new_deleted.append(self.old_deleted_cursor.suffix());
1736        (self.new_visible, self.new_deleted)
1737    }
1738}
1739
1740impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
1741    type Item = Edit<D>;
1742
1743    fn next(&mut self) -> Option<Self::Item> {
1744        let mut pending_edit: Option<Edit<D>> = None;
1745        let cursor = self.fragments_cursor.as_mut()?;
1746
1747        while let Some(fragment) = cursor.item() {
1748            if fragment.id < *self.range.start.0 {
1749                cursor.next(&None);
1750                continue;
1751            } else if fragment.id > *self.range.end.0 {
1752                break;
1753            }
1754
1755            if cursor.start().visible > self.visible_cursor.offset() {
1756                let summary = self.visible_cursor.summary(cursor.start().visible);
1757                self.old_end.add_assign(&summary);
1758                self.new_end.add_assign(&summary);
1759            }
1760
1761            if pending_edit
1762                .as_ref()
1763                .map_or(false, |change| change.new.end < self.new_end)
1764            {
1765                break;
1766            }
1767
1768            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
1769                let mut visible_end = cursor.end(&None).visible;
1770                if fragment.id == *self.range.end.0 {
1771                    visible_end = cmp::min(
1772                        visible_end,
1773                        cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
1774                    );
1775                }
1776
1777                let fragment_summary = self.visible_cursor.summary(visible_end);
1778                let mut new_end = self.new_end.clone();
1779                new_end.add_assign(&fragment_summary);
1780                if let Some(pending_edit) = pending_edit.as_mut() {
1781                    pending_edit.new.end = new_end.clone();
1782                } else {
1783                    pending_edit = Some(Edit {
1784                        old: self.old_end.clone()..self.old_end.clone(),
1785                        new: self.new_end.clone()..new_end.clone(),
1786                    });
1787                }
1788
1789                self.new_end = new_end;
1790            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
1791                let mut deleted_end = cursor.end(&None).deleted;
1792                if fragment.id == *self.range.end.0 {
1793                    deleted_end = cmp::min(
1794                        deleted_end,
1795                        cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
1796                    );
1797                }
1798
1799                if cursor.start().deleted > self.deleted_cursor.offset() {
1800                    self.deleted_cursor.seek_forward(cursor.start().deleted);
1801                }
1802                let fragment_summary = self.deleted_cursor.summary(deleted_end);
1803                let mut old_end = self.old_end.clone();
1804                old_end.add_assign(&fragment_summary);
1805                if let Some(pending_edit) = pending_edit.as_mut() {
1806                    pending_edit.old.end = old_end.clone();
1807                } else {
1808                    pending_edit = Some(Edit {
1809                        old: self.old_end.clone()..old_end.clone(),
1810                        new: self.new_end.clone()..self.new_end.clone(),
1811                    });
1812                }
1813
1814                self.old_end = old_end;
1815            }
1816
1817            cursor.next(&None);
1818        }
1819
1820        pending_edit
1821    }
1822}
1823
1824impl Fragment {
1825    fn is_visible(&self, undos: &UndoMap) -> bool {
1826        !undos.is_undone(self.insertion_timestamp.local())
1827            && self.deletions.iter().all(|d| undos.is_undone(*d))
1828    }
1829
1830    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
1831        (version.observed(self.insertion_timestamp.local())
1832            && !undos.was_undone(self.insertion_timestamp.local(), version))
1833            && self
1834                .deletions
1835                .iter()
1836                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
1837    }
1838}
1839
1840impl sum_tree::Item for Fragment {
1841    type Summary = FragmentSummary;
1842
1843    fn summary(&self) -> Self::Summary {
1844        let mut max_version = clock::Global::new();
1845        max_version.observe(self.insertion_timestamp.local());
1846        for deletion in &self.deletions {
1847            max_version.observe(*deletion);
1848        }
1849        max_version.join(&self.max_undos);
1850
1851        let mut min_insertion_version = clock::Global::new();
1852        min_insertion_version.observe(self.insertion_timestamp.local());
1853        let max_insertion_version = min_insertion_version.clone();
1854        if self.visible {
1855            FragmentSummary {
1856                max_id: self.id.clone(),
1857                text: FragmentTextSummary {
1858                    visible: self.len,
1859                    deleted: 0,
1860                },
1861                max_version,
1862                min_insertion_version,
1863                max_insertion_version,
1864            }
1865        } else {
1866            FragmentSummary {
1867                max_id: self.id.clone(),
1868                text: FragmentTextSummary {
1869                    visible: 0,
1870                    deleted: self.len,
1871                },
1872                max_version,
1873                min_insertion_version,
1874                max_insertion_version,
1875            }
1876        }
1877    }
1878}
1879
1880impl sum_tree::Summary for FragmentSummary {
1881    type Context = Option<clock::Global>;
1882
1883    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
1884        self.max_id.assign(&other.max_id);
1885        self.text.visible += &other.text.visible;
1886        self.text.deleted += &other.text.deleted;
1887        self.max_version.join(&other.max_version);
1888        self.min_insertion_version
1889            .meet(&other.min_insertion_version);
1890        self.max_insertion_version
1891            .join(&other.max_insertion_version);
1892    }
1893}
1894
1895impl Default for FragmentSummary {
1896    fn default() -> Self {
1897        FragmentSummary {
1898            max_id: Locator::min(),
1899            text: FragmentTextSummary::default(),
1900            max_version: clock::Global::new(),
1901            min_insertion_version: clock::Global::new(),
1902            max_insertion_version: clock::Global::new(),
1903        }
1904    }
1905}
1906
1907impl sum_tree::Item for InsertionFragment {
1908    type Summary = InsertionFragmentKey;
1909
1910    fn summary(&self) -> Self::Summary {
1911        InsertionFragmentKey {
1912            timestamp: self.timestamp,
1913            split_offset: self.split_offset,
1914        }
1915    }
1916}
1917
1918impl sum_tree::KeyedItem for InsertionFragment {
1919    type Key = InsertionFragmentKey;
1920
1921    fn key(&self) -> Self::Key {
1922        sum_tree::Item::summary(self)
1923    }
1924}
1925
1926impl InsertionFragment {
1927    fn new(fragment: &Fragment) -> Self {
1928        Self {
1929            timestamp: fragment.insertion_timestamp.local(),
1930            split_offset: fragment.insertion_offset,
1931            fragment_id: fragment.id.clone(),
1932        }
1933    }
1934
1935    fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
1936        sum_tree::Edit::Insert(Self::new(fragment))
1937    }
1938}
1939
1940impl sum_tree::Summary for InsertionFragmentKey {
1941    type Context = ();
1942
1943    fn add_summary(&mut self, summary: &Self, _: &()) {
1944        *self = *summary;
1945    }
1946}
1947
1948#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
1949pub struct FullOffset(pub usize);
1950
1951impl ops::AddAssign<usize> for FullOffset {
1952    fn add_assign(&mut self, rhs: usize) {
1953        self.0 += rhs;
1954    }
1955}
1956
1957impl ops::Add<usize> for FullOffset {
1958    type Output = Self;
1959
1960    fn add(mut self, rhs: usize) -> Self::Output {
1961        self += rhs;
1962        self
1963    }
1964}
1965
1966impl ops::Sub for FullOffset {
1967    type Output = usize;
1968
1969    fn sub(self, rhs: Self) -> Self::Output {
1970        self.0 - rhs.0
1971    }
1972}
1973
1974impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
1975    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
1976        *self += summary.text.visible;
1977    }
1978}
1979
1980impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
1981    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
1982        self.0 += summary.text.visible + summary.text.deleted;
1983    }
1984}
1985
1986impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
1987    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
1988        *self = Some(&summary.max_id);
1989    }
1990}
1991
1992impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
1993    fn cmp(
1994        &self,
1995        cursor_location: &FragmentTextSummary,
1996        _: &Option<clock::Global>,
1997    ) -> cmp::Ordering {
1998        Ord::cmp(self, &cursor_location.visible)
1999    }
2000}
2001
2002#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2003enum VersionedFullOffset {
2004    Offset(FullOffset),
2005    Invalid,
2006}
2007
2008impl VersionedFullOffset {
2009    fn full_offset(&self) -> FullOffset {
2010        if let Self::Offset(position) = self {
2011            *position
2012        } else {
2013            panic!("invalid version")
2014        }
2015    }
2016}
2017
2018impl Default for VersionedFullOffset {
2019    fn default() -> Self {
2020        Self::Offset(Default::default())
2021    }
2022}
2023
2024impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
2025    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
2026        if let Self::Offset(offset) = self {
2027            let version = cx.as_ref().unwrap();
2028            if version.ge(&summary.max_insertion_version) {
2029                *offset += summary.text.visible + summary.text.deleted;
2030            } else if version.observed_any(&summary.min_insertion_version) {
2031                *self = Self::Invalid;
2032            }
2033        }
2034    }
2035}
2036
2037impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedFullOffset {
2038    fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
2039        match (self, cursor_position) {
2040            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
2041            (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
2042            (Self::Invalid, _) => unreachable!(),
2043        }
2044    }
2045}
2046
2047impl Operation {
2048    fn replica_id(&self) -> ReplicaId {
2049        operation_queue::Operation::lamport_timestamp(self).replica_id
2050    }
2051
2052    pub fn is_edit(&self) -> bool {
2053        match self {
2054            Operation::Edit { .. } => true,
2055            _ => false,
2056        }
2057    }
2058}
2059
2060impl operation_queue::Operation for Operation {
2061    fn lamport_timestamp(&self) -> clock::Lamport {
2062        match self {
2063            Operation::Edit(edit) => edit.timestamp.lamport(),
2064            Operation::Undo {
2065                lamport_timestamp, ..
2066            } => *lamport_timestamp,
2067        }
2068    }
2069}
2070
2071pub trait ToOffset {
2072    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize;
2073}
2074
2075impl ToOffset for Point {
2076    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2077        snapshot.point_to_offset(*self)
2078    }
2079}
2080
2081impl ToOffset for PointUtf16 {
2082    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2083        snapshot.point_utf16_to_offset(*self)
2084    }
2085}
2086
2087impl ToOffset for usize {
2088    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2089        assert!(*self <= snapshot.len(), "offset is out of range");
2090        *self
2091    }
2092}
2093
2094impl ToOffset for Anchor {
2095    fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
2096        snapshot.summary_for_anchor(self)
2097    }
2098}
2099
2100impl<'a, T: ToOffset> ToOffset for &'a T {
2101    fn to_offset(&self, content: &BufferSnapshot) -> usize {
2102        (*self).to_offset(content)
2103    }
2104}
2105
2106pub trait ToPoint {
2107    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point;
2108}
2109
2110impl ToPoint for Anchor {
2111    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2112        snapshot.summary_for_anchor(self)
2113    }
2114}
2115
2116impl ToPoint for usize {
2117    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2118        snapshot.offset_to_point(*self)
2119    }
2120}
2121
2122impl ToPoint for PointUtf16 {
2123    fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
2124        snapshot.point_utf16_to_point(*self)
2125    }
2126}
2127
2128impl ToPoint for Point {
2129    fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
2130        *self
2131    }
2132}
2133
2134pub trait Clip {
2135    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self;
2136}
2137
2138impl Clip for usize {
2139    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2140        snapshot.clip_offset(*self, bias)
2141    }
2142}
2143
2144impl Clip for Point {
2145    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2146        snapshot.clip_point(*self, bias)
2147    }
2148}
2149
2150impl Clip for PointUtf16 {
2151    fn clip(&self, bias: Bias, snapshot: &BufferSnapshot) -> Self {
2152        snapshot.clip_point_utf16(*self, bias)
2153    }
2154}
2155
2156pub trait FromAnchor {
2157    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
2158}
2159
2160impl FromAnchor for Point {
2161    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2162        snapshot.summary_for_anchor(anchor)
2163    }
2164}
2165
2166impl FromAnchor for PointUtf16 {
2167    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2168        snapshot.summary_for_anchor(anchor)
2169    }
2170}
2171
2172impl FromAnchor for usize {
2173    fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2174        snapshot.summary_for_anchor(anchor)
2175    }
2176}