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