buffer.rs

   1pub use crate::{
   2    diagnostic_set::DiagnosticSet,
   3    highlight_map::{HighlightId, HighlightMap},
   4    proto, BracketPair, Grammar, Language, LanguageConfig, LanguageRegistry, PLAIN_TEXT,
   5};
   6use crate::{
   7    diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
   8    outline::OutlineItem,
   9    CodeLabel, Outline,
  10};
  11use anyhow::{anyhow, Result};
  12use clock::ReplicaId;
  13use futures::FutureExt as _;
  14use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, MutableAppContext, Task};
  15use lazy_static::lazy_static;
  16use parking_lot::Mutex;
  17use settings::Settings;
  18use similar::{ChangeTag, TextDiff};
  19use smol::future::yield_now;
  20use std::{
  21    any::Any,
  22    cmp::{self, Ordering},
  23    collections::BTreeMap,
  24    ffi::OsStr,
  25    future::Future,
  26    iter::{self, Iterator, Peekable},
  27    mem,
  28    ops::{Deref, DerefMut, Range},
  29    path::{Path, PathBuf},
  30    str,
  31    sync::Arc,
  32    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  33    vec,
  34};
  35use sum_tree::TreeMap;
  36use text::operation_queue::OperationQueue;
  37pub use text::{Buffer as TextBuffer, BufferSnapshot as TextBufferSnapshot, Operation as _, *};
  38use theme::SyntaxTheme;
  39use tree_sitter::{InputEdit, QueryCursor, Tree};
  40use util::TryFutureExt as _;
  41
  42#[cfg(any(test, feature = "test-support"))]
  43pub use {tree_sitter_rust, tree_sitter_typescript};
  44
  45pub use lsp::DiagnosticSeverity;
  46
  47lazy_static! {
  48    static ref QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Default::default();
  49}
  50
  51pub struct Buffer {
  52    text: TextBuffer,
  53    file: Option<Arc<dyn File>>,
  54    saved_version: clock::Global,
  55    saved_version_fingerprint: String,
  56    saved_mtime: SystemTime,
  57    transaction_depth: usize,
  58    was_dirty_before_starting_transaction: Option<bool>,
  59    language: Option<Arc<Language>>,
  60    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  61    pending_autoindent: Option<Task<()>>,
  62    sync_parse_timeout: Duration,
  63    syntax_tree: Mutex<Option<SyntaxTree>>,
  64    parsing_in_background: bool,
  65    parse_count: usize,
  66    diagnostics: DiagnosticSet,
  67    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  68    selections_update_count: usize,
  69    diagnostics_update_count: usize,
  70    diagnostics_timestamp: clock::Lamport,
  71    file_update_count: usize,
  72    completion_triggers: Vec<String>,
  73    completion_triggers_timestamp: clock::Lamport,
  74    deferred_ops: OperationQueue<Operation>,
  75}
  76
  77pub struct BufferSnapshot {
  78    text: text::BufferSnapshot,
  79    tree: Option<Tree>,
  80    file: Option<Arc<dyn File>>,
  81    diagnostics: DiagnosticSet,
  82    diagnostics_update_count: usize,
  83    file_update_count: usize,
  84    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  85    selections_update_count: usize,
  86    language: Option<Arc<Language>>,
  87    parse_count: usize,
  88}
  89
  90#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  91pub struct IndentSize {
  92    pub len: u32,
  93    pub kind: IndentKind,
  94}
  95
  96#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  97pub enum IndentKind {
  98    Space,
  99    Tab,
 100}
 101
 102#[derive(Clone, Debug)]
 103struct SelectionSet {
 104    line_mode: bool,
 105    selections: Arc<[Selection<Anchor>]>,
 106    lamport_timestamp: clock::Lamport,
 107}
 108
 109#[derive(Clone, Debug, PartialEq, Eq)]
 110pub struct GroupId {
 111    source: Arc<str>,
 112    id: usize,
 113}
 114
 115#[derive(Clone, Debug, PartialEq, Eq)]
 116pub struct Diagnostic {
 117    pub code: Option<String>,
 118    pub severity: DiagnosticSeverity,
 119    pub message: String,
 120    pub group_id: usize,
 121    pub is_valid: bool,
 122    pub is_primary: bool,
 123    pub is_disk_based: bool,
 124    pub is_unnecessary: bool,
 125}
 126
 127#[derive(Clone, Debug)]
 128pub struct Completion {
 129    pub old_range: Range<Anchor>,
 130    pub new_text: String,
 131    pub label: CodeLabel,
 132    pub lsp_completion: lsp::CompletionItem,
 133}
 134
 135#[derive(Clone, Debug)]
 136pub struct CodeAction {
 137    pub range: Range<Anchor>,
 138    pub lsp_action: lsp::CodeAction,
 139}
 140
 141#[derive(Clone, Debug, PartialEq, Eq)]
 142pub enum Operation {
 143    Buffer(text::Operation),
 144    UpdateDiagnostics {
 145        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 146        lamport_timestamp: clock::Lamport,
 147    },
 148    UpdateSelections {
 149        selections: Arc<[Selection<Anchor>]>,
 150        lamport_timestamp: clock::Lamport,
 151        line_mode: bool,
 152    },
 153    UpdateCompletionTriggers {
 154        triggers: Vec<String>,
 155        lamport_timestamp: clock::Lamport,
 156    },
 157}
 158
 159#[derive(Clone, Debug, PartialEq, Eq)]
 160pub enum Event {
 161    Operation(Operation),
 162    Edited,
 163    DirtyChanged,
 164    Saved,
 165    FileHandleChanged,
 166    Reloaded,
 167    Reparsed,
 168    DiagnosticsUpdated,
 169    Closed,
 170}
 171
 172pub trait File: Send + Sync {
 173    fn as_local(&self) -> Option<&dyn LocalFile>;
 174
 175    fn is_local(&self) -> bool {
 176        self.as_local().is_some()
 177    }
 178
 179    fn mtime(&self) -> SystemTime;
 180
 181    /// Returns the path of this file relative to the worktree's root directory.
 182    fn path(&self) -> &Arc<Path>;
 183
 184    /// Returns the path of this file relative to the worktree's parent directory (this means it
 185    /// includes the name of the worktree's root folder).
 186    fn full_path(&self, cx: &AppContext) -> PathBuf;
 187
 188    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 189    /// of its worktree, then this method will return the name of the worktree itself.
 190    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
 191
 192    fn is_deleted(&self) -> bool;
 193
 194    fn save(
 195        &self,
 196        buffer_id: u64,
 197        text: Rope,
 198        version: clock::Global,
 199        line_ending: LineEnding,
 200        cx: &mut MutableAppContext,
 201    ) -> Task<Result<(clock::Global, String, SystemTime)>>;
 202
 203    fn as_any(&self) -> &dyn Any;
 204
 205    fn to_proto(&self) -> rpc::proto::File;
 206}
 207
 208pub trait LocalFile: File {
 209    /// Returns the absolute path of this file.
 210    fn abs_path(&self, cx: &AppContext) -> PathBuf;
 211
 212    fn load(&self, cx: &AppContext) -> Task<Result<String>>;
 213
 214    fn buffer_reloaded(
 215        &self,
 216        buffer_id: u64,
 217        version: &clock::Global,
 218        fingerprint: String,
 219        line_ending: LineEnding,
 220        mtime: SystemTime,
 221        cx: &mut MutableAppContext,
 222    );
 223}
 224
 225pub(crate) struct QueryCursorHandle(Option<QueryCursor>);
 226
 227#[derive(Clone)]
 228struct SyntaxTree {
 229    tree: Tree,
 230    version: clock::Global,
 231}
 232
 233#[derive(Clone, Debug)]
 234pub enum AutoindentMode {
 235    /// Indent each line of inserted text.
 236    EachLine,
 237    /// Apply the same indentation adjustment to all of the lines
 238    /// in a given insertion.
 239    Block {
 240        /// The original indentation level of the first line of each
 241        /// insertion, if it has been copied.
 242        original_indent_columns: Vec<u32>,
 243    },
 244}
 245
 246#[derive(Clone)]
 247struct AutoindentRequest {
 248    before_edit: BufferSnapshot,
 249    entries: Vec<AutoindentRequestEntry>,
 250    indent_size: IndentSize,
 251    is_block_mode: bool,
 252}
 253
 254#[derive(Clone)]
 255struct AutoindentRequestEntry {
 256    /// A range of the buffer whose indentation should be adjusted.
 257    range: Range<Anchor>,
 258    /// Whether or not these lines should be considered brand new, for the
 259    /// purpose of auto-indent. When text is not new, its indentation will
 260    /// only be adjusted if the suggested indentation level has *changed*
 261    /// since the edit was made.
 262    first_line_is_new: bool,
 263    original_indent_column: Option<u32>,
 264}
 265
 266#[derive(Debug)]
 267struct IndentSuggestion {
 268    basis_row: u32,
 269    delta: Ordering,
 270}
 271
 272pub(crate) struct TextProvider<'a>(pub(crate) &'a Rope);
 273
 274struct BufferChunkHighlights<'a> {
 275    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
 276    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
 277    stack: Vec<(usize, HighlightId)>,
 278    highlight_map: HighlightMap,
 279    _query_cursor: QueryCursorHandle,
 280}
 281
 282pub struct BufferChunks<'a> {
 283    range: Range<usize>,
 284    chunks: rope::Chunks<'a>,
 285    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 286    error_depth: usize,
 287    warning_depth: usize,
 288    information_depth: usize,
 289    hint_depth: usize,
 290    unnecessary_depth: usize,
 291    highlights: Option<BufferChunkHighlights<'a>>,
 292}
 293
 294#[derive(Clone, Copy, Debug, Default)]
 295pub struct Chunk<'a> {
 296    pub text: &'a str,
 297    pub syntax_highlight_id: Option<HighlightId>,
 298    pub highlight_style: Option<HighlightStyle>,
 299    pub diagnostic_severity: Option<DiagnosticSeverity>,
 300    pub is_unnecessary: bool,
 301}
 302
 303pub struct Diff {
 304    base_version: clock::Global,
 305    new_text: Arc<str>,
 306    changes: Vec<(ChangeTag, usize)>,
 307    line_ending: LineEnding,
 308    start_offset: usize,
 309}
 310
 311#[derive(Clone, Copy)]
 312pub(crate) struct DiagnosticEndpoint {
 313    offset: usize,
 314    is_start: bool,
 315    severity: DiagnosticSeverity,
 316    is_unnecessary: bool,
 317}
 318
 319#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 320pub enum CharKind {
 321    Punctuation,
 322    Whitespace,
 323    Word,
 324}
 325
 326impl CharKind {
 327    pub fn coerce_punctuation(self, treat_punctuation_as_word: bool) -> Self {
 328        if treat_punctuation_as_word && self == CharKind::Punctuation {
 329            CharKind::Word
 330        } else {
 331            self
 332        }
 333    }
 334}
 335
 336impl Buffer {
 337    pub fn new<T: Into<String>>(
 338        replica_id: ReplicaId,
 339        base_text: T,
 340        cx: &mut ModelContext<Self>,
 341    ) -> Self {
 342        Self::build(
 343            TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
 344            None,
 345        )
 346    }
 347
 348    pub fn from_file<T: Into<String>>(
 349        replica_id: ReplicaId,
 350        base_text: T,
 351        file: Arc<dyn File>,
 352        cx: &mut ModelContext<Self>,
 353    ) -> Self {
 354        Self::build(
 355            TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
 356            Some(file),
 357        )
 358    }
 359
 360    pub fn from_proto(
 361        replica_id: ReplicaId,
 362        message: proto::Buffer,
 363        file: Option<Arc<dyn File>>,
 364        cx: &mut ModelContext<Self>,
 365    ) -> Result<Self> {
 366        let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
 367        let mut this = Self::build(buffer, file);
 368        this.text.set_line_ending(proto::deserialize_line_ending(
 369            proto::LineEnding::from_i32(message.line_ending)
 370                .ok_or_else(|| anyhow!("missing line_ending"))?,
 371        ));
 372        let ops = message
 373            .operations
 374            .into_iter()
 375            .map(proto::deserialize_operation)
 376            .collect::<Result<Vec<_>>>()?;
 377        this.apply_ops(ops, cx)?;
 378
 379        Ok(this)
 380    }
 381
 382    pub fn to_proto(&self) -> proto::Buffer {
 383        let mut operations = self
 384            .text
 385            .history()
 386            .map(|op| proto::serialize_operation(&Operation::Buffer(op.clone())))
 387            .chain(self.deferred_ops.iter().map(proto::serialize_operation))
 388            .chain(self.remote_selections.iter().map(|(_, set)| {
 389                proto::serialize_operation(&Operation::UpdateSelections {
 390                    selections: set.selections.clone(),
 391                    lamport_timestamp: set.lamport_timestamp,
 392                    line_mode: set.line_mode,
 393                })
 394            }))
 395            .chain(Some(proto::serialize_operation(
 396                &Operation::UpdateDiagnostics {
 397                    diagnostics: self.diagnostics.iter().cloned().collect(),
 398                    lamport_timestamp: self.diagnostics_timestamp,
 399                },
 400            )))
 401            .chain(Some(proto::serialize_operation(
 402                &Operation::UpdateCompletionTriggers {
 403                    triggers: self.completion_triggers.clone(),
 404                    lamport_timestamp: self.completion_triggers_timestamp,
 405                },
 406            )))
 407            .collect::<Vec<_>>();
 408        operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
 409        proto::Buffer {
 410            id: self.remote_id(),
 411            file: self.file.as_ref().map(|f| f.to_proto()),
 412            base_text: self.base_text().to_string(),
 413            operations,
 414            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
 415        }
 416    }
 417
 418    pub fn with_language(mut self, language: Arc<Language>, cx: &mut ModelContext<Self>) -> Self {
 419        self.set_language(Some(language), cx);
 420        self
 421    }
 422
 423    fn build(buffer: TextBuffer, file: Option<Arc<dyn File>>) -> Self {
 424        let saved_mtime = if let Some(file) = file.as_ref() {
 425            file.mtime()
 426        } else {
 427            UNIX_EPOCH
 428        };
 429
 430        Self {
 431            saved_mtime,
 432            saved_version: buffer.version(),
 433            saved_version_fingerprint: buffer.as_rope().fingerprint(),
 434            transaction_depth: 0,
 435            was_dirty_before_starting_transaction: None,
 436            text: buffer,
 437            file,
 438            syntax_tree: Mutex::new(None),
 439            parsing_in_background: false,
 440            parse_count: 0,
 441            sync_parse_timeout: Duration::from_millis(1),
 442            autoindent_requests: Default::default(),
 443            pending_autoindent: Default::default(),
 444            language: None,
 445            remote_selections: Default::default(),
 446            selections_update_count: 0,
 447            diagnostics: Default::default(),
 448            diagnostics_update_count: 0,
 449            diagnostics_timestamp: Default::default(),
 450            file_update_count: 0,
 451            completion_triggers: Default::default(),
 452            completion_triggers_timestamp: Default::default(),
 453            deferred_ops: OperationQueue::new(),
 454        }
 455    }
 456
 457    pub fn snapshot(&self) -> BufferSnapshot {
 458        BufferSnapshot {
 459            text: self.text.snapshot(),
 460            tree: self.syntax_tree(),
 461            file: self.file.clone(),
 462            remote_selections: self.remote_selections.clone(),
 463            diagnostics: self.diagnostics.clone(),
 464            diagnostics_update_count: self.diagnostics_update_count,
 465            file_update_count: self.file_update_count,
 466            language: self.language.clone(),
 467            parse_count: self.parse_count,
 468            selections_update_count: self.selections_update_count,
 469        }
 470    }
 471
 472    pub fn as_text_snapshot(&self) -> &text::BufferSnapshot {
 473        &self.text
 474    }
 475
 476    pub fn text_snapshot(&self) -> text::BufferSnapshot {
 477        self.text.snapshot()
 478    }
 479
 480    pub fn file(&self) -> Option<&dyn File> {
 481        self.file.as_deref()
 482    }
 483
 484    pub fn save(
 485        &mut self,
 486        cx: &mut ModelContext<Self>,
 487    ) -> Task<Result<(clock::Global, String, SystemTime)>> {
 488        let file = if let Some(file) = self.file.as_ref() {
 489            file
 490        } else {
 491            return Task::ready(Err(anyhow!("buffer has no file")));
 492        };
 493        let text = self.as_rope().clone();
 494        let version = self.version();
 495        let save = file.save(
 496            self.remote_id(),
 497            text,
 498            version,
 499            self.line_ending(),
 500            cx.as_mut(),
 501        );
 502        cx.spawn(|this, mut cx| async move {
 503            let (version, fingerprint, mtime) = save.await?;
 504            this.update(&mut cx, |this, cx| {
 505                this.did_save(version.clone(), fingerprint.clone(), mtime, None, cx);
 506            });
 507            Ok((version, fingerprint, mtime))
 508        })
 509    }
 510
 511    pub fn saved_version(&self) -> &clock::Global {
 512        &self.saved_version
 513    }
 514
 515    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 516        *self.syntax_tree.lock() = None;
 517        self.language = language;
 518        self.reparse(cx);
 519    }
 520
 521    pub fn did_save(
 522        &mut self,
 523        version: clock::Global,
 524        fingerprint: String,
 525        mtime: SystemTime,
 526        new_file: Option<Arc<dyn File>>,
 527        cx: &mut ModelContext<Self>,
 528    ) {
 529        self.saved_version = version;
 530        self.saved_version_fingerprint = fingerprint;
 531        self.saved_mtime = mtime;
 532        if let Some(new_file) = new_file {
 533            self.file = Some(new_file);
 534            self.file_update_count += 1;
 535        }
 536        cx.emit(Event::Saved);
 537        cx.notify();
 538    }
 539
 540    pub fn reload(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<Option<Transaction>>> {
 541        cx.spawn(|this, mut cx| async move {
 542            if let Some((new_mtime, new_text)) = this.read_with(&cx, |this, cx| {
 543                let file = this.file.as_ref()?.as_local()?;
 544                Some((file.mtime(), file.load(cx)))
 545            }) {
 546                let new_text = new_text.await?;
 547                let diff = this
 548                    .read_with(&cx, |this, cx| this.diff(new_text, cx))
 549                    .await;
 550                this.update(&mut cx, |this, cx| {
 551                    if let Some(transaction) = this.apply_diff(diff, cx).cloned() {
 552                        this.did_reload(
 553                            this.version(),
 554                            this.as_rope().fingerprint(),
 555                            this.line_ending(),
 556                            new_mtime,
 557                            cx,
 558                        );
 559                        Ok(Some(transaction))
 560                    } else {
 561                        Ok(None)
 562                    }
 563                })
 564            } else {
 565                Ok(None)
 566            }
 567        })
 568    }
 569
 570    pub fn did_reload(
 571        &mut self,
 572        version: clock::Global,
 573        fingerprint: String,
 574        line_ending: LineEnding,
 575        mtime: SystemTime,
 576        cx: &mut ModelContext<Self>,
 577    ) {
 578        self.saved_version = version;
 579        self.saved_version_fingerprint = fingerprint;
 580        self.text.set_line_ending(line_ending);
 581        self.saved_mtime = mtime;
 582        if let Some(file) = self.file.as_ref().and_then(|f| f.as_local()) {
 583            file.buffer_reloaded(
 584                self.remote_id(),
 585                &self.saved_version,
 586                self.saved_version_fingerprint.clone(),
 587                self.line_ending(),
 588                self.saved_mtime,
 589                cx,
 590            );
 591        }
 592        cx.emit(Event::Reloaded);
 593        cx.notify();
 594    }
 595
 596    pub fn file_updated(
 597        &mut self,
 598        new_file: Arc<dyn File>,
 599        cx: &mut ModelContext<Self>,
 600    ) -> Task<()> {
 601        let old_file = if let Some(file) = self.file.as_ref() {
 602            file
 603        } else {
 604            return Task::ready(());
 605        };
 606        let mut file_changed = false;
 607        let mut task = Task::ready(());
 608
 609        if new_file.path() != old_file.path() {
 610            file_changed = true;
 611        }
 612
 613        if new_file.is_deleted() {
 614            if !old_file.is_deleted() {
 615                file_changed = true;
 616                if !self.is_dirty() {
 617                    cx.emit(Event::DirtyChanged);
 618                }
 619            }
 620        } else {
 621            let new_mtime = new_file.mtime();
 622            if new_mtime != old_file.mtime() {
 623                file_changed = true;
 624
 625                if !self.is_dirty() {
 626                    let reload = self.reload(cx).log_err().map(drop);
 627                    task = cx.foreground().spawn(reload);
 628                }
 629            }
 630        }
 631
 632        if file_changed {
 633            self.file_update_count += 1;
 634            cx.emit(Event::FileHandleChanged);
 635            cx.notify();
 636        }
 637        self.file = Some(new_file);
 638        task
 639    }
 640
 641    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 642        cx.emit(Event::Closed);
 643    }
 644
 645    pub fn language(&self) -> Option<&Arc<Language>> {
 646        self.language.as_ref()
 647    }
 648
 649    pub fn parse_count(&self) -> usize {
 650        self.parse_count
 651    }
 652
 653    pub fn selections_update_count(&self) -> usize {
 654        self.selections_update_count
 655    }
 656
 657    pub fn diagnostics_update_count(&self) -> usize {
 658        self.diagnostics_update_count
 659    }
 660
 661    pub fn file_update_count(&self) -> usize {
 662        self.file_update_count
 663    }
 664
 665    pub(crate) fn syntax_tree(&self) -> Option<Tree> {
 666        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
 667            self.interpolate_tree(syntax_tree);
 668            Some(syntax_tree.tree.clone())
 669        } else {
 670            None
 671        }
 672    }
 673
 674    #[cfg(any(test, feature = "test-support"))]
 675    pub fn is_parsing(&self) -> bool {
 676        self.parsing_in_background
 677    }
 678
 679    #[cfg(test)]
 680    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 681        self.sync_parse_timeout = timeout;
 682    }
 683
 684    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
 685        if self.parsing_in_background {
 686            return false;
 687        }
 688
 689        if let Some(grammar) = self.grammar().cloned() {
 690            let old_tree = self.syntax_tree();
 691            let text = self.as_rope().clone();
 692            let parsed_version = self.version();
 693            let parse_task = cx.background().spawn({
 694                let grammar = grammar.clone();
 695                async move { grammar.parse_text(&text, old_tree) }
 696            });
 697
 698            match cx
 699                .background()
 700                .block_with_timeout(self.sync_parse_timeout, parse_task)
 701            {
 702                Ok(new_tree) => {
 703                    self.did_finish_parsing(new_tree, parsed_version, cx);
 704                    return true;
 705                }
 706                Err(parse_task) => {
 707                    self.parsing_in_background = true;
 708                    cx.spawn(move |this, mut cx| async move {
 709                        let new_tree = parse_task.await;
 710                        this.update(&mut cx, move |this, cx| {
 711                            let grammar_changed = this
 712                                .grammar()
 713                                .map_or(true, |curr_grammar| !Arc::ptr_eq(&grammar, curr_grammar));
 714                            let parse_again =
 715                                this.version.changed_since(&parsed_version) || grammar_changed;
 716                            this.parsing_in_background = false;
 717                            this.did_finish_parsing(new_tree, parsed_version, cx);
 718
 719                            if parse_again && this.reparse(cx) {}
 720                        });
 721                    })
 722                    .detach();
 723                }
 724            }
 725        }
 726        false
 727    }
 728
 729    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
 730        for edit in self.edits_since::<(usize, Point)>(&tree.version) {
 731            let (bytes, lines) = edit.flatten();
 732            tree.tree.edit(&InputEdit {
 733                start_byte: bytes.new.start,
 734                old_end_byte: bytes.new.start + bytes.old.len(),
 735                new_end_byte: bytes.new.end,
 736                start_position: lines.new.start.to_ts_point(),
 737                old_end_position: (lines.new.start + (lines.old.end - lines.old.start))
 738                    .to_ts_point(),
 739                new_end_position: lines.new.end.to_ts_point(),
 740            });
 741        }
 742        tree.version = self.version();
 743    }
 744
 745    fn did_finish_parsing(
 746        &mut self,
 747        tree: Tree,
 748        version: clock::Global,
 749        cx: &mut ModelContext<Self>,
 750    ) {
 751        self.parse_count += 1;
 752        *self.syntax_tree.lock() = Some(SyntaxTree { tree, version });
 753        self.request_autoindent(cx);
 754        cx.emit(Event::Reparsed);
 755        cx.notify();
 756    }
 757
 758    pub fn update_diagnostics(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
 759        let lamport_timestamp = self.text.lamport_clock.tick();
 760        let op = Operation::UpdateDiagnostics {
 761            diagnostics: diagnostics.iter().cloned().collect(),
 762            lamport_timestamp,
 763        };
 764        self.apply_diagnostic_update(diagnostics, lamport_timestamp, cx);
 765        self.send_operation(op, cx);
 766    }
 767
 768    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 769        if let Some(indent_sizes) = self.compute_autoindents() {
 770            let indent_sizes = cx.background().spawn(indent_sizes);
 771            match cx
 772                .background()
 773                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 774            {
 775                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
 776                Err(indent_sizes) => {
 777                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 778                        let indent_sizes = indent_sizes.await;
 779                        this.update(&mut cx, |this, cx| {
 780                            this.apply_autoindents(indent_sizes, cx);
 781                        });
 782                    }));
 783                }
 784            }
 785        }
 786    }
 787
 788    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
 789        let max_rows_between_yields = 100;
 790        let snapshot = self.snapshot();
 791        if snapshot.language.is_none()
 792            || snapshot.tree.is_none()
 793            || self.autoindent_requests.is_empty()
 794        {
 795            return None;
 796        }
 797
 798        let autoindent_requests = self.autoindent_requests.clone();
 799        Some(async move {
 800            let mut indent_sizes = BTreeMap::new();
 801            for request in autoindent_requests {
 802                // Resolve each edited range to its row in the current buffer and in the
 803                // buffer before this batch of edits.
 804                let mut row_ranges = Vec::new();
 805                let mut old_to_new_rows = BTreeMap::new();
 806                for entry in &request.entries {
 807                    let position = entry.range.start;
 808                    let new_row = position.to_point(&snapshot).row;
 809                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
 810                    if !entry.first_line_is_new {
 811                        let old_row = position.to_point(&request.before_edit).row;
 812                        old_to_new_rows.insert(old_row, new_row);
 813                    }
 814                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
 815                }
 816
 817                // Build a map containing the suggested indentation for each of the edited lines
 818                // with respect to the state of the buffer before these edits. This map is keyed
 819                // by the rows for these lines in the current state of the buffer.
 820                let mut old_suggestions = BTreeMap::<u32, IndentSize>::default();
 821                let old_edited_ranges =
 822                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 823                for old_edited_range in old_edited_ranges {
 824                    let suggestions = request
 825                        .before_edit
 826                        .suggest_autoindents(old_edited_range.clone())
 827                        .into_iter()
 828                        .flatten();
 829                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 830                        if let Some(suggestion) = suggestion {
 831                            let suggested_indent = old_to_new_rows
 832                                .get(&suggestion.basis_row)
 833                                .and_then(|from_row| old_suggestions.get(from_row).copied())
 834                                .unwrap_or_else(|| {
 835                                    request
 836                                        .before_edit
 837                                        .indent_size_for_line(suggestion.basis_row)
 838                                })
 839                                .with_delta(suggestion.delta, request.indent_size);
 840                            old_suggestions
 841                                .insert(*old_to_new_rows.get(&old_row).unwrap(), suggested_indent);
 842                        }
 843                    }
 844                    yield_now().await;
 845                }
 846
 847                // In block mode, only compute indentation suggestions for the first line
 848                // of each insertion. Otherwise, compute suggestions for every inserted line.
 849                let new_edited_row_ranges = contiguous_ranges(
 850                    row_ranges.iter().flat_map(|(range, _)| {
 851                        if request.is_block_mode {
 852                            range.start..range.start + 1
 853                        } else {
 854                            range.clone()
 855                        }
 856                    }),
 857                    max_rows_between_yields,
 858                );
 859
 860                // Compute new suggestions for each line, but only include them in the result
 861                // if they differ from the old suggestion for that line.
 862                for new_edited_row_range in new_edited_row_ranges {
 863                    let suggestions = snapshot
 864                        .suggest_autoindents(new_edited_row_range.clone())
 865                        .into_iter()
 866                        .flatten();
 867                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 868                        if let Some(suggestion) = suggestion {
 869                            let suggested_indent = indent_sizes
 870                                .get(&suggestion.basis_row)
 871                                .copied()
 872                                .unwrap_or_else(|| {
 873                                    snapshot.indent_size_for_line(suggestion.basis_row)
 874                                })
 875                                .with_delta(suggestion.delta, request.indent_size);
 876                            if old_suggestions
 877                                .get(&new_row)
 878                                .map_or(true, |old_indentation| {
 879                                    suggested_indent != *old_indentation
 880                                })
 881                            {
 882                                indent_sizes.insert(new_row, suggested_indent);
 883                            }
 884                        }
 885                    }
 886                    yield_now().await;
 887                }
 888
 889                // For each block of inserted text, adjust the indentation of the remaining
 890                // lines of the block by the same amount as the first line was adjusted.
 891                if request.is_block_mode {
 892                    for (row_range, original_indent_column) in
 893                        row_ranges
 894                            .into_iter()
 895                            .filter_map(|(range, original_indent_column)| {
 896                                if range.len() > 1 {
 897                                    Some((range, original_indent_column?))
 898                                } else {
 899                                    None
 900                                }
 901                            })
 902                    {
 903                        let new_indent = indent_sizes
 904                            .get(&row_range.start)
 905                            .copied()
 906                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
 907                        let delta = new_indent.len as i64 - original_indent_column as i64;
 908                        if delta != 0 {
 909                            for row in row_range.skip(1) {
 910                                indent_sizes.entry(row).or_insert_with(|| {
 911                                    let mut size = snapshot.indent_size_for_line(row);
 912                                    if size.kind == new_indent.kind {
 913                                        match delta.cmp(&0) {
 914                                            Ordering::Greater => size.len += delta as u32,
 915                                            Ordering::Less => {
 916                                                size.len = size.len.saturating_sub(-delta as u32)
 917                                            }
 918                                            Ordering::Equal => {}
 919                                        }
 920                                    }
 921                                    size
 922                                });
 923                            }
 924                        }
 925                    }
 926                }
 927            }
 928
 929            indent_sizes
 930        })
 931    }
 932
 933    fn apply_autoindents(
 934        &mut self,
 935        indent_sizes: BTreeMap<u32, IndentSize>,
 936        cx: &mut ModelContext<Self>,
 937    ) {
 938        self.autoindent_requests.clear();
 939
 940        let edits: Vec<_> = indent_sizes
 941            .into_iter()
 942            .filter_map(|(row, indent_size)| {
 943                let current_size = indent_size_for_line(self, row);
 944                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
 945            })
 946            .collect();
 947
 948        self.edit(edits, None, cx);
 949    }
 950
 951    pub fn edit_for_indent_size_adjustment(
 952        row: u32,
 953        current_size: IndentSize,
 954        new_size: IndentSize,
 955    ) -> Option<(Range<Point>, String)> {
 956        if new_size.kind != current_size.kind && current_size.len > 0 {
 957            return None;
 958        }
 959
 960        match new_size.len.cmp(&current_size.len) {
 961            Ordering::Greater => {
 962                let point = Point::new(row, 0);
 963                Some((
 964                    point..point,
 965                    iter::repeat(new_size.char())
 966                        .take((new_size.len - current_size.len) as usize)
 967                        .collect::<String>(),
 968                ))
 969            }
 970
 971            Ordering::Less => Some((
 972                Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
 973                String::new(),
 974            )),
 975
 976            Ordering::Equal => None,
 977        }
 978    }
 979
 980    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
 981        let old_text = self.as_rope().clone();
 982        let base_version = self.version();
 983        cx.background().spawn(async move {
 984            let old_text = old_text.to_string();
 985            let line_ending = LineEnding::detect(&new_text);
 986            LineEnding::normalize(&mut new_text);
 987            let changes = TextDiff::from_chars(old_text.as_str(), new_text.as_str())
 988                .iter_all_changes()
 989                .map(|c| (c.tag(), c.value().len()))
 990                .collect::<Vec<_>>();
 991            Diff {
 992                base_version,
 993                new_text: new_text.into(),
 994                changes,
 995                line_ending,
 996                start_offset: 0,
 997            }
 998        })
 999    }
1000
1001    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<&Transaction> {
1002        if self.version == diff.base_version {
1003            self.finalize_last_transaction();
1004            self.start_transaction();
1005            self.text.set_line_ending(diff.line_ending);
1006            let mut offset = diff.start_offset;
1007            for (tag, len) in diff.changes {
1008                let range = offset..(offset + len);
1009                match tag {
1010                    ChangeTag::Equal => offset += len,
1011                    ChangeTag::Delete => {
1012                        self.edit([(range, "")], None, cx);
1013                    }
1014                    ChangeTag::Insert => {
1015                        self.edit(
1016                            [(
1017                                offset..offset,
1018                                &diff.new_text[range.start - diff.start_offset
1019                                    ..range.end - diff.start_offset],
1020                            )],
1021                            None,
1022                            cx,
1023                        );
1024                        offset += len;
1025                    }
1026                }
1027            }
1028            if self.end_transaction(cx).is_some() {
1029                self.finalize_last_transaction()
1030            } else {
1031                None
1032            }
1033        } else {
1034            None
1035        }
1036    }
1037
1038    pub fn is_dirty(&self) -> bool {
1039        self.saved_version_fingerprint != self.as_rope().fingerprint()
1040            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1041    }
1042
1043    pub fn has_conflict(&self) -> bool {
1044        self.saved_version_fingerprint != self.as_rope().fingerprint()
1045            && self
1046                .file
1047                .as_ref()
1048                .map_or(false, |file| file.mtime() > self.saved_mtime)
1049    }
1050
1051    pub fn subscribe(&mut self) -> Subscription {
1052        self.text.subscribe()
1053    }
1054
1055    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1056        self.start_transaction_at(Instant::now())
1057    }
1058
1059    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1060        self.transaction_depth += 1;
1061        if self.was_dirty_before_starting_transaction.is_none() {
1062            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1063        }
1064        self.text.start_transaction_at(now)
1065    }
1066
1067    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1068        self.end_transaction_at(Instant::now(), cx)
1069    }
1070
1071    pub fn end_transaction_at(
1072        &mut self,
1073        now: Instant,
1074        cx: &mut ModelContext<Self>,
1075    ) -> Option<TransactionId> {
1076        assert!(self.transaction_depth > 0);
1077        self.transaction_depth -= 1;
1078        let was_dirty = if self.transaction_depth == 0 {
1079            self.was_dirty_before_starting_transaction.take().unwrap()
1080        } else {
1081            false
1082        };
1083        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1084            self.did_edit(&start_version, was_dirty, cx);
1085            Some(transaction_id)
1086        } else {
1087            None
1088        }
1089    }
1090
1091    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1092        self.text.push_transaction(transaction, now);
1093    }
1094
1095    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1096        self.text.finalize_last_transaction()
1097    }
1098
1099    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1100        self.text.group_until_transaction(transaction_id);
1101    }
1102
1103    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1104        self.text.forget_transaction(transaction_id);
1105    }
1106
1107    pub fn wait_for_edits(
1108        &mut self,
1109        edit_ids: impl IntoIterator<Item = clock::Local>,
1110    ) -> impl Future<Output = ()> {
1111        self.text.wait_for_edits(edit_ids)
1112    }
1113
1114    pub fn wait_for_anchors<'a>(
1115        &mut self,
1116        anchors: impl IntoIterator<Item = &'a Anchor>,
1117    ) -> impl Future<Output = ()> {
1118        self.text.wait_for_anchors(anchors)
1119    }
1120
1121    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1122        self.text.wait_for_version(version)
1123    }
1124
1125    pub fn set_active_selections(
1126        &mut self,
1127        selections: Arc<[Selection<Anchor>]>,
1128        line_mode: bool,
1129        cx: &mut ModelContext<Self>,
1130    ) {
1131        let lamport_timestamp = self.text.lamport_clock.tick();
1132        self.remote_selections.insert(
1133            self.text.replica_id(),
1134            SelectionSet {
1135                selections: selections.clone(),
1136                lamport_timestamp,
1137                line_mode,
1138            },
1139        );
1140        self.send_operation(
1141            Operation::UpdateSelections {
1142                selections,
1143                line_mode,
1144                lamport_timestamp,
1145            },
1146            cx,
1147        );
1148    }
1149
1150    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1151        self.set_active_selections(Arc::from([]), false, cx);
1152    }
1153
1154    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
1155    where
1156        T: Into<Arc<str>>,
1157    {
1158        self.edit([(0..self.len(), text)], None, cx)
1159    }
1160
1161    pub fn edit<I, S, T>(
1162        &mut self,
1163        edits_iter: I,
1164        autoindent_mode: Option<AutoindentMode>,
1165        cx: &mut ModelContext<Self>,
1166    ) -> Option<clock::Local>
1167    where
1168        I: IntoIterator<Item = (Range<S>, T)>,
1169        S: ToOffset,
1170        T: Into<Arc<str>>,
1171    {
1172        // Skip invalid edits and coalesce contiguous ones.
1173        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1174        for (range, new_text) in edits_iter {
1175            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1176            if range.start > range.end {
1177                mem::swap(&mut range.start, &mut range.end);
1178            }
1179            let new_text = new_text.into();
1180            if !new_text.is_empty() || !range.is_empty() {
1181                if let Some((prev_range, prev_text)) = edits.last_mut() {
1182                    if prev_range.end >= range.start {
1183                        prev_range.end = cmp::max(prev_range.end, range.end);
1184                        *prev_text = format!("{prev_text}{new_text}").into();
1185                    } else {
1186                        edits.push((range, new_text));
1187                    }
1188                } else {
1189                    edits.push((range, new_text));
1190                }
1191            }
1192        }
1193        if edits.is_empty() {
1194            return None;
1195        }
1196
1197        self.start_transaction();
1198        self.pending_autoindent.take();
1199        let autoindent_request = autoindent_mode
1200            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1201
1202        let edit_operation = self.text.edit(edits.iter().cloned());
1203        let edit_id = edit_operation.local_timestamp();
1204
1205        if let Some((before_edit, mode)) = autoindent_request {
1206            let indent_size = before_edit.single_indent_size(cx);
1207            let (start_columns, is_block_mode) = match mode {
1208                AutoindentMode::Block {
1209                    original_indent_columns: start_columns,
1210                } => (start_columns, true),
1211                AutoindentMode::EachLine => (Default::default(), false),
1212            };
1213
1214            let mut delta = 0isize;
1215            let entries = edits
1216                .into_iter()
1217                .enumerate()
1218                .zip(&edit_operation.as_edit().unwrap().new_text)
1219                .map(|((ix, (range, _)), new_text)| {
1220                    let new_text_len = new_text.len();
1221                    let old_start = range.start.to_point(&before_edit);
1222                    let new_start = (delta + range.start as isize) as usize;
1223                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1224
1225                    let mut range_of_insertion_to_indent = 0..new_text_len;
1226                    let mut first_line_is_new = false;
1227                    let mut start_column = None;
1228
1229                    // When inserting an entire line at the beginning of an existing line,
1230                    // treat the insertion as new.
1231                    if new_text.contains('\n')
1232                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1233                    {
1234                        first_line_is_new = true;
1235                    }
1236
1237                    // When inserting text starting with a newline, avoid auto-indenting the
1238                    // previous line.
1239                    if new_text[range_of_insertion_to_indent.clone()].starts_with('\n') {
1240                        range_of_insertion_to_indent.start += 1;
1241                        first_line_is_new = true;
1242                    }
1243
1244                    // Avoid auto-indenting after the insertion.
1245                    if is_block_mode {
1246                        start_column = start_columns.get(ix).copied();
1247                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1248                            range_of_insertion_to_indent.end -= 1;
1249                        }
1250                    }
1251
1252                    AutoindentRequestEntry {
1253                        first_line_is_new,
1254                        original_indent_column: start_column,
1255                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1256                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1257                    }
1258                })
1259                .collect();
1260
1261            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1262                before_edit,
1263                entries,
1264                indent_size,
1265                is_block_mode,
1266            }));
1267        }
1268
1269        self.end_transaction(cx);
1270        self.send_operation(Operation::Buffer(edit_operation), cx);
1271        Some(edit_id)
1272    }
1273
1274    fn did_edit(
1275        &mut self,
1276        old_version: &clock::Global,
1277        was_dirty: bool,
1278        cx: &mut ModelContext<Self>,
1279    ) {
1280        if self.edits_since::<usize>(old_version).next().is_none() {
1281            return;
1282        }
1283
1284        self.reparse(cx);
1285
1286        cx.emit(Event::Edited);
1287        if was_dirty != self.is_dirty() {
1288            cx.emit(Event::DirtyChanged);
1289        }
1290        cx.notify();
1291    }
1292
1293    fn grammar(&self) -> Option<&Arc<Grammar>> {
1294        self.language.as_ref().and_then(|l| l.grammar.as_ref())
1295    }
1296
1297    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1298        &mut self,
1299        ops: I,
1300        cx: &mut ModelContext<Self>,
1301    ) -> Result<()> {
1302        self.pending_autoindent.take();
1303        let was_dirty = self.is_dirty();
1304        let old_version = self.version.clone();
1305        let mut deferred_ops = Vec::new();
1306        let buffer_ops = ops
1307            .into_iter()
1308            .filter_map(|op| match op {
1309                Operation::Buffer(op) => Some(op),
1310                _ => {
1311                    if self.can_apply_op(&op) {
1312                        self.apply_op(op, cx);
1313                    } else {
1314                        deferred_ops.push(op);
1315                    }
1316                    None
1317                }
1318            })
1319            .collect::<Vec<_>>();
1320        self.text.apply_ops(buffer_ops)?;
1321        self.deferred_ops.insert(deferred_ops);
1322        self.flush_deferred_ops(cx);
1323        self.did_edit(&old_version, was_dirty, cx);
1324        // Notify independently of whether the buffer was edited as the operations could include a
1325        // selection update.
1326        cx.notify();
1327        Ok(())
1328    }
1329
1330    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1331        let mut deferred_ops = Vec::new();
1332        for op in self.deferred_ops.drain().iter().cloned() {
1333            if self.can_apply_op(&op) {
1334                self.apply_op(op, cx);
1335            } else {
1336                deferred_ops.push(op);
1337            }
1338        }
1339        self.deferred_ops.insert(deferred_ops);
1340    }
1341
1342    fn can_apply_op(&self, operation: &Operation) -> bool {
1343        match operation {
1344            Operation::Buffer(_) => {
1345                unreachable!("buffer operations should never be applied at this layer")
1346            }
1347            Operation::UpdateDiagnostics {
1348                diagnostics: diagnostic_set,
1349                ..
1350            } => diagnostic_set.iter().all(|diagnostic| {
1351                self.text.can_resolve(&diagnostic.range.start)
1352                    && self.text.can_resolve(&diagnostic.range.end)
1353            }),
1354            Operation::UpdateSelections { selections, .. } => selections
1355                .iter()
1356                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1357            Operation::UpdateCompletionTriggers { .. } => true,
1358        }
1359    }
1360
1361    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1362        match operation {
1363            Operation::Buffer(_) => {
1364                unreachable!("buffer operations should never be applied at this layer")
1365            }
1366            Operation::UpdateDiagnostics {
1367                diagnostics: diagnostic_set,
1368                lamport_timestamp,
1369            } => {
1370                let snapshot = self.snapshot();
1371                self.apply_diagnostic_update(
1372                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1373                    lamport_timestamp,
1374                    cx,
1375                );
1376            }
1377            Operation::UpdateSelections {
1378                selections,
1379                lamport_timestamp,
1380                line_mode,
1381            } => {
1382                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1383                    if set.lamport_timestamp > lamport_timestamp {
1384                        return;
1385                    }
1386                }
1387
1388                self.remote_selections.insert(
1389                    lamport_timestamp.replica_id,
1390                    SelectionSet {
1391                        selections,
1392                        lamport_timestamp,
1393                        line_mode,
1394                    },
1395                );
1396                self.text.lamport_clock.observe(lamport_timestamp);
1397                self.selections_update_count += 1;
1398            }
1399            Operation::UpdateCompletionTriggers {
1400                triggers,
1401                lamport_timestamp,
1402            } => {
1403                self.completion_triggers = triggers;
1404                self.text.lamport_clock.observe(lamport_timestamp);
1405            }
1406        }
1407    }
1408
1409    fn apply_diagnostic_update(
1410        &mut self,
1411        diagnostics: DiagnosticSet,
1412        lamport_timestamp: clock::Lamport,
1413        cx: &mut ModelContext<Self>,
1414    ) {
1415        if lamport_timestamp > self.diagnostics_timestamp {
1416            self.diagnostics = diagnostics;
1417            self.diagnostics_timestamp = lamport_timestamp;
1418            self.diagnostics_update_count += 1;
1419            self.text.lamport_clock.observe(lamport_timestamp);
1420            cx.notify();
1421            cx.emit(Event::DiagnosticsUpdated);
1422        }
1423    }
1424
1425    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1426        cx.emit(Event::Operation(operation));
1427    }
1428
1429    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1430        self.remote_selections.remove(&replica_id);
1431        cx.notify();
1432    }
1433
1434    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1435        let was_dirty = self.is_dirty();
1436        let old_version = self.version.clone();
1437
1438        if let Some((transaction_id, operation)) = self.text.undo() {
1439            self.send_operation(Operation::Buffer(operation), cx);
1440            self.did_edit(&old_version, was_dirty, cx);
1441            Some(transaction_id)
1442        } else {
1443            None
1444        }
1445    }
1446
1447    pub fn undo_to_transaction(
1448        &mut self,
1449        transaction_id: TransactionId,
1450        cx: &mut ModelContext<Self>,
1451    ) -> bool {
1452        let was_dirty = self.is_dirty();
1453        let old_version = self.version.clone();
1454
1455        let operations = self.text.undo_to_transaction(transaction_id);
1456        let undone = !operations.is_empty();
1457        for operation in operations {
1458            self.send_operation(Operation::Buffer(operation), cx);
1459        }
1460        if undone {
1461            self.did_edit(&old_version, was_dirty, cx)
1462        }
1463        undone
1464    }
1465
1466    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1467        let was_dirty = self.is_dirty();
1468        let old_version = self.version.clone();
1469
1470        if let Some((transaction_id, operation)) = self.text.redo() {
1471            self.send_operation(Operation::Buffer(operation), cx);
1472            self.did_edit(&old_version, was_dirty, cx);
1473            Some(transaction_id)
1474        } else {
1475            None
1476        }
1477    }
1478
1479    pub fn redo_to_transaction(
1480        &mut self,
1481        transaction_id: TransactionId,
1482        cx: &mut ModelContext<Self>,
1483    ) -> bool {
1484        let was_dirty = self.is_dirty();
1485        let old_version = self.version.clone();
1486
1487        let operations = self.text.redo_to_transaction(transaction_id);
1488        let redone = !operations.is_empty();
1489        for operation in operations {
1490            self.send_operation(Operation::Buffer(operation), cx);
1491        }
1492        if redone {
1493            self.did_edit(&old_version, was_dirty, cx)
1494        }
1495        redone
1496    }
1497
1498    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1499        self.completion_triggers = triggers.clone();
1500        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1501        self.send_operation(
1502            Operation::UpdateCompletionTriggers {
1503                triggers,
1504                lamport_timestamp: self.completion_triggers_timestamp,
1505            },
1506            cx,
1507        );
1508        cx.notify();
1509    }
1510
1511    pub fn completion_triggers(&self) -> &[String] {
1512        &self.completion_triggers
1513    }
1514}
1515
1516#[cfg(any(test, feature = "test-support"))]
1517impl Buffer {
1518    pub fn set_group_interval(&mut self, group_interval: Duration) {
1519        self.text.set_group_interval(group_interval);
1520    }
1521
1522    pub fn randomly_edit<T>(
1523        &mut self,
1524        rng: &mut T,
1525        old_range_count: usize,
1526        cx: &mut ModelContext<Self>,
1527    ) where
1528        T: rand::Rng,
1529    {
1530        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1531        let mut last_end = None;
1532        for _ in 0..old_range_count {
1533            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1534                break;
1535            }
1536
1537            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1538            let mut range = self.random_byte_range(new_start, rng);
1539            if rng.gen_bool(0.2) {
1540                mem::swap(&mut range.start, &mut range.end);
1541            }
1542            last_end = Some(range.end);
1543
1544            let new_text_len = rng.gen_range(0..10);
1545            let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1546                .take(new_text_len)
1547                .collect();
1548
1549            edits.push((range, new_text));
1550        }
1551        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1552        self.edit(edits, None, cx);
1553    }
1554
1555    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1556        let was_dirty = self.is_dirty();
1557        let old_version = self.version.clone();
1558
1559        let ops = self.text.randomly_undo_redo(rng);
1560        if !ops.is_empty() {
1561            for op in ops {
1562                self.send_operation(Operation::Buffer(op), cx);
1563                self.did_edit(&old_version, was_dirty, cx);
1564            }
1565        }
1566    }
1567}
1568
1569impl Entity for Buffer {
1570    type Event = Event;
1571}
1572
1573impl Deref for Buffer {
1574    type Target = TextBuffer;
1575
1576    fn deref(&self) -> &Self::Target {
1577        &self.text
1578    }
1579}
1580
1581impl BufferSnapshot {
1582    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1583        indent_size_for_line(self, row)
1584    }
1585
1586    pub fn single_indent_size(&self, cx: &AppContext) -> IndentSize {
1587        let language_name = self.language().map(|language| language.name());
1588        let settings = cx.global::<Settings>();
1589        if settings.hard_tabs(language_name.as_deref()) {
1590            IndentSize::tab()
1591        } else {
1592            IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
1593        }
1594    }
1595
1596    pub fn suggested_indents(
1597        &self,
1598        rows: impl Iterator<Item = u32>,
1599        single_indent_size: IndentSize,
1600    ) -> BTreeMap<u32, IndentSize> {
1601        let mut result = BTreeMap::new();
1602
1603        for row_range in contiguous_ranges(rows, 10) {
1604            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1605                Some(suggestions) => suggestions,
1606                _ => break,
1607            };
1608
1609            for (row, suggestion) in row_range.zip(suggestions) {
1610                let indent_size = if let Some(suggestion) = suggestion {
1611                    result
1612                        .get(&suggestion.basis_row)
1613                        .copied()
1614                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1615                        .with_delta(suggestion.delta, single_indent_size)
1616                } else {
1617                    self.indent_size_for_line(row)
1618                };
1619
1620                result.insert(row, indent_size);
1621            }
1622        }
1623
1624        result
1625    }
1626
1627    fn suggest_autoindents(
1628        &self,
1629        row_range: Range<u32>,
1630    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1631        let language = self.language.as_ref()?;
1632        let grammar = language.grammar.as_ref()?;
1633        let config = &language.config;
1634        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1635
1636        // Find the suggested indentation ranges based on the syntax tree.
1637        let indents_query = grammar.indents_query.as_ref()?;
1638        let mut query_cursor = QueryCursorHandle::new();
1639        let indent_capture_ix = indents_query.capture_index_for_name("indent");
1640        let end_capture_ix = indents_query.capture_index_for_name("end");
1641        query_cursor.set_point_range(
1642            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1643                ..Point::new(row_range.end, 0).to_ts_point(),
1644        );
1645
1646        let mut indent_ranges = Vec::<Range<Point>>::new();
1647        for mat in query_cursor.matches(
1648            indents_query,
1649            self.tree.as_ref()?.root_node(),
1650            TextProvider(self.as_rope()),
1651        ) {
1652            let mut start: Option<Point> = None;
1653            let mut end: Option<Point> = None;
1654            for capture in mat.captures {
1655                if Some(capture.index) == indent_capture_ix {
1656                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1657                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1658                } else if Some(capture.index) == end_capture_ix {
1659                    end = Some(Point::from_ts_point(capture.node.start_position()));
1660                }
1661            }
1662
1663            if let Some((start, end)) = start.zip(end) {
1664                if start.row == end.row {
1665                    continue;
1666                }
1667
1668                let range = start..end;
1669                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1670                    Err(ix) => indent_ranges.insert(ix, range),
1671                    Ok(ix) => {
1672                        let prev_range = &mut indent_ranges[ix];
1673                        prev_range.end = prev_range.end.max(range.end);
1674                    }
1675                }
1676            }
1677        }
1678
1679        // Find the suggested indentation increases and decreased based on regexes.
1680        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1681        self.for_each_line(
1682            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1683                ..Point::new(row_range.end, 0),
1684            |row, line| {
1685                if config
1686                    .decrease_indent_pattern
1687                    .as_ref()
1688                    .map_or(false, |regex| regex.is_match(line))
1689                {
1690                    indent_change_rows.push((row, Ordering::Less));
1691                }
1692                if config
1693                    .increase_indent_pattern
1694                    .as_ref()
1695                    .map_or(false, |regex| regex.is_match(line))
1696                {
1697                    indent_change_rows.push((row + 1, Ordering::Greater));
1698                }
1699            },
1700        );
1701
1702        let mut indent_changes = indent_change_rows.into_iter().peekable();
1703        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1704            prev_non_blank_row.unwrap_or(0)
1705        } else {
1706            row_range.start.saturating_sub(1)
1707        };
1708        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1709        Some(row_range.map(move |row| {
1710            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1711
1712            let mut indent_from_prev_row = false;
1713            let mut outdent_from_prev_row = false;
1714            let mut outdent_to_row = u32::MAX;
1715
1716            while let Some((indent_row, delta)) = indent_changes.peek() {
1717                match indent_row.cmp(&row) {
1718                    Ordering::Equal => match delta {
1719                        Ordering::Less => outdent_from_prev_row = true,
1720                        Ordering::Greater => indent_from_prev_row = true,
1721                        _ => {}
1722                    },
1723
1724                    Ordering::Greater => break,
1725                    Ordering::Less => {}
1726                }
1727
1728                indent_changes.next();
1729            }
1730
1731            for range in &indent_ranges {
1732                if range.start.row >= row {
1733                    break;
1734                }
1735                if range.start.row == prev_row && range.end > row_start {
1736                    indent_from_prev_row = true;
1737                }
1738                if range.end > prev_row_start && range.end <= row_start {
1739                    outdent_to_row = outdent_to_row.min(range.start.row);
1740                }
1741            }
1742
1743            let suggestion = if outdent_to_row == prev_row
1744                || (outdent_from_prev_row && indent_from_prev_row)
1745            {
1746                Some(IndentSuggestion {
1747                    basis_row: prev_row,
1748                    delta: Ordering::Equal,
1749                })
1750            } else if indent_from_prev_row {
1751                Some(IndentSuggestion {
1752                    basis_row: prev_row,
1753                    delta: Ordering::Greater,
1754                })
1755            } else if outdent_to_row < prev_row {
1756                Some(IndentSuggestion {
1757                    basis_row: outdent_to_row,
1758                    delta: Ordering::Equal,
1759                })
1760            } else if outdent_from_prev_row {
1761                Some(IndentSuggestion {
1762                    basis_row: prev_row,
1763                    delta: Ordering::Less,
1764                })
1765            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
1766            {
1767                Some(IndentSuggestion {
1768                    basis_row: prev_row,
1769                    delta: Ordering::Equal,
1770                })
1771            } else {
1772                None
1773            };
1774
1775            prev_row = row;
1776            prev_row_start = row_start;
1777            suggestion
1778        }))
1779    }
1780
1781    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1782        while row > 0 {
1783            row -= 1;
1784            if !self.is_line_blank(row) {
1785                return Some(row);
1786            }
1787        }
1788        None
1789    }
1790
1791    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
1792        let range = range.start.to_offset(self)..range.end.to_offset(self);
1793
1794        let mut tree = None;
1795        let mut diagnostic_endpoints = Vec::new();
1796        if language_aware {
1797            tree = self.tree.as_ref();
1798            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1799                diagnostic_endpoints.push(DiagnosticEndpoint {
1800                    offset: entry.range.start,
1801                    is_start: true,
1802                    severity: entry.diagnostic.severity,
1803                    is_unnecessary: entry.diagnostic.is_unnecessary,
1804                });
1805                diagnostic_endpoints.push(DiagnosticEndpoint {
1806                    offset: entry.range.end,
1807                    is_start: false,
1808                    severity: entry.diagnostic.severity,
1809                    is_unnecessary: entry.diagnostic.is_unnecessary,
1810                });
1811            }
1812            diagnostic_endpoints
1813                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1814        }
1815
1816        BufferChunks::new(
1817            self.text.as_rope(),
1818            range,
1819            tree,
1820            self.grammar(),
1821            diagnostic_endpoints,
1822        )
1823    }
1824
1825    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
1826        let mut line = String::new();
1827        let mut row = range.start.row;
1828        for chunk in self
1829            .as_rope()
1830            .chunks_in_range(range.to_offset(self))
1831            .chain(["\n"])
1832        {
1833            for (newline_ix, text) in chunk.split('\n').enumerate() {
1834                if newline_ix > 0 {
1835                    callback(row, &line);
1836                    row += 1;
1837                    line.clear();
1838                }
1839                line.push_str(text);
1840            }
1841        }
1842    }
1843
1844    pub fn language(&self) -> Option<&Arc<Language>> {
1845        self.language.as_ref()
1846    }
1847
1848    fn grammar(&self) -> Option<&Arc<Grammar>> {
1849        self.language
1850            .as_ref()
1851            .and_then(|language| language.grammar.as_ref())
1852    }
1853
1854    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
1855        let mut start = start.to_offset(self);
1856        let mut end = start;
1857        let mut next_chars = self.chars_at(start).peekable();
1858        let mut prev_chars = self.reversed_chars_at(start).peekable();
1859        let word_kind = cmp::max(
1860            prev_chars.peek().copied().map(char_kind),
1861            next_chars.peek().copied().map(char_kind),
1862        );
1863
1864        for ch in prev_chars {
1865            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1866                start -= ch.len_utf8();
1867            } else {
1868                break;
1869            }
1870        }
1871
1872        for ch in next_chars {
1873            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1874                end += ch.len_utf8();
1875            } else {
1876                break;
1877            }
1878        }
1879
1880        (start..end, word_kind)
1881    }
1882
1883    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1884        let tree = self.tree.as_ref()?;
1885        let range = range.start.to_offset(self)..range.end.to_offset(self);
1886        let mut cursor = tree.root_node().walk();
1887
1888        // Descend to the first leaf that touches the start of the range,
1889        // and if the range is non-empty, extends beyond the start.
1890        while cursor.goto_first_child_for_byte(range.start).is_some() {
1891            if !range.is_empty() && cursor.node().end_byte() == range.start {
1892                cursor.goto_next_sibling();
1893            }
1894        }
1895
1896        // Ascend to the smallest ancestor that strictly contains the range.
1897        loop {
1898            let node_range = cursor.node().byte_range();
1899            if node_range.start <= range.start
1900                && node_range.end >= range.end
1901                && node_range.len() > range.len()
1902            {
1903                break;
1904            }
1905            if !cursor.goto_parent() {
1906                break;
1907            }
1908        }
1909
1910        let left_node = cursor.node();
1911
1912        // For an empty range, try to find another node immediately to the right of the range.
1913        if left_node.end_byte() == range.start {
1914            let mut right_node = None;
1915            while !cursor.goto_next_sibling() {
1916                if !cursor.goto_parent() {
1917                    break;
1918                }
1919            }
1920
1921            while cursor.node().start_byte() == range.start {
1922                right_node = Some(cursor.node());
1923                if !cursor.goto_first_child() {
1924                    break;
1925                }
1926            }
1927
1928            // If there is a candidate node on both sides of the (empty) range, then
1929            // decide between the two by favoring a named node over an anonymous token.
1930            // If both nodes are the same in that regard, favor the right one.
1931            if let Some(right_node) = right_node {
1932                if right_node.is_named() || !left_node.is_named() {
1933                    return Some(right_node.byte_range());
1934                }
1935            }
1936        }
1937
1938        Some(left_node.byte_range())
1939    }
1940
1941    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1942        self.outline_items_containing(0..self.len(), theme)
1943            .map(Outline::new)
1944    }
1945
1946    pub fn symbols_containing<T: ToOffset>(
1947        &self,
1948        position: T,
1949        theme: Option<&SyntaxTheme>,
1950    ) -> Option<Vec<OutlineItem<Anchor>>> {
1951        let position = position.to_offset(self);
1952        let mut items =
1953            self.outline_items_containing(position.saturating_sub(1)..position + 1, theme)?;
1954        let mut prev_depth = None;
1955        items.retain(|item| {
1956            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
1957            prev_depth = Some(item.depth);
1958            result
1959        });
1960        Some(items)
1961    }
1962
1963    fn outline_items_containing(
1964        &self,
1965        range: Range<usize>,
1966        theme: Option<&SyntaxTheme>,
1967    ) -> Option<Vec<OutlineItem<Anchor>>> {
1968        let tree = self.tree.as_ref()?;
1969        let grammar = self
1970            .language
1971            .as_ref()
1972            .and_then(|language| language.grammar.as_ref())?;
1973
1974        let outline_query = grammar.outline_query.as_ref()?;
1975        let mut cursor = QueryCursorHandle::new();
1976        cursor.set_byte_range(range.clone());
1977        let matches = cursor.matches(
1978            outline_query,
1979            tree.root_node(),
1980            TextProvider(self.as_rope()),
1981        );
1982
1983        let mut chunks = self.chunks(0..self.len(), true);
1984
1985        let item_capture_ix = outline_query.capture_index_for_name("item")?;
1986        let name_capture_ix = outline_query.capture_index_for_name("name")?;
1987        let context_capture_ix = outline_query
1988            .capture_index_for_name("context")
1989            .unwrap_or(u32::MAX);
1990
1991        let mut stack = Vec::<Range<usize>>::new();
1992        let items = matches
1993            .filter_map(|mat| {
1994                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1995                let item_range = item_node.start_byte()..item_node.end_byte();
1996                if item_range.end < range.start || item_range.start > range.end {
1997                    return None;
1998                }
1999                let mut text = String::new();
2000                let mut name_ranges = Vec::new();
2001                let mut highlight_ranges = Vec::new();
2002
2003                for capture in mat.captures {
2004                    let node_is_name;
2005                    if capture.index == name_capture_ix {
2006                        node_is_name = true;
2007                    } else if capture.index == context_capture_ix {
2008                        node_is_name = false;
2009                    } else {
2010                        continue;
2011                    }
2012
2013                    let range = capture.node.start_byte()..capture.node.end_byte();
2014                    if !text.is_empty() {
2015                        text.push(' ');
2016                    }
2017                    if node_is_name {
2018                        let mut start = text.len();
2019                        let end = start + range.len();
2020
2021                        // When multiple names are captured, then the matcheable text
2022                        // includes the whitespace in between the names.
2023                        if !name_ranges.is_empty() {
2024                            start -= 1;
2025                        }
2026
2027                        name_ranges.push(start..end);
2028                    }
2029
2030                    let mut offset = range.start;
2031                    chunks.seek(offset);
2032                    for mut chunk in chunks.by_ref() {
2033                        if chunk.text.len() > range.end - offset {
2034                            chunk.text = &chunk.text[0..(range.end - offset)];
2035                            offset = range.end;
2036                        } else {
2037                            offset += chunk.text.len();
2038                        }
2039                        let style = chunk
2040                            .syntax_highlight_id
2041                            .zip(theme)
2042                            .and_then(|(highlight, theme)| highlight.style(theme));
2043                        if let Some(style) = style {
2044                            let start = text.len();
2045                            let end = start + chunk.text.len();
2046                            highlight_ranges.push((start..end, style));
2047                        }
2048                        text.push_str(chunk.text);
2049                        if offset >= range.end {
2050                            break;
2051                        }
2052                    }
2053                }
2054
2055                while stack.last().map_or(false, |prev_range| {
2056                    prev_range.start > item_range.start || prev_range.end < item_range.end
2057                }) {
2058                    stack.pop();
2059                }
2060                stack.push(item_range.clone());
2061
2062                Some(OutlineItem {
2063                    depth: stack.len() - 1,
2064                    range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2065                    text,
2066                    highlight_ranges,
2067                    name_ranges,
2068                })
2069            })
2070            .collect::<Vec<_>>();
2071        Some(items)
2072    }
2073
2074    pub fn enclosing_bracket_ranges<T: ToOffset>(
2075        &self,
2076        range: Range<T>,
2077    ) -> Option<(Range<usize>, Range<usize>)> {
2078        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
2079        let brackets_query = grammar.brackets_query.as_ref()?;
2080        let open_capture_ix = brackets_query.capture_index_for_name("open")?;
2081        let close_capture_ix = brackets_query.capture_index_for_name("close")?;
2082
2083        // Find bracket pairs that *inclusively* contain the given range.
2084        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
2085        let mut cursor = QueryCursorHandle::new();
2086        let matches = cursor.set_byte_range(range).matches(
2087            brackets_query,
2088            tree.root_node(),
2089            TextProvider(self.as_rope()),
2090        );
2091
2092        // Get the ranges of the innermost pair of brackets.
2093        matches
2094            .filter_map(|mat| {
2095                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
2096                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
2097                Some((open.byte_range(), close.byte_range()))
2098            })
2099            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
2100    }
2101
2102    #[allow(clippy::type_complexity)]
2103    pub fn remote_selections_in_range(
2104        &self,
2105        range: Range<Anchor>,
2106    ) -> impl Iterator<
2107        Item = (
2108            ReplicaId,
2109            bool,
2110            impl Iterator<Item = &Selection<Anchor>> + '_,
2111        ),
2112    > + '_ {
2113        self.remote_selections
2114            .iter()
2115            .filter(|(replica_id, set)| {
2116                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2117            })
2118            .map(move |(replica_id, set)| {
2119                let start_ix = match set.selections.binary_search_by(|probe| {
2120                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2121                }) {
2122                    Ok(ix) | Err(ix) => ix,
2123                };
2124                let end_ix = match set.selections.binary_search_by(|probe| {
2125                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2126                }) {
2127                    Ok(ix) | Err(ix) => ix,
2128                };
2129
2130                (
2131                    *replica_id,
2132                    set.line_mode,
2133                    set.selections[start_ix..end_ix].iter(),
2134                )
2135            })
2136    }
2137
2138    pub fn diagnostics_in_range<'a, T, O>(
2139        &'a self,
2140        search_range: Range<T>,
2141        reversed: bool,
2142    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2143    where
2144        T: 'a + Clone + ToOffset,
2145        O: 'a + FromAnchor,
2146    {
2147        self.diagnostics.range(search_range, self, true, reversed)
2148    }
2149
2150    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2151        let mut groups = Vec::new();
2152        self.diagnostics.groups(&mut groups, self);
2153        groups
2154    }
2155
2156    pub fn diagnostic_group<'a, O>(
2157        &'a self,
2158        group_id: usize,
2159    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2160    where
2161        O: 'a + FromAnchor,
2162    {
2163        self.diagnostics.group(group_id, self)
2164    }
2165
2166    pub fn diagnostics_update_count(&self) -> usize {
2167        self.diagnostics_update_count
2168    }
2169
2170    pub fn parse_count(&self) -> usize {
2171        self.parse_count
2172    }
2173
2174    pub fn selections_update_count(&self) -> usize {
2175        self.selections_update_count
2176    }
2177
2178    pub fn file(&self) -> Option<&dyn File> {
2179        self.file.as_deref()
2180    }
2181
2182    pub fn file_update_count(&self) -> usize {
2183        self.file_update_count
2184    }
2185}
2186
2187pub fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2188    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2189}
2190
2191pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2192    let mut result = IndentSize::spaces(0);
2193    for c in text {
2194        let kind = match c {
2195            ' ' => IndentKind::Space,
2196            '\t' => IndentKind::Tab,
2197            _ => break,
2198        };
2199        if result.len == 0 {
2200            result.kind = kind;
2201        }
2202        result.len += 1;
2203    }
2204    result
2205}
2206
2207impl Clone for BufferSnapshot {
2208    fn clone(&self) -> Self {
2209        Self {
2210            text: self.text.clone(),
2211            tree: self.tree.clone(),
2212            file: self.file.clone(),
2213            remote_selections: self.remote_selections.clone(),
2214            diagnostics: self.diagnostics.clone(),
2215            selections_update_count: self.selections_update_count,
2216            diagnostics_update_count: self.diagnostics_update_count,
2217            file_update_count: self.file_update_count,
2218            language: self.language.clone(),
2219            parse_count: self.parse_count,
2220        }
2221    }
2222}
2223
2224impl Deref for BufferSnapshot {
2225    type Target = text::BufferSnapshot;
2226
2227    fn deref(&self) -> &Self::Target {
2228        &self.text
2229    }
2230}
2231
2232impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
2233    type I = ByteChunks<'a>;
2234
2235    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
2236        ByteChunks(self.0.chunks_in_range(node.byte_range()))
2237    }
2238}
2239
2240pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
2241
2242impl<'a> Iterator for ByteChunks<'a> {
2243    type Item = &'a [u8];
2244
2245    fn next(&mut self) -> Option<Self::Item> {
2246        self.0.next().map(str::as_bytes)
2247    }
2248}
2249
2250unsafe impl<'a> Send for BufferChunks<'a> {}
2251
2252impl<'a> BufferChunks<'a> {
2253    pub(crate) fn new(
2254        text: &'a Rope,
2255        range: Range<usize>,
2256        tree: Option<&'a Tree>,
2257        grammar: Option<&'a Arc<Grammar>>,
2258        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2259    ) -> Self {
2260        let mut highlights = None;
2261        if let Some((grammar, tree)) = grammar.zip(tree) {
2262            if let Some(highlights_query) = grammar.highlights_query.as_ref() {
2263                let mut query_cursor = QueryCursorHandle::new();
2264
2265                // TODO - add a Tree-sitter API to remove the need for this.
2266                let cursor = unsafe {
2267                    std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
2268                };
2269                let captures = cursor.set_byte_range(range.clone()).captures(
2270                    highlights_query,
2271                    tree.root_node(),
2272                    TextProvider(text),
2273                );
2274                highlights = Some(BufferChunkHighlights {
2275                    captures,
2276                    next_capture: None,
2277                    stack: Default::default(),
2278                    highlight_map: grammar.highlight_map(),
2279                    _query_cursor: query_cursor,
2280                })
2281            }
2282        }
2283
2284        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2285        let chunks = text.chunks_in_range(range.clone());
2286
2287        BufferChunks {
2288            range,
2289            chunks,
2290            diagnostic_endpoints,
2291            error_depth: 0,
2292            warning_depth: 0,
2293            information_depth: 0,
2294            hint_depth: 0,
2295            unnecessary_depth: 0,
2296            highlights,
2297        }
2298    }
2299
2300    pub fn seek(&mut self, offset: usize) {
2301        self.range.start = offset;
2302        self.chunks.seek(self.range.start);
2303        if let Some(highlights) = self.highlights.as_mut() {
2304            highlights
2305                .stack
2306                .retain(|(end_offset, _)| *end_offset > offset);
2307            if let Some((mat, capture_ix)) = &highlights.next_capture {
2308                let capture = mat.captures[*capture_ix as usize];
2309                if offset >= capture.node.start_byte() {
2310                    let next_capture_end = capture.node.end_byte();
2311                    if offset < next_capture_end {
2312                        highlights.stack.push((
2313                            next_capture_end,
2314                            highlights.highlight_map.get(capture.index),
2315                        ));
2316                    }
2317                    highlights.next_capture.take();
2318                }
2319            }
2320            highlights.captures.set_byte_range(self.range.clone());
2321        }
2322    }
2323
2324    pub fn offset(&self) -> usize {
2325        self.range.start
2326    }
2327
2328    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2329        let depth = match endpoint.severity {
2330            DiagnosticSeverity::ERROR => &mut self.error_depth,
2331            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2332            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2333            DiagnosticSeverity::HINT => &mut self.hint_depth,
2334            _ => return,
2335        };
2336        if endpoint.is_start {
2337            *depth += 1;
2338        } else {
2339            *depth -= 1;
2340        }
2341
2342        if endpoint.is_unnecessary {
2343            if endpoint.is_start {
2344                self.unnecessary_depth += 1;
2345            } else {
2346                self.unnecessary_depth -= 1;
2347            }
2348        }
2349    }
2350
2351    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2352        if self.error_depth > 0 {
2353            Some(DiagnosticSeverity::ERROR)
2354        } else if self.warning_depth > 0 {
2355            Some(DiagnosticSeverity::WARNING)
2356        } else if self.information_depth > 0 {
2357            Some(DiagnosticSeverity::INFORMATION)
2358        } else if self.hint_depth > 0 {
2359            Some(DiagnosticSeverity::HINT)
2360        } else {
2361            None
2362        }
2363    }
2364
2365    fn current_code_is_unnecessary(&self) -> bool {
2366        self.unnecessary_depth > 0
2367    }
2368}
2369
2370impl<'a> Iterator for BufferChunks<'a> {
2371    type Item = Chunk<'a>;
2372
2373    fn next(&mut self) -> Option<Self::Item> {
2374        let mut next_capture_start = usize::MAX;
2375        let mut next_diagnostic_endpoint = usize::MAX;
2376
2377        if let Some(highlights) = self.highlights.as_mut() {
2378            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2379                if *parent_capture_end <= self.range.start {
2380                    highlights.stack.pop();
2381                } else {
2382                    break;
2383                }
2384            }
2385
2386            if highlights.next_capture.is_none() {
2387                highlights.next_capture = highlights.captures.next();
2388            }
2389
2390            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2391                let capture = mat.captures[*capture_ix as usize];
2392                if self.range.start < capture.node.start_byte() {
2393                    next_capture_start = capture.node.start_byte();
2394                    break;
2395                } else {
2396                    let highlight_id = highlights.highlight_map.get(capture.index);
2397                    highlights
2398                        .stack
2399                        .push((capture.node.end_byte(), highlight_id));
2400                    highlights.next_capture = highlights.captures.next();
2401                }
2402            }
2403        }
2404
2405        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2406            if endpoint.offset <= self.range.start {
2407                self.update_diagnostic_depths(endpoint);
2408                self.diagnostic_endpoints.next();
2409            } else {
2410                next_diagnostic_endpoint = endpoint.offset;
2411                break;
2412            }
2413        }
2414
2415        if let Some(chunk) = self.chunks.peek() {
2416            let chunk_start = self.range.start;
2417            let mut chunk_end = (self.chunks.offset() + chunk.len())
2418                .min(next_capture_start)
2419                .min(next_diagnostic_endpoint);
2420            let mut highlight_id = None;
2421            if let Some(highlights) = self.highlights.as_ref() {
2422                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2423                    chunk_end = chunk_end.min(*parent_capture_end);
2424                    highlight_id = Some(*parent_highlight_id);
2425                }
2426            }
2427
2428            let slice =
2429                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2430            self.range.start = chunk_end;
2431            if self.range.start == self.chunks.offset() + chunk.len() {
2432                self.chunks.next().unwrap();
2433            }
2434
2435            Some(Chunk {
2436                text: slice,
2437                syntax_highlight_id: highlight_id,
2438                highlight_style: None,
2439                diagnostic_severity: self.current_diagnostic_severity(),
2440                is_unnecessary: self.current_code_is_unnecessary(),
2441            })
2442        } else {
2443            None
2444        }
2445    }
2446}
2447
2448impl QueryCursorHandle {
2449    pub(crate) fn new() -> Self {
2450        let mut cursor = QUERY_CURSORS.lock().pop().unwrap_or_else(QueryCursor::new);
2451        cursor.set_match_limit(64);
2452        QueryCursorHandle(Some(cursor))
2453    }
2454}
2455
2456impl Deref for QueryCursorHandle {
2457    type Target = QueryCursor;
2458
2459    fn deref(&self) -> &Self::Target {
2460        self.0.as_ref().unwrap()
2461    }
2462}
2463
2464impl DerefMut for QueryCursorHandle {
2465    fn deref_mut(&mut self) -> &mut Self::Target {
2466        self.0.as_mut().unwrap()
2467    }
2468}
2469
2470impl Drop for QueryCursorHandle {
2471    fn drop(&mut self) {
2472        let mut cursor = self.0.take().unwrap();
2473        cursor.set_byte_range(0..usize::MAX);
2474        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2475        QUERY_CURSORS.lock().push(cursor)
2476    }
2477}
2478
2479trait ToTreeSitterPoint {
2480    fn to_ts_point(self) -> tree_sitter::Point;
2481    fn from_ts_point(point: tree_sitter::Point) -> Self;
2482}
2483
2484impl ToTreeSitterPoint for Point {
2485    fn to_ts_point(self) -> tree_sitter::Point {
2486        tree_sitter::Point::new(self.row as usize, self.column as usize)
2487    }
2488
2489    fn from_ts_point(point: tree_sitter::Point) -> Self {
2490        Point::new(point.row as u32, point.column as u32)
2491    }
2492}
2493
2494impl operation_queue::Operation for Operation {
2495    fn lamport_timestamp(&self) -> clock::Lamport {
2496        match self {
2497            Operation::Buffer(_) => {
2498                unreachable!("buffer operations should never be deferred at this layer")
2499            }
2500            Operation::UpdateDiagnostics {
2501                lamport_timestamp, ..
2502            }
2503            | Operation::UpdateSelections {
2504                lamport_timestamp, ..
2505            }
2506            | Operation::UpdateCompletionTriggers {
2507                lamport_timestamp, ..
2508            } => *lamport_timestamp,
2509        }
2510    }
2511}
2512
2513impl Default for Diagnostic {
2514    fn default() -> Self {
2515        Self {
2516            code: None,
2517            severity: DiagnosticSeverity::ERROR,
2518            message: Default::default(),
2519            group_id: 0,
2520            is_primary: false,
2521            is_valid: true,
2522            is_disk_based: false,
2523            is_unnecessary: false,
2524        }
2525    }
2526}
2527
2528impl IndentSize {
2529    pub fn spaces(len: u32) -> Self {
2530        Self {
2531            len,
2532            kind: IndentKind::Space,
2533        }
2534    }
2535
2536    pub fn tab() -> Self {
2537        Self {
2538            len: 1,
2539            kind: IndentKind::Tab,
2540        }
2541    }
2542
2543    pub fn chars(&self) -> impl Iterator<Item = char> {
2544        iter::repeat(self.char()).take(self.len as usize)
2545    }
2546
2547    pub fn char(&self) -> char {
2548        match self.kind {
2549            IndentKind::Space => ' ',
2550            IndentKind::Tab => '\t',
2551        }
2552    }
2553
2554    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2555        match direction {
2556            Ordering::Less => {
2557                if self.kind == size.kind && self.len >= size.len {
2558                    self.len -= size.len;
2559                }
2560            }
2561            Ordering::Equal => {}
2562            Ordering::Greater => {
2563                if self.len == 0 {
2564                    self = size;
2565                } else if self.kind == size.kind {
2566                    self.len += size.len;
2567                }
2568            }
2569        }
2570        self
2571    }
2572}
2573
2574impl Completion {
2575    pub fn sort_key(&self) -> (usize, &str) {
2576        let kind_key = match self.lsp_completion.kind {
2577            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2578            _ => 1,
2579        };
2580        (kind_key, &self.label.text[self.label.filter_range.clone()])
2581    }
2582
2583    pub fn is_snippet(&self) -> bool {
2584        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2585    }
2586}
2587
2588pub fn contiguous_ranges(
2589    values: impl Iterator<Item = u32>,
2590    max_len: usize,
2591) -> impl Iterator<Item = Range<u32>> {
2592    let mut values = values;
2593    let mut current_range: Option<Range<u32>> = None;
2594    std::iter::from_fn(move || loop {
2595        if let Some(value) = values.next() {
2596            if let Some(range) = &mut current_range {
2597                if value == range.end && range.len() < max_len {
2598                    range.end += 1;
2599                    continue;
2600                }
2601            }
2602
2603            let prev_range = current_range.clone();
2604            current_range = Some(value..(value + 1));
2605            if prev_range.is_some() {
2606                return prev_range;
2607            }
2608        } else {
2609            return current_range.take();
2610        }
2611    })
2612}
2613
2614pub fn char_kind(c: char) -> CharKind {
2615    if c.is_whitespace() {
2616        CharKind::Whitespace
2617    } else if c.is_alphanumeric() || c == '_' {
2618        CharKind::Word
2619    } else {
2620        CharKind::Punctuation
2621    }
2622}