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