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
  11pub use anchor::*;
  12use anyhow::{anyhow, Result};
  13use clock::ReplicaId;
  14use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task};
  15pub use highlight_map::{HighlightId, HighlightMap};
  16use language::Tree;
  17pub use language::{AutoclosePair, Language, LanguageConfig, LanguageRegistry};
  18use lazy_static::lazy_static;
  19use operation_queue::OperationQueue;
  20use parking_lot::Mutex;
  21pub use point::*;
  22#[cfg(any(test, feature = "test-support"))]
  23pub use random_char_iter::*;
  24pub use rope::{Chunks, Rope, TextSummary};
  25use rpc::proto;
  26use seahash::SeaHasher;
  27pub use selection::*;
  28use similar::{ChangeTag, TextDiff};
  29use std::{
  30    any::Any,
  31    cell::RefCell,
  32    cmp,
  33    collections::{BTreeMap, VecDeque},
  34    convert::{TryFrom, TryInto},
  35    ffi::OsString,
  36    hash::BuildHasher,
  37    iter::Iterator,
  38    ops::{Deref, DerefMut, Range},
  39    path::{Path, PathBuf},
  40    str,
  41    sync::Arc,
  42    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  43};
  44use sum_tree::{Bias, FilterCursor, SumTree};
  45use tree_sitter::{InputEdit, Parser, QueryCursor};
  46
  47pub trait File {
  48    fn worktree_id(&self) -> usize;
  49
  50    fn entry_id(&self) -> Option<usize>;
  51
  52    fn set_entry_id(&mut self, entry_id: Option<usize>);
  53
  54    fn mtime(&self) -> SystemTime;
  55
  56    fn set_mtime(&mut self, mtime: SystemTime);
  57
  58    fn path(&self) -> &Arc<Path>;
  59
  60    fn set_path(&mut self, path: Arc<Path>);
  61
  62    fn full_path(&self, cx: &AppContext) -> PathBuf;
  63
  64    /// Returns the last component of this handle's absolute path. If this handle refers to the root
  65    /// of its worktree, then this method will return the name of the worktree itself.
  66    fn file_name<'a>(&'a self, cx: &'a AppContext) -> Option<OsString>;
  67
  68    fn is_deleted(&self) -> bool;
  69
  70    fn save(
  71        &self,
  72        buffer_id: u64,
  73        text: Rope,
  74        version: clock::Global,
  75        cx: &mut MutableAppContext,
  76    ) -> Task<Result<(clock::Global, SystemTime)>>;
  77
  78    fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext);
  79
  80    fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext);
  81
  82    fn boxed_clone(&self) -> Box<dyn File>;
  83
  84    fn as_any(&self) -> &dyn Any;
  85}
  86
  87#[derive(Clone, Default)]
  88struct DeterministicState;
  89
  90impl BuildHasher for DeterministicState {
  91    type Hasher = SeaHasher;
  92
  93    fn build_hasher(&self) -> Self::Hasher {
  94        SeaHasher::new()
  95    }
  96}
  97
  98#[cfg(any(test, feature = "test-support"))]
  99type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
 100
 101#[cfg(any(test, feature = "test-support"))]
 102type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
 103
 104#[cfg(not(any(test, feature = "test-support")))]
 105type HashMap<K, V> = std::collections::HashMap<K, V>;
 106
 107#[cfg(not(any(test, feature = "test-support")))]
 108type HashSet<T> = std::collections::HashSet<T>;
 109
 110thread_local! {
 111    static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
 112}
 113
 114lazy_static! {
 115    static ref QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Default::default();
 116}
 117
 118// TODO - Make this configurable
 119const INDENT_SIZE: u32 = 4;
 120
 121struct QueryCursorHandle(Option<QueryCursor>);
 122
 123impl QueryCursorHandle {
 124    fn new() -> Self {
 125        QueryCursorHandle(Some(
 126            QUERY_CURSORS
 127                .lock()
 128                .pop()
 129                .unwrap_or_else(|| QueryCursor::new()),
 130        ))
 131    }
 132}
 133
 134impl Deref for QueryCursorHandle {
 135    type Target = QueryCursor;
 136
 137    fn deref(&self) -> &Self::Target {
 138        self.0.as_ref().unwrap()
 139    }
 140}
 141
 142impl DerefMut for QueryCursorHandle {
 143    fn deref_mut(&mut self) -> &mut Self::Target {
 144        self.0.as_mut().unwrap()
 145    }
 146}
 147
 148impl Drop for QueryCursorHandle {
 149    fn drop(&mut self) {
 150        let mut cursor = self.0.take().unwrap();
 151        cursor.set_byte_range(0..usize::MAX);
 152        cursor.set_point_range(Point::zero().into()..Point::MAX.into());
 153        QUERY_CURSORS.lock().push(cursor)
 154    }
 155}
 156
 157pub struct Buffer {
 158    fragments: SumTree<Fragment>,
 159    visible_text: Rope,
 160    deleted_text: Rope,
 161    pub version: clock::Global,
 162    saved_version: clock::Global,
 163    saved_mtime: SystemTime,
 164    last_edit: clock::Local,
 165    undo_map: UndoMap,
 166    history: History,
 167    file: Option<Box<dyn File>>,
 168    language: Option<Arc<Language>>,
 169    autoindent_requests: VecDeque<AutoindentRequest>,
 170    sync_parse_timeout: Duration,
 171    syntax_tree: Mutex<Option<SyntaxTree>>,
 172    parsing_in_background: bool,
 173    parse_count: usize,
 174    selections: HashMap<SelectionSetId, SelectionSet>,
 175    deferred_ops: OperationQueue,
 176    deferred_replicas: HashSet<ReplicaId>,
 177    replica_id: ReplicaId,
 178    remote_id: u64,
 179    local_clock: clock::Local,
 180    lamport_clock: clock::Lamport,
 181    #[cfg(test)]
 182    operations: Vec<Operation>,
 183}
 184
 185#[derive(Clone, Debug, Eq, PartialEq)]
 186pub struct SelectionSet {
 187    pub selections: Arc<[Selection]>,
 188    pub active: bool,
 189}
 190
 191#[derive(Clone)]
 192struct SyntaxTree {
 193    tree: Tree,
 194    version: clock::Global,
 195}
 196
 197struct AutoindentRequest {
 198    before_edit: Snapshot,
 199    version_after_edit: clock::Global,
 200    edited: AnchorSet,
 201    inserted: Option<AnchorRangeSet>,
 202}
 203
 204impl AutoindentRequest {
 205    fn version_after_edit(&self) -> &clock::Global {
 206        &self.version_after_edit
 207    }
 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: Option<(SelectionSetId, Arc<[Selection]>)>,
 218    selections_after: Option<(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: Option<(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: selections,
 314                selections_after: None,
 315                first_edit_at: now,
 316                last_edit_at: now,
 317            });
 318        }
 319    }
 320
 321    fn end_transaction(
 322        &mut self,
 323        selections: Option<(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;
 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.selections_after = transaction.selections_after.take();
 371                last_transaction.end = transaction.end.clone();
 372            }
 373        }
 374
 375        self.undo_stack.truncate(new_len);
 376    }
 377
 378    fn push_undo(&mut self, edit_id: clock::Local) {
 379        assert_ne!(self.transaction_depth, 0);
 380        let last_transaction = self.undo_stack.last_mut().unwrap();
 381        last_transaction.push_edit(&self.ops[&edit_id]);
 382    }
 383
 384    fn pop_undo(&mut self) -> Option<&Transaction> {
 385        assert_eq!(self.transaction_depth, 0);
 386        if let Some(transaction) = self.undo_stack.pop() {
 387            self.redo_stack.push(transaction);
 388            self.redo_stack.last()
 389        } else {
 390            None
 391        }
 392    }
 393
 394    fn pop_redo(&mut self) -> Option<&Transaction> {
 395        assert_eq!(self.transaction_depth, 0);
 396        if let Some(transaction) = self.redo_stack.pop() {
 397            self.undo_stack.push(transaction);
 398            self.undo_stack.last()
 399        } else {
 400            None
 401        }
 402    }
 403}
 404
 405#[derive(Clone, Default, Debug)]
 406struct UndoMap(HashMap<clock::Local, Vec<(clock::Local, u32)>>);
 407
 408impl UndoMap {
 409    fn insert(&mut self, undo: &UndoOperation) {
 410        for (edit_id, count) in &undo.counts {
 411            self.0.entry(*edit_id).or_default().push((undo.id, *count));
 412        }
 413    }
 414
 415    fn is_undone(&self, edit_id: clock::Local) -> bool {
 416        self.undo_count(edit_id) % 2 == 1
 417    }
 418
 419    fn was_undone(&self, edit_id: clock::Local, version: &clock::Global) -> bool {
 420        let undo_count = self
 421            .0
 422            .get(&edit_id)
 423            .unwrap_or(&Vec::new())
 424            .iter()
 425            .filter(|(undo_id, _)| version.observed(*undo_id))
 426            .map(|(_, undo_count)| *undo_count)
 427            .max()
 428            .unwrap_or(0);
 429        undo_count % 2 == 1
 430    }
 431
 432    fn undo_count(&self, edit_id: clock::Local) -> u32 {
 433        self.0
 434            .get(&edit_id)
 435            .unwrap_or(&Vec::new())
 436            .iter()
 437            .map(|(_, undo_count)| *undo_count)
 438            .max()
 439            .unwrap_or(0)
 440    }
 441}
 442
 443struct Edits<'a, F: Fn(&FragmentSummary) -> bool> {
 444    visible_text: &'a Rope,
 445    deleted_text: &'a Rope,
 446    cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
 447    undos: &'a UndoMap,
 448    since: clock::Global,
 449    old_offset: usize,
 450    new_offset: usize,
 451    old_point: Point,
 452    new_point: Point,
 453}
 454
 455#[derive(Clone, Debug, Default, Eq, PartialEq)]
 456pub struct Edit {
 457    pub old_bytes: Range<usize>,
 458    pub new_bytes: Range<usize>,
 459    pub old_lines: Range<Point>,
 460}
 461
 462impl Edit {
 463    pub fn delta(&self) -> isize {
 464        self.inserted_bytes() as isize - self.deleted_bytes() as isize
 465    }
 466
 467    pub fn deleted_bytes(&self) -> usize {
 468        self.old_bytes.end - self.old_bytes.start
 469    }
 470
 471    pub fn inserted_bytes(&self) -> usize {
 472        self.new_bytes.end - self.new_bytes.start
 473    }
 474
 475    pub fn deleted_lines(&self) -> Point {
 476        self.old_lines.end - self.old_lines.start
 477    }
 478}
 479
 480struct Diff {
 481    base_version: clock::Global,
 482    new_text: Arc<str>,
 483    changes: Vec<(ChangeTag, usize)>,
 484}
 485
 486#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
 487struct InsertionTimestamp {
 488    replica_id: ReplicaId,
 489    local: clock::Seq,
 490    lamport: clock::Seq,
 491}
 492
 493impl InsertionTimestamp {
 494    fn local(&self) -> clock::Local {
 495        clock::Local {
 496            replica_id: self.replica_id,
 497            value: self.local,
 498        }
 499    }
 500
 501    fn lamport(&self) -> clock::Lamport {
 502        clock::Lamport {
 503            replica_id: self.replica_id,
 504            value: self.lamport,
 505        }
 506    }
 507}
 508
 509#[derive(Eq, PartialEq, Clone, Debug)]
 510struct Fragment {
 511    timestamp: InsertionTimestamp,
 512    len: usize,
 513    visible: bool,
 514    deletions: HashSet<clock::Local>,
 515    max_undos: clock::Global,
 516}
 517
 518#[derive(Eq, PartialEq, Clone, Debug)]
 519pub struct FragmentSummary {
 520    text: FragmentTextSummary,
 521    max_version: clock::Global,
 522    min_insertion_version: clock::Global,
 523    max_insertion_version: clock::Global,
 524}
 525
 526#[derive(Copy, Default, Clone, Debug, PartialEq, Eq)]
 527struct FragmentTextSummary {
 528    visible: usize,
 529    deleted: usize,
 530}
 531
 532impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentTextSummary {
 533    fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
 534        self.visible += summary.text.visible;
 535        self.deleted += summary.text.deleted;
 536    }
 537}
 538
 539#[derive(Clone, Debug, Eq, PartialEq)]
 540pub enum Operation {
 541    Edit(EditOperation),
 542    Undo {
 543        undo: UndoOperation,
 544        lamport_timestamp: clock::Lamport,
 545    },
 546    UpdateSelections {
 547        set_id: SelectionSetId,
 548        selections: Option<Arc<[Selection]>>,
 549        lamport_timestamp: clock::Lamport,
 550    },
 551    SetActiveSelections {
 552        set_id: Option<SelectionSetId>,
 553        lamport_timestamp: clock::Lamport,
 554    },
 555    #[cfg(test)]
 556    Test(clock::Lamport),
 557}
 558
 559#[derive(Clone, Debug, Eq, PartialEq)]
 560pub struct EditOperation {
 561    timestamp: InsertionTimestamp,
 562    version: clock::Global,
 563    ranges: Vec<Range<usize>>,
 564    new_text: Option<String>,
 565}
 566
 567#[derive(Clone, Debug, Eq, PartialEq)]
 568pub struct UndoOperation {
 569    id: clock::Local,
 570    counts: HashMap<clock::Local, u32>,
 571    ranges: Vec<Range<usize>>,
 572    version: clock::Global,
 573}
 574
 575impl Buffer {
 576    pub fn new<T: Into<Arc<str>>>(
 577        replica_id: ReplicaId,
 578        base_text: T,
 579        cx: &mut ModelContext<Self>,
 580    ) -> Self {
 581        Self::build(
 582            replica_id,
 583            History::new(base_text.into()),
 584            None,
 585            cx.model_id() as u64,
 586            None,
 587            cx,
 588        )
 589    }
 590
 591    pub fn from_history(
 592        replica_id: ReplicaId,
 593        history: History,
 594        file: Option<Box<dyn File>>,
 595        language: Option<Arc<Language>>,
 596        cx: &mut ModelContext<Self>,
 597    ) -> Self {
 598        Self::build(
 599            replica_id,
 600            history,
 601            file,
 602            cx.model_id() as u64,
 603            language,
 604            cx,
 605        )
 606    }
 607
 608    fn build(
 609        replica_id: ReplicaId,
 610        history: History,
 611        file: Option<Box<dyn File>>,
 612        remote_id: u64,
 613        language: Option<Arc<Language>>,
 614        cx: &mut ModelContext<Self>,
 615    ) -> Self {
 616        let saved_mtime;
 617        if let Some(file) = file.as_ref() {
 618            saved_mtime = file.mtime();
 619        } else {
 620            saved_mtime = UNIX_EPOCH;
 621        }
 622
 623        let mut fragments = SumTree::new();
 624
 625        let visible_text = Rope::from(history.base_text.as_ref());
 626        if visible_text.len() > 0 {
 627            fragments.push(
 628                Fragment {
 629                    timestamp: Default::default(),
 630                    len: visible_text.len(),
 631                    visible: true,
 632                    deletions: Default::default(),
 633                    max_undos: Default::default(),
 634                },
 635                &None,
 636            );
 637        }
 638
 639        let mut result = Self {
 640            visible_text,
 641            deleted_text: Rope::new(),
 642            fragments,
 643            version: clock::Global::new(),
 644            saved_version: clock::Global::new(),
 645            last_edit: clock::Local::default(),
 646            undo_map: Default::default(),
 647            history,
 648            file,
 649            syntax_tree: Mutex::new(None),
 650            parsing_in_background: false,
 651            parse_count: 0,
 652            sync_parse_timeout: Duration::from_millis(1),
 653            autoindent_requests: Default::default(),
 654            language,
 655            saved_mtime,
 656            selections: HashMap::default(),
 657            deferred_ops: OperationQueue::new(),
 658            deferred_replicas: HashSet::default(),
 659            replica_id,
 660            remote_id,
 661            local_clock: clock::Local::new(replica_id),
 662            lamport_clock: clock::Lamport::new(replica_id),
 663
 664            #[cfg(test)]
 665            operations: Default::default(),
 666        };
 667        result.reparse(cx);
 668        result
 669    }
 670
 671    pub fn replica_id(&self) -> ReplicaId {
 672        self.local_clock.replica_id
 673    }
 674
 675    pub fn snapshot(&self) -> Snapshot {
 676        Snapshot {
 677            visible_text: self.visible_text.clone(),
 678            fragments: self.fragments.clone(),
 679            version: self.version.clone(),
 680            tree: self.syntax_tree(),
 681            is_parsing: self.parsing_in_background,
 682            language: self.language.clone(),
 683            query_cursor: QueryCursorHandle::new(),
 684        }
 685    }
 686
 687    pub fn from_proto(
 688        replica_id: ReplicaId,
 689        message: proto::Buffer,
 690        file: Option<Box<dyn File>>,
 691        language: Option<Arc<Language>>,
 692        cx: &mut ModelContext<Self>,
 693    ) -> Result<Self> {
 694        let mut buffer = Buffer::build(
 695            replica_id,
 696            History::new(message.content.into()),
 697            file,
 698            message.id,
 699            language,
 700            cx,
 701        );
 702        let ops = message
 703            .history
 704            .into_iter()
 705            .map(|op| Operation::Edit(op.into()));
 706        buffer.apply_ops(ops, cx)?;
 707        buffer.selections = message
 708            .selections
 709            .into_iter()
 710            .map(|set| {
 711                let set_id = clock::Lamport {
 712                    replica_id: set.replica_id as ReplicaId,
 713                    value: set.local_timestamp,
 714                };
 715                let selections: Vec<Selection> = set
 716                    .selections
 717                    .into_iter()
 718                    .map(TryFrom::try_from)
 719                    .collect::<Result<_, _>>()?;
 720                let set = SelectionSet {
 721                    selections: Arc::from(selections),
 722                    active: set.is_active,
 723                };
 724                Result::<_, anyhow::Error>::Ok((set_id, set))
 725            })
 726            .collect::<Result<_, _>>()?;
 727        Ok(buffer)
 728    }
 729
 730    pub fn to_proto(&self, cx: &mut ModelContext<Self>) -> proto::Buffer {
 731        let ops = self.history.ops.values().map(Into::into).collect();
 732        proto::Buffer {
 733            id: cx.model_id() as u64,
 734            content: self.history.base_text.to_string(),
 735            history: ops,
 736            selections: self
 737                .selections
 738                .iter()
 739                .map(|(set_id, set)| proto::SelectionSetSnapshot {
 740                    replica_id: set_id.replica_id as u32,
 741                    local_timestamp: set_id.value,
 742                    selections: set.selections.iter().map(Into::into).collect(),
 743                    is_active: set.active,
 744                })
 745                .collect(),
 746        }
 747    }
 748
 749    pub fn file(&self) -> Option<&dyn File> {
 750        self.file.as_deref()
 751    }
 752
 753    pub fn file_mut(&mut self) -> Option<&mut dyn File> {
 754        self.file.as_mut().map(|f| f.deref_mut() as &mut dyn File)
 755    }
 756
 757    pub fn save(
 758        &mut self,
 759        cx: &mut ModelContext<Self>,
 760    ) -> Result<Task<Result<(clock::Global, SystemTime)>>> {
 761        let file = self
 762            .file
 763            .as_ref()
 764            .ok_or_else(|| anyhow!("buffer has no file"))?;
 765        let text = self.visible_text.clone();
 766        let version = self.version.clone();
 767        let save = file.save(self.remote_id, text, version, cx.as_mut());
 768        Ok(cx.spawn(|this, mut cx| async move {
 769            let (version, mtime) = save.await?;
 770            this.update(&mut cx, |this, cx| {
 771                this.did_save(version.clone(), mtime, None, cx);
 772            });
 773            Ok((version, mtime))
 774        }))
 775    }
 776
 777    pub fn as_rope(&self) -> &Rope {
 778        &self.visible_text
 779    }
 780
 781    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 782        self.language = language;
 783        self.reparse(cx);
 784    }
 785
 786    pub fn did_save(
 787        &mut self,
 788        version: clock::Global,
 789        mtime: SystemTime,
 790        new_file: Option<Box<dyn File>>,
 791        cx: &mut ModelContext<Self>,
 792    ) {
 793        self.saved_mtime = mtime;
 794        self.saved_version = version;
 795        if let Some(new_file) = new_file {
 796            self.file = Some(new_file);
 797        }
 798        cx.emit(Event::Saved);
 799    }
 800
 801    pub fn file_updated(
 802        &mut self,
 803        path: Arc<Path>,
 804        mtime: SystemTime,
 805        new_text: Option<String>,
 806        cx: &mut ModelContext<Self>,
 807    ) {
 808        let file = self.file.as_mut().unwrap();
 809        let mut changed = false;
 810        if path != *file.path() {
 811            file.set_path(path);
 812            changed = true;
 813        }
 814
 815        if mtime != file.mtime() {
 816            file.set_mtime(mtime);
 817            changed = true;
 818            if let Some(new_text) = new_text {
 819                if self.version == self.saved_version {
 820                    cx.spawn(|this, mut cx| async move {
 821                        let diff = this
 822                            .read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
 823                            .await;
 824                        this.update(&mut cx, |this, cx| {
 825                            if this.apply_diff(diff, cx) {
 826                                this.saved_version = this.version.clone();
 827                                this.saved_mtime = mtime;
 828                                cx.emit(Event::Reloaded);
 829                            }
 830                        });
 831                    })
 832                    .detach();
 833                }
 834            }
 835        }
 836
 837        if changed {
 838            cx.emit(Event::FileHandleChanged);
 839        }
 840    }
 841
 842    pub fn file_deleted(&mut self, cx: &mut ModelContext<Self>) {
 843        if self.version == self.saved_version {
 844            cx.emit(Event::Dirtied);
 845        }
 846        cx.emit(Event::FileHandleChanged);
 847    }
 848
 849    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 850        cx.emit(Event::Closed);
 851    }
 852
 853    pub fn language(&self) -> Option<&Arc<Language>> {
 854        self.language.as_ref()
 855    }
 856
 857    pub fn parse_count(&self) -> usize {
 858        self.parse_count
 859    }
 860
 861    fn syntax_tree(&self) -> Option<Tree> {
 862        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
 863            self.interpolate_tree(syntax_tree);
 864            Some(syntax_tree.tree.clone())
 865        } else {
 866            None
 867        }
 868    }
 869
 870    #[cfg(any(test, feature = "test-support"))]
 871    pub fn is_parsing(&self) -> bool {
 872        self.parsing_in_background
 873    }
 874
 875    #[cfg(test)]
 876    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 877        self.sync_parse_timeout = timeout;
 878    }
 879
 880    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
 881        if self.parsing_in_background {
 882            return false;
 883        }
 884
 885        if let Some(language) = self.language.clone() {
 886            let old_tree = self.syntax_tree();
 887            let text = self.visible_text.clone();
 888            let parsed_version = self.version();
 889            let parse_task = cx.background().spawn({
 890                let language = language.clone();
 891                async move { Self::parse_text(&text, old_tree, &language) }
 892            });
 893
 894            match cx
 895                .background()
 896                .block_with_timeout(self.sync_parse_timeout, parse_task)
 897            {
 898                Ok(new_tree) => {
 899                    self.did_finish_parsing(new_tree, parsed_version, language, cx);
 900                    return true;
 901                }
 902                Err(parse_task) => {
 903                    self.parsing_in_background = true;
 904                    cx.spawn(move |this, mut cx| async move {
 905                        let new_tree = parse_task.await;
 906                        this.update(&mut cx, move |this, cx| {
 907                            let language_changed =
 908                                this.language.as_ref().map_or(true, |curr_language| {
 909                                    !Arc::ptr_eq(curr_language, &language)
 910                                });
 911                            let parse_again = this.version > parsed_version || language_changed;
 912                            this.parsing_in_background = false;
 913                            this.did_finish_parsing(new_tree, parsed_version, language, cx);
 914
 915                            if parse_again && this.reparse(cx) {
 916                                return;
 917                            }
 918                        });
 919                    })
 920                    .detach();
 921                }
 922            }
 923        }
 924        false
 925    }
 926
 927    fn parse_text(text: &Rope, old_tree: Option<Tree>, language: &Language) -> Tree {
 928        PARSER.with(|parser| {
 929            let mut parser = parser.borrow_mut();
 930            parser
 931                .set_language(language.grammar)
 932                .expect("incompatible grammar");
 933            let mut chunks = text.chunks_in_range(0..text.len());
 934            let tree = parser
 935                .parse_with(
 936                    &mut move |offset, _| {
 937                        chunks.seek(offset);
 938                        chunks.next().unwrap_or("").as_bytes()
 939                    },
 940                    old_tree.as_ref(),
 941                )
 942                .unwrap();
 943            tree
 944        })
 945    }
 946
 947    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
 948        let mut delta = 0_isize;
 949        for edit in self.edits_since(tree.version.clone()) {
 950            let start_offset = (edit.old_bytes.start as isize + delta) as usize;
 951            let start_point = self.visible_text.to_point(start_offset);
 952            tree.tree.edit(&InputEdit {
 953                start_byte: start_offset,
 954                old_end_byte: start_offset + edit.deleted_bytes(),
 955                new_end_byte: start_offset + edit.inserted_bytes(),
 956                start_position: start_point.into(),
 957                old_end_position: (start_point + edit.deleted_lines()).into(),
 958                new_end_position: self
 959                    .visible_text
 960                    .to_point(start_offset + edit.inserted_bytes())
 961                    .into(),
 962            });
 963            delta += edit.inserted_bytes() as isize - edit.deleted_bytes() as isize;
 964        }
 965        tree.version = self.version();
 966    }
 967
 968    fn did_finish_parsing(
 969        &mut self,
 970        tree: Tree,
 971        version: clock::Global,
 972        language: Arc<Language>,
 973        cx: &mut ModelContext<Self>,
 974    ) {
 975        self.perform_autoindent(&tree, &version, language, cx);
 976        self.parse_count += 1;
 977        *self.syntax_tree.lock() = Some(SyntaxTree { tree, version });
 978        cx.emit(Event::Reparsed);
 979        cx.notify();
 980    }
 981
 982    fn perform_autoindent(
 983        &mut self,
 984        new_tree: &Tree,
 985        new_version: &clock::Global,
 986        language: Arc<Language>,
 987        cx: &mut ModelContext<Self>,
 988    ) {
 989        let mut cursor = QueryCursorHandle::new();
 990        while let Some(request) = self.autoindent_requests.front() {
 991            if new_version < request.version_after_edit() {
 992                break;
 993            }
 994
 995            let request = self.autoindent_requests.pop_front().unwrap();
 996
 997            let old_to_new_rows = request
 998                .edited
 999                .to_points(request.before_edit.content())
1000                .map(|point| point.row)
1001                .zip(
1002                    request
1003                        .edited
1004                        .to_points(self.content())
1005                        .map(|point| point.row),
1006                )
1007                .collect::<BTreeMap<u32, u32>>();
1008
1009            let mut old_suggestions = HashMap::default();
1010            if let Some(old_tree) = &request.before_edit.tree {
1011                let old_edited_ranges = contiguous_ranges(old_to_new_rows.keys().copied());
1012                for old_edited_range in old_edited_ranges {
1013                    let old_content = request.before_edit.content();
1014                    let suggestions = old_content.suggest_autoindents(
1015                        old_edited_range.clone(),
1016                        old_tree,
1017                        &language,
1018                        &mut cursor,
1019                    );
1020                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
1021                        let indentation_basis = old_to_new_rows
1022                            .get(&suggestion.basis_row)
1023                            .and_then(|from_row| old_suggestions.get(from_row).copied())
1024                            .unwrap_or_else(|| {
1025                                request
1026                                    .before_edit
1027                                    .indent_column_for_line(suggestion.basis_row)
1028                            });
1029                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1030                        old_suggestions.insert(
1031                            *old_to_new_rows.get(&old_row).unwrap(),
1032                            indentation_basis + delta,
1033                        );
1034                    }
1035                }
1036            }
1037
1038            // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
1039            // buffer before the edit, but keyed by the row for these lines after the edits were applied.
1040
1041            self.start_transaction(None).unwrap();
1042            let new_edited_row_ranges = contiguous_ranges(old_to_new_rows.values().copied());
1043            for new_edited_row_range in new_edited_row_ranges {
1044                let suggestions = self
1045                    .content()
1046                    .suggest_autoindents(
1047                        new_edited_row_range.clone(),
1048                        &new_tree,
1049                        &language,
1050                        &mut cursor,
1051                    )
1052                    .collect::<Vec<_>>();
1053                for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1054                    let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1055                    let new_indentation = self.indent_column_for_line(suggestion.basis_row) + delta;
1056                    if old_suggestions
1057                        .get(&new_row)
1058                        .map_or(true, |old_indentation| new_indentation != *old_indentation)
1059                    {
1060                        self.set_indent_column_for_line(new_row, new_indentation, cx);
1061                    }
1062                }
1063            }
1064
1065            if let Some(inserted) = request.inserted {
1066                let inserted_row_ranges = contiguous_ranges(
1067                    inserted
1068                        .to_point_ranges(self.content())
1069                        .flat_map(|range| range.start.row..range.end.row + 1),
1070                )
1071                .collect::<Vec<_>>();
1072                for inserted_row_range in inserted_row_ranges {
1073                    let suggestions = self
1074                        .content()
1075                        .suggest_autoindents(
1076                            inserted_row_range.clone(),
1077                            &new_tree,
1078                            &language,
1079                            &mut cursor,
1080                        )
1081                        .collect::<Vec<_>>();
1082
1083                    for (row, suggestion) in inserted_row_range.zip(suggestions) {
1084                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1085                        let new_indentation =
1086                            self.indent_column_for_line(suggestion.basis_row) + delta;
1087                        self.set_indent_column_for_line(row, new_indentation, cx);
1088                    }
1089                }
1090            }
1091
1092            self.end_transaction(None, cx).unwrap();
1093        }
1094    }
1095
1096    pub fn indent_column_for_line(&self, row: u32) -> u32 {
1097        self.content().indent_column_for_line(row)
1098    }
1099
1100    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
1101        let current_column = self.indent_column_for_line(row);
1102        if column > current_column {
1103            let offset = self.visible_text.to_offset(Point::new(row, 0));
1104
1105            // TODO: do this differently. By replacing the preceding newline,
1106            // we force the new indentation to come before any left-biased anchors
1107            // on the line.
1108            let delta = (column - current_column) as usize;
1109            if offset > 0 {
1110                let mut prefix = String::with_capacity(1 + delta);
1111                prefix.push('\n');
1112                prefix.extend(std::iter::repeat(' ').take(delta));
1113                self.edit([(offset - 1)..offset], prefix, cx);
1114            } else {
1115                self.edit(
1116                    [offset..offset],
1117                    std::iter::repeat(' ').take(delta).collect::<String>(),
1118                    cx,
1119                );
1120            }
1121        } else if column < current_column {
1122            self.edit(
1123                [Point::new(row, 0)..Point::new(row, current_column - column)],
1124                "",
1125                cx,
1126            );
1127        }
1128    }
1129
1130    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1131        if let Some(tree) = self.syntax_tree() {
1132            let root = tree.root_node();
1133            let range = range.start.to_offset(self)..range.end.to_offset(self);
1134            let mut node = root.descendant_for_byte_range(range.start, range.end);
1135            while node.map_or(false, |n| n.byte_range() == range) {
1136                node = node.unwrap().parent();
1137            }
1138            node.map(|n| n.byte_range())
1139        } else {
1140            None
1141        }
1142    }
1143
1144    pub fn enclosing_bracket_ranges<T: ToOffset>(
1145        &self,
1146        range: Range<T>,
1147    ) -> Option<(Range<usize>, Range<usize>)> {
1148        let (lang, tree) = self.language.as_ref().zip(self.syntax_tree())?;
1149        let open_capture_ix = lang.brackets_query.capture_index_for_name("open")?;
1150        let close_capture_ix = lang.brackets_query.capture_index_for_name("close")?;
1151
1152        // Find bracket pairs that *inclusively* contain the given range.
1153        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1154        let mut cursor = QueryCursorHandle::new();
1155        let matches = cursor.set_byte_range(range).matches(
1156            &lang.brackets_query,
1157            tree.root_node(),
1158            TextProvider(&self.visible_text),
1159        );
1160
1161        // Get the ranges of the innermost pair of brackets.
1162        matches
1163            .filter_map(|mat| {
1164                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1165                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1166                Some((open.byte_range(), close.byte_range()))
1167            })
1168            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1169    }
1170
1171    fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
1172        // TODO: it would be nice to not allocate here.
1173        let old_text = self.text();
1174        let base_version = self.version();
1175        cx.background().spawn(async move {
1176            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
1177                .iter_all_changes()
1178                .map(|c| (c.tag(), c.value().len()))
1179                .collect::<Vec<_>>();
1180            Diff {
1181                base_version,
1182                new_text,
1183                changes,
1184            }
1185        })
1186    }
1187
1188    pub fn set_text_from_disk(&self, new_text: Arc<str>, cx: &mut ModelContext<Self>) -> Task<()> {
1189        cx.spawn(|this, mut cx| async move {
1190            let diff = this
1191                .read_with(&cx, |this, cx| this.diff(new_text, cx))
1192                .await;
1193
1194            this.update(&mut cx, |this, cx| {
1195                if this.apply_diff(diff, cx) {
1196                    this.saved_version = this.version.clone();
1197                }
1198            });
1199        })
1200    }
1201
1202    fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
1203        if self.version == diff.base_version {
1204            self.start_transaction(None).unwrap();
1205            let mut offset = 0;
1206            for (tag, len) in diff.changes {
1207                let range = offset..(offset + len);
1208                match tag {
1209                    ChangeTag::Equal => offset += len,
1210                    ChangeTag::Delete => self.edit(Some(range), "", cx),
1211                    ChangeTag::Insert => {
1212                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
1213                        offset += len;
1214                    }
1215                }
1216            }
1217            self.end_transaction(None, cx).unwrap();
1218            true
1219        } else {
1220            false
1221        }
1222    }
1223
1224    pub fn is_dirty(&self) -> bool {
1225        self.version > self.saved_version
1226            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1227    }
1228
1229    pub fn has_conflict(&self) -> bool {
1230        self.version > self.saved_version
1231            && self
1232                .file
1233                .as_ref()
1234                .map_or(false, |file| file.mtime() > self.saved_mtime)
1235    }
1236
1237    pub fn remote_id(&self) -> u64 {
1238        self.remote_id
1239    }
1240
1241    pub fn version(&self) -> clock::Global {
1242        self.version.clone()
1243    }
1244
1245    pub fn text_summary(&self) -> TextSummary {
1246        self.visible_text.summary()
1247    }
1248
1249    pub fn len(&self) -> usize {
1250        self.content().len()
1251    }
1252
1253    pub fn line_len(&self, row: u32) -> u32 {
1254        self.content().line_len(row)
1255    }
1256
1257    pub fn max_point(&self) -> Point {
1258        self.visible_text.max_point()
1259    }
1260
1261    pub fn row_count(&self) -> u32 {
1262        self.max_point().row + 1
1263    }
1264
1265    pub fn text(&self) -> String {
1266        self.text_for_range(0..self.len()).collect()
1267    }
1268
1269    pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
1270        self.content().text_for_range(range)
1271    }
1272
1273    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1274        self.chars_at(0)
1275    }
1276
1277    pub fn chars_at<'a, T: 'a + ToOffset>(
1278        &'a self,
1279        position: T,
1280    ) -> impl Iterator<Item = char> + 'a {
1281        self.content().chars_at(position)
1282    }
1283
1284    pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1285        self.text_for_range(range).flat_map(str::chars)
1286    }
1287
1288    pub fn bytes_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = u8> + '_ {
1289        let offset = position.to_offset(self);
1290        self.visible_text.bytes_at(offset)
1291    }
1292
1293    pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1294    where
1295        T: ToOffset,
1296    {
1297        let position = position.to_offset(self);
1298        position == self.clip_offset(position, Bias::Left)
1299            && self
1300                .bytes_at(position)
1301                .take(needle.len())
1302                .eq(needle.bytes())
1303    }
1304
1305    pub fn edits_since<'a>(&'a self, since: clock::Global) -> impl 'a + Iterator<Item = Edit> {
1306        let since_2 = since.clone();
1307        let cursor = if since == self.version {
1308            None
1309        } else {
1310            Some(self.fragments.filter(
1311                move |summary| summary.max_version.changed_since(&since_2),
1312                &None,
1313            ))
1314        };
1315
1316        Edits {
1317            visible_text: &self.visible_text,
1318            deleted_text: &self.deleted_text,
1319            cursor,
1320            undos: &self.undo_map,
1321            since,
1322            old_offset: 0,
1323            new_offset: 0,
1324            old_point: Point::zero(),
1325            new_point: Point::zero(),
1326        }
1327    }
1328
1329    pub fn deferred_ops_len(&self) -> usize {
1330        self.deferred_ops.len()
1331    }
1332
1333    pub fn start_transaction(&mut self, set_id: Option<SelectionSetId>) -> Result<()> {
1334        self.start_transaction_at(set_id, Instant::now())
1335    }
1336
1337    fn start_transaction_at(&mut self, set_id: Option<SelectionSetId>, now: Instant) -> Result<()> {
1338        let selections = if let Some(set_id) = set_id {
1339            let set = self
1340                .selections
1341                .get(&set_id)
1342                .ok_or_else(|| anyhow!("invalid selection set {:?}", set_id))?;
1343            Some((set_id, set.selections.clone()))
1344        } else {
1345            None
1346        };
1347        self.history
1348            .start_transaction(self.version.clone(), self.is_dirty(), selections, now);
1349        Ok(())
1350    }
1351
1352    pub fn end_transaction(
1353        &mut self,
1354        set_id: Option<SelectionSetId>,
1355        cx: &mut ModelContext<Self>,
1356    ) -> Result<()> {
1357        self.end_transaction_at(set_id, Instant::now(), cx)
1358    }
1359
1360    fn end_transaction_at(
1361        &mut self,
1362        set_id: Option<SelectionSetId>,
1363        now: Instant,
1364        cx: &mut ModelContext<Self>,
1365    ) -> Result<()> {
1366        let selections = if let Some(set_id) = set_id {
1367            let set = self
1368                .selections
1369                .get(&set_id)
1370                .ok_or_else(|| anyhow!("invalid selection set {:?}", set_id))?;
1371            Some((set_id, set.selections.clone()))
1372        } else {
1373            None
1374        };
1375
1376        if let Some(transaction) = self.history.end_transaction(selections, now) {
1377            let since = transaction.start.clone();
1378            let was_dirty = transaction.buffer_was_dirty;
1379            self.history.group();
1380
1381            cx.notify();
1382            if self.edits_since(since).next().is_some() {
1383                self.did_edit(was_dirty, cx);
1384                self.reparse(cx);
1385            }
1386        }
1387
1388        Ok(())
1389    }
1390
1391    pub fn edit<I, S, T>(&mut self, ranges_iter: I, new_text: T, cx: &mut ModelContext<Self>)
1392    where
1393        I: IntoIterator<Item = Range<S>>,
1394        S: ToOffset,
1395        T: Into<String>,
1396    {
1397        self.edit_internal(ranges_iter, new_text, false, cx)
1398    }
1399
1400    pub fn edit_with_autoindent<I, S, T>(
1401        &mut self,
1402        ranges_iter: I,
1403        new_text: T,
1404        cx: &mut ModelContext<Self>,
1405    ) where
1406        I: IntoIterator<Item = Range<S>>,
1407        S: ToOffset,
1408        T: Into<String>,
1409    {
1410        self.edit_internal(ranges_iter, new_text, true, cx)
1411    }
1412
1413    pub fn edit_internal<I, S, T>(
1414        &mut self,
1415        ranges_iter: I,
1416        new_text: T,
1417        autoindent: bool,
1418        cx: &mut ModelContext<Self>,
1419    ) where
1420        I: IntoIterator<Item = Range<S>>,
1421        S: ToOffset,
1422        T: Into<String>,
1423    {
1424        let new_text = new_text.into();
1425
1426        // Skip invalid ranges and coalesce contiguous ones.
1427        let mut ranges: Vec<Range<usize>> = Vec::new();
1428        for range in ranges_iter {
1429            let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
1430            if !new_text.is_empty() || !range.is_empty() {
1431                if let Some(prev_range) = ranges.last_mut() {
1432                    if prev_range.end >= range.start {
1433                        prev_range.end = cmp::max(prev_range.end, range.end);
1434                    } else {
1435                        ranges.push(range);
1436                    }
1437                } else {
1438                    ranges.push(range);
1439                }
1440            }
1441        }
1442        if ranges.is_empty() {
1443            return;
1444        }
1445
1446        let autoindent_request = if autoindent && self.language.is_some() {
1447            let before_edit = self.snapshot();
1448            let edited = self.content().anchor_set(ranges.iter().filter_map(|range| {
1449                let start = range.start.to_point(&*self);
1450                if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
1451                    None
1452                } else {
1453                    Some((range.start, Bias::Left))
1454                }
1455            }));
1456            Some((before_edit, edited))
1457        } else {
1458            None
1459        };
1460
1461        let first_newline_ix = new_text.find('\n');
1462        let new_text_len = new_text.len();
1463        let new_text = if new_text_len > 0 {
1464            Some(new_text)
1465        } else {
1466            None
1467        };
1468
1469        self.start_transaction_at(None, Instant::now()).unwrap();
1470        let timestamp = InsertionTimestamp {
1471            replica_id: self.replica_id,
1472            local: self.local_clock.tick().value,
1473            lamport: self.lamport_clock.tick().value,
1474        };
1475        let edit = self.apply_local_edit(&ranges, new_text, timestamp);
1476
1477        self.history.push(edit.clone());
1478        self.history.push_undo(edit.timestamp.local());
1479        self.last_edit = edit.timestamp.local();
1480        self.version.observe(edit.timestamp.local());
1481
1482        if let Some((before_edit, edited)) = autoindent_request {
1483            let mut inserted = None;
1484            if let Some(first_newline_ix) = first_newline_ix {
1485                inserted = Some(self.content().anchor_range_set(ranges.iter().map(|range| {
1486                    (range.start + first_newline_ix + 1, Bias::Left)
1487                        ..(range.start + new_text_len, Bias::Right)
1488                })));
1489            }
1490
1491            self.autoindent_requests.push_back(AutoindentRequest {
1492                before_edit,
1493                version_after_edit: self.version.clone(),
1494                edited,
1495                inserted,
1496            });
1497        }
1498
1499        self.end_transaction_at(None, Instant::now(), cx).unwrap();
1500        self.send_operation(Operation::Edit(edit), cx);
1501    }
1502
1503    fn did_edit(&self, was_dirty: bool, cx: &mut ModelContext<Self>) {
1504        cx.emit(Event::Edited);
1505        if !was_dirty {
1506            cx.emit(Event::Dirtied);
1507        }
1508    }
1509
1510    pub fn add_selection_set(
1511        &mut self,
1512        selections: impl Into<Arc<[Selection]>>,
1513        cx: &mut ModelContext<Self>,
1514    ) -> SelectionSetId {
1515        let selections = selections.into();
1516        let lamport_timestamp = self.lamport_clock.tick();
1517        self.selections.insert(
1518            lamport_timestamp,
1519            SelectionSet {
1520                selections: selections.clone(),
1521                active: false,
1522            },
1523        );
1524        cx.notify();
1525
1526        self.send_operation(
1527            Operation::UpdateSelections {
1528                set_id: lamport_timestamp,
1529                selections: Some(selections),
1530                lamport_timestamp,
1531            },
1532            cx,
1533        );
1534
1535        lamport_timestamp
1536    }
1537
1538    pub fn update_selection_set(
1539        &mut self,
1540        set_id: SelectionSetId,
1541        selections: impl Into<Arc<[Selection]>>,
1542        cx: &mut ModelContext<Self>,
1543    ) -> Result<()> {
1544        let selections = selections.into();
1545        let set = self
1546            .selections
1547            .get_mut(&set_id)
1548            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?;
1549        set.selections = selections.clone();
1550        let lamport_timestamp = self.lamport_clock.tick();
1551        cx.notify();
1552        self.send_operation(
1553            Operation::UpdateSelections {
1554                set_id,
1555                selections: Some(selections),
1556                lamport_timestamp,
1557            },
1558            cx,
1559        );
1560        Ok(())
1561    }
1562
1563    pub fn set_active_selection_set(
1564        &mut self,
1565        set_id: Option<SelectionSetId>,
1566        cx: &mut ModelContext<Self>,
1567    ) -> Result<()> {
1568        if let Some(set_id) = set_id {
1569            assert_eq!(set_id.replica_id, self.replica_id());
1570        }
1571
1572        for (id, set) in &mut self.selections {
1573            if id.replica_id == self.local_clock.replica_id {
1574                if Some(*id) == set_id {
1575                    set.active = true;
1576                } else {
1577                    set.active = false;
1578                }
1579            }
1580        }
1581
1582        let lamport_timestamp = self.lamport_clock.tick();
1583        self.send_operation(
1584            Operation::SetActiveSelections {
1585                set_id,
1586                lamport_timestamp,
1587            },
1588            cx,
1589        );
1590        Ok(())
1591    }
1592
1593    pub fn remove_selection_set(
1594        &mut self,
1595        set_id: SelectionSetId,
1596        cx: &mut ModelContext<Self>,
1597    ) -> Result<()> {
1598        self.selections
1599            .remove(&set_id)
1600            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?;
1601        let lamport_timestamp = self.lamport_clock.tick();
1602        cx.notify();
1603        self.send_operation(
1604            Operation::UpdateSelections {
1605                set_id,
1606                selections: None,
1607                lamport_timestamp,
1608            },
1609            cx,
1610        );
1611        Ok(())
1612    }
1613
1614    pub fn selection_set(&self, set_id: SelectionSetId) -> Result<&SelectionSet> {
1615        self.selections
1616            .get(&set_id)
1617            .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))
1618    }
1619
1620    pub fn selection_sets(&self) -> impl Iterator<Item = (&SelectionSetId, &SelectionSet)> {
1621        self.selections.iter()
1622    }
1623
1624    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1625        &mut self,
1626        ops: I,
1627        cx: &mut ModelContext<Self>,
1628    ) -> Result<()> {
1629        let was_dirty = self.is_dirty();
1630        let old_version = self.version.clone();
1631
1632        let mut deferred_ops = Vec::new();
1633        for op in ops {
1634            if self.can_apply_op(&op) {
1635                self.apply_op(op)?;
1636            } else {
1637                self.deferred_replicas.insert(op.replica_id());
1638                deferred_ops.push(op);
1639            }
1640        }
1641        self.deferred_ops.insert(deferred_ops);
1642        self.flush_deferred_ops()?;
1643
1644        cx.notify();
1645        if self.edits_since(old_version).next().is_some() {
1646            self.did_edit(was_dirty, cx);
1647            self.reparse(cx);
1648        }
1649
1650        Ok(())
1651    }
1652
1653    fn apply_op(&mut self, op: Operation) -> Result<()> {
1654        match op {
1655            Operation::Edit(edit) => {
1656                if !self.version.observed(edit.timestamp.local()) {
1657                    self.apply_remote_edit(
1658                        &edit.version,
1659                        &edit.ranges,
1660                        edit.new_text.as_deref(),
1661                        edit.timestamp,
1662                    );
1663                    self.version.observe(edit.timestamp.local());
1664                    self.history.push(edit);
1665                }
1666            }
1667            Operation::Undo {
1668                undo,
1669                lamport_timestamp,
1670            } => {
1671                if !self.version.observed(undo.id) {
1672                    self.apply_undo(&undo)?;
1673                    self.version.observe(undo.id);
1674                    self.lamport_clock.observe(lamport_timestamp);
1675                }
1676            }
1677            Operation::UpdateSelections {
1678                set_id,
1679                selections,
1680                lamport_timestamp,
1681            } => {
1682                if let Some(selections) = selections {
1683                    if let Some(set) = self.selections.get_mut(&set_id) {
1684                        set.selections = selections;
1685                    } else {
1686                        self.selections.insert(
1687                            set_id,
1688                            SelectionSet {
1689                                selections,
1690                                active: false,
1691                            },
1692                        );
1693                    }
1694                } else {
1695                    self.selections.remove(&set_id);
1696                }
1697                self.lamport_clock.observe(lamport_timestamp);
1698            }
1699            Operation::SetActiveSelections {
1700                set_id,
1701                lamport_timestamp,
1702            } => {
1703                for (id, set) in &mut self.selections {
1704                    if id.replica_id == lamport_timestamp.replica_id {
1705                        if Some(*id) == set_id {
1706                            set.active = true;
1707                        } else {
1708                            set.active = false;
1709                        }
1710                    }
1711                }
1712                self.lamport_clock.observe(lamport_timestamp);
1713            }
1714            #[cfg(test)]
1715            Operation::Test(_) => {}
1716        }
1717        Ok(())
1718    }
1719
1720    fn apply_remote_edit(
1721        &mut self,
1722        version: &clock::Global,
1723        ranges: &[Range<usize>],
1724        new_text: Option<&str>,
1725        timestamp: InsertionTimestamp,
1726    ) {
1727        if ranges.is_empty() {
1728            return;
1729        }
1730
1731        let cx = Some(version.clone());
1732        let mut new_ropes =
1733            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1734        let mut old_fragments = self.fragments.cursor::<VersionedOffset>();
1735        let mut new_fragments =
1736            old_fragments.slice(&VersionedOffset::Offset(ranges[0].start), Bias::Left, &cx);
1737        new_ropes.push_tree(new_fragments.summary().text);
1738
1739        let mut fragment_start = old_fragments.start().offset();
1740        for range in ranges {
1741            let fragment_end = old_fragments.end(&cx).offset();
1742
1743            // If the current fragment ends before this range, then jump ahead to the first fragment
1744            // that extends past the start of this range, reusing any intervening fragments.
1745            if fragment_end < range.start {
1746                // If the current fragment has been partially consumed, then consume the rest of it
1747                // and advance to the next fragment before slicing.
1748                if fragment_start > old_fragments.start().offset() {
1749                    if fragment_end > fragment_start {
1750                        let mut suffix = old_fragments.item().unwrap().clone();
1751                        suffix.len = fragment_end - fragment_start;
1752                        new_ropes.push_fragment(&suffix, suffix.visible);
1753                        new_fragments.push(suffix, &None);
1754                    }
1755                    old_fragments.next(&cx);
1756                }
1757
1758                let slice =
1759                    old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Left, &cx);
1760                new_ropes.push_tree(slice.summary().text);
1761                new_fragments.push_tree(slice, &None);
1762                fragment_start = old_fragments.start().offset();
1763            }
1764
1765            // If we are at the end of a non-concurrent fragment, advance to the next one.
1766            let fragment_end = old_fragments.end(&cx).offset();
1767            if fragment_end == range.start && fragment_end > fragment_start {
1768                let mut fragment = old_fragments.item().unwrap().clone();
1769                fragment.len = fragment_end - fragment_start;
1770                new_ropes.push_fragment(&fragment, fragment.visible);
1771                new_fragments.push(fragment, &None);
1772                old_fragments.next(&cx);
1773                fragment_start = old_fragments.start().offset();
1774            }
1775
1776            // Skip over insertions that are concurrent to this edit, but have a lower lamport
1777            // timestamp.
1778            while let Some(fragment) = old_fragments.item() {
1779                if fragment_start == range.start
1780                    && fragment.timestamp.lamport() > timestamp.lamport()
1781                {
1782                    new_ropes.push_fragment(fragment, fragment.visible);
1783                    new_fragments.push(fragment.clone(), &None);
1784                    old_fragments.next(&cx);
1785                    debug_assert_eq!(fragment_start, range.start);
1786                } else {
1787                    break;
1788                }
1789            }
1790            debug_assert!(fragment_start <= range.start);
1791
1792            // Preserve any portion of the current fragment that precedes this range.
1793            if fragment_start < range.start {
1794                let mut prefix = old_fragments.item().unwrap().clone();
1795                prefix.len = range.start - fragment_start;
1796                fragment_start = range.start;
1797                new_ropes.push_fragment(&prefix, prefix.visible);
1798                new_fragments.push(prefix, &None);
1799            }
1800
1801            // Insert the new text before any existing fragments within the range.
1802            if let Some(new_text) = new_text {
1803                new_ropes.push_str(new_text);
1804                new_fragments.push(
1805                    Fragment {
1806                        timestamp,
1807                        len: new_text.len(),
1808                        deletions: Default::default(),
1809                        max_undos: Default::default(),
1810                        visible: true,
1811                    },
1812                    &None,
1813                );
1814            }
1815
1816            // Advance through every fragment that intersects this range, marking the intersecting
1817            // portions as deleted.
1818            while fragment_start < range.end {
1819                let fragment = old_fragments.item().unwrap();
1820                let fragment_end = old_fragments.end(&cx).offset();
1821                let mut intersection = fragment.clone();
1822                let intersection_end = cmp::min(range.end, fragment_end);
1823                if fragment.was_visible(version, &self.undo_map) {
1824                    intersection.len = intersection_end - fragment_start;
1825                    intersection.deletions.insert(timestamp.local());
1826                    intersection.visible = false;
1827                }
1828                if intersection.len > 0 {
1829                    new_ropes.push_fragment(&intersection, fragment.visible);
1830                    new_fragments.push(intersection, &None);
1831                    fragment_start = intersection_end;
1832                }
1833                if fragment_end <= range.end {
1834                    old_fragments.next(&cx);
1835                }
1836            }
1837        }
1838
1839        // If the current fragment has been partially consumed, then consume the rest of it
1840        // and advance to the next fragment before slicing.
1841        if fragment_start > old_fragments.start().offset() {
1842            let fragment_end = old_fragments.end(&cx).offset();
1843            if fragment_end > fragment_start {
1844                let mut suffix = old_fragments.item().unwrap().clone();
1845                suffix.len = fragment_end - fragment_start;
1846                new_ropes.push_fragment(&suffix, suffix.visible);
1847                new_fragments.push(suffix, &None);
1848            }
1849            old_fragments.next(&cx);
1850        }
1851
1852        let suffix = old_fragments.suffix(&cx);
1853        new_ropes.push_tree(suffix.summary().text);
1854        new_fragments.push_tree(suffix, &None);
1855        let (visible_text, deleted_text) = new_ropes.finish();
1856        drop(old_fragments);
1857
1858        self.fragments = new_fragments;
1859        self.visible_text = visible_text;
1860        self.deleted_text = deleted_text;
1861        self.local_clock.observe(timestamp.local());
1862        self.lamport_clock.observe(timestamp.lamport());
1863    }
1864
1865    #[cfg(not(test))]
1866    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1867        if let Some(file) = &self.file {
1868            file.buffer_updated(self.remote_id, operation, cx.as_mut());
1869        }
1870    }
1871
1872    #[cfg(test)]
1873    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
1874        self.operations.push(operation);
1875    }
1876
1877    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1878        self.selections
1879            .retain(|set_id, _| set_id.replica_id != replica_id);
1880        cx.notify();
1881    }
1882
1883    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
1884        let was_dirty = self.is_dirty();
1885        let old_version = self.version.clone();
1886
1887        if let Some(transaction) = self.history.pop_undo().cloned() {
1888            let selections = transaction.selections_before.clone();
1889            self.undo_or_redo(transaction, cx).unwrap();
1890            if let Some((set_id, selections)) = selections {
1891                let _ = self.update_selection_set(set_id, selections, cx);
1892            }
1893        }
1894
1895        cx.notify();
1896        if self.edits_since(old_version).next().is_some() {
1897            self.did_edit(was_dirty, cx);
1898            self.reparse(cx);
1899        }
1900    }
1901
1902    pub fn redo(&mut self, cx: &mut ModelContext<Self>) {
1903        let was_dirty = self.is_dirty();
1904        let old_version = self.version.clone();
1905
1906        if let Some(transaction) = self.history.pop_redo().cloned() {
1907            let selections = transaction.selections_after.clone();
1908            self.undo_or_redo(transaction, cx).unwrap();
1909            if let Some((set_id, selections)) = selections {
1910                let _ = self.update_selection_set(set_id, selections, cx);
1911            }
1912        }
1913
1914        cx.notify();
1915        if self.edits_since(old_version).next().is_some() {
1916            self.did_edit(was_dirty, cx);
1917            self.reparse(cx);
1918        }
1919    }
1920
1921    fn undo_or_redo(
1922        &mut self,
1923        transaction: Transaction,
1924        cx: &mut ModelContext<Self>,
1925    ) -> Result<()> {
1926        let mut counts = HashMap::default();
1927        for edit_id in transaction.edits {
1928            counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1929        }
1930
1931        let undo = UndoOperation {
1932            id: self.local_clock.tick(),
1933            counts,
1934            ranges: transaction.ranges,
1935            version: transaction.start.clone(),
1936        };
1937        self.apply_undo(&undo)?;
1938        self.version.observe(undo.id);
1939
1940        let operation = Operation::Undo {
1941            undo,
1942            lamport_timestamp: self.lamport_clock.tick(),
1943        };
1944        self.send_operation(operation, cx);
1945
1946        Ok(())
1947    }
1948
1949    fn apply_undo(&mut self, undo: &UndoOperation) -> Result<()> {
1950        self.undo_map.insert(undo);
1951
1952        let mut cx = undo.version.clone();
1953        for edit_id in undo.counts.keys().copied() {
1954            cx.observe(edit_id);
1955        }
1956        let cx = Some(cx);
1957
1958        let mut old_fragments = self.fragments.cursor::<VersionedOffset>();
1959        let mut new_fragments = old_fragments.slice(
1960            &VersionedOffset::Offset(undo.ranges[0].start),
1961            Bias::Right,
1962            &cx,
1963        );
1964        let mut new_ropes =
1965            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
1966        new_ropes.push_tree(new_fragments.summary().text);
1967
1968        for range in &undo.ranges {
1969            let mut end_offset = old_fragments.end(&cx).offset();
1970
1971            if end_offset < range.start {
1972                let preceding_fragments =
1973                    old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Right, &cx);
1974                new_ropes.push_tree(preceding_fragments.summary().text);
1975                new_fragments.push_tree(preceding_fragments, &None);
1976            }
1977
1978            while end_offset <= range.end {
1979                if let Some(fragment) = old_fragments.item() {
1980                    let mut fragment = fragment.clone();
1981                    let fragment_was_visible = fragment.visible;
1982
1983                    if fragment.was_visible(&undo.version, &self.undo_map)
1984                        || undo.counts.contains_key(&fragment.timestamp.local())
1985                    {
1986                        fragment.visible = fragment.is_visible(&self.undo_map);
1987                        fragment.max_undos.observe(undo.id);
1988                    }
1989                    new_ropes.push_fragment(&fragment, fragment_was_visible);
1990                    new_fragments.push(fragment, &None);
1991
1992                    old_fragments.next(&cx);
1993                    if end_offset == old_fragments.end(&cx).offset() {
1994                        let unseen_fragments = old_fragments.slice(
1995                            &VersionedOffset::Offset(end_offset),
1996                            Bias::Right,
1997                            &cx,
1998                        );
1999                        new_ropes.push_tree(unseen_fragments.summary().text);
2000                        new_fragments.push_tree(unseen_fragments, &None);
2001                    }
2002                    end_offset = old_fragments.end(&cx).offset();
2003                } else {
2004                    break;
2005                }
2006            }
2007        }
2008
2009        let suffix = old_fragments.suffix(&cx);
2010        new_ropes.push_tree(suffix.summary().text);
2011        new_fragments.push_tree(suffix, &None);
2012
2013        drop(old_fragments);
2014        let (visible_text, deleted_text) = new_ropes.finish();
2015        self.fragments = new_fragments;
2016        self.visible_text = visible_text;
2017        self.deleted_text = deleted_text;
2018        Ok(())
2019    }
2020
2021    fn flush_deferred_ops(&mut self) -> Result<()> {
2022        self.deferred_replicas.clear();
2023        let mut deferred_ops = Vec::new();
2024        for op in self.deferred_ops.drain().cursor().cloned() {
2025            if self.can_apply_op(&op) {
2026                self.apply_op(op)?;
2027            } else {
2028                self.deferred_replicas.insert(op.replica_id());
2029                deferred_ops.push(op);
2030            }
2031        }
2032        self.deferred_ops.insert(deferred_ops);
2033        Ok(())
2034    }
2035
2036    fn can_apply_op(&self, op: &Operation) -> bool {
2037        if self.deferred_replicas.contains(&op.replica_id()) {
2038            false
2039        } else {
2040            match op {
2041                Operation::Edit(edit) => self.version >= edit.version,
2042                Operation::Undo { undo, .. } => self.version >= undo.version,
2043                Operation::UpdateSelections { selections, .. } => {
2044                    if let Some(selections) = selections {
2045                        selections.iter().all(|selection| {
2046                            let contains_start = self.version >= selection.start.version;
2047                            let contains_end = self.version >= selection.end.version;
2048                            contains_start && contains_end
2049                        })
2050                    } else {
2051                        true
2052                    }
2053                }
2054                Operation::SetActiveSelections { set_id, .. } => {
2055                    set_id.map_or(true, |set_id| self.selections.contains_key(&set_id))
2056                }
2057                #[cfg(test)]
2058                Operation::Test(_) => true,
2059            }
2060        }
2061    }
2062
2063    fn apply_local_edit(
2064        &mut self,
2065        ranges: &[Range<usize>],
2066        new_text: Option<String>,
2067        timestamp: InsertionTimestamp,
2068    ) -> EditOperation {
2069        let mut edit = EditOperation {
2070            timestamp,
2071            version: self.version(),
2072            ranges: Vec::with_capacity(ranges.len()),
2073            new_text: None,
2074        };
2075
2076        let mut new_ropes =
2077            RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
2078        let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>();
2079        let mut new_fragments = old_fragments.slice(&ranges[0].start, Bias::Right, &None);
2080        new_ropes.push_tree(new_fragments.summary().text);
2081
2082        let mut fragment_start = old_fragments.start().visible;
2083        for range in ranges {
2084            let fragment_end = old_fragments.end(&None).visible;
2085
2086            // If the current fragment ends before this range, then jump ahead to the first fragment
2087            // that extends past the start of this range, reusing any intervening fragments.
2088            if fragment_end < range.start {
2089                // If the current fragment has been partially consumed, then consume the rest of it
2090                // and advance to the next fragment before slicing.
2091                if fragment_start > old_fragments.start().visible {
2092                    if fragment_end > fragment_start {
2093                        let mut suffix = old_fragments.item().unwrap().clone();
2094                        suffix.len = fragment_end - fragment_start;
2095                        new_ropes.push_fragment(&suffix, suffix.visible);
2096                        new_fragments.push(suffix, &None);
2097                    }
2098                    old_fragments.next(&None);
2099                }
2100
2101                let slice = old_fragments.slice(&range.start, Bias::Right, &None);
2102                new_ropes.push_tree(slice.summary().text);
2103                new_fragments.push_tree(slice, &None);
2104                fragment_start = old_fragments.start().visible;
2105            }
2106
2107            let full_range_start = range.start + old_fragments.start().deleted;
2108
2109            // Preserve any portion of the current fragment that precedes this range.
2110            if fragment_start < range.start {
2111                let mut prefix = old_fragments.item().unwrap().clone();
2112                prefix.len = range.start - fragment_start;
2113                new_ropes.push_fragment(&prefix, prefix.visible);
2114                new_fragments.push(prefix, &None);
2115                fragment_start = range.start;
2116            }
2117
2118            // Insert the new text before any existing fragments within the range.
2119            if let Some(new_text) = new_text.as_deref() {
2120                new_ropes.push_str(new_text);
2121                new_fragments.push(
2122                    Fragment {
2123                        timestamp,
2124                        len: new_text.len(),
2125                        deletions: Default::default(),
2126                        max_undos: Default::default(),
2127                        visible: true,
2128                    },
2129                    &None,
2130                );
2131            }
2132
2133            // Advance through every fragment that intersects this range, marking the intersecting
2134            // portions as deleted.
2135            while fragment_start < range.end {
2136                let fragment = old_fragments.item().unwrap();
2137                let fragment_end = old_fragments.end(&None).visible;
2138                let mut intersection = fragment.clone();
2139                let intersection_end = cmp::min(range.end, fragment_end);
2140                if fragment.visible {
2141                    intersection.len = intersection_end - fragment_start;
2142                    intersection.deletions.insert(timestamp.local());
2143                    intersection.visible = false;
2144                }
2145                if intersection.len > 0 {
2146                    new_ropes.push_fragment(&intersection, fragment.visible);
2147                    new_fragments.push(intersection, &None);
2148                    fragment_start = intersection_end;
2149                }
2150                if fragment_end <= range.end {
2151                    old_fragments.next(&None);
2152                }
2153            }
2154
2155            let full_range_end = range.end + old_fragments.start().deleted;
2156            edit.ranges.push(full_range_start..full_range_end);
2157        }
2158
2159        // If the current fragment has been partially consumed, then consume the rest of it
2160        // and advance to the next fragment before slicing.
2161        if fragment_start > old_fragments.start().visible {
2162            let fragment_end = old_fragments.end(&None).visible;
2163            if fragment_end > fragment_start {
2164                let mut suffix = old_fragments.item().unwrap().clone();
2165                suffix.len = fragment_end - fragment_start;
2166                new_ropes.push_fragment(&suffix, suffix.visible);
2167                new_fragments.push(suffix, &None);
2168            }
2169            old_fragments.next(&None);
2170        }
2171
2172        let suffix = old_fragments.suffix(&None);
2173        new_ropes.push_tree(suffix.summary().text);
2174        new_fragments.push_tree(suffix, &None);
2175        let (visible_text, deleted_text) = new_ropes.finish();
2176        drop(old_fragments);
2177
2178        self.fragments = new_fragments;
2179        self.visible_text = visible_text;
2180        self.deleted_text = deleted_text;
2181        edit.new_text = new_text;
2182        edit
2183    }
2184
2185    fn content<'a>(&'a self) -> Content<'a> {
2186        self.into()
2187    }
2188
2189    pub fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
2190        self.content().text_summary_for_range(range)
2191    }
2192
2193    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2194        self.anchor_at(position, Bias::Left)
2195    }
2196
2197    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2198        self.anchor_at(position, Bias::Right)
2199    }
2200
2201    pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
2202        self.content().anchor_at(position, bias)
2203    }
2204
2205    pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
2206        self.content().point_for_offset(offset)
2207    }
2208
2209    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
2210        self.visible_text.clip_point(point, bias)
2211    }
2212
2213    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
2214        self.visible_text.clip_offset(offset, bias)
2215    }
2216}
2217
2218#[cfg(any(test, feature = "test-support"))]
2219impl Buffer {
2220    fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
2221        let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
2222        let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
2223        start..end
2224    }
2225
2226    pub fn randomly_edit<T>(
2227        &mut self,
2228        rng: &mut T,
2229        old_range_count: usize,
2230        cx: &mut ModelContext<Self>,
2231    ) -> (Vec<Range<usize>>, String)
2232    where
2233        T: rand::Rng,
2234    {
2235        let mut old_ranges: Vec<Range<usize>> = Vec::new();
2236        for _ in 0..old_range_count {
2237            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
2238            if last_end > self.len() {
2239                break;
2240            }
2241            old_ranges.push(self.random_byte_range(last_end, rng));
2242        }
2243        let new_text_len = rng.gen_range(0..10);
2244        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
2245            .take(new_text_len)
2246            .collect();
2247        log::info!(
2248            "mutating buffer {} at {:?}: {:?}",
2249            self.replica_id,
2250            old_ranges,
2251            new_text
2252        );
2253        self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
2254        (old_ranges, new_text)
2255    }
2256
2257    pub fn randomly_mutate<T>(
2258        &mut self,
2259        rng: &mut T,
2260        cx: &mut ModelContext<Self>,
2261    ) -> (Vec<Range<usize>>, String)
2262    where
2263        T: rand::Rng,
2264    {
2265        use rand::prelude::*;
2266
2267        let (old_ranges, new_text) = self.randomly_edit(rng, 5, cx);
2268
2269        // Randomly add, remove or mutate selection sets.
2270        let replica_selection_sets = &self
2271            .selection_sets()
2272            .map(|(set_id, _)| *set_id)
2273            .filter(|set_id| self.replica_id == set_id.replica_id)
2274            .collect::<Vec<_>>();
2275        let set_id = replica_selection_sets.choose(rng);
2276        if set_id.is_some() && rng.gen_bool(1.0 / 6.0) {
2277            self.remove_selection_set(*set_id.unwrap(), cx).unwrap();
2278        } else {
2279            let mut ranges = Vec::new();
2280            for _ in 0..5 {
2281                ranges.push(self.random_byte_range(0, rng));
2282            }
2283            let new_selections = self.selections_from_ranges(ranges).unwrap();
2284
2285            if set_id.is_none() || rng.gen_bool(1.0 / 5.0) {
2286                self.add_selection_set(new_selections, cx);
2287            } else {
2288                self.update_selection_set(*set_id.unwrap(), new_selections, cx)
2289                    .unwrap();
2290            }
2291        }
2292
2293        (old_ranges, new_text)
2294    }
2295
2296    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
2297        use rand::prelude::*;
2298
2299        for _ in 0..rng.gen_range(1..=5) {
2300            if let Some(transaction) = self.history.undo_stack.choose(rng).cloned() {
2301                log::info!(
2302                    "undoing buffer {} transaction {:?}",
2303                    self.replica_id,
2304                    transaction
2305                );
2306                self.undo_or_redo(transaction, cx).unwrap();
2307            }
2308        }
2309    }
2310
2311    fn selections_from_ranges<I>(&self, ranges: I) -> Result<Vec<Selection>>
2312    where
2313        I: IntoIterator<Item = Range<usize>>,
2314    {
2315        use std::sync::atomic::{self, AtomicUsize};
2316
2317        static NEXT_SELECTION_ID: AtomicUsize = AtomicUsize::new(0);
2318
2319        let mut ranges = ranges.into_iter().collect::<Vec<_>>();
2320        ranges.sort_unstable_by_key(|range| range.start);
2321
2322        let mut selections = Vec::with_capacity(ranges.len());
2323        for range in ranges {
2324            if range.start > range.end {
2325                selections.push(Selection {
2326                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2327                    start: self.anchor_before(range.end),
2328                    end: self.anchor_before(range.start),
2329                    reversed: true,
2330                    goal: SelectionGoal::None,
2331                });
2332            } else {
2333                selections.push(Selection {
2334                    id: NEXT_SELECTION_ID.fetch_add(1, atomic::Ordering::SeqCst),
2335                    start: self.anchor_after(range.start),
2336                    end: self.anchor_before(range.end),
2337                    reversed: false,
2338                    goal: SelectionGoal::None,
2339                });
2340            }
2341        }
2342        Ok(selections)
2343    }
2344
2345    pub fn selection_ranges<'a>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<usize>>> {
2346        Ok(self
2347            .selection_set(set_id)?
2348            .selections
2349            .iter()
2350            .map(move |selection| {
2351                let start = selection.start.to_offset(self);
2352                let end = selection.end.to_offset(self);
2353                if selection.reversed {
2354                    end..start
2355                } else {
2356                    start..end
2357                }
2358            })
2359            .collect())
2360    }
2361
2362    pub fn all_selection_ranges<'a>(
2363        &'a self,
2364    ) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)> {
2365        self.selections
2366            .keys()
2367            .map(move |set_id| (*set_id, self.selection_ranges(*set_id).unwrap()))
2368    }
2369
2370    pub fn enclosing_bracket_point_ranges<T: ToOffset>(
2371        &self,
2372        range: Range<T>,
2373    ) -> Option<(Range<Point>, Range<Point>)> {
2374        self.enclosing_bracket_ranges(range).map(|(start, end)| {
2375            let point_start = start.start.to_point(self)..start.end.to_point(self);
2376            let point_end = end.start.to_point(self)..end.end.to_point(self);
2377            (point_start, point_end)
2378        })
2379    }
2380}
2381
2382impl Clone for Buffer {
2383    fn clone(&self) -> Self {
2384        Self {
2385            fragments: self.fragments.clone(),
2386            visible_text: self.visible_text.clone(),
2387            deleted_text: self.deleted_text.clone(),
2388            version: self.version.clone(),
2389            saved_version: self.saved_version.clone(),
2390            saved_mtime: self.saved_mtime,
2391            last_edit: self.last_edit.clone(),
2392            undo_map: self.undo_map.clone(),
2393            history: self.history.clone(),
2394            selections: self.selections.clone(),
2395            deferred_ops: self.deferred_ops.clone(),
2396            file: self.file.as_ref().map(|f| f.boxed_clone()),
2397            language: self.language.clone(),
2398            syntax_tree: Mutex::new(self.syntax_tree.lock().clone()),
2399            parsing_in_background: false,
2400            sync_parse_timeout: self.sync_parse_timeout,
2401            parse_count: self.parse_count,
2402            autoindent_requests: Default::default(),
2403            deferred_replicas: self.deferred_replicas.clone(),
2404            replica_id: self.replica_id,
2405            remote_id: self.remote_id.clone(),
2406            local_clock: self.local_clock.clone(),
2407            lamport_clock: self.lamport_clock.clone(),
2408
2409            #[cfg(test)]
2410            operations: self.operations.clone(),
2411        }
2412    }
2413}
2414
2415pub struct Snapshot {
2416    visible_text: Rope,
2417    fragments: SumTree<Fragment>,
2418    version: clock::Global,
2419    tree: Option<Tree>,
2420    is_parsing: bool,
2421    language: Option<Arc<Language>>,
2422    query_cursor: QueryCursorHandle,
2423}
2424
2425impl Clone for Snapshot {
2426    fn clone(&self) -> Self {
2427        Self {
2428            visible_text: self.visible_text.clone(),
2429            fragments: self.fragments.clone(),
2430            version: self.version.clone(),
2431            tree: self.tree.clone(),
2432            is_parsing: self.is_parsing,
2433            language: self.language.clone(),
2434            query_cursor: QueryCursorHandle::new(),
2435        }
2436    }
2437}
2438
2439impl Snapshot {
2440    pub fn len(&self) -> usize {
2441        self.visible_text.len()
2442    }
2443
2444    pub fn line_len(&self, row: u32) -> u32 {
2445        self.content().line_len(row)
2446    }
2447
2448    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2449        self.content().indent_column_for_line(row)
2450    }
2451
2452    pub fn text(&self) -> Rope {
2453        self.visible_text.clone()
2454    }
2455
2456    pub fn text_summary(&self) -> TextSummary {
2457        self.visible_text.summary()
2458    }
2459
2460    pub fn max_point(&self) -> Point {
2461        self.visible_text.max_point()
2462    }
2463
2464    pub fn text_for_range(&self, range: Range<usize>) -> Chunks {
2465        self.visible_text.chunks_in_range(range)
2466    }
2467
2468    pub fn highlighted_text_for_range(&mut self, range: Range<usize>) -> HighlightedChunks {
2469        let chunks = self.visible_text.chunks_in_range(range.clone());
2470        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
2471            let captures = self.query_cursor.set_byte_range(range.clone()).captures(
2472                &language.highlights_query,
2473                tree.root_node(),
2474                TextProvider(&self.visible_text),
2475            );
2476
2477            HighlightedChunks {
2478                range,
2479                chunks,
2480                highlights: Some(Highlights {
2481                    captures,
2482                    next_capture: None,
2483                    stack: Default::default(),
2484                    highlight_map: language.highlight_map(),
2485                }),
2486            }
2487        } else {
2488            HighlightedChunks {
2489                range,
2490                chunks,
2491                highlights: None,
2492            }
2493        }
2494    }
2495
2496    pub fn text_summary_for_range<T>(&self, range: Range<T>) -> TextSummary
2497    where
2498        T: ToOffset,
2499    {
2500        let range = range.start.to_offset(self.content())..range.end.to_offset(self.content());
2501        self.content().text_summary_for_range(range)
2502    }
2503
2504    pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
2505        self.content().point_for_offset(offset)
2506    }
2507
2508    pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
2509        self.visible_text.clip_offset(offset, bias)
2510    }
2511
2512    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
2513        self.visible_text.clip_point(point, bias)
2514    }
2515
2516    pub fn to_offset(&self, point: Point) -> usize {
2517        self.visible_text.to_offset(point)
2518    }
2519
2520    pub fn to_point(&self, offset: usize) -> Point {
2521        self.visible_text.to_point(offset)
2522    }
2523
2524    pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
2525        self.content().anchor_at(position, Bias::Left)
2526    }
2527
2528    pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
2529        self.content().anchor_at(position, Bias::Right)
2530    }
2531
2532    fn content(&self) -> Content {
2533        self.into()
2534    }
2535}
2536
2537pub struct Content<'a> {
2538    visible_text: &'a Rope,
2539    fragments: &'a SumTree<Fragment>,
2540    version: &'a clock::Global,
2541}
2542
2543impl<'a> From<&'a Snapshot> for Content<'a> {
2544    fn from(snapshot: &'a Snapshot) -> Self {
2545        Self {
2546            visible_text: &snapshot.visible_text,
2547            fragments: &snapshot.fragments,
2548            version: &snapshot.version,
2549        }
2550    }
2551}
2552
2553impl<'a> From<&'a Buffer> for Content<'a> {
2554    fn from(buffer: &'a Buffer) -> Self {
2555        Self {
2556            visible_text: &buffer.visible_text,
2557            fragments: &buffer.fragments,
2558            version: &buffer.version,
2559        }
2560    }
2561}
2562
2563impl<'a> From<&'a mut Buffer> for Content<'a> {
2564    fn from(buffer: &'a mut Buffer) -> Self {
2565        Self {
2566            visible_text: &buffer.visible_text,
2567            fragments: &buffer.fragments,
2568            version: &buffer.version,
2569        }
2570    }
2571}
2572
2573impl<'a> From<&'a Content<'a>> for Content<'a> {
2574    fn from(content: &'a Content) -> Self {
2575        Self {
2576            visible_text: &content.visible_text,
2577            fragments: &content.fragments,
2578            version: &content.version,
2579        }
2580    }
2581}
2582
2583impl<'a> Content<'a> {
2584    fn max_point(&self) -> Point {
2585        self.visible_text.max_point()
2586    }
2587
2588    fn len(&self) -> usize {
2589        self.fragments.extent::<usize>(&None)
2590    }
2591
2592    pub fn chars_at<T: ToOffset>(&self, position: T) -> impl Iterator<Item = char> + 'a {
2593        let offset = position.to_offset(self);
2594        self.visible_text.chars_at(offset)
2595    }
2596
2597    pub fn text_for_range<T: ToOffset>(&self, range: Range<T>) -> Chunks<'a> {
2598        let start = range.start.to_offset(self);
2599        let end = range.end.to_offset(self);
2600        self.visible_text.chunks_in_range(start..end)
2601    }
2602
2603    fn line_len(&self, row: u32) -> u32 {
2604        let row_start_offset = Point::new(row, 0).to_offset(self);
2605        let row_end_offset = if row >= self.max_point().row {
2606            self.len()
2607        } else {
2608            Point::new(row + 1, 0).to_offset(self) - 1
2609        };
2610        (row_end_offset - row_start_offset) as u32
2611    }
2612
2613    pub fn indent_column_for_line(&self, row: u32) -> u32 {
2614        let mut result = 0;
2615        for c in self.chars_at(Point::new(row, 0)) {
2616            if c == ' ' {
2617                result += 1;
2618            } else {
2619                break;
2620            }
2621        }
2622        result
2623    }
2624
2625    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2626        while row > 0 {
2627            row -= 1;
2628            if !self.is_line_blank(row) {
2629                return Some(row);
2630            }
2631        }
2632        None
2633    }
2634
2635    fn is_line_blank(&self, row: u32) -> bool {
2636        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
2637            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
2638    }
2639
2640    fn summary_for_anchor(&self, anchor: &Anchor) -> TextSummary {
2641        let cx = Some(anchor.version.clone());
2642        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2643        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
2644        let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2645            anchor.offset - cursor.start().0.offset()
2646        } else {
2647            0
2648        };
2649        self.text_summary_for_range(0..cursor.start().1 + overshoot)
2650    }
2651
2652    fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
2653        self.visible_text.cursor(range.start).summary(range.end)
2654    }
2655
2656    fn summaries_for_anchors<T>(
2657        &self,
2658        map: &'a AnchorMap<T>,
2659    ) -> impl Iterator<Item = (TextSummary, &'a T)> {
2660        let cx = Some(map.version.clone());
2661        let mut summary = TextSummary::default();
2662        let mut rope_cursor = self.visible_text.cursor(0);
2663        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2664        map.entries.iter().map(move |((offset, bias), value)| {
2665            cursor.seek_forward(&VersionedOffset::Offset(*offset), *bias, &cx);
2666            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2667                offset - cursor.start().0.offset()
2668            } else {
2669                0
2670            };
2671            summary += rope_cursor.summary(cursor.start().1 + overshoot);
2672            (summary.clone(), value)
2673        })
2674    }
2675
2676    fn summaries_for_anchor_ranges<T>(
2677        &self,
2678        map: &'a AnchorRangeMap<T>,
2679    ) -> impl Iterator<Item = (Range<TextSummary>, &'a T)> {
2680        let cx = Some(map.version.clone());
2681        let mut summary = TextSummary::default();
2682        let mut rope_cursor = self.visible_text.cursor(0);
2683        let mut cursor = self.fragments.cursor::<(VersionedOffset, usize)>();
2684        map.entries.iter().map(move |(range, value)| {
2685            let Range {
2686                start: (start_offset, start_bias),
2687                end: (end_offset, end_bias),
2688            } = range;
2689
2690            cursor.seek_forward(&VersionedOffset::Offset(*start_offset), *start_bias, &cx);
2691            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2692                start_offset - cursor.start().0.offset()
2693            } else {
2694                0
2695            };
2696            summary += rope_cursor.summary(cursor.start().1 + overshoot);
2697            let start_summary = summary.clone();
2698
2699            cursor.seek_forward(&VersionedOffset::Offset(*end_offset), *end_bias, &cx);
2700            let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
2701                end_offset - cursor.start().0.offset()
2702            } else {
2703                0
2704            };
2705            summary += rope_cursor.summary(cursor.start().1 + overshoot);
2706            let end_summary = summary.clone();
2707
2708            (start_summary..end_summary, value)
2709        })
2710    }
2711
2712    fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
2713        let offset = position.to_offset(self);
2714        let max_offset = self.len();
2715        assert!(offset <= max_offset, "offset is out of range");
2716        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
2717        cursor.seek(&offset, bias, &None);
2718        Anchor {
2719            offset: offset + cursor.start().deleted,
2720            bias,
2721            version: self.version.clone(),
2722        }
2723    }
2724
2725    pub fn anchor_map<T, E>(&self, entries: E) -> AnchorMap<T>
2726    where
2727        E: IntoIterator<Item = ((usize, Bias), T)>,
2728    {
2729        let version = self.version.clone();
2730        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
2731        let entries = entries
2732            .into_iter()
2733            .map(|((offset, bias), value)| {
2734                cursor.seek_forward(&offset, bias, &None);
2735                let full_offset = cursor.start().deleted + offset;
2736                ((full_offset, bias), value)
2737            })
2738            .collect();
2739
2740        AnchorMap { version, entries }
2741    }
2742
2743    pub fn anchor_range_map<T, E>(&self, entries: E) -> AnchorRangeMap<T>
2744    where
2745        E: IntoIterator<Item = (Range<(usize, Bias)>, T)>,
2746    {
2747        let version = self.version.clone();
2748        let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
2749        let entries = entries
2750            .into_iter()
2751            .map(|(range, value)| {
2752                let Range {
2753                    start: (start_offset, start_bias),
2754                    end: (end_offset, end_bias),
2755                } = range;
2756                cursor.seek_forward(&start_offset, start_bias, &None);
2757                let full_start_offset = cursor.start().deleted + start_offset;
2758                cursor.seek_forward(&end_offset, end_bias, &None);
2759                let full_end_offset = cursor.start().deleted + end_offset;
2760                (
2761                    (full_start_offset, start_bias)..(full_end_offset, end_bias),
2762                    value,
2763                )
2764            })
2765            .collect();
2766
2767        AnchorRangeMap { version, entries }
2768    }
2769
2770    pub fn anchor_set<E>(&self, entries: E) -> AnchorSet
2771    where
2772        E: IntoIterator<Item = (usize, Bias)>,
2773    {
2774        AnchorSet(self.anchor_map(entries.into_iter().map(|range| (range, ()))))
2775    }
2776
2777    pub fn anchor_range_set<E>(&self, entries: E) -> AnchorRangeSet
2778    where
2779        E: IntoIterator<Item = Range<(usize, Bias)>>,
2780    {
2781        AnchorRangeSet(self.anchor_range_map(entries.into_iter().map(|range| (range, ()))))
2782    }
2783
2784    fn full_offset_for_anchor(&self, anchor: &Anchor) -> usize {
2785        let cx = Some(anchor.version.clone());
2786        let mut cursor = self
2787            .fragments
2788            .cursor::<(VersionedOffset, FragmentTextSummary)>();
2789        cursor.seek(&VersionedOffset::Offset(anchor.offset), anchor.bias, &cx);
2790        let overshoot = if cursor.item().is_some() {
2791            anchor.offset - cursor.start().0.offset()
2792        } else {
2793            0
2794        };
2795        let summary = cursor.start().1;
2796        summary.visible + summary.deleted + overshoot
2797    }
2798
2799    fn point_for_offset(&self, offset: usize) -> Result<Point> {
2800        if offset <= self.len() {
2801            Ok(self.text_summary_for_range(0..offset).lines)
2802        } else {
2803            Err(anyhow!("offset out of bounds"))
2804        }
2805    }
2806
2807    fn suggest_autoindents(
2808        &'a self,
2809        row_range: Range<u32>,
2810        tree: &Tree,
2811        language: &Language,
2812        cursor: &mut QueryCursor,
2813    ) -> impl Iterator<Item = IndentSuggestion> + 'a {
2814        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2815
2816        // Get the "indentation ranges" that intersect this row range.
2817        let indent_capture_ix = language.indents_query.capture_index_for_name("indent");
2818        let end_capture_ix = language.indents_query.capture_index_for_name("end");
2819        cursor.set_point_range(
2820            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).into()
2821                ..Point::new(row_range.end, 0).into(),
2822        );
2823        let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
2824        for mat in cursor.matches(
2825            &language.indents_query,
2826            tree.root_node(),
2827            TextProvider(&self.visible_text),
2828        ) {
2829            let mut node_kind = "";
2830            let mut start: Option<Point> = None;
2831            let mut end: Option<Point> = None;
2832            for capture in mat.captures {
2833                if Some(capture.index) == indent_capture_ix {
2834                    node_kind = capture.node.kind();
2835                    start.get_or_insert(capture.node.start_position().into());
2836                    end.get_or_insert(capture.node.end_position().into());
2837                } else if Some(capture.index) == end_capture_ix {
2838                    end = Some(capture.node.start_position().into());
2839                }
2840            }
2841
2842            if let Some((start, end)) = start.zip(end) {
2843                if start.row == end.row {
2844                    continue;
2845                }
2846
2847                let range = start..end;
2848                match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
2849                    Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
2850                    Ok(ix) => {
2851                        let prev_range = &mut indentation_ranges[ix];
2852                        prev_range.0.end = prev_range.0.end.max(range.end);
2853                    }
2854                }
2855            }
2856        }
2857
2858        eprintln!(
2859            "autoindent {:?}. ranges: {:?}",
2860            row_range, indentation_ranges
2861        );
2862
2863        let mut prev_row = prev_non_blank_row.unwrap_or(0);
2864        row_range.map(move |row| {
2865            let row_start = Point::new(row, self.indent_column_for_line(row));
2866
2867            eprintln!("  autoindent row: {:?}", row);
2868
2869            let mut indent_from_prev_row = false;
2870            let mut outdent_to_row = u32::MAX;
2871            for (range, node_kind) in &indentation_ranges {
2872                if range.start.row >= row {
2873                    break;
2874                }
2875
2876                if range.start.row == prev_row && range.end > row_start {
2877                    eprintln!("    indent because of {} {:?}", node_kind, range);
2878                    indent_from_prev_row = true;
2879                }
2880                if range.end.row >= prev_row && range.end <= row_start {
2881                    eprintln!("    outdent because of {} {:?}", node_kind, range);
2882                    outdent_to_row = outdent_to_row.min(range.start.row);
2883                }
2884            }
2885
2886            let suggestion = if outdent_to_row == prev_row {
2887                IndentSuggestion {
2888                    basis_row: prev_row,
2889                    indent: false,
2890                }
2891            } else if indent_from_prev_row {
2892                IndentSuggestion {
2893                    basis_row: prev_row,
2894                    indent: true,
2895                }
2896            } else if outdent_to_row < prev_row {
2897                IndentSuggestion {
2898                    basis_row: outdent_to_row,
2899                    indent: false,
2900                }
2901            } else {
2902                IndentSuggestion {
2903                    basis_row: prev_row,
2904                    indent: false,
2905                }
2906            };
2907
2908            prev_row = row;
2909            suggestion
2910        })
2911    }
2912}
2913
2914#[derive(Debug)]
2915struct IndentSuggestion {
2916    basis_row: u32,
2917    indent: bool,
2918}
2919
2920struct RopeBuilder<'a> {
2921    old_visible_cursor: rope::Cursor<'a>,
2922    old_deleted_cursor: rope::Cursor<'a>,
2923    new_visible: Rope,
2924    new_deleted: Rope,
2925}
2926
2927impl<'a> RopeBuilder<'a> {
2928    fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
2929        Self {
2930            old_visible_cursor,
2931            old_deleted_cursor,
2932            new_visible: Rope::new(),
2933            new_deleted: Rope::new(),
2934        }
2935    }
2936
2937    fn push_tree(&mut self, len: FragmentTextSummary) {
2938        self.push(len.visible, true, true);
2939        self.push(len.deleted, false, false);
2940    }
2941
2942    fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
2943        debug_assert!(fragment.len > 0);
2944        self.push(fragment.len, was_visible, fragment.visible)
2945    }
2946
2947    fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
2948        let text = if was_visible {
2949            self.old_visible_cursor
2950                .slice(self.old_visible_cursor.offset() + len)
2951        } else {
2952            self.old_deleted_cursor
2953                .slice(self.old_deleted_cursor.offset() + len)
2954        };
2955        if is_visible {
2956            self.new_visible.append(text);
2957        } else {
2958            self.new_deleted.append(text);
2959        }
2960    }
2961
2962    fn push_str(&mut self, text: &str) {
2963        self.new_visible.push(text);
2964    }
2965
2966    fn finish(mut self) -> (Rope, Rope) {
2967        self.new_visible.append(self.old_visible_cursor.suffix());
2968        self.new_deleted.append(self.old_deleted_cursor.suffix());
2969        (self.new_visible, self.new_deleted)
2970    }
2971}
2972
2973#[derive(Clone, Debug, Eq, PartialEq)]
2974pub enum Event {
2975    Edited,
2976    Dirtied,
2977    Saved,
2978    FileHandleChanged,
2979    Reloaded,
2980    Reparsed,
2981    Closed,
2982}
2983
2984impl Entity for Buffer {
2985    type Event = Event;
2986
2987    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
2988        if let Some(file) = self.file.as_ref() {
2989            file.buffer_removed(self.remote_id, cx);
2990        }
2991    }
2992}
2993
2994impl<'a, F: Fn(&FragmentSummary) -> bool> Iterator for Edits<'a, F> {
2995    type Item = Edit;
2996
2997    fn next(&mut self) -> Option<Self::Item> {
2998        let mut change: Option<Edit> = None;
2999        let cursor = self.cursor.as_mut()?;
3000
3001        while let Some(fragment) = cursor.item() {
3002            let bytes = cursor.start().visible - self.new_offset;
3003            let lines = self.visible_text.to_point(cursor.start().visible) - self.new_point;
3004            self.old_offset += bytes;
3005            self.old_point += &lines;
3006            self.new_offset += bytes;
3007            self.new_point += &lines;
3008
3009            if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
3010                let fragment_lines =
3011                    self.visible_text.to_point(self.new_offset + fragment.len) - self.new_point;
3012                if let Some(ref mut change) = change {
3013                    if change.new_bytes.end == self.new_offset {
3014                        change.new_bytes.end += fragment.len;
3015                    } else {
3016                        break;
3017                    }
3018                } else {
3019                    change = Some(Edit {
3020                        old_bytes: self.old_offset..self.old_offset,
3021                        new_bytes: self.new_offset..self.new_offset + fragment.len,
3022                        old_lines: self.old_point..self.old_point,
3023                    });
3024                }
3025
3026                self.new_offset += fragment.len;
3027                self.new_point += &fragment_lines;
3028            } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
3029                let deleted_start = cursor.start().deleted;
3030                let fragment_lines = self.deleted_text.to_point(deleted_start + fragment.len)
3031                    - self.deleted_text.to_point(deleted_start);
3032                if let Some(ref mut change) = change {
3033                    if change.new_bytes.end == self.new_offset {
3034                        change.old_bytes.end += fragment.len;
3035                        change.old_lines.end += &fragment_lines;
3036                    } else {
3037                        break;
3038                    }
3039                } else {
3040                    change = Some(Edit {
3041                        old_bytes: self.old_offset..self.old_offset + fragment.len,
3042                        new_bytes: self.new_offset..self.new_offset,
3043                        old_lines: self.old_point..self.old_point + &fragment_lines,
3044                    });
3045                }
3046
3047                self.old_offset += fragment.len;
3048                self.old_point += &fragment_lines;
3049            }
3050
3051            cursor.next(&None);
3052        }
3053
3054        change
3055    }
3056}
3057
3058struct ByteChunks<'a>(rope::Chunks<'a>);
3059
3060impl<'a> Iterator for ByteChunks<'a> {
3061    type Item = &'a [u8];
3062
3063    fn next(&mut self) -> Option<Self::Item> {
3064        self.0.next().map(str::as_bytes)
3065    }
3066}
3067
3068struct TextProvider<'a>(&'a Rope);
3069
3070impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
3071    type I = ByteChunks<'a>;
3072
3073    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
3074        ByteChunks(self.0.chunks_in_range(node.byte_range()))
3075    }
3076}
3077
3078struct Highlights<'a> {
3079    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
3080    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
3081    stack: Vec<(usize, HighlightId)>,
3082    highlight_map: HighlightMap,
3083}
3084
3085pub struct HighlightedChunks<'a> {
3086    range: Range<usize>,
3087    chunks: Chunks<'a>,
3088    highlights: Option<Highlights<'a>>,
3089}
3090
3091impl<'a> HighlightedChunks<'a> {
3092    pub fn seek(&mut self, offset: usize) {
3093        self.range.start = offset;
3094        self.chunks.seek(self.range.start);
3095        if let Some(highlights) = self.highlights.as_mut() {
3096            highlights
3097                .stack
3098                .retain(|(end_offset, _)| *end_offset > offset);
3099            if let Some((mat, capture_ix)) = &highlights.next_capture {
3100                let capture = mat.captures[*capture_ix as usize];
3101                if offset >= capture.node.start_byte() {
3102                    let next_capture_end = capture.node.end_byte();
3103                    if offset < next_capture_end {
3104                        highlights.stack.push((
3105                            next_capture_end,
3106                            highlights.highlight_map.get(capture.index),
3107                        ));
3108                    }
3109                    highlights.next_capture.take();
3110                }
3111            }
3112            highlights.captures.set_byte_range(self.range.clone());
3113        }
3114    }
3115
3116    pub fn offset(&self) -> usize {
3117        self.range.start
3118    }
3119}
3120
3121impl<'a> Iterator for HighlightedChunks<'a> {
3122    type Item = (&'a str, HighlightId);
3123
3124    fn next(&mut self) -> Option<Self::Item> {
3125        let mut next_capture_start = usize::MAX;
3126
3127        if let Some(highlights) = self.highlights.as_mut() {
3128            while let Some((parent_capture_end, _)) = highlights.stack.last() {
3129                if *parent_capture_end <= self.range.start {
3130                    highlights.stack.pop();
3131                } else {
3132                    break;
3133                }
3134            }
3135
3136            if highlights.next_capture.is_none() {
3137                highlights.next_capture = highlights.captures.next();
3138            }
3139
3140            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
3141                let capture = mat.captures[*capture_ix as usize];
3142                if self.range.start < capture.node.start_byte() {
3143                    next_capture_start = capture.node.start_byte();
3144                    break;
3145                } else {
3146                    let style_id = highlights.highlight_map.get(capture.index);
3147                    highlights.stack.push((capture.node.end_byte(), style_id));
3148                    highlights.next_capture = highlights.captures.next();
3149                }
3150            }
3151        }
3152
3153        if let Some(chunk) = self.chunks.peek() {
3154            let chunk_start = self.range.start;
3155            let mut chunk_end = (self.chunks.offset() + chunk.len()).min(next_capture_start);
3156            let mut style_id = HighlightId::default();
3157            if let Some((parent_capture_end, parent_style_id)) =
3158                self.highlights.as_ref().and_then(|h| h.stack.last())
3159            {
3160                chunk_end = chunk_end.min(*parent_capture_end);
3161                style_id = *parent_style_id;
3162            }
3163
3164            let slice =
3165                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
3166            self.range.start = chunk_end;
3167            if self.range.start == self.chunks.offset() + chunk.len() {
3168                self.chunks.next().unwrap();
3169            }
3170
3171            Some((slice, style_id))
3172        } else {
3173            None
3174        }
3175    }
3176}
3177
3178impl Fragment {
3179    fn is_visible(&self, undos: &UndoMap) -> bool {
3180        !undos.is_undone(self.timestamp.local())
3181            && self.deletions.iter().all(|d| undos.is_undone(*d))
3182    }
3183
3184    fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
3185        (version.observed(self.timestamp.local())
3186            && !undos.was_undone(self.timestamp.local(), version))
3187            && self
3188                .deletions
3189                .iter()
3190                .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
3191    }
3192}
3193
3194impl sum_tree::Item for Fragment {
3195    type Summary = FragmentSummary;
3196
3197    fn summary(&self) -> Self::Summary {
3198        let mut max_version = clock::Global::new();
3199        max_version.observe(self.timestamp.local());
3200        for deletion in &self.deletions {
3201            max_version.observe(*deletion);
3202        }
3203        max_version.join(&self.max_undos);
3204
3205        let mut min_insertion_version = clock::Global::new();
3206        min_insertion_version.observe(self.timestamp.local());
3207        let max_insertion_version = min_insertion_version.clone();
3208        if self.visible {
3209            FragmentSummary {
3210                text: FragmentTextSummary {
3211                    visible: self.len,
3212                    deleted: 0,
3213                },
3214                max_version,
3215                min_insertion_version,
3216                max_insertion_version,
3217            }
3218        } else {
3219            FragmentSummary {
3220                text: FragmentTextSummary {
3221                    visible: 0,
3222                    deleted: self.len,
3223                },
3224                max_version,
3225                min_insertion_version,
3226                max_insertion_version,
3227            }
3228        }
3229    }
3230}
3231
3232impl sum_tree::Summary for FragmentSummary {
3233    type Context = Option<clock::Global>;
3234
3235    fn add_summary(&mut self, other: &Self, _: &Self::Context) {
3236        self.text.visible += &other.text.visible;
3237        self.text.deleted += &other.text.deleted;
3238        self.max_version.join(&other.max_version);
3239        self.min_insertion_version
3240            .meet(&other.min_insertion_version);
3241        self.max_insertion_version
3242            .join(&other.max_insertion_version);
3243    }
3244}
3245
3246impl Default for FragmentSummary {
3247    fn default() -> Self {
3248        FragmentSummary {
3249            text: FragmentTextSummary::default(),
3250            max_version: clock::Global::new(),
3251            min_insertion_version: clock::Global::new(),
3252            max_insertion_version: clock::Global::new(),
3253        }
3254    }
3255}
3256
3257impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
3258    fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
3259        *self += summary.text.visible;
3260    }
3261}
3262
3263impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
3264    fn cmp(
3265        &self,
3266        cursor_location: &FragmentTextSummary,
3267        _: &Option<clock::Global>,
3268    ) -> cmp::Ordering {
3269        Ord::cmp(self, &cursor_location.visible)
3270    }
3271}
3272
3273#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3274enum VersionedOffset {
3275    Offset(usize),
3276    InvalidVersion,
3277}
3278
3279impl VersionedOffset {
3280    fn offset(&self) -> usize {
3281        if let Self::Offset(offset) = self {
3282            *offset
3283        } else {
3284            panic!("invalid version")
3285        }
3286    }
3287}
3288
3289impl Default for VersionedOffset {
3290    fn default() -> Self {
3291        Self::Offset(0)
3292    }
3293}
3294
3295impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedOffset {
3296    fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
3297        if let Self::Offset(offset) = self {
3298            let version = cx.as_ref().unwrap();
3299            if *version >= summary.max_insertion_version {
3300                *offset += summary.text.visible + summary.text.deleted;
3301            } else if !summary
3302                .min_insertion_version
3303                .iter()
3304                .all(|t| !version.observed(*t))
3305            {
3306                *self = Self::InvalidVersion;
3307            }
3308        }
3309    }
3310}
3311
3312impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedOffset {
3313    fn cmp(&self, other: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
3314        match (self, other) {
3315            (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
3316            (Self::Offset(_), Self::InvalidVersion) => cmp::Ordering::Less,
3317            (Self::InvalidVersion, _) => unreachable!(),
3318        }
3319    }
3320}
3321
3322impl Operation {
3323    fn replica_id(&self) -> ReplicaId {
3324        self.lamport_timestamp().replica_id
3325    }
3326
3327    fn lamport_timestamp(&self) -> clock::Lamport {
3328        match self {
3329            Operation::Edit(edit) => edit.timestamp.lamport(),
3330            Operation::Undo {
3331                lamport_timestamp, ..
3332            } => *lamport_timestamp,
3333            Operation::UpdateSelections {
3334                lamport_timestamp, ..
3335            } => *lamport_timestamp,
3336            Operation::SetActiveSelections {
3337                lamport_timestamp, ..
3338            } => *lamport_timestamp,
3339            #[cfg(test)]
3340            Operation::Test(lamport_timestamp) => *lamport_timestamp,
3341        }
3342    }
3343
3344    pub fn is_edit(&self) -> bool {
3345        match self {
3346            Operation::Edit { .. } => true,
3347            _ => false,
3348        }
3349    }
3350}
3351
3352impl<'a> Into<proto::Operation> for &'a Operation {
3353    fn into(self) -> proto::Operation {
3354        proto::Operation {
3355            variant: Some(match self {
3356                Operation::Edit(edit) => proto::operation::Variant::Edit(edit.into()),
3357                Operation::Undo {
3358                    undo,
3359                    lamport_timestamp,
3360                } => proto::operation::Variant::Undo(proto::operation::Undo {
3361                    replica_id: undo.id.replica_id as u32,
3362                    local_timestamp: undo.id.value,
3363                    lamport_timestamp: lamport_timestamp.value,
3364                    ranges: undo
3365                        .ranges
3366                        .iter()
3367                        .map(|r| proto::Range {
3368                            start: r.start as u64,
3369                            end: r.end as u64,
3370                        })
3371                        .collect(),
3372                    counts: undo
3373                        .counts
3374                        .iter()
3375                        .map(|(edit_id, count)| proto::operation::UndoCount {
3376                            replica_id: edit_id.replica_id as u32,
3377                            local_timestamp: edit_id.value,
3378                            count: *count,
3379                        })
3380                        .collect(),
3381                    version: From::from(&undo.version),
3382                }),
3383                Operation::UpdateSelections {
3384                    set_id,
3385                    selections,
3386                    lamport_timestamp,
3387                } => proto::operation::Variant::UpdateSelections(
3388                    proto::operation::UpdateSelections {
3389                        replica_id: set_id.replica_id as u32,
3390                        local_timestamp: set_id.value,
3391                        lamport_timestamp: lamport_timestamp.value,
3392                        set: selections.as_ref().map(|selections| proto::SelectionSet {
3393                            selections: selections.iter().map(Into::into).collect(),
3394                        }),
3395                    },
3396                ),
3397                Operation::SetActiveSelections {
3398                    set_id,
3399                    lamport_timestamp,
3400                } => proto::operation::Variant::SetActiveSelections(
3401                    proto::operation::SetActiveSelections {
3402                        replica_id: lamport_timestamp.replica_id as u32,
3403                        local_timestamp: set_id.map(|set_id| set_id.value),
3404                        lamport_timestamp: lamport_timestamp.value,
3405                    },
3406                ),
3407                #[cfg(test)]
3408                Operation::Test(_) => unimplemented!(),
3409            }),
3410        }
3411    }
3412}
3413
3414impl<'a> Into<proto::operation::Edit> for &'a EditOperation {
3415    fn into(self) -> proto::operation::Edit {
3416        let ranges = self
3417            .ranges
3418            .iter()
3419            .map(|range| proto::Range {
3420                start: range.start as u64,
3421                end: range.end as u64,
3422            })
3423            .collect();
3424        proto::operation::Edit {
3425            replica_id: self.timestamp.replica_id as u32,
3426            local_timestamp: self.timestamp.local,
3427            lamport_timestamp: self.timestamp.lamport,
3428            version: From::from(&self.version),
3429            ranges,
3430            new_text: self.new_text.clone(),
3431        }
3432    }
3433}
3434
3435impl<'a> Into<proto::Anchor> for &'a Anchor {
3436    fn into(self) -> proto::Anchor {
3437        proto::Anchor {
3438            version: (&self.version).into(),
3439            offset: self.offset as u64,
3440            bias: match self.bias {
3441                Bias::Left => proto::anchor::Bias::Left as i32,
3442                Bias::Right => proto::anchor::Bias::Right as i32,
3443            },
3444        }
3445    }
3446}
3447
3448impl<'a> Into<proto::Selection> for &'a Selection {
3449    fn into(self) -> proto::Selection {
3450        proto::Selection {
3451            id: self.id as u64,
3452            start: Some((&self.start).into()),
3453            end: Some((&self.end).into()),
3454            reversed: self.reversed,
3455        }
3456    }
3457}
3458
3459impl TryFrom<proto::Operation> for Operation {
3460    type Error = anyhow::Error;
3461
3462    fn try_from(message: proto::Operation) -> Result<Self, Self::Error> {
3463        Ok(
3464            match message
3465                .variant
3466                .ok_or_else(|| anyhow!("missing operation variant"))?
3467            {
3468                proto::operation::Variant::Edit(edit) => Operation::Edit(edit.into()),
3469                proto::operation::Variant::Undo(undo) => Operation::Undo {
3470                    lamport_timestamp: clock::Lamport {
3471                        replica_id: undo.replica_id as ReplicaId,
3472                        value: undo.lamport_timestamp,
3473                    },
3474                    undo: UndoOperation {
3475                        id: clock::Local {
3476                            replica_id: undo.replica_id as ReplicaId,
3477                            value: undo.local_timestamp,
3478                        },
3479                        counts: undo
3480                            .counts
3481                            .into_iter()
3482                            .map(|c| {
3483                                (
3484                                    clock::Local {
3485                                        replica_id: c.replica_id as ReplicaId,
3486                                        value: c.local_timestamp,
3487                                    },
3488                                    c.count,
3489                                )
3490                            })
3491                            .collect(),
3492                        ranges: undo
3493                            .ranges
3494                            .into_iter()
3495                            .map(|r| r.start as usize..r.end as usize)
3496                            .collect(),
3497                        version: undo.version.into(),
3498                    },
3499                },
3500                proto::operation::Variant::UpdateSelections(message) => {
3501                    let selections: Option<Vec<Selection>> = if let Some(set) = message.set {
3502                        Some(
3503                            set.selections
3504                                .into_iter()
3505                                .map(TryFrom::try_from)
3506                                .collect::<Result<_, _>>()?,
3507                        )
3508                    } else {
3509                        None
3510                    };
3511                    Operation::UpdateSelections {
3512                        set_id: clock::Lamport {
3513                            replica_id: message.replica_id as ReplicaId,
3514                            value: message.local_timestamp,
3515                        },
3516                        lamport_timestamp: clock::Lamport {
3517                            replica_id: message.replica_id as ReplicaId,
3518                            value: message.lamport_timestamp,
3519                        },
3520                        selections: selections.map(Arc::from),
3521                    }
3522                }
3523                proto::operation::Variant::SetActiveSelections(message) => {
3524                    Operation::SetActiveSelections {
3525                        set_id: message.local_timestamp.map(|value| clock::Lamport {
3526                            replica_id: message.replica_id as ReplicaId,
3527                            value,
3528                        }),
3529                        lamport_timestamp: clock::Lamport {
3530                            replica_id: message.replica_id as ReplicaId,
3531                            value: message.lamport_timestamp,
3532                        },
3533                    }
3534                }
3535            },
3536        )
3537    }
3538}
3539
3540impl From<proto::operation::Edit> for EditOperation {
3541    fn from(edit: proto::operation::Edit) -> Self {
3542        let ranges = edit
3543            .ranges
3544            .into_iter()
3545            .map(|range| range.start as usize..range.end as usize)
3546            .collect();
3547        EditOperation {
3548            timestamp: InsertionTimestamp {
3549                replica_id: edit.replica_id as ReplicaId,
3550                local: edit.local_timestamp,
3551                lamport: edit.lamport_timestamp,
3552            },
3553            version: edit.version.into(),
3554            ranges,
3555            new_text: edit.new_text,
3556        }
3557    }
3558}
3559
3560impl TryFrom<proto::Anchor> for Anchor {
3561    type Error = anyhow::Error;
3562
3563    fn try_from(message: proto::Anchor) -> Result<Self, Self::Error> {
3564        let mut version = clock::Global::new();
3565        for entry in message.version {
3566            version.observe(clock::Local {
3567                replica_id: entry.replica_id as ReplicaId,
3568                value: entry.timestamp,
3569            });
3570        }
3571
3572        Ok(Self {
3573            offset: message.offset as usize,
3574            bias: if message.bias == proto::anchor::Bias::Left as i32 {
3575                Bias::Left
3576            } else if message.bias == proto::anchor::Bias::Right as i32 {
3577                Bias::Right
3578            } else {
3579                Err(anyhow!("invalid anchor bias {}", message.bias))?
3580            },
3581            version,
3582        })
3583    }
3584}
3585
3586impl TryFrom<proto::Selection> for Selection {
3587    type Error = anyhow::Error;
3588
3589    fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> {
3590        Ok(Selection {
3591            id: selection.id as usize,
3592            start: selection
3593                .start
3594                .ok_or_else(|| anyhow!("missing selection start"))?
3595                .try_into()?,
3596            end: selection
3597                .end
3598                .ok_or_else(|| anyhow!("missing selection end"))?
3599                .try_into()?,
3600            reversed: selection.reversed,
3601            goal: SelectionGoal::None,
3602        })
3603    }
3604}
3605
3606pub trait ToOffset {
3607    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize;
3608}
3609
3610impl ToOffset for Point {
3611    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3612        content.into().visible_text.to_offset(*self)
3613    }
3614}
3615
3616impl ToOffset for usize {
3617    fn to_offset<'a>(&self, _: impl Into<Content<'a>>) -> usize {
3618        *self
3619    }
3620}
3621
3622impl ToOffset for Anchor {
3623    fn to_offset<'a>(&self, content: impl Into<Content<'a>>) -> usize {
3624        content.into().summary_for_anchor(self).bytes
3625    }
3626}
3627
3628impl<'a> ToOffset for &'a Anchor {
3629    fn to_offset<'b>(&self, content: impl Into<Content<'b>>) -> usize {
3630        content.into().summary_for_anchor(self).bytes
3631    }
3632}
3633
3634pub trait ToPoint {
3635    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point;
3636}
3637
3638impl ToPoint for Anchor {
3639    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3640        content.into().summary_for_anchor(self).lines
3641    }
3642}
3643
3644impl ToPoint for usize {
3645    fn to_point<'a>(&self, content: impl Into<Content<'a>>) -> Point {
3646        content.into().visible_text.to_point(*self)
3647    }
3648}
3649
3650fn contiguous_ranges(mut values: impl Iterator<Item = u32>) -> impl Iterator<Item = Range<u32>> {
3651    let mut current_range: Option<Range<u32>> = None;
3652    std::iter::from_fn(move || loop {
3653        if let Some(value) = values.next() {
3654            if let Some(range) = &mut current_range {
3655                if value == range.end {
3656                    range.end += 1;
3657                    continue;
3658                }
3659            }
3660
3661            let prev_range = current_range.clone();
3662            current_range = Some(value..(value + 1));
3663            if prev_range.is_some() {
3664                return prev_range;
3665            }
3666        } else {
3667            return current_range.take();
3668        }
3669    })
3670}
3671
3672#[cfg(test)]
3673mod tests {
3674    use crate::random_char_iter::RandomCharIter;
3675
3676    use super::*;
3677    use gpui::ModelHandle;
3678    use rand::prelude::*;
3679    use std::{cell::RefCell, cmp::Ordering, env, mem, rc::Rc};
3680
3681    #[gpui::test]
3682    fn test_edit(cx: &mut gpui::MutableAppContext) {
3683        cx.add_model(|cx| {
3684            let mut buffer = Buffer::new(0, "abc", cx);
3685            assert_eq!(buffer.text(), "abc");
3686            buffer.edit(vec![3..3], "def", cx);
3687            assert_eq!(buffer.text(), "abcdef");
3688            buffer.edit(vec![0..0], "ghi", cx);
3689            assert_eq!(buffer.text(), "ghiabcdef");
3690            buffer.edit(vec![5..5], "jkl", cx);
3691            assert_eq!(buffer.text(), "ghiabjklcdef");
3692            buffer.edit(vec![6..7], "", cx);
3693            assert_eq!(buffer.text(), "ghiabjlcdef");
3694            buffer.edit(vec![4..9], "mno", cx);
3695            assert_eq!(buffer.text(), "ghiamnoef");
3696            buffer
3697        });
3698    }
3699
3700    #[gpui::test]
3701    fn test_edit_events(cx: &mut gpui::MutableAppContext) {
3702        let mut now = Instant::now();
3703        let buffer_1_events = Rc::new(RefCell::new(Vec::new()));
3704        let buffer_2_events = Rc::new(RefCell::new(Vec::new()));
3705
3706        let buffer1 = cx.add_model(|cx| Buffer::new(0, "abcdef", cx));
3707        let buffer2 = cx.add_model(|cx| Buffer::new(1, "abcdef", cx));
3708        let buffer_ops = buffer1.update(cx, |buffer, cx| {
3709            let buffer_1_events = buffer_1_events.clone();
3710            cx.subscribe(&buffer1, move |_, _, event, _| {
3711                buffer_1_events.borrow_mut().push(event.clone())
3712            })
3713            .detach();
3714            let buffer_2_events = buffer_2_events.clone();
3715            cx.subscribe(&buffer2, move |_, _, event, _| {
3716                buffer_2_events.borrow_mut().push(event.clone())
3717            })
3718            .detach();
3719
3720            // An edit emits an edited event, followed by a dirtied event,
3721            // since the buffer was previously in a clean state.
3722            buffer.edit(Some(2..4), "XYZ", cx);
3723
3724            // An empty transaction does not emit any events.
3725            buffer.start_transaction(None).unwrap();
3726            buffer.end_transaction(None, cx).unwrap();
3727
3728            // A transaction containing two edits emits one edited event.
3729            now += Duration::from_secs(1);
3730            buffer.start_transaction_at(None, now).unwrap();
3731            buffer.edit(Some(5..5), "u", cx);
3732            buffer.edit(Some(6..6), "w", cx);
3733            buffer.end_transaction_at(None, now, cx).unwrap();
3734
3735            // Undoing a transaction emits one edited event.
3736            buffer.undo(cx);
3737
3738            buffer.operations.clone()
3739        });
3740
3741        // Incorporating a set of remote ops emits a single edited event,
3742        // followed by a dirtied event.
3743        buffer2.update(cx, |buffer, cx| {
3744            buffer.apply_ops(buffer_ops, cx).unwrap();
3745        });
3746
3747        let buffer_1_events = buffer_1_events.borrow();
3748        assert_eq!(
3749            *buffer_1_events,
3750            vec![Event::Edited, Event::Dirtied, Event::Edited, Event::Edited]
3751        );
3752
3753        let buffer_2_events = buffer_2_events.borrow();
3754        assert_eq!(*buffer_2_events, vec![Event::Edited, Event::Dirtied]);
3755    }
3756
3757    #[gpui::test(iterations = 100)]
3758    fn test_random_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
3759        let operations = env::var("OPERATIONS")
3760            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
3761            .unwrap_or(10);
3762
3763        let reference_string_len = rng.gen_range(0..3);
3764        let mut reference_string = RandomCharIter::new(&mut rng)
3765            .take(reference_string_len)
3766            .collect::<String>();
3767        cx.add_model(|cx| {
3768            let mut buffer = Buffer::new(0, reference_string.as_str(), cx);
3769            buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
3770            let mut buffer_versions = Vec::new();
3771            log::info!(
3772                "buffer text {:?}, version: {:?}",
3773                buffer.text(),
3774                buffer.version()
3775            );
3776
3777            for _i in 0..operations {
3778                let (old_ranges, new_text) = buffer.randomly_mutate(&mut rng, cx);
3779                for old_range in old_ranges.iter().rev() {
3780                    reference_string.replace_range(old_range.clone(), &new_text);
3781                }
3782                assert_eq!(buffer.text(), reference_string);
3783                log::info!(
3784                    "buffer text {:?}, version: {:?}",
3785                    buffer.text(),
3786                    buffer.version()
3787                );
3788
3789                if rng.gen_bool(0.25) {
3790                    buffer.randomly_undo_redo(&mut rng, cx);
3791                    reference_string = buffer.text();
3792                    log::info!(
3793                        "buffer text {:?}, version: {:?}",
3794                        buffer.text(),
3795                        buffer.version()
3796                    );
3797                }
3798
3799                let range = buffer.random_byte_range(0, &mut rng);
3800                assert_eq!(
3801                    buffer.text_summary_for_range(range.clone()),
3802                    TextSummary::from(&reference_string[range])
3803                );
3804
3805                if rng.gen_bool(0.3) {
3806                    buffer_versions.push(buffer.clone());
3807                }
3808            }
3809
3810            for mut old_buffer in buffer_versions {
3811                let edits = buffer
3812                    .edits_since(old_buffer.version.clone())
3813                    .collect::<Vec<_>>();
3814
3815                log::info!(
3816                    "mutating old buffer version {:?}, text: {:?}, edits since: {:?}",
3817                    old_buffer.version(),
3818                    old_buffer.text(),
3819                    edits,
3820                );
3821
3822                let mut delta = 0_isize;
3823                for edit in edits {
3824                    let old_start = (edit.old_bytes.start as isize + delta) as usize;
3825                    let new_text: String = buffer.text_for_range(edit.new_bytes.clone()).collect();
3826                    old_buffer.edit(
3827                        Some(old_start..old_start + edit.deleted_bytes()),
3828                        new_text,
3829                        cx,
3830                    );
3831                    delta += edit.delta();
3832                }
3833                assert_eq!(old_buffer.text(), buffer.text());
3834            }
3835
3836            buffer
3837        });
3838    }
3839
3840    #[gpui::test]
3841    fn test_line_len(cx: &mut gpui::MutableAppContext) {
3842        cx.add_model(|cx| {
3843            let mut buffer = Buffer::new(0, "", cx);
3844            buffer.edit(vec![0..0], "abcd\nefg\nhij", cx);
3845            buffer.edit(vec![12..12], "kl\nmno", cx);
3846            buffer.edit(vec![18..18], "\npqrs\n", cx);
3847            buffer.edit(vec![18..21], "\nPQ", cx);
3848
3849            assert_eq!(buffer.line_len(0), 4);
3850            assert_eq!(buffer.line_len(1), 3);
3851            assert_eq!(buffer.line_len(2), 5);
3852            assert_eq!(buffer.line_len(3), 3);
3853            assert_eq!(buffer.line_len(4), 4);
3854            assert_eq!(buffer.line_len(5), 0);
3855            buffer
3856        });
3857    }
3858
3859    #[gpui::test]
3860    fn test_text_summary_for_range(cx: &mut gpui::MutableAppContext) {
3861        cx.add_model(|cx| {
3862            let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz", cx);
3863            assert_eq!(
3864                buffer.text_summary_for_range(1..3),
3865                TextSummary {
3866                    bytes: 2,
3867                    lines: Point::new(1, 0),
3868                    first_line_chars: 1,
3869                    last_line_chars: 0,
3870                    longest_row: 0,
3871                    longest_row_chars: 1,
3872                }
3873            );
3874            assert_eq!(
3875                buffer.text_summary_for_range(1..12),
3876                TextSummary {
3877                    bytes: 11,
3878                    lines: Point::new(3, 0),
3879                    first_line_chars: 1,
3880                    last_line_chars: 0,
3881                    longest_row: 2,
3882                    longest_row_chars: 4,
3883                }
3884            );
3885            assert_eq!(
3886                buffer.text_summary_for_range(0..20),
3887                TextSummary {
3888                    bytes: 20,
3889                    lines: Point::new(4, 1),
3890                    first_line_chars: 2,
3891                    last_line_chars: 1,
3892                    longest_row: 3,
3893                    longest_row_chars: 6,
3894                }
3895            );
3896            assert_eq!(
3897                buffer.text_summary_for_range(0..22),
3898                TextSummary {
3899                    bytes: 22,
3900                    lines: Point::new(4, 3),
3901                    first_line_chars: 2,
3902                    last_line_chars: 3,
3903                    longest_row: 3,
3904                    longest_row_chars: 6,
3905                }
3906            );
3907            assert_eq!(
3908                buffer.text_summary_for_range(7..22),
3909                TextSummary {
3910                    bytes: 15,
3911                    lines: Point::new(2, 3),
3912                    first_line_chars: 4,
3913                    last_line_chars: 3,
3914                    longest_row: 1,
3915                    longest_row_chars: 6,
3916                }
3917            );
3918            buffer
3919        });
3920    }
3921
3922    #[gpui::test]
3923    fn test_chars_at(cx: &mut gpui::MutableAppContext) {
3924        cx.add_model(|cx| {
3925            let mut buffer = Buffer::new(0, "", cx);
3926            buffer.edit(vec![0..0], "abcd\nefgh\nij", cx);
3927            buffer.edit(vec![12..12], "kl\nmno", cx);
3928            buffer.edit(vec![18..18], "\npqrs", cx);
3929            buffer.edit(vec![18..21], "\nPQ", cx);
3930
3931            let chars = buffer.chars_at(Point::new(0, 0));
3932            assert_eq!(chars.collect::<String>(), "abcd\nefgh\nijkl\nmno\nPQrs");
3933
3934            let chars = buffer.chars_at(Point::new(1, 0));
3935            assert_eq!(chars.collect::<String>(), "efgh\nijkl\nmno\nPQrs");
3936
3937            let chars = buffer.chars_at(Point::new(2, 0));
3938            assert_eq!(chars.collect::<String>(), "ijkl\nmno\nPQrs");
3939
3940            let chars = buffer.chars_at(Point::new(3, 0));
3941            assert_eq!(chars.collect::<String>(), "mno\nPQrs");
3942
3943            let chars = buffer.chars_at(Point::new(4, 0));
3944            assert_eq!(chars.collect::<String>(), "PQrs");
3945
3946            // Regression test:
3947            let mut buffer = Buffer::new(0, "", cx);
3948            buffer.edit(vec![0..0], "[workspace]\nmembers = [\n    \"xray_core\",\n    \"xray_server\",\n    \"xray_cli\",\n    \"xray_wasm\",\n]\n", cx);
3949            buffer.edit(vec![60..60], "\n", cx);
3950
3951            let chars = buffer.chars_at(Point::new(6, 0));
3952            assert_eq!(chars.collect::<String>(), "    \"xray_wasm\",\n]\n");
3953
3954            buffer
3955        });
3956    }
3957
3958    #[gpui::test]
3959    fn test_anchors(cx: &mut gpui::MutableAppContext) {
3960        cx.add_model(|cx| {
3961            let mut buffer = Buffer::new(0, "", cx);
3962            buffer.edit(vec![0..0], "abc", cx);
3963            let left_anchor = buffer.anchor_before(2);
3964            let right_anchor = buffer.anchor_after(2);
3965
3966            buffer.edit(vec![1..1], "def\n", cx);
3967            assert_eq!(buffer.text(), "adef\nbc");
3968            assert_eq!(left_anchor.to_offset(&buffer), 6);
3969            assert_eq!(right_anchor.to_offset(&buffer), 6);
3970            assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
3971            assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
3972
3973            buffer.edit(vec![2..3], "", cx);
3974            assert_eq!(buffer.text(), "adf\nbc");
3975            assert_eq!(left_anchor.to_offset(&buffer), 5);
3976            assert_eq!(right_anchor.to_offset(&buffer), 5);
3977            assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
3978            assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 1 });
3979
3980            buffer.edit(vec![5..5], "ghi\n", cx);
3981            assert_eq!(buffer.text(), "adf\nbghi\nc");
3982            assert_eq!(left_anchor.to_offset(&buffer), 5);
3983            assert_eq!(right_anchor.to_offset(&buffer), 9);
3984            assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 });
3985            assert_eq!(right_anchor.to_point(&buffer), Point { row: 2, column: 0 });
3986
3987            buffer.edit(vec![7..9], "", cx);
3988            assert_eq!(buffer.text(), "adf\nbghc");
3989            assert_eq!(left_anchor.to_offset(&buffer), 5);
3990            assert_eq!(right_anchor.to_offset(&buffer), 7);
3991            assert_eq!(left_anchor.to_point(&buffer), Point { row: 1, column: 1 },);
3992            assert_eq!(right_anchor.to_point(&buffer), Point { row: 1, column: 3 });
3993
3994            // Ensure anchoring to a point is equivalent to anchoring to an offset.
3995            assert_eq!(
3996                buffer.anchor_before(Point { row: 0, column: 0 }),
3997                buffer.anchor_before(0)
3998            );
3999            assert_eq!(
4000                buffer.anchor_before(Point { row: 0, column: 1 }),
4001                buffer.anchor_before(1)
4002            );
4003            assert_eq!(
4004                buffer.anchor_before(Point { row: 0, column: 2 }),
4005                buffer.anchor_before(2)
4006            );
4007            assert_eq!(
4008                buffer.anchor_before(Point { row: 0, column: 3 }),
4009                buffer.anchor_before(3)
4010            );
4011            assert_eq!(
4012                buffer.anchor_before(Point { row: 1, column: 0 }),
4013                buffer.anchor_before(4)
4014            );
4015            assert_eq!(
4016                buffer.anchor_before(Point { row: 1, column: 1 }),
4017                buffer.anchor_before(5)
4018            );
4019            assert_eq!(
4020                buffer.anchor_before(Point { row: 1, column: 2 }),
4021                buffer.anchor_before(6)
4022            );
4023            assert_eq!(
4024                buffer.anchor_before(Point { row: 1, column: 3 }),
4025                buffer.anchor_before(7)
4026            );
4027            assert_eq!(
4028                buffer.anchor_before(Point { row: 1, column: 4 }),
4029                buffer.anchor_before(8)
4030            );
4031
4032            // Comparison between anchors.
4033            let anchor_at_offset_0 = buffer.anchor_before(0);
4034            let anchor_at_offset_1 = buffer.anchor_before(1);
4035            let anchor_at_offset_2 = buffer.anchor_before(2);
4036
4037            assert_eq!(
4038                anchor_at_offset_0
4039                    .cmp(&anchor_at_offset_0, &buffer)
4040                    .unwrap(),
4041                Ordering::Equal
4042            );
4043            assert_eq!(
4044                anchor_at_offset_1
4045                    .cmp(&anchor_at_offset_1, &buffer)
4046                    .unwrap(),
4047                Ordering::Equal
4048            );
4049            assert_eq!(
4050                anchor_at_offset_2
4051                    .cmp(&anchor_at_offset_2, &buffer)
4052                    .unwrap(),
4053                Ordering::Equal
4054            );
4055
4056            assert_eq!(
4057                anchor_at_offset_0
4058                    .cmp(&anchor_at_offset_1, &buffer)
4059                    .unwrap(),
4060                Ordering::Less
4061            );
4062            assert_eq!(
4063                anchor_at_offset_1
4064                    .cmp(&anchor_at_offset_2, &buffer)
4065                    .unwrap(),
4066                Ordering::Less
4067            );
4068            assert_eq!(
4069                anchor_at_offset_0
4070                    .cmp(&anchor_at_offset_2, &buffer)
4071                    .unwrap(),
4072                Ordering::Less
4073            );
4074
4075            assert_eq!(
4076                anchor_at_offset_1
4077                    .cmp(&anchor_at_offset_0, &buffer)
4078                    .unwrap(),
4079                Ordering::Greater
4080            );
4081            assert_eq!(
4082                anchor_at_offset_2
4083                    .cmp(&anchor_at_offset_1, &buffer)
4084                    .unwrap(),
4085                Ordering::Greater
4086            );
4087            assert_eq!(
4088                anchor_at_offset_2
4089                    .cmp(&anchor_at_offset_0, &buffer)
4090                    .unwrap(),
4091                Ordering::Greater
4092            );
4093            buffer
4094        });
4095    }
4096
4097    #[gpui::test]
4098    fn test_anchors_at_start_and_end(cx: &mut gpui::MutableAppContext) {
4099        cx.add_model(|cx| {
4100            let mut buffer = Buffer::new(0, "", cx);
4101            let before_start_anchor = buffer.anchor_before(0);
4102            let after_end_anchor = buffer.anchor_after(0);
4103
4104            buffer.edit(vec![0..0], "abc", cx);
4105            assert_eq!(buffer.text(), "abc");
4106            assert_eq!(before_start_anchor.to_offset(&buffer), 0);
4107            assert_eq!(after_end_anchor.to_offset(&buffer), 3);
4108
4109            let after_start_anchor = buffer.anchor_after(0);
4110            let before_end_anchor = buffer.anchor_before(3);
4111
4112            buffer.edit(vec![3..3], "def", cx);
4113            buffer.edit(vec![0..0], "ghi", cx);
4114            assert_eq!(buffer.text(), "ghiabcdef");
4115            assert_eq!(before_start_anchor.to_offset(&buffer), 0);
4116            assert_eq!(after_start_anchor.to_offset(&buffer), 3);
4117            assert_eq!(before_end_anchor.to_offset(&buffer), 6);
4118            assert_eq!(after_end_anchor.to_offset(&buffer), 9);
4119            buffer
4120        });
4121    }
4122
4123    #[gpui::test]
4124    async fn test_apply_diff(mut cx: gpui::TestAppContext) {
4125        let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
4126        let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
4127
4128        let text = "a\nccc\ndddd\nffffff\n";
4129        let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
4130        buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
4131        cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
4132
4133        let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
4134        let diff = buffer.read_with(&cx, |b, cx| b.diff(text.into(), cx)).await;
4135        buffer.update(&mut cx, |b, cx| b.apply_diff(diff, cx));
4136        cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
4137    }
4138
4139    #[gpui::test]
4140    fn test_undo_redo(cx: &mut gpui::MutableAppContext) {
4141        cx.add_model(|cx| {
4142            let mut buffer = Buffer::new(0, "1234", cx);
4143            // Set group interval to zero so as to not group edits in the undo stack.
4144            buffer.history.group_interval = Duration::from_secs(0);
4145
4146            buffer.edit(vec![1..1], "abx", cx);
4147            buffer.edit(vec![3..4], "yzef", cx);
4148            buffer.edit(vec![3..5], "cd", cx);
4149            assert_eq!(buffer.text(), "1abcdef234");
4150
4151            let transactions = buffer.history.undo_stack.clone();
4152            assert_eq!(transactions.len(), 3);
4153
4154            buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
4155            assert_eq!(buffer.text(), "1cdef234");
4156            buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
4157            assert_eq!(buffer.text(), "1abcdef234");
4158
4159            buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
4160            assert_eq!(buffer.text(), "1abcdx234");
4161            buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
4162            assert_eq!(buffer.text(), "1abx234");
4163            buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
4164            assert_eq!(buffer.text(), "1abyzef234");
4165            buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
4166            assert_eq!(buffer.text(), "1abcdef234");
4167
4168            buffer.undo_or_redo(transactions[2].clone(), cx).unwrap();
4169            assert_eq!(buffer.text(), "1abyzef234");
4170            buffer.undo_or_redo(transactions[0].clone(), cx).unwrap();
4171            assert_eq!(buffer.text(), "1yzef234");
4172            buffer.undo_or_redo(transactions[1].clone(), cx).unwrap();
4173            assert_eq!(buffer.text(), "1234");
4174
4175            buffer
4176        });
4177    }
4178
4179    #[gpui::test]
4180    fn test_history(cx: &mut gpui::MutableAppContext) {
4181        cx.add_model(|cx| {
4182            let mut now = Instant::now();
4183            let mut buffer = Buffer::new(0, "123456", cx);
4184
4185            let set_id =
4186                buffer.add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap(), cx);
4187            buffer.start_transaction_at(Some(set_id), now).unwrap();
4188            buffer.edit(vec![2..4], "cd", cx);
4189            buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
4190            assert_eq!(buffer.text(), "12cd56");
4191            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
4192
4193            buffer.start_transaction_at(Some(set_id), now).unwrap();
4194            buffer
4195                .update_selection_set(
4196                    set_id,
4197                    buffer.selections_from_ranges(vec![1..3]).unwrap(),
4198                    cx,
4199                )
4200                .unwrap();
4201            buffer.edit(vec![4..5], "e", cx);
4202            buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
4203            assert_eq!(buffer.text(), "12cde6");
4204            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
4205
4206            now += buffer.history.group_interval + Duration::from_millis(1);
4207            buffer.start_transaction_at(Some(set_id), now).unwrap();
4208            buffer
4209                .update_selection_set(
4210                    set_id,
4211                    buffer.selections_from_ranges(vec![2..2]).unwrap(),
4212                    cx,
4213                )
4214                .unwrap();
4215            buffer.edit(vec![0..1], "a", cx);
4216            buffer.edit(vec![1..1], "b", cx);
4217            buffer.end_transaction_at(Some(set_id), now, cx).unwrap();
4218            assert_eq!(buffer.text(), "ab2cde6");
4219            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
4220
4221            // Last transaction happened past the group interval, undo it on its
4222            // own.
4223            buffer.undo(cx);
4224            assert_eq!(buffer.text(), "12cde6");
4225            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
4226
4227            // First two transactions happened within the group interval, undo them
4228            // together.
4229            buffer.undo(cx);
4230            assert_eq!(buffer.text(), "123456");
4231            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![4..4]);
4232
4233            // Redo the first two transactions together.
4234            buffer.redo(cx);
4235            assert_eq!(buffer.text(), "12cde6");
4236            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![1..3]);
4237
4238            // Redo the last transaction on its own.
4239            buffer.redo(cx);
4240            assert_eq!(buffer.text(), "ab2cde6");
4241            assert_eq!(buffer.selection_ranges(set_id).unwrap(), vec![3..3]);
4242
4243            buffer.start_transaction_at(None, now).unwrap();
4244            buffer.end_transaction_at(None, now, cx).unwrap();
4245            buffer.undo(cx);
4246            assert_eq!(buffer.text(), "12cde6");
4247
4248            buffer
4249        });
4250    }
4251
4252    #[gpui::test]
4253    fn test_concurrent_edits(cx: &mut gpui::MutableAppContext) {
4254        let text = "abcdef";
4255
4256        let buffer1 = cx.add_model(|cx| Buffer::new(1, text, cx));
4257        let buffer2 = cx.add_model(|cx| Buffer::new(2, text, cx));
4258        let buffer3 = cx.add_model(|cx| Buffer::new(3, text, cx));
4259
4260        let buf1_op = buffer1.update(cx, |buffer, cx| {
4261            buffer.edit(vec![1..2], "12", cx);
4262            assert_eq!(buffer.text(), "a12cdef");
4263            buffer.operations.last().unwrap().clone()
4264        });
4265        let buf2_op = buffer2.update(cx, |buffer, cx| {
4266            buffer.edit(vec![3..4], "34", cx);
4267            assert_eq!(buffer.text(), "abc34ef");
4268            buffer.operations.last().unwrap().clone()
4269        });
4270        let buf3_op = buffer3.update(cx, |buffer, cx| {
4271            buffer.edit(vec![5..6], "56", cx);
4272            assert_eq!(buffer.text(), "abcde56");
4273            buffer.operations.last().unwrap().clone()
4274        });
4275
4276        buffer1.update(cx, |buffer, _| {
4277            buffer.apply_op(buf2_op.clone()).unwrap();
4278            buffer.apply_op(buf3_op.clone()).unwrap();
4279        });
4280        buffer2.update(cx, |buffer, _| {
4281            buffer.apply_op(buf1_op.clone()).unwrap();
4282            buffer.apply_op(buf3_op.clone()).unwrap();
4283        });
4284        buffer3.update(cx, |buffer, _| {
4285            buffer.apply_op(buf1_op.clone()).unwrap();
4286            buffer.apply_op(buf2_op.clone()).unwrap();
4287        });
4288
4289        assert_eq!(buffer1.read(cx).text(), "a12c34e56");
4290        assert_eq!(buffer2.read(cx).text(), "a12c34e56");
4291        assert_eq!(buffer3.read(cx).text(), "a12c34e56");
4292    }
4293
4294    #[gpui::test(iterations = 100)]
4295    fn test_random_concurrent_edits(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
4296        let peers = env::var("PEERS")
4297            .map(|i| i.parse().expect("invalid `PEERS` variable"))
4298            .unwrap_or(5);
4299        let operations = env::var("OPERATIONS")
4300            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
4301            .unwrap_or(10);
4302
4303        let base_text_len = rng.gen_range(0..10);
4304        let base_text = RandomCharIter::new(&mut rng)
4305            .take(base_text_len)
4306            .collect::<String>();
4307        let mut replica_ids = Vec::new();
4308        let mut buffers = Vec::new();
4309        let mut network = Network::new(rng.clone());
4310
4311        for i in 0..peers {
4312            let buffer = cx.add_model(|cx| {
4313                let mut buf = Buffer::new(i as ReplicaId, base_text.as_str(), cx);
4314                buf.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
4315                buf
4316            });
4317            buffers.push(buffer);
4318            replica_ids.push(i as u16);
4319            network.add_peer(i as u16);
4320        }
4321
4322        log::info!("initial text: {:?}", base_text);
4323
4324        let mut mutation_count = operations;
4325        loop {
4326            let replica_index = rng.gen_range(0..peers);
4327            let replica_id = replica_ids[replica_index];
4328            buffers[replica_index].update(cx, |buffer, cx| match rng.gen_range(0..=100) {
4329                0..=50 if mutation_count != 0 => {
4330                    buffer.randomly_mutate(&mut rng, cx);
4331                    network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
4332                    log::info!("buffer {} text: {:?}", buffer.replica_id, buffer.text());
4333                    mutation_count -= 1;
4334                }
4335                51..=70 if mutation_count != 0 => {
4336                    buffer.randomly_undo_redo(&mut rng, cx);
4337                    network.broadcast(buffer.replica_id, mem::take(&mut buffer.operations));
4338                    mutation_count -= 1;
4339                }
4340                71..=100 if network.has_unreceived(replica_id) => {
4341                    let ops = network.receive(replica_id);
4342                    if !ops.is_empty() {
4343                        log::info!(
4344                            "peer {} applying {} ops from the network.",
4345                            replica_id,
4346                            ops.len()
4347                        );
4348                        buffer.apply_ops(ops, cx).unwrap();
4349                    }
4350                }
4351                _ => {}
4352            });
4353
4354            if mutation_count == 0 && network.is_idle() {
4355                break;
4356            }
4357        }
4358
4359        let first_buffer = buffers[0].read(cx);
4360        for buffer in &buffers[1..] {
4361            let buffer = buffer.read(cx);
4362            assert_eq!(
4363                buffer.text(),
4364                first_buffer.text(),
4365                "Replica {} text != Replica 0 text",
4366                buffer.replica_id
4367            );
4368            assert_eq!(
4369                buffer.selection_sets().collect::<HashMap<_, _>>(),
4370                first_buffer.selection_sets().collect::<HashMap<_, _>>()
4371            );
4372            assert_eq!(
4373                buffer.all_selection_ranges().collect::<HashMap<_, _>>(),
4374                first_buffer
4375                    .all_selection_ranges()
4376                    .collect::<HashMap<_, _>>()
4377            );
4378        }
4379    }
4380
4381    #[gpui::test]
4382    async fn test_reparse(mut cx: gpui::TestAppContext) {
4383        let rust_lang = rust_lang();
4384        let buffer = cx.add_model(|cx| {
4385            let text = "fn a() {}".into();
4386            Buffer::from_history(0, History::new(text), None, Some(rust_lang.clone()), cx)
4387        });
4388
4389        // Wait for the initial text to parse
4390        buffer
4391            .condition(&cx, |buffer, _| !buffer.is_parsing())
4392            .await;
4393        assert_eq!(
4394            get_tree_sexp(&buffer, &cx),
4395            concat!(
4396                "(source_file (function_item name: (identifier) ",
4397                "parameters: (parameters) ",
4398                "body: (block)))"
4399            )
4400        );
4401
4402        buffer.update(&mut cx, |buffer, _| {
4403            buffer.set_sync_parse_timeout(Duration::ZERO)
4404        });
4405
4406        // Perform some edits (add parameter and variable reference)
4407        // Parsing doesn't begin until the transaction is complete
4408        buffer.update(&mut cx, |buf, cx| {
4409            buf.start_transaction(None).unwrap();
4410
4411            let offset = buf.text().find(")").unwrap();
4412            buf.edit(vec![offset..offset], "b: C", cx);
4413            assert!(!buf.is_parsing());
4414
4415            let offset = buf.text().find("}").unwrap();
4416            buf.edit(vec![offset..offset], " d; ", cx);
4417            assert!(!buf.is_parsing());
4418
4419            buf.end_transaction(None, cx).unwrap();
4420            assert_eq!(buf.text(), "fn a(b: C) { d; }");
4421            assert!(buf.is_parsing());
4422        });
4423        buffer
4424            .condition(&cx, |buffer, _| !buffer.is_parsing())
4425            .await;
4426        assert_eq!(
4427            get_tree_sexp(&buffer, &cx),
4428            concat!(
4429                "(source_file (function_item name: (identifier) ",
4430                    "parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
4431                    "body: (block (identifier))))"
4432            )
4433        );
4434
4435        // Perform a series of edits without waiting for the current parse to complete:
4436        // * turn identifier into a field expression
4437        // * turn field expression into a method call
4438        // * add a turbofish to the method call
4439        buffer.update(&mut cx, |buf, cx| {
4440            let offset = buf.text().find(";").unwrap();
4441            buf.edit(vec![offset..offset], ".e", cx);
4442            assert_eq!(buf.text(), "fn a(b: C) { d.e; }");
4443            assert!(buf.is_parsing());
4444        });
4445        buffer.update(&mut cx, |buf, cx| {
4446            let offset = buf.text().find(";").unwrap();
4447            buf.edit(vec![offset..offset], "(f)", cx);
4448            assert_eq!(buf.text(), "fn a(b: C) { d.e(f); }");
4449            assert!(buf.is_parsing());
4450        });
4451        buffer.update(&mut cx, |buf, cx| {
4452            let offset = buf.text().find("(f)").unwrap();
4453            buf.edit(vec![offset..offset], "::<G>", cx);
4454            assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
4455            assert!(buf.is_parsing());
4456        });
4457        buffer
4458            .condition(&cx, |buffer, _| !buffer.is_parsing())
4459            .await;
4460        assert_eq!(
4461            get_tree_sexp(&buffer, &cx),
4462            concat!(
4463                "(source_file (function_item name: (identifier) ",
4464                    "parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
4465                    "body: (block (call_expression ",
4466                        "function: (generic_function ",
4467                            "function: (field_expression value: (identifier) field: (field_identifier)) ",
4468                            "type_arguments: (type_arguments (type_identifier))) ",
4469                            "arguments: (arguments (identifier))))))",
4470            )
4471        );
4472
4473        buffer.update(&mut cx, |buf, cx| {
4474            buf.undo(cx);
4475            assert_eq!(buf.text(), "fn a() {}");
4476            assert!(buf.is_parsing());
4477        });
4478        buffer
4479            .condition(&cx, |buffer, _| !buffer.is_parsing())
4480            .await;
4481        assert_eq!(
4482            get_tree_sexp(&buffer, &cx),
4483            concat!(
4484                "(source_file (function_item name: (identifier) ",
4485                "parameters: (parameters) ",
4486                "body: (block)))"
4487            )
4488        );
4489
4490        buffer.update(&mut cx, |buf, cx| {
4491            buf.redo(cx);
4492            assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
4493            assert!(buf.is_parsing());
4494        });
4495        buffer
4496            .condition(&cx, |buffer, _| !buffer.is_parsing())
4497            .await;
4498        assert_eq!(
4499            get_tree_sexp(&buffer, &cx),
4500            concat!(
4501                "(source_file (function_item name: (identifier) ",
4502                    "parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
4503                    "body: (block (call_expression ",
4504                        "function: (generic_function ",
4505                            "function: (field_expression value: (identifier) field: (field_identifier)) ",
4506                            "type_arguments: (type_arguments (type_identifier))) ",
4507                            "arguments: (arguments (identifier))))))",
4508            )
4509        );
4510
4511        fn get_tree_sexp(buffer: &ModelHandle<Buffer>, cx: &gpui::TestAppContext) -> String {
4512            buffer.read_with(cx, |buffer, _| {
4513                buffer.syntax_tree().unwrap().root_node().to_sexp()
4514            })
4515        }
4516    }
4517
4518    #[gpui::test]
4519    async fn test_enclosing_bracket_ranges(mut cx: gpui::TestAppContext) {
4520        use unindent::Unindent as _;
4521
4522        let rust_lang = rust_lang();
4523        let buffer = cx.add_model(|cx| {
4524            let text = "
4525                mod x {
4526                    mod y {
4527
4528                    }
4529                }
4530            "
4531            .unindent()
4532            .into();
4533            Buffer::from_history(0, History::new(text), None, Some(rust_lang.clone()), cx)
4534        });
4535        buffer
4536            .condition(&cx, |buffer, _| !buffer.is_parsing())
4537            .await;
4538        buffer.read_with(&cx, |buf, _| {
4539            assert_eq!(
4540                buf.enclosing_bracket_point_ranges(Point::new(1, 6)..Point::new(1, 6)),
4541                Some((
4542                    Point::new(0, 6)..Point::new(0, 7),
4543                    Point::new(4, 0)..Point::new(4, 1)
4544                ))
4545            );
4546            assert_eq!(
4547                buf.enclosing_bracket_point_ranges(Point::new(1, 10)..Point::new(1, 10)),
4548                Some((
4549                    Point::new(1, 10)..Point::new(1, 11),
4550                    Point::new(3, 4)..Point::new(3, 5)
4551                ))
4552            );
4553            assert_eq!(
4554                buf.enclosing_bracket_point_ranges(Point::new(3, 5)..Point::new(3, 5)),
4555                Some((
4556                    Point::new(1, 10)..Point::new(1, 11),
4557                    Point::new(3, 4)..Point::new(3, 5)
4558                ))
4559            );
4560        });
4561    }
4562
4563    #[gpui::test]
4564    async fn test_edit_with_autoindent(mut cx: gpui::TestAppContext) {
4565        let rust_lang = rust_lang();
4566        let buffer = cx.add_model(|cx| {
4567            let text = "fn a() {}".into();
4568            Buffer::from_history(0, History::new(text), None, Some(rust_lang.clone()), cx)
4569        });
4570
4571        buffer
4572            .condition(&cx, |buffer, _| !buffer.is_parsing())
4573            .await;
4574
4575        buffer.update(&mut cx, |buffer, cx| {
4576            buffer.edit_with_autoindent([8..8], "\n\n", cx);
4577            assert_eq!(buffer.text(), "fn a() {\n    \n}");
4578
4579            buffer.edit_with_autoindent([Point::new(1, 4)..Point::new(1, 4)], "b()\n", cx);
4580            assert_eq!(buffer.text(), "fn a() {\n    b()\n    \n}");
4581
4582            buffer.edit_with_autoindent([Point::new(2, 4)..Point::new(2, 4)], ".c", cx);
4583            assert_eq!(buffer.text(), "fn a() {\n    b()\n        .c\n}");
4584        });
4585    }
4586
4587    #[test]
4588    fn test_contiguous_ranges() {
4589        assert_eq!(
4590            contiguous_ranges([1, 2, 3, 5, 6, 9, 10, 11, 12].iter().copied()).collect::<Vec<_>>(),
4591            &[1..4, 5..7, 9..13]
4592        );
4593    }
4594
4595    #[derive(Clone)]
4596    struct Envelope<T: Clone> {
4597        message: T,
4598        sender: ReplicaId,
4599    }
4600
4601    struct Network<T: Clone, R: rand::Rng> {
4602        inboxes: std::collections::BTreeMap<ReplicaId, Vec<Envelope<T>>>,
4603        all_messages: Vec<T>,
4604        rng: R,
4605    }
4606
4607    impl<T: Clone, R: rand::Rng> Network<T, R> {
4608        fn new(rng: R) -> Self {
4609            Network {
4610                inboxes: Default::default(),
4611                all_messages: Vec::new(),
4612                rng,
4613            }
4614        }
4615
4616        fn add_peer(&mut self, id: ReplicaId) {
4617            self.inboxes.insert(id, Vec::new());
4618        }
4619
4620        fn is_idle(&self) -> bool {
4621            self.inboxes.values().all(|i| i.is_empty())
4622        }
4623
4624        fn broadcast(&mut self, sender: ReplicaId, messages: Vec<T>) {
4625            for (replica, inbox) in self.inboxes.iter_mut() {
4626                if *replica != sender {
4627                    for message in &messages {
4628                        let min_index = inbox
4629                            .iter()
4630                            .enumerate()
4631                            .rev()
4632                            .find_map(|(index, envelope)| {
4633                                if sender == envelope.sender {
4634                                    Some(index + 1)
4635                                } else {
4636                                    None
4637                                }
4638                            })
4639                            .unwrap_or(0);
4640
4641                        // Insert one or more duplicates of this message *after* the previous
4642                        // message delivered by this replica.
4643                        for _ in 0..self.rng.gen_range(1..4) {
4644                            let insertion_index = self.rng.gen_range(min_index..inbox.len() + 1);
4645                            inbox.insert(
4646                                insertion_index,
4647                                Envelope {
4648                                    message: message.clone(),
4649                                    sender,
4650                                },
4651                            );
4652                        }
4653                    }
4654                }
4655            }
4656            self.all_messages.extend(messages);
4657        }
4658
4659        fn has_unreceived(&self, receiver: ReplicaId) -> bool {
4660            !self.inboxes[&receiver].is_empty()
4661        }
4662
4663        fn receive(&mut self, receiver: ReplicaId) -> Vec<T> {
4664            let inbox = self.inboxes.get_mut(&receiver).unwrap();
4665            let count = self.rng.gen_range(0..inbox.len() + 1);
4666            inbox
4667                .drain(0..count)
4668                .map(|envelope| envelope.message)
4669                .collect()
4670        }
4671    }
4672
4673    fn rust_lang() -> Arc<Language> {
4674        Arc::new(
4675            Language::new(
4676                LanguageConfig {
4677                    name: "Rust".to_string(),
4678                    path_suffixes: vec!["rs".to_string()],
4679                    ..Default::default()
4680                },
4681                tree_sitter_rust::language(),
4682            )
4683            .with_indents_query(
4684                r#"
4685                    (call_expression) @indent
4686                    (field_expression) @indent
4687                    (_ "{" "}" @end) @indent
4688                "#,
4689            )
4690            .unwrap()
4691            .with_brackets_query(r#" ("{" @open "}" @close) "#)
4692            .unwrap(),
4693        )
4694    }
4695}