lib.rs

   1mod anchor;
   2mod highlight_map;
   3mod language;
   4mod operation_queue;
   5mod point;
   6#[cfg(any(test, feature = "test-support"))]
   7pub mod random_char_iter;
   8pub mod rope;
   9mod selection;
  10#[cfg(test)]
  11mod tests;
  12
  13pub use anchor::*;
  14use anyhow::{anyhow, Result};
  15use clock::ReplicaId;
  16use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task};
  17pub use highlight_map::{HighlightId, HighlightMap};
  18use language::Tree;
  19pub use language::{BracketPair, Language, LanguageConfig, LanguageRegistry};
  20use lazy_static::lazy_static;
  21use operation_queue::OperationQueue;
  22use parking_lot::Mutex;
  23pub use point::*;
  24#[cfg(any(test, feature = "test-support"))]
  25pub use random_char_iter::*;
  26pub use rope::{Chunks, Rope, TextSummary};
  27use rpc::proto;
  28use seahash::SeaHasher;
  29pub use selection::*;
  30use similar::{ChangeTag, TextDiff};
  31use smol::future::yield_now;
  32use std::{
  33    any::Any,
  34    cell::RefCell,
  35    cmp,
  36    collections::BTreeMap,
  37    convert::{TryFrom, TryInto},
  38    ffi::OsString,
  39    future::Future,
  40    hash::BuildHasher,
  41    iter::Iterator,
  42    ops::{Deref, DerefMut, Range},
  43    path::{Path, PathBuf},
  44    str,
  45    sync::Arc,
  46    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  47};
  48use sum_tree::{Bias, FilterCursor, SumTree};
  49use tree_sitter::{InputEdit, Parser, QueryCursor};
  50
  51pub trait File {
  52    fn worktree_id(&self) -> usize;
  53
  54    fn entry_id(&self) -> Option<usize>;
  55
  56    fn set_entry_id(&mut self, entry_id: Option<usize>);
  57
  58    fn mtime(&self) -> SystemTime;
  59
  60    fn set_mtime(&mut self, mtime: SystemTime);
  61
  62    fn path(&self) -> &Arc<Path>;
  63
  64    fn set_path(&mut self, path: Arc<Path>);
  65
  66    fn full_path(&self, cx: &AppContext) -> PathBuf;
  67
  68    /// Returns the last component of this handle's absolute path. If this handle refers to the root
  69    /// of its worktree, then this method will return the name of the worktree itself.
  70    fn file_name<'a>(&'a self, cx: &'a AppContext) -> Option<OsString>;
  71
  72    fn is_deleted(&self) -> bool;
  73
  74    fn save(
  75        &self,
  76        buffer_id: u64,
  77        text: Rope,
  78        version: clock::Global,
  79        cx: &mut MutableAppContext,
  80    ) -> Task<Result<(clock::Global, SystemTime)>>;
  81
  82    fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext);
  83
  84    fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext);
  85
  86    fn boxed_clone(&self) -> Box<dyn File>;
  87
  88    fn as_any(&self) -> &dyn Any;
  89}
  90
  91#[derive(Clone, Default)]
  92struct DeterministicState;
  93
  94impl BuildHasher for DeterministicState {
  95    type Hasher = SeaHasher;
  96
  97    fn build_hasher(&self) -> Self::Hasher {
  98        SeaHasher::new()
  99    }
 100}
 101
 102#[cfg(any(test, feature = "test-support"))]
 103type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
 104
 105#[cfg(any(test, feature = "test-support"))]
 106type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
 107
 108#[cfg(not(any(test, feature = "test-support")))]
 109type HashMap<K, V> = std::collections::HashMap<K, V>;
 110
 111#[cfg(not(any(test, feature = "test-support")))]
 112type HashSet<T> = std::collections::HashSet<T>;
 113
 114thread_local! {
 115    static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
 116}
 117
 118lazy_static! {
 119    static ref QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Default::default();
 120}
 121
 122// TODO - Make this configurable
 123const INDENT_SIZE: u32 = 4;
 124
 125struct QueryCursorHandle(Option<QueryCursor>);
 126
 127impl QueryCursorHandle {
 128    fn new() -> Self {
 129        QueryCursorHandle(Some(
 130            QUERY_CURSORS
 131                .lock()
 132                .pop()
 133                .unwrap_or_else(|| QueryCursor::new()),
 134        ))
 135    }
 136}
 137
 138impl Deref for QueryCursorHandle {
 139    type Target = QueryCursor;
 140
 141    fn deref(&self) -> &Self::Target {
 142        self.0.as_ref().unwrap()
 143    }
 144}
 145
 146impl DerefMut for QueryCursorHandle {
 147    fn deref_mut(&mut self) -> &mut Self::Target {
 148        self.0.as_mut().unwrap()
 149    }
 150}
 151
 152impl Drop for QueryCursorHandle {
 153    fn drop(&mut self) {
 154        let mut cursor = self.0.take().unwrap();
 155        cursor.set_byte_range(0..usize::MAX);
 156        cursor.set_point_range(Point::zero().into()..Point::MAX.into());
 157        QUERY_CURSORS.lock().push(cursor)
 158    }
 159}
 160
 161#[derive(Clone)]
 162pub struct TextBuffer {
 163    fragments: SumTree<Fragment>,
 164    visible_text: Rope,
 165    deleted_text: Rope,
 166    pub version: clock::Global,
 167    last_edit: clock::Local,
 168    undo_map: UndoMap,
 169    history: History,
 170    selections: HashMap<SelectionSetId, SelectionSet>,
 171    deferred_ops: OperationQueue,
 172    deferred_replicas: HashSet<ReplicaId>,
 173    replica_id: ReplicaId,
 174    remote_id: u64,
 175    local_clock: clock::Local,
 176    lamport_clock: clock::Lamport,
 177}
 178
 179pub struct Buffer {
 180    buffer: TextBuffer,
 181    file: Option<Box<dyn File>>,
 182    saved_version: clock::Global,
 183    saved_mtime: SystemTime,
 184    language: Option<Arc<Language>>,
 185    autoindent_requests: Vec<Arc<AutoindentRequest>>,
 186    pending_autoindent: Option<Task<()>>,
 187    sync_parse_timeout: Duration,
 188    syntax_tree: Mutex<Option<SyntaxTree>>,
 189    parsing_in_background: bool,
 190    parse_count: usize,
 191    #[cfg(test)]
 192    operations: Vec<Operation>,
 193}
 194
 195#[derive(Clone, Debug, Eq, PartialEq)]
 196pub struct SelectionSet {
 197    pub selections: Arc<[Selection]>,
 198    pub active: bool,
 199}
 200
 201#[derive(Clone)]
 202struct SyntaxTree {
 203    tree: Tree,
 204    version: clock::Global,
 205}
 206
 207#[derive(Clone)]
 208struct AutoindentRequest {
 209    selection_set_ids: HashSet<SelectionSetId>,
 210    before_edit: Snapshot,
 211    edited: AnchorSet,
 212    inserted: Option<AnchorRangeSet>,
 213}
 214
 215#[derive(Clone, Debug)]
 216pub struct Transaction {
 217    start: clock::Global,
 218    end: clock::Global,
 219    edits: Vec<clock::Local>,
 220    ranges: Vec<Range<usize>>,
 221    selections_before: HashMap<SelectionSetId, Arc<[Selection]>>,
 222    selections_after: HashMap<SelectionSetId, Arc<[Selection]>>,
 223    first_edit_at: Instant,
 224    last_edit_at: Instant,
 225}
 226
 227impl Transaction {
 228    pub fn starting_selection_set_ids<'a>(&'a self) -> impl Iterator<Item = SelectionSetId> + 'a {
 229        self.selections_before.keys().copied()
 230    }
 231
 232    fn push_edit(&mut self, edit: &EditOperation) {
 233        self.edits.push(edit.timestamp.local());
 234        self.end.observe(edit.timestamp.local());
 235
 236        let mut other_ranges = edit.ranges.iter().peekable();
 237        let mut new_ranges: Vec<Range<usize>> = Vec::new();
 238        let insertion_len = edit.new_text.as_ref().map_or(0, |t| t.len());
 239        let mut delta = 0;
 240
 241        for mut self_range in self.ranges.iter().cloned() {
 242            self_range.start += delta;
 243            self_range.end += delta;
 244
 245            while let Some(other_range) = other_ranges.peek() {
 246                let mut other_range = (*other_range).clone();
 247                other_range.start += delta;
 248                other_range.end += delta;
 249
 250                if other_range.start <= self_range.end {
 251                    other_ranges.next().unwrap();
 252                    delta += insertion_len;
 253
 254                    if other_range.end < self_range.start {
 255                        new_ranges.push(other_range.start..other_range.end + insertion_len);
 256                        self_range.start += insertion_len;
 257                        self_range.end += insertion_len;
 258                    } else {
 259                        self_range.start = cmp::min(self_range.start, other_range.start);
 260                        self_range.end = cmp::max(self_range.end, other_range.end) + insertion_len;
 261                    }
 262                } else {
 263                    break;
 264                }
 265            }
 266
 267            new_ranges.push(self_range);
 268        }
 269
 270        for other_range in other_ranges {
 271            new_ranges.push(other_range.start + delta..other_range.end + delta + insertion_len);
 272            delta += insertion_len;
 273        }
 274
 275        self.ranges = new_ranges;
 276    }
 277}
 278
 279#[derive(Clone)]
 280pub struct History {
 281    // TODO: Turn this into a String or Rope, maybe.
 282    pub base_text: Arc<str>,
 283    ops: HashMap<clock::Local, EditOperation>,
 284    undo_stack: Vec<Transaction>,
 285    redo_stack: Vec<Transaction>,
 286    transaction_depth: usize,
 287    group_interval: Duration,
 288}
 289
 290impl History {
 291    pub fn new(base_text: Arc<str>) -> Self {
 292        Self {
 293            base_text,
 294            ops: Default::default(),
 295            undo_stack: Vec::new(),
 296            redo_stack: Vec::new(),
 297            transaction_depth: 0,
 298            group_interval: Duration::from_millis(300),
 299        }
 300    }
 301
 302    fn push(&mut self, op: EditOperation) {
 303        self.ops.insert(op.timestamp.local(), op);
 304    }
 305
 306    fn start_transaction(
 307        &mut self,
 308        start: clock::Global,
 309        selections_before: HashMap<SelectionSetId, Arc<[Selection]>>,
 310        now: Instant,
 311    ) {
 312        self.transaction_depth += 1;
 313        if self.transaction_depth == 1 {
 314            self.undo_stack.push(Transaction {
 315                start: start.clone(),
 316                end: start,
 317                edits: Vec::new(),
 318                ranges: Vec::new(),
 319                selections_before,
 320                selections_after: Default::default(),
 321                first_edit_at: now,
 322                last_edit_at: now,
 323            });
 324        }
 325    }
 326
 327    fn end_transaction(
 328        &mut self,
 329        selections_after: HashMap<SelectionSetId, Arc<[Selection]>>,
 330        now: Instant,
 331    ) -> Option<&Transaction> {
 332        assert_ne!(self.transaction_depth, 0);
 333        self.transaction_depth -= 1;
 334        if self.transaction_depth == 0 {
 335            if self.undo_stack.last().unwrap().ranges.is_empty() {
 336                self.undo_stack.pop();
 337                None
 338            } else {
 339                let transaction = self.undo_stack.last_mut().unwrap();
 340                transaction.selections_after = selections_after;
 341                transaction.last_edit_at = now;
 342                Some(transaction)
 343            }
 344        } else {
 345            None
 346        }
 347    }
 348
 349    fn group(&mut self) {
 350        let mut new_len = self.undo_stack.len();
 351        let mut transactions = self.undo_stack.iter_mut();
 352
 353        if let Some(mut transaction) = transactions.next_back() {
 354            while let Some(prev_transaction) = transactions.next_back() {
 355                if transaction.first_edit_at - prev_transaction.last_edit_at <= self.group_interval
 356                    && transaction.start == prev_transaction.end
 357                {
 358                    transaction = prev_transaction;
 359                    new_len -= 1;
 360                } else {
 361                    break;
 362                }
 363            }
 364        }
 365
 366        let (transactions_to_keep, transactions_to_merge) = self.undo_stack.split_at_mut(new_len);
 367        if let Some(last_transaction) = transactions_to_keep.last_mut() {
 368            for transaction in &*transactions_to_merge {
 369                for edit_id in &transaction.edits {
 370                    last_transaction.push_edit(&self.ops[edit_id]);
 371                }
 372            }
 373
 374            if let Some(transaction) = transactions_to_merge.last_mut() {
 375                last_transaction.last_edit_at = transaction.last_edit_at;
 376                last_transaction
 377                    .selections_after
 378                    .extend(transaction.selections_after.drain());
 379                last_transaction.end = transaction.end.clone();
 380            }
 381        }
 382
 383        self.undo_stack.truncate(new_len);
 384    }
 385
 386    fn push_undo(&mut self, edit_id: clock::Local) {
 387        assert_ne!(self.transaction_depth, 0);
 388        let last_transaction = self.undo_stack.last_mut().unwrap();
 389        last_transaction.push_edit(&self.ops[&edit_id]);
 390    }
 391
 392    fn pop_undo(&mut self) -> Option<&Transaction> {
 393        assert_eq!(self.transaction_depth, 0);
 394        if let Some(transaction) = self.undo_stack.pop() {
 395            self.redo_stack.push(transaction);
 396            self.redo_stack.last()
 397        } else {
 398            None
 399        }
 400    }
 401
 402    fn pop_redo(&mut self) -> Option<&Transaction> {
 403        assert_eq!(self.transaction_depth, 0);
 404        if let Some(transaction) = self.redo_stack.pop() {
 405            self.undo_stack.push(transaction);
 406            self.undo_stack.last()
 407        } else {
 408            None
 409        }
 410    }
 411}
 412
 413#[derive(Clone, Default, Debug)]
 414struct UndoMap(HashMap<clock::Local, Vec<(clock::Local, u32)>>);
 415
 416impl UndoMap {
 417    fn insert(&mut self, undo: &UndoOperation) {
 418        for (edit_id, count) in &undo.counts {
 419            self.0.entry(*edit_id).or_default().push((undo.id, *count));
 420        }
 421    }
 422
 423    fn is_undone(&self, edit_id: clock::Local) -> bool {
 424        self.undo_count(edit_id) % 2 == 1
 425    }
 426
 427    fn was_undone(&self, edit_id: clock::Local, version: &clock::Global) -> bool {
 428        let undo_count = self
 429            .0
 430            .get(&edit_id)
 431            .unwrap_or(&Vec::new())
 432            .iter()
 433            .filter(|(undo_id, _)| version.observed(*undo_id))
 434            .map(|(_, undo_count)| *undo_count)
 435            .max()
 436            .unwrap_or(0);
 437        undo_count % 2 == 1
 438    }
 439
 440    fn undo_count(&self, edit_id: clock::Local) -> u32 {
 441        self.0
 442            .get(&edit_id)
 443            .unwrap_or(&Vec::new())
 444            .iter()
 445            .map(|(_, undo_count)| *undo_count)
 446            .max()
 447            .unwrap_or(0)
 448    }
 449}
 450
 451struct Edits<'a, F: Fn(&FragmentSummary) -> bool> {
 452    visible_text: &'a Rope,
 453    deleted_text: &'a Rope,
 454    cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
 455    undos: &'a UndoMap,
 456    since: clock::Global,
 457    old_offset: usize,
 458    new_offset: usize,
 459    old_point: Point,
 460    new_point: Point,
 461}
 462
 463#[derive(Clone, Debug, Default, Eq, PartialEq)]
 464pub struct Edit {
 465    pub old_bytes: Range<usize>,
 466    pub new_bytes: Range<usize>,
 467    pub old_lines: Range<Point>,
 468}
 469
 470impl Edit {
 471    pub fn delta(&self) -> isize {
 472        self.inserted_bytes() as isize - self.deleted_bytes() as isize
 473    }
 474
 475    pub fn deleted_bytes(&self) -> usize {
 476        self.old_bytes.end - self.old_bytes.start
 477    }
 478
 479    pub fn inserted_bytes(&self) -> usize {
 480        self.new_bytes.end - self.new_bytes.start
 481    }
 482
 483    pub fn deleted_lines(&self) -> Point {
 484        self.old_lines.end - self.old_lines.start
 485    }
 486}
 487
 488struct Diff {
 489    base_version: clock::Global,
 490    new_text: Arc<str>,
 491    changes: Vec<(ChangeTag, usize)>,
 492}
 493
 494#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
 495struct InsertionTimestamp {
 496    replica_id: ReplicaId,
 497    local: clock::Seq,
 498    lamport: clock::Seq,
 499}
 500
 501impl InsertionTimestamp {
 502    fn local(&self) -> clock::Local {
 503        clock::Local {
 504            replica_id: self.replica_id,
 505            value: self.local,
 506        }
 507    }
 508
 509    fn lamport(&self) -> clock::Lamport {
 510        clock::Lamport {
 511            replica_id: self.replica_id,
 512            value: self.lamport,
 513        }
 514    }
 515}
 516
 517#[derive(Eq, PartialEq, Clone, Debug)]
 518struct Fragment {
 519    timestamp: InsertionTimestamp,
 520    len: usize,
 521    visible: bool,
 522    deletions: HashSet<clock::Local>,
 523    max_undos: clock::Global,
 524}
 525
 526#[derive(Eq, PartialEq, Clone, Debug)]
 527pub struct FragmentSummary {
 528    text: FragmentTextSummary,
 529    max_version: clock::Global,
 530    min_insertion_version: clock::Global,
 531    max_insertion_version: clock::Global,
 532}
 533
 534#[derive(Copy, Default, Clone, Debug, PartialEq, Eq)]
 535struct FragmentTextSummary {
 536    visible: usize,
 537    deleted: usize,
 538}
 539
 540impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentTextSummary {
 541    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
 542        self.visible += summary.text.visible;
 543        self.deleted += summary.text.deleted;
 544    }
 545}
 546
 547#[derive(Clone, Debug, Eq, PartialEq)]
 548pub enum Operation {
 549    Edit(EditOperation),
 550    Undo {
 551        undo: UndoOperation,
 552        lamport_timestamp: clock::Lamport,
 553    },
 554    UpdateSelections {
 555        set_id: SelectionSetId,
 556        selections: Option<Arc<[Selection]>>,
 557        lamport_timestamp: clock::Lamport,
 558    },
 559    SetActiveSelections {
 560        set_id: Option<SelectionSetId>,
 561        lamport_timestamp: clock::Lamport,
 562    },
 563    #[cfg(test)]
 564    Test(clock::Lamport),
 565}
 566
 567#[derive(Clone, Debug, Eq, PartialEq)]
 568pub struct EditOperation {
 569    timestamp: InsertionTimestamp,
 570    version: clock::Global,
 571    ranges: Vec<Range<usize>>,
 572    new_text: Option<String>,
 573}
 574
 575#[derive(Clone, Debug, Eq, PartialEq)]
 576pub struct UndoOperation {
 577    id: clock::Local,
 578    counts: HashMap<clock::Local, u32>,
 579    ranges: Vec<Range<usize>>,
 580    version: clock::Global,
 581}
 582
 583impl Deref for Buffer {
 584    type Target = TextBuffer;
 585
 586    fn deref(&self) -> &Self::Target {
 587        &self.buffer
 588    }
 589}
 590
 591impl TextBuffer {
 592    pub fn new(replica_id: u16, remote_id: u64, history: History) -> TextBuffer {
 593        let mut fragments = SumTree::new();
 594
 595        let visible_text = Rope::from(history.base_text.as_ref());
 596        if visible_text.len() > 0 {
 597            fragments.push(
 598                Fragment {
 599                    timestamp: Default::default(),
 600                    len: visible_text.len(),
 601                    visible: true,
 602                    deletions: Default::default(),
 603                    max_undos: Default::default(),
 604                },
 605                &None,
 606            );
 607        }
 608
 609        TextBuffer {
 610            visible_text,
 611            deleted_text: Rope::new(),
 612            fragments,
 613            version: clock::Global::new(),
 614            last_edit: clock::Local::default(),
 615            undo_map: Default::default(),
 616            history,
 617            selections: HashMap::default(),
 618            deferred_ops: OperationQueue::new(),
 619            deferred_replicas: HashSet::default(),
 620            replica_id,
 621            remote_id,
 622            local_clock: clock::Local::new(replica_id),
 623            lamport_clock: clock::Lamport::new(replica_id),
 624        }
 625    }
 626
 627    pub fn version(&self) -> clock::Global {
 628        self.version.clone()
 629    }
 630
 631    fn content<'a>(&'a self) -> Content<'a> {
 632        self.into()
 633    }
 634
 635    pub fn as_rope(&self) -> &Rope {
 636        &self.visible_text
 637    }
 638
 639    pub fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
 640        self.content().text_summary_for_range(range)
 641    }
 642
 643    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
 644        self.anchor_at(position, Bias::Left)
 645    }
 646
 647    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
 648        self.anchor_at(position, Bias::Right)
 649    }
 650
 651    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
 652        self.content().anchor_at(position, bias)
 653    }
 654
 655    pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
 656        self.content().point_for_offset(offset)
 657    }
 658
 659    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
 660        self.visible_text.clip_point(point, bias)
 661    }
 662
 663    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
 664        self.visible_text.clip_offset(offset, bias)
 665    }
 666
 667    pub fn replica_id(&self) -> ReplicaId {
 668        self.local_clock.replica_id
 669    }
 670
 671    pub fn remote_id(&self) -> u64 {
 672        self.remote_id
 673    }
 674
 675    pub fn text_summary(&self) -> TextSummary {
 676        self.visible_text.summary()
 677    }
 678
 679    pub fn len(&self) -> usize {
 680        self.content().len()
 681    }
 682
 683    pub fn line_len(&self, row: u32) -> u32 {
 684        self.content().line_len(row)
 685    }
 686
 687    pub fn max_point(&self) -> Point {
 688        self.visible_text.max_point()
 689    }
 690
 691    pub fn row_count(&self) -> u32 {
 692        self.max_point().row + 1
 693    }
 694
 695    pub fn text(&self) -> String {
 696        self.text_for_range(0..self.len()).collect()
 697    }
 698
 699    pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
 700        self.content().text_for_range(range)
 701    }
 702
 703    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
 704        self.chars_at(0)
 705    }
 706
 707    pub fn chars_at<'a, T: 'a + ToOffset>(
 708        &'a self,
 709        position: T,
 710    ) -> impl Iterator<Item = char> + 'a {
 711        self.content().chars_at(position)
 712    }
 713
 714    pub fn reversed_chars_at<'a, T: 'a + ToOffset>(
 715        &'a self,
 716        position: T,
 717    ) -> impl Iterator<Item = char> + 'a {
 718        self.content().reversed_chars_at(position)
 719    }
 720
 721    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
 722        self.text_for_range(range).flat_map(str::chars)
 723    }
 724
 725    pub fn bytes_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = u8> + '_ {
 726        let offset = position.to_offset(self);
 727        self.visible_text.bytes_at(offset)
 728    }
 729
 730    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
 731    where
 732        T: ToOffset,
 733    {
 734        let position = position.to_offset(self);
 735        position == self.clip_offset(position, Bias::Left)
 736            && self
 737                .bytes_at(position)
 738                .take(needle.len())
 739                .eq(needle.bytes())
 740    }
 741
 742    pub fn deferred_ops_len(&self) -> usize {
 743        self.deferred_ops.len()
 744    }
 745
 746    pub fn edit<R, I, S, T>(&mut self, ranges: R, new_text: T) -> EditOperation
 747    where
 748        R: IntoIterator<IntoIter = I>,
 749        I: ExactSizeIterator<Item = Range<S>>,
 750        S: ToOffset,
 751        T: Into<String>,
 752    {
 753        let new_text = new_text.into();
 754        let new_text_len = new_text.len();
 755        let new_text = if new_text_len > 0 {
 756            Some(new_text)
 757        } else {
 758            None
 759        };
 760
 761        self.start_transaction(None).unwrap();
 762        let timestamp = InsertionTimestamp {
 763            replica_id: self.replica_id,
 764            local: self.local_clock.tick().value,
 765            lamport: self.lamport_clock.tick().value,
 766        };
 767        let edit = self.apply_local_edit(ranges.into_iter(), new_text, timestamp);
 768
 769        self.history.push(edit.clone());
 770        self.history.push_undo(edit.timestamp.local());
 771        self.last_edit = edit.timestamp.local();
 772        self.version.observe(edit.timestamp.local());
 773        self.end_transaction(None);
 774        edit
 775    }
 776
 777    fn apply_local_edit<S: ToOffset>(
 778        &mut self,
 779        ranges: impl ExactSizeIterator<Item = Range<S>>,
 780        new_text: Option<String>,
 781        timestamp: InsertionTimestamp,
 782    ) -> EditOperation {
 783        let mut edit = EditOperation {
 784            timestamp,
 785            version: self.version(),
 786            ranges: Vec::with_capacity(ranges.len()),
 787            new_text: None,
 788        };
 789
 790        let mut ranges = ranges
 791            .map(|range| range.start.to_offset(&*self)..range.end.to_offset(&*self))
 792            .peekable();
 793
 794        let mut new_ropes =
 795            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
 796        let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>();
 797        let mut new_fragments =
 798            old_fragments.slice(&ranges.peek().unwrap().start, Bias::Right, &None);
 799        new_ropes.push_tree(new_fragments.summary().text);
 800
 801        let mut fragment_start = old_fragments.start().visible;
 802        for range in ranges {
 803            let fragment_end = old_fragments.end(&None).visible;
 804
 805            // If the current fragment ends before this range, then jump ahead to the first fragment
 806            // that extends past the start of this range, reusing any intervening fragments.
 807            if fragment_end < range.start {
 808                // If the current fragment has been partially consumed, then consume the rest of it
 809                // and advance to the next fragment before slicing.
 810                if fragment_start > old_fragments.start().visible {
 811                    if fragment_end > fragment_start {
 812                        let mut suffix = old_fragments.item().unwrap().clone();
 813                        suffix.len = fragment_end - fragment_start;
 814                        new_ropes.push_fragment(&suffix, suffix.visible);
 815                        new_fragments.push(suffix, &None);
 816                    }
 817                    old_fragments.next(&None);
 818                }
 819
 820                let slice = old_fragments.slice(&range.start, Bias::Right, &None);
 821                new_ropes.push_tree(slice.summary().text);
 822                new_fragments.push_tree(slice, &None);
 823                fragment_start = old_fragments.start().visible;
 824            }
 825
 826            let full_range_start = range.start + old_fragments.start().deleted;
 827
 828            // Preserve any portion of the current fragment that precedes this range.
 829            if fragment_start < range.start {
 830                let mut prefix = old_fragments.item().unwrap().clone();
 831                prefix.len = range.start - fragment_start;
 832                new_ropes.push_fragment(&prefix, prefix.visible);
 833                new_fragments.push(prefix, &None);
 834                fragment_start = range.start;
 835            }
 836
 837            // Insert the new text before any existing fragments within the range.
 838            if let Some(new_text) = new_text.as_deref() {
 839                new_ropes.push_str(new_text);
 840                new_fragments.push(
 841                    Fragment {
 842                        timestamp,
 843                        len: new_text.len(),
 844                        deletions: Default::default(),
 845                        max_undos: Default::default(),
 846                        visible: true,
 847                    },
 848                    &None,
 849                );
 850            }
 851
 852            // Advance through every fragment that intersects this range, marking the intersecting
 853            // portions as deleted.
 854            while fragment_start < range.end {
 855                let fragment = old_fragments.item().unwrap();
 856                let fragment_end = old_fragments.end(&None).visible;
 857                let mut intersection = fragment.clone();
 858                let intersection_end = cmp::min(range.end, fragment_end);
 859                if fragment.visible {
 860                    intersection.len = intersection_end - fragment_start;
 861                    intersection.deletions.insert(timestamp.local());
 862                    intersection.visible = false;
 863                }
 864                if intersection.len > 0 {
 865                    new_ropes.push_fragment(&intersection, fragment.visible);
 866                    new_fragments.push(intersection, &None);
 867                    fragment_start = intersection_end;
 868                }
 869                if fragment_end <= range.end {
 870                    old_fragments.next(&None);
 871                }
 872            }
 873
 874            let full_range_end = range.end + old_fragments.start().deleted;
 875            edit.ranges.push(full_range_start..full_range_end);
 876        }
 877
 878        // If the current fragment has been partially consumed, then consume the rest of it
 879        // and advance to the next fragment before slicing.
 880        if fragment_start > old_fragments.start().visible {
 881            let fragment_end = old_fragments.end(&None).visible;
 882            if fragment_end > fragment_start {
 883                let mut suffix = old_fragments.item().unwrap().clone();
 884                suffix.len = fragment_end - fragment_start;
 885                new_ropes.push_fragment(&suffix, suffix.visible);
 886                new_fragments.push(suffix, &None);
 887            }
 888            old_fragments.next(&None);
 889        }
 890
 891        let suffix = old_fragments.suffix(&None);
 892        new_ropes.push_tree(suffix.summary().text);
 893        new_fragments.push_tree(suffix, &None);
 894        let (visible_text, deleted_text) = new_ropes.finish();
 895        drop(old_fragments);
 896
 897        self.fragments = new_fragments;
 898        self.visible_text = visible_text;
 899        self.deleted_text = deleted_text;
 900        edit.new_text = new_text;
 901        edit
 902    }
 903
 904    pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I) -> Result<()> {
 905        let mut deferred_ops = Vec::new();
 906        for op in ops {
 907            if self.can_apply_op(&op) {
 908                self.apply_op(op)?;
 909            } else {
 910                self.deferred_replicas.insert(op.replica_id());
 911                deferred_ops.push(op);
 912            }
 913        }
 914        self.deferred_ops.insert(deferred_ops);
 915        self.flush_deferred_ops()?;
 916        Ok(())
 917    }
 918
 919    fn apply_op(&mut self, op: Operation) -> Result<()> {
 920        match op {
 921            Operation::Edit(edit) => {
 922                if !self.version.observed(edit.timestamp.local()) {
 923                    self.apply_remote_edit(
 924                        &edit.version,
 925                        &edit.ranges,
 926                        edit.new_text.as_deref(),
 927                        edit.timestamp,
 928                    );
 929                    self.version.observe(edit.timestamp.local());
 930                    self.history.push(edit);
 931                }
 932            }
 933            Operation::Undo {
 934                undo,
 935                lamport_timestamp,
 936            } => {
 937                if !self.version.observed(undo.id) {
 938                    self.apply_undo(&undo)?;
 939                    self.version.observe(undo.id);
 940                    self.lamport_clock.observe(lamport_timestamp);
 941                }
 942            }
 943            Operation::UpdateSelections {
 944                set_id,
 945                selections,
 946                lamport_timestamp,
 947            } => {
 948                if let Some(selections) = selections {
 949                    if let Some(set) = self.selections.get_mut(&set_id) {
 950                        set.selections = selections;
 951                    } else {
 952                        self.selections.insert(
 953                            set_id,
 954                            SelectionSet {
 955                                selections,
 956                                active: false,
 957                            },
 958                        );
 959                    }
 960                } else {
 961                    self.selections.remove(&set_id);
 962                }
 963                self.lamport_clock.observe(lamport_timestamp);
 964            }
 965            Operation::SetActiveSelections {
 966                set_id,
 967                lamport_timestamp,
 968            } => {
 969                for (id, set) in &mut self.selections {
 970                    if id.replica_id == lamport_timestamp.replica_id {
 971                        if Some(*id) == set_id {
 972                            set.active = true;
 973                        } else {
 974                            set.active = false;
 975                        }
 976                    }
 977                }
 978                self.lamport_clock.observe(lamport_timestamp);
 979            }
 980            #[cfg(test)]
 981            Operation::Test(_) => {}
 982        }
 983        Ok(())
 984    }
 985
 986    fn apply_remote_edit(
 987        &mut self,
 988        version: &clock::Global,
 989        ranges: &[Range<usize>],
 990        new_text: Option<&str>,
 991        timestamp: InsertionTimestamp,
 992    ) {
 993        if ranges.is_empty() {
 994            return;
 995        }
 996
 997        let cx = Some(version.clone());
 998        let mut new_ropes =
 999            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1000        let mut old_fragments = self.fragments.cursor::<VersionedOffset>();
1001        let mut new_fragments =
1002            old_fragments.slice(&VersionedOffset::Offset(ranges[0].start), Bias::Left, &cx);
1003        new_ropes.push_tree(new_fragments.summary().text);
1004
1005        let mut fragment_start = old_fragments.start().offset();
1006        for range in ranges {
1007            let fragment_end = old_fragments.end(&cx).offset();
1008
1009            // If the current fragment ends before this range, then jump ahead to the first fragment
1010            // that extends past the start of this range, reusing any intervening fragments.
1011            if fragment_end < range.start {
1012                // If the current fragment has been partially consumed, then consume the rest of it
1013                // and advance to the next fragment before slicing.
1014                if fragment_start > old_fragments.start().offset() {
1015                    if fragment_end > fragment_start {
1016                        let mut suffix = old_fragments.item().unwrap().clone();
1017                        suffix.len = fragment_end - fragment_start;
1018                        new_ropes.push_fragment(&suffix, suffix.visible);
1019                        new_fragments.push(suffix, &None);
1020                    }
1021                    old_fragments.next(&cx);
1022                }
1023
1024                let slice =
1025                    old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Left, &cx);
1026                new_ropes.push_tree(slice.summary().text);
1027                new_fragments.push_tree(slice, &None);
1028                fragment_start = old_fragments.start().offset();
1029            }
1030
1031            // If we are at the end of a non-concurrent fragment, advance to the next one.
1032            let fragment_end = old_fragments.end(&cx).offset();
1033            if fragment_end == range.start && fragment_end > fragment_start {
1034                let mut fragment = old_fragments.item().unwrap().clone();
1035                fragment.len = fragment_end - fragment_start;
1036                new_ropes.push_fragment(&fragment, fragment.visible);
1037                new_fragments.push(fragment, &None);
1038                old_fragments.next(&cx);
1039                fragment_start = old_fragments.start().offset();
1040            }
1041
1042            // Skip over insertions that are concurrent to this edit, but have a lower lamport
1043            // timestamp.
1044            while let Some(fragment) = old_fragments.item() {
1045                if fragment_start == range.start
1046                    && fragment.timestamp.lamport() > timestamp.lamport()
1047                {
1048                    new_ropes.push_fragment(fragment, fragment.visible);
1049                    new_fragments.push(fragment.clone(), &None);
1050                    old_fragments.next(&cx);
1051                    debug_assert_eq!(fragment_start, range.start);
1052                } else {
1053                    break;
1054                }
1055            }
1056            debug_assert!(fragment_start <= range.start);
1057
1058            // Preserve any portion of the current fragment that precedes this range.
1059            if fragment_start < range.start {
1060                let mut prefix = old_fragments.item().unwrap().clone();
1061                prefix.len = range.start - fragment_start;
1062                fragment_start = range.start;
1063                new_ropes.push_fragment(&prefix, prefix.visible);
1064                new_fragments.push(prefix, &None);
1065            }
1066
1067            // Insert the new text before any existing fragments within the range.
1068            if let Some(new_text) = new_text {
1069                new_ropes.push_str(new_text);
1070                new_fragments.push(
1071                    Fragment {
1072                        timestamp,
1073                        len: new_text.len(),
1074                        deletions: Default::default(),
1075                        max_undos: Default::default(),
1076                        visible: true,
1077                    },
1078                    &None,
1079                );
1080            }
1081
1082            // Advance through every fragment that intersects this range, marking the intersecting
1083            // portions as deleted.
1084            while fragment_start < range.end {
1085                let fragment = old_fragments.item().unwrap();
1086                let fragment_end = old_fragments.end(&cx).offset();
1087                let mut intersection = fragment.clone();
1088                let intersection_end = cmp::min(range.end, fragment_end);
1089                if fragment.was_visible(version, &self.undo_map) {
1090                    intersection.len = intersection_end - fragment_start;
1091                    intersection.deletions.insert(timestamp.local());
1092                    intersection.visible = false;
1093                }
1094                if intersection.len > 0 {
1095                    new_ropes.push_fragment(&intersection, fragment.visible);
1096                    new_fragments.push(intersection, &None);
1097                    fragment_start = intersection_end;
1098                }
1099                if fragment_end <= range.end {
1100                    old_fragments.next(&cx);
1101                }
1102            }
1103        }
1104
1105        // If the current fragment has been partially consumed, then consume the rest of it
1106        // and advance to the next fragment before slicing.
1107        if fragment_start > old_fragments.start().offset() {
1108            let fragment_end = old_fragments.end(&cx).offset();
1109            if fragment_end > fragment_start {
1110                let mut suffix = old_fragments.item().unwrap().clone();
1111                suffix.len = fragment_end - fragment_start;
1112                new_ropes.push_fragment(&suffix, suffix.visible);
1113                new_fragments.push(suffix, &None);
1114            }
1115            old_fragments.next(&cx);
1116        }
1117
1118        let suffix = old_fragments.suffix(&cx);
1119        new_ropes.push_tree(suffix.summary().text);
1120        new_fragments.push_tree(suffix, &None);
1121        let (visible_text, deleted_text) = new_ropes.finish();
1122        drop(old_fragments);
1123
1124        self.fragments = new_fragments;
1125        self.visible_text = visible_text;
1126        self.deleted_text = deleted_text;
1127        self.local_clock.observe(timestamp.local());
1128        self.lamport_clock.observe(timestamp.lamport());
1129    }
1130
1131    fn apply_undo(&mut self, undo: &UndoOperation) -> Result<()> {
1132        self.undo_map.insert(undo);
1133
1134        let mut cx = undo.version.clone();
1135        for edit_id in undo.counts.keys().copied() {
1136            cx.observe(edit_id);
1137        }
1138        let cx = Some(cx);
1139
1140        let mut old_fragments = self.fragments.cursor::<VersionedOffset>();
1141        let mut new_fragments = old_fragments.slice(
1142            &VersionedOffset::Offset(undo.ranges[0].start),
1143            Bias::Right,
1144            &cx,
1145        );
1146        let mut new_ropes =
1147            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1148        new_ropes.push_tree(new_fragments.summary().text);
1149
1150        for range in &undo.ranges {
1151            let mut end_offset = old_fragments.end(&cx).offset();
1152
1153            if end_offset < range.start {
1154                let preceding_fragments =
1155                    old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Right, &cx);
1156                new_ropes.push_tree(preceding_fragments.summary().text);
1157                new_fragments.push_tree(preceding_fragments, &None);
1158            }
1159
1160            while end_offset <= range.end {
1161                if let Some(fragment) = old_fragments.item() {
1162                    let mut fragment = fragment.clone();
1163                    let fragment_was_visible = fragment.visible;
1164
1165                    if fragment.was_visible(&undo.version, &self.undo_map)
1166                        || undo.counts.contains_key(&fragment.timestamp.local())
1167                    {
1168                        fragment.visible = fragment.is_visible(&self.undo_map);
1169                        fragment.max_undos.observe(undo.id);
1170                    }
1171                    new_ropes.push_fragment(&fragment, fragment_was_visible);
1172                    new_fragments.push(fragment, &None);
1173
1174                    old_fragments.next(&cx);
1175                    if end_offset == old_fragments.end(&cx).offset() {
1176                        let unseen_fragments = old_fragments.slice(
1177                            &VersionedOffset::Offset(end_offset),
1178                            Bias::Right,
1179                            &cx,
1180                        );
1181                        new_ropes.push_tree(unseen_fragments.summary().text);
1182                        new_fragments.push_tree(unseen_fragments, &None);
1183                    }
1184                    end_offset = old_fragments.end(&cx).offset();
1185                } else {
1186                    break;
1187                }
1188            }
1189        }
1190
1191        let suffix = old_fragments.suffix(&cx);
1192        new_ropes.push_tree(suffix.summary().text);
1193        new_fragments.push_tree(suffix, &None);
1194
1195        drop(old_fragments);
1196        let (visible_text, deleted_text) = new_ropes.finish();
1197        self.fragments = new_fragments;
1198        self.visible_text = visible_text;
1199        self.deleted_text = deleted_text;
1200        Ok(())
1201    }
1202
1203    fn flush_deferred_ops(&mut self) -> Result<()> {
1204        self.deferred_replicas.clear();
1205        let mut deferred_ops = Vec::new();
1206        for op in self.deferred_ops.drain().cursor().cloned() {
1207            if self.can_apply_op(&op) {
1208                self.apply_op(op)?;
1209            } else {
1210                self.deferred_replicas.insert(op.replica_id());
1211                deferred_ops.push(op);
1212            }
1213        }
1214        self.deferred_ops.insert(deferred_ops);
1215        Ok(())
1216    }
1217
1218    fn can_apply_op(&self, op: &Operation) -> bool {
1219        if self.deferred_replicas.contains(&op.replica_id()) {
1220            false
1221        } else {
1222            match op {
1223                Operation::Edit(edit) => self.version >= edit.version,
1224                Operation::Undo { undo, .. } => self.version >= undo.version,
1225                Operation::UpdateSelections { selections, .. } => {
1226                    if let Some(selections) = selections {
1227                        selections.iter().all(|selection| {
1228                            let contains_start = self.version >= selection.start.version;
1229                            let contains_end = self.version >= selection.end.version;
1230                            contains_start && contains_end
1231                        })
1232                    } else {
1233                        true
1234                    }
1235                }
1236                Operation::SetActiveSelections { set_id, .. } => {
1237                    set_id.map_or(true, |set_id| self.selections.contains_key(&set_id))
1238                }
1239                #[cfg(test)]
1240                Operation::Test(_) => true,
1241            }
1242        }
1243    }
1244
1245    pub fn peek_undo_stack(&self) -> Option<&Transaction> {
1246        self.history.undo_stack.last()
1247    }
1248
1249    pub fn start_transaction(
1250        &mut self,
1251        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1252    ) -> Result<()> {
1253        self.start_transaction_at(selection_set_ids, Instant::now())?;
1254        Ok(())
1255    }
1256
1257    fn start_transaction_at(
1258        &mut self,
1259        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1260        now: Instant,
1261    ) -> Result<()> {
1262        let selections = selection_set_ids
1263            .into_iter()
1264            .map(|set_id| {
1265                let set = self
1266                    .selections
1267                    .get(&set_id)
1268                    .expect("invalid selection set id");
1269                (set_id, set.selections.clone())
1270            })
1271            .collect();
1272        self.history
1273            .start_transaction(self.version.clone(), selections, now);
1274        Ok(())
1275    }
1276
1277    fn end_transaction(&mut self, selection_set_ids: impl IntoIterator<Item = SelectionSetId>) {
1278        self.end_transaction_at(selection_set_ids, Instant::now());
1279    }
1280
1281    fn end_transaction_at(
1282        &mut self,
1283        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1284        now: Instant,
1285    ) -> Option<clock::Global> {
1286        let selections = selection_set_ids
1287            .into_iter()
1288            .map(|set_id| {
1289                let set = self
1290                    .selections
1291                    .get(&set_id)
1292                    .expect("invalid selection set id");
1293                (set_id, set.selections.clone())
1294            })
1295            .collect();
1296
1297        if let Some(transaction) = self.history.end_transaction(selections, now) {
1298            let since = transaction.start.clone();
1299            self.history.group();
1300            Some(since)
1301        } else {
1302            None
1303        }
1304    }
1305
1306    fn remove_peer(&mut self, replica_id: ReplicaId) {
1307        self.selections
1308            .retain(|set_id, _| set_id.replica_id != replica_id)
1309    }
1310
1311    fn undo(&mut self) -> Vec<Operation> {
1312        let mut ops = Vec::new();
1313        if let Some(transaction) = self.history.pop_undo().cloned() {
1314            let selections = transaction.selections_before.clone();
1315            ops.push(self.undo_or_redo(transaction).unwrap());
1316            for (set_id, selections) in selections {
1317                ops.extend(self.update_selection_set(set_id, selections));
1318            }
1319        }
1320        ops
1321    }
1322
1323    fn redo(&mut self) -> Vec<Operation> {
1324        let mut ops = Vec::new();
1325        if let Some(transaction) = self.history.pop_redo().cloned() {
1326            let selections = transaction.selections_after.clone();
1327            ops.push(self.undo_or_redo(transaction).unwrap());
1328            for (set_id, selections) in selections {
1329                ops.extend(self.update_selection_set(set_id, selections));
1330            }
1331        }
1332        ops
1333    }
1334
1335    fn undo_or_redo(&mut self, transaction: Transaction) -> Result<Operation> {
1336        let mut counts = HashMap::default();
1337        for edit_id in transaction.edits {
1338            counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1339        }
1340
1341        let undo = UndoOperation {
1342            id: self.local_clock.tick(),
1343            counts,
1344            ranges: transaction.ranges,
1345            version: transaction.start.clone(),
1346        };
1347        self.apply_undo(&undo)?;
1348        self.version.observe(undo.id);
1349
1350        Ok(Operation::Undo {
1351            undo,
1352            lamport_timestamp: self.lamport_clock.tick(),
1353        })
1354    }
1355
1356    pub fn selection_set(&self, set_id: SelectionSetId) -> Result<&SelectionSet> {
1357        self.selections
1358            .get(&set_id)
1359            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))
1360    }
1361
1362    pub fn selection_sets(&self) -> impl Iterator<Item = (&SelectionSetId, &SelectionSet)> {
1363        self.selections.iter()
1364    }
1365
1366    pub fn update_selection_set(
1367        &mut self,
1368        set_id: SelectionSetId,
1369        selections: impl Into<Arc<[Selection]>>,
1370    ) -> Result<Operation> {
1371        let selections = selections.into();
1372        let set = self
1373            .selections
1374            .get_mut(&set_id)
1375            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?;
1376        set.selections = selections.clone();
1377        Ok(Operation::UpdateSelections {
1378            set_id,
1379            selections: Some(selections),
1380            lamport_timestamp: self.lamport_clock.tick(),
1381        })
1382    }
1383
1384    pub fn add_selection_set(&mut self, selections: impl Into<Arc<[Selection]>>) -> Operation {
1385        let selections = selections.into();
1386        let lamport_timestamp = self.lamport_clock.tick();
1387        self.selections.insert(
1388            lamport_timestamp,
1389            SelectionSet {
1390                selections: selections.clone(),
1391                active: false,
1392            },
1393        );
1394        Operation::UpdateSelections {
1395            set_id: lamport_timestamp,
1396            selections: Some(selections),
1397            lamport_timestamp,
1398        }
1399    }
1400
1401    pub fn set_active_selection_set(
1402        &mut self,
1403        set_id: Option<SelectionSetId>,
1404    ) -> Result<Operation> {
1405        if let Some(set_id) = set_id {
1406            assert_eq!(set_id.replica_id, self.replica_id());
1407        }
1408
1409        for (id, set) in &mut self.selections {
1410            if id.replica_id == self.local_clock.replica_id {
1411                if Some(*id) == set_id {
1412                    set.active = true;
1413                } else {
1414                    set.active = false;
1415                }
1416            }
1417        }
1418
1419        Ok(Operation::SetActiveSelections {
1420            set_id,
1421            lamport_timestamp: self.lamport_clock.tick(),
1422        })
1423    }
1424
1425    pub fn remove_selection_set(&mut self, set_id: SelectionSetId) -> Result<Operation> {
1426        self.selections
1427            .remove(&set_id)
1428            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?;
1429        Ok(Operation::UpdateSelections {
1430            set_id,
1431            selections: None,
1432            lamport_timestamp: self.lamport_clock.tick(),
1433        })
1434    }
1435
1436    pub fn edits_since<'a>(&'a self, since: clock::Global) -> impl 'a + Iterator<Item = Edit> {
1437        let since_2 = since.clone();
1438        let cursor = if since == self.version {
1439            None
1440        } else {
1441            Some(self.fragments.filter(
1442                move |summary| summary.max_version.changed_since(&since_2),
1443                &None,
1444            ))
1445        };
1446
1447        Edits {
1448            visible_text: &self.visible_text,
1449            deleted_text: &self.deleted_text,
1450            cursor,
1451            undos: &self.undo_map,
1452            since,
1453            old_offset: 0,
1454            new_offset: 0,
1455            old_point: Point::zero(),
1456            new_point: Point::zero(),
1457        }
1458    }
1459}
1460
1461impl Buffer {
1462    pub fn new<T: Into<Arc<str>>>(
1463        replica_id: ReplicaId,
1464        base_text: T,
1465        cx: &mut ModelContext<Self>,
1466    ) -> Self {
1467        Self::build(
1468            replica_id,
1469            History::new(base_text.into()),
1470            None,
1471            cx.model_id() as u64,
1472            None,
1473            cx,
1474        )
1475    }
1476
1477    pub fn from_history(
1478        replica_id: ReplicaId,
1479        history: History,
1480        file: Option<Box<dyn File>>,
1481        language: Option<Arc<Language>>,
1482        cx: &mut ModelContext<Self>,
1483    ) -> Self {
1484        Self::build(
1485            replica_id,
1486            history,
1487            file,
1488            cx.model_id() as u64,
1489            language,
1490            cx,
1491        )
1492    }
1493
1494    fn build(
1495        replica_id: ReplicaId,
1496        history: History,
1497        file: Option<Box<dyn File>>,
1498        remote_id: u64,
1499        language: Option<Arc<Language>>,
1500        cx: &mut ModelContext<Self>,
1501    ) -> Self {
1502        let saved_mtime;
1503        if let Some(file) = file.as_ref() {
1504            saved_mtime = file.mtime();
1505        } else {
1506            saved_mtime = UNIX_EPOCH;
1507        }
1508
1509        let mut result = Self {
1510            buffer: TextBuffer::new(replica_id, remote_id, history),
1511            saved_mtime,
1512            saved_version: clock::Global::new(),
1513            file,
1514            syntax_tree: Mutex::new(None),
1515            parsing_in_background: false,
1516            parse_count: 0,
1517            sync_parse_timeout: Duration::from_millis(1),
1518            autoindent_requests: Default::default(),
1519            pending_autoindent: Default::default(),
1520            language,
1521
1522            #[cfg(test)]
1523            operations: Default::default(),
1524        };
1525        result.reparse(cx);
1526        result
1527    }
1528
1529    pub fn snapshot(&self) -> Snapshot {
1530        Snapshot {
1531            visible_text: self.visible_text.clone(),
1532            fragments: self.fragments.clone(),
1533            version: self.version.clone(),
1534            tree: self.syntax_tree(),
1535            is_parsing: self.parsing_in_background,
1536            language: self.language.clone(),
1537            query_cursor: QueryCursorHandle::new(),
1538        }
1539    }
1540
1541    pub fn from_proto(
1542        replica_id: ReplicaId,
1543        message: proto::Buffer,
1544        file: Option<Box<dyn File>>,
1545        language: Option<Arc<Language>>,
1546        cx: &mut ModelContext<Self>,
1547    ) -> Result<Self> {
1548        let mut buffer = Buffer::build(
1549            replica_id,
1550            History::new(message.content.into()),
1551            file,
1552            message.id,
1553            language,
1554            cx,
1555        );
1556        let ops = message
1557            .history
1558            .into_iter()
1559            .map(|op| Operation::Edit(op.into()));
1560        buffer.apply_ops(ops, cx)?;
1561        buffer.buffer.selections = message
1562            .selections
1563            .into_iter()
1564            .map(|set| {
1565                let set_id = clock::Lamport {
1566                    replica_id: set.replica_id as ReplicaId,
1567                    value: set.local_timestamp,
1568                };
1569                let selections: Vec<Selection> = set
1570                    .selections
1571                    .into_iter()
1572                    .map(TryFrom::try_from)
1573                    .collect::<Result<_, _>>()?;
1574                let set = SelectionSet {
1575                    selections: Arc::from(selections),
1576                    active: set.is_active,
1577                };
1578                Result::<_, anyhow::Error>::Ok((set_id, set))
1579            })
1580            .collect::<Result<_, _>>()?;
1581        Ok(buffer)
1582    }
1583
1584    pub fn to_proto(&self, cx: &mut ModelContext<Self>) -> proto::Buffer {
1585        let ops = self.history.ops.values().map(Into::into).collect();
1586        proto::Buffer {
1587            id: cx.model_id() as u64,
1588            content: self.history.base_text.to_string(),
1589            history: ops,
1590            selections: self
1591                .selections
1592                .iter()
1593                .map(|(set_id, set)| proto::SelectionSetSnapshot {
1594                    replica_id: set_id.replica_id as u32,
1595                    local_timestamp: set_id.value,
1596                    selections: set.selections.iter().map(Into::into).collect(),
1597                    is_active: set.active,
1598                })
1599                .collect(),
1600        }
1601    }
1602
1603    pub fn file(&self) -> Option<&dyn File> {
1604        self.file.as_deref()
1605    }
1606
1607    pub fn file_mut(&mut self) -> Option<&mut dyn File> {
1608        self.file.as_mut().map(|f| f.deref_mut() as &mut dyn File)
1609    }
1610
1611    pub fn save(
1612        &mut self,
1613        cx: &mut ModelContext<Self>,
1614    ) -> Result<Task<Result<(clock::Global, SystemTime)>>> {
1615        let file = self
1616            .file
1617            .as_ref()
1618            .ok_or_else(|| anyhow!("buffer has no file"))?;
1619        let text = self.visible_text.clone();
1620        let version = self.version.clone();
1621        let save = file.save(self.remote_id, text, version, cx.as_mut());
1622        Ok(cx.spawn(|this, mut cx| async move {
1623            let (version, mtime) = save.await?;
1624            this.update(&mut cx, |this, cx| {
1625                this.did_save(version.clone(), mtime, None, cx);
1626            });
1627            Ok((version, mtime))
1628        }))
1629    }
1630
1631    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
1632        self.language = language;
1633        self.reparse(cx);
1634    }
1635
1636    pub fn did_save(
1637        &mut self,
1638        version: clock::Global,
1639        mtime: SystemTime,
1640        new_file: Option<Box<dyn File>>,
1641        cx: &mut ModelContext<Self>,
1642    ) {
1643        self.saved_mtime = mtime;
1644        self.saved_version = version;
1645        if let Some(new_file) = new_file {
1646            self.file = Some(new_file);
1647        }
1648        cx.emit(Event::Saved);
1649    }
1650
1651    pub fn file_updated(
1652        &mut self,
1653        path: Arc<Path>,
1654        mtime: SystemTime,
1655        new_text: Option<String>,
1656        cx: &mut ModelContext<Self>,
1657    ) {
1658        let file = self.file.as_mut().unwrap();
1659        let mut changed = false;
1660        if path != *file.path() {
1661            file.set_path(path);
1662            changed = true;
1663        }
1664
1665        if mtime != file.mtime() {
1666            file.set_mtime(mtime);
1667            changed = true;
1668            if let Some(new_text) = new_text {
1669                if self.version == self.saved_version {
1670                    cx.spawn(|this, mut cx| async move {
1671                        let diff = this
1672                            .read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
1673                            .await;
1674                        this.update(&mut cx, |this, cx| {
1675                            if this.apply_diff(diff, cx) {
1676                                this.saved_version = this.version.clone();
1677                                this.saved_mtime = mtime;
1678                                cx.emit(Event::Reloaded);
1679                            }
1680                        });
1681                    })
1682                    .detach();
1683                }
1684            }
1685        }
1686
1687        if changed {
1688            cx.emit(Event::FileHandleChanged);
1689        }
1690    }
1691
1692    pub fn file_deleted(&mut self, cx: &mut ModelContext<Self>) {
1693        if self.version == self.saved_version {
1694            cx.emit(Event::Dirtied);
1695        }
1696        cx.emit(Event::FileHandleChanged);
1697    }
1698
1699    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
1700        cx.emit(Event::Closed);
1701    }
1702
1703    pub fn language(&self) -> Option<&Arc<Language>> {
1704        self.language.as_ref()
1705    }
1706
1707    pub fn parse_count(&self) -> usize {
1708        self.parse_count
1709    }
1710
1711    fn syntax_tree(&self) -> Option<Tree> {
1712        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
1713            self.interpolate_tree(syntax_tree);
1714            Some(syntax_tree.tree.clone())
1715        } else {
1716            None
1717        }
1718    }
1719
1720    #[cfg(any(test, feature = "test-support"))]
1721    pub fn is_parsing(&self) -> bool {
1722        self.parsing_in_background
1723    }
1724
1725    #[cfg(test)]
1726    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
1727        self.sync_parse_timeout = timeout;
1728    }
1729
1730    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
1731        if self.parsing_in_background {
1732            return false;
1733        }
1734
1735        if let Some(language) = self.language.clone() {
1736            let old_tree = self.syntax_tree();
1737            let text = self.visible_text.clone();
1738            let parsed_version = self.version();
1739            let parse_task = cx.background().spawn({
1740                let language = language.clone();
1741                async move { Self::parse_text(&text, old_tree, &language) }
1742            });
1743
1744            match cx
1745                .background()
1746                .block_with_timeout(self.sync_parse_timeout, parse_task)
1747            {
1748                Ok(new_tree) => {
1749                    self.did_finish_parsing(new_tree, parsed_version, cx);
1750                    return true;
1751                }
1752                Err(parse_task) => {
1753                    self.parsing_in_background = true;
1754                    cx.spawn(move |this, mut cx| async move {
1755                        let new_tree = parse_task.await;
1756                        this.update(&mut cx, move |this, cx| {
1757                            let language_changed =
1758                                this.language.as_ref().map_or(true, |curr_language| {
1759                                    !Arc::ptr_eq(curr_language, &language)
1760                                });
1761                            let parse_again = this.version > parsed_version || language_changed;
1762                            this.parsing_in_background = false;
1763                            this.did_finish_parsing(new_tree, parsed_version, cx);
1764
1765                            if parse_again && this.reparse(cx) {
1766                                return;
1767                            }
1768                        });
1769                    })
1770                    .detach();
1771                }
1772            }
1773        }
1774        false
1775    }
1776
1777    fn parse_text(text: &Rope, old_tree: Option<Tree>, language: &Language) -> Tree {
1778        PARSER.with(|parser| {
1779            let mut parser = parser.borrow_mut();
1780            parser
1781                .set_language(language.grammar)
1782                .expect("incompatible grammar");
1783            let mut chunks = text.chunks_in_range(0..text.len());
1784            let tree = parser
1785                .parse_with(
1786                    &mut move |offset, _| {
1787                        chunks.seek(offset);
1788                        chunks.next().unwrap_or("").as_bytes()
1789                    },
1790                    old_tree.as_ref(),
1791                )
1792                .unwrap();
1793            tree
1794        })
1795    }
1796
1797    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
1798        let mut delta = 0_isize;
1799        for edit in self.edits_since(tree.version.clone()) {
1800            let start_offset = (edit.old_bytes.start as isize + delta) as usize;
1801            let start_point = self.visible_text.to_point(start_offset);
1802            tree.tree.edit(&InputEdit {
1803                start_byte: start_offset,
1804                old_end_byte: start_offset + edit.deleted_bytes(),
1805                new_end_byte: start_offset + edit.inserted_bytes(),
1806                start_position: start_point.into(),
1807                old_end_position: (start_point + edit.deleted_lines()).into(),
1808                new_end_position: self
1809                    .visible_text
1810                    .to_point(start_offset + edit.inserted_bytes())
1811                    .into(),
1812            });
1813            delta += edit.inserted_bytes() as isize - edit.deleted_bytes() as isize;
1814        }
1815        tree.version = self.version();
1816    }
1817
1818    fn did_finish_parsing(
1819        &mut self,
1820        tree: Tree,
1821        version: clock::Global,
1822        cx: &mut ModelContext<Self>,
1823    ) {
1824        self.parse_count += 1;
1825        *self.syntax_tree.lock() = Some(SyntaxTree { tree, version });
1826        self.request_autoindent(cx);
1827        cx.emit(Event::Reparsed);
1828        cx.notify();
1829    }
1830
1831    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
1832        if let Some(indent_columns) = self.compute_autoindents() {
1833            let indent_columns = cx.background().spawn(indent_columns);
1834            match cx
1835                .background()
1836                .block_with_timeout(Duration::from_micros(500), indent_columns)
1837            {
1838                Ok(indent_columns) => self.apply_autoindents(indent_columns, cx),
1839                Err(indent_columns) => {
1840                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
1841                        let indent_columns = indent_columns.await;
1842                        this.update(&mut cx, |this, cx| {
1843                            this.apply_autoindents(indent_columns, cx);
1844                        });
1845                    }));
1846                }
1847            }
1848        }
1849    }
1850
1851    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, u32>>> {
1852        let max_rows_between_yields = 100;
1853        let snapshot = self.snapshot();
1854        if snapshot.language.is_none()
1855            || snapshot.tree.is_none()
1856            || self.autoindent_requests.is_empty()
1857        {
1858            return None;
1859        }
1860
1861        let autoindent_requests = self.autoindent_requests.clone();
1862        Some(async move {
1863            let mut indent_columns = BTreeMap::new();
1864            for request in autoindent_requests {
1865                let old_to_new_rows = request
1866                    .edited
1867                    .to_points(&request.before_edit)
1868                    .map(|point| point.row)
1869                    .zip(request.edited.to_points(&snapshot).map(|point| point.row))
1870                    .collect::<BTreeMap<u32, u32>>();
1871
1872                let mut old_suggestions = HashMap::default();
1873                let old_edited_ranges =
1874                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
1875                for old_edited_range in old_edited_ranges {
1876                    let suggestions = request
1877                        .before_edit
1878                        .suggest_autoindents(old_edited_range.clone())
1879                        .into_iter()
1880                        .flatten();
1881                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
1882                        let indentation_basis = old_to_new_rows
1883                            .get(&suggestion.basis_row)
1884                            .and_then(|from_row| old_suggestions.get(from_row).copied())
1885                            .unwrap_or_else(|| {
1886                                request
1887                                    .before_edit
1888                                    .indent_column_for_line(suggestion.basis_row)
1889                            });
1890                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1891                        old_suggestions.insert(
1892                            *old_to_new_rows.get(&old_row).unwrap(),
1893                            indentation_basis + delta,
1894                        );
1895                    }
1896                    yield_now().await;
1897                }
1898
1899                // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
1900                // buffer before the edit, but keyed by the row for these lines after the edits were applied.
1901                let new_edited_row_ranges =
1902                    contiguous_ranges(old_to_new_rows.values().copied(), max_rows_between_yields);
1903                for new_edited_row_range in new_edited_row_ranges {
1904                    let suggestions = snapshot
1905                        .suggest_autoindents(new_edited_row_range.clone())
1906                        .into_iter()
1907                        .flatten();
1908                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1909                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1910                        let new_indentation = indent_columns
1911                            .get(&suggestion.basis_row)
1912                            .copied()
1913                            .unwrap_or_else(|| {
1914                                snapshot.indent_column_for_line(suggestion.basis_row)
1915                            })
1916                            + delta;
1917                        if old_suggestions
1918                            .get(&new_row)
1919                            .map_or(true, |old_indentation| new_indentation != *old_indentation)
1920                        {
1921                            indent_columns.insert(new_row, new_indentation);
1922                        }
1923                    }
1924                    yield_now().await;
1925                }
1926
1927                if let Some(inserted) = request.inserted.as_ref() {
1928                    let inserted_row_ranges = contiguous_ranges(
1929                        inserted
1930                            .to_point_ranges(&snapshot)
1931                            .flat_map(|range| range.start.row..range.end.row + 1),
1932                        max_rows_between_yields,
1933                    );
1934                    for inserted_row_range in inserted_row_ranges {
1935                        let suggestions = snapshot
1936                            .suggest_autoindents(inserted_row_range.clone())
1937                            .into_iter()
1938                            .flatten();
1939                        for (row, suggestion) in inserted_row_range.zip(suggestions) {
1940                            let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1941                            let new_indentation = indent_columns
1942                                .get(&suggestion.basis_row)
1943                                .copied()
1944                                .unwrap_or_else(|| {
1945                                    snapshot.indent_column_for_line(suggestion.basis_row)
1946                                })
1947                                + delta;
1948                            indent_columns.insert(row, new_indentation);
1949                        }
1950                        yield_now().await;
1951                    }
1952                }
1953            }
1954            indent_columns
1955        })
1956    }
1957
1958    fn apply_autoindents(
1959        &mut self,
1960        indent_columns: BTreeMap<u32, u32>,
1961        cx: &mut ModelContext<Self>,
1962    ) {
1963        let selection_set_ids = self
1964            .autoindent_requests
1965            .drain(..)
1966            .flat_map(|req| req.selection_set_ids.clone())
1967            .collect::<HashSet<_>>();
1968
1969        self.start_transaction(selection_set_ids.iter().copied())
1970            .unwrap();
1971        for (row, indent_column) in &indent_columns {
1972            self.set_indent_column_for_line(*row, *indent_column, cx);
1973        }
1974
1975        for selection_set_id in &selection_set_ids {
1976            if let Some(set) = self.selections.get(selection_set_id) {
1977                let new_selections = set
1978                    .selections
1979                    .iter()
1980                    .map(|selection| {
1981                        let start_point = selection.start.to_point(&self.buffer);
1982                        if start_point.column == 0 {
1983                            let end_point = selection.end.to_point(&self.buffer);
1984                            let delta = Point::new(
1985                                0,
1986                                indent_columns.get(&start_point.row).copied().unwrap_or(0),
1987                            );
1988                            if delta.column > 0 {
1989                                return Selection {
1990                                    id: selection.id,
1991                                    goal: selection.goal,
1992                                    reversed: selection.reversed,
1993                                    start: self
1994                                        .anchor_at(start_point + delta, selection.start.bias),
1995                                    end: self.anchor_at(end_point + delta, selection.end.bias),
1996                                };
1997                            }
1998                        }
1999                        selection.clone()
2000                    })
2001                    .collect::<Arc<[_]>>();
2002                self.update_selection_set(*selection_set_id, new_selections, cx)
2003                    .unwrap();
2004            }
2005        }
2006
2007        self.end_transaction(selection_set_ids.iter().copied(), cx)
2008            .unwrap();
2009    }
2010
2011    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2012        self.content().indent_column_for_line(row)
2013    }
2014
2015    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
2016        let current_column = self.indent_column_for_line(row);
2017        if column > current_column {
2018            let offset = self.visible_text.to_offset(Point::new(row, 0));
2019            self.edit(
2020                [offset..offset],
2021                " ".repeat((column - current_column) as usize),
2022                cx,
2023            );
2024        } else if column < current_column {
2025            self.edit(
2026                [Point::new(row, 0)..Point::new(row, current_column - column)],
2027                "",
2028                cx,
2029            );
2030        }
2031    }
2032
2033    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2034        if let Some(tree) = self.syntax_tree() {
2035            let root = tree.root_node();
2036            let range = range.start.to_offset(self)..range.end.to_offset(self);
2037            let mut node = root.descendant_for_byte_range(range.start, range.end);
2038            while node.map_or(false, |n| n.byte_range() == range) {
2039                node = node.unwrap().parent();
2040            }
2041            node.map(|n| n.byte_range())
2042        } else {
2043            None
2044        }
2045    }
2046
2047    pub fn enclosing_bracket_ranges<T: ToOffset>(
2048        &self,
2049        range: Range<T>,
2050    ) -> Option<(Range<usize>, Range<usize>)> {
2051        let (lang, tree) = self.language.as_ref().zip(self.syntax_tree())?;
2052        let open_capture_ix = lang.brackets_query.capture_index_for_name("open")?;
2053        let close_capture_ix = lang.brackets_query.capture_index_for_name("close")?;
2054
2055        // Find bracket pairs that *inclusively* contain the given range.
2056        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
2057        let mut cursor = QueryCursorHandle::new();
2058        let matches = cursor.set_byte_range(range).matches(
2059            &lang.brackets_query,
2060            tree.root_node(),
2061            TextProvider(&self.visible_text),
2062        );
2063
2064        // Get the ranges of the innermost pair of brackets.
2065        matches
2066            .filter_map(|mat| {
2067                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
2068                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
2069                Some((open.byte_range(), close.byte_range()))
2070            })
2071            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
2072    }
2073
2074    fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
2075        // TODO: it would be nice to not allocate here.
2076        let old_text = self.text();
2077        let base_version = self.version();
2078        cx.background().spawn(async move {
2079            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
2080                .iter_all_changes()
2081                .map(|c| (c.tag(), c.value().len()))
2082                .collect::<Vec<_>>();
2083            Diff {
2084                base_version,
2085                new_text,
2086                changes,
2087            }
2088        })
2089    }
2090
2091    pub fn set_text_from_disk(&self, new_text: Arc<str>, cx: &mut ModelContext<Self>) -> Task<()> {
2092        cx.spawn(|this, mut cx| async move {
2093            let diff = this
2094                .read_with(&cx, |this, cx| this.diff(new_text, cx))
2095                .await;
2096
2097            this.update(&mut cx, |this, cx| {
2098                if this.apply_diff(diff, cx) {
2099                    this.saved_version = this.version.clone();
2100                }
2101            });
2102        })
2103    }
2104
2105    fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
2106        if self.version == diff.base_version {
2107            self.start_transaction(None).unwrap();
2108            let mut offset = 0;
2109            for (tag, len) in diff.changes {
2110                let range = offset..(offset + len);
2111                match tag {
2112                    ChangeTag::Equal => offset += len,
2113                    ChangeTag::Delete => self.edit(Some(range), "", cx),
2114                    ChangeTag::Insert => {
2115                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
2116                        offset += len;
2117                    }
2118                }
2119            }
2120            self.end_transaction(None, cx).unwrap();
2121            true
2122        } else {
2123            false
2124        }
2125    }
2126
2127    pub fn is_dirty(&self) -> bool {
2128        self.version > self.saved_version
2129            || self.file.as_ref().map_or(false, |file| file.is_deleted())
2130    }
2131
2132    pub fn has_conflict(&self) -> bool {
2133        self.version > self.saved_version
2134            && self
2135                .file
2136                .as_ref()
2137                .map_or(false, |file| file.mtime() > self.saved_mtime)
2138    }
2139
2140    pub fn start_transaction(
2141        &mut self,
2142        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
2143    ) -> Result<()> {
2144        self.start_transaction_at(selection_set_ids, Instant::now())?;
2145        Ok(())
2146    }
2147
2148    fn start_transaction_at(
2149        &mut self,
2150        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
2151        now: Instant,
2152    ) -> Result<()> {
2153        self.buffer.start_transaction_at(selection_set_ids, now)?;
2154        Ok(())
2155    }
2156
2157    pub fn end_transaction(
2158        &mut self,
2159        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
2160        cx: &mut ModelContext<Self>,
2161    ) -> Result<()> {
2162        self.end_transaction_at(selection_set_ids, Instant::now(), cx)
2163    }
2164
2165    fn end_transaction_at(
2166        &mut self,
2167        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
2168        now: Instant,
2169        cx: &mut ModelContext<Self>,
2170    ) -> Result<()> {
2171        if let Some(start_version) = self.buffer.end_transaction_at(selection_set_ids, now) {
2172            cx.notify();
2173            let was_dirty = start_version != self.saved_version;
2174            let edited = self.edits_since(start_version).next().is_some();
2175            if edited {
2176                self.did_edit(was_dirty, cx);
2177                self.reparse(cx);
2178            }
2179        }
2180        Ok(())
2181    }
2182
2183    pub fn edit<I, S, T>(&mut self, ranges_iter: I, new_text: T, cx: &mut ModelContext<Self>)
2184    where
2185        I: IntoIterator<Item = Range<S>>,
2186        S: ToOffset,
2187        T: Into<String>,
2188    {
2189        self.edit_internal(ranges_iter, new_text, false, cx)
2190    }
2191
2192    pub fn edit_with_autoindent<I, S, T>(
2193        &mut self,
2194        ranges_iter: I,
2195        new_text: T,
2196        cx: &mut ModelContext<Self>,
2197    ) where
2198        I: IntoIterator<Item = Range<S>>,
2199        S: ToOffset,
2200        T: Into<String>,
2201    {
2202        self.edit_internal(ranges_iter, new_text, true, cx)
2203    }
2204
2205    pub fn edit_internal<I, S, T>(
2206        &mut self,
2207        ranges_iter: I,
2208        new_text: T,
2209        autoindent: bool,
2210        cx: &mut ModelContext<Self>,
2211    ) where
2212        I: IntoIterator<Item = Range<S>>,
2213        S: ToOffset,
2214        T: Into<String>,
2215    {
2216        let new_text = new_text.into();
2217
2218        // Skip invalid ranges and coalesce contiguous ones.
2219        let mut ranges: Vec<Range<usize>> = Vec::new();
2220        for range in ranges_iter {
2221            let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
2222            if !new_text.is_empty() || !range.is_empty() {
2223                if let Some(prev_range) = ranges.last_mut() {
2224                    if prev_range.end >= range.start {
2225                        prev_range.end = cmp::max(prev_range.end, range.end);
2226                    } else {
2227                        ranges.push(range);
2228                    }
2229                } else {
2230                    ranges.push(range);
2231                }
2232            }
2233        }
2234        if ranges.is_empty() {
2235            return;
2236        }
2237
2238        self.start_transaction(None).unwrap();
2239        self.pending_autoindent.take();
2240        let autoindent_request = if autoindent && self.language.is_some() {
2241            let before_edit = self.snapshot();
2242            let edited = self.content().anchor_set(ranges.iter().filter_map(|range| {
2243                let start = range.start.to_point(&*self);
2244                if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
2245                    None
2246                } else {
2247                    Some((range.start, Bias::Left))
2248                }
2249            }));
2250            Some((before_edit, edited))
2251        } else {
2252            None
2253        };
2254
2255        let first_newline_ix = new_text.find('\n');
2256        let new_text_len = new_text.len();
2257
2258        let edit = self.buffer.edit(ranges.iter().cloned(), new_text);
2259
2260        if let Some((before_edit, edited)) = autoindent_request {
2261            let mut inserted = None;
2262            if let Some(first_newline_ix) = first_newline_ix {
2263                let mut delta = 0isize;
2264                inserted = Some(self.content().anchor_range_set(ranges.iter().map(|range| {
2265                    let start = (delta + range.start as isize) as usize + first_newline_ix + 1;
2266                    let end = (delta + range.start as isize) as usize + new_text_len;
2267                    delta += (range.end as isize - range.start as isize) + new_text_len as isize;
2268                    (start, Bias::Left)..(end, Bias::Right)
2269                })));
2270            }
2271
2272            let selection_set_ids = self
2273                .buffer
2274                .peek_undo_stack()
2275                .unwrap()
2276                .starting_selection_set_ids()
2277                .collect();
2278            self.autoindent_requests.push(Arc::new(AutoindentRequest {
2279                selection_set_ids,
2280                before_edit,
2281                edited,
2282                inserted,
2283            }));
2284        }
2285
2286        self.end_transaction(None, cx).unwrap();
2287        self.send_operation(Operation::Edit(edit), cx);
2288    }
2289
2290    fn did_edit(&self, was_dirty: bool, cx: &mut ModelContext<Self>) {
2291        cx.emit(Event::Edited);
2292        if !was_dirty {
2293            cx.emit(Event::Dirtied);
2294        }
2295    }
2296
2297    pub fn add_selection_set(
2298        &mut self,
2299        selections: impl Into<Arc<[Selection]>>,
2300        cx: &mut ModelContext<Self>,
2301    ) -> SelectionSetId {
2302        let operation = self.buffer.add_selection_set(selections);
2303        if let Operation::UpdateSelections { set_id, .. } = &operation {
2304            let set_id = *set_id;
2305            cx.notify();
2306            self.send_operation(operation, cx);
2307            set_id
2308        } else {
2309            unreachable!()
2310        }
2311    }
2312
2313    pub fn update_selection_set(
2314        &mut self,
2315        set_id: SelectionSetId,
2316        selections: impl Into<Arc<[Selection]>>,
2317        cx: &mut ModelContext<Self>,
2318    ) -> Result<()> {
2319        let operation = self.buffer.update_selection_set(set_id, selections)?;
2320        cx.notify();
2321        self.send_operation(operation, cx);
2322        Ok(())
2323    }
2324
2325    pub fn set_active_selection_set(
2326        &mut self,
2327        set_id: Option<SelectionSetId>,
2328        cx: &mut ModelContext<Self>,
2329    ) -> Result<()> {
2330        let operation = self.buffer.set_active_selection_set(set_id)?;
2331        self.send_operation(operation, cx);
2332        Ok(())
2333    }
2334
2335    pub fn remove_selection_set(
2336        &mut self,
2337        set_id: SelectionSetId,
2338        cx: &mut ModelContext<Self>,
2339    ) -> Result<()> {
2340        let operation = self.buffer.remove_selection_set(set_id)?;
2341        cx.notify();
2342        self.send_operation(operation, cx);
2343        Ok(())
2344    }
2345
2346    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
2347        &mut self,
2348        ops: I,
2349        cx: &mut ModelContext<Self>,
2350    ) -> Result<()> {
2351        self.pending_autoindent.take();
2352
2353        let was_dirty = self.is_dirty();
2354        let old_version = self.version.clone();
2355
2356        self.buffer.apply_ops(ops)?;
2357
2358        cx.notify();
2359        if self.edits_since(old_version).next().is_some() {
2360            self.did_edit(was_dirty, cx);
2361            self.reparse(cx);
2362        }
2363
2364        Ok(())
2365    }
2366
2367    #[cfg(not(test))]
2368    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
2369        if let Some(file) = &self.file {
2370            file.buffer_updated(self.remote_id, operation, cx.as_mut());
2371        }
2372    }
2373
2374    #[cfg(test)]
2375    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
2376        self.operations.push(operation);
2377    }
2378
2379    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
2380        self.buffer.remove_peer(replica_id);
2381        cx.notify();
2382    }
2383
2384    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
2385        let was_dirty = self.is_dirty();
2386        let old_version = self.version.clone();
2387
2388        for operation in self.buffer.undo() {
2389            self.send_operation(operation, cx);
2390        }
2391
2392        cx.notify();
2393        if self.edits_since(old_version).next().is_some() {
2394            self.did_edit(was_dirty, cx);
2395            self.reparse(cx);
2396        }
2397    }
2398
2399    pub fn redo(&mut self, cx: &mut ModelContext<Self>) {
2400        let was_dirty = self.is_dirty();
2401        let old_version = self.version.clone();
2402
2403        for operation in self.buffer.redo() {
2404            self.send_operation(operation, cx);
2405        }
2406
2407        cx.notify();
2408        if self.edits_since(old_version).next().is_some() {
2409            self.did_edit(was_dirty, cx);
2410            self.reparse(cx);
2411        }
2412    }
2413}
2414
2415#[cfg(any(test, feature = "test-support"))]
2416impl Buffer {
2417    pub fn randomly_edit<T>(
2418        &mut self,
2419        rng: &mut T,
2420        old_range_count: usize,
2421        _: &mut ModelContext<Self>,
2422    ) -> (Vec<Range<usize>>, String)
2423    where
2424        T: rand::Rng,
2425    {
2426        self.buffer.randomly_edit(rng, old_range_count)
2427    }
2428
2429    pub fn randomly_mutate<T>(
2430        &mut self,
2431        rng: &mut T,
2432        _: &mut ModelContext<Self>,
2433    ) -> (Vec<Range<usize>>, String)
2434    where
2435        T: rand::Rng,
2436    {
2437        self.buffer.randomly_mutate(rng)
2438    }
2439}
2440
2441#[cfg(any(test, feature = "test-support"))]
2442impl TextBuffer {
2443    fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
2444        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
2445        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
2446        start..end
2447    }
2448
2449    pub fn randomly_edit<T>(
2450        &mut self,
2451        rng: &mut T,
2452        old_range_count: usize,
2453    ) -> (Vec<Range<usize>>, String)
2454    where
2455        T: rand::Rng,
2456    {
2457        let mut old_ranges: Vec<Range<usize>> = Vec::new();
2458        for _ in 0..old_range_count {
2459            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
2460            if last_end > self.len() {
2461                break;
2462            }
2463            old_ranges.push(self.random_byte_range(last_end, rng));
2464        }
2465        let new_text_len = rng.gen_range(0..10);
2466        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
2467            .take(new_text_len)
2468            .collect();
2469        log::info!(
2470            "mutating buffer {} at {:?}: {:?}",
2471            self.replica_id,
2472            old_ranges,
2473            new_text
2474        );
2475        self.edit(old_ranges.iter().cloned(), new_text.as_str());
2476        (old_ranges, new_text)
2477    }
2478
2479    pub fn randomly_mutate<T>(&mut self, rng: &mut T) -> (Vec<Range<usize>>, String)
2480    where
2481        T: rand::Rng,
2482    {
2483        use rand::prelude::*;
2484
2485        let (old_ranges, new_text) = self.randomly_edit(rng, 5);
2486
2487        // Randomly add, remove or mutate selection sets.
2488        let replica_selection_sets = &self
2489            .selection_sets()
2490            .map(|(set_id, _)| *set_id)
2491            .filter(|set_id| self.replica_id == set_id.replica_id)
2492            .collect::<Vec<_>>();
2493        let set_id = replica_selection_sets.choose(rng);
2494        if set_id.is_some() && rng.gen_bool(1.0 / 6.0) {
2495            self.remove_selection_set(*set_id.unwrap()).unwrap();
2496        } else {
2497            let mut ranges = Vec::new();
2498            for _ in 0..5 {
2499                ranges.push(self.random_byte_range(0, rng));
2500            }
2501            let new_selections = self.selections_from_ranges(ranges).unwrap();
2502
2503            if set_id.is_none() || rng.gen_bool(1.0 / 5.0) {
2504                self.add_selection_set(new_selections);
2505            } else {
2506                self.update_selection_set(*set_id.unwrap(), new_selections)
2507                    .unwrap();
2508            }
2509        }
2510
2511        (old_ranges, new_text)
2512    }
2513
2514    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) {
2515        use rand::prelude::*;
2516
2517        for _ in 0..rng.gen_range(1..=5) {
2518            if let Some(transaction) = self.history.undo_stack.choose(rng).cloned() {
2519                log::info!(
2520                    "undoing buffer {} transaction {:?}",
2521                    self.replica_id,
2522                    transaction
2523                );
2524                self.undo_or_redo(transaction).unwrap();
2525            }
2526        }
2527    }
2528
2529    fn selections_from_ranges<I>(&self, ranges: I) -> Result<Vec<Selection>>
2530    where
2531        I: IntoIterator<Item = Range<usize>>,
2532    {
2533        use std::sync::atomic::{self, AtomicUsize};
2534
2535        static NEXT_SELECTION_ID: AtomicUsize = AtomicUsize::new(0);
2536
2537        let mut ranges = ranges.into_iter().collect::<Vec<_>>();
2538        ranges.sort_unstable_by_key(|range| range.start);
2539
2540        let mut selections = Vec::with_capacity(ranges.len());
2541        for range in ranges {
2542            if range.start > range.end {
2543                selections.push(Selection {
2544                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2545                    start: self.anchor_before(range.end),
2546                    end: self.anchor_before(range.start),
2547                    reversed: true,
2548                    goal: SelectionGoal::None,
2549                });
2550            } else {
2551                selections.push(Selection {
2552                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2553                    start: self.anchor_after(range.start),
2554                    end: self.anchor_before(range.end),
2555                    reversed: false,
2556                    goal: SelectionGoal::None,
2557                });
2558            }
2559        }
2560        Ok(selections)
2561    }
2562
2563    pub fn selection_ranges<'a>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<usize>>> {
2564        Ok(self
2565            .selection_set(set_id)?
2566            .selections
2567            .iter()
2568            .map(move |selection| {
2569                let start = selection.start.to_offset(self);
2570                let end = selection.end.to_offset(self);
2571                if selection.reversed {
2572                    end..start
2573                } else {
2574                    start..end
2575                }
2576            })
2577            .collect())
2578    }
2579
2580    pub fn all_selection_ranges<'a>(
2581        &'a self,
2582    ) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)> {
2583        self.selections
2584            .keys()
2585            .map(move |set_id| (*set_id, self.selection_ranges(*set_id).unwrap()))
2586    }
2587}
2588
2589impl Clone for Buffer {
2590    fn clone(&self) -> Self {
2591        Self {
2592            buffer: self.buffer.clone(),
2593            saved_version: self.saved_version.clone(),
2594            saved_mtime: self.saved_mtime,
2595            file: self.file.as_ref().map(|f| f.boxed_clone()),
2596            language: self.language.clone(),
2597            syntax_tree: Mutex::new(self.syntax_tree.lock().clone()),
2598            parsing_in_background: false,
2599            sync_parse_timeout: self.sync_parse_timeout,
2600            parse_count: self.parse_count,
2601            autoindent_requests: Default::default(),
2602            pending_autoindent: Default::default(),
2603
2604            #[cfg(test)]
2605            operations: self.operations.clone(),
2606        }
2607    }
2608}
2609
2610pub struct Snapshot {
2611    visible_text: Rope,
2612    fragments: SumTree<Fragment>,
2613    version: clock::Global,
2614    tree: Option<Tree>,
2615    is_parsing: bool,
2616    language: Option<Arc<Language>>,
2617    query_cursor: QueryCursorHandle,
2618}
2619
2620impl Clone for Snapshot {
2621    fn clone(&self) -> Self {
2622        Self {
2623            visible_text: self.visible_text.clone(),
2624            fragments: self.fragments.clone(),
2625            version: self.version.clone(),
2626            tree: self.tree.clone(),
2627            is_parsing: self.is_parsing,
2628            language: self.language.clone(),
2629            query_cursor: QueryCursorHandle::new(),
2630        }
2631    }
2632}
2633
2634impl Snapshot {
2635    pub fn len(&self) -> usize {
2636        self.visible_text.len()
2637    }
2638
2639    pub fn line_len(&self, row: u32) -> u32 {
2640        self.content().line_len(row)
2641    }
2642
2643    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2644        self.content().indent_column_for_line(row)
2645    }
2646
2647    fn suggest_autoindents<'a>(
2648        &'a self,
2649        row_range: Range<u32>,
2650    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
2651        let mut query_cursor = QueryCursorHandle::new();
2652        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
2653            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2654
2655            // Get the "indentation ranges" that intersect this row range.
2656            let indent_capture_ix = language.indents_query.capture_index_for_name("indent");
2657            let end_capture_ix = language.indents_query.capture_index_for_name("end");
2658            query_cursor.set_point_range(
2659                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).into()
2660                    ..Point::new(row_range.end, 0).into(),
2661            );
2662            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
2663            for mat in query_cursor.matches(
2664                &language.indents_query,
2665                tree.root_node(),
2666                TextProvider(&self.visible_text),
2667            ) {
2668                let mut node_kind = "";
2669                let mut start: Option<Point> = None;
2670                let mut end: Option<Point> = None;
2671                for capture in mat.captures {
2672                    if Some(capture.index) == indent_capture_ix {
2673                        node_kind = capture.node.kind();
2674                        start.get_or_insert(capture.node.start_position().into());
2675                        end.get_or_insert(capture.node.end_position().into());
2676                    } else if Some(capture.index) == end_capture_ix {
2677                        end = Some(capture.node.start_position().into());
2678                    }
2679                }
2680
2681                if let Some((start, end)) = start.zip(end) {
2682                    if start.row == end.row {
2683                        continue;
2684                    }
2685
2686                    let range = start..end;
2687                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
2688                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
2689                        Ok(ix) => {
2690                            let prev_range = &mut indentation_ranges[ix];
2691                            prev_range.0.end = prev_range.0.end.max(range.end);
2692                        }
2693                    }
2694                }
2695            }
2696
2697            let mut prev_row = prev_non_blank_row.unwrap_or(0);
2698            Some(row_range.map(move |row| {
2699                let row_start = Point::new(row, self.indent_column_for_line(row));
2700
2701                let mut indent_from_prev_row = false;
2702                let mut outdent_to_row = u32::MAX;
2703                for (range, _node_kind) in &indentation_ranges {
2704                    if range.start.row >= row {
2705                        break;
2706                    }
2707
2708                    if range.start.row == prev_row && range.end > row_start {
2709                        indent_from_prev_row = true;
2710                    }
2711                    if range.end.row >= prev_row && range.end <= row_start {
2712                        outdent_to_row = outdent_to_row.min(range.start.row);
2713                    }
2714                }
2715
2716                let suggestion = if outdent_to_row == prev_row {
2717                    IndentSuggestion {
2718                        basis_row: prev_row,
2719                        indent: false,
2720                    }
2721                } else if indent_from_prev_row {
2722                    IndentSuggestion {
2723                        basis_row: prev_row,
2724                        indent: true,
2725                    }
2726                } else if outdent_to_row < prev_row {
2727                    IndentSuggestion {
2728                        basis_row: outdent_to_row,
2729                        indent: false,
2730                    }
2731                } else {
2732                    IndentSuggestion {
2733                        basis_row: prev_row,
2734                        indent: false,
2735                    }
2736                };
2737
2738                prev_row = row;
2739                suggestion
2740            }))
2741        } else {
2742            None
2743        }
2744    }
2745
2746    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2747        while row > 0 {
2748            row -= 1;
2749            if !self.is_line_blank(row) {
2750                return Some(row);
2751            }
2752        }
2753        None
2754    }
2755
2756    fn is_line_blank(&self, row: u32) -> bool {
2757        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
2758            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
2759    }
2760
2761    pub fn text(&self) -> Rope {
2762        self.visible_text.clone()
2763    }
2764
2765    pub fn text_summary(&self) -> TextSummary {
2766        self.visible_text.summary()
2767    }
2768
2769    pub fn max_point(&self) -> Point {
2770        self.visible_text.max_point()
2771    }
2772
2773    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks {
2774        let range = range.start.to_offset(self)..range.end.to_offset(self);
2775        self.visible_text.chunks_in_range(range)
2776    }
2777
2778    pub fn highlighted_text_for_range<T: ToOffset>(
2779        &mut self,
2780        range: Range<T>,
2781    ) -> HighlightedChunks {
2782        let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
2783        let chunks = self.visible_text.chunks_in_range(range.clone());
2784        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
2785            let captures = self.query_cursor.set_byte_range(range.clone()).captures(
2786                &language.highlights_query,
2787                tree.root_node(),
2788                TextProvider(&self.visible_text),
2789            );
2790
2791            HighlightedChunks {
2792                range,
2793                chunks,
2794                highlights: Some(Highlights {
2795                    captures,
2796                    next_capture: None,
2797                    stack: Default::default(),
2798                    highlight_map: language.highlight_map(),
2799                }),
2800            }
2801        } else {
2802            HighlightedChunks {
2803                range,
2804                chunks,
2805                highlights: None,
2806            }
2807        }
2808    }
2809
2810    pub fn text_summary_for_range<T>(&self, range: Range<T>) -> TextSummary
2811    where
2812        T: ToOffset,
2813    {
2814        let range = range.start.to_offset(self.content())..range.end.to_offset(self.content());
2815        self.content().text_summary_for_range(range)
2816    }
2817
2818    pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
2819        self.content().point_for_offset(offset)
2820    }
2821
2822    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
2823        self.visible_text.clip_offset(offset, bias)
2824    }
2825
2826    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
2827        self.visible_text.clip_point(point, bias)
2828    }
2829
2830    pub fn to_offset(&self, point: Point) -> usize {
2831        self.visible_text.to_offset(point)
2832    }
2833
2834    pub fn to_point(&self, offset: usize) -> Point {
2835        self.visible_text.to_point(offset)
2836    }
2837
2838    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2839        self.content().anchor_at(position, Bias::Left)
2840    }
2841
2842    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2843        self.content().anchor_at(position, Bias::Right)
2844    }
2845
2846    fn content(&self) -> Content {
2847        self.into()
2848    }
2849}
2850
2851pub struct Content<'a> {
2852    visible_text: &'a Rope,
2853    fragments: &'a SumTree<Fragment>,
2854    version: &'a clock::Global,
2855}
2856
2857impl<'a> From<&'a Snapshot> for Content<'a> {
2858    fn from(snapshot: &'a Snapshot) -> Self {
2859        Self {
2860            visible_text: &snapshot.visible_text,
2861            fragments: &snapshot.fragments,
2862            version: &snapshot.version,
2863        }
2864    }
2865}
2866
2867impl<'a> From<&'a Buffer> for Content<'a> {
2868    fn from(buffer: &'a Buffer) -> Self {
2869        Self {
2870            visible_text: &buffer.visible_text,
2871            fragments: &buffer.fragments,
2872            version: &buffer.version,
2873        }
2874    }
2875}
2876
2877impl<'a> From<&'a mut Buffer> for Content<'a> {
2878    fn from(buffer: &'a mut Buffer) -> Self {
2879        Self {
2880            visible_text: &buffer.visible_text,
2881            fragments: &buffer.fragments,
2882            version: &buffer.version,
2883        }
2884    }
2885}
2886
2887impl<'a> From<&'a TextBuffer> for Content<'a> {
2888    fn from(buffer: &'a TextBuffer) -> Self {
2889        Self {
2890            visible_text: &buffer.visible_text,
2891            fragments: &buffer.fragments,
2892            version: &buffer.version,
2893        }
2894    }
2895}
2896
2897impl<'a> From<&'a mut TextBuffer> for Content<'a> {
2898    fn from(buffer: &'a mut TextBuffer) -> Self {
2899        Self {
2900            visible_text: &buffer.visible_text,
2901            fragments: &buffer.fragments,
2902            version: &buffer.version,
2903        }
2904    }
2905}
2906
2907impl<'a> From<&'a Content<'a>> for Content<'a> {
2908    fn from(content: &'a Content) -> Self {
2909        Self {
2910            visible_text: &content.visible_text,
2911            fragments: &content.fragments,
2912            version: &content.version,
2913        }
2914    }
2915}
2916
2917impl<'a> Content<'a> {
2918    fn max_point(&self) -> Point {
2919        self.visible_text.max_point()
2920    }
2921
2922    fn len(&self) -> usize {
2923        self.fragments.extent::<usize>(&None)
2924    }
2925
2926    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + 'a {
2927        let offset = position.to_offset(self);
2928        self.visible_text.chars_at(offset)
2929    }
2930
2931    pub fn reversed_chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + 'a {
2932        let offset = position.to_offset(self);
2933        self.visible_text.reversed_chars_at(offset)
2934    }
2935
2936    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks<'a> {
2937        let start = range.start.to_offset(self);
2938        let end = range.end.to_offset(self);
2939        self.visible_text.chunks_in_range(start..end)
2940    }
2941
2942    fn line_len(&self, row: u32) -> u32 {
2943        let row_start_offset = Point::new(row, 0).to_offset(self);
2944        let row_end_offset = if row >= self.max_point().row {
2945            self.len()
2946        } else {
2947            Point::new(row + 1, 0).to_offset(self) - 1
2948        };
2949        (row_end_offset - row_start_offset) as u32
2950    }
2951
2952    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2953        let mut result = 0;
2954        for c in self.chars_at(Point::new(row, 0)) {
2955            if c == ' ' {
2956                result += 1;
2957            } else {
2958                break;
2959            }
2960        }
2961        result
2962    }
2963
2964    fn summary_for_anchor(&self, anchor: &Anchor) -> TextSummary {
2965        let cx = Some(anchor.version.clone());
2966        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2967        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
2968        let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2969            anchor.offset - cursor.start().0.offset()
2970        } else {
2971            0
2972        };
2973        self.text_summary_for_range(0..cursor.start().1 + overshoot)
2974    }
2975
2976    fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
2977        self.visible_text.cursor(range.start).summary(range.end)
2978    }
2979
2980    fn summaries_for_anchors<T>(
2981        &self,
2982        map: &'a AnchorMap<T>,
2983    ) -> impl Iterator<Item = (TextSummary, &'a T)> {
2984        let cx = Some(map.version.clone());
2985        let mut summary = TextSummary::default();
2986        let mut rope_cursor = self.visible_text.cursor(0);
2987        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2988        map.entries.iter().map(move |((offset, bias), value)| {
2989            cursor.seek_forward(&VersionedOffset::Offset(*offset), *bias, &cx);
2990            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2991                offset - cursor.start().0.offset()
2992            } else {
2993                0
2994            };
2995            summary += rope_cursor.summary(cursor.start().1 + overshoot);
2996            (summary.clone(), value)
2997        })
2998    }
2999
3000    fn summaries_for_anchor_ranges<T>(
3001        &self,
3002        map: &'a AnchorRangeMap<T>,
3003    ) -> impl Iterator<Item = (Range<TextSummary>, &'a T)> {
3004        let cx = Some(map.version.clone());
3005        let mut summary = TextSummary::default();
3006        let mut rope_cursor = self.visible_text.cursor(0);
3007        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
3008        map.entries.iter().map(move |(range, value)| {
3009            let Range {
3010                start: (start_offset, start_bias),
3011                end: (end_offset, end_bias),
3012            } = range;
3013
3014            cursor.seek_forward(&VersionedOffset::Offset(*start_offset), *start_bias, &cx);
3015            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
3016                start_offset - cursor.start().0.offset()
3017            } else {
3018                0
3019            };
3020            summary += rope_cursor.summary(cursor.start().1 + overshoot);
3021            let start_summary = summary.clone();
3022
3023            cursor.seek_forward(&VersionedOffset::Offset(*end_offset), *end_bias, &cx);
3024            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
3025                end_offset - cursor.start().0.offset()
3026            } else {
3027                0
3028            };
3029            summary += rope_cursor.summary(cursor.start().1 + overshoot);
3030            let end_summary = summary.clone();
3031
3032            (start_summary..end_summary, value)
3033        })
3034    }
3035
3036    fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
3037        let offset = position.to_offset(self);
3038        let max_offset = self.len();
3039        assert!(offset <= max_offset, "offset is out of range");
3040        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3041        cursor.seek(&offset, bias, &None);
3042        Anchor {
3043            offset: offset + cursor.start().deleted,
3044            bias,
3045            version: self.version.clone(),
3046        }
3047    }
3048
3049    pub fn anchor_map<T, E>(&self, entries: E) -> AnchorMap<T>
3050    where
3051        E: IntoIterator<Item = ((usize, Bias), T)>,
3052    {
3053        let version = self.version.clone();
3054        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3055        let entries = entries
3056            .into_iter()
3057            .map(|((offset, bias), value)| {
3058                cursor.seek_forward(&offset, bias, &None);
3059                let full_offset = cursor.start().deleted + offset;
3060                ((full_offset, bias), value)
3061            })
3062            .collect();
3063
3064        AnchorMap { version, entries }
3065    }
3066
3067    pub fn anchor_range_map<T, E>(&self, entries: E) -> AnchorRangeMap<T>
3068    where
3069        E: IntoIterator<Item = (Range<(usize, Bias)>, T)>,
3070    {
3071        let version = self.version.clone();
3072        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3073        let entries = entries
3074            .into_iter()
3075            .map(|(range, value)| {
3076                let Range {
3077                    start: (start_offset, start_bias),
3078                    end: (end_offset, end_bias),
3079                } = range;
3080                cursor.seek_forward(&start_offset, start_bias, &None);
3081                let full_start_offset = cursor.start().deleted + start_offset;
3082                cursor.seek_forward(&end_offset, end_bias, &None);
3083                let full_end_offset = cursor.start().deleted + end_offset;
3084                (
3085                    (full_start_offset, start_bias)..(full_end_offset, end_bias),
3086                    value,
3087                )
3088            })
3089            .collect();
3090
3091        AnchorRangeMap { version, entries }
3092    }
3093
3094    pub fn anchor_set<E>(&self, entries: E) -> AnchorSet
3095    where
3096        E: IntoIterator<Item = (usize, Bias)>,
3097    {
3098        AnchorSet(self.anchor_map(entries.into_iter().map(|range| (range, ()))))
3099    }
3100
3101    pub fn anchor_range_set<E>(&self, entries: E) -> AnchorRangeSet
3102    where
3103        E: IntoIterator<Item = Range<(usize, Bias)>>,
3104    {
3105        AnchorRangeSet(self.anchor_range_map(entries.into_iter().map(|range| (range, ()))))
3106    }
3107
3108    fn full_offset_for_anchor(&self, anchor: &Anchor) -> usize {
3109        let cx = Some(anchor.version.clone());
3110        let mut cursor = self
3111            .fragments
3112            .cursor::<(VersionedOffset, FragmentTextSummary)>();
3113        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
3114        let overshoot = if cursor.item().is_some() {
3115            anchor.offset - cursor.start().0.offset()
3116        } else {
3117            0
3118        };
3119        let summary = cursor.start().1;
3120        summary.visible + summary.deleted + overshoot
3121    }
3122
3123    fn point_for_offset(&self, offset: usize) -> Result<Point> {
3124        if offset <= self.len() {
3125            Ok(self.text_summary_for_range(0..offset).lines)
3126        } else {
3127            Err(anyhow!("offset out of bounds"))
3128        }
3129    }
3130}
3131
3132#[derive(Debug)]
3133struct IndentSuggestion {
3134    basis_row: u32,
3135    indent: bool,
3136}
3137
3138struct RopeBuilder<'a> {
3139    old_visible_cursor: rope::Cursor<'a>,
3140    old_deleted_cursor: rope::Cursor<'a>,
3141    new_visible: Rope,
3142    new_deleted: Rope,
3143}
3144
3145impl<'a> RopeBuilder<'a> {
3146    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
3147        Self {
3148            old_visible_cursor,
3149            old_deleted_cursor,
3150            new_visible: Rope::new(),
3151            new_deleted: Rope::new(),
3152        }
3153    }
3154
3155    fn push_tree(&mut self, len: FragmentTextSummary) {
3156        self.push(len.visible, true, true);
3157        self.push(len.deleted, false, false);
3158    }
3159
3160    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
3161        debug_assert!(fragment.len > 0);
3162        self.push(fragment.len, was_visible, fragment.visible)
3163    }
3164
3165    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
3166        let text = if was_visible {
3167            self.old_visible_cursor
3168                .slice(self.old_visible_cursor.offset() + len)
3169        } else {
3170            self.old_deleted_cursor
3171                .slice(self.old_deleted_cursor.offset() + len)
3172        };
3173        if is_visible {
3174            self.new_visible.append(text);
3175        } else {
3176            self.new_deleted.append(text);
3177        }
3178    }
3179
3180    fn push_str(&mut self, text: &str) {
3181        self.new_visible.push(text);
3182    }
3183
3184    fn finish(mut self) -> (Rope, Rope) {
3185        self.new_visible.append(self.old_visible_cursor.suffix());
3186        self.new_deleted.append(self.old_deleted_cursor.suffix());
3187        (self.new_visible, self.new_deleted)
3188    }
3189}
3190
3191#[derive(Clone, Debug, Eq, PartialEq)]
3192pub enum Event {
3193    Edited,
3194    Dirtied,
3195    Saved,
3196    FileHandleChanged,
3197    Reloaded,
3198    Reparsed,
3199    Closed,
3200}
3201
3202impl Entity for Buffer {
3203    type Event = Event;
3204
3205    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
3206        if let Some(file) = self.file.as_ref() {
3207            file.buffer_removed(self.remote_id, cx);
3208        }
3209    }
3210}
3211
3212impl<'a, F: Fn(&FragmentSummary) -> bool> Iterator for Edits<'a, F> {
3213    type Item = Edit;
3214
3215    fn next(&mut self) -> Option<Self::Item> {
3216        let mut change: Option<Edit> = None;
3217        let cursor = self.cursor.as_mut()?;
3218
3219        while let Some(fragment) = cursor.item() {
3220            let bytes = cursor.start().visible - self.new_offset;
3221            let lines = self.visible_text.to_point(cursor.start().visible) - self.new_point;
3222            self.old_offset += bytes;
3223            self.old_point += &lines;
3224            self.new_offset += bytes;
3225            self.new_point += &lines;
3226
3227            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
3228                let fragment_lines =
3229                    self.visible_text.to_point(self.new_offset + fragment.len) - self.new_point;
3230                if let Some(ref mut change) = change {
3231                    if change.new_bytes.end == self.new_offset {
3232                        change.new_bytes.end += fragment.len;
3233                    } else {
3234                        break;
3235                    }
3236                } else {
3237                    change = Some(Edit {
3238                        old_bytes: self.old_offset..self.old_offset,
3239                        new_bytes: self.new_offset..self.new_offset + fragment.len,
3240                        old_lines: self.old_point..self.old_point,
3241                    });
3242                }
3243
3244                self.new_offset += fragment.len;
3245                self.new_point += &fragment_lines;
3246            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
3247                let deleted_start = cursor.start().deleted;
3248                let fragment_lines = self.deleted_text.to_point(deleted_start + fragment.len)
3249                    - self.deleted_text.to_point(deleted_start);
3250                if let Some(ref mut change) = change {
3251                    if change.new_bytes.end == self.new_offset {
3252                        change.old_bytes.end += fragment.len;
3253                        change.old_lines.end += &fragment_lines;
3254                    } else {
3255                        break;
3256                    }
3257                } else {
3258                    change = Some(Edit {
3259                        old_bytes: self.old_offset..self.old_offset + fragment.len,
3260                        new_bytes: self.new_offset..self.new_offset,
3261                        old_lines: self.old_point..self.old_point + &fragment_lines,
3262                    });
3263                }
3264
3265                self.old_offset += fragment.len;
3266                self.old_point += &fragment_lines;
3267            }
3268
3269            cursor.next(&None);
3270        }
3271
3272        change
3273    }
3274}
3275
3276struct ByteChunks<'a>(rope::Chunks<'a>);
3277
3278impl<'a> Iterator for ByteChunks<'a> {
3279    type Item = &'a [u8];
3280
3281    fn next(&mut self) -> Option<Self::Item> {
3282        self.0.next().map(str::as_bytes)
3283    }
3284}
3285
3286struct TextProvider<'a>(&'a Rope);
3287
3288impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
3289    type I = ByteChunks<'a>;
3290
3291    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
3292        ByteChunks(self.0.chunks_in_range(node.byte_range()))
3293    }
3294}
3295
3296struct Highlights<'a> {
3297    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
3298    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
3299    stack: Vec<(usize, HighlightId)>,
3300    highlight_map: HighlightMap,
3301}
3302
3303pub struct HighlightedChunks<'a> {
3304    range: Range<usize>,
3305    chunks: Chunks<'a>,
3306    highlights: Option<Highlights<'a>>,
3307}
3308
3309impl<'a> HighlightedChunks<'a> {
3310    pub fn seek(&mut self, offset: usize) {
3311        self.range.start = offset;
3312        self.chunks.seek(self.range.start);
3313        if let Some(highlights) = self.highlights.as_mut() {
3314            highlights
3315                .stack
3316                .retain(|(end_offset, _)| *end_offset > offset);
3317            if let Some((mat, capture_ix)) = &highlights.next_capture {
3318                let capture = mat.captures[*capture_ix as usize];
3319                if offset >= capture.node.start_byte() {
3320                    let next_capture_end = capture.node.end_byte();
3321                    if offset < next_capture_end {
3322                        highlights.stack.push((
3323                            next_capture_end,
3324                            highlights.highlight_map.get(capture.index),
3325                        ));
3326                    }
3327                    highlights.next_capture.take();
3328                }
3329            }
3330            highlights.captures.set_byte_range(self.range.clone());
3331        }
3332    }
3333
3334    pub fn offset(&self) -> usize {
3335        self.range.start
3336    }
3337}
3338
3339impl<'a> Iterator for HighlightedChunks<'a> {
3340    type Item = (&'a str, HighlightId);
3341
3342    fn next(&mut self) -> Option<Self::Item> {
3343        let mut next_capture_start = usize::MAX;
3344
3345        if let Some(highlights) = self.highlights.as_mut() {
3346            while let Some((parent_capture_end, _)) = highlights.stack.last() {
3347                if *parent_capture_end <= self.range.start {
3348                    highlights.stack.pop();
3349                } else {
3350                    break;
3351                }
3352            }
3353
3354            if highlights.next_capture.is_none() {
3355                highlights.next_capture = highlights.captures.next();
3356            }
3357
3358            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
3359                let capture = mat.captures[*capture_ix as usize];
3360                if self.range.start < capture.node.start_byte() {
3361                    next_capture_start = capture.node.start_byte();
3362                    break;
3363                } else {
3364                    let style_id = highlights.highlight_map.get(capture.index);
3365                    highlights.stack.push((capture.node.end_byte(), style_id));
3366                    highlights.next_capture = highlights.captures.next();
3367                }
3368            }
3369        }
3370
3371        if let Some(chunk) = self.chunks.peek() {
3372            let chunk_start = self.range.start;
3373            let mut chunk_end = (self.chunks.offset() + chunk.len()).min(next_capture_start);
3374            let mut style_id = HighlightId::default();
3375            if let Some((parent_capture_end, parent_style_id)) =
3376                self.highlights.as_ref().and_then(|h| h.stack.last())
3377            {
3378                chunk_end = chunk_end.min(*parent_capture_end);
3379                style_id = *parent_style_id;
3380            }
3381
3382            let slice =
3383                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
3384            self.range.start = chunk_end;
3385            if self.range.start == self.chunks.offset() + chunk.len() {
3386                self.chunks.next().unwrap();
3387            }
3388
3389            Some((slice, style_id))
3390        } else {
3391            None
3392        }
3393    }
3394}
3395
3396impl Fragment {
3397    fn is_visible(&self, undos: &UndoMap) -> bool {
3398        !undos.is_undone(self.timestamp.local())
3399            && self.deletions.iter().all(|d| undos.is_undone(*d))
3400    }
3401
3402    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
3403        (version.observed(self.timestamp.local())
3404            && !undos.was_undone(self.timestamp.local(), version))
3405            && self
3406                .deletions
3407                .iter()
3408                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
3409    }
3410}
3411
3412impl sum_tree::Item for Fragment {
3413    type Summary = FragmentSummary;
3414
3415    fn summary(&self) -> Self::Summary {
3416        let mut max_version = clock::Global::new();
3417        max_version.observe(self.timestamp.local());
3418        for deletion in &self.deletions {
3419            max_version.observe(*deletion);
3420        }
3421        max_version.join(&self.max_undos);
3422
3423        let mut min_insertion_version = clock::Global::new();
3424        min_insertion_version.observe(self.timestamp.local());
3425        let max_insertion_version = min_insertion_version.clone();
3426        if self.visible {
3427            FragmentSummary {
3428                text: FragmentTextSummary {
3429                    visible: self.len,
3430                    deleted: 0,
3431                },
3432                max_version,
3433                min_insertion_version,
3434                max_insertion_version,
3435            }
3436        } else {
3437            FragmentSummary {
3438                text: FragmentTextSummary {
3439                    visible: 0,
3440                    deleted: self.len,
3441                },
3442                max_version,
3443                min_insertion_version,
3444                max_insertion_version,
3445            }
3446        }
3447    }
3448}
3449
3450impl sum_tree::Summary for FragmentSummary {
3451    type Context = Option<clock::Global>;
3452
3453    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
3454        self.text.visible += &other.text.visible;
3455        self.text.deleted += &other.text.deleted;
3456        self.max_version.join(&other.max_version);
3457        self.min_insertion_version
3458            .meet(&other.min_insertion_version);
3459        self.max_insertion_version
3460            .join(&other.max_insertion_version);
3461    }
3462}
3463
3464impl Default for FragmentSummary {
3465    fn default() -> Self {
3466        FragmentSummary {
3467            text: FragmentTextSummary::default(),
3468            max_version: clock::Global::new(),
3469            min_insertion_version: clock::Global::new(),
3470            max_insertion_version: clock::Global::new(),
3471        }
3472    }
3473}
3474
3475impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
3476    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
3477        *self += summary.text.visible;
3478    }
3479}
3480
3481impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
3482    fn cmp(
3483        &self,
3484        cursor_location: &FragmentTextSummary,
3485        _: &Option<clock::Global>,
3486    ) -> cmp::Ordering {
3487        Ord::cmp(self, &cursor_location.visible)
3488    }
3489}
3490
3491#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3492enum VersionedOffset {
3493    Offset(usize),
3494    InvalidVersion,
3495}
3496
3497impl VersionedOffset {
3498    fn offset(&self) -> usize {
3499        if let Self::Offset(offset) = self {
3500            *offset
3501        } else {
3502            panic!("invalid version")
3503        }
3504    }
3505}
3506
3507impl Default for VersionedOffset {
3508    fn default() -> Self {
3509        Self::Offset(0)
3510    }
3511}
3512
3513impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedOffset {
3514    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
3515        if let Self::Offset(offset) = self {
3516            let version = cx.as_ref().unwrap();
3517            if *version >= summary.max_insertion_version {
3518                *offset += summary.text.visible + summary.text.deleted;
3519            } else if !summary
3520                .min_insertion_version
3521                .iter()
3522                .all(|t| !version.observed(*t))
3523            {
3524                *self = Self::InvalidVersion;
3525            }
3526        }
3527    }
3528}
3529
3530impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedOffset {
3531    fn cmp(&self, other: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
3532        match (self, other) {
3533            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
3534            (Self::Offset(_), Self::InvalidVersion) => cmp::Ordering::Less,
3535            (Self::InvalidVersion, _) => unreachable!(),
3536        }
3537    }
3538}
3539
3540impl Operation {
3541    fn replica_id(&self) -> ReplicaId {
3542        self.lamport_timestamp().replica_id
3543    }
3544
3545    fn lamport_timestamp(&self) -> clock::Lamport {
3546        match self {
3547            Operation::Edit(edit) => edit.timestamp.lamport(),
3548            Operation::Undo {
3549                lamport_timestamp, ..
3550            } => *lamport_timestamp,
3551            Operation::UpdateSelections {
3552                lamport_timestamp, ..
3553            } => *lamport_timestamp,
3554            Operation::SetActiveSelections {
3555                lamport_timestamp, ..
3556            } => *lamport_timestamp,
3557            #[cfg(test)]
3558            Operation::Test(lamport_timestamp) => *lamport_timestamp,
3559        }
3560    }
3561
3562    pub fn is_edit(&self) -> bool {
3563        match self {
3564            Operation::Edit { .. } => true,
3565            _ => false,
3566        }
3567    }
3568}
3569
3570impl<'a> Into<proto::Operation> for &'a Operation {
3571    fn into(self) -> proto::Operation {
3572        proto::Operation {
3573            variant: Some(match self {
3574                Operation::Edit(edit) => proto::operation::Variant::Edit(edit.into()),
3575                Operation::Undo {
3576                    undo,
3577                    lamport_timestamp,
3578                } => proto::operation::Variant::Undo(proto::operation::Undo {
3579                    replica_id: undo.id.replica_id as u32,
3580                    local_timestamp: undo.id.value,
3581                    lamport_timestamp: lamport_timestamp.value,
3582                    ranges: undo
3583                        .ranges
3584                        .iter()
3585                        .map(|r| proto::Range {
3586                            start: r.start as u64,
3587                            end: r.end as u64,
3588                        })
3589                        .collect(),
3590                    counts: undo
3591                        .counts
3592                        .iter()
3593                        .map(|(edit_id, count)| proto::operation::UndoCount {
3594                            replica_id: edit_id.replica_id as u32,
3595                            local_timestamp: edit_id.value,
3596                            count: *count,
3597                        })
3598                        .collect(),
3599                    version: From::from(&undo.version),
3600                }),
3601                Operation::UpdateSelections {
3602                    set_id,
3603                    selections,
3604                    lamport_timestamp,
3605                } => proto::operation::Variant::UpdateSelections(
3606                    proto::operation::UpdateSelections {
3607                        replica_id: set_id.replica_id as u32,
3608                        local_timestamp: set_id.value,
3609                        lamport_timestamp: lamport_timestamp.value,
3610                        set: selections.as_ref().map(|selections| proto::SelectionSet {
3611                            selections: selections.iter().map(Into::into).collect(),
3612                        }),
3613                    },
3614                ),
3615                Operation::SetActiveSelections {
3616                    set_id,
3617                    lamport_timestamp,
3618                } => proto::operation::Variant::SetActiveSelections(
3619                    proto::operation::SetActiveSelections {
3620                        replica_id: lamport_timestamp.replica_id as u32,
3621                        local_timestamp: set_id.map(|set_id| set_id.value),
3622                        lamport_timestamp: lamport_timestamp.value,
3623                    },
3624                ),
3625                #[cfg(test)]
3626                Operation::Test(_) => unimplemented!(),
3627            }),
3628        }
3629    }
3630}
3631
3632impl<'a> Into<proto::operation::Edit> for &'a EditOperation {
3633    fn into(self) -> proto::operation::Edit {
3634        let ranges = self
3635            .ranges
3636            .iter()
3637            .map(|range| proto::Range {
3638                start: range.start as u64,
3639                end: range.end as u64,
3640            })
3641            .collect();
3642        proto::operation::Edit {
3643            replica_id: self.timestamp.replica_id as u32,
3644            local_timestamp: self.timestamp.local,
3645            lamport_timestamp: self.timestamp.lamport,
3646            version: From::from(&self.version),
3647            ranges,
3648            new_text: self.new_text.clone(),
3649        }
3650    }
3651}
3652
3653impl<'a> Into<proto::Anchor> for &'a Anchor {
3654    fn into(self) -> proto::Anchor {
3655        proto::Anchor {
3656            version: (&self.version).into(),
3657            offset: self.offset as u64,
3658            bias: match self.bias {
3659                Bias::Left => proto::anchor::Bias::Left as i32,
3660                Bias::Right => proto::anchor::Bias::Right as i32,
3661            },
3662        }
3663    }
3664}
3665
3666impl<'a> Into<proto::Selection> for &'a Selection {
3667    fn into(self) -> proto::Selection {
3668        proto::Selection {
3669            id: self.id as u64,
3670            start: Some((&self.start).into()),
3671            end: Some((&self.end).into()),
3672            reversed: self.reversed,
3673        }
3674    }
3675}
3676
3677impl TryFrom<proto::Operation> for Operation {
3678    type Error = anyhow::Error;
3679
3680    fn try_from(message: proto::Operation) -> Result<Self, Self::Error> {
3681        Ok(
3682            match message
3683                .variant
3684                .ok_or_else(|| anyhow!("missing operation variant"))?
3685            {
3686                proto::operation::Variant::Edit(edit) => Operation::Edit(edit.into()),
3687                proto::operation::Variant::Undo(undo) => Operation::Undo {
3688                    lamport_timestamp: clock::Lamport {
3689                        replica_id: undo.replica_id as ReplicaId,
3690                        value: undo.lamport_timestamp,
3691                    },
3692                    undo: UndoOperation {
3693                        id: clock::Local {
3694                            replica_id: undo.replica_id as ReplicaId,
3695                            value: undo.local_timestamp,
3696                        },
3697                        counts: undo
3698                            .counts
3699                            .into_iter()
3700                            .map(|c| {
3701                                (
3702                                    clock::Local {
3703                                        replica_id: c.replica_id as ReplicaId,
3704                                        value: c.local_timestamp,
3705                                    },
3706                                    c.count,
3707                                )
3708                            })
3709                            .collect(),
3710                        ranges: undo
3711                            .ranges
3712                            .into_iter()
3713                            .map(|r| r.start as usize..r.end as usize)
3714                            .collect(),
3715                        version: undo.version.into(),
3716                    },
3717                },
3718                proto::operation::Variant::UpdateSelections(message) => {
3719                    let selections: Option<Vec<Selection>> = if let Some(set) = message.set {
3720                        Some(
3721                            set.selections
3722                                .into_iter()
3723                                .map(TryFrom::try_from)
3724                                .collect::<Result<_, _>>()?,
3725                        )
3726                    } else {
3727                        None
3728                    };
3729                    Operation::UpdateSelections {
3730                        set_id: clock::Lamport {
3731                            replica_id: message.replica_id as ReplicaId,
3732                            value: message.local_timestamp,
3733                        },
3734                        lamport_timestamp: clock::Lamport {
3735                            replica_id: message.replica_id as ReplicaId,
3736                            value: message.lamport_timestamp,
3737                        },
3738                        selections: selections.map(Arc::from),
3739                    }
3740                }
3741                proto::operation::Variant::SetActiveSelections(message) => {
3742                    Operation::SetActiveSelections {
3743                        set_id: message.local_timestamp.map(|value| clock::Lamport {
3744                            replica_id: message.replica_id as ReplicaId,
3745                            value,
3746                        }),
3747                        lamport_timestamp: clock::Lamport {
3748                            replica_id: message.replica_id as ReplicaId,
3749                            value: message.lamport_timestamp,
3750                        },
3751                    }
3752                }
3753            },
3754        )
3755    }
3756}
3757
3758impl From<proto::operation::Edit> for EditOperation {
3759    fn from(edit: proto::operation::Edit) -> Self {
3760        let ranges = edit
3761            .ranges
3762            .into_iter()
3763            .map(|range| range.start as usize..range.end as usize)
3764            .collect();
3765        EditOperation {
3766            timestamp: InsertionTimestamp {
3767                replica_id: edit.replica_id as ReplicaId,
3768                local: edit.local_timestamp,
3769                lamport: edit.lamport_timestamp,
3770            },
3771            version: edit.version.into(),
3772            ranges,
3773            new_text: edit.new_text,
3774        }
3775    }
3776}
3777
3778impl TryFrom<proto::Anchor> for Anchor {
3779    type Error = anyhow::Error;
3780
3781    fn try_from(message: proto::Anchor) -> Result<Self, Self::Error> {
3782        let mut version = clock::Global::new();
3783        for entry in message.version {
3784            version.observe(clock::Local {
3785                replica_id: entry.replica_id as ReplicaId,
3786                value: entry.timestamp,
3787            });
3788        }
3789
3790        Ok(Self {
3791            offset: message.offset as usize,
3792            bias: if message.bias == proto::anchor::Bias::Left as i32 {
3793                Bias::Left
3794            } else if message.bias == proto::anchor::Bias::Right as i32 {
3795                Bias::Right
3796            } else {
3797                Err(anyhow!("invalid anchor bias {}", message.bias))?
3798            },
3799            version,
3800        })
3801    }
3802}
3803
3804impl TryFrom<proto::Selection> for Selection {
3805    type Error = anyhow::Error;
3806
3807    fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> {
3808        Ok(Selection {
3809            id: selection.id as usize,
3810            start: selection
3811                .start
3812                .ok_or_else(|| anyhow!("missing selection start"))?
3813                .try_into()?,
3814            end: selection
3815                .end
3816                .ok_or_else(|| anyhow!("missing selection end"))?
3817                .try_into()?,
3818            reversed: selection.reversed,
3819            goal: SelectionGoal::None,
3820        })
3821    }
3822}
3823
3824pub trait ToOffset {
3825    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize;
3826}
3827
3828impl ToOffset for Point {
3829    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3830        content.into().visible_text.to_offset(*self)
3831    }
3832}
3833
3834impl ToOffset for usize {
3835    fn to_offset<'a>(&self, _: impl Into<Content<'a>>) -> usize {
3836        *self
3837    }
3838}
3839
3840impl ToOffset for Anchor {
3841    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3842        content.into().summary_for_anchor(self).bytes
3843    }
3844}
3845
3846impl<'a> ToOffset for &'a Anchor {
3847    fn to_offset<'b>(&self, content: impl Into<Content<'b>>) -> usize {
3848        content.into().summary_for_anchor(self).bytes
3849    }
3850}
3851
3852pub trait ToPoint {
3853    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point;
3854}
3855
3856impl ToPoint for Anchor {
3857    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3858        content.into().summary_for_anchor(self).lines
3859    }
3860}
3861
3862impl ToPoint for usize {
3863    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3864        content.into().visible_text.to_point(*self)
3865    }
3866}
3867
3868fn contiguous_ranges(
3869    values: impl IntoIterator<Item = u32>,
3870    max_len: usize,
3871) -> impl Iterator<Item = Range<u32>> {
3872    let mut values = values.into_iter();
3873    let mut current_range: Option<Range<u32>> = None;
3874    std::iter::from_fn(move || loop {
3875        if let Some(value) = values.next() {
3876            if let Some(range) = &mut current_range {
3877                if value == range.end && range.len() < max_len {
3878                    range.end += 1;
3879                    continue;
3880                }
3881            }
3882
3883            let prev_range = current_range.clone();
3884            current_range = Some(value..(value + 1));
3885            if prev_range.is_some() {
3886                return prev_range;
3887            }
3888        } else {
3889            return current_range.take();
3890        }
3891    })
3892}