text.rs

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