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>(&mut self, rng: &mut T, old_range_count: usize)
2418    where
2419        T: rand::Rng,
2420    {
2421        self.buffer.randomly_edit(rng, old_range_count);
2422    }
2423
2424    pub fn randomly_mutate<T>(&mut self, rng: &mut T)
2425    where
2426        T: rand::Rng,
2427    {
2428        self.buffer.randomly_mutate(rng);
2429    }
2430}
2431
2432#[cfg(any(test, feature = "test-support"))]
2433impl TextBuffer {
2434    fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
2435        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
2436        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
2437        start..end
2438    }
2439
2440    pub fn randomly_edit<T>(
2441        &mut self,
2442        rng: &mut T,
2443        old_range_count: usize,
2444    ) -> (Vec<Range<usize>>, String, Operation)
2445    where
2446        T: rand::Rng,
2447    {
2448        let mut old_ranges: Vec<Range<usize>> = Vec::new();
2449        for _ in 0..old_range_count {
2450            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
2451            if last_end > self.len() {
2452                break;
2453            }
2454            old_ranges.push(self.random_byte_range(last_end, rng));
2455        }
2456        let new_text_len = rng.gen_range(0..10);
2457        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
2458            .take(new_text_len)
2459            .collect();
2460        log::info!(
2461            "mutating buffer {} at {:?}: {:?}",
2462            self.replica_id,
2463            old_ranges,
2464            new_text
2465        );
2466        let op = self.edit(old_ranges.iter().cloned(), new_text.as_str());
2467        (old_ranges, new_text, Operation::Edit(op))
2468    }
2469
2470    pub fn randomly_mutate<T>(&mut self, rng: &mut T) -> Vec<Operation>
2471    where
2472        T: rand::Rng,
2473    {
2474        use rand::prelude::*;
2475
2476        let mut ops = vec![self.randomly_edit(rng, 5).2];
2477
2478        // Randomly add, remove or mutate selection sets.
2479        let replica_selection_sets = &self
2480            .selection_sets()
2481            .map(|(set_id, _)| *set_id)
2482            .filter(|set_id| self.replica_id == set_id.replica_id)
2483            .collect::<Vec<_>>();
2484        let set_id = replica_selection_sets.choose(rng);
2485        if set_id.is_some() && rng.gen_bool(1.0 / 6.0) {
2486            ops.push(self.remove_selection_set(*set_id.unwrap()).unwrap());
2487        } else {
2488            let mut ranges = Vec::new();
2489            for _ in 0..5 {
2490                ranges.push(self.random_byte_range(0, rng));
2491            }
2492            let new_selections = self.selections_from_ranges(ranges).unwrap();
2493
2494            let op = if set_id.is_none() || rng.gen_bool(1.0 / 5.0) {
2495                self.add_selection_set(new_selections)
2496            } else {
2497                self.update_selection_set(*set_id.unwrap(), new_selections)
2498                    .unwrap()
2499            };
2500            ops.push(op);
2501        }
2502
2503        ops
2504    }
2505
2506    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
2507        use rand::prelude::*;
2508
2509        let mut ops = Vec::new();
2510        for _ in 0..rng.gen_range(1..=5) {
2511            if let Some(transaction) = self.history.undo_stack.choose(rng).cloned() {
2512                log::info!(
2513                    "undoing buffer {} transaction {:?}",
2514                    self.replica_id,
2515                    transaction
2516                );
2517                ops.push(self.undo_or_redo(transaction).unwrap());
2518            }
2519        }
2520        ops
2521    }
2522
2523    fn selections_from_ranges<I>(&self, ranges: I) -> Result<Vec<Selection>>
2524    where
2525        I: IntoIterator<Item = Range<usize>>,
2526    {
2527        use std::sync::atomic::{self, AtomicUsize};
2528
2529        static NEXT_SELECTION_ID: AtomicUsize = AtomicUsize::new(0);
2530
2531        let mut ranges = ranges.into_iter().collect::<Vec<_>>();
2532        ranges.sort_unstable_by_key(|range| range.start);
2533
2534        let mut selections = Vec::with_capacity(ranges.len());
2535        for range in ranges {
2536            if range.start > range.end {
2537                selections.push(Selection {
2538                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2539                    start: self.anchor_before(range.end),
2540                    end: self.anchor_before(range.start),
2541                    reversed: true,
2542                    goal: SelectionGoal::None,
2543                });
2544            } else {
2545                selections.push(Selection {
2546                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2547                    start: self.anchor_after(range.start),
2548                    end: self.anchor_before(range.end),
2549                    reversed: false,
2550                    goal: SelectionGoal::None,
2551                });
2552            }
2553        }
2554        Ok(selections)
2555    }
2556
2557    pub fn selection_ranges<'a>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<usize>>> {
2558        Ok(self
2559            .selection_set(set_id)?
2560            .selections
2561            .iter()
2562            .map(move |selection| {
2563                let start = selection.start.to_offset(self);
2564                let end = selection.end.to_offset(self);
2565                if selection.reversed {
2566                    end..start
2567                } else {
2568                    start..end
2569                }
2570            })
2571            .collect())
2572    }
2573
2574    pub fn all_selection_ranges<'a>(
2575        &'a self,
2576    ) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)> {
2577        self.selections
2578            .keys()
2579            .map(move |set_id| (*set_id, self.selection_ranges(*set_id).unwrap()))
2580    }
2581}
2582
2583impl Clone for Buffer {
2584    fn clone(&self) -> Self {
2585        Self {
2586            buffer: self.buffer.clone(),
2587            saved_version: self.saved_version.clone(),
2588            saved_mtime: self.saved_mtime,
2589            file: self.file.as_ref().map(|f| f.boxed_clone()),
2590            language: self.language.clone(),
2591            syntax_tree: Mutex::new(self.syntax_tree.lock().clone()),
2592            parsing_in_background: false,
2593            sync_parse_timeout: self.sync_parse_timeout,
2594            parse_count: self.parse_count,
2595            autoindent_requests: Default::default(),
2596            pending_autoindent: Default::default(),
2597
2598            #[cfg(test)]
2599            operations: self.operations.clone(),
2600        }
2601    }
2602}
2603
2604pub struct Snapshot {
2605    visible_text: Rope,
2606    fragments: SumTree<Fragment>,
2607    version: clock::Global,
2608    tree: Option<Tree>,
2609    is_parsing: bool,
2610    language: Option<Arc<Language>>,
2611    query_cursor: QueryCursorHandle,
2612}
2613
2614impl Clone for Snapshot {
2615    fn clone(&self) -> Self {
2616        Self {
2617            visible_text: self.visible_text.clone(),
2618            fragments: self.fragments.clone(),
2619            version: self.version.clone(),
2620            tree: self.tree.clone(),
2621            is_parsing: self.is_parsing,
2622            language: self.language.clone(),
2623            query_cursor: QueryCursorHandle::new(),
2624        }
2625    }
2626}
2627
2628impl Snapshot {
2629    pub fn len(&self) -> usize {
2630        self.visible_text.len()
2631    }
2632
2633    pub fn line_len(&self, row: u32) -> u32 {
2634        self.content().line_len(row)
2635    }
2636
2637    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2638        self.content().indent_column_for_line(row)
2639    }
2640
2641    fn suggest_autoindents<'a>(
2642        &'a self,
2643        row_range: Range<u32>,
2644    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
2645        let mut query_cursor = QueryCursorHandle::new();
2646        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
2647            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2648
2649            // Get the "indentation ranges" that intersect this row range.
2650            let indent_capture_ix = language.indents_query.capture_index_for_name("indent");
2651            let end_capture_ix = language.indents_query.capture_index_for_name("end");
2652            query_cursor.set_point_range(
2653                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).into()
2654                    ..Point::new(row_range.end, 0).into(),
2655            );
2656            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
2657            for mat in query_cursor.matches(
2658                &language.indents_query,
2659                tree.root_node(),
2660                TextProvider(&self.visible_text),
2661            ) {
2662                let mut node_kind = "";
2663                let mut start: Option<Point> = None;
2664                let mut end: Option<Point> = None;
2665                for capture in mat.captures {
2666                    if Some(capture.index) == indent_capture_ix {
2667                        node_kind = capture.node.kind();
2668                        start.get_or_insert(capture.node.start_position().into());
2669                        end.get_or_insert(capture.node.end_position().into());
2670                    } else if Some(capture.index) == end_capture_ix {
2671                        end = Some(capture.node.start_position().into());
2672                    }
2673                }
2674
2675                if let Some((start, end)) = start.zip(end) {
2676                    if start.row == end.row {
2677                        continue;
2678                    }
2679
2680                    let range = start..end;
2681                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
2682                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
2683                        Ok(ix) => {
2684                            let prev_range = &mut indentation_ranges[ix];
2685                            prev_range.0.end = prev_range.0.end.max(range.end);
2686                        }
2687                    }
2688                }
2689            }
2690
2691            let mut prev_row = prev_non_blank_row.unwrap_or(0);
2692            Some(row_range.map(move |row| {
2693                let row_start = Point::new(row, self.indent_column_for_line(row));
2694
2695                let mut indent_from_prev_row = false;
2696                let mut outdent_to_row = u32::MAX;
2697                for (range, _node_kind) in &indentation_ranges {
2698                    if range.start.row >= row {
2699                        break;
2700                    }
2701
2702                    if range.start.row == prev_row && range.end > row_start {
2703                        indent_from_prev_row = true;
2704                    }
2705                    if range.end.row >= prev_row && range.end <= row_start {
2706                        outdent_to_row = outdent_to_row.min(range.start.row);
2707                    }
2708                }
2709
2710                let suggestion = if outdent_to_row == prev_row {
2711                    IndentSuggestion {
2712                        basis_row: prev_row,
2713                        indent: false,
2714                    }
2715                } else if indent_from_prev_row {
2716                    IndentSuggestion {
2717                        basis_row: prev_row,
2718                        indent: true,
2719                    }
2720                } else if outdent_to_row < prev_row {
2721                    IndentSuggestion {
2722                        basis_row: outdent_to_row,
2723                        indent: false,
2724                    }
2725                } else {
2726                    IndentSuggestion {
2727                        basis_row: prev_row,
2728                        indent: false,
2729                    }
2730                };
2731
2732                prev_row = row;
2733                suggestion
2734            }))
2735        } else {
2736            None
2737        }
2738    }
2739
2740    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2741        while row > 0 {
2742            row -= 1;
2743            if !self.is_line_blank(row) {
2744                return Some(row);
2745            }
2746        }
2747        None
2748    }
2749
2750    fn is_line_blank(&self, row: u32) -> bool {
2751        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
2752            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
2753    }
2754
2755    pub fn text(&self) -> Rope {
2756        self.visible_text.clone()
2757    }
2758
2759    pub fn text_summary(&self) -> TextSummary {
2760        self.visible_text.summary()
2761    }
2762
2763    pub fn max_point(&self) -> Point {
2764        self.visible_text.max_point()
2765    }
2766
2767    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks {
2768        let range = range.start.to_offset(self)..range.end.to_offset(self);
2769        self.visible_text.chunks_in_range(range)
2770    }
2771
2772    pub fn highlighted_text_for_range<T: ToOffset>(
2773        &mut self,
2774        range: Range<T>,
2775    ) -> HighlightedChunks {
2776        let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
2777        let chunks = self.visible_text.chunks_in_range(range.clone());
2778        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
2779            let captures = self.query_cursor.set_byte_range(range.clone()).captures(
2780                &language.highlights_query,
2781                tree.root_node(),
2782                TextProvider(&self.visible_text),
2783            );
2784
2785            HighlightedChunks {
2786                range,
2787                chunks,
2788                highlights: Some(Highlights {
2789                    captures,
2790                    next_capture: None,
2791                    stack: Default::default(),
2792                    highlight_map: language.highlight_map(),
2793                }),
2794            }
2795        } else {
2796            HighlightedChunks {
2797                range,
2798                chunks,
2799                highlights: None,
2800            }
2801        }
2802    }
2803
2804    pub fn text_summary_for_range<T>(&self, range: Range<T>) -> TextSummary
2805    where
2806        T: ToOffset,
2807    {
2808        let range = range.start.to_offset(self.content())..range.end.to_offset(self.content());
2809        self.content().text_summary_for_range(range)
2810    }
2811
2812    pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
2813        self.content().point_for_offset(offset)
2814    }
2815
2816    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
2817        self.visible_text.clip_offset(offset, bias)
2818    }
2819
2820    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
2821        self.visible_text.clip_point(point, bias)
2822    }
2823
2824    pub fn to_offset(&self, point: Point) -> usize {
2825        self.visible_text.to_offset(point)
2826    }
2827
2828    pub fn to_point(&self, offset: usize) -> Point {
2829        self.visible_text.to_point(offset)
2830    }
2831
2832    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2833        self.content().anchor_at(position, Bias::Left)
2834    }
2835
2836    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2837        self.content().anchor_at(position, Bias::Right)
2838    }
2839
2840    fn content(&self) -> Content {
2841        self.into()
2842    }
2843}
2844
2845pub struct Content<'a> {
2846    visible_text: &'a Rope,
2847    fragments: &'a SumTree<Fragment>,
2848    version: &'a clock::Global,
2849}
2850
2851impl<'a> From<&'a Snapshot> for Content<'a> {
2852    fn from(snapshot: &'a Snapshot) -> Self {
2853        Self {
2854            visible_text: &snapshot.visible_text,
2855            fragments: &snapshot.fragments,
2856            version: &snapshot.version,
2857        }
2858    }
2859}
2860
2861impl<'a> From<&'a Buffer> for Content<'a> {
2862    fn from(buffer: &'a Buffer) -> Self {
2863        Self {
2864            visible_text: &buffer.visible_text,
2865            fragments: &buffer.fragments,
2866            version: &buffer.version,
2867        }
2868    }
2869}
2870
2871impl<'a> From<&'a mut Buffer> for Content<'a> {
2872    fn from(buffer: &'a mut Buffer) -> Self {
2873        Self {
2874            visible_text: &buffer.visible_text,
2875            fragments: &buffer.fragments,
2876            version: &buffer.version,
2877        }
2878    }
2879}
2880
2881impl<'a> From<&'a TextBuffer> for Content<'a> {
2882    fn from(buffer: &'a TextBuffer) -> Self {
2883        Self {
2884            visible_text: &buffer.visible_text,
2885            fragments: &buffer.fragments,
2886            version: &buffer.version,
2887        }
2888    }
2889}
2890
2891impl<'a> From<&'a mut TextBuffer> for Content<'a> {
2892    fn from(buffer: &'a mut TextBuffer) -> Self {
2893        Self {
2894            visible_text: &buffer.visible_text,
2895            fragments: &buffer.fragments,
2896            version: &buffer.version,
2897        }
2898    }
2899}
2900
2901impl<'a> From<&'a Content<'a>> for Content<'a> {
2902    fn from(content: &'a Content) -> Self {
2903        Self {
2904            visible_text: &content.visible_text,
2905            fragments: &content.fragments,
2906            version: &content.version,
2907        }
2908    }
2909}
2910
2911impl<'a> Content<'a> {
2912    fn max_point(&self) -> Point {
2913        self.visible_text.max_point()
2914    }
2915
2916    fn len(&self) -> usize {
2917        self.fragments.extent::<usize>(&None)
2918    }
2919
2920    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + 'a {
2921        let offset = position.to_offset(self);
2922        self.visible_text.chars_at(offset)
2923    }
2924
2925    pub fn reversed_chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + 'a {
2926        let offset = position.to_offset(self);
2927        self.visible_text.reversed_chars_at(offset)
2928    }
2929
2930    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks<'a> {
2931        let start = range.start.to_offset(self);
2932        let end = range.end.to_offset(self);
2933        self.visible_text.chunks_in_range(start..end)
2934    }
2935
2936    fn line_len(&self, row: u32) -> u32 {
2937        let row_start_offset = Point::new(row, 0).to_offset(self);
2938        let row_end_offset = if row >= self.max_point().row {
2939            self.len()
2940        } else {
2941            Point::new(row + 1, 0).to_offset(self) - 1
2942        };
2943        (row_end_offset - row_start_offset) as u32
2944    }
2945
2946    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2947        let mut result = 0;
2948        for c in self.chars_at(Point::new(row, 0)) {
2949            if c == ' ' {
2950                result += 1;
2951            } else {
2952                break;
2953            }
2954        }
2955        result
2956    }
2957
2958    fn summary_for_anchor(&self, anchor: &Anchor) -> TextSummary {
2959        let cx = Some(anchor.version.clone());
2960        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2961        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
2962        let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2963            anchor.offset - cursor.start().0.offset()
2964        } else {
2965            0
2966        };
2967        self.text_summary_for_range(0..cursor.start().1 + overshoot)
2968    }
2969
2970    fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
2971        self.visible_text.cursor(range.start).summary(range.end)
2972    }
2973
2974    fn summaries_for_anchors<T>(
2975        &self,
2976        map: &'a AnchorMap<T>,
2977    ) -> impl Iterator<Item = (TextSummary, &'a T)> {
2978        let cx = Some(map.version.clone());
2979        let mut summary = TextSummary::default();
2980        let mut rope_cursor = self.visible_text.cursor(0);
2981        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2982        map.entries.iter().map(move |((offset, bias), value)| {
2983            cursor.seek_forward(&VersionedOffset::Offset(*offset), *bias, &cx);
2984            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2985                offset - cursor.start().0.offset()
2986            } else {
2987                0
2988            };
2989            summary += rope_cursor.summary(cursor.start().1 + overshoot);
2990            (summary.clone(), value)
2991        })
2992    }
2993
2994    fn summaries_for_anchor_ranges<T>(
2995        &self,
2996        map: &'a AnchorRangeMap<T>,
2997    ) -> impl Iterator<Item = (Range<TextSummary>, &'a T)> {
2998        let cx = Some(map.version.clone());
2999        let mut summary = TextSummary::default();
3000        let mut rope_cursor = self.visible_text.cursor(0);
3001        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
3002        map.entries.iter().map(move |(range, value)| {
3003            let Range {
3004                start: (start_offset, start_bias),
3005                end: (end_offset, end_bias),
3006            } = range;
3007
3008            cursor.seek_forward(&VersionedOffset::Offset(*start_offset), *start_bias, &cx);
3009            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
3010                start_offset - cursor.start().0.offset()
3011            } else {
3012                0
3013            };
3014            summary += rope_cursor.summary(cursor.start().1 + overshoot);
3015            let start_summary = summary.clone();
3016
3017            cursor.seek_forward(&VersionedOffset::Offset(*end_offset), *end_bias, &cx);
3018            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
3019                end_offset - cursor.start().0.offset()
3020            } else {
3021                0
3022            };
3023            summary += rope_cursor.summary(cursor.start().1 + overshoot);
3024            let end_summary = summary.clone();
3025
3026            (start_summary..end_summary, value)
3027        })
3028    }
3029
3030    fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
3031        let offset = position.to_offset(self);
3032        let max_offset = self.len();
3033        assert!(offset <= max_offset, "offset is out of range");
3034        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3035        cursor.seek(&offset, bias, &None);
3036        Anchor {
3037            offset: offset + cursor.start().deleted,
3038            bias,
3039            version: self.version.clone(),
3040        }
3041    }
3042
3043    pub fn anchor_map<T, E>(&self, entries: E) -> AnchorMap<T>
3044    where
3045        E: IntoIterator<Item = ((usize, Bias), T)>,
3046    {
3047        let version = self.version.clone();
3048        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3049        let entries = entries
3050            .into_iter()
3051            .map(|((offset, bias), value)| {
3052                cursor.seek_forward(&offset, bias, &None);
3053                let full_offset = cursor.start().deleted + offset;
3054                ((full_offset, bias), value)
3055            })
3056            .collect();
3057
3058        AnchorMap { version, entries }
3059    }
3060
3061    pub fn anchor_range_map<T, E>(&self, entries: E) -> AnchorRangeMap<T>
3062    where
3063        E: IntoIterator<Item = (Range<(usize, Bias)>, T)>,
3064    {
3065        let version = self.version.clone();
3066        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
3067        let entries = entries
3068            .into_iter()
3069            .map(|(range, value)| {
3070                let Range {
3071                    start: (start_offset, start_bias),
3072                    end: (end_offset, end_bias),
3073                } = range;
3074                cursor.seek_forward(&start_offset, start_bias, &None);
3075                let full_start_offset = cursor.start().deleted + start_offset;
3076                cursor.seek_forward(&end_offset, end_bias, &None);
3077                let full_end_offset = cursor.start().deleted + end_offset;
3078                (
3079                    (full_start_offset, start_bias)..(full_end_offset, end_bias),
3080                    value,
3081                )
3082            })
3083            .collect();
3084
3085        AnchorRangeMap { version, entries }
3086    }
3087
3088    pub fn anchor_set<E>(&self, entries: E) -> AnchorSet
3089    where
3090        E: IntoIterator<Item = (usize, Bias)>,
3091    {
3092        AnchorSet(self.anchor_map(entries.into_iter().map(|range| (range, ()))))
3093    }
3094
3095    pub fn anchor_range_set<E>(&self, entries: E) -> AnchorRangeSet
3096    where
3097        E: IntoIterator<Item = Range<(usize, Bias)>>,
3098    {
3099        AnchorRangeSet(self.anchor_range_map(entries.into_iter().map(|range| (range, ()))))
3100    }
3101
3102    fn full_offset_for_anchor(&self, anchor: &Anchor) -> usize {
3103        let cx = Some(anchor.version.clone());
3104        let mut cursor = self
3105            .fragments
3106            .cursor::<(VersionedOffset, FragmentTextSummary)>();
3107        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
3108        let overshoot = if cursor.item().is_some() {
3109            anchor.offset - cursor.start().0.offset()
3110        } else {
3111            0
3112        };
3113        let summary = cursor.start().1;
3114        summary.visible + summary.deleted + overshoot
3115    }
3116
3117    fn point_for_offset(&self, offset: usize) -> Result<Point> {
3118        if offset <= self.len() {
3119            Ok(self.text_summary_for_range(0..offset).lines)
3120        } else {
3121            Err(anyhow!("offset out of bounds"))
3122        }
3123    }
3124}
3125
3126#[derive(Debug)]
3127struct IndentSuggestion {
3128    basis_row: u32,
3129    indent: bool,
3130}
3131
3132struct RopeBuilder<'a> {
3133    old_visible_cursor: rope::Cursor<'a>,
3134    old_deleted_cursor: rope::Cursor<'a>,
3135    new_visible: Rope,
3136    new_deleted: Rope,
3137}
3138
3139impl<'a> RopeBuilder<'a> {
3140    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
3141        Self {
3142            old_visible_cursor,
3143            old_deleted_cursor,
3144            new_visible: Rope::new(),
3145            new_deleted: Rope::new(),
3146        }
3147    }
3148
3149    fn push_tree(&mut self, len: FragmentTextSummary) {
3150        self.push(len.visible, true, true);
3151        self.push(len.deleted, false, false);
3152    }
3153
3154    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
3155        debug_assert!(fragment.len > 0);
3156        self.push(fragment.len, was_visible, fragment.visible)
3157    }
3158
3159    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
3160        let text = if was_visible {
3161            self.old_visible_cursor
3162                .slice(self.old_visible_cursor.offset() + len)
3163        } else {
3164            self.old_deleted_cursor
3165                .slice(self.old_deleted_cursor.offset() + len)
3166        };
3167        if is_visible {
3168            self.new_visible.append(text);
3169        } else {
3170            self.new_deleted.append(text);
3171        }
3172    }
3173
3174    fn push_str(&mut self, text: &str) {
3175        self.new_visible.push(text);
3176    }
3177
3178    fn finish(mut self) -> (Rope, Rope) {
3179        self.new_visible.append(self.old_visible_cursor.suffix());
3180        self.new_deleted.append(self.old_deleted_cursor.suffix());
3181        (self.new_visible, self.new_deleted)
3182    }
3183}
3184
3185#[derive(Clone, Debug, Eq, PartialEq)]
3186pub enum Event {
3187    Edited,
3188    Dirtied,
3189    Saved,
3190    FileHandleChanged,
3191    Reloaded,
3192    Reparsed,
3193    Closed,
3194}
3195
3196impl Entity for Buffer {
3197    type Event = Event;
3198
3199    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
3200        if let Some(file) = self.file.as_ref() {
3201            file.buffer_removed(self.remote_id, cx);
3202        }
3203    }
3204}
3205
3206impl<'a, F: Fn(&FragmentSummary) -> bool> Iterator for Edits<'a, F> {
3207    type Item = Edit;
3208
3209    fn next(&mut self) -> Option<Self::Item> {
3210        let mut change: Option<Edit> = None;
3211        let cursor = self.cursor.as_mut()?;
3212
3213        while let Some(fragment) = cursor.item() {
3214            let bytes = cursor.start().visible - self.new_offset;
3215            let lines = self.visible_text.to_point(cursor.start().visible) - self.new_point;
3216            self.old_offset += bytes;
3217            self.old_point += &lines;
3218            self.new_offset += bytes;
3219            self.new_point += &lines;
3220
3221            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
3222                let fragment_lines =
3223                    self.visible_text.to_point(self.new_offset + fragment.len) - self.new_point;
3224                if let Some(ref mut change) = change {
3225                    if change.new_bytes.end == self.new_offset {
3226                        change.new_bytes.end += fragment.len;
3227                    } else {
3228                        break;
3229                    }
3230                } else {
3231                    change = Some(Edit {
3232                        old_bytes: self.old_offset..self.old_offset,
3233                        new_bytes: self.new_offset..self.new_offset + fragment.len,
3234                        old_lines: self.old_point..self.old_point,
3235                    });
3236                }
3237
3238                self.new_offset += fragment.len;
3239                self.new_point += &fragment_lines;
3240            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
3241                let deleted_start = cursor.start().deleted;
3242                let fragment_lines = self.deleted_text.to_point(deleted_start + fragment.len)
3243                    - self.deleted_text.to_point(deleted_start);
3244                if let Some(ref mut change) = change {
3245                    if change.new_bytes.end == self.new_offset {
3246                        change.old_bytes.end += fragment.len;
3247                        change.old_lines.end += &fragment_lines;
3248                    } else {
3249                        break;
3250                    }
3251                } else {
3252                    change = Some(Edit {
3253                        old_bytes: self.old_offset..self.old_offset + fragment.len,
3254                        new_bytes: self.new_offset..self.new_offset,
3255                        old_lines: self.old_point..self.old_point + &fragment_lines,
3256                    });
3257                }
3258
3259                self.old_offset += fragment.len;
3260                self.old_point += &fragment_lines;
3261            }
3262
3263            cursor.next(&None);
3264        }
3265
3266        change
3267    }
3268}
3269
3270struct ByteChunks<'a>(rope::Chunks<'a>);
3271
3272impl<'a> Iterator for ByteChunks<'a> {
3273    type Item = &'a [u8];
3274
3275    fn next(&mut self) -> Option<Self::Item> {
3276        self.0.next().map(str::as_bytes)
3277    }
3278}
3279
3280struct TextProvider<'a>(&'a Rope);
3281
3282impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
3283    type I = ByteChunks<'a>;
3284
3285    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
3286        ByteChunks(self.0.chunks_in_range(node.byte_range()))
3287    }
3288}
3289
3290struct Highlights<'a> {
3291    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
3292    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
3293    stack: Vec<(usize, HighlightId)>,
3294    highlight_map: HighlightMap,
3295}
3296
3297pub struct HighlightedChunks<'a> {
3298    range: Range<usize>,
3299    chunks: Chunks<'a>,
3300    highlights: Option<Highlights<'a>>,
3301}
3302
3303impl<'a> HighlightedChunks<'a> {
3304    pub fn seek(&mut self, offset: usize) {
3305        self.range.start = offset;
3306        self.chunks.seek(self.range.start);
3307        if let Some(highlights) = self.highlights.as_mut() {
3308            highlights
3309                .stack
3310                .retain(|(end_offset, _)| *end_offset > offset);
3311            if let Some((mat, capture_ix)) = &highlights.next_capture {
3312                let capture = mat.captures[*capture_ix as usize];
3313                if offset >= capture.node.start_byte() {
3314                    let next_capture_end = capture.node.end_byte();
3315                    if offset < next_capture_end {
3316                        highlights.stack.push((
3317                            next_capture_end,
3318                            highlights.highlight_map.get(capture.index),
3319                        ));
3320                    }
3321                    highlights.next_capture.take();
3322                }
3323            }
3324            highlights.captures.set_byte_range(self.range.clone());
3325        }
3326    }
3327
3328    pub fn offset(&self) -> usize {
3329        self.range.start
3330    }
3331}
3332
3333impl<'a> Iterator for HighlightedChunks<'a> {
3334    type Item = (&'a str, HighlightId);
3335
3336    fn next(&mut self) -> Option<Self::Item> {
3337        let mut next_capture_start = usize::MAX;
3338
3339        if let Some(highlights) = self.highlights.as_mut() {
3340            while let Some((parent_capture_end, _)) = highlights.stack.last() {
3341                if *parent_capture_end <= self.range.start {
3342                    highlights.stack.pop();
3343                } else {
3344                    break;
3345                }
3346            }
3347
3348            if highlights.next_capture.is_none() {
3349                highlights.next_capture = highlights.captures.next();
3350            }
3351
3352            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
3353                let capture = mat.captures[*capture_ix as usize];
3354                if self.range.start < capture.node.start_byte() {
3355                    next_capture_start = capture.node.start_byte();
3356                    break;
3357                } else {
3358                    let style_id = highlights.highlight_map.get(capture.index);
3359                    highlights.stack.push((capture.node.end_byte(), style_id));
3360                    highlights.next_capture = highlights.captures.next();
3361                }
3362            }
3363        }
3364
3365        if let Some(chunk) = self.chunks.peek() {
3366            let chunk_start = self.range.start;
3367            let mut chunk_end = (self.chunks.offset() + chunk.len()).min(next_capture_start);
3368            let mut style_id = HighlightId::default();
3369            if let Some((parent_capture_end, parent_style_id)) =
3370                self.highlights.as_ref().and_then(|h| h.stack.last())
3371            {
3372                chunk_end = chunk_end.min(*parent_capture_end);
3373                style_id = *parent_style_id;
3374            }
3375
3376            let slice =
3377                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
3378            self.range.start = chunk_end;
3379            if self.range.start == self.chunks.offset() + chunk.len() {
3380                self.chunks.next().unwrap();
3381            }
3382
3383            Some((slice, style_id))
3384        } else {
3385            None
3386        }
3387    }
3388}
3389
3390impl Fragment {
3391    fn is_visible(&self, undos: &UndoMap) -> bool {
3392        !undos.is_undone(self.timestamp.local())
3393            && self.deletions.iter().all(|d| undos.is_undone(*d))
3394    }
3395
3396    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
3397        (version.observed(self.timestamp.local())
3398            && !undos.was_undone(self.timestamp.local(), version))
3399            && self
3400                .deletions
3401                .iter()
3402                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
3403    }
3404}
3405
3406impl sum_tree::Item for Fragment {
3407    type Summary = FragmentSummary;
3408
3409    fn summary(&self) -> Self::Summary {
3410        let mut max_version = clock::Global::new();
3411        max_version.observe(self.timestamp.local());
3412        for deletion in &self.deletions {
3413            max_version.observe(*deletion);
3414        }
3415        max_version.join(&self.max_undos);
3416
3417        let mut min_insertion_version = clock::Global::new();
3418        min_insertion_version.observe(self.timestamp.local());
3419        let max_insertion_version = min_insertion_version.clone();
3420        if self.visible {
3421            FragmentSummary {
3422                text: FragmentTextSummary {
3423                    visible: self.len,
3424                    deleted: 0,
3425                },
3426                max_version,
3427                min_insertion_version,
3428                max_insertion_version,
3429            }
3430        } else {
3431            FragmentSummary {
3432                text: FragmentTextSummary {
3433                    visible: 0,
3434                    deleted: self.len,
3435                },
3436                max_version,
3437                min_insertion_version,
3438                max_insertion_version,
3439            }
3440        }
3441    }
3442}
3443
3444impl sum_tree::Summary for FragmentSummary {
3445    type Context = Option<clock::Global>;
3446
3447    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
3448        self.text.visible += &other.text.visible;
3449        self.text.deleted += &other.text.deleted;
3450        self.max_version.join(&other.max_version);
3451        self.min_insertion_version
3452            .meet(&other.min_insertion_version);
3453        self.max_insertion_version
3454            .join(&other.max_insertion_version);
3455    }
3456}
3457
3458impl Default for FragmentSummary {
3459    fn default() -> Self {
3460        FragmentSummary {
3461            text: FragmentTextSummary::default(),
3462            max_version: clock::Global::new(),
3463            min_insertion_version: clock::Global::new(),
3464            max_insertion_version: clock::Global::new(),
3465        }
3466    }
3467}
3468
3469impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
3470    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
3471        *self += summary.text.visible;
3472    }
3473}
3474
3475impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
3476    fn cmp(
3477        &self,
3478        cursor_location: &FragmentTextSummary,
3479        _: &Option<clock::Global>,
3480    ) -> cmp::Ordering {
3481        Ord::cmp(self, &cursor_location.visible)
3482    }
3483}
3484
3485#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3486enum VersionedOffset {
3487    Offset(usize),
3488    InvalidVersion,
3489}
3490
3491impl VersionedOffset {
3492    fn offset(&self) -> usize {
3493        if let Self::Offset(offset) = self {
3494            *offset
3495        } else {
3496            panic!("invalid version")
3497        }
3498    }
3499}
3500
3501impl Default for VersionedOffset {
3502    fn default() -> Self {
3503        Self::Offset(0)
3504    }
3505}
3506
3507impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedOffset {
3508    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
3509        if let Self::Offset(offset) = self {
3510            let version = cx.as_ref().unwrap();
3511            if *version >= summary.max_insertion_version {
3512                *offset += summary.text.visible + summary.text.deleted;
3513            } else if !summary
3514                .min_insertion_version
3515                .iter()
3516                .all(|t| !version.observed(*t))
3517            {
3518                *self = Self::InvalidVersion;
3519            }
3520        }
3521    }
3522}
3523
3524impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedOffset {
3525    fn cmp(&self, other: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
3526        match (self, other) {
3527            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
3528            (Self::Offset(_), Self::InvalidVersion) => cmp::Ordering::Less,
3529            (Self::InvalidVersion, _) => unreachable!(),
3530        }
3531    }
3532}
3533
3534impl Operation {
3535    fn replica_id(&self) -> ReplicaId {
3536        self.lamport_timestamp().replica_id
3537    }
3538
3539    fn lamport_timestamp(&self) -> clock::Lamport {
3540        match self {
3541            Operation::Edit(edit) => edit.timestamp.lamport(),
3542            Operation::Undo {
3543                lamport_timestamp, ..
3544            } => *lamport_timestamp,
3545            Operation::UpdateSelections {
3546                lamport_timestamp, ..
3547            } => *lamport_timestamp,
3548            Operation::SetActiveSelections {
3549                lamport_timestamp, ..
3550            } => *lamport_timestamp,
3551            #[cfg(test)]
3552            Operation::Test(lamport_timestamp) => *lamport_timestamp,
3553        }
3554    }
3555
3556    pub fn is_edit(&self) -> bool {
3557        match self {
3558            Operation::Edit { .. } => true,
3559            _ => false,
3560        }
3561    }
3562}
3563
3564impl<'a> Into<proto::Operation> for &'a Operation {
3565    fn into(self) -> proto::Operation {
3566        proto::Operation {
3567            variant: Some(match self {
3568                Operation::Edit(edit) => proto::operation::Variant::Edit(edit.into()),
3569                Operation::Undo {
3570                    undo,
3571                    lamport_timestamp,
3572                } => proto::operation::Variant::Undo(proto::operation::Undo {
3573                    replica_id: undo.id.replica_id as u32,
3574                    local_timestamp: undo.id.value,
3575                    lamport_timestamp: lamport_timestamp.value,
3576                    ranges: undo
3577                        .ranges
3578                        .iter()
3579                        .map(|r| proto::Range {
3580                            start: r.start as u64,
3581                            end: r.end as u64,
3582                        })
3583                        .collect(),
3584                    counts: undo
3585                        .counts
3586                        .iter()
3587                        .map(|(edit_id, count)| proto::operation::UndoCount {
3588                            replica_id: edit_id.replica_id as u32,
3589                            local_timestamp: edit_id.value,
3590                            count: *count,
3591                        })
3592                        .collect(),
3593                    version: From::from(&undo.version),
3594                }),
3595                Operation::UpdateSelections {
3596                    set_id,
3597                    selections,
3598                    lamport_timestamp,
3599                } => proto::operation::Variant::UpdateSelections(
3600                    proto::operation::UpdateSelections {
3601                        replica_id: set_id.replica_id as u32,
3602                        local_timestamp: set_id.value,
3603                        lamport_timestamp: lamport_timestamp.value,
3604                        set: selections.as_ref().map(|selections| proto::SelectionSet {
3605                            selections: selections.iter().map(Into::into).collect(),
3606                        }),
3607                    },
3608                ),
3609                Operation::SetActiveSelections {
3610                    set_id,
3611                    lamport_timestamp,
3612                } => proto::operation::Variant::SetActiveSelections(
3613                    proto::operation::SetActiveSelections {
3614                        replica_id: lamport_timestamp.replica_id as u32,
3615                        local_timestamp: set_id.map(|set_id| set_id.value),
3616                        lamport_timestamp: lamport_timestamp.value,
3617                    },
3618                ),
3619                #[cfg(test)]
3620                Operation::Test(_) => unimplemented!(),
3621            }),
3622        }
3623    }
3624}
3625
3626impl<'a> Into<proto::operation::Edit> for &'a EditOperation {
3627    fn into(self) -> proto::operation::Edit {
3628        let ranges = self
3629            .ranges
3630            .iter()
3631            .map(|range| proto::Range {
3632                start: range.start as u64,
3633                end: range.end as u64,
3634            })
3635            .collect();
3636        proto::operation::Edit {
3637            replica_id: self.timestamp.replica_id as u32,
3638            local_timestamp: self.timestamp.local,
3639            lamport_timestamp: self.timestamp.lamport,
3640            version: From::from(&self.version),
3641            ranges,
3642            new_text: self.new_text.clone(),
3643        }
3644    }
3645}
3646
3647impl<'a> Into<proto::Anchor> for &'a Anchor {
3648    fn into(self) -> proto::Anchor {
3649        proto::Anchor {
3650            version: (&self.version).into(),
3651            offset: self.offset as u64,
3652            bias: match self.bias {
3653                Bias::Left => proto::anchor::Bias::Left as i32,
3654                Bias::Right => proto::anchor::Bias::Right as i32,
3655            },
3656        }
3657    }
3658}
3659
3660impl<'a> Into<proto::Selection> for &'a Selection {
3661    fn into(self) -> proto::Selection {
3662        proto::Selection {
3663            id: self.id as u64,
3664            start: Some((&self.start).into()),
3665            end: Some((&self.end).into()),
3666            reversed: self.reversed,
3667        }
3668    }
3669}
3670
3671impl TryFrom<proto::Operation> for Operation {
3672    type Error = anyhow::Error;
3673
3674    fn try_from(message: proto::Operation) -> Result<Self, Self::Error> {
3675        Ok(
3676            match message
3677                .variant
3678                .ok_or_else(|| anyhow!("missing operation variant"))?
3679            {
3680                proto::operation::Variant::Edit(edit) => Operation::Edit(edit.into()),
3681                proto::operation::Variant::Undo(undo) => Operation::Undo {
3682                    lamport_timestamp: clock::Lamport {
3683                        replica_id: undo.replica_id as ReplicaId,
3684                        value: undo.lamport_timestamp,
3685                    },
3686                    undo: UndoOperation {
3687                        id: clock::Local {
3688                            replica_id: undo.replica_id as ReplicaId,
3689                            value: undo.local_timestamp,
3690                        },
3691                        counts: undo
3692                            .counts
3693                            .into_iter()
3694                            .map(|c| {
3695                                (
3696                                    clock::Local {
3697                                        replica_id: c.replica_id as ReplicaId,
3698                                        value: c.local_timestamp,
3699                                    },
3700                                    c.count,
3701                                )
3702                            })
3703                            .collect(),
3704                        ranges: undo
3705                            .ranges
3706                            .into_iter()
3707                            .map(|r| r.start as usize..r.end as usize)
3708                            .collect(),
3709                        version: undo.version.into(),
3710                    },
3711                },
3712                proto::operation::Variant::UpdateSelections(message) => {
3713                    let selections: Option<Vec<Selection>> = if let Some(set) = message.set {
3714                        Some(
3715                            set.selections
3716                                .into_iter()
3717                                .map(TryFrom::try_from)
3718                                .collect::<Result<_, _>>()?,
3719                        )
3720                    } else {
3721                        None
3722                    };
3723                    Operation::UpdateSelections {
3724                        set_id: clock::Lamport {
3725                            replica_id: message.replica_id as ReplicaId,
3726                            value: message.local_timestamp,
3727                        },
3728                        lamport_timestamp: clock::Lamport {
3729                            replica_id: message.replica_id as ReplicaId,
3730                            value: message.lamport_timestamp,
3731                        },
3732                        selections: selections.map(Arc::from),
3733                    }
3734                }
3735                proto::operation::Variant::SetActiveSelections(message) => {
3736                    Operation::SetActiveSelections {
3737                        set_id: message.local_timestamp.map(|value| clock::Lamport {
3738                            replica_id: message.replica_id as ReplicaId,
3739                            value,
3740                        }),
3741                        lamport_timestamp: clock::Lamport {
3742                            replica_id: message.replica_id as ReplicaId,
3743                            value: message.lamport_timestamp,
3744                        },
3745                    }
3746                }
3747            },
3748        )
3749    }
3750}
3751
3752impl From<proto::operation::Edit> for EditOperation {
3753    fn from(edit: proto::operation::Edit) -> Self {
3754        let ranges = edit
3755            .ranges
3756            .into_iter()
3757            .map(|range| range.start as usize..range.end as usize)
3758            .collect();
3759        EditOperation {
3760            timestamp: InsertionTimestamp {
3761                replica_id: edit.replica_id as ReplicaId,
3762                local: edit.local_timestamp,
3763                lamport: edit.lamport_timestamp,
3764            },
3765            version: edit.version.into(),
3766            ranges,
3767            new_text: edit.new_text,
3768        }
3769    }
3770}
3771
3772impl TryFrom<proto::Anchor> for Anchor {
3773    type Error = anyhow::Error;
3774
3775    fn try_from(message: proto::Anchor) -> Result<Self, Self::Error> {
3776        let mut version = clock::Global::new();
3777        for entry in message.version {
3778            version.observe(clock::Local {
3779                replica_id: entry.replica_id as ReplicaId,
3780                value: entry.timestamp,
3781            });
3782        }
3783
3784        Ok(Self {
3785            offset: message.offset as usize,
3786            bias: if message.bias == proto::anchor::Bias::Left as i32 {
3787                Bias::Left
3788            } else if message.bias == proto::anchor::Bias::Right as i32 {
3789                Bias::Right
3790            } else {
3791                Err(anyhow!("invalid anchor bias {}", message.bias))?
3792            },
3793            version,
3794        })
3795    }
3796}
3797
3798impl TryFrom<proto::Selection> for Selection {
3799    type Error = anyhow::Error;
3800
3801    fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> {
3802        Ok(Selection {
3803            id: selection.id as usize,
3804            start: selection
3805                .start
3806                .ok_or_else(|| anyhow!("missing selection start"))?
3807                .try_into()?,
3808            end: selection
3809                .end
3810                .ok_or_else(|| anyhow!("missing selection end"))?
3811                .try_into()?,
3812            reversed: selection.reversed,
3813            goal: SelectionGoal::None,
3814        })
3815    }
3816}
3817
3818pub trait ToOffset {
3819    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize;
3820}
3821
3822impl ToOffset for Point {
3823    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3824        content.into().visible_text.to_offset(*self)
3825    }
3826}
3827
3828impl ToOffset for usize {
3829    fn to_offset<'a>(&self, _: impl Into<Content<'a>>) -> usize {
3830        *self
3831    }
3832}
3833
3834impl ToOffset for Anchor {
3835    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3836        content.into().summary_for_anchor(self).bytes
3837    }
3838}
3839
3840impl<'a> ToOffset for &'a Anchor {
3841    fn to_offset<'b>(&self, content: impl Into<Content<'b>>) -> usize {
3842        content.into().summary_for_anchor(self).bytes
3843    }
3844}
3845
3846pub trait ToPoint {
3847    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point;
3848}
3849
3850impl ToPoint for Anchor {
3851    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3852        content.into().summary_for_anchor(self).lines
3853    }
3854}
3855
3856impl ToPoint for usize {
3857    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3858        content.into().visible_text.to_point(*self)
3859    }
3860}
3861
3862fn contiguous_ranges(
3863    values: impl IntoIterator<Item = u32>,
3864    max_len: usize,
3865) -> impl Iterator<Item = Range<u32>> {
3866    let mut values = values.into_iter();
3867    let mut current_range: Option<Range<u32>> = None;
3868    std::iter::from_fn(move || loop {
3869        if let Some(value) = values.next() {
3870            if let Some(range) = &mut current_range {
3871                if value == range.end && range.len() < max_len {
3872                    range.end += 1;
3873                    continue;
3874                }
3875            }
3876
3877            let prev_range = current_range.clone();
3878            current_range = Some(value..(value + 1));
3879            if prev_range.is_some() {
3880                return prev_range;
3881            }
3882        } else {
3883            return current_range.take();
3884        }
3885    })
3886}