buffer.rs

   1pub use crate::{
   2    diagnostic_set::DiagnosticSet,
   3    highlight_map::{HighlightId, HighlightMap},
   4    markdown::ParsedMarkdown,
   5    proto, Grammar, Language, LanguageRegistry,
   6};
   7use crate::{
   8    diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
   9    language_settings::{language_settings, LanguageSettings},
  10    markdown::parse_markdown,
  11    outline::OutlineItem,
  12    syntax_map::{
  13        SyntaxLayerInfo, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxMapMatches,
  14        SyntaxSnapshot, ToTreeSitterPoint,
  15    },
  16    CodeLabel, LanguageScope, Outline,
  17};
  18use anyhow::{anyhow, Result};
  19pub use clock::ReplicaId;
  20use futures::channel::oneshot;
  21use gpui::{AppContext, EventEmitter, HighlightStyle, ModelContext, Task, TaskLabel};
  22use lazy_static::lazy_static;
  23use lsp::LanguageServerId;
  24use parking_lot::Mutex;
  25use similar::{ChangeTag, TextDiff};
  26use smallvec::SmallVec;
  27use smol::future::yield_now;
  28use std::{
  29    any::Any,
  30    cmp::{self, Ordering},
  31    collections::BTreeMap,
  32    ffi::OsStr,
  33    future::Future,
  34    iter::{self, Iterator, Peekable},
  35    mem,
  36    ops::{Deref, Range},
  37    path::{Path, PathBuf},
  38    str,
  39    sync::Arc,
  40    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  41    vec,
  42};
  43use sum_tree::TreeMap;
  44use text::operation_queue::OperationQueue;
  45use text::*;
  46pub use text::{
  47    Anchor, Bias, Buffer as TextBuffer, BufferSnapshot as TextBufferSnapshot, Edit, OffsetRangeExt,
  48    OffsetUtf16, Patch, Point, PointUtf16, Rope, RopeFingerprint, Selection, SelectionGoal,
  49    Subscription, TextDimension, TextSummary, ToOffset, ToOffsetUtf16, ToPoint, ToPointUtf16,
  50    Transaction, TransactionId, Unclipped,
  51};
  52use theme::SyntaxTheme;
  53#[cfg(any(test, feature = "test-support"))]
  54use util::RandomCharIter;
  55use util::RangeExt;
  56
  57#[cfg(any(test, feature = "test-support"))]
  58pub use {tree_sitter_rust, tree_sitter_typescript};
  59
  60pub use lsp::DiagnosticSeverity;
  61
  62lazy_static! {
  63    pub static ref BUFFER_DIFF_TASK: TaskLabel = TaskLabel::new();
  64}
  65
  66#[derive(PartialEq, Clone, Copy, Debug)]
  67pub enum Capability {
  68    ReadWrite,
  69    ReadOnly,
  70}
  71
  72/// An in-memory representation of a source code file.
  73pub struct Buffer {
  74    text: TextBuffer,
  75    diff_base: Option<String>,
  76    git_diff: git::diff::BufferDiff,
  77    file: Option<Arc<dyn File>>,
  78    /// The mtime of the file when this buffer was last loaded from
  79    /// or saved to disk.
  80    saved_mtime: SystemTime,
  81    /// The version vector when this buffer was last loaded from
  82    /// or saved to disk.
  83    saved_version: clock::Global,
  84    /// A hash of the current contents of the buffer's file.
  85    file_fingerprint: RopeFingerprint,
  86    transaction_depth: usize,
  87    was_dirty_before_starting_transaction: Option<bool>,
  88    reload_task: Option<Task<Result<()>>>,
  89    language: Option<Arc<Language>>,
  90    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  91    pending_autoindent: Option<Task<()>>,
  92    sync_parse_timeout: Duration,
  93    syntax_map: Mutex<SyntaxMap>,
  94    parsing_in_background: bool,
  95    parse_count: usize,
  96    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
  97    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  98    selections_update_count: usize,
  99    diagnostics_update_count: usize,
 100    diagnostics_timestamp: clock::Lamport,
 101    file_update_count: usize,
 102    git_diff_update_count: usize,
 103    completion_triggers: Vec<String>,
 104    completion_triggers_timestamp: clock::Lamport,
 105    deferred_ops: OperationQueue<Operation>,
 106    capability: Capability,
 107}
 108
 109/// An immutable, cheaply cloneable representation of a certain
 110/// state of a buffer.
 111pub struct BufferSnapshot {
 112    text: text::BufferSnapshot,
 113    pub git_diff: git::diff::BufferDiff,
 114    pub(crate) syntax: SyntaxSnapshot,
 115    file: Option<Arc<dyn File>>,
 116    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
 117    diagnostics_update_count: usize,
 118    file_update_count: usize,
 119    git_diff_update_count: usize,
 120    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 121    selections_update_count: usize,
 122    language: Option<Arc<Language>>,
 123    parse_count: usize,
 124}
 125
 126#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
 127pub struct IndentSize {
 128    pub len: u32,
 129    pub kind: IndentKind,
 130}
 131
 132#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
 133pub enum IndentKind {
 134    #[default]
 135    Space,
 136    Tab,
 137}
 138
 139#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 140pub enum CursorShape {
 141    #[default]
 142    Bar,
 143    Block,
 144    Underscore,
 145    Hollow,
 146}
 147
 148#[derive(Clone, Debug)]
 149struct SelectionSet {
 150    line_mode: bool,
 151    cursor_shape: CursorShape,
 152    selections: Arc<[Selection<Anchor>]>,
 153    lamport_timestamp: clock::Lamport,
 154}
 155
 156#[derive(Clone, Debug, PartialEq, Eq)]
 157pub struct GroupId {
 158    source: Arc<str>,
 159    id: usize,
 160}
 161
 162/// A diagnostic associated with a certain range of a buffer.
 163#[derive(Clone, Debug, PartialEq, Eq)]
 164pub struct Diagnostic {
 165    pub source: Option<String>,
 166    pub code: Option<String>,
 167    pub severity: DiagnosticSeverity,
 168    pub message: String,
 169    pub group_id: usize,
 170    pub is_valid: bool,
 171    pub is_primary: bool,
 172    pub is_disk_based: bool,
 173    pub is_unnecessary: bool,
 174}
 175
 176pub async fn prepare_completion_documentation(
 177    documentation: &lsp::Documentation,
 178    language_registry: &Arc<LanguageRegistry>,
 179    language: Option<Arc<Language>>,
 180) -> Documentation {
 181    match documentation {
 182        lsp::Documentation::String(text) => {
 183            if text.lines().count() <= 1 {
 184                Documentation::SingleLine(text.clone())
 185            } else {
 186                Documentation::MultiLinePlainText(text.clone())
 187            }
 188        }
 189
 190        lsp::Documentation::MarkupContent(lsp::MarkupContent { kind, value }) => match kind {
 191            lsp::MarkupKind::PlainText => {
 192                if value.lines().count() <= 1 {
 193                    Documentation::SingleLine(value.clone())
 194                } else {
 195                    Documentation::MultiLinePlainText(value.clone())
 196                }
 197            }
 198
 199            lsp::MarkupKind::Markdown => {
 200                let parsed = parse_markdown(value, language_registry, language).await;
 201                Documentation::MultiLineMarkdown(parsed)
 202            }
 203        },
 204    }
 205}
 206
 207#[derive(Clone, Debug)]
 208pub enum Documentation {
 209    Undocumented,
 210    SingleLine(String),
 211    MultiLinePlainText(String),
 212    MultiLineMarkdown(ParsedMarkdown),
 213}
 214
 215#[derive(Clone, Debug)]
 216pub struct Completion {
 217    pub old_range: Range<Anchor>,
 218    pub new_text: String,
 219    pub label: CodeLabel,
 220    pub server_id: LanguageServerId,
 221    pub documentation: Option<Documentation>,
 222    pub lsp_completion: lsp::CompletionItem,
 223}
 224
 225#[derive(Clone, Debug)]
 226pub struct CodeAction {
 227    pub server_id: LanguageServerId,
 228    pub range: Range<Anchor>,
 229    pub lsp_action: lsp::CodeAction,
 230}
 231
 232#[derive(Clone, Debug, PartialEq)]
 233pub enum Operation {
 234    Buffer(text::Operation),
 235
 236    UpdateDiagnostics {
 237        server_id: LanguageServerId,
 238        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 239        lamport_timestamp: clock::Lamport,
 240    },
 241
 242    UpdateSelections {
 243        selections: Arc<[Selection<Anchor>]>,
 244        lamport_timestamp: clock::Lamport,
 245        line_mode: bool,
 246        cursor_shape: CursorShape,
 247    },
 248
 249    UpdateCompletionTriggers {
 250        triggers: Vec<String>,
 251        lamport_timestamp: clock::Lamport,
 252    },
 253}
 254
 255#[derive(Clone, Debug, PartialEq)]
 256pub enum Event {
 257    Operation(Operation),
 258    Edited,
 259    DirtyChanged,
 260    Saved,
 261    FileHandleChanged,
 262    Reloaded,
 263    DiffBaseChanged,
 264    LanguageChanged,
 265    Reparsed,
 266    DiagnosticsUpdated,
 267    Closed,
 268}
 269
 270/// The file associated with a buffer.
 271pub trait File: Send + Sync {
 272    fn as_local(&self) -> Option<&dyn LocalFile>;
 273
 274    fn is_local(&self) -> bool {
 275        self.as_local().is_some()
 276    }
 277
 278    fn mtime(&self) -> SystemTime;
 279
 280    /// Returns the path of this file relative to the worktree's root directory.
 281    fn path(&self) -> &Arc<Path>;
 282
 283    /// Returns the path of this file relative to the worktree's parent directory (this means it
 284    /// includes the name of the worktree's root folder).
 285    fn full_path(&self, cx: &AppContext) -> PathBuf;
 286
 287    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 288    /// of its worktree, then this method will return the name of the worktree itself.
 289    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
 290
 291    /// Returns the id of the worktree to which this file belongs.
 292    ///
 293    /// This is needed for looking up project-specific settings.
 294    fn worktree_id(&self) -> usize;
 295
 296    fn is_deleted(&self) -> bool;
 297
 298    fn as_any(&self) -> &dyn Any;
 299
 300    fn to_proto(&self) -> rpc::proto::File;
 301}
 302
 303pub trait LocalFile: File {
 304    /// Returns the absolute path of this file.
 305    fn abs_path(&self, cx: &AppContext) -> PathBuf;
 306
 307    fn load(&self, cx: &AppContext) -> Task<Result<String>>;
 308
 309    fn buffer_reloaded(
 310        &self,
 311        buffer_id: u64,
 312        version: &clock::Global,
 313        fingerprint: RopeFingerprint,
 314        line_ending: LineEnding,
 315        mtime: SystemTime,
 316        cx: &mut AppContext,
 317    );
 318}
 319
 320/// The auto-indent behavior associated with an editing operation.
 321/// For some editing operations, each affected line of text has its
 322/// indentation recomputed. For other operations, the entire block
 323/// of edited text is adjusted uniformly.
 324#[derive(Clone, Debug)]
 325pub enum AutoindentMode {
 326    /// Indent each line of inserted text.
 327    EachLine,
 328    /// Apply the same indentation adjustment to all of the lines
 329    /// in a given insertion.
 330    Block {
 331        /// The original indentation level of the first line of each
 332        /// insertion, if it has been copied.
 333        original_indent_columns: Vec<u32>,
 334    },
 335}
 336
 337#[derive(Clone)]
 338struct AutoindentRequest {
 339    before_edit: BufferSnapshot,
 340    entries: Vec<AutoindentRequestEntry>,
 341    is_block_mode: bool,
 342}
 343
 344#[derive(Clone)]
 345struct AutoindentRequestEntry {
 346    /// A range of the buffer whose indentation should be adjusted.
 347    range: Range<Anchor>,
 348    /// Whether or not these lines should be considered brand new, for the
 349    /// purpose of auto-indent. When text is not new, its indentation will
 350    /// only be adjusted if the suggested indentation level has *changed*
 351    /// since the edit was made.
 352    first_line_is_new: bool,
 353    indent_size: IndentSize,
 354    original_indent_column: Option<u32>,
 355}
 356
 357#[derive(Debug)]
 358struct IndentSuggestion {
 359    basis_row: u32,
 360    delta: Ordering,
 361    within_error: bool,
 362}
 363
 364struct BufferChunkHighlights<'a> {
 365    captures: SyntaxMapCaptures<'a>,
 366    next_capture: Option<SyntaxMapCapture<'a>>,
 367    stack: Vec<(usize, HighlightId)>,
 368    highlight_maps: Vec<HighlightMap>,
 369}
 370
 371/// An iterator that yields chunks of a buffer's text, along with their
 372/// syntax highlights and diagnostic status.
 373pub struct BufferChunks<'a> {
 374    range: Range<usize>,
 375    chunks: text::Chunks<'a>,
 376    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 377    error_depth: usize,
 378    warning_depth: usize,
 379    information_depth: usize,
 380    hint_depth: usize,
 381    unnecessary_depth: usize,
 382    highlights: Option<BufferChunkHighlights<'a>>,
 383}
 384
 385/// A chunk of a buffer's text, along with its syntax highlight and
 386/// diagnostic status.
 387#[derive(Clone, Copy, Debug, Default)]
 388pub struct Chunk<'a> {
 389    pub text: &'a str,
 390    pub syntax_highlight_id: Option<HighlightId>,
 391    pub highlight_style: Option<HighlightStyle>,
 392    pub diagnostic_severity: Option<DiagnosticSeverity>,
 393    pub is_unnecessary: bool,
 394    pub is_tab: bool,
 395}
 396
 397/// A set of edits to a given version of a buffer, computed asynchronously.
 398pub struct Diff {
 399    pub(crate) base_version: clock::Global,
 400    line_ending: LineEnding,
 401    edits: Vec<(Range<usize>, Arc<str>)>,
 402}
 403
 404#[derive(Clone, Copy)]
 405pub(crate) struct DiagnosticEndpoint {
 406    offset: usize,
 407    is_start: bool,
 408    severity: DiagnosticSeverity,
 409    is_unnecessary: bool,
 410}
 411
 412#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 413pub enum CharKind {
 414    Whitespace,
 415    Punctuation,
 416    Word,
 417}
 418
 419impl CharKind {
 420    pub fn coerce_punctuation(self, treat_punctuation_as_word: bool) -> Self {
 421        if treat_punctuation_as_word && self == CharKind::Punctuation {
 422            CharKind::Word
 423        } else {
 424            self
 425        }
 426    }
 427}
 428
 429impl Buffer {
 430    /// Create a new buffer with the given base text.
 431    pub fn new<T: Into<String>>(replica_id: ReplicaId, id: u64, base_text: T) -> Self {
 432        Self::build(
 433            TextBuffer::new(replica_id, id, base_text.into()),
 434            None,
 435            None,
 436            Capability::ReadWrite,
 437        )
 438    }
 439
 440    /// Create a new buffer that is a replica of a remote buffer.
 441    pub fn remote(
 442        remote_id: u64,
 443        replica_id: ReplicaId,
 444        capability: Capability,
 445        base_text: String,
 446    ) -> Self {
 447        Self::build(
 448            TextBuffer::new(replica_id, remote_id, base_text),
 449            None,
 450            None,
 451            capability,
 452        )
 453    }
 454
 455    /// Create a new buffer that is a replica of a remote buffer, populating its
 456    /// state from the given protobuf message.
 457    pub fn from_proto(
 458        replica_id: ReplicaId,
 459        capability: Capability,
 460        message: proto::BufferState,
 461        file: Option<Arc<dyn File>>,
 462    ) -> Result<Self> {
 463        let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
 464        let mut this = Self::build(
 465            buffer,
 466            message.diff_base.map(|text| text.into_boxed_str().into()),
 467            file,
 468            capability,
 469        );
 470        this.text.set_line_ending(proto::deserialize_line_ending(
 471            rpc::proto::LineEnding::from_i32(message.line_ending)
 472                .ok_or_else(|| anyhow!("missing line_ending"))?,
 473        ));
 474        this.saved_version = proto::deserialize_version(&message.saved_version);
 475        this.file_fingerprint = proto::deserialize_fingerprint(&message.saved_version_fingerprint)?;
 476        this.saved_mtime = message
 477            .saved_mtime
 478            .ok_or_else(|| anyhow!("invalid saved_mtime"))?
 479            .into();
 480        Ok(this)
 481    }
 482
 483    /// Serialize the buffer's state to a protobuf message.
 484    pub fn to_proto(&self) -> proto::BufferState {
 485        proto::BufferState {
 486            id: self.remote_id(),
 487            file: self.file.as_ref().map(|f| f.to_proto()),
 488            base_text: self.base_text().to_string(),
 489            diff_base: self.diff_base.as_ref().map(|h| h.to_string()),
 490            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
 491            saved_version: proto::serialize_version(&self.saved_version),
 492            saved_version_fingerprint: proto::serialize_fingerprint(self.file_fingerprint),
 493            saved_mtime: Some(self.saved_mtime.into()),
 494        }
 495    }
 496
 497    /// Serialize as protobufs all of the changes to the buffer since the given version.
 498    pub fn serialize_ops(
 499        &self,
 500        since: Option<clock::Global>,
 501        cx: &AppContext,
 502    ) -> Task<Vec<proto::Operation>> {
 503        let mut operations = Vec::new();
 504        operations.extend(self.deferred_ops.iter().map(proto::serialize_operation));
 505
 506        operations.extend(self.remote_selections.iter().map(|(_, set)| {
 507            proto::serialize_operation(&Operation::UpdateSelections {
 508                selections: set.selections.clone(),
 509                lamport_timestamp: set.lamport_timestamp,
 510                line_mode: set.line_mode,
 511                cursor_shape: set.cursor_shape,
 512            })
 513        }));
 514
 515        for (server_id, diagnostics) in &self.diagnostics {
 516            operations.push(proto::serialize_operation(&Operation::UpdateDiagnostics {
 517                lamport_timestamp: self.diagnostics_timestamp,
 518                server_id: *server_id,
 519                diagnostics: diagnostics.iter().cloned().collect(),
 520            }));
 521        }
 522
 523        operations.push(proto::serialize_operation(
 524            &Operation::UpdateCompletionTriggers {
 525                triggers: self.completion_triggers.clone(),
 526                lamport_timestamp: self.completion_triggers_timestamp,
 527            },
 528        ));
 529
 530        let text_operations = self.text.operations().clone();
 531        cx.background_executor().spawn(async move {
 532            let since = since.unwrap_or_default();
 533            operations.extend(
 534                text_operations
 535                    .iter()
 536                    .filter(|(_, op)| !since.observed(op.timestamp()))
 537                    .map(|(_, op)| proto::serialize_operation(&Operation::Buffer(op.clone()))),
 538            );
 539            operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
 540            operations
 541        })
 542    }
 543
 544    /// Assign a language to the buffer, returning the buffer.
 545    pub fn with_language(mut self, language: Arc<Language>, cx: &mut ModelContext<Self>) -> Self {
 546        self.set_language(Some(language), cx);
 547        self
 548    }
 549
 550    pub fn capability(&self) -> Capability {
 551        self.capability
 552    }
 553
 554    pub fn read_only(&self) -> bool {
 555        self.capability == Capability::ReadOnly
 556    }
 557
 558    pub fn build(
 559        buffer: TextBuffer,
 560        diff_base: Option<String>,
 561        file: Option<Arc<dyn File>>,
 562        capability: Capability,
 563    ) -> Self {
 564        let saved_mtime = if let Some(file) = file.as_ref() {
 565            file.mtime()
 566        } else {
 567            UNIX_EPOCH
 568        };
 569
 570        Self {
 571            saved_mtime,
 572            saved_version: buffer.version(),
 573            file_fingerprint: buffer.as_rope().fingerprint(),
 574            reload_task: None,
 575            transaction_depth: 0,
 576            was_dirty_before_starting_transaction: None,
 577            text: buffer,
 578            diff_base,
 579            git_diff: git::diff::BufferDiff::new(),
 580            file,
 581            capability,
 582            syntax_map: Mutex::new(SyntaxMap::new()),
 583            parsing_in_background: false,
 584            parse_count: 0,
 585            sync_parse_timeout: Duration::from_millis(1),
 586            autoindent_requests: Default::default(),
 587            pending_autoindent: Default::default(),
 588            language: None,
 589            remote_selections: Default::default(),
 590            selections_update_count: 0,
 591            diagnostics: Default::default(),
 592            diagnostics_update_count: 0,
 593            diagnostics_timestamp: Default::default(),
 594            file_update_count: 0,
 595            git_diff_update_count: 0,
 596            completion_triggers: Default::default(),
 597            completion_triggers_timestamp: Default::default(),
 598            deferred_ops: OperationQueue::new(),
 599        }
 600    }
 601
 602    /// Retrieve a snapshot of the buffer's current state. This is computationally
 603    /// cheap, and allows reading from the buffer on a background thread.
 604    pub fn snapshot(&self) -> BufferSnapshot {
 605        let text = self.text.snapshot();
 606        let mut syntax_map = self.syntax_map.lock();
 607        syntax_map.interpolate(&text);
 608        let syntax = syntax_map.snapshot();
 609
 610        BufferSnapshot {
 611            text,
 612            syntax,
 613            git_diff: self.git_diff.clone(),
 614            file: self.file.clone(),
 615            remote_selections: self.remote_selections.clone(),
 616            diagnostics: self.diagnostics.clone(),
 617            diagnostics_update_count: self.diagnostics_update_count,
 618            file_update_count: self.file_update_count,
 619            git_diff_update_count: self.git_diff_update_count,
 620            language: self.language.clone(),
 621            parse_count: self.parse_count,
 622            selections_update_count: self.selections_update_count,
 623        }
 624    }
 625
 626    pub(crate) fn as_text_snapshot(&self) -> &text::BufferSnapshot {
 627        &self.text
 628    }
 629
 630    /// Retrieve a snapshot of the buffer's raw text, without any
 631    /// language-related state like the syntax tree or diagnostics.
 632    pub fn text_snapshot(&self) -> text::BufferSnapshot {
 633        self.text.snapshot()
 634    }
 635
 636    /// The file associated with the buffer, if any.
 637    pub fn file(&self) -> Option<&Arc<dyn File>> {
 638        self.file.as_ref()
 639    }
 640
 641    /// The version of the buffer that was last saved or reloaded from disk.
 642    pub fn saved_version(&self) -> &clock::Global {
 643        &self.saved_version
 644    }
 645
 646    pub fn saved_version_fingerprint(&self) -> RopeFingerprint {
 647        self.file_fingerprint
 648    }
 649
 650    /// The mtime of the buffer's file when the buffer was last saved or reloaded from disk.
 651    pub fn saved_mtime(&self) -> SystemTime {
 652        self.saved_mtime
 653    }
 654
 655    /// Assign a language to the buffer.
 656    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 657        self.syntax_map.lock().clear();
 658        self.language = language;
 659        self.reparse(cx);
 660        cx.emit(Event::LanguageChanged);
 661    }
 662
 663    /// Assign a language registry to the buffer. This allows the buffer to retrieve
 664    /// other languages if parts of the buffer are written in different languages.
 665    pub fn set_language_registry(&mut self, language_registry: Arc<LanguageRegistry>) {
 666        self.syntax_map
 667            .lock()
 668            .set_language_registry(language_registry);
 669    }
 670
 671    pub fn did_save(
 672        &mut self,
 673        version: clock::Global,
 674        fingerprint: RopeFingerprint,
 675        mtime: SystemTime,
 676        cx: &mut ModelContext<Self>,
 677    ) {
 678        self.saved_version = version;
 679        self.file_fingerprint = fingerprint;
 680        self.saved_mtime = mtime;
 681        cx.emit(Event::Saved);
 682        cx.notify();
 683    }
 684
 685    pub fn reload(
 686        &mut self,
 687        cx: &mut ModelContext<Self>,
 688    ) -> oneshot::Receiver<Option<Transaction>> {
 689        let (tx, rx) = futures::channel::oneshot::channel();
 690        let prev_version = self.text.version();
 691        self.reload_task = Some(cx.spawn(|this, mut cx| async move {
 692            let Some((new_mtime, new_text)) = this.update(&mut cx, |this, cx| {
 693                let file = this.file.as_ref()?.as_local()?;
 694                Some((file.mtime(), file.load(cx)))
 695            })?
 696            else {
 697                return Ok(());
 698            };
 699
 700            let new_text = new_text.await?;
 701            let diff = this
 702                .update(&mut cx, |this, cx| this.diff(new_text.clone(), cx))?
 703                .await;
 704            this.update(&mut cx, |this, cx| {
 705                if this.version() == diff.base_version {
 706                    this.finalize_last_transaction();
 707                    this.apply_diff(diff, cx);
 708                    tx.send(this.finalize_last_transaction().cloned()).ok();
 709
 710                    this.did_reload(
 711                        this.version(),
 712                        this.as_rope().fingerprint(),
 713                        this.line_ending(),
 714                        new_mtime,
 715                        cx,
 716                    );
 717                } else {
 718                    this.did_reload(
 719                        prev_version,
 720                        Rope::text_fingerprint(&new_text),
 721                        this.line_ending(),
 722                        this.saved_mtime,
 723                        cx,
 724                    );
 725                }
 726
 727                this.reload_task.take();
 728            })
 729        }));
 730        rx
 731    }
 732
 733    pub fn did_reload(
 734        &mut self,
 735        version: clock::Global,
 736        fingerprint: RopeFingerprint,
 737        line_ending: LineEnding,
 738        mtime: SystemTime,
 739        cx: &mut ModelContext<Self>,
 740    ) {
 741        self.saved_version = version;
 742        self.file_fingerprint = fingerprint;
 743        self.text.set_line_ending(line_ending);
 744        self.saved_mtime = mtime;
 745        if let Some(file) = self.file.as_ref().and_then(|f| f.as_local()) {
 746            file.buffer_reloaded(
 747                self.remote_id(),
 748                &self.saved_version,
 749                self.file_fingerprint,
 750                self.line_ending(),
 751                self.saved_mtime,
 752                cx,
 753            );
 754        }
 755        cx.emit(Event::Reloaded);
 756        cx.notify();
 757    }
 758
 759    pub fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut ModelContext<Self>) {
 760        let mut file_changed = false;
 761
 762        if let Some(old_file) = self.file.as_ref() {
 763            if new_file.path() != old_file.path() {
 764                file_changed = true;
 765            }
 766
 767            if new_file.is_deleted() {
 768                if !old_file.is_deleted() {
 769                    file_changed = true;
 770                    if !self.is_dirty() {
 771                        cx.emit(Event::DirtyChanged);
 772                    }
 773                }
 774            } else {
 775                let new_mtime = new_file.mtime();
 776                if new_mtime != old_file.mtime() {
 777                    file_changed = true;
 778
 779                    if !self.is_dirty() {
 780                        self.reload(cx).close();
 781                    }
 782                }
 783            }
 784        } else {
 785            file_changed = true;
 786        };
 787
 788        self.file = Some(new_file);
 789        if file_changed {
 790            self.file_update_count += 1;
 791            cx.emit(Event::FileHandleChanged);
 792            cx.notify();
 793        }
 794    }
 795
 796    pub fn diff_base(&self) -> Option<&str> {
 797        self.diff_base.as_deref()
 798    }
 799
 800    pub fn set_diff_base(&mut self, diff_base: Option<String>, cx: &mut ModelContext<Self>) {
 801        self.diff_base = diff_base;
 802        self.git_diff_recalc(cx);
 803        cx.emit(Event::DiffBaseChanged);
 804    }
 805
 806    pub fn git_diff_recalc(&mut self, cx: &mut ModelContext<Self>) -> Option<Task<()>> {
 807        let diff_base = self.diff_base.clone()?; // TODO: Make this an Arc
 808        let snapshot = self.snapshot();
 809
 810        let mut diff = self.git_diff.clone();
 811        let diff = cx.background_executor().spawn(async move {
 812            diff.update(&diff_base, &snapshot).await;
 813            diff
 814        });
 815
 816        Some(cx.spawn(|this, mut cx| async move {
 817            let buffer_diff = diff.await;
 818            this.update(&mut cx, |this, _| {
 819                this.git_diff = buffer_diff;
 820                this.git_diff_update_count += 1;
 821            })
 822            .ok();
 823        }))
 824    }
 825
 826    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 827        cx.emit(Event::Closed);
 828    }
 829
 830    pub fn language(&self) -> Option<&Arc<Language>> {
 831        self.language.as_ref()
 832    }
 833
 834    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
 835        let offset = position.to_offset(self);
 836        self.syntax_map
 837            .lock()
 838            .layers_for_range(offset..offset, &self.text)
 839            .last()
 840            .map(|info| info.language.clone())
 841            .or_else(|| self.language.clone())
 842    }
 843
 844    pub fn parse_count(&self) -> usize {
 845        self.parse_count
 846    }
 847
 848    pub fn selections_update_count(&self) -> usize {
 849        self.selections_update_count
 850    }
 851
 852    pub fn diagnostics_update_count(&self) -> usize {
 853        self.diagnostics_update_count
 854    }
 855
 856    pub fn file_update_count(&self) -> usize {
 857        self.file_update_count
 858    }
 859
 860    pub fn git_diff_update_count(&self) -> usize {
 861        self.git_diff_update_count
 862    }
 863
 864    #[cfg(any(test, feature = "test-support"))]
 865    pub fn is_parsing(&self) -> bool {
 866        self.parsing_in_background
 867    }
 868
 869    pub fn contains_unknown_injections(&self) -> bool {
 870        self.syntax_map.lock().contains_unknown_injections()
 871    }
 872
 873    #[cfg(test)]
 874    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 875        self.sync_parse_timeout = timeout;
 876    }
 877
 878    /// Called after an edit to synchronize the buffer's main parse tree with
 879    /// the buffer's new underlying state.
 880    ///
 881    /// Locks the syntax map and interpolates the edits since the last reparse
 882    /// into the foreground syntax tree.
 883    ///
 884    /// Then takes a stable snapshot of the syntax map before unlocking it.
 885    /// The snapshot with the interpolated edits is sent to a background thread,
 886    /// where we ask Tree-sitter to perform an incremental parse.
 887    ///
 888    /// Meanwhile, in the foreground, we block the main thread for up to 1ms
 889    /// waiting on the parse to complete. As soon as it completes, we proceed
 890    /// synchronously, unless a 1ms timeout elapses.
 891    ///
 892    /// If we time out waiting on the parse, we spawn a second task waiting
 893    /// until the parse does complete and return with the interpolated tree still
 894    /// in the foreground. When the background parse completes, call back into
 895    /// the main thread and assign the foreground parse state.
 896    ///
 897    /// If the buffer or grammar changed since the start of the background parse,
 898    /// initiate an additional reparse recursively. To avoid concurrent parses
 899    /// for the same buffer, we only initiate a new parse if we are not already
 900    /// parsing in the background.
 901    pub fn reparse(&mut self, cx: &mut ModelContext<Self>) {
 902        if self.parsing_in_background {
 903            return;
 904        }
 905        let language = if let Some(language) = self.language.clone() {
 906            language
 907        } else {
 908            return;
 909        };
 910
 911        let text = self.text_snapshot();
 912        let parsed_version = self.version();
 913
 914        let mut syntax_map = self.syntax_map.lock();
 915        syntax_map.interpolate(&text);
 916        let language_registry = syntax_map.language_registry();
 917        let mut syntax_snapshot = syntax_map.snapshot();
 918        drop(syntax_map);
 919
 920        let parse_task = cx.background_executor().spawn({
 921            let language = language.clone();
 922            let language_registry = language_registry.clone();
 923            async move {
 924                syntax_snapshot.reparse(&text, language_registry, language);
 925                syntax_snapshot
 926            }
 927        });
 928
 929        match cx
 930            .background_executor()
 931            .block_with_timeout(self.sync_parse_timeout, parse_task)
 932        {
 933            Ok(new_syntax_snapshot) => {
 934                self.did_finish_parsing(new_syntax_snapshot, cx);
 935                return;
 936            }
 937            Err(parse_task) => {
 938                self.parsing_in_background = true;
 939                cx.spawn(move |this, mut cx| async move {
 940                    let new_syntax_map = parse_task.await;
 941                    this.update(&mut cx, move |this, cx| {
 942                        let grammar_changed =
 943                            this.language.as_ref().map_or(true, |current_language| {
 944                                !Arc::ptr_eq(&language, current_language)
 945                            });
 946                        let language_registry_changed = new_syntax_map
 947                            .contains_unknown_injections()
 948                            && language_registry.map_or(false, |registry| {
 949                                registry.version() != new_syntax_map.language_registry_version()
 950                            });
 951                        let parse_again = language_registry_changed
 952                            || grammar_changed
 953                            || this.version.changed_since(&parsed_version);
 954                        this.did_finish_parsing(new_syntax_map, cx);
 955                        this.parsing_in_background = false;
 956                        if parse_again {
 957                            this.reparse(cx);
 958                        }
 959                    })
 960                    .ok();
 961                })
 962                .detach();
 963            }
 964        }
 965    }
 966
 967    fn did_finish_parsing(&mut self, syntax_snapshot: SyntaxSnapshot, cx: &mut ModelContext<Self>) {
 968        self.parse_count += 1;
 969        self.syntax_map.lock().did_parse(syntax_snapshot);
 970        self.request_autoindent(cx);
 971        cx.emit(Event::Reparsed);
 972        cx.notify();
 973    }
 974
 975    /// Assign to the buffer a set of diagnostics created by a given language server.
 976    pub fn update_diagnostics(
 977        &mut self,
 978        server_id: LanguageServerId,
 979        diagnostics: DiagnosticSet,
 980        cx: &mut ModelContext<Self>,
 981    ) {
 982        let lamport_timestamp = self.text.lamport_clock.tick();
 983        let op = Operation::UpdateDiagnostics {
 984            server_id,
 985            diagnostics: diagnostics.iter().cloned().collect(),
 986            lamport_timestamp,
 987        };
 988        self.apply_diagnostic_update(server_id, diagnostics, lamport_timestamp, cx);
 989        self.send_operation(op, cx);
 990    }
 991
 992    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 993        if let Some(indent_sizes) = self.compute_autoindents() {
 994            let indent_sizes = cx.background_executor().spawn(indent_sizes);
 995            match cx
 996                .background_executor()
 997                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 998            {
 999                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
1000                Err(indent_sizes) => {
1001                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
1002                        let indent_sizes = indent_sizes.await;
1003                        this.update(&mut cx, |this, cx| {
1004                            this.apply_autoindents(indent_sizes, cx);
1005                        })
1006                        .ok();
1007                    }));
1008                }
1009            }
1010        } else {
1011            self.autoindent_requests.clear();
1012        }
1013    }
1014
1015    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
1016        let max_rows_between_yields = 100;
1017        let snapshot = self.snapshot();
1018        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
1019            return None;
1020        }
1021
1022        let autoindent_requests = self.autoindent_requests.clone();
1023        Some(async move {
1024            let mut indent_sizes = BTreeMap::new();
1025            for request in autoindent_requests {
1026                // Resolve each edited range to its row in the current buffer and in the
1027                // buffer before this batch of edits.
1028                let mut row_ranges = Vec::new();
1029                let mut old_to_new_rows = BTreeMap::new();
1030                let mut language_indent_sizes_by_new_row = Vec::new();
1031                for entry in &request.entries {
1032                    let position = entry.range.start;
1033                    let new_row = position.to_point(&snapshot).row;
1034                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
1035                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
1036
1037                    if !entry.first_line_is_new {
1038                        let old_row = position.to_point(&request.before_edit).row;
1039                        old_to_new_rows.insert(old_row, new_row);
1040                    }
1041                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
1042                }
1043
1044                // Build a map containing the suggested indentation for each of the edited lines
1045                // with respect to the state of the buffer before these edits. This map is keyed
1046                // by the rows for these lines in the current state of the buffer.
1047                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
1048                let old_edited_ranges =
1049                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
1050                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
1051                let mut language_indent_size = IndentSize::default();
1052                for old_edited_range in old_edited_ranges {
1053                    let suggestions = request
1054                        .before_edit
1055                        .suggest_autoindents(old_edited_range.clone())
1056                        .into_iter()
1057                        .flatten();
1058                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
1059                        if let Some(suggestion) = suggestion {
1060                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
1061
1062                            // Find the indent size based on the language for this row.
1063                            while let Some((row, size)) = language_indent_sizes.peek() {
1064                                if *row > new_row {
1065                                    break;
1066                                }
1067                                language_indent_size = *size;
1068                                language_indent_sizes.next();
1069                            }
1070
1071                            let suggested_indent = old_to_new_rows
1072                                .get(&suggestion.basis_row)
1073                                .and_then(|from_row| {
1074                                    Some(old_suggestions.get(from_row).copied()?.0)
1075                                })
1076                                .unwrap_or_else(|| {
1077                                    request
1078                                        .before_edit
1079                                        .indent_size_for_line(suggestion.basis_row)
1080                                })
1081                                .with_delta(suggestion.delta, language_indent_size);
1082                            old_suggestions
1083                                .insert(new_row, (suggested_indent, suggestion.within_error));
1084                        }
1085                    }
1086                    yield_now().await;
1087                }
1088
1089                // In block mode, only compute indentation suggestions for the first line
1090                // of each insertion. Otherwise, compute suggestions for every inserted line.
1091                let new_edited_row_ranges = contiguous_ranges(
1092                    row_ranges.iter().flat_map(|(range, _)| {
1093                        if request.is_block_mode {
1094                            range.start..range.start + 1
1095                        } else {
1096                            range.clone()
1097                        }
1098                    }),
1099                    max_rows_between_yields,
1100                );
1101
1102                // Compute new suggestions for each line, but only include them in the result
1103                // if they differ from the old suggestion for that line.
1104                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
1105                let mut language_indent_size = IndentSize::default();
1106                for new_edited_row_range in new_edited_row_ranges {
1107                    let suggestions = snapshot
1108                        .suggest_autoindents(new_edited_row_range.clone())
1109                        .into_iter()
1110                        .flatten();
1111                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1112                        if let Some(suggestion) = suggestion {
1113                            // Find the indent size based on the language for this row.
1114                            while let Some((row, size)) = language_indent_sizes.peek() {
1115                                if *row > new_row {
1116                                    break;
1117                                }
1118                                language_indent_size = *size;
1119                                language_indent_sizes.next();
1120                            }
1121
1122                            let suggested_indent = indent_sizes
1123                                .get(&suggestion.basis_row)
1124                                .copied()
1125                                .unwrap_or_else(|| {
1126                                    snapshot.indent_size_for_line(suggestion.basis_row)
1127                                })
1128                                .with_delta(suggestion.delta, language_indent_size);
1129                            if old_suggestions.get(&new_row).map_or(
1130                                true,
1131                                |(old_indentation, was_within_error)| {
1132                                    suggested_indent != *old_indentation
1133                                        && (!suggestion.within_error || *was_within_error)
1134                                },
1135                            ) {
1136                                indent_sizes.insert(new_row, suggested_indent);
1137                            }
1138                        }
1139                    }
1140                    yield_now().await;
1141                }
1142
1143                // For each block of inserted text, adjust the indentation of the remaining
1144                // lines of the block by the same amount as the first line was adjusted.
1145                if request.is_block_mode {
1146                    for (row_range, original_indent_column) in
1147                        row_ranges
1148                            .into_iter()
1149                            .filter_map(|(range, original_indent_column)| {
1150                                if range.len() > 1 {
1151                                    Some((range, original_indent_column?))
1152                                } else {
1153                                    None
1154                                }
1155                            })
1156                    {
1157                        let new_indent = indent_sizes
1158                            .get(&row_range.start)
1159                            .copied()
1160                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
1161                        let delta = new_indent.len as i64 - original_indent_column as i64;
1162                        if delta != 0 {
1163                            for row in row_range.skip(1) {
1164                                indent_sizes.entry(row).or_insert_with(|| {
1165                                    let mut size = snapshot.indent_size_for_line(row);
1166                                    if size.kind == new_indent.kind {
1167                                        match delta.cmp(&0) {
1168                                            Ordering::Greater => size.len += delta as u32,
1169                                            Ordering::Less => {
1170                                                size.len = size.len.saturating_sub(-delta as u32)
1171                                            }
1172                                            Ordering::Equal => {}
1173                                        }
1174                                    }
1175                                    size
1176                                });
1177                            }
1178                        }
1179                    }
1180                }
1181            }
1182
1183            indent_sizes
1184        })
1185    }
1186
1187    fn apply_autoindents(
1188        &mut self,
1189        indent_sizes: BTreeMap<u32, IndentSize>,
1190        cx: &mut ModelContext<Self>,
1191    ) {
1192        self.autoindent_requests.clear();
1193
1194        let edits: Vec<_> = indent_sizes
1195            .into_iter()
1196            .filter_map(|(row, indent_size)| {
1197                let current_size = indent_size_for_line(self, row);
1198                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
1199            })
1200            .collect();
1201
1202        self.edit(edits, None, cx);
1203    }
1204
1205    /// Create a minimal edit that will cause the the given row to be indented
1206    /// with the given size. After applying this edit, the length of the line
1207    /// will always be at least `new_size.len`.
1208    pub fn edit_for_indent_size_adjustment(
1209        row: u32,
1210        current_size: IndentSize,
1211        new_size: IndentSize,
1212    ) -> Option<(Range<Point>, String)> {
1213        if new_size.kind != current_size.kind {
1214            Some((
1215                Point::new(row, 0)..Point::new(row, current_size.len),
1216                iter::repeat(new_size.char())
1217                    .take(new_size.len as usize)
1218                    .collect::<String>(),
1219            ))
1220        } else {
1221            match new_size.len.cmp(&current_size.len) {
1222                Ordering::Greater => {
1223                    let point = Point::new(row, 0);
1224                    Some((
1225                        point..point,
1226                        iter::repeat(new_size.char())
1227                            .take((new_size.len - current_size.len) as usize)
1228                            .collect::<String>(),
1229                    ))
1230                }
1231
1232                Ordering::Less => Some((
1233                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
1234                    String::new(),
1235                )),
1236
1237                Ordering::Equal => None,
1238            }
1239        }
1240    }
1241
1242    /// Spawns a background task that asynchronously computes a `Diff` between the buffer's text
1243    /// and the given new text.
1244    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
1245        let old_text = self.as_rope().clone();
1246        let base_version = self.version();
1247        cx.background_executor()
1248            .spawn_labeled(*BUFFER_DIFF_TASK, async move {
1249                let old_text = old_text.to_string();
1250                let line_ending = LineEnding::detect(&new_text);
1251                LineEnding::normalize(&mut new_text);
1252
1253                let diff = TextDiff::from_chars(old_text.as_str(), new_text.as_str());
1254                let empty: Arc<str> = "".into();
1255
1256                let mut edits = Vec::new();
1257                let mut old_offset = 0;
1258                let mut new_offset = 0;
1259                let mut last_edit: Option<(Range<usize>, Range<usize>)> = None;
1260                for change in diff.iter_all_changes().map(Some).chain([None]) {
1261                    if let Some(change) = &change {
1262                        let len = change.value().len();
1263                        match change.tag() {
1264                            ChangeTag::Equal => {
1265                                old_offset += len;
1266                                new_offset += len;
1267                            }
1268                            ChangeTag::Delete => {
1269                                let old_end_offset = old_offset + len;
1270                                if let Some((last_old_range, _)) = &mut last_edit {
1271                                    last_old_range.end = old_end_offset;
1272                                } else {
1273                                    last_edit =
1274                                        Some((old_offset..old_end_offset, new_offset..new_offset));
1275                                }
1276                                old_offset = old_end_offset;
1277                            }
1278                            ChangeTag::Insert => {
1279                                let new_end_offset = new_offset + len;
1280                                if let Some((_, last_new_range)) = &mut last_edit {
1281                                    last_new_range.end = new_end_offset;
1282                                } else {
1283                                    last_edit =
1284                                        Some((old_offset..old_offset, new_offset..new_end_offset));
1285                                }
1286                                new_offset = new_end_offset;
1287                            }
1288                        }
1289                    }
1290
1291                    if let Some((old_range, new_range)) = &last_edit {
1292                        if old_offset > old_range.end
1293                            || new_offset > new_range.end
1294                            || change.is_none()
1295                        {
1296                            let text = if new_range.is_empty() {
1297                                empty.clone()
1298                            } else {
1299                                new_text[new_range.clone()].into()
1300                            };
1301                            edits.push((old_range.clone(), text));
1302                            last_edit.take();
1303                        }
1304                    }
1305                }
1306
1307                Diff {
1308                    base_version,
1309                    line_ending,
1310                    edits,
1311                }
1312            })
1313    }
1314
1315    /// Spawns a background task that searches the buffer for any whitespace
1316    /// at the ends of a lines, and returns a `Diff` that removes that whitespace.
1317    pub fn remove_trailing_whitespace(&self, cx: &AppContext) -> Task<Diff> {
1318        let old_text = self.as_rope().clone();
1319        let line_ending = self.line_ending();
1320        let base_version = self.version();
1321        cx.background_executor().spawn(async move {
1322            let ranges = trailing_whitespace_ranges(&old_text);
1323            let empty = Arc::<str>::from("");
1324            Diff {
1325                base_version,
1326                line_ending,
1327                edits: ranges
1328                    .into_iter()
1329                    .map(|range| (range, empty.clone()))
1330                    .collect(),
1331            }
1332        })
1333    }
1334
1335    /// Ensures that the buffer ends with a single newline character, and
1336    /// no other whitespace.
1337    pub fn ensure_final_newline(&mut self, cx: &mut ModelContext<Self>) {
1338        let len = self.len();
1339        let mut offset = len;
1340        for chunk in self.as_rope().reversed_chunks_in_range(0..len) {
1341            let non_whitespace_len = chunk
1342                .trim_end_matches(|c: char| c.is_ascii_whitespace())
1343                .len();
1344            offset -= chunk.len();
1345            offset += non_whitespace_len;
1346            if non_whitespace_len != 0 {
1347                if offset == len - 1 && chunk.get(non_whitespace_len..) == Some("\n") {
1348                    return;
1349                }
1350                break;
1351            }
1352        }
1353        self.edit([(offset..len, "\n")], None, cx);
1354    }
1355
1356    /// Applies a diff to the buffer. If the buffer has changed since the given diff was
1357    /// calculated, then adjust the diff to account for those changes, and discard any
1358    /// parts of the diff that conflict with those changes.
1359    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1360        // Check for any edits to the buffer that have occurred since this diff
1361        // was computed.
1362        let snapshot = self.snapshot();
1363        let mut edits_since = snapshot.edits_since::<usize>(&diff.base_version).peekable();
1364        let mut delta = 0;
1365        let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
1366            while let Some(edit_since) = edits_since.peek() {
1367                // If the edit occurs after a diff hunk, then it does not
1368                // affect that hunk.
1369                if edit_since.old.start > range.end {
1370                    break;
1371                }
1372                // If the edit precedes the diff hunk, then adjust the hunk
1373                // to reflect the edit.
1374                else if edit_since.old.end < range.start {
1375                    delta += edit_since.new_len() as i64 - edit_since.old_len() as i64;
1376                    edits_since.next();
1377                }
1378                // If the edit intersects a diff hunk, then discard that hunk.
1379                else {
1380                    return None;
1381                }
1382            }
1383
1384            let start = (range.start as i64 + delta) as usize;
1385            let end = (range.end as i64 + delta) as usize;
1386            Some((start..end, new_text))
1387        });
1388
1389        self.start_transaction();
1390        self.text.set_line_ending(diff.line_ending);
1391        self.edit(adjusted_edits, None, cx);
1392        self.end_transaction(cx)
1393    }
1394
1395    /// Checks if the buffer has unsaved changes.
1396    pub fn is_dirty(&self) -> bool {
1397        self.file_fingerprint != self.as_rope().fingerprint()
1398            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1399    }
1400
1401    /// Checks if the buffer and its file have both changed since the buffer
1402    /// was last saved or reloaded.
1403    pub fn has_conflict(&self) -> bool {
1404        self.file_fingerprint != self.as_rope().fingerprint()
1405            && self
1406                .file
1407                .as_ref()
1408                .map_or(false, |file| file.mtime() > self.saved_mtime)
1409    }
1410
1411    /// Gets a [`Subscription`] that tracks all of the changes to the buffer's text.
1412    pub fn subscribe(&mut self) -> Subscription {
1413        self.text.subscribe()
1414    }
1415
1416    /// Starts a transaction, if one is not already in-progress. When undoing or
1417    /// redoing edits, all of the edits performed within a transaction are undone
1418    /// or redone together.
1419    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1420        self.start_transaction_at(Instant::now())
1421    }
1422
1423    /// Starts a transaction, providing the current time. Subsequent transactions
1424    /// that occur within a short period of time will be grouped together. This
1425    /// is controlled by the buffer's undo grouping duration.
1426    ///
1427    /// See [`Buffer::set_group_interval`].
1428    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1429        self.transaction_depth += 1;
1430        if self.was_dirty_before_starting_transaction.is_none() {
1431            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1432        }
1433        self.text.start_transaction_at(now)
1434    }
1435
1436    /// Terminates the current transaction, if this is the outermost transaction.
1437    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1438        self.end_transaction_at(Instant::now(), cx)
1439    }
1440
1441    /// Terminates the current transaction, providing the current time. Subsequent transactions
1442    /// that occur within a short period of time will be grouped together. This
1443    /// is controlled by the buffer's undo grouping duration.
1444    ///
1445    /// See [`Buffer::set_group_interval`].
1446    pub fn end_transaction_at(
1447        &mut self,
1448        now: Instant,
1449        cx: &mut ModelContext<Self>,
1450    ) -> Option<TransactionId> {
1451        assert!(self.transaction_depth > 0);
1452        self.transaction_depth -= 1;
1453        let was_dirty = if self.transaction_depth == 0 {
1454            self.was_dirty_before_starting_transaction.take().unwrap()
1455        } else {
1456            false
1457        };
1458        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1459            self.did_edit(&start_version, was_dirty, cx);
1460            Some(transaction_id)
1461        } else {
1462            None
1463        }
1464    }
1465
1466    /// Manually add a transaction to the buffer's undo history.
1467    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1468        self.text.push_transaction(transaction, now);
1469    }
1470
1471    /// Prevent the last transaction from being grouped with any subsequent transactions,
1472    /// even if they occur with the buffer's undo grouping duration.
1473    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1474        self.text.finalize_last_transaction()
1475    }
1476
1477    /// Manually group all changes since a given transaction.
1478    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1479        self.text.group_until_transaction(transaction_id);
1480    }
1481
1482    /// Manually remove a transaction from the buffer's undo history
1483    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1484        self.text.forget_transaction(transaction_id);
1485    }
1486
1487    /// Manually merge two adjacent transactions in the buffer's undo history.
1488    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
1489        self.text.merge_transactions(transaction, destination);
1490    }
1491
1492    /// Waits for the buffer to receive operations with the given timestamps.
1493    pub fn wait_for_edits(
1494        &mut self,
1495        edit_ids: impl IntoIterator<Item = clock::Lamport>,
1496    ) -> impl Future<Output = Result<()>> {
1497        self.text.wait_for_edits(edit_ids)
1498    }
1499
1500    /// Waits for the buffer to receive the operations necessary for resolving the given anchors.
1501    pub fn wait_for_anchors(
1502        &mut self,
1503        anchors: impl IntoIterator<Item = Anchor>,
1504    ) -> impl 'static + Future<Output = Result<()>> {
1505        self.text.wait_for_anchors(anchors)
1506    }
1507
1508    /// Waits for the buffer to receive operations up to the given version.
1509    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = Result<()>> {
1510        self.text.wait_for_version(version)
1511    }
1512
1513    /// Forces all futures returned by [`Buffer::wait_for_version`], [`Buffer::wait_for_edits`], or
1514    /// [`Buffer::wait_for_version`] to resolve with an error.
1515    pub fn give_up_waiting(&mut self) {
1516        self.text.give_up_waiting();
1517    }
1518
1519    /// Stores a set of selections that should be broadcasted to all of the buffer's replicas.
1520    pub fn set_active_selections(
1521        &mut self,
1522        selections: Arc<[Selection<Anchor>]>,
1523        line_mode: bool,
1524        cursor_shape: CursorShape,
1525        cx: &mut ModelContext<Self>,
1526    ) {
1527        let lamport_timestamp = self.text.lamport_clock.tick();
1528        self.remote_selections.insert(
1529            self.text.replica_id(),
1530            SelectionSet {
1531                selections: selections.clone(),
1532                lamport_timestamp,
1533                line_mode,
1534                cursor_shape,
1535            },
1536        );
1537        self.send_operation(
1538            Operation::UpdateSelections {
1539                selections,
1540                line_mode,
1541                lamport_timestamp,
1542                cursor_shape,
1543            },
1544            cx,
1545        );
1546    }
1547
1548    /// Clears the selections, so that other replicas of the buffer do not see any selections for
1549    /// this replica.
1550    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1551        if self
1552            .remote_selections
1553            .get(&self.text.replica_id())
1554            .map_or(true, |set| !set.selections.is_empty())
1555        {
1556            self.set_active_selections(Arc::from([]), false, Default::default(), cx);
1557        }
1558    }
1559
1560    /// Replaces the buffer's entire text.
1561    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Lamport>
1562    where
1563        T: Into<Arc<str>>,
1564    {
1565        self.autoindent_requests.clear();
1566        self.edit([(0..self.len(), text)], None, cx)
1567    }
1568
1569    /// Applies the given edits to the buffer. Each edit is specified as a range of text to
1570    /// delete, and a string of text to insert at that location.
1571    ///
1572    /// If an [`AutoindentMode`] is provided, then the buffer will enqueue an auto-indent
1573    /// request for the edited ranges, which will be processed when the buffer finishes
1574    /// parsing.
1575    ///
1576    /// Parsing takes place at the end of a transaction, and may compute synchronously
1577    /// or asynchronously, depending on the changes.
1578    pub fn edit<I, S, T>(
1579        &mut self,
1580        edits_iter: I,
1581        autoindent_mode: Option<AutoindentMode>,
1582        cx: &mut ModelContext<Self>,
1583    ) -> Option<clock::Lamport>
1584    where
1585        I: IntoIterator<Item = (Range<S>, T)>,
1586        S: ToOffset,
1587        T: Into<Arc<str>>,
1588    {
1589        // Skip invalid edits and coalesce contiguous ones.
1590        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1591        for (range, new_text) in edits_iter {
1592            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1593            if range.start > range.end {
1594                mem::swap(&mut range.start, &mut range.end);
1595            }
1596            let new_text = new_text.into();
1597            if !new_text.is_empty() || !range.is_empty() {
1598                if let Some((prev_range, prev_text)) = edits.last_mut() {
1599                    if prev_range.end >= range.start {
1600                        prev_range.end = cmp::max(prev_range.end, range.end);
1601                        *prev_text = format!("{prev_text}{new_text}").into();
1602                    } else {
1603                        edits.push((range, new_text));
1604                    }
1605                } else {
1606                    edits.push((range, new_text));
1607                }
1608            }
1609        }
1610        if edits.is_empty() {
1611            return None;
1612        }
1613
1614        self.start_transaction();
1615        self.pending_autoindent.take();
1616        let autoindent_request = autoindent_mode
1617            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1618
1619        let edit_operation = self.text.edit(edits.iter().cloned());
1620        let edit_id = edit_operation.timestamp();
1621
1622        if let Some((before_edit, mode)) = autoindent_request {
1623            let mut delta = 0isize;
1624            let entries = edits
1625                .into_iter()
1626                .enumerate()
1627                .zip(&edit_operation.as_edit().unwrap().new_text)
1628                .map(|((ix, (range, _)), new_text)| {
1629                    let new_text_length = new_text.len();
1630                    let old_start = range.start.to_point(&before_edit);
1631                    let new_start = (delta + range.start as isize) as usize;
1632                    delta += new_text_length as isize - (range.end as isize - range.start as isize);
1633
1634                    let mut range_of_insertion_to_indent = 0..new_text_length;
1635                    let mut first_line_is_new = false;
1636                    let mut original_indent_column = None;
1637
1638                    // When inserting an entire line at the beginning of an existing line,
1639                    // treat the insertion as new.
1640                    if new_text.contains('\n')
1641                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1642                    {
1643                        first_line_is_new = true;
1644                    }
1645
1646                    // When inserting text starting with a newline, avoid auto-indenting the
1647                    // previous line.
1648                    if new_text.starts_with('\n') {
1649                        range_of_insertion_to_indent.start += 1;
1650                        first_line_is_new = true;
1651                    }
1652
1653                    // Avoid auto-indenting after the insertion.
1654                    if let AutoindentMode::Block {
1655                        original_indent_columns,
1656                    } = &mode
1657                    {
1658                        original_indent_column =
1659                            Some(original_indent_columns.get(ix).copied().unwrap_or_else(|| {
1660                                indent_size_for_text(
1661                                    new_text[range_of_insertion_to_indent.clone()].chars(),
1662                                )
1663                                .len
1664                            }));
1665                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1666                            range_of_insertion_to_indent.end -= 1;
1667                        }
1668                    }
1669
1670                    AutoindentRequestEntry {
1671                        first_line_is_new,
1672                        original_indent_column,
1673                        indent_size: before_edit.language_indent_size_at(range.start, cx),
1674                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1675                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1676                    }
1677                })
1678                .collect();
1679
1680            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1681                before_edit,
1682                entries,
1683                is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
1684            }));
1685        }
1686
1687        self.end_transaction(cx);
1688        self.send_operation(Operation::Buffer(edit_operation), cx);
1689        Some(edit_id)
1690    }
1691
1692    fn did_edit(
1693        &mut self,
1694        old_version: &clock::Global,
1695        was_dirty: bool,
1696        cx: &mut ModelContext<Self>,
1697    ) {
1698        if self.edits_since::<usize>(old_version).next().is_none() {
1699            return;
1700        }
1701
1702        self.reparse(cx);
1703
1704        cx.emit(Event::Edited);
1705        if was_dirty != self.is_dirty() {
1706            cx.emit(Event::DirtyChanged);
1707        }
1708        cx.notify();
1709    }
1710
1711    /// Applies the given remote operations to the buffer.
1712    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1713        &mut self,
1714        ops: I,
1715        cx: &mut ModelContext<Self>,
1716    ) -> Result<()> {
1717        self.pending_autoindent.take();
1718        let was_dirty = self.is_dirty();
1719        let old_version = self.version.clone();
1720        let mut deferred_ops = Vec::new();
1721        let buffer_ops = ops
1722            .into_iter()
1723            .filter_map(|op| match op {
1724                Operation::Buffer(op) => Some(op),
1725                _ => {
1726                    if self.can_apply_op(&op) {
1727                        self.apply_op(op, cx);
1728                    } else {
1729                        deferred_ops.push(op);
1730                    }
1731                    None
1732                }
1733            })
1734            .collect::<Vec<_>>();
1735        self.text.apply_ops(buffer_ops)?;
1736        self.deferred_ops.insert(deferred_ops);
1737        self.flush_deferred_ops(cx);
1738        self.did_edit(&old_version, was_dirty, cx);
1739        // Notify independently of whether the buffer was edited as the operations could include a
1740        // selection update.
1741        cx.notify();
1742        Ok(())
1743    }
1744
1745    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1746        let mut deferred_ops = Vec::new();
1747        for op in self.deferred_ops.drain().iter().cloned() {
1748            if self.can_apply_op(&op) {
1749                self.apply_op(op, cx);
1750            } else {
1751                deferred_ops.push(op);
1752            }
1753        }
1754        self.deferred_ops.insert(deferred_ops);
1755    }
1756
1757    fn can_apply_op(&self, operation: &Operation) -> bool {
1758        match operation {
1759            Operation::Buffer(_) => {
1760                unreachable!("buffer operations should never be applied at this layer")
1761            }
1762            Operation::UpdateDiagnostics {
1763                diagnostics: diagnostic_set,
1764                ..
1765            } => diagnostic_set.iter().all(|diagnostic| {
1766                self.text.can_resolve(&diagnostic.range.start)
1767                    && self.text.can_resolve(&diagnostic.range.end)
1768            }),
1769            Operation::UpdateSelections { selections, .. } => selections
1770                .iter()
1771                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1772            Operation::UpdateCompletionTriggers { .. } => true,
1773        }
1774    }
1775
1776    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1777        match operation {
1778            Operation::Buffer(_) => {
1779                unreachable!("buffer operations should never be applied at this layer")
1780            }
1781            Operation::UpdateDiagnostics {
1782                server_id,
1783                diagnostics: diagnostic_set,
1784                lamport_timestamp,
1785            } => {
1786                let snapshot = self.snapshot();
1787                self.apply_diagnostic_update(
1788                    server_id,
1789                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1790                    lamport_timestamp,
1791                    cx,
1792                );
1793            }
1794            Operation::UpdateSelections {
1795                selections,
1796                lamport_timestamp,
1797                line_mode,
1798                cursor_shape,
1799            } => {
1800                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1801                    if set.lamport_timestamp > lamport_timestamp {
1802                        return;
1803                    }
1804                }
1805
1806                self.remote_selections.insert(
1807                    lamport_timestamp.replica_id,
1808                    SelectionSet {
1809                        selections,
1810                        lamport_timestamp,
1811                        line_mode,
1812                        cursor_shape,
1813                    },
1814                );
1815                self.text.lamport_clock.observe(lamport_timestamp);
1816                self.selections_update_count += 1;
1817            }
1818            Operation::UpdateCompletionTriggers {
1819                triggers,
1820                lamport_timestamp,
1821            } => {
1822                self.completion_triggers = triggers;
1823                self.text.lamport_clock.observe(lamport_timestamp);
1824            }
1825        }
1826    }
1827
1828    fn apply_diagnostic_update(
1829        &mut self,
1830        server_id: LanguageServerId,
1831        diagnostics: DiagnosticSet,
1832        lamport_timestamp: clock::Lamport,
1833        cx: &mut ModelContext<Self>,
1834    ) {
1835        if lamport_timestamp > self.diagnostics_timestamp {
1836            let ix = self.diagnostics.binary_search_by_key(&server_id, |e| e.0);
1837            if diagnostics.len() == 0 {
1838                if let Ok(ix) = ix {
1839                    self.diagnostics.remove(ix);
1840                }
1841            } else {
1842                match ix {
1843                    Err(ix) => self.diagnostics.insert(ix, (server_id, diagnostics)),
1844                    Ok(ix) => self.diagnostics[ix].1 = diagnostics,
1845                };
1846            }
1847            self.diagnostics_timestamp = lamport_timestamp;
1848            self.diagnostics_update_count += 1;
1849            self.text.lamport_clock.observe(lamport_timestamp);
1850            cx.notify();
1851            cx.emit(Event::DiagnosticsUpdated);
1852        }
1853    }
1854
1855    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1856        cx.emit(Event::Operation(operation));
1857    }
1858
1859    /// Removes the selections for a given peer.
1860    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1861        self.remote_selections.remove(&replica_id);
1862        cx.notify();
1863    }
1864
1865    /// Undoes the most recent transaction.
1866    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1867        let was_dirty = self.is_dirty();
1868        let old_version = self.version.clone();
1869
1870        if let Some((transaction_id, operation)) = self.text.undo() {
1871            self.send_operation(Operation::Buffer(operation), cx);
1872            self.did_edit(&old_version, was_dirty, cx);
1873            Some(transaction_id)
1874        } else {
1875            None
1876        }
1877    }
1878
1879    /// Manually undoes a specific transaction in the buffer's undo history.
1880    pub fn undo_transaction(
1881        &mut self,
1882        transaction_id: TransactionId,
1883        cx: &mut ModelContext<Self>,
1884    ) -> bool {
1885        let was_dirty = self.is_dirty();
1886        let old_version = self.version.clone();
1887        if let Some(operation) = self.text.undo_transaction(transaction_id) {
1888            self.send_operation(Operation::Buffer(operation), cx);
1889            self.did_edit(&old_version, was_dirty, cx);
1890            true
1891        } else {
1892            false
1893        }
1894    }
1895
1896    /// Manually undoes all changes after a given transaction in the buffer's undo history.
1897    pub fn undo_to_transaction(
1898        &mut self,
1899        transaction_id: TransactionId,
1900        cx: &mut ModelContext<Self>,
1901    ) -> bool {
1902        let was_dirty = self.is_dirty();
1903        let old_version = self.version.clone();
1904
1905        let operations = self.text.undo_to_transaction(transaction_id);
1906        let undone = !operations.is_empty();
1907        for operation in operations {
1908            self.send_operation(Operation::Buffer(operation), cx);
1909        }
1910        if undone {
1911            self.did_edit(&old_version, was_dirty, cx)
1912        }
1913        undone
1914    }
1915
1916    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1917        let was_dirty = self.is_dirty();
1918        let old_version = self.version.clone();
1919
1920        if let Some((transaction_id, operation)) = self.text.redo() {
1921            self.send_operation(Operation::Buffer(operation), cx);
1922            self.did_edit(&old_version, was_dirty, cx);
1923            Some(transaction_id)
1924        } else {
1925            None
1926        }
1927    }
1928
1929    pub fn redo_to_transaction(
1930        &mut self,
1931        transaction_id: TransactionId,
1932        cx: &mut ModelContext<Self>,
1933    ) -> bool {
1934        let was_dirty = self.is_dirty();
1935        let old_version = self.version.clone();
1936
1937        let operations = self.text.redo_to_transaction(transaction_id);
1938        let redone = !operations.is_empty();
1939        for operation in operations {
1940            self.send_operation(Operation::Buffer(operation), cx);
1941        }
1942        if redone {
1943            self.did_edit(&old_version, was_dirty, cx)
1944        }
1945        redone
1946    }
1947
1948    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1949        self.completion_triggers = triggers.clone();
1950        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1951        self.send_operation(
1952            Operation::UpdateCompletionTriggers {
1953                triggers,
1954                lamport_timestamp: self.completion_triggers_timestamp,
1955            },
1956            cx,
1957        );
1958        cx.notify();
1959    }
1960
1961    pub fn completion_triggers(&self) -> &[String] {
1962        &self.completion_triggers
1963    }
1964}
1965
1966#[cfg(any(test, feature = "test-support"))]
1967impl Buffer {
1968    pub fn edit_via_marked_text(
1969        &mut self,
1970        marked_string: &str,
1971        autoindent_mode: Option<AutoindentMode>,
1972        cx: &mut ModelContext<Self>,
1973    ) {
1974        let edits = self.edits_for_marked_text(marked_string);
1975        self.edit(edits, autoindent_mode, cx);
1976    }
1977
1978    pub fn set_group_interval(&mut self, group_interval: Duration) {
1979        self.text.set_group_interval(group_interval);
1980    }
1981
1982    pub fn randomly_edit<T>(
1983        &mut self,
1984        rng: &mut T,
1985        old_range_count: usize,
1986        cx: &mut ModelContext<Self>,
1987    ) where
1988        T: rand::Rng,
1989    {
1990        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1991        let mut last_end = None;
1992        for _ in 0..old_range_count {
1993            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1994                break;
1995            }
1996
1997            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1998            let mut range = self.random_byte_range(new_start, rng);
1999            if rng.gen_bool(0.2) {
2000                mem::swap(&mut range.start, &mut range.end);
2001            }
2002            last_end = Some(range.end);
2003
2004            let new_text_len = rng.gen_range(0..10);
2005            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
2006
2007            edits.push((range, new_text));
2008        }
2009        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
2010        self.edit(edits, None, cx);
2011    }
2012
2013    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
2014        let was_dirty = self.is_dirty();
2015        let old_version = self.version.clone();
2016
2017        let ops = self.text.randomly_undo_redo(rng);
2018        if !ops.is_empty() {
2019            for op in ops {
2020                self.send_operation(Operation::Buffer(op), cx);
2021                self.did_edit(&old_version, was_dirty, cx);
2022            }
2023        }
2024    }
2025}
2026
2027impl EventEmitter<Event> for Buffer {}
2028
2029impl Deref for Buffer {
2030    type Target = TextBuffer;
2031
2032    fn deref(&self) -> &Self::Target {
2033        &self.text
2034    }
2035}
2036
2037impl BufferSnapshot {
2038    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
2039        indent_size_for_line(self, row)
2040    }
2041
2042    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
2043        let settings = language_settings(self.language_at(position), self.file(), cx);
2044        if settings.hard_tabs {
2045            IndentSize::tab()
2046        } else {
2047            IndentSize::spaces(settings.tab_size.get())
2048        }
2049    }
2050
2051    pub fn suggested_indents(
2052        &self,
2053        rows: impl Iterator<Item = u32>,
2054        single_indent_size: IndentSize,
2055    ) -> BTreeMap<u32, IndentSize> {
2056        let mut result = BTreeMap::new();
2057
2058        for row_range in contiguous_ranges(rows, 10) {
2059            let suggestions = match self.suggest_autoindents(row_range.clone()) {
2060                Some(suggestions) => suggestions,
2061                _ => break,
2062            };
2063
2064            for (row, suggestion) in row_range.zip(suggestions) {
2065                let indent_size = if let Some(suggestion) = suggestion {
2066                    result
2067                        .get(&suggestion.basis_row)
2068                        .copied()
2069                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
2070                        .with_delta(suggestion.delta, single_indent_size)
2071                } else {
2072                    self.indent_size_for_line(row)
2073                };
2074
2075                result.insert(row, indent_size);
2076            }
2077        }
2078
2079        result
2080    }
2081
2082    fn suggest_autoindents(
2083        &self,
2084        row_range: Range<u32>,
2085    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
2086        let config = &self.language.as_ref()?.config;
2087        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2088
2089        // Find the suggested indentation ranges based on the syntax tree.
2090        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
2091        let end = Point::new(row_range.end, 0);
2092        let range = (start..end).to_offset(&self.text);
2093        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2094            Some(&grammar.indents_config.as_ref()?.query)
2095        });
2096        let indent_configs = matches
2097            .grammars()
2098            .iter()
2099            .map(|grammar| grammar.indents_config.as_ref().unwrap())
2100            .collect::<Vec<_>>();
2101
2102        let mut indent_ranges = Vec::<Range<Point>>::new();
2103        let mut outdent_positions = Vec::<Point>::new();
2104        while let Some(mat) = matches.peek() {
2105            let mut start: Option<Point> = None;
2106            let mut end: Option<Point> = None;
2107
2108            let config = &indent_configs[mat.grammar_index];
2109            for capture in mat.captures {
2110                if capture.index == config.indent_capture_ix {
2111                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
2112                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
2113                } else if Some(capture.index) == config.start_capture_ix {
2114                    start = Some(Point::from_ts_point(capture.node.end_position()));
2115                } else if Some(capture.index) == config.end_capture_ix {
2116                    end = Some(Point::from_ts_point(capture.node.start_position()));
2117                } else if Some(capture.index) == config.outdent_capture_ix {
2118                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
2119                }
2120            }
2121
2122            matches.advance();
2123            if let Some((start, end)) = start.zip(end) {
2124                if start.row == end.row {
2125                    continue;
2126                }
2127
2128                let range = start..end;
2129                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
2130                    Err(ix) => indent_ranges.insert(ix, range),
2131                    Ok(ix) => {
2132                        let prev_range = &mut indent_ranges[ix];
2133                        prev_range.end = prev_range.end.max(range.end);
2134                    }
2135                }
2136            }
2137        }
2138
2139        let mut error_ranges = Vec::<Range<Point>>::new();
2140        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2141            Some(&grammar.error_query)
2142        });
2143        while let Some(mat) = matches.peek() {
2144            let node = mat.captures[0].node;
2145            let start = Point::from_ts_point(node.start_position());
2146            let end = Point::from_ts_point(node.end_position());
2147            let range = start..end;
2148            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
2149                Ok(ix) | Err(ix) => ix,
2150            };
2151            let mut end_ix = ix;
2152            while let Some(existing_range) = error_ranges.get(end_ix) {
2153                if existing_range.end < end {
2154                    end_ix += 1;
2155                } else {
2156                    break;
2157                }
2158            }
2159            error_ranges.splice(ix..end_ix, [range]);
2160            matches.advance();
2161        }
2162
2163        outdent_positions.sort();
2164        for outdent_position in outdent_positions {
2165            // find the innermost indent range containing this outdent_position
2166            // set its end to the outdent position
2167            if let Some(range_to_truncate) = indent_ranges
2168                .iter_mut()
2169                .filter(|indent_range| indent_range.contains(&outdent_position))
2170                .last()
2171            {
2172                range_to_truncate.end = outdent_position;
2173            }
2174        }
2175
2176        // Find the suggested indentation increases and decreased based on regexes.
2177        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
2178        self.for_each_line(
2179            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
2180                ..Point::new(row_range.end, 0),
2181            |row, line| {
2182                if config
2183                    .decrease_indent_pattern
2184                    .as_ref()
2185                    .map_or(false, |regex| regex.is_match(line))
2186                {
2187                    indent_change_rows.push((row, Ordering::Less));
2188                }
2189                if config
2190                    .increase_indent_pattern
2191                    .as_ref()
2192                    .map_or(false, |regex| regex.is_match(line))
2193                {
2194                    indent_change_rows.push((row + 1, Ordering::Greater));
2195                }
2196            },
2197        );
2198
2199        let mut indent_changes = indent_change_rows.into_iter().peekable();
2200        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
2201            prev_non_blank_row.unwrap_or(0)
2202        } else {
2203            row_range.start.saturating_sub(1)
2204        };
2205        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
2206        Some(row_range.map(move |row| {
2207            let row_start = Point::new(row, self.indent_size_for_line(row).len);
2208
2209            let mut indent_from_prev_row = false;
2210            let mut outdent_from_prev_row = false;
2211            let mut outdent_to_row = u32::MAX;
2212
2213            while let Some((indent_row, delta)) = indent_changes.peek() {
2214                match indent_row.cmp(&row) {
2215                    Ordering::Equal => match delta {
2216                        Ordering::Less => outdent_from_prev_row = true,
2217                        Ordering::Greater => indent_from_prev_row = true,
2218                        _ => {}
2219                    },
2220
2221                    Ordering::Greater => break,
2222                    Ordering::Less => {}
2223                }
2224
2225                indent_changes.next();
2226            }
2227
2228            for range in &indent_ranges {
2229                if range.start.row >= row {
2230                    break;
2231                }
2232                if range.start.row == prev_row && range.end > row_start {
2233                    indent_from_prev_row = true;
2234                }
2235                if range.end > prev_row_start && range.end <= row_start {
2236                    outdent_to_row = outdent_to_row.min(range.start.row);
2237                }
2238            }
2239
2240            let within_error = error_ranges
2241                .iter()
2242                .any(|e| e.start.row < row && e.end > row_start);
2243
2244            let suggestion = if outdent_to_row == prev_row
2245                || (outdent_from_prev_row && indent_from_prev_row)
2246            {
2247                Some(IndentSuggestion {
2248                    basis_row: prev_row,
2249                    delta: Ordering::Equal,
2250                    within_error,
2251                })
2252            } else if indent_from_prev_row {
2253                Some(IndentSuggestion {
2254                    basis_row: prev_row,
2255                    delta: Ordering::Greater,
2256                    within_error,
2257                })
2258            } else if outdent_to_row < prev_row {
2259                Some(IndentSuggestion {
2260                    basis_row: outdent_to_row,
2261                    delta: Ordering::Equal,
2262                    within_error,
2263                })
2264            } else if outdent_from_prev_row {
2265                Some(IndentSuggestion {
2266                    basis_row: prev_row,
2267                    delta: Ordering::Less,
2268                    within_error,
2269                })
2270            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
2271            {
2272                Some(IndentSuggestion {
2273                    basis_row: prev_row,
2274                    delta: Ordering::Equal,
2275                    within_error,
2276                })
2277            } else {
2278                None
2279            };
2280
2281            prev_row = row;
2282            prev_row_start = row_start;
2283            suggestion
2284        }))
2285    }
2286
2287    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2288        while row > 0 {
2289            row -= 1;
2290            if !self.is_line_blank(row) {
2291                return Some(row);
2292            }
2293        }
2294        None
2295    }
2296
2297    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
2298        let range = range.start.to_offset(self)..range.end.to_offset(self);
2299
2300        let mut syntax = None;
2301        let mut diagnostic_endpoints = Vec::new();
2302        if language_aware {
2303            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
2304                grammar.highlights_query.as_ref()
2305            });
2306            let highlight_maps = captures
2307                .grammars()
2308                .into_iter()
2309                .map(|grammar| grammar.highlight_map())
2310                .collect();
2311            syntax = Some((captures, highlight_maps));
2312            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
2313                diagnostic_endpoints.push(DiagnosticEndpoint {
2314                    offset: entry.range.start,
2315                    is_start: true,
2316                    severity: entry.diagnostic.severity,
2317                    is_unnecessary: entry.diagnostic.is_unnecessary,
2318                });
2319                diagnostic_endpoints.push(DiagnosticEndpoint {
2320                    offset: entry.range.end,
2321                    is_start: false,
2322                    severity: entry.diagnostic.severity,
2323                    is_unnecessary: entry.diagnostic.is_unnecessary,
2324                });
2325            }
2326            diagnostic_endpoints
2327                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2328        }
2329
2330        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
2331    }
2332
2333    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2334        let mut line = String::new();
2335        let mut row = range.start.row;
2336        for chunk in self
2337            .as_rope()
2338            .chunks_in_range(range.to_offset(self))
2339            .chain(["\n"])
2340        {
2341            for (newline_ix, text) in chunk.split('\n').enumerate() {
2342                if newline_ix > 0 {
2343                    callback(row, &line);
2344                    row += 1;
2345                    line.clear();
2346                }
2347                line.push_str(text);
2348            }
2349        }
2350    }
2351
2352    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayerInfo> + '_ {
2353        self.syntax.layers_for_range(0..self.len(), &self.text)
2354    }
2355
2356    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayerInfo> {
2357        let offset = position.to_offset(self);
2358        self.syntax
2359            .layers_for_range(offset..offset, &self.text)
2360            .filter(|l| l.node().end_byte() > offset)
2361            .last()
2362    }
2363
2364    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2365        self.syntax_layer_at(position)
2366            .map(|info| info.language)
2367            .or(self.language.as_ref())
2368    }
2369
2370    pub fn settings_at<'a, D: ToOffset>(
2371        &self,
2372        position: D,
2373        cx: &'a AppContext,
2374    ) -> &'a LanguageSettings {
2375        language_settings(self.language_at(position), self.file.as_ref(), cx)
2376    }
2377
2378    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2379        let offset = position.to_offset(self);
2380        let mut scope = None;
2381        let mut smallest_range: Option<Range<usize>> = None;
2382
2383        // Use the layer that has the smallest node intersecting the given point.
2384        for layer in self.syntax.layers_for_range(offset..offset, &self.text) {
2385            let mut cursor = layer.node().walk();
2386
2387            let mut range = None;
2388            loop {
2389                let child_range = cursor.node().byte_range();
2390                if !child_range.to_inclusive().contains(&offset) {
2391                    break;
2392                }
2393
2394                range = Some(child_range);
2395                if cursor.goto_first_child_for_byte(offset).is_none() {
2396                    break;
2397                }
2398            }
2399
2400            if let Some(range) = range {
2401                if smallest_range
2402                    .as_ref()
2403                    .map_or(true, |smallest_range| range.len() < smallest_range.len())
2404                {
2405                    smallest_range = Some(range);
2406                    scope = Some(LanguageScope {
2407                        language: layer.language.clone(),
2408                        override_id: layer.override_id(offset, &self.text),
2409                    });
2410                }
2411            }
2412        }
2413
2414        scope.or_else(|| {
2415            self.language.clone().map(|language| LanguageScope {
2416                language,
2417                override_id: None,
2418            })
2419        })
2420    }
2421
2422    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2423        let mut start = start.to_offset(self);
2424        let mut end = start;
2425        let mut next_chars = self.chars_at(start).peekable();
2426        let mut prev_chars = self.reversed_chars_at(start).peekable();
2427
2428        let scope = self.language_scope_at(start);
2429        let kind = |c| char_kind(&scope, c);
2430        let word_kind = cmp::max(
2431            prev_chars.peek().copied().map(kind),
2432            next_chars.peek().copied().map(kind),
2433        );
2434
2435        for ch in prev_chars {
2436            if Some(kind(ch)) == word_kind && ch != '\n' {
2437                start -= ch.len_utf8();
2438            } else {
2439                break;
2440            }
2441        }
2442
2443        for ch in next_chars {
2444            if Some(kind(ch)) == word_kind && ch != '\n' {
2445                end += ch.len_utf8();
2446            } else {
2447                break;
2448            }
2449        }
2450
2451        (start..end, word_kind)
2452    }
2453
2454    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2455        let range = range.start.to_offset(self)..range.end.to_offset(self);
2456        let mut result: Option<Range<usize>> = None;
2457        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2458            let mut cursor = layer.node().walk();
2459
2460            // Descend to the first leaf that touches the start of the range,
2461            // and if the range is non-empty, extends beyond the start.
2462            while cursor.goto_first_child_for_byte(range.start).is_some() {
2463                if !range.is_empty() && cursor.node().end_byte() == range.start {
2464                    cursor.goto_next_sibling();
2465                }
2466            }
2467
2468            // Ascend to the smallest ancestor that strictly contains the range.
2469            loop {
2470                let node_range = cursor.node().byte_range();
2471                if node_range.start <= range.start
2472                    && node_range.end >= range.end
2473                    && node_range.len() > range.len()
2474                {
2475                    break;
2476                }
2477                if !cursor.goto_parent() {
2478                    continue 'outer;
2479                }
2480            }
2481
2482            let left_node = cursor.node();
2483            let mut layer_result = left_node.byte_range();
2484
2485            // For an empty range, try to find another node immediately to the right of the range.
2486            if left_node.end_byte() == range.start {
2487                let mut right_node = None;
2488                while !cursor.goto_next_sibling() {
2489                    if !cursor.goto_parent() {
2490                        break;
2491                    }
2492                }
2493
2494                while cursor.node().start_byte() == range.start {
2495                    right_node = Some(cursor.node());
2496                    if !cursor.goto_first_child() {
2497                        break;
2498                    }
2499                }
2500
2501                // If there is a candidate node on both sides of the (empty) range, then
2502                // decide between the two by favoring a named node over an anonymous token.
2503                // If both nodes are the same in that regard, favor the right one.
2504                if let Some(right_node) = right_node {
2505                    if right_node.is_named() || !left_node.is_named() {
2506                        layer_result = right_node.byte_range();
2507                    }
2508                }
2509            }
2510
2511            if let Some(previous_result) = &result {
2512                if previous_result.len() < layer_result.len() {
2513                    continue;
2514                }
2515            }
2516            result = Some(layer_result);
2517        }
2518
2519        result
2520    }
2521
2522    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2523        self.outline_items_containing(0..self.len(), true, theme)
2524            .map(Outline::new)
2525    }
2526
2527    pub fn symbols_containing<T: ToOffset>(
2528        &self,
2529        position: T,
2530        theme: Option<&SyntaxTheme>,
2531    ) -> Option<Vec<OutlineItem<Anchor>>> {
2532        let position = position.to_offset(self);
2533        let mut items = self.outline_items_containing(
2534            position.saturating_sub(1)..self.len().min(position + 1),
2535            false,
2536            theme,
2537        )?;
2538        let mut prev_depth = None;
2539        items.retain(|item| {
2540            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2541            prev_depth = Some(item.depth);
2542            result
2543        });
2544        Some(items)
2545    }
2546
2547    fn outline_items_containing(
2548        &self,
2549        range: Range<usize>,
2550        include_extra_context: bool,
2551        theme: Option<&SyntaxTheme>,
2552    ) -> Option<Vec<OutlineItem<Anchor>>> {
2553        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2554            grammar.outline_config.as_ref().map(|c| &c.query)
2555        });
2556        let configs = matches
2557            .grammars()
2558            .iter()
2559            .map(|g| g.outline_config.as_ref().unwrap())
2560            .collect::<Vec<_>>();
2561
2562        let mut stack = Vec::<Range<usize>>::new();
2563        let mut items = Vec::new();
2564        while let Some(mat) = matches.peek() {
2565            let config = &configs[mat.grammar_index];
2566            let item_node = mat.captures.iter().find_map(|cap| {
2567                if cap.index == config.item_capture_ix {
2568                    Some(cap.node)
2569                } else {
2570                    None
2571                }
2572            })?;
2573
2574            let item_range = item_node.byte_range();
2575            if item_range.end < range.start || item_range.start > range.end {
2576                matches.advance();
2577                continue;
2578            }
2579
2580            let mut buffer_ranges = Vec::new();
2581            for capture in mat.captures {
2582                let node_is_name;
2583                if capture.index == config.name_capture_ix {
2584                    node_is_name = true;
2585                } else if Some(capture.index) == config.context_capture_ix
2586                    || (Some(capture.index) == config.extra_context_capture_ix
2587                        && include_extra_context)
2588                {
2589                    node_is_name = false;
2590                } else {
2591                    continue;
2592                }
2593
2594                let mut range = capture.node.start_byte()..capture.node.end_byte();
2595                let start = capture.node.start_position();
2596                if capture.node.end_position().row > start.row {
2597                    range.end =
2598                        range.start + self.line_len(start.row as u32) as usize - start.column;
2599                }
2600
2601                buffer_ranges.push((range, node_is_name));
2602            }
2603
2604            if buffer_ranges.is_empty() {
2605                continue;
2606            }
2607
2608            let mut text = String::new();
2609            let mut highlight_ranges = Vec::new();
2610            let mut name_ranges = Vec::new();
2611            let mut chunks = self.chunks(
2612                buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end,
2613                true,
2614            );
2615            let mut last_buffer_range_end = 0;
2616            for (buffer_range, is_name) in buffer_ranges {
2617                if !text.is_empty() && buffer_range.start > last_buffer_range_end {
2618                    text.push(' ');
2619                }
2620                last_buffer_range_end = buffer_range.end;
2621                if is_name {
2622                    let mut start = text.len();
2623                    let end = start + buffer_range.len();
2624
2625                    // When multiple names are captured, then the matcheable text
2626                    // includes the whitespace in between the names.
2627                    if !name_ranges.is_empty() {
2628                        start -= 1;
2629                    }
2630
2631                    name_ranges.push(start..end);
2632                }
2633
2634                let mut offset = buffer_range.start;
2635                chunks.seek(offset);
2636                for mut chunk in chunks.by_ref() {
2637                    if chunk.text.len() > buffer_range.end - offset {
2638                        chunk.text = &chunk.text[0..(buffer_range.end - offset)];
2639                        offset = buffer_range.end;
2640                    } else {
2641                        offset += chunk.text.len();
2642                    }
2643                    let style = chunk
2644                        .syntax_highlight_id
2645                        .zip(theme)
2646                        .and_then(|(highlight, theme)| highlight.style(theme));
2647                    if let Some(style) = style {
2648                        let start = text.len();
2649                        let end = start + chunk.text.len();
2650                        highlight_ranges.push((start..end, style));
2651                    }
2652                    text.push_str(chunk.text);
2653                    if offset >= buffer_range.end {
2654                        break;
2655                    }
2656                }
2657            }
2658
2659            matches.advance();
2660            while stack.last().map_or(false, |prev_range| {
2661                prev_range.start > item_range.start || prev_range.end < item_range.end
2662            }) {
2663                stack.pop();
2664            }
2665            stack.push(item_range.clone());
2666
2667            items.push(OutlineItem {
2668                depth: stack.len() - 1,
2669                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2670                text,
2671                highlight_ranges,
2672                name_ranges,
2673            })
2674        }
2675        Some(items)
2676    }
2677
2678    pub fn matches(
2679        &self,
2680        range: Range<usize>,
2681        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
2682    ) -> SyntaxMapMatches {
2683        self.syntax.matches(range, self, query)
2684    }
2685
2686    /// Returns bracket range pairs overlapping or adjacent to `range`
2687    pub fn bracket_ranges<'a, T: ToOffset>(
2688        &'a self,
2689        range: Range<T>,
2690    ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + 'a {
2691        // Find bracket pairs that *inclusively* contain the given range.
2692        let range = range.start.to_offset(self).saturating_sub(1)
2693            ..self.len().min(range.end.to_offset(self) + 1);
2694
2695        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2696            grammar.brackets_config.as_ref().map(|c| &c.query)
2697        });
2698        let configs = matches
2699            .grammars()
2700            .iter()
2701            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2702            .collect::<Vec<_>>();
2703
2704        iter::from_fn(move || {
2705            while let Some(mat) = matches.peek() {
2706                let mut open = None;
2707                let mut close = None;
2708                let config = &configs[mat.grammar_index];
2709                for capture in mat.captures {
2710                    if capture.index == config.open_capture_ix {
2711                        open = Some(capture.node.byte_range());
2712                    } else if capture.index == config.close_capture_ix {
2713                        close = Some(capture.node.byte_range());
2714                    }
2715                }
2716
2717                matches.advance();
2718
2719                let Some((open, close)) = open.zip(close) else {
2720                    continue;
2721                };
2722
2723                let bracket_range = open.start..=close.end;
2724                if !bracket_range.overlaps(&range) {
2725                    continue;
2726                }
2727
2728                return Some((open, close));
2729            }
2730            None
2731        })
2732    }
2733
2734    #[allow(clippy::type_complexity)]
2735    pub fn remote_selections_in_range(
2736        &self,
2737        range: Range<Anchor>,
2738    ) -> impl Iterator<
2739        Item = (
2740            ReplicaId,
2741            bool,
2742            CursorShape,
2743            impl Iterator<Item = &Selection<Anchor>> + '_,
2744        ),
2745    > + '_ {
2746        self.remote_selections
2747            .iter()
2748            .filter(|(replica_id, set)| {
2749                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2750            })
2751            .map(move |(replica_id, set)| {
2752                let start_ix = match set.selections.binary_search_by(|probe| {
2753                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2754                }) {
2755                    Ok(ix) | Err(ix) => ix,
2756                };
2757                let end_ix = match set.selections.binary_search_by(|probe| {
2758                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2759                }) {
2760                    Ok(ix) | Err(ix) => ix,
2761                };
2762
2763                (
2764                    *replica_id,
2765                    set.line_mode,
2766                    set.cursor_shape,
2767                    set.selections[start_ix..end_ix].iter(),
2768                )
2769            })
2770    }
2771
2772    pub fn git_diff_hunks_in_row_range<'a>(
2773        &'a self,
2774        range: Range<u32>,
2775    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2776        self.git_diff.hunks_in_row_range(range, self)
2777    }
2778
2779    pub fn git_diff_hunks_intersecting_range<'a>(
2780        &'a self,
2781        range: Range<Anchor>,
2782    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2783        self.git_diff.hunks_intersecting_range(range, self)
2784    }
2785
2786    pub fn git_diff_hunks_intersecting_range_rev<'a>(
2787        &'a self,
2788        range: Range<Anchor>,
2789    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2790        self.git_diff.hunks_intersecting_range_rev(range, self)
2791    }
2792
2793    pub fn diagnostics_in_range<'a, T, O>(
2794        &'a self,
2795        search_range: Range<T>,
2796        reversed: bool,
2797    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2798    where
2799        T: 'a + Clone + ToOffset,
2800        O: 'a + FromAnchor + Ord,
2801    {
2802        let mut iterators: Vec<_> = self
2803            .diagnostics
2804            .iter()
2805            .map(|(_, collection)| {
2806                collection
2807                    .range::<T, O>(search_range.clone(), self, true, reversed)
2808                    .peekable()
2809            })
2810            .collect();
2811
2812        std::iter::from_fn(move || {
2813            let (next_ix, _) = iterators
2814                .iter_mut()
2815                .enumerate()
2816                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
2817                .min_by(|(_, a), (_, b)| a.range.start.cmp(&b.range.start))?;
2818            iterators[next_ix].next()
2819        })
2820    }
2821
2822    pub fn diagnostic_groups(
2823        &self,
2824        language_server_id: Option<LanguageServerId>,
2825    ) -> Vec<(LanguageServerId, DiagnosticGroup<Anchor>)> {
2826        let mut groups = Vec::new();
2827
2828        if let Some(language_server_id) = language_server_id {
2829            if let Ok(ix) = self
2830                .diagnostics
2831                .binary_search_by_key(&language_server_id, |e| e.0)
2832            {
2833                self.diagnostics[ix]
2834                    .1
2835                    .groups(language_server_id, &mut groups, self);
2836            }
2837        } else {
2838            for (language_server_id, diagnostics) in self.diagnostics.iter() {
2839                diagnostics.groups(*language_server_id, &mut groups, self);
2840            }
2841        }
2842
2843        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
2844            let a_start = &group_a.entries[group_a.primary_ix].range.start;
2845            let b_start = &group_b.entries[group_b.primary_ix].range.start;
2846            a_start.cmp(b_start, self).then_with(|| id_a.cmp(&id_b))
2847        });
2848
2849        groups
2850    }
2851
2852    pub fn diagnostic_group<'a, O>(
2853        &'a self,
2854        group_id: usize,
2855    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2856    where
2857        O: 'a + FromAnchor,
2858    {
2859        self.diagnostics
2860            .iter()
2861            .flat_map(move |(_, set)| set.group(group_id, self))
2862    }
2863
2864    pub fn diagnostics_update_count(&self) -> usize {
2865        self.diagnostics_update_count
2866    }
2867
2868    pub fn parse_count(&self) -> usize {
2869        self.parse_count
2870    }
2871
2872    pub fn selections_update_count(&self) -> usize {
2873        self.selections_update_count
2874    }
2875
2876    pub fn file(&self) -> Option<&Arc<dyn File>> {
2877        self.file.as_ref()
2878    }
2879
2880    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2881        if let Some(file) = self.file() {
2882            if file.path().file_name().is_none() || include_root {
2883                Some(file.full_path(cx))
2884            } else {
2885                Some(file.path().to_path_buf())
2886            }
2887        } else {
2888            None
2889        }
2890    }
2891
2892    pub fn file_update_count(&self) -> usize {
2893        self.file_update_count
2894    }
2895
2896    pub fn git_diff_update_count(&self) -> usize {
2897        self.git_diff_update_count
2898    }
2899}
2900
2901fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2902    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2903}
2904
2905pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2906    let mut result = IndentSize::spaces(0);
2907    for c in text {
2908        let kind = match c {
2909            ' ' => IndentKind::Space,
2910            '\t' => IndentKind::Tab,
2911            _ => break,
2912        };
2913        if result.len == 0 {
2914            result.kind = kind;
2915        }
2916        result.len += 1;
2917    }
2918    result
2919}
2920
2921impl Clone for BufferSnapshot {
2922    fn clone(&self) -> Self {
2923        Self {
2924            text: self.text.clone(),
2925            git_diff: self.git_diff.clone(),
2926            syntax: self.syntax.clone(),
2927            file: self.file.clone(),
2928            remote_selections: self.remote_selections.clone(),
2929            diagnostics: self.diagnostics.clone(),
2930            selections_update_count: self.selections_update_count,
2931            diagnostics_update_count: self.diagnostics_update_count,
2932            file_update_count: self.file_update_count,
2933            git_diff_update_count: self.git_diff_update_count,
2934            language: self.language.clone(),
2935            parse_count: self.parse_count,
2936        }
2937    }
2938}
2939
2940impl Deref for BufferSnapshot {
2941    type Target = text::BufferSnapshot;
2942
2943    fn deref(&self) -> &Self::Target {
2944        &self.text
2945    }
2946}
2947
2948unsafe impl<'a> Send for BufferChunks<'a> {}
2949
2950impl<'a> BufferChunks<'a> {
2951    pub(crate) fn new(
2952        text: &'a Rope,
2953        range: Range<usize>,
2954        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2955        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2956    ) -> Self {
2957        let mut highlights = None;
2958        if let Some((captures, highlight_maps)) = syntax {
2959            highlights = Some(BufferChunkHighlights {
2960                captures,
2961                next_capture: None,
2962                stack: Default::default(),
2963                highlight_maps,
2964            })
2965        }
2966
2967        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2968        let chunks = text.chunks_in_range(range.clone());
2969
2970        BufferChunks {
2971            range,
2972            chunks,
2973            diagnostic_endpoints,
2974            error_depth: 0,
2975            warning_depth: 0,
2976            information_depth: 0,
2977            hint_depth: 0,
2978            unnecessary_depth: 0,
2979            highlights,
2980        }
2981    }
2982
2983    pub fn seek(&mut self, offset: usize) {
2984        self.range.start = offset;
2985        self.chunks.seek(self.range.start);
2986        if let Some(highlights) = self.highlights.as_mut() {
2987            highlights
2988                .stack
2989                .retain(|(end_offset, _)| *end_offset > offset);
2990            if let Some(capture) = &highlights.next_capture {
2991                if offset >= capture.node.start_byte() {
2992                    let next_capture_end = capture.node.end_byte();
2993                    if offset < next_capture_end {
2994                        highlights.stack.push((
2995                            next_capture_end,
2996                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2997                        ));
2998                    }
2999                    highlights.next_capture.take();
3000                }
3001            }
3002            highlights.captures.set_byte_range(self.range.clone());
3003        }
3004    }
3005
3006    pub fn offset(&self) -> usize {
3007        self.range.start
3008    }
3009
3010    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
3011        let depth = match endpoint.severity {
3012            DiagnosticSeverity::ERROR => &mut self.error_depth,
3013            DiagnosticSeverity::WARNING => &mut self.warning_depth,
3014            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
3015            DiagnosticSeverity::HINT => &mut self.hint_depth,
3016            _ => return,
3017        };
3018        if endpoint.is_start {
3019            *depth += 1;
3020        } else {
3021            *depth -= 1;
3022        }
3023
3024        if endpoint.is_unnecessary {
3025            if endpoint.is_start {
3026                self.unnecessary_depth += 1;
3027            } else {
3028                self.unnecessary_depth -= 1;
3029            }
3030        }
3031    }
3032
3033    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
3034        if self.error_depth > 0 {
3035            Some(DiagnosticSeverity::ERROR)
3036        } else if self.warning_depth > 0 {
3037            Some(DiagnosticSeverity::WARNING)
3038        } else if self.information_depth > 0 {
3039            Some(DiagnosticSeverity::INFORMATION)
3040        } else if self.hint_depth > 0 {
3041            Some(DiagnosticSeverity::HINT)
3042        } else {
3043            None
3044        }
3045    }
3046
3047    fn current_code_is_unnecessary(&self) -> bool {
3048        self.unnecessary_depth > 0
3049    }
3050}
3051
3052impl<'a> Iterator for BufferChunks<'a> {
3053    type Item = Chunk<'a>;
3054
3055    fn next(&mut self) -> Option<Self::Item> {
3056        let mut next_capture_start = usize::MAX;
3057        let mut next_diagnostic_endpoint = usize::MAX;
3058
3059        if let Some(highlights) = self.highlights.as_mut() {
3060            while let Some((parent_capture_end, _)) = highlights.stack.last() {
3061                if *parent_capture_end <= self.range.start {
3062                    highlights.stack.pop();
3063                } else {
3064                    break;
3065                }
3066            }
3067
3068            if highlights.next_capture.is_none() {
3069                highlights.next_capture = highlights.captures.next();
3070            }
3071
3072            while let Some(capture) = highlights.next_capture.as_ref() {
3073                if self.range.start < capture.node.start_byte() {
3074                    next_capture_start = capture.node.start_byte();
3075                    break;
3076                } else {
3077                    let highlight_id =
3078                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
3079                    highlights
3080                        .stack
3081                        .push((capture.node.end_byte(), highlight_id));
3082                    highlights.next_capture = highlights.captures.next();
3083                }
3084            }
3085        }
3086
3087        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
3088            if endpoint.offset <= self.range.start {
3089                self.update_diagnostic_depths(endpoint);
3090                self.diagnostic_endpoints.next();
3091            } else {
3092                next_diagnostic_endpoint = endpoint.offset;
3093                break;
3094            }
3095        }
3096
3097        if let Some(chunk) = self.chunks.peek() {
3098            let chunk_start = self.range.start;
3099            let mut chunk_end = (self.chunks.offset() + chunk.len())
3100                .min(next_capture_start)
3101                .min(next_diagnostic_endpoint);
3102            let mut highlight_id = None;
3103            if let Some(highlights) = self.highlights.as_ref() {
3104                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
3105                    chunk_end = chunk_end.min(*parent_capture_end);
3106                    highlight_id = Some(*parent_highlight_id);
3107                }
3108            }
3109
3110            let slice =
3111                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
3112            self.range.start = chunk_end;
3113            if self.range.start == self.chunks.offset() + chunk.len() {
3114                self.chunks.next().unwrap();
3115            }
3116
3117            Some(Chunk {
3118                text: slice,
3119                syntax_highlight_id: highlight_id,
3120                diagnostic_severity: self.current_diagnostic_severity(),
3121                is_unnecessary: self.current_code_is_unnecessary(),
3122                ..Default::default()
3123            })
3124        } else {
3125            None
3126        }
3127    }
3128}
3129
3130impl operation_queue::Operation for Operation {
3131    fn lamport_timestamp(&self) -> clock::Lamport {
3132        match self {
3133            Operation::Buffer(_) => {
3134                unreachable!("buffer operations should never be deferred at this layer")
3135            }
3136            Operation::UpdateDiagnostics {
3137                lamport_timestamp, ..
3138            }
3139            | Operation::UpdateSelections {
3140                lamport_timestamp, ..
3141            }
3142            | Operation::UpdateCompletionTriggers {
3143                lamport_timestamp, ..
3144            } => *lamport_timestamp,
3145        }
3146    }
3147}
3148
3149impl Default for Diagnostic {
3150    fn default() -> Self {
3151        Self {
3152            source: Default::default(),
3153            code: None,
3154            severity: DiagnosticSeverity::ERROR,
3155            message: Default::default(),
3156            group_id: 0,
3157            is_primary: false,
3158            is_valid: true,
3159            is_disk_based: false,
3160            is_unnecessary: false,
3161        }
3162    }
3163}
3164
3165impl IndentSize {
3166    pub fn spaces(len: u32) -> Self {
3167        Self {
3168            len,
3169            kind: IndentKind::Space,
3170        }
3171    }
3172
3173    pub fn tab() -> Self {
3174        Self {
3175            len: 1,
3176            kind: IndentKind::Tab,
3177        }
3178    }
3179
3180    pub fn chars(&self) -> impl Iterator<Item = char> {
3181        iter::repeat(self.char()).take(self.len as usize)
3182    }
3183
3184    pub fn char(&self) -> char {
3185        match self.kind {
3186            IndentKind::Space => ' ',
3187            IndentKind::Tab => '\t',
3188        }
3189    }
3190
3191    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
3192        match direction {
3193            Ordering::Less => {
3194                if self.kind == size.kind && self.len >= size.len {
3195                    self.len -= size.len;
3196                }
3197            }
3198            Ordering::Equal => {}
3199            Ordering::Greater => {
3200                if self.len == 0 {
3201                    self = size;
3202                } else if self.kind == size.kind {
3203                    self.len += size.len;
3204                }
3205            }
3206        }
3207        self
3208    }
3209}
3210
3211impl Completion {
3212    pub fn sort_key(&self) -> (usize, &str) {
3213        let kind_key = match self.lsp_completion.kind {
3214            Some(lsp::CompletionItemKind::VARIABLE) => 0,
3215            _ => 1,
3216        };
3217        (kind_key, &self.label.text[self.label.filter_range.clone()])
3218    }
3219
3220    pub fn is_snippet(&self) -> bool {
3221        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
3222    }
3223}
3224
3225pub fn contiguous_ranges(
3226    values: impl Iterator<Item = u32>,
3227    max_len: usize,
3228) -> impl Iterator<Item = Range<u32>> {
3229    let mut values = values;
3230    let mut current_range: Option<Range<u32>> = None;
3231    std::iter::from_fn(move || loop {
3232        if let Some(value) = values.next() {
3233            if let Some(range) = &mut current_range {
3234                if value == range.end && range.len() < max_len {
3235                    range.end += 1;
3236                    continue;
3237                }
3238            }
3239
3240            let prev_range = current_range.clone();
3241            current_range = Some(value..(value + 1));
3242            if prev_range.is_some() {
3243                return prev_range;
3244            }
3245        } else {
3246            return current_range.take();
3247        }
3248    })
3249}
3250
3251pub fn char_kind(scope: &Option<LanguageScope>, c: char) -> CharKind {
3252    if c.is_whitespace() {
3253        return CharKind::Whitespace;
3254    } else if c.is_alphanumeric() || c == '_' {
3255        return CharKind::Word;
3256    }
3257
3258    if let Some(scope) = scope {
3259        if let Some(characters) = scope.word_characters() {
3260            if characters.contains(&c) {
3261                return CharKind::Word;
3262            }
3263        }
3264    }
3265
3266    CharKind::Punctuation
3267}
3268
3269/// Find all of the ranges of whitespace that occur at the ends of lines
3270/// in the given rope.
3271///
3272/// This could also be done with a regex search, but this implementation
3273/// avoids copying text.
3274pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
3275    let mut ranges = Vec::new();
3276
3277    let mut offset = 0;
3278    let mut prev_chunk_trailing_whitespace_range = 0..0;
3279    for chunk in rope.chunks() {
3280        let mut prev_line_trailing_whitespace_range = 0..0;
3281        for (i, line) in chunk.split('\n').enumerate() {
3282            let line_end_offset = offset + line.len();
3283            let trimmed_line_len = line.trim_end_matches(|c| matches!(c, ' ' | '\t')).len();
3284            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
3285
3286            if i == 0 && trimmed_line_len == 0 {
3287                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
3288            }
3289            if !prev_line_trailing_whitespace_range.is_empty() {
3290                ranges.push(prev_line_trailing_whitespace_range);
3291            }
3292
3293            offset = line_end_offset + 1;
3294            prev_line_trailing_whitespace_range = trailing_whitespace_range;
3295        }
3296
3297        offset -= 1;
3298        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
3299    }
3300
3301    if !prev_chunk_trailing_whitespace_range.is_empty() {
3302        ranges.push(prev_chunk_trailing_whitespace_range);
3303    }
3304
3305    ranges
3306}