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