buffer.rs

   1pub mod row_chunk;
   2
   3use crate::{
   4    DebuggerTextObject, LanguageScope, ModelineSettings, Outline, OutlineConfig, PLAIN_TEXT,
   5    RunnableCapture, RunnableTag, TextObject, TreeSitterOptions,
   6    diagnostic_set::{DiagnosticEntry, DiagnosticEntryRef, DiagnosticGroup},
   7    language_settings::{AutoIndentMode, LanguageSettings},
   8    outline::OutlineItem,
   9    row_chunk::RowChunks,
  10    syntax_map::{
  11        MAX_BYTES_TO_QUERY, SyntaxLayer, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures,
  12        SyntaxMapMatch, SyntaxMapMatches, SyntaxSnapshot, ToTreeSitterPoint,
  13    },
  14    task_context::RunnableRange,
  15    text_diff::text_diff,
  16    unified_diff_with_offsets,
  17};
  18pub use crate::{
  19    Grammar, Language, LanguageRegistry,
  20    diagnostic_set::DiagnosticSet,
  21    highlight_map::{HighlightId, HighlightMap},
  22    proto,
  23};
  24use anyhow::{Context as _, Result};
  25use clock::Lamport;
  26pub use clock::ReplicaId;
  27use collections::{HashMap, HashSet};
  28use encoding_rs::Encoding;
  29use fs::MTime;
  30use futures::channel::oneshot;
  31use gpui::{
  32    App, AppContext as _, Context, Entity, EventEmitter, HighlightStyle, SharedString, StyledText,
  33    Task, TextStyle,
  34};
  35
  36use lsp::{LanguageServerId, NumberOrString};
  37use parking_lot::Mutex;
  38use serde::{Deserialize, Serialize};
  39use serde_json::Value;
  40use settings::WorktreeId;
  41use smallvec::SmallVec;
  42use smol::future::yield_now;
  43use std::{
  44    any::Any,
  45    borrow::Cow,
  46    cell::Cell,
  47    cmp::{self, Ordering, Reverse},
  48    collections::{BTreeMap, BTreeSet},
  49    future::Future,
  50    iter::{self, Iterator, Peekable},
  51    mem,
  52    num::NonZeroU32,
  53    ops::{Deref, Range},
  54    path::PathBuf,
  55    rc,
  56    sync::Arc,
  57    time::{Duration, Instant},
  58    vec,
  59};
  60use sum_tree::TreeMap;
  61use text::operation_queue::OperationQueue;
  62use text::*;
  63pub use text::{
  64    Anchor, Bias, Buffer as TextBuffer, BufferId, BufferSnapshot as TextBufferSnapshot, Edit,
  65    LineIndent, OffsetRangeExt, OffsetUtf16, Patch, Point, PointUtf16, Rope, Selection,
  66    SelectionGoal, Subscription, TextDimension, TextSummary, ToOffset, ToOffsetUtf16, ToPoint,
  67    ToPointUtf16, Transaction, TransactionId, Unclipped,
  68};
  69use theme::{ActiveTheme as _, SyntaxTheme};
  70#[cfg(any(test, feature = "test-support"))]
  71use util::RandomCharIter;
  72use util::{RangeExt, debug_panic, maybe, paths::PathStyle, rel_path::RelPath};
  73
  74#[cfg(any(test, feature = "test-support"))]
  75pub use {tree_sitter_python, tree_sitter_rust, tree_sitter_typescript};
  76
  77pub use lsp::DiagnosticSeverity;
  78
  79/// Indicate whether a [`Buffer`] has permissions to edit.
  80#[derive(PartialEq, Clone, Copy, Debug)]
  81pub enum Capability {
  82    /// The buffer is a mutable replica.
  83    ReadWrite,
  84    /// The buffer is a mutable replica, but toggled to be only readable.
  85    Read,
  86    /// The buffer is a read-only replica.
  87    ReadOnly,
  88}
  89
  90impl Capability {
  91    /// Returns `true` if the capability is `ReadWrite`.
  92    pub fn editable(self) -> bool {
  93        matches!(self, Capability::ReadWrite)
  94    }
  95}
  96
  97pub type BufferRow = u32;
  98
  99/// An in-memory representation of a source code file, including its text,
 100/// syntax trees, git status, and diagnostics.
 101pub struct Buffer {
 102    text: TextBuffer,
 103    branch_state: Option<BufferBranchState>,
 104    /// Filesystem state, `None` when there is no path.
 105    file: Option<Arc<dyn File>>,
 106    /// The mtime of the file when this buffer was last loaded from
 107    /// or saved to disk.
 108    saved_mtime: Option<MTime>,
 109    /// The version vector when this buffer was last loaded from
 110    /// or saved to disk.
 111    saved_version: clock::Global,
 112    preview_version: clock::Global,
 113    transaction_depth: usize,
 114    was_dirty_before_starting_transaction: Option<bool>,
 115    reload_task: Option<Task<Result<()>>>,
 116    language: Option<Arc<Language>>,
 117    autoindent_requests: Vec<Arc<AutoindentRequest>>,
 118    wait_for_autoindent_txs: Vec<oneshot::Sender<()>>,
 119    pending_autoindent: Option<Task<()>>,
 120    sync_parse_timeout: Option<Duration>,
 121    syntax_map: Mutex<SyntaxMap>,
 122    reparse: Option<Task<()>>,
 123    parse_status: (watch::Sender<ParseStatus>, watch::Receiver<ParseStatus>),
 124    non_text_state_update_count: usize,
 125    diagnostics: TreeMap<LanguageServerId, DiagnosticSet>,
 126    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 127    diagnostics_timestamp: clock::Lamport,
 128    completion_triggers: BTreeSet<String>,
 129    completion_triggers_per_language_server: HashMap<LanguageServerId, BTreeSet<String>>,
 130    completion_triggers_timestamp: clock::Lamport,
 131    deferred_ops: OperationQueue<Operation>,
 132    capability: Capability,
 133    has_conflict: bool,
 134    /// Memoize calls to has_changes_since(saved_version).
 135    /// The contents of a cell are (self.version, has_changes) at the time of a last call.
 136    has_unsaved_edits: Cell<(clock::Global, bool)>,
 137    change_bits: Vec<rc::Weak<Cell<bool>>>,
 138    modeline: Option<Arc<ModelineSettings>>,
 139    _subscriptions: Vec<gpui::Subscription>,
 140    tree_sitter_data: Arc<TreeSitterData>,
 141    encoding: &'static Encoding,
 142    has_bom: bool,
 143    reload_with_encoding_txns: HashMap<TransactionId, (&'static Encoding, bool)>,
 144}
 145
 146#[derive(Debug)]
 147pub struct TreeSitterData {
 148    chunks: RowChunks,
 149    brackets_by_chunks: Mutex<Vec<Option<Vec<BracketMatch<usize>>>>>,
 150}
 151
 152const MAX_ROWS_IN_A_CHUNK: u32 = 50;
 153
 154impl TreeSitterData {
 155    fn clear(&mut self, snapshot: &text::BufferSnapshot) {
 156        self.chunks = RowChunks::new(&snapshot, MAX_ROWS_IN_A_CHUNK);
 157        self.brackets_by_chunks.get_mut().clear();
 158        self.brackets_by_chunks
 159            .get_mut()
 160            .resize(self.chunks.len(), None);
 161    }
 162
 163    fn new(snapshot: &text::BufferSnapshot) -> Self {
 164        let chunks = RowChunks::new(&snapshot, MAX_ROWS_IN_A_CHUNK);
 165        Self {
 166            brackets_by_chunks: Mutex::new(vec![None; chunks.len()]),
 167            chunks,
 168        }
 169    }
 170
 171    fn version(&self) -> &clock::Global {
 172        self.chunks.version()
 173    }
 174}
 175
 176#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 177pub enum ParseStatus {
 178    Idle,
 179    Parsing,
 180}
 181
 182struct BufferBranchState {
 183    base_buffer: Entity<Buffer>,
 184    merged_operations: Vec<Lamport>,
 185}
 186
 187/// An immutable, cheaply cloneable representation of a fixed
 188/// state of a buffer.
 189pub struct BufferSnapshot {
 190    pub text: text::BufferSnapshot,
 191    pub(crate) syntax: SyntaxSnapshot,
 192    tree_sitter_data: Arc<TreeSitterData>,
 193    diagnostics: TreeMap<LanguageServerId, DiagnosticSet>,
 194    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 195    language: Option<Arc<Language>>,
 196    file: Option<Arc<dyn File>>,
 197    non_text_state_update_count: usize,
 198    pub capability: Capability,
 199    modeline: Option<Arc<ModelineSettings>>,
 200}
 201
 202/// The kind and amount of indentation in a particular line. For now,
 203/// assumes that indentation is all the same character.
 204#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
 205pub struct IndentSize {
 206    /// The number of bytes that comprise the indentation.
 207    pub len: u32,
 208    /// The kind of whitespace used for indentation.
 209    pub kind: IndentKind,
 210}
 211
 212/// A whitespace character that's used for indentation.
 213#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
 214pub enum IndentKind {
 215    /// An ASCII space character.
 216    #[default]
 217    Space,
 218    /// An ASCII tab character.
 219    Tab,
 220}
 221
 222/// The shape of a selection cursor.
 223#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
 224pub enum CursorShape {
 225    /// A vertical bar
 226    #[default]
 227    Bar,
 228    /// A block that surrounds the following character
 229    Block,
 230    /// An underline that runs along the following character
 231    Underline,
 232    /// A box drawn around the following character
 233    Hollow,
 234}
 235
 236impl From<settings::CursorShape> for CursorShape {
 237    fn from(shape: settings::CursorShape) -> Self {
 238        match shape {
 239            settings::CursorShape::Bar => CursorShape::Bar,
 240            settings::CursorShape::Block => CursorShape::Block,
 241            settings::CursorShape::Underline => CursorShape::Underline,
 242            settings::CursorShape::Hollow => CursorShape::Hollow,
 243        }
 244    }
 245}
 246
 247#[derive(Clone, Debug)]
 248struct SelectionSet {
 249    line_mode: bool,
 250    cursor_shape: CursorShape,
 251    selections: Arc<[Selection<Anchor>]>,
 252    lamport_timestamp: clock::Lamport,
 253}
 254
 255/// A diagnostic associated with a certain range of a buffer.
 256#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
 257pub struct Diagnostic {
 258    /// The name of the service that produced this diagnostic.
 259    pub source: Option<String>,
 260    /// The ID provided by the dynamic registration that produced this diagnostic.
 261    pub registration_id: Option<SharedString>,
 262    /// A machine-readable code that identifies this diagnostic.
 263    pub code: Option<NumberOrString>,
 264    pub code_description: Option<lsp::Uri>,
 265    /// Whether this diagnostic is a hint, warning, or error.
 266    pub severity: DiagnosticSeverity,
 267    /// The human-readable message associated with this diagnostic.
 268    pub message: String,
 269    /// The human-readable message (in markdown format)
 270    pub markdown: Option<String>,
 271    /// An id that identifies the group to which this diagnostic belongs.
 272    ///
 273    /// When a language server produces a diagnostic with
 274    /// one or more associated diagnostics, those diagnostics are all
 275    /// assigned a single group ID.
 276    pub group_id: usize,
 277    /// Whether this diagnostic is the primary diagnostic for its group.
 278    ///
 279    /// In a given group, the primary diagnostic is the top-level diagnostic
 280    /// returned by the language server. The non-primary diagnostics are the
 281    /// associated diagnostics.
 282    pub is_primary: bool,
 283    /// Whether this diagnostic is considered to originate from an analysis of
 284    /// files on disk, as opposed to any unsaved buffer contents. This is a
 285    /// property of a given diagnostic source, and is configured for a given
 286    /// language server via the [`LspAdapter::disk_based_diagnostic_sources`](crate::LspAdapter::disk_based_diagnostic_sources) method
 287    /// for the language server.
 288    pub is_disk_based: bool,
 289    /// Whether this diagnostic marks unnecessary code.
 290    pub is_unnecessary: bool,
 291    /// Quick separation of diagnostics groups based by their source.
 292    pub source_kind: DiagnosticSourceKind,
 293    /// Data from language server that produced this diagnostic. Passed back to the LS when we request code actions for this diagnostic.
 294    pub data: Option<Value>,
 295    /// Whether to underline the corresponding text range in the editor.
 296    pub underline: bool,
 297}
 298
 299#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
 300pub enum DiagnosticSourceKind {
 301    Pulled,
 302    Pushed,
 303    Other,
 304}
 305
 306/// An operation used to synchronize this buffer with its other replicas.
 307#[derive(Clone, Debug, PartialEq)]
 308pub enum Operation {
 309    /// A text operation.
 310    Buffer(text::Operation),
 311
 312    /// An update to the buffer's diagnostics.
 313    UpdateDiagnostics {
 314        /// The id of the language server that produced the new diagnostics.
 315        server_id: LanguageServerId,
 316        /// The diagnostics.
 317        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 318        /// The buffer's lamport timestamp.
 319        lamport_timestamp: clock::Lamport,
 320    },
 321
 322    /// An update to the most recent selections in this buffer.
 323    UpdateSelections {
 324        /// The selections.
 325        selections: Arc<[Selection<Anchor>]>,
 326        /// The buffer's lamport timestamp.
 327        lamport_timestamp: clock::Lamport,
 328        /// Whether the selections are in 'line mode'.
 329        line_mode: bool,
 330        /// The [`CursorShape`] associated with these selections.
 331        cursor_shape: CursorShape,
 332    },
 333
 334    /// An update to the characters that should trigger autocompletion
 335    /// for this buffer.
 336    UpdateCompletionTriggers {
 337        /// The characters that trigger autocompletion.
 338        triggers: Vec<String>,
 339        /// The buffer's lamport timestamp.
 340        lamport_timestamp: clock::Lamport,
 341        /// The language server ID.
 342        server_id: LanguageServerId,
 343    },
 344
 345    /// An update to the line ending type of this buffer.
 346    UpdateLineEnding {
 347        /// The line ending type.
 348        line_ending: LineEnding,
 349        /// The buffer's lamport timestamp.
 350        lamport_timestamp: clock::Lamport,
 351    },
 352}
 353
 354/// An event that occurs in a buffer.
 355#[derive(Clone, Debug, PartialEq)]
 356pub enum BufferEvent {
 357    /// The buffer was changed in a way that must be
 358    /// propagated to its other replicas.
 359    Operation {
 360        operation: Operation,
 361        is_local: bool,
 362    },
 363    /// The buffer was edited.
 364    Edited { is_local: bool },
 365    /// The buffer's `dirty` bit changed.
 366    DirtyChanged,
 367    /// The buffer was saved.
 368    Saved,
 369    /// The buffer's file was changed on disk.
 370    FileHandleChanged,
 371    /// The buffer was reloaded.
 372    Reloaded,
 373    /// The buffer is in need of a reload
 374    ReloadNeeded,
 375    /// The buffer's language was changed.
 376    /// The boolean indicates whether this buffer did not have a language before, but does now.
 377    LanguageChanged(bool),
 378    /// The buffer's syntax trees were updated.
 379    Reparsed,
 380    /// The buffer's diagnostics were updated.
 381    DiagnosticsUpdated,
 382    /// The buffer gained or lost editing capabilities.
 383    CapabilityChanged,
 384}
 385
 386/// The file associated with a buffer.
 387pub trait File: Send + Sync + Any {
 388    /// Returns the [`LocalFile`] associated with this file, if the
 389    /// file is local.
 390    fn as_local(&self) -> Option<&dyn LocalFile>;
 391
 392    /// Returns whether this file is local.
 393    fn is_local(&self) -> bool {
 394        self.as_local().is_some()
 395    }
 396
 397    /// Returns whether the file is new, exists in storage, or has been deleted. Includes metadata
 398    /// only available in some states, such as modification time.
 399    fn disk_state(&self) -> DiskState;
 400
 401    /// Returns the path of this file relative to the worktree's root directory.
 402    fn path(&self) -> &Arc<RelPath>;
 403
 404    /// Returns the path of this file relative to the worktree's parent directory (this means it
 405    /// includes the name of the worktree's root folder).
 406    fn full_path(&self, cx: &App) -> PathBuf;
 407
 408    /// Returns the path style of this file.
 409    fn path_style(&self, cx: &App) -> PathStyle;
 410
 411    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 412    /// of its worktree, then this method will return the name of the worktree itself.
 413    fn file_name<'a>(&'a self, cx: &'a App) -> &'a str;
 414
 415    /// Returns the id of the worktree to which this file belongs.
 416    ///
 417    /// This is needed for looking up project-specific settings.
 418    fn worktree_id(&self, cx: &App) -> WorktreeId;
 419
 420    /// Converts this file into a protobuf message.
 421    fn to_proto(&self, cx: &App) -> rpc::proto::File;
 422
 423    /// Return whether Zed considers this to be a private file.
 424    fn is_private(&self) -> bool;
 425
 426    fn can_open(&self) -> bool {
 427        !self.is_local()
 428    }
 429}
 430
 431/// The file's storage status - whether it's stored (`Present`), and if so when it was last
 432/// modified. In the case where the file is not stored, it can be either `New` or `Deleted`. In the
 433/// UI these two states are distinguished. For example, the buffer tab does not display a deletion
 434/// indicator for new files.
 435#[derive(Copy, Clone, Debug, PartialEq)]
 436pub enum DiskState {
 437    /// File created in Zed that has not been saved.
 438    New,
 439    /// File present on the filesystem.
 440    Present { mtime: MTime, size: u64 },
 441    /// Deleted file that was previously present.
 442    Deleted,
 443    /// An old version of a file that was previously present
 444    /// usually from a version control system. e.g. A git blob
 445    Historic { was_deleted: bool },
 446}
 447
 448impl DiskState {
 449    /// Returns the file's last known modification time on disk.
 450    pub fn mtime(self) -> Option<MTime> {
 451        match self {
 452            DiskState::New => None,
 453            DiskState::Present { mtime, .. } => Some(mtime),
 454            DiskState::Deleted => None,
 455            DiskState::Historic { .. } => None,
 456        }
 457    }
 458
 459    /// Returns the file's size on disk in bytes.
 460    pub fn size(self) -> Option<u64> {
 461        match self {
 462            DiskState::New => None,
 463            DiskState::Present { size, .. } => Some(size),
 464            DiskState::Deleted => None,
 465            DiskState::Historic { .. } => None,
 466        }
 467    }
 468
 469    pub fn exists(&self) -> bool {
 470        match self {
 471            DiskState::New => false,
 472            DiskState::Present { .. } => true,
 473            DiskState::Deleted => false,
 474            DiskState::Historic { .. } => false,
 475        }
 476    }
 477
 478    /// Returns true if this state represents a deleted file.
 479    pub fn is_deleted(&self) -> bool {
 480        match self {
 481            DiskState::Deleted => true,
 482            DiskState::Historic { was_deleted } => *was_deleted,
 483            _ => false,
 484        }
 485    }
 486}
 487
 488/// The file associated with a buffer, in the case where the file is on the local disk.
 489pub trait LocalFile: File {
 490    /// Returns the absolute path of this file
 491    fn abs_path(&self, cx: &App) -> PathBuf;
 492
 493    /// Loads the file contents from disk and returns them as a UTF-8 encoded string.
 494    fn load(&self, cx: &App) -> Task<Result<String>>;
 495
 496    /// Loads the file's contents from disk.
 497    fn load_bytes(&self, cx: &App) -> Task<Result<Vec<u8>>>;
 498}
 499
 500/// The auto-indent behavior associated with an editing operation.
 501/// For some editing operations, each affected line of text has its
 502/// indentation recomputed. For other operations, the entire block
 503/// of edited text is adjusted uniformly.
 504#[derive(Clone, Debug)]
 505pub enum AutoindentMode {
 506    /// Indent each line of inserted text.
 507    EachLine,
 508    /// Apply the same indentation adjustment to all of the lines
 509    /// in a given insertion.
 510    Block {
 511        /// The original indentation column of the first line of each
 512        /// insertion, if it has been copied.
 513        ///
 514        /// Knowing this makes it possible to preserve the relative indentation
 515        /// of every line in the insertion from when it was copied.
 516        ///
 517        /// If the original indent column is `a`, and the first line of insertion
 518        /// is then auto-indented to column `b`, then every other line of
 519        /// the insertion will be auto-indented to column `b - a`
 520        original_indent_columns: Vec<Option<u32>>,
 521    },
 522}
 523
 524#[derive(Clone)]
 525struct AutoindentRequest {
 526    before_edit: BufferSnapshot,
 527    entries: Vec<AutoindentRequestEntry>,
 528    is_block_mode: bool,
 529    ignore_empty_lines: bool,
 530}
 531
 532#[derive(Debug, Clone)]
 533struct AutoindentRequestEntry {
 534    /// A range of the buffer whose indentation should be adjusted.
 535    range: Range<Anchor>,
 536    /// The row of the edit start in the buffer before the edit was applied.
 537    /// This is stored here because the anchor in range is created after
 538    /// the edit, so it cannot be used with the before_edit snapshot.
 539    old_row: Option<u32>,
 540    indent_size: IndentSize,
 541    original_indent_column: Option<u32>,
 542}
 543
 544#[derive(Debug)]
 545struct IndentSuggestion {
 546    basis_row: u32,
 547    delta: Ordering,
 548    within_error: bool,
 549}
 550
 551struct BufferChunkHighlights<'a> {
 552    captures: SyntaxMapCaptures<'a>,
 553    next_capture: Option<SyntaxMapCapture<'a>>,
 554    stack: Vec<(usize, HighlightId)>,
 555    highlight_maps: Vec<HighlightMap>,
 556}
 557
 558/// An iterator that yields chunks of a buffer's text, along with their
 559/// syntax highlights and diagnostic status.
 560pub struct BufferChunks<'a> {
 561    buffer_snapshot: Option<&'a BufferSnapshot>,
 562    range: Range<usize>,
 563    chunks: text::Chunks<'a>,
 564    diagnostic_endpoints: Option<Peekable<vec::IntoIter<DiagnosticEndpoint>>>,
 565    error_depth: usize,
 566    warning_depth: usize,
 567    information_depth: usize,
 568    hint_depth: usize,
 569    unnecessary_depth: usize,
 570    underline: bool,
 571    highlights: Option<BufferChunkHighlights<'a>>,
 572}
 573
 574/// A chunk of a buffer's text, along with its syntax highlight and
 575/// diagnostic status.
 576#[derive(Clone, Debug, Default)]
 577pub struct Chunk<'a> {
 578    /// The text of the chunk.
 579    pub text: &'a str,
 580    /// The syntax highlighting style of the chunk.
 581    pub syntax_highlight_id: Option<HighlightId>,
 582    /// The highlight style that has been applied to this chunk in
 583    /// the editor.
 584    pub highlight_style: Option<HighlightStyle>,
 585    /// The severity of diagnostic associated with this chunk, if any.
 586    pub diagnostic_severity: Option<DiagnosticSeverity>,
 587    /// A bitset of which characters are tabs in this string.
 588    pub tabs: u128,
 589    /// Bitmap of character indices in this chunk
 590    pub chars: u128,
 591    /// Bitmap of newline indices in this chunk
 592    pub newlines: u128,
 593    /// Whether this chunk of text is marked as unnecessary.
 594    pub is_unnecessary: bool,
 595    /// Whether this chunk of text was originally a tab character.
 596    pub is_tab: bool,
 597    /// Whether this chunk of text was originally an inlay.
 598    pub is_inlay: bool,
 599    /// Whether to underline the corresponding text range in the editor.
 600    pub underline: bool,
 601}
 602
 603/// A set of edits to a given version of a buffer, computed asynchronously.
 604#[derive(Debug, Clone)]
 605pub struct Diff {
 606    pub base_version: clock::Global,
 607    pub line_ending: LineEnding,
 608    pub edits: Vec<(Range<usize>, Arc<str>)>,
 609}
 610
 611#[derive(Debug, Clone, Copy)]
 612pub(crate) struct DiagnosticEndpoint {
 613    offset: usize,
 614    is_start: bool,
 615    underline: bool,
 616    severity: DiagnosticSeverity,
 617    is_unnecessary: bool,
 618}
 619
 620/// A class of characters, used for characterizing a run of text.
 621#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 622pub enum CharKind {
 623    /// Whitespace.
 624    Whitespace,
 625    /// Punctuation.
 626    Punctuation,
 627    /// Word.
 628    Word,
 629}
 630
 631/// Context for character classification within a specific scope.
 632#[derive(Copy, Clone, Eq, PartialEq, Debug)]
 633pub enum CharScopeContext {
 634    /// Character classification for completion queries.
 635    ///
 636    /// This context treats certain characters as word constituents that would
 637    /// normally be considered punctuation, such as '-' in Tailwind classes
 638    /// ("bg-yellow-100") or '.' in import paths ("foo.ts").
 639    Completion,
 640    /// Character classification for linked edits.
 641    ///
 642    /// This context handles characters that should be treated as part of
 643    /// identifiers during linked editing operations, such as '.' in JSX
 644    /// component names like `<Animated.View>`.
 645    LinkedEdit,
 646}
 647
 648/// A runnable is a set of data about a region that could be resolved into a task
 649pub struct Runnable {
 650    pub tags: SmallVec<[RunnableTag; 1]>,
 651    pub language: Arc<Language>,
 652    pub buffer: BufferId,
 653}
 654
 655#[derive(Default, Clone, Debug)]
 656pub struct HighlightedText {
 657    pub text: SharedString,
 658    pub highlights: Vec<(Range<usize>, HighlightStyle)>,
 659}
 660
 661#[derive(Default, Debug)]
 662struct HighlightedTextBuilder {
 663    pub text: String,
 664    highlights: Vec<(Range<usize>, HighlightStyle)>,
 665}
 666
 667impl HighlightedText {
 668    pub fn from_buffer_range<T: ToOffset>(
 669        range: Range<T>,
 670        snapshot: &text::BufferSnapshot,
 671        syntax_snapshot: &SyntaxSnapshot,
 672        override_style: Option<HighlightStyle>,
 673        syntax_theme: &SyntaxTheme,
 674    ) -> Self {
 675        let mut highlighted_text = HighlightedTextBuilder::default();
 676        highlighted_text.add_text_from_buffer_range(
 677            range,
 678            snapshot,
 679            syntax_snapshot,
 680            override_style,
 681            syntax_theme,
 682        );
 683        highlighted_text.build()
 684    }
 685
 686    pub fn to_styled_text(&self, default_style: &TextStyle) -> StyledText {
 687        gpui::StyledText::new(self.text.clone())
 688            .with_default_highlights(default_style, self.highlights.iter().cloned())
 689    }
 690
 691    /// Returns the first line without leading whitespace unless highlighted
 692    /// and a boolean indicating if there are more lines after
 693    pub fn first_line_preview(self) -> (Self, bool) {
 694        let newline_ix = self.text.find('\n').unwrap_or(self.text.len());
 695        let first_line = &self.text[..newline_ix];
 696
 697        // Trim leading whitespace, unless an edit starts prior to it.
 698        let mut preview_start_ix = first_line.len() - first_line.trim_start().len();
 699        if let Some((first_highlight_range, _)) = self.highlights.first() {
 700            preview_start_ix = preview_start_ix.min(first_highlight_range.start);
 701        }
 702
 703        let preview_text = &first_line[preview_start_ix..];
 704        let preview_highlights = self
 705            .highlights
 706            .into_iter()
 707            .skip_while(|(range, _)| range.end <= preview_start_ix)
 708            .take_while(|(range, _)| range.start < newline_ix)
 709            .filter_map(|(mut range, highlight)| {
 710                range.start = range.start.saturating_sub(preview_start_ix);
 711                range.end = range.end.min(newline_ix).saturating_sub(preview_start_ix);
 712                if range.is_empty() {
 713                    None
 714                } else {
 715                    Some((range, highlight))
 716                }
 717            });
 718
 719        let preview = Self {
 720            text: SharedString::new(preview_text),
 721            highlights: preview_highlights.collect(),
 722        };
 723
 724        (preview, self.text.len() > newline_ix)
 725    }
 726}
 727
 728impl HighlightedTextBuilder {
 729    pub fn build(self) -> HighlightedText {
 730        HighlightedText {
 731            text: self.text.into(),
 732            highlights: self.highlights,
 733        }
 734    }
 735
 736    pub fn add_text_from_buffer_range<T: ToOffset>(
 737        &mut self,
 738        range: Range<T>,
 739        snapshot: &text::BufferSnapshot,
 740        syntax_snapshot: &SyntaxSnapshot,
 741        override_style: Option<HighlightStyle>,
 742        syntax_theme: &SyntaxTheme,
 743    ) {
 744        let range = range.to_offset(snapshot);
 745        for chunk in Self::highlighted_chunks(range, snapshot, syntax_snapshot) {
 746            let start = self.text.len();
 747            self.text.push_str(chunk.text);
 748            let end = self.text.len();
 749
 750            if let Some(highlight_style) = chunk
 751                .syntax_highlight_id
 752                .and_then(|id| id.style(syntax_theme))
 753            {
 754                let highlight_style = override_style.map_or(highlight_style, |override_style| {
 755                    highlight_style.highlight(override_style)
 756                });
 757                self.highlights.push((start..end, highlight_style));
 758            } else if let Some(override_style) = override_style {
 759                self.highlights.push((start..end, override_style));
 760            }
 761        }
 762    }
 763
 764    fn highlighted_chunks<'a>(
 765        range: Range<usize>,
 766        snapshot: &'a text::BufferSnapshot,
 767        syntax_snapshot: &'a SyntaxSnapshot,
 768    ) -> BufferChunks<'a> {
 769        let captures = syntax_snapshot.captures(range.clone(), snapshot, |grammar| {
 770            grammar
 771                .highlights_config
 772                .as_ref()
 773                .map(|config| &config.query)
 774        });
 775
 776        let highlight_maps = captures
 777            .grammars()
 778            .iter()
 779            .map(|grammar| grammar.highlight_map())
 780            .collect();
 781
 782        BufferChunks::new(
 783            snapshot.as_rope(),
 784            range,
 785            Some((captures, highlight_maps)),
 786            false,
 787            None,
 788        )
 789    }
 790}
 791
 792#[derive(Clone)]
 793pub struct EditPreview {
 794    old_snapshot: text::BufferSnapshot,
 795    applied_edits_snapshot: text::BufferSnapshot,
 796    syntax_snapshot: SyntaxSnapshot,
 797}
 798
 799impl EditPreview {
 800    pub fn as_unified_diff(
 801        &self,
 802        file: Option<&Arc<dyn File>>,
 803        edits: &[(Range<Anchor>, impl AsRef<str>)],
 804    ) -> Option<String> {
 805        let (first, _) = edits.first()?;
 806        let (last, _) = edits.last()?;
 807
 808        let start = first.start.to_point(&self.old_snapshot);
 809        let old_end = last.end.to_point(&self.old_snapshot);
 810        let new_end = last
 811            .end
 812            .bias_right(&self.old_snapshot)
 813            .to_point(&self.applied_edits_snapshot);
 814
 815        let start = Point::new(start.row.saturating_sub(3), 0);
 816        let old_end = Point::new(old_end.row + 4, 0).min(self.old_snapshot.max_point());
 817        let new_end = Point::new(new_end.row + 4, 0).min(self.applied_edits_snapshot.max_point());
 818
 819        let diff_body = unified_diff_with_offsets(
 820            &self
 821                .old_snapshot
 822                .text_for_range(start..old_end)
 823                .collect::<String>(),
 824            &self
 825                .applied_edits_snapshot
 826                .text_for_range(start..new_end)
 827                .collect::<String>(),
 828            start.row,
 829            start.row,
 830        );
 831
 832        let path = file.map(|f| f.path().as_unix_str());
 833        let header = match path {
 834            Some(p) => format!("--- a/{}\n+++ b/{}\n", p, p),
 835            None => String::new(),
 836        };
 837
 838        Some(format!("{}{}", header, diff_body))
 839    }
 840
 841    pub fn highlight_edits(
 842        &self,
 843        current_snapshot: &BufferSnapshot,
 844        edits: &[(Range<Anchor>, impl AsRef<str>)],
 845        include_deletions: bool,
 846        cx: &App,
 847    ) -> HighlightedText {
 848        let Some(visible_range_in_preview_snapshot) = self.compute_visible_range(edits) else {
 849            return HighlightedText::default();
 850        };
 851
 852        let mut highlighted_text = HighlightedTextBuilder::default();
 853
 854        let visible_range_in_preview_snapshot =
 855            visible_range_in_preview_snapshot.to_offset(&self.applied_edits_snapshot);
 856        let mut offset_in_preview_snapshot = visible_range_in_preview_snapshot.start;
 857
 858        let insertion_highlight_style = HighlightStyle {
 859            background_color: Some(cx.theme().status().created_background),
 860            ..Default::default()
 861        };
 862        let deletion_highlight_style = HighlightStyle {
 863            background_color: Some(cx.theme().status().deleted_background),
 864            ..Default::default()
 865        };
 866        let syntax_theme = cx.theme().syntax();
 867
 868        for (range, edit_text) in edits {
 869            let edit_new_end_in_preview_snapshot = range
 870                .end
 871                .bias_right(&self.old_snapshot)
 872                .to_offset(&self.applied_edits_snapshot);
 873            let edit_start_in_preview_snapshot =
 874                edit_new_end_in_preview_snapshot - edit_text.as_ref().len();
 875
 876            let unchanged_range_in_preview_snapshot =
 877                offset_in_preview_snapshot..edit_start_in_preview_snapshot;
 878            if !unchanged_range_in_preview_snapshot.is_empty() {
 879                highlighted_text.add_text_from_buffer_range(
 880                    unchanged_range_in_preview_snapshot,
 881                    &self.applied_edits_snapshot,
 882                    &self.syntax_snapshot,
 883                    None,
 884                    syntax_theme,
 885                );
 886            }
 887
 888            let range_in_current_snapshot = range.to_offset(current_snapshot);
 889            if include_deletions && !range_in_current_snapshot.is_empty() {
 890                highlighted_text.add_text_from_buffer_range(
 891                    range_in_current_snapshot,
 892                    &current_snapshot.text,
 893                    &current_snapshot.syntax,
 894                    Some(deletion_highlight_style),
 895                    syntax_theme,
 896                );
 897            }
 898
 899            if !edit_text.as_ref().is_empty() {
 900                highlighted_text.add_text_from_buffer_range(
 901                    edit_start_in_preview_snapshot..edit_new_end_in_preview_snapshot,
 902                    &self.applied_edits_snapshot,
 903                    &self.syntax_snapshot,
 904                    Some(insertion_highlight_style),
 905                    syntax_theme,
 906                );
 907            }
 908
 909            offset_in_preview_snapshot = edit_new_end_in_preview_snapshot;
 910        }
 911
 912        highlighted_text.add_text_from_buffer_range(
 913            offset_in_preview_snapshot..visible_range_in_preview_snapshot.end,
 914            &self.applied_edits_snapshot,
 915            &self.syntax_snapshot,
 916            None,
 917            syntax_theme,
 918        );
 919
 920        highlighted_text.build()
 921    }
 922
 923    pub fn build_result_buffer(&self, cx: &mut App) -> Entity<Buffer> {
 924        cx.new(|cx| {
 925            let mut buffer = Buffer::local_normalized(
 926                self.applied_edits_snapshot.as_rope().clone(),
 927                self.applied_edits_snapshot.line_ending(),
 928                cx,
 929            );
 930            buffer.set_language_async(self.syntax_snapshot.root_language(), cx);
 931            buffer
 932        })
 933    }
 934
 935    pub fn anchor_to_offset_in_result(&self, anchor: Anchor) -> usize {
 936        anchor
 937            .bias_right(&self.old_snapshot)
 938            .to_offset(&self.applied_edits_snapshot)
 939    }
 940
 941    pub fn compute_visible_range<T>(&self, edits: &[(Range<Anchor>, T)]) -> Option<Range<Point>> {
 942        let (first, _) = edits.first()?;
 943        let (last, _) = edits.last()?;
 944
 945        let start = first
 946            .start
 947            .bias_left(&self.old_snapshot)
 948            .to_point(&self.applied_edits_snapshot);
 949        let end = last
 950            .end
 951            .bias_right(&self.old_snapshot)
 952            .to_point(&self.applied_edits_snapshot);
 953
 954        // Ensure that the first line of the first edit and the last line of the last edit are always fully visible
 955        let range = Point::new(start.row, 0)
 956            ..Point::new(end.row, self.applied_edits_snapshot.line_len(end.row));
 957
 958        Some(range)
 959    }
 960}
 961
 962#[derive(Clone, Debug, PartialEq, Eq)]
 963pub struct BracketMatch<T> {
 964    pub open_range: Range<T>,
 965    pub close_range: Range<T>,
 966    pub newline_only: bool,
 967    pub syntax_layer_depth: usize,
 968    pub color_index: Option<usize>,
 969}
 970
 971impl<T> BracketMatch<T> {
 972    pub fn bracket_ranges(self) -> (Range<T>, Range<T>) {
 973        (self.open_range, self.close_range)
 974    }
 975}
 976
 977impl Buffer {
 978    /// Create a new buffer with the given base text.
 979    pub fn local<T: Into<String>>(base_text: T, cx: &Context<Self>) -> Self {
 980        Self::build(
 981            TextBuffer::new(
 982                ReplicaId::LOCAL,
 983                cx.entity_id().as_non_zero_u64().into(),
 984                base_text.into(),
 985            ),
 986            None,
 987            Capability::ReadWrite,
 988        )
 989    }
 990
 991    /// Create a new buffer with the given base text that has proper line endings and other normalization applied.
 992    pub fn local_normalized(
 993        base_text_normalized: Rope,
 994        line_ending: LineEnding,
 995        cx: &Context<Self>,
 996    ) -> Self {
 997        Self::build(
 998            TextBuffer::new_normalized(
 999                ReplicaId::LOCAL,
1000                cx.entity_id().as_non_zero_u64().into(),
1001                line_ending,
1002                base_text_normalized,
1003            ),
1004            None,
1005            Capability::ReadWrite,
1006        )
1007    }
1008
1009    /// Create a new buffer that is a replica of a remote buffer.
1010    pub fn remote(
1011        remote_id: BufferId,
1012        replica_id: ReplicaId,
1013        capability: Capability,
1014        base_text: impl Into<String>,
1015    ) -> Self {
1016        Self::build(
1017            TextBuffer::new(replica_id, remote_id, base_text.into()),
1018            None,
1019            capability,
1020        )
1021    }
1022
1023    /// Create a new buffer that is a replica of a remote buffer, populating its
1024    /// state from the given protobuf message.
1025    pub fn from_proto(
1026        replica_id: ReplicaId,
1027        capability: Capability,
1028        message: proto::BufferState,
1029        file: Option<Arc<dyn File>>,
1030    ) -> Result<Self> {
1031        let buffer_id = BufferId::new(message.id).context("Could not deserialize buffer_id")?;
1032        let buffer = TextBuffer::new(replica_id, buffer_id, message.base_text);
1033        let mut this = Self::build(buffer, file, capability);
1034        this.text.set_line_ending(proto::deserialize_line_ending(
1035            rpc::proto::LineEnding::from_i32(message.line_ending).context("missing line_ending")?,
1036        ));
1037        this.saved_version = proto::deserialize_version(&message.saved_version);
1038        this.saved_mtime = message.saved_mtime.map(|time| time.into());
1039        Ok(this)
1040    }
1041
1042    /// Serialize the buffer's state to a protobuf message.
1043    pub fn to_proto(&self, cx: &App) -> proto::BufferState {
1044        proto::BufferState {
1045            id: self.remote_id().into(),
1046            file: self.file.as_ref().map(|f| f.to_proto(cx)),
1047            base_text: self.base_text().to_string(),
1048            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
1049            saved_version: proto::serialize_version(&self.saved_version),
1050            saved_mtime: self.saved_mtime.map(|time| time.into()),
1051        }
1052    }
1053
1054    /// Serialize as protobufs all of the changes to the buffer since the given version.
1055    pub fn serialize_ops(
1056        &self,
1057        since: Option<clock::Global>,
1058        cx: &App,
1059    ) -> Task<Vec<proto::Operation>> {
1060        let mut operations = Vec::new();
1061        operations.extend(self.deferred_ops.iter().map(proto::serialize_operation));
1062
1063        operations.extend(self.remote_selections.iter().map(|(_, set)| {
1064            proto::serialize_operation(&Operation::UpdateSelections {
1065                selections: set.selections.clone(),
1066                lamport_timestamp: set.lamport_timestamp,
1067                line_mode: set.line_mode,
1068                cursor_shape: set.cursor_shape,
1069            })
1070        }));
1071
1072        for (server_id, diagnostics) in self.diagnostics.iter() {
1073            operations.push(proto::serialize_operation(&Operation::UpdateDiagnostics {
1074                lamport_timestamp: self.diagnostics_timestamp,
1075                server_id: *server_id,
1076                diagnostics: diagnostics.iter().cloned().collect(),
1077            }));
1078        }
1079
1080        for (server_id, completions) in &self.completion_triggers_per_language_server {
1081            operations.push(proto::serialize_operation(
1082                &Operation::UpdateCompletionTriggers {
1083                    triggers: completions.iter().cloned().collect(),
1084                    lamport_timestamp: self.completion_triggers_timestamp,
1085                    server_id: *server_id,
1086                },
1087            ));
1088        }
1089
1090        let text_operations = self.text.operations().clone();
1091        cx.background_spawn(async move {
1092            let since = since.unwrap_or_default();
1093            operations.extend(
1094                text_operations
1095                    .iter()
1096                    .filter(|(_, op)| !since.observed(op.timestamp()))
1097                    .map(|(_, op)| proto::serialize_operation(&Operation::Buffer(op.clone()))),
1098            );
1099            operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
1100            operations
1101        })
1102    }
1103
1104    /// Assign a language to the buffer, returning the buffer.
1105    pub fn with_language_async(mut self, language: Arc<Language>, cx: &mut Context<Self>) -> Self {
1106        self.set_language_async(Some(language), cx);
1107        self
1108    }
1109
1110    /// Assign a language to the buffer, blocking for up to 1ms to reparse the buffer, returning the buffer.
1111    #[ztracing::instrument(skip_all, fields(lang = language.config.name.0.as_str()))]
1112    pub fn with_language(mut self, language: Arc<Language>, cx: &mut Context<Self>) -> Self {
1113        self.set_language(Some(language), cx);
1114        self
1115    }
1116
1117    /// Returns the [`Capability`] of this buffer.
1118    pub fn capability(&self) -> Capability {
1119        self.capability
1120    }
1121
1122    /// Whether this buffer can only be read.
1123    pub fn read_only(&self) -> bool {
1124        !self.capability.editable()
1125    }
1126
1127    /// Builds a [`Buffer`] with the given underlying [`TextBuffer`], diff base, [`File`] and [`Capability`].
1128    pub fn build(buffer: TextBuffer, file: Option<Arc<dyn File>>, capability: Capability) -> Self {
1129        let saved_mtime = file.as_ref().and_then(|file| file.disk_state().mtime());
1130        let snapshot = buffer.snapshot();
1131        let syntax_map = Mutex::new(SyntaxMap::new(&snapshot));
1132        let tree_sitter_data = TreeSitterData::new(snapshot);
1133        Self {
1134            saved_mtime,
1135            tree_sitter_data: Arc::new(tree_sitter_data),
1136            saved_version: buffer.version(),
1137            preview_version: buffer.version(),
1138            reload_task: None,
1139            transaction_depth: 0,
1140            was_dirty_before_starting_transaction: None,
1141            has_unsaved_edits: Cell::new((buffer.version(), false)),
1142            text: buffer,
1143            branch_state: None,
1144            file,
1145            capability,
1146            syntax_map,
1147            reparse: None,
1148            non_text_state_update_count: 0,
1149            sync_parse_timeout: if cfg!(any(test, feature = "test-support")) {
1150                Some(Duration::from_millis(10))
1151            } else {
1152                Some(Duration::from_millis(1))
1153            },
1154            parse_status: watch::channel(ParseStatus::Idle),
1155            autoindent_requests: Default::default(),
1156            wait_for_autoindent_txs: Default::default(),
1157            pending_autoindent: Default::default(),
1158            language: None,
1159            remote_selections: Default::default(),
1160            diagnostics: Default::default(),
1161            diagnostics_timestamp: Lamport::MIN,
1162            completion_triggers: Default::default(),
1163            completion_triggers_per_language_server: Default::default(),
1164            completion_triggers_timestamp: Lamport::MIN,
1165            deferred_ops: OperationQueue::new(),
1166            has_conflict: false,
1167            change_bits: Default::default(),
1168            modeline: None,
1169            _subscriptions: Vec::new(),
1170            encoding: encoding_rs::UTF_8,
1171            has_bom: false,
1172            reload_with_encoding_txns: HashMap::default(),
1173        }
1174    }
1175
1176    #[ztracing::instrument(skip_all)]
1177    pub fn build_snapshot(
1178        text: Rope,
1179        language: Option<Arc<Language>>,
1180        language_registry: Option<Arc<LanguageRegistry>>,
1181        modeline: Option<Arc<ModelineSettings>>,
1182        cx: &mut App,
1183    ) -> impl Future<Output = BufferSnapshot> + use<> {
1184        let entity_id = cx.reserve_entity::<Self>().entity_id();
1185        let buffer_id = entity_id.as_non_zero_u64().into();
1186        async move {
1187            let text =
1188                TextBuffer::new_normalized(ReplicaId::LOCAL, buffer_id, Default::default(), text);
1189            let text = text.into_snapshot();
1190            let mut syntax = SyntaxMap::new(&text).snapshot();
1191            if let Some(language) = language.clone() {
1192                let language_registry = language_registry.clone();
1193                syntax.reparse(&text, language_registry, language);
1194            }
1195            let tree_sitter_data = TreeSitterData::new(&text);
1196            BufferSnapshot {
1197                text,
1198                syntax,
1199                file: None,
1200                diagnostics: Default::default(),
1201                remote_selections: Default::default(),
1202                tree_sitter_data: Arc::new(tree_sitter_data),
1203                language,
1204                non_text_state_update_count: 0,
1205                capability: Capability::ReadOnly,
1206                modeline,
1207            }
1208        }
1209    }
1210
1211    pub fn build_empty_snapshot(cx: &mut App) -> BufferSnapshot {
1212        let entity_id = cx.reserve_entity::<Self>().entity_id();
1213        let buffer_id = entity_id.as_non_zero_u64().into();
1214        let text = TextBuffer::new_normalized(
1215            ReplicaId::LOCAL,
1216            buffer_id,
1217            Default::default(),
1218            Rope::new(),
1219        );
1220        let text = text.into_snapshot();
1221        let syntax = SyntaxMap::new(&text).snapshot();
1222        let tree_sitter_data = TreeSitterData::new(&text);
1223        BufferSnapshot {
1224            text,
1225            syntax,
1226            tree_sitter_data: Arc::new(tree_sitter_data),
1227            file: None,
1228            diagnostics: Default::default(),
1229            remote_selections: Default::default(),
1230            language: None,
1231            non_text_state_update_count: 0,
1232            capability: Capability::ReadOnly,
1233            modeline: None,
1234        }
1235    }
1236
1237    #[cfg(any(test, feature = "test-support"))]
1238    pub fn build_snapshot_sync(
1239        text: Rope,
1240        language: Option<Arc<Language>>,
1241        language_registry: Option<Arc<LanguageRegistry>>,
1242        cx: &mut App,
1243    ) -> BufferSnapshot {
1244        let entity_id = cx.reserve_entity::<Self>().entity_id();
1245        let buffer_id = entity_id.as_non_zero_u64().into();
1246        let text =
1247            TextBuffer::new_normalized(ReplicaId::LOCAL, buffer_id, Default::default(), text)
1248                .into_snapshot();
1249        let mut syntax = SyntaxMap::new(&text).snapshot();
1250        if let Some(language) = language.clone() {
1251            syntax.reparse(&text, language_registry, language);
1252        }
1253        let tree_sitter_data = TreeSitterData::new(&text);
1254        BufferSnapshot {
1255            text,
1256            syntax,
1257            tree_sitter_data: Arc::new(tree_sitter_data),
1258            file: None,
1259            diagnostics: Default::default(),
1260            remote_selections: Default::default(),
1261            language,
1262            non_text_state_update_count: 0,
1263            capability: Capability::ReadOnly,
1264            modeline: None,
1265        }
1266    }
1267
1268    /// Retrieve a snapshot of the buffer's current state. This is computationally
1269    /// cheap, and allows reading from the buffer on a background thread.
1270    pub fn snapshot(&self) -> BufferSnapshot {
1271        let text = self.text.snapshot();
1272
1273        let syntax = {
1274            let mut syntax_map = self.syntax_map.lock();
1275            syntax_map.interpolate(text);
1276            syntax_map.snapshot()
1277        };
1278
1279        let tree_sitter_data = if self.text.version() != *self.tree_sitter_data.version() {
1280            Arc::new(TreeSitterData::new(text))
1281        } else {
1282            self.tree_sitter_data.clone()
1283        };
1284
1285        BufferSnapshot {
1286            text: text.clone(),
1287            syntax,
1288            tree_sitter_data,
1289            file: self.file.clone(),
1290            remote_selections: self.remote_selections.clone(),
1291            diagnostics: self.diagnostics.clone(),
1292            language: self.language.clone(),
1293            non_text_state_update_count: self.non_text_state_update_count,
1294            capability: self.capability,
1295            modeline: self.modeline.clone(),
1296        }
1297    }
1298
1299    pub fn branch(&mut self, cx: &mut Context<Self>) -> Entity<Self> {
1300        let this = cx.entity();
1301        cx.new(|cx| {
1302            let mut branch = Self {
1303                branch_state: Some(BufferBranchState {
1304                    base_buffer: this.clone(),
1305                    merged_operations: Default::default(),
1306                }),
1307                language: self.language.clone(),
1308                has_conflict: self.has_conflict,
1309                has_unsaved_edits: Cell::new(self.has_unsaved_edits.get_mut().clone()),
1310                _subscriptions: vec![cx.subscribe(&this, Self::on_base_buffer_event)],
1311                ..Self::build(self.text.branch(), self.file.clone(), self.capability())
1312            };
1313            if let Some(language_registry) = self.language_registry() {
1314                branch.set_language_registry(language_registry);
1315            }
1316
1317            // Reparse the branch buffer so that we get syntax highlighting immediately.
1318            branch.reparse(cx, true);
1319
1320            branch
1321        })
1322    }
1323
1324    #[ztracing::instrument(skip_all)]
1325    pub fn preview_edits(
1326        &self,
1327        edits: Arc<[(Range<Anchor>, Arc<str>)]>,
1328        cx: &App,
1329    ) -> Task<EditPreview> {
1330        let registry = self.language_registry();
1331        let language = self.language().cloned();
1332        let old_snapshot = self.text.snapshot().clone();
1333        let mut branch_buffer = self.text.branch();
1334        let mut syntax_snapshot = self.syntax_map.lock().snapshot();
1335        cx.background_spawn(async move {
1336            if !edits.is_empty() {
1337                if let Some(language) = language.clone() {
1338                    syntax_snapshot.reparse(&old_snapshot, registry.clone(), language);
1339                }
1340
1341                branch_buffer.edit(edits.iter().cloned());
1342                let snapshot = branch_buffer.snapshot();
1343                syntax_snapshot.interpolate(&snapshot);
1344
1345                if let Some(language) = language {
1346                    syntax_snapshot.reparse(&snapshot, registry, language);
1347                }
1348            }
1349            EditPreview {
1350                old_snapshot,
1351                applied_edits_snapshot: branch_buffer.into_snapshot(),
1352                syntax_snapshot,
1353            }
1354        })
1355    }
1356
1357    /// Applies all of the changes in this buffer that intersect any of the
1358    /// given `ranges` to its base buffer.
1359    ///
1360    /// If `ranges` is empty, then all changes will be applied. This buffer must
1361    /// be a branch buffer to call this method.
1362    pub fn merge_into_base(&mut self, ranges: Vec<Range<usize>>, cx: &mut Context<Self>) {
1363        let Some(base_buffer) = self.base_buffer() else {
1364            debug_panic!("not a branch buffer");
1365            return;
1366        };
1367
1368        let mut ranges = if ranges.is_empty() {
1369            &[0..usize::MAX]
1370        } else {
1371            ranges.as_slice()
1372        }
1373        .iter()
1374        .peekable();
1375
1376        let mut edits = Vec::new();
1377        for edit in self.edits_since::<usize>(&base_buffer.read(cx).version()) {
1378            let mut is_included = false;
1379            while let Some(range) = ranges.peek() {
1380                if range.end < edit.new.start {
1381                    ranges.next().unwrap();
1382                } else {
1383                    if range.start <= edit.new.end {
1384                        is_included = true;
1385                    }
1386                    break;
1387                }
1388            }
1389
1390            if is_included {
1391                edits.push((
1392                    edit.old.clone(),
1393                    self.text_for_range(edit.new.clone()).collect::<String>(),
1394                ));
1395            }
1396        }
1397
1398        let operation = base_buffer.update(cx, |base_buffer, cx| {
1399            // cx.emit(BufferEvent::DiffBaseChanged);
1400            base_buffer.edit(edits, None, cx)
1401        });
1402
1403        if let Some(operation) = operation
1404            && let Some(BufferBranchState {
1405                merged_operations, ..
1406            }) = &mut self.branch_state
1407        {
1408            merged_operations.push(operation);
1409        }
1410    }
1411
1412    fn on_base_buffer_event(
1413        &mut self,
1414        _: Entity<Buffer>,
1415        event: &BufferEvent,
1416        cx: &mut Context<Self>,
1417    ) {
1418        let BufferEvent::Operation { operation, .. } = event else {
1419            return;
1420        };
1421        let Some(BufferBranchState {
1422            merged_operations, ..
1423        }) = &mut self.branch_state
1424        else {
1425            return;
1426        };
1427
1428        let mut operation_to_undo = None;
1429        if let Operation::Buffer(text::Operation::Edit(operation)) = &operation
1430            && let Ok(ix) = merged_operations.binary_search(&operation.timestamp)
1431        {
1432            merged_operations.remove(ix);
1433            operation_to_undo = Some(operation.timestamp);
1434        }
1435
1436        self.apply_ops([operation.clone()], cx);
1437
1438        if let Some(timestamp) = operation_to_undo {
1439            let counts = [(timestamp, u32::MAX)].into_iter().collect();
1440            self.undo_operations(counts, cx);
1441        }
1442    }
1443
1444    pub fn as_text_snapshot(&self) -> &text::BufferSnapshot {
1445        &self.text
1446    }
1447
1448    /// Retrieve a snapshot of the buffer's raw text, without any
1449    /// language-related state like the syntax tree or diagnostics.
1450    #[ztracing::instrument(skip_all)]
1451    pub fn text_snapshot(&self) -> text::BufferSnapshot {
1452        // todo lw
1453        self.text.snapshot().clone()
1454    }
1455
1456    /// The file associated with the buffer, if any.
1457    pub fn file(&self) -> Option<&Arc<dyn File>> {
1458        self.file.as_ref()
1459    }
1460
1461    /// The version of the buffer that was last saved or reloaded from disk.
1462    pub fn saved_version(&self) -> &clock::Global {
1463        &self.saved_version
1464    }
1465
1466    /// The mtime of the buffer's file when the buffer was last saved or reloaded from disk.
1467    pub fn saved_mtime(&self) -> Option<MTime> {
1468        self.saved_mtime
1469    }
1470
1471    /// Returns the character encoding of the buffer's file.
1472    pub fn encoding(&self) -> &'static Encoding {
1473        self.encoding
1474    }
1475
1476    /// Sets the character encoding of the buffer.
1477    pub fn set_encoding(&mut self, encoding: &'static Encoding) {
1478        self.encoding = encoding;
1479    }
1480
1481    /// Returns whether the buffer has a Byte Order Mark.
1482    pub fn has_bom(&self) -> bool {
1483        self.has_bom
1484    }
1485
1486    /// Sets whether the buffer has a Byte Order Mark.
1487    pub fn set_has_bom(&mut self, has_bom: bool) {
1488        self.has_bom = has_bom;
1489    }
1490
1491    /// Assign a language to the buffer.
1492    pub fn set_language_async(&mut self, language: Option<Arc<Language>>, cx: &mut Context<Self>) {
1493        self.set_language_(language, cfg!(any(test, feature = "test-support")), cx);
1494    }
1495
1496    /// Assign a language to the buffer, blocking for up to 1ms to reparse the buffer.
1497    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut Context<Self>) {
1498        self.set_language_(language, true, cx);
1499    }
1500
1501    #[ztracing::instrument(skip_all)]
1502    fn set_language_(
1503        &mut self,
1504        language: Option<Arc<Language>>,
1505        may_block: bool,
1506        cx: &mut Context<Self>,
1507    ) {
1508        if language == self.language {
1509            return;
1510        }
1511        self.non_text_state_update_count += 1;
1512        self.syntax_map.lock().clear(&self.text);
1513        let old_language = std::mem::replace(&mut self.language, language);
1514        self.was_changed();
1515        self.reparse(cx, may_block);
1516        let has_fresh_language =
1517            self.language.is_some() && old_language.is_none_or(|old| old == *PLAIN_TEXT);
1518        cx.emit(BufferEvent::LanguageChanged(has_fresh_language));
1519    }
1520
1521    /// Assign a language registry to the buffer. This allows the buffer to retrieve
1522    /// other languages if parts of the buffer are written in different languages.
1523    pub fn set_language_registry(&self, language_registry: Arc<LanguageRegistry>) {
1524        self.syntax_map
1525            .lock()
1526            .set_language_registry(language_registry);
1527    }
1528
1529    pub fn language_registry(&self) -> Option<Arc<LanguageRegistry>> {
1530        self.syntax_map.lock().language_registry()
1531    }
1532
1533    /// Assign the line ending type to the buffer.
1534    pub fn set_line_ending(&mut self, line_ending: LineEnding, cx: &mut Context<Self>) {
1535        self.text.set_line_ending(line_ending);
1536
1537        let lamport_timestamp = self.text.lamport_clock.tick();
1538        self.send_operation(
1539            Operation::UpdateLineEnding {
1540                line_ending,
1541                lamport_timestamp,
1542            },
1543            true,
1544            cx,
1545        );
1546    }
1547
1548    /// Assign the buffer [`ModelineSettings`].
1549    pub fn set_modeline(&mut self, modeline: Option<ModelineSettings>) -> bool {
1550        if modeline.as_ref() != self.modeline.as_deref() {
1551            self.modeline = modeline.map(Arc::new);
1552            true
1553        } else {
1554            false
1555        }
1556    }
1557
1558    /// Returns the [`ModelineSettings`].
1559    pub fn modeline(&self) -> Option<&Arc<ModelineSettings>> {
1560        self.modeline.as_ref()
1561    }
1562
1563    /// Assign the buffer a new [`Capability`].
1564    pub fn set_capability(&mut self, capability: Capability, cx: &mut Context<Self>) {
1565        if self.capability != capability {
1566            self.capability = capability;
1567            cx.emit(BufferEvent::CapabilityChanged)
1568        }
1569    }
1570
1571    /// This method is called to signal that the buffer has been saved.
1572    pub fn did_save(
1573        &mut self,
1574        version: clock::Global,
1575        mtime: Option<MTime>,
1576        cx: &mut Context<Self>,
1577    ) {
1578        self.saved_version = version.clone();
1579        self.has_unsaved_edits.set((version, false));
1580        self.has_conflict = false;
1581        self.saved_mtime = mtime;
1582        self.was_changed();
1583        cx.emit(BufferEvent::Saved);
1584        cx.notify();
1585    }
1586
1587    /// Reloads the contents of the buffer from disk.
1588    pub fn reload(&mut self, cx: &Context<Self>) -> oneshot::Receiver<Option<Transaction>> {
1589        self.reload_impl(None, cx)
1590    }
1591
1592    /// Reloads the contents of the buffer from disk using the specified encoding.
1593    ///
1594    /// This bypasses automatic encoding detection heuristics (like BOM checks) for non-Unicode encodings,
1595    /// allowing users to force a specific interpretation of the bytes.
1596    pub fn reload_with_encoding(
1597        &mut self,
1598        encoding: &'static Encoding,
1599        cx: &Context<Self>,
1600    ) -> oneshot::Receiver<Option<Transaction>> {
1601        self.reload_impl(Some(encoding), cx)
1602    }
1603
1604    fn reload_impl(
1605        &mut self,
1606        force_encoding: Option<&'static Encoding>,
1607        cx: &Context<Self>,
1608    ) -> oneshot::Receiver<Option<Transaction>> {
1609        let (tx, rx) = futures::channel::oneshot::channel();
1610        let prev_version = self.text.version();
1611
1612        self.reload_task = Some(cx.spawn(async move |this, cx| {
1613            let Some((new_mtime, load_bytes_task, current_encoding)) =
1614                this.update(cx, |this, cx| {
1615                    let file = this.file.as_ref()?.as_local()?;
1616                    Some((
1617                        file.disk_state().mtime(),
1618                        file.load_bytes(cx),
1619                        this.encoding,
1620                    ))
1621                })?
1622            else {
1623                return Ok(());
1624            };
1625
1626            let target_encoding = force_encoding.unwrap_or(current_encoding);
1627
1628            let is_unicode = target_encoding == encoding_rs::UTF_8
1629                || target_encoding == encoding_rs::UTF_16LE
1630                || target_encoding == encoding_rs::UTF_16BE;
1631
1632            let (new_text, has_bom, encoding_used) = if force_encoding.is_some() && !is_unicode {
1633                let bytes = load_bytes_task.await?;
1634                let (cow, _had_errors) = target_encoding.decode_without_bom_handling(&bytes);
1635                (cow.into_owned(), false, target_encoding)
1636            } else {
1637                let bytes = load_bytes_task.await?;
1638                let (cow, used_enc, _had_errors) = target_encoding.decode(&bytes);
1639
1640                let actual_has_bom = if used_enc == encoding_rs::UTF_8 {
1641                    bytes.starts_with(&[0xEF, 0xBB, 0xBF])
1642                } else if used_enc == encoding_rs::UTF_16LE {
1643                    bytes.starts_with(&[0xFF, 0xFE])
1644                } else if used_enc == encoding_rs::UTF_16BE {
1645                    bytes.starts_with(&[0xFE, 0xFF])
1646                } else {
1647                    false
1648                };
1649                (cow.into_owned(), actual_has_bom, used_enc)
1650            };
1651
1652            let diff = this.update(cx, |this, cx| this.diff(new_text, cx))?.await;
1653            this.update(cx, |this, cx| {
1654                if this.version() == diff.base_version {
1655                    this.finalize_last_transaction();
1656                    let old_encoding = this.encoding;
1657                    let old_has_bom = this.has_bom;
1658                    this.apply_diff(diff, cx);
1659                    this.encoding = encoding_used;
1660                    this.has_bom = has_bom;
1661                    let transaction = this.finalize_last_transaction().cloned();
1662                    if let Some(ref txn) = transaction {
1663                        if old_encoding != encoding_used || old_has_bom != has_bom {
1664                            this.reload_with_encoding_txns
1665                                .insert(txn.id, (old_encoding, old_has_bom));
1666                        }
1667                    }
1668                    tx.send(transaction).ok();
1669                    this.has_conflict = false;
1670                    this.did_reload(this.version(), this.line_ending(), new_mtime, cx);
1671                } else {
1672                    if !diff.edits.is_empty()
1673                        || this
1674                            .edits_since::<usize>(&diff.base_version)
1675                            .next()
1676                            .is_some()
1677                    {
1678                        this.has_conflict = true;
1679                    }
1680
1681                    this.did_reload(prev_version, this.line_ending(), this.saved_mtime, cx);
1682                }
1683
1684                this.reload_task.take();
1685            })
1686        }));
1687        rx
1688    }
1689
1690    /// This method is called to signal that the buffer has been reloaded.
1691    pub fn did_reload(
1692        &mut self,
1693        version: clock::Global,
1694        line_ending: LineEnding,
1695        mtime: Option<MTime>,
1696        cx: &mut Context<Self>,
1697    ) {
1698        self.saved_version = version;
1699        self.has_unsaved_edits
1700            .set((self.saved_version.clone(), false));
1701        self.text.set_line_ending(line_ending);
1702        self.saved_mtime = mtime;
1703        cx.emit(BufferEvent::Reloaded);
1704        cx.notify();
1705    }
1706
1707    /// Updates the [`File`] backing this buffer. This should be called when
1708    /// the file has changed or has been deleted.
1709    pub fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut Context<Self>) {
1710        let was_dirty = self.is_dirty();
1711        let mut file_changed = false;
1712
1713        if let Some(old_file) = self.file.as_ref() {
1714            if new_file.path() != old_file.path() {
1715                file_changed = true;
1716            }
1717
1718            let old_state = old_file.disk_state();
1719            let new_state = new_file.disk_state();
1720            if old_state != new_state {
1721                file_changed = true;
1722                if !was_dirty && matches!(new_state, DiskState::Present { .. }) {
1723                    cx.emit(BufferEvent::ReloadNeeded)
1724                }
1725            }
1726        } else {
1727            file_changed = true;
1728        };
1729
1730        self.file = Some(new_file);
1731        if file_changed {
1732            self.was_changed();
1733            self.non_text_state_update_count += 1;
1734            if was_dirty != self.is_dirty() {
1735                cx.emit(BufferEvent::DirtyChanged);
1736            }
1737            cx.emit(BufferEvent::FileHandleChanged);
1738            cx.notify();
1739        }
1740    }
1741
1742    pub fn base_buffer(&self) -> Option<Entity<Self>> {
1743        Some(self.branch_state.as_ref()?.base_buffer.clone())
1744    }
1745
1746    /// Returns the primary [`Language`] assigned to this [`Buffer`].
1747    pub fn language(&self) -> Option<&Arc<Language>> {
1748        self.language.as_ref()
1749    }
1750
1751    /// Returns the [`Language`] at the given location.
1752    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
1753        let offset = position.to_offset(self);
1754        let text: &TextBufferSnapshot = &self.text;
1755        self.syntax_map
1756            .lock()
1757            .layers_for_range(offset..offset, text, false)
1758            .filter(|layer| {
1759                layer
1760                    .included_sub_ranges
1761                    .is_none_or(|ranges| offset_in_sub_ranges(ranges, offset, text))
1762            })
1763            .last()
1764            .map(|info| info.language.clone())
1765            .or_else(|| self.language.clone())
1766    }
1767
1768    /// Returns each [`Language`] for the active syntax layers at the given location.
1769    pub fn languages_at<D: ToOffset>(&self, position: D) -> Vec<Arc<Language>> {
1770        let offset = position.to_offset(self);
1771        let text: &TextBufferSnapshot = &self.text;
1772        let mut languages: Vec<Arc<Language>> = self
1773            .syntax_map
1774            .lock()
1775            .layers_for_range(offset..offset, text, false)
1776            .filter(|layer| {
1777                // For combined injections, check if offset is within the actual sub-ranges.
1778                layer
1779                    .included_sub_ranges
1780                    .is_none_or(|ranges| offset_in_sub_ranges(ranges, offset, text))
1781            })
1782            .map(|info| info.language.clone())
1783            .collect();
1784
1785        if languages.is_empty()
1786            && let Some(buffer_language) = self.language()
1787        {
1788            languages.push(buffer_language.clone());
1789        }
1790
1791        languages
1792    }
1793
1794    /// An integer version number that accounts for all updates besides
1795    /// the buffer's text itself (which is versioned via a version vector).
1796    pub fn non_text_state_update_count(&self) -> usize {
1797        self.non_text_state_update_count
1798    }
1799
1800    /// Whether the buffer is being parsed in the background.
1801    #[cfg(any(test, feature = "test-support"))]
1802    pub fn is_parsing(&self) -> bool {
1803        self.reparse.is_some()
1804    }
1805
1806    /// Indicates whether the buffer contains any regions that may be
1807    /// written in a language that hasn't been loaded yet.
1808    pub fn contains_unknown_injections(&self) -> bool {
1809        self.syntax_map.lock().contains_unknown_injections()
1810    }
1811
1812    /// Sets the sync parse timeout for this buffer.
1813    ///
1814    /// Setting this to `None` disables sync parsing entirely.
1815    pub fn set_sync_parse_timeout(&mut self, timeout: Option<Duration>) {
1816        self.sync_parse_timeout = timeout;
1817    }
1818
1819    fn invalidate_tree_sitter_data(
1820        tree_sitter_data: &mut Arc<TreeSitterData>,
1821        snapshot: &text::BufferSnapshot,
1822    ) {
1823        match Arc::get_mut(tree_sitter_data) {
1824            Some(tree_sitter_data) => tree_sitter_data.clear(snapshot),
1825            None => {
1826                let new_tree_sitter_data = TreeSitterData::new(snapshot);
1827                *tree_sitter_data = Arc::new(new_tree_sitter_data)
1828            }
1829        }
1830    }
1831
1832    /// Called after an edit to synchronize the buffer's main parse tree with
1833    /// the buffer's new underlying state.
1834    ///
1835    /// Locks the syntax map and interpolates the edits since the last reparse
1836    /// into the foreground syntax tree.
1837    ///
1838    /// Then takes a stable snapshot of the syntax map before unlocking it.
1839    /// The snapshot with the interpolated edits is sent to a background thread,
1840    /// where we ask Tree-sitter to perform an incremental parse.
1841    ///
1842    /// Meanwhile, in the foreground if `may_block` is true, we block the main
1843    /// thread for up to 1ms waiting on the parse to complete. As soon as it
1844    /// completes, we proceed synchronously, unless a 1ms timeout elapses.
1845    ///
1846    /// If we time out waiting on the parse, we spawn a second task waiting
1847    /// until the parse does complete and return with the interpolated tree still
1848    /// in the foreground. When the background parse completes, call back into
1849    /// the main thread and assign the foreground parse state.
1850    ///
1851    /// If the buffer or grammar changed since the start of the background parse,
1852    /// initiate an additional reparse recursively. To avoid concurrent parses
1853    /// for the same buffer, we only initiate a new parse if we are not already
1854    /// parsing in the background.
1855    #[ztracing::instrument(skip_all)]
1856    pub fn reparse(&mut self, cx: &mut Context<Self>, may_block: bool) {
1857        if self.text.version() != *self.tree_sitter_data.version() {
1858            Self::invalidate_tree_sitter_data(&mut self.tree_sitter_data, self.text.snapshot());
1859        }
1860        if self.reparse.is_some() {
1861            return;
1862        }
1863        let language = if let Some(language) = self.language.clone() {
1864            language
1865        } else {
1866            return;
1867        };
1868
1869        let text = self.text_snapshot();
1870        let parsed_version = self.version();
1871
1872        let mut syntax_map = self.syntax_map.lock();
1873        syntax_map.interpolate(&text);
1874        let language_registry = syntax_map.language_registry();
1875        let mut syntax_snapshot = syntax_map.snapshot();
1876        drop(syntax_map);
1877
1878        self.parse_status.0.send(ParseStatus::Parsing).unwrap();
1879        if may_block && let Some(sync_parse_timeout) = self.sync_parse_timeout {
1880            if let Ok(()) = syntax_snapshot.reparse_with_timeout(
1881                &text,
1882                language_registry.clone(),
1883                language.clone(),
1884                sync_parse_timeout,
1885            ) {
1886                self.did_finish_parsing(syntax_snapshot, Some(Duration::from_millis(300)), cx);
1887                self.reparse = None;
1888                return;
1889            }
1890        }
1891
1892        let parse_task = cx.background_spawn({
1893            let language = language.clone();
1894            let language_registry = language_registry.clone();
1895            async move {
1896                syntax_snapshot.reparse(&text, language_registry, language);
1897                syntax_snapshot
1898            }
1899        });
1900
1901        self.reparse = Some(cx.spawn(async move |this, cx| {
1902            let new_syntax_map = parse_task.await;
1903            this.update(cx, move |this, cx| {
1904                let grammar_changed = || {
1905                    this.language
1906                        .as_ref()
1907                        .is_none_or(|current_language| !Arc::ptr_eq(&language, current_language))
1908                };
1909                let language_registry_changed = || {
1910                    new_syntax_map.contains_unknown_injections()
1911                        && language_registry.is_some_and(|registry| {
1912                            registry.version() != new_syntax_map.language_registry_version()
1913                        })
1914                };
1915                let parse_again = this.version.changed_since(&parsed_version)
1916                    || language_registry_changed()
1917                    || grammar_changed();
1918                this.did_finish_parsing(new_syntax_map, None, cx);
1919                this.reparse = None;
1920                if parse_again {
1921                    this.reparse(cx, false);
1922                }
1923            })
1924            .ok();
1925        }));
1926    }
1927
1928    fn did_finish_parsing(
1929        &mut self,
1930        syntax_snapshot: SyntaxSnapshot,
1931        block_budget: Option<Duration>,
1932        cx: &mut Context<Self>,
1933    ) {
1934        self.non_text_state_update_count += 1;
1935        self.syntax_map.lock().did_parse(syntax_snapshot);
1936        self.was_changed();
1937        self.request_autoindent(cx, block_budget);
1938        self.parse_status.0.send(ParseStatus::Idle).unwrap();
1939        Self::invalidate_tree_sitter_data(&mut self.tree_sitter_data, &self.text.snapshot());
1940        cx.emit(BufferEvent::Reparsed);
1941        cx.notify();
1942    }
1943
1944    pub fn parse_status(&self) -> watch::Receiver<ParseStatus> {
1945        self.parse_status.1.clone()
1946    }
1947
1948    /// Wait until the buffer is no longer parsing
1949    pub fn parsing_idle(&self) -> impl Future<Output = ()> + use<> {
1950        let mut parse_status = self.parse_status();
1951        async move {
1952            while *parse_status.borrow() != ParseStatus::Idle {
1953                if parse_status.changed().await.is_err() {
1954                    break;
1955                }
1956            }
1957        }
1958    }
1959
1960    /// Assign to the buffer a set of diagnostics created by a given language server.
1961    pub fn update_diagnostics(
1962        &mut self,
1963        server_id: LanguageServerId,
1964        diagnostics: DiagnosticSet,
1965        cx: &mut Context<Self>,
1966    ) {
1967        let lamport_timestamp = self.text.lamport_clock.tick();
1968        let op = Operation::UpdateDiagnostics {
1969            server_id,
1970            diagnostics: diagnostics.iter().cloned().collect(),
1971            lamport_timestamp,
1972        };
1973
1974        self.apply_diagnostic_update(server_id, diagnostics, lamport_timestamp, cx);
1975        self.send_operation(op, true, cx);
1976    }
1977
1978    pub fn buffer_diagnostics(
1979        &self,
1980        for_server: Option<LanguageServerId>,
1981    ) -> Vec<&DiagnosticEntry<Anchor>> {
1982        match for_server {
1983            Some(server_id) => self
1984                .diagnostics
1985                .get(&server_id)
1986                .map_or_else(Vec::new, |diagnostics| diagnostics.iter().collect()),
1987            None => self
1988                .diagnostics
1989                .iter()
1990                .flat_map(|(_, diagnostic_set)| diagnostic_set.iter())
1991                .collect(),
1992        }
1993    }
1994
1995    fn request_autoindent(&mut self, cx: &mut Context<Self>, block_budget: Option<Duration>) {
1996        if let Some(indent_sizes) = self.compute_autoindents() {
1997            let indent_sizes = cx.background_spawn(indent_sizes);
1998            let Some(block_budget) = block_budget else {
1999                self.pending_autoindent = Some(cx.spawn(async move |this, cx| {
2000                    let indent_sizes = indent_sizes.await;
2001                    this.update(cx, |this, cx| {
2002                        this.apply_autoindents(indent_sizes, cx);
2003                    })
2004                    .ok();
2005                }));
2006                return;
2007            };
2008            match cx
2009                .foreground_executor()
2010                .block_with_timeout(block_budget, indent_sizes)
2011            {
2012                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
2013                Err(indent_sizes) => {
2014                    self.pending_autoindent = Some(cx.spawn(async move |this, cx| {
2015                        let indent_sizes = indent_sizes.await;
2016                        this.update(cx, |this, cx| {
2017                            this.apply_autoindents(indent_sizes, cx);
2018                        })
2019                        .ok();
2020                    }));
2021                }
2022            }
2023        } else {
2024            self.autoindent_requests.clear();
2025            for tx in self.wait_for_autoindent_txs.drain(..) {
2026                tx.send(()).ok();
2027            }
2028        }
2029    }
2030
2031    fn compute_autoindents(
2032        &self,
2033    ) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>> + use<>> {
2034        let max_rows_between_yields = 100;
2035        let snapshot = self.snapshot();
2036        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
2037            return None;
2038        }
2039
2040        let autoindent_requests = self.autoindent_requests.clone();
2041        Some(async move {
2042            let mut indent_sizes = BTreeMap::<u32, (IndentSize, bool)>::new();
2043            for request in autoindent_requests {
2044                // Resolve each edited range to its row in the current buffer and in the
2045                // buffer before this batch of edits.
2046                let mut row_ranges = Vec::new();
2047                let mut old_to_new_rows = BTreeMap::new();
2048                let mut language_indent_sizes_by_new_row = Vec::new();
2049                for entry in &request.entries {
2050                    let position = entry.range.start;
2051                    let new_row = position.to_point(&snapshot).row;
2052                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
2053                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
2054
2055                    if let Some(old_row) = entry.old_row {
2056                        old_to_new_rows.insert(old_row, new_row);
2057                    }
2058                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
2059                }
2060
2061                // Build a map containing the suggested indentation for each of the edited lines
2062                // with respect to the state of the buffer before these edits. This map is keyed
2063                // by the rows for these lines in the current state of the buffer.
2064                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
2065                let old_edited_ranges =
2066                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
2067                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
2068                let mut language_indent_size = IndentSize::default();
2069                for old_edited_range in old_edited_ranges {
2070                    let suggestions = request
2071                        .before_edit
2072                        .suggest_autoindents(old_edited_range.clone())
2073                        .into_iter()
2074                        .flatten();
2075                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
2076                        if let Some(suggestion) = suggestion {
2077                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
2078
2079                            // Find the indent size based on the language for this row.
2080                            while let Some((row, size)) = language_indent_sizes.peek() {
2081                                if *row > new_row {
2082                                    break;
2083                                }
2084                                language_indent_size = *size;
2085                                language_indent_sizes.next();
2086                            }
2087
2088                            let suggested_indent = old_to_new_rows
2089                                .get(&suggestion.basis_row)
2090                                .and_then(|from_row| {
2091                                    Some(old_suggestions.get(from_row).copied()?.0)
2092                                })
2093                                .unwrap_or_else(|| {
2094                                    request
2095                                        .before_edit
2096                                        .indent_size_for_line(suggestion.basis_row)
2097                                })
2098                                .with_delta(suggestion.delta, language_indent_size);
2099                            old_suggestions
2100                                .insert(new_row, (suggested_indent, suggestion.within_error));
2101                        }
2102                    }
2103                    yield_now().await;
2104                }
2105
2106                // Compute new suggestions for each line, but only include them in the result
2107                // if they differ from the old suggestion for that line.
2108                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
2109                let mut language_indent_size = IndentSize::default();
2110                for (row_range, original_indent_column) in row_ranges {
2111                    let new_edited_row_range = if request.is_block_mode {
2112                        row_range.start..row_range.start + 1
2113                    } else {
2114                        row_range.clone()
2115                    };
2116
2117                    let suggestions = snapshot
2118                        .suggest_autoindents(new_edited_row_range.clone())
2119                        .into_iter()
2120                        .flatten();
2121                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
2122                        if let Some(suggestion) = suggestion {
2123                            // Find the indent size based on the language for this row.
2124                            while let Some((row, size)) = language_indent_sizes.peek() {
2125                                if *row > new_row {
2126                                    break;
2127                                }
2128                                language_indent_size = *size;
2129                                language_indent_sizes.next();
2130                            }
2131
2132                            let suggested_indent = indent_sizes
2133                                .get(&suggestion.basis_row)
2134                                .copied()
2135                                .map(|e| e.0)
2136                                .unwrap_or_else(|| {
2137                                    snapshot.indent_size_for_line(suggestion.basis_row)
2138                                })
2139                                .with_delta(suggestion.delta, language_indent_size);
2140
2141                            if old_suggestions.get(&new_row).is_none_or(
2142                                |(old_indentation, was_within_error)| {
2143                                    suggested_indent != *old_indentation
2144                                        && (!suggestion.within_error || *was_within_error)
2145                                },
2146                            ) {
2147                                indent_sizes.insert(
2148                                    new_row,
2149                                    (suggested_indent, request.ignore_empty_lines),
2150                                );
2151                            }
2152                        }
2153                    }
2154
2155                    if let (true, Some(original_indent_column)) =
2156                        (request.is_block_mode, original_indent_column)
2157                    {
2158                        let new_indent =
2159                            if let Some((indent, _)) = indent_sizes.get(&row_range.start) {
2160                                *indent
2161                            } else {
2162                                snapshot.indent_size_for_line(row_range.start)
2163                            };
2164                        let delta = new_indent.len as i64 - original_indent_column as i64;
2165                        if delta != 0 {
2166                            for row in row_range.skip(1) {
2167                                indent_sizes.entry(row).or_insert_with(|| {
2168                                    let mut size = snapshot.indent_size_for_line(row);
2169                                    if size.kind == new_indent.kind {
2170                                        match delta.cmp(&0) {
2171                                            Ordering::Greater => size.len += delta as u32,
2172                                            Ordering::Less => {
2173                                                size.len = size.len.saturating_sub(-delta as u32)
2174                                            }
2175                                            Ordering::Equal => {}
2176                                        }
2177                                    }
2178                                    (size, request.ignore_empty_lines)
2179                                });
2180                            }
2181                        }
2182                    }
2183
2184                    yield_now().await;
2185                }
2186            }
2187
2188            indent_sizes
2189                .into_iter()
2190                .filter_map(|(row, (indent, ignore_empty_lines))| {
2191                    if ignore_empty_lines && snapshot.line_len(row) == 0 {
2192                        None
2193                    } else {
2194                        Some((row, indent))
2195                    }
2196                })
2197                .collect()
2198        })
2199    }
2200
2201    fn apply_autoindents(
2202        &mut self,
2203        indent_sizes: BTreeMap<u32, IndentSize>,
2204        cx: &mut Context<Self>,
2205    ) {
2206        self.autoindent_requests.clear();
2207        for tx in self.wait_for_autoindent_txs.drain(..) {
2208            tx.send(()).ok();
2209        }
2210
2211        let edits: Vec<_> = indent_sizes
2212            .into_iter()
2213            .filter_map(|(row, indent_size)| {
2214                let current_size = indent_size_for_line(self, row);
2215                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
2216            })
2217            .collect();
2218
2219        let preserve_preview = self.preserve_preview();
2220        self.edit(edits, None, cx);
2221        if preserve_preview {
2222            self.refresh_preview();
2223        }
2224    }
2225
2226    /// Create a minimal edit that will cause the given row to be indented
2227    /// with the given size. After applying this edit, the length of the line
2228    /// will always be at least `new_size.len`.
2229    pub fn edit_for_indent_size_adjustment(
2230        row: u32,
2231        current_size: IndentSize,
2232        new_size: IndentSize,
2233    ) -> Option<(Range<Point>, String)> {
2234        if new_size.kind == current_size.kind {
2235            match new_size.len.cmp(&current_size.len) {
2236                Ordering::Greater => {
2237                    let point = Point::new(row, 0);
2238                    Some((
2239                        point..point,
2240                        iter::repeat(new_size.char())
2241                            .take((new_size.len - current_size.len) as usize)
2242                            .collect::<String>(),
2243                    ))
2244                }
2245
2246                Ordering::Less => Some((
2247                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
2248                    String::new(),
2249                )),
2250
2251                Ordering::Equal => None,
2252            }
2253        } else {
2254            Some((
2255                Point::new(row, 0)..Point::new(row, current_size.len),
2256                iter::repeat(new_size.char())
2257                    .take(new_size.len as usize)
2258                    .collect::<String>(),
2259            ))
2260        }
2261    }
2262
2263    /// Spawns a background task that asynchronously computes a `Diff` between the buffer's text
2264    /// and the given new text.
2265    pub fn diff<T>(&self, new_text: T, cx: &App) -> Task<Diff>
2266    where
2267        T: AsRef<str> + Send + 'static,
2268    {
2269        let old_text = self.as_rope().clone();
2270        let base_version = self.version();
2271        cx.background_spawn(async move {
2272            let old_text = old_text.to_string();
2273            let mut new_text = new_text.as_ref().to_owned();
2274            let line_ending = LineEnding::detect(&new_text);
2275            LineEnding::normalize(&mut new_text);
2276            let edits = text_diff(&old_text, &new_text);
2277            Diff {
2278                base_version,
2279                line_ending,
2280                edits,
2281            }
2282        })
2283    }
2284
2285    /// Spawns a background task that searches the buffer for any whitespace
2286    /// at the ends of a lines, and returns a `Diff` that removes that whitespace.
2287    pub fn remove_trailing_whitespace(&self, cx: &App) -> Task<Diff> {
2288        let old_text = self.as_rope().clone();
2289        let line_ending = self.line_ending();
2290        let base_version = self.version();
2291        cx.background_spawn(async move {
2292            let ranges = trailing_whitespace_ranges(&old_text);
2293            let empty = Arc::<str>::from("");
2294            Diff {
2295                base_version,
2296                line_ending,
2297                edits: ranges
2298                    .into_iter()
2299                    .map(|range| (range, empty.clone()))
2300                    .collect(),
2301            }
2302        })
2303    }
2304
2305    /// Ensures that the buffer ends with a single newline character, and
2306    /// no other whitespace. Skips if the buffer is empty.
2307    pub fn ensure_final_newline(&mut self, cx: &mut Context<Self>) {
2308        let len = self.len();
2309        if len == 0 {
2310            return;
2311        }
2312        let mut offset = len;
2313        for chunk in self.as_rope().reversed_chunks_in_range(0..len) {
2314            let non_whitespace_len = chunk
2315                .trim_end_matches(|c: char| c.is_ascii_whitespace())
2316                .len();
2317            offset -= chunk.len();
2318            offset += non_whitespace_len;
2319            if non_whitespace_len != 0 {
2320                if offset == len - 1 && chunk.get(non_whitespace_len..) == Some("\n") {
2321                    return;
2322                }
2323                break;
2324            }
2325        }
2326        self.edit([(offset..len, "\n")], None, cx);
2327    }
2328
2329    /// Applies a diff to the buffer. If the buffer has changed since the given diff was
2330    /// calculated, then adjust the diff to account for those changes, and discard any
2331    /// parts of the diff that conflict with those changes.
2332    pub fn apply_diff(&mut self, diff: Diff, cx: &mut Context<Self>) -> Option<TransactionId> {
2333        let snapshot = self.snapshot();
2334        let mut edits_since = snapshot.edits_since::<usize>(&diff.base_version).peekable();
2335        let mut delta = 0;
2336        let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
2337            while let Some(edit_since) = edits_since.peek() {
2338                // If the edit occurs after a diff hunk, then it does not
2339                // affect that hunk.
2340                if edit_since.old.start > range.end {
2341                    break;
2342                }
2343                // If the edit precedes the diff hunk, then adjust the hunk
2344                // to reflect the edit.
2345                else if edit_since.old.end < range.start {
2346                    delta += edit_since.new_len() as i64 - edit_since.old_len() as i64;
2347                    edits_since.next();
2348                }
2349                // If the edit intersects a diff hunk, then discard that hunk.
2350                else {
2351                    return None;
2352                }
2353            }
2354
2355            let start = (range.start as i64 + delta) as usize;
2356            let end = (range.end as i64 + delta) as usize;
2357            Some((start..end, new_text))
2358        });
2359
2360        self.start_transaction();
2361        self.text.set_line_ending(diff.line_ending);
2362        self.edit(adjusted_edits, None, cx);
2363        self.end_transaction(cx)
2364    }
2365
2366    pub fn has_unsaved_edits(&self) -> bool {
2367        let (last_version, has_unsaved_edits) = self.has_unsaved_edits.take();
2368
2369        if last_version == self.version {
2370            self.has_unsaved_edits
2371                .set((last_version, has_unsaved_edits));
2372            return has_unsaved_edits;
2373        }
2374
2375        let has_edits = self.has_edits_since(&self.saved_version);
2376        self.has_unsaved_edits
2377            .set((self.version.clone(), has_edits));
2378        has_edits
2379    }
2380
2381    /// Checks if the buffer has unsaved changes.
2382    pub fn is_dirty(&self) -> bool {
2383        if self.capability == Capability::ReadOnly {
2384            return false;
2385        }
2386        if self.has_conflict {
2387            return true;
2388        }
2389        match self.file.as_ref().map(|f| f.disk_state()) {
2390            Some(DiskState::New) | Some(DiskState::Deleted) => {
2391                !self.is_empty() && self.has_unsaved_edits()
2392            }
2393            _ => self.has_unsaved_edits(),
2394        }
2395    }
2396
2397    /// Marks the buffer as having a conflict regardless of current buffer state.
2398    pub fn set_conflict(&mut self) {
2399        self.has_conflict = true;
2400    }
2401
2402    /// Checks if the buffer and its file have both changed since the buffer
2403    /// was last saved or reloaded.
2404    pub fn has_conflict(&self) -> bool {
2405        if self.has_conflict {
2406            return true;
2407        }
2408        let Some(file) = self.file.as_ref() else {
2409            return false;
2410        };
2411        match file.disk_state() {
2412            DiskState::New => false,
2413            DiskState::Present { mtime, .. } => match self.saved_mtime {
2414                Some(saved_mtime) => {
2415                    mtime.bad_is_greater_than(saved_mtime) && self.has_unsaved_edits()
2416                }
2417                None => true,
2418            },
2419            DiskState::Deleted => false,
2420            DiskState::Historic { .. } => false,
2421        }
2422    }
2423
2424    /// Gets a [`Subscription`] that tracks all of the changes to the buffer's text.
2425    pub fn subscribe(&mut self) -> Subscription<usize> {
2426        self.text.subscribe()
2427    }
2428
2429    /// Adds a bit to the list of bits that are set when the buffer's text changes.
2430    ///
2431    /// This allows downstream code to check if the buffer's text has changed without
2432    /// waiting for an effect cycle, which would be required if using eents.
2433    pub fn record_changes(&mut self, bit: rc::Weak<Cell<bool>>) {
2434        if let Err(ix) = self
2435            .change_bits
2436            .binary_search_by_key(&rc::Weak::as_ptr(&bit), rc::Weak::as_ptr)
2437        {
2438            self.change_bits.insert(ix, bit);
2439        }
2440    }
2441
2442    /// Set the change bit for all "listeners".
2443    fn was_changed(&mut self) {
2444        self.change_bits.retain(|change_bit| {
2445            change_bit
2446                .upgrade()
2447                .inspect(|bit| {
2448                    _ = bit.replace(true);
2449                })
2450                .is_some()
2451        });
2452    }
2453
2454    /// Starts a transaction, if one is not already in-progress. When undoing or
2455    /// redoing edits, all of the edits performed within a transaction are undone
2456    /// or redone together.
2457    pub fn start_transaction(&mut self) -> Option<TransactionId> {
2458        self.start_transaction_at(Instant::now())
2459    }
2460
2461    /// Starts a transaction, providing the current time. Subsequent transactions
2462    /// that occur within a short period of time will be grouped together. This
2463    /// is controlled by the buffer's undo grouping duration.
2464    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
2465        self.transaction_depth += 1;
2466        if self.was_dirty_before_starting_transaction.is_none() {
2467            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
2468        }
2469        self.text.start_transaction_at(now)
2470    }
2471
2472    /// Terminates the current transaction, if this is the outermost transaction.
2473    pub fn end_transaction(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
2474        self.end_transaction_at(Instant::now(), cx)
2475    }
2476
2477    /// Terminates the current transaction, providing the current time. Subsequent transactions
2478    /// that occur within a short period of time will be grouped together. This
2479    /// is controlled by the buffer's undo grouping duration.
2480    pub fn end_transaction_at(
2481        &mut self,
2482        now: Instant,
2483        cx: &mut Context<Self>,
2484    ) -> Option<TransactionId> {
2485        assert!(self.transaction_depth > 0);
2486        self.transaction_depth -= 1;
2487        let was_dirty = if self.transaction_depth == 0 {
2488            self.was_dirty_before_starting_transaction.take().unwrap()
2489        } else {
2490            false
2491        };
2492        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
2493            self.did_edit(&start_version, was_dirty, true, cx);
2494            Some(transaction_id)
2495        } else {
2496            None
2497        }
2498    }
2499
2500    /// Manually add a transaction to the buffer's undo history.
2501    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
2502        self.text.push_transaction(transaction, now);
2503    }
2504
2505    /// Differs from `push_transaction` in that it does not clear the redo
2506    /// stack. Intended to be used to create a parent transaction to merge
2507    /// potential child transactions into.
2508    ///
2509    /// The caller is responsible for removing it from the undo history using
2510    /// `forget_transaction` if no edits are merged into it. Otherwise, if edits
2511    /// are merged into this transaction, the caller is responsible for ensuring
2512    /// the redo stack is cleared. The easiest way to ensure the redo stack is
2513    /// cleared is to create transactions with the usual `start_transaction` and
2514    /// `end_transaction` methods and merging the resulting transactions into
2515    /// the transaction created by this method
2516    pub fn push_empty_transaction(&mut self, now: Instant) -> TransactionId {
2517        self.text.push_empty_transaction(now)
2518    }
2519
2520    /// Prevent the last transaction from being grouped with any subsequent transactions,
2521    /// even if they occur with the buffer's undo grouping duration.
2522    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
2523        self.text.finalize_last_transaction()
2524    }
2525
2526    /// Manually group all changes since a given transaction.
2527    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
2528        self.text.group_until_transaction(transaction_id);
2529    }
2530
2531    /// Manually remove a transaction from the buffer's undo history
2532    pub fn forget_transaction(&mut self, transaction_id: TransactionId) -> Option<Transaction> {
2533        self.text.forget_transaction(transaction_id)
2534    }
2535
2536    /// Retrieve a transaction from the buffer's undo history
2537    pub fn get_transaction(&self, transaction_id: TransactionId) -> Option<&Transaction> {
2538        self.text.get_transaction(transaction_id)
2539    }
2540
2541    /// Manually merge two transactions in the buffer's undo history.
2542    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
2543        self.text.merge_transactions(transaction, destination);
2544    }
2545
2546    /// Waits for the buffer to receive operations with the given timestamps.
2547    pub fn wait_for_edits<It: IntoIterator<Item = clock::Lamport>>(
2548        &mut self,
2549        edit_ids: It,
2550    ) -> impl Future<Output = Result<()>> + use<It> {
2551        self.text.wait_for_edits(edit_ids)
2552    }
2553
2554    /// Waits for the buffer to receive the operations necessary for resolving the given anchors.
2555    pub fn wait_for_anchors<It: IntoIterator<Item = Anchor>>(
2556        &mut self,
2557        anchors: It,
2558    ) -> impl 'static + Future<Output = Result<()>> + use<It> {
2559        self.text.wait_for_anchors(anchors)
2560    }
2561
2562    /// Waits for the buffer to receive operations up to the given version.
2563    pub fn wait_for_version(
2564        &mut self,
2565        version: clock::Global,
2566    ) -> impl Future<Output = Result<()>> + use<> {
2567        self.text.wait_for_version(version)
2568    }
2569
2570    /// Forces all futures returned by [`Buffer::wait_for_version`], [`Buffer::wait_for_edits`], or
2571    /// [`Buffer::wait_for_version`] to resolve with an error.
2572    pub fn give_up_waiting(&mut self) {
2573        self.text.give_up_waiting();
2574    }
2575
2576    pub fn wait_for_autoindent_applied(&mut self) -> Option<oneshot::Receiver<()>> {
2577        let mut rx = None;
2578        if !self.autoindent_requests.is_empty() {
2579            let channel = oneshot::channel();
2580            self.wait_for_autoindent_txs.push(channel.0);
2581            rx = Some(channel.1);
2582        }
2583        rx
2584    }
2585
2586    /// Stores a set of selections that should be broadcasted to all of the buffer's replicas.
2587    pub fn set_active_selections(
2588        &mut self,
2589        selections: Arc<[Selection<Anchor>]>,
2590        line_mode: bool,
2591        cursor_shape: CursorShape,
2592        cx: &mut Context<Self>,
2593    ) {
2594        let lamport_timestamp = self.text.lamport_clock.tick();
2595        self.remote_selections.insert(
2596            self.text.replica_id(),
2597            SelectionSet {
2598                selections: selections.clone(),
2599                lamport_timestamp,
2600                line_mode,
2601                cursor_shape,
2602            },
2603        );
2604        self.send_operation(
2605            Operation::UpdateSelections {
2606                selections,
2607                line_mode,
2608                lamport_timestamp,
2609                cursor_shape,
2610            },
2611            true,
2612            cx,
2613        );
2614        self.non_text_state_update_count += 1;
2615        cx.notify();
2616    }
2617
2618    /// Clears the selections, so that other replicas of the buffer do not see any selections for
2619    /// this replica.
2620    pub fn remove_active_selections(&mut self, cx: &mut Context<Self>) {
2621        if self
2622            .remote_selections
2623            .get(&self.text.replica_id())
2624            .is_none_or(|set| !set.selections.is_empty())
2625        {
2626            self.set_active_selections(Arc::default(), false, Default::default(), cx);
2627        }
2628    }
2629
2630    pub fn set_agent_selections(
2631        &mut self,
2632        selections: Arc<[Selection<Anchor>]>,
2633        line_mode: bool,
2634        cursor_shape: CursorShape,
2635        cx: &mut Context<Self>,
2636    ) {
2637        let lamport_timestamp = self.text.lamport_clock.tick();
2638        self.remote_selections.insert(
2639            ReplicaId::AGENT,
2640            SelectionSet {
2641                selections,
2642                lamport_timestamp,
2643                line_mode,
2644                cursor_shape,
2645            },
2646        );
2647        self.non_text_state_update_count += 1;
2648        cx.notify();
2649    }
2650
2651    pub fn remove_agent_selections(&mut self, cx: &mut Context<Self>) {
2652        self.set_agent_selections(Arc::default(), false, Default::default(), cx);
2653    }
2654
2655    /// Replaces the buffer's entire text.
2656    pub fn set_text<T>(&mut self, text: T, cx: &mut Context<Self>) -> Option<clock::Lamport>
2657    where
2658        T: Into<Arc<str>>,
2659    {
2660        self.autoindent_requests.clear();
2661        self.edit([(0..self.len(), text)], None, cx)
2662    }
2663
2664    /// Appends the given text to the end of the buffer.
2665    pub fn append<T>(&mut self, text: T, cx: &mut Context<Self>) -> Option<clock::Lamport>
2666    where
2667        T: Into<Arc<str>>,
2668    {
2669        self.edit([(self.len()..self.len(), text)], None, cx)
2670    }
2671
2672    /// Applies the given edits to the buffer. Each edit is specified as a range of text to
2673    /// delete, and a string of text to insert at that location. Adjacent edits are coalesced.
2674    ///
2675    /// If an [`AutoindentMode`] is provided, then the buffer will enqueue an auto-indent
2676    /// request for the edited ranges, which will be processed when the buffer finishes
2677    /// parsing.
2678    ///
2679    /// Parsing takes place at the end of a transaction, and may compute synchronously
2680    /// or asynchronously, depending on the changes.
2681    pub fn edit<I, S, T>(
2682        &mut self,
2683        edits_iter: I,
2684        autoindent_mode: Option<AutoindentMode>,
2685        cx: &mut Context<Self>,
2686    ) -> Option<clock::Lamport>
2687    where
2688        I: IntoIterator<Item = (Range<S>, T)>,
2689        S: ToOffset,
2690        T: Into<Arc<str>>,
2691    {
2692        self.edit_internal(edits_iter, autoindent_mode, true, cx)
2693    }
2694
2695    /// Like [`edit`](Self::edit), but does not coalesce adjacent edits.
2696    pub fn edit_non_coalesce<I, S, T>(
2697        &mut self,
2698        edits_iter: I,
2699        autoindent_mode: Option<AutoindentMode>,
2700        cx: &mut Context<Self>,
2701    ) -> Option<clock::Lamport>
2702    where
2703        I: IntoIterator<Item = (Range<S>, T)>,
2704        S: ToOffset,
2705        T: Into<Arc<str>>,
2706    {
2707        self.edit_internal(edits_iter, autoindent_mode, false, cx)
2708    }
2709
2710    fn edit_internal<I, S, T>(
2711        &mut self,
2712        edits_iter: I,
2713        autoindent_mode: Option<AutoindentMode>,
2714        coalesce_adjacent: bool,
2715        cx: &mut Context<Self>,
2716    ) -> Option<clock::Lamport>
2717    where
2718        I: IntoIterator<Item = (Range<S>, T)>,
2719        S: ToOffset,
2720        T: Into<Arc<str>>,
2721    {
2722        // Skip invalid edits and coalesce contiguous ones.
2723        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
2724
2725        for (range, new_text) in edits_iter {
2726            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
2727
2728            if range.start > range.end {
2729                mem::swap(&mut range.start, &mut range.end);
2730            }
2731            let new_text = new_text.into();
2732            if !new_text.is_empty() || !range.is_empty() {
2733                let prev_edit = edits.last_mut();
2734                let should_coalesce = prev_edit.as_ref().is_some_and(|(prev_range, _)| {
2735                    if coalesce_adjacent {
2736                        prev_range.end >= range.start
2737                    } else {
2738                        prev_range.end > range.start
2739                    }
2740                });
2741
2742                if let Some((prev_range, prev_text)) = prev_edit
2743                    && should_coalesce
2744                {
2745                    prev_range.end = cmp::max(prev_range.end, range.end);
2746                    *prev_text = format!("{prev_text}{new_text}").into();
2747                } else {
2748                    edits.push((range, new_text));
2749                }
2750            }
2751        }
2752        if edits.is_empty() {
2753            return None;
2754        }
2755
2756        self.start_transaction();
2757        self.pending_autoindent.take();
2758        let autoindent_request = autoindent_mode
2759            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
2760
2761        let edit_operation = self.text.edit(edits.iter().cloned());
2762        let edit_id = edit_operation.timestamp();
2763
2764        if let Some((before_edit, mode)) = autoindent_request {
2765            let mut delta = 0isize;
2766            let mut previous_setting = None;
2767            let entries: Vec<_> = edits
2768                .into_iter()
2769                .enumerate()
2770                .zip(&edit_operation.as_edit().unwrap().new_text)
2771                .filter(|((_, (range, _)), _)| {
2772                    let language = before_edit.language_at(range.start);
2773                    let language_id = language.map(|l| l.id());
2774                    if let Some((cached_language_id, apply_syntax_indent)) = previous_setting
2775                        && cached_language_id == language_id
2776                    {
2777                        apply_syntax_indent
2778                    } else {
2779                        // The auto-indent setting is not present in editorconfigs, hence
2780                        // we can avoid passing the file here.
2781                        let auto_indent_mode = LanguageSettings::resolve(
2782                            None,
2783                            language.map(|l| l.name()).as_ref(),
2784                            cx,
2785                        )
2786                        .auto_indent;
2787                        let apply_syntax_indent = auto_indent_mode == AutoIndentMode::SyntaxAware;
2788                        previous_setting = Some((language_id, apply_syntax_indent));
2789                        apply_syntax_indent
2790                    }
2791                })
2792                .map(|((ix, (range, _)), new_text)| {
2793                    let new_text_length = new_text.len();
2794                    let old_start = range.start.to_point(&before_edit);
2795                    let new_start = (delta + range.start as isize) as usize;
2796                    let range_len = range.end - range.start;
2797                    delta += new_text_length as isize - range_len as isize;
2798
2799                    // Decide what range of the insertion to auto-indent, and whether
2800                    // the first line of the insertion should be considered a newly-inserted line
2801                    // or an edit to an existing line.
2802                    let mut range_of_insertion_to_indent = 0..new_text_length;
2803                    let mut first_line_is_new = true;
2804
2805                    let old_line_start = before_edit.indent_size_for_line(old_start.row).len;
2806                    let old_line_end = before_edit.line_len(old_start.row);
2807
2808                    if old_start.column > old_line_start {
2809                        first_line_is_new = false;
2810                    }
2811
2812                    if !new_text.contains('\n')
2813                        && (old_start.column + (range_len as u32) < old_line_end
2814                            || old_line_end == old_line_start)
2815                    {
2816                        first_line_is_new = false;
2817                    }
2818
2819                    // When inserting text starting with a newline, avoid auto-indenting the
2820                    // previous line.
2821                    if new_text.starts_with('\n') {
2822                        range_of_insertion_to_indent.start += 1;
2823                        first_line_is_new = true;
2824                    }
2825
2826                    let mut original_indent_column = None;
2827                    if let AutoindentMode::Block {
2828                        original_indent_columns,
2829                    } = &mode
2830                    {
2831                        original_indent_column = Some(if new_text.starts_with('\n') {
2832                            indent_size_for_text(
2833                                new_text[range_of_insertion_to_indent.clone()].chars(),
2834                            )
2835                            .len
2836                        } else {
2837                            original_indent_columns
2838                                .get(ix)
2839                                .copied()
2840                                .flatten()
2841                                .unwrap_or_else(|| {
2842                                    indent_size_for_text(
2843                                        new_text[range_of_insertion_to_indent.clone()].chars(),
2844                                    )
2845                                    .len
2846                                })
2847                        });
2848
2849                        // Avoid auto-indenting the line after the edit.
2850                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
2851                            range_of_insertion_to_indent.end -= 1;
2852                        }
2853                    }
2854
2855                    AutoindentRequestEntry {
2856                        original_indent_column,
2857                        old_row: if first_line_is_new {
2858                            None
2859                        } else {
2860                            Some(old_start.row)
2861                        },
2862                        indent_size: before_edit.language_indent_size_at(range.start, cx),
2863                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
2864                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
2865                    }
2866                })
2867                .collect();
2868
2869            if !entries.is_empty() {
2870                self.autoindent_requests.push(Arc::new(AutoindentRequest {
2871                    before_edit,
2872                    entries,
2873                    is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
2874                    ignore_empty_lines: false,
2875                }));
2876            }
2877        }
2878
2879        self.end_transaction(cx);
2880        self.send_operation(Operation::Buffer(edit_operation), true, cx);
2881        Some(edit_id)
2882    }
2883
2884    fn did_edit(
2885        &mut self,
2886        old_version: &clock::Global,
2887        was_dirty: bool,
2888        is_local: bool,
2889        cx: &mut Context<Self>,
2890    ) {
2891        self.was_changed();
2892
2893        if self.edits_since::<usize>(old_version).next().is_none() {
2894            return;
2895        }
2896
2897        self.reparse(cx, true);
2898        cx.emit(BufferEvent::Edited { is_local });
2899        let is_dirty = self.is_dirty();
2900        if was_dirty != is_dirty {
2901            cx.emit(BufferEvent::DirtyChanged);
2902        }
2903        if was_dirty && !is_dirty {
2904            if let Some(file) = self.file.as_ref() {
2905                if matches!(file.disk_state(), DiskState::Present { .. })
2906                    && file.disk_state().mtime() != self.saved_mtime
2907                {
2908                    cx.emit(BufferEvent::ReloadNeeded);
2909                }
2910            }
2911        }
2912        cx.notify();
2913    }
2914
2915    pub fn autoindent_ranges<I, T>(&mut self, ranges: I, cx: &mut Context<Self>)
2916    where
2917        I: IntoIterator<Item = Range<T>>,
2918        T: ToOffset + Copy,
2919    {
2920        let before_edit = self.snapshot();
2921        let entries = ranges
2922            .into_iter()
2923            .map(|range| AutoindentRequestEntry {
2924                range: before_edit.anchor_before(range.start)..before_edit.anchor_after(range.end),
2925                old_row: None,
2926                indent_size: before_edit.language_indent_size_at(range.start, cx),
2927                original_indent_column: None,
2928            })
2929            .collect();
2930        self.autoindent_requests.push(Arc::new(AutoindentRequest {
2931            before_edit,
2932            entries,
2933            is_block_mode: false,
2934            ignore_empty_lines: true,
2935        }));
2936        self.request_autoindent(cx, Some(Duration::from_micros(300)));
2937    }
2938
2939    // Inserts newlines at the given position to create an empty line, returning the start of the new line.
2940    // You can also request the insertion of empty lines above and below the line starting at the returned point.
2941    pub fn insert_empty_line(
2942        &mut self,
2943        position: impl ToPoint,
2944        space_above: bool,
2945        space_below: bool,
2946        cx: &mut Context<Self>,
2947    ) -> Point {
2948        let mut position = position.to_point(self);
2949
2950        self.start_transaction();
2951
2952        self.edit(
2953            [(position..position, "\n")],
2954            Some(AutoindentMode::EachLine),
2955            cx,
2956        );
2957
2958        if position.column > 0 {
2959            position += Point::new(1, 0);
2960        }
2961
2962        if !self.is_line_blank(position.row) {
2963            self.edit(
2964                [(position..position, "\n")],
2965                Some(AutoindentMode::EachLine),
2966                cx,
2967            );
2968        }
2969
2970        if space_above && position.row > 0 && !self.is_line_blank(position.row - 1) {
2971            self.edit(
2972                [(position..position, "\n")],
2973                Some(AutoindentMode::EachLine),
2974                cx,
2975            );
2976            position.row += 1;
2977        }
2978
2979        if space_below
2980            && (position.row == self.max_point().row || !self.is_line_blank(position.row + 1))
2981        {
2982            self.edit(
2983                [(position..position, "\n")],
2984                Some(AutoindentMode::EachLine),
2985                cx,
2986            );
2987        }
2988
2989        self.end_transaction(cx);
2990
2991        position
2992    }
2993
2994    /// Applies the given remote operations to the buffer.
2995    pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I, cx: &mut Context<Self>) {
2996        self.pending_autoindent.take();
2997        let was_dirty = self.is_dirty();
2998        let old_version = self.version.clone();
2999        let mut deferred_ops = Vec::new();
3000        let buffer_ops = ops
3001            .into_iter()
3002            .filter_map(|op| match op {
3003                Operation::Buffer(op) => Some(op),
3004                _ => {
3005                    if self.can_apply_op(&op) {
3006                        self.apply_op(op, cx);
3007                    } else {
3008                        deferred_ops.push(op);
3009                    }
3010                    None
3011                }
3012            })
3013            .collect::<Vec<_>>();
3014        for operation in buffer_ops.iter() {
3015            self.send_operation(Operation::Buffer(operation.clone()), false, cx);
3016        }
3017        self.text.apply_ops(buffer_ops);
3018        self.deferred_ops.insert(deferred_ops);
3019        self.flush_deferred_ops(cx);
3020        self.did_edit(&old_version, was_dirty, false, cx);
3021        // Notify independently of whether the buffer was edited as the operations could include a
3022        // selection update.
3023        cx.notify();
3024    }
3025
3026    fn flush_deferred_ops(&mut self, cx: &mut Context<Self>) {
3027        let mut deferred_ops = Vec::new();
3028        for op in self.deferred_ops.drain().iter().cloned() {
3029            if self.can_apply_op(&op) {
3030                self.apply_op(op, cx);
3031            } else {
3032                deferred_ops.push(op);
3033            }
3034        }
3035        self.deferred_ops.insert(deferred_ops);
3036    }
3037
3038    pub fn has_deferred_ops(&self) -> bool {
3039        !self.deferred_ops.is_empty() || self.text.has_deferred_ops()
3040    }
3041
3042    fn can_apply_op(&self, operation: &Operation) -> bool {
3043        match operation {
3044            Operation::Buffer(_) => {
3045                unreachable!("buffer operations should never be applied at this layer")
3046            }
3047            Operation::UpdateDiagnostics {
3048                diagnostics: diagnostic_set,
3049                ..
3050            } => diagnostic_set.iter().all(|diagnostic| {
3051                self.text.can_resolve(&diagnostic.range.start)
3052                    && self.text.can_resolve(&diagnostic.range.end)
3053            }),
3054            Operation::UpdateSelections { selections, .. } => selections
3055                .iter()
3056                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
3057            Operation::UpdateCompletionTriggers { .. } | Operation::UpdateLineEnding { .. } => true,
3058        }
3059    }
3060
3061    fn apply_op(&mut self, operation: Operation, cx: &mut Context<Self>) {
3062        match operation {
3063            Operation::Buffer(_) => {
3064                unreachable!("buffer operations should never be applied at this layer")
3065            }
3066            Operation::UpdateDiagnostics {
3067                server_id,
3068                diagnostics: diagnostic_set,
3069                lamport_timestamp,
3070            } => {
3071                let snapshot = self.snapshot();
3072                self.apply_diagnostic_update(
3073                    server_id,
3074                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
3075                    lamport_timestamp,
3076                    cx,
3077                );
3078            }
3079            Operation::UpdateSelections {
3080                selections,
3081                lamport_timestamp,
3082                line_mode,
3083                cursor_shape,
3084            } => {
3085                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id)
3086                    && set.lamport_timestamp > lamport_timestamp
3087                {
3088                    return;
3089                }
3090
3091                self.remote_selections.insert(
3092                    lamport_timestamp.replica_id,
3093                    SelectionSet {
3094                        selections,
3095                        lamport_timestamp,
3096                        line_mode,
3097                        cursor_shape,
3098                    },
3099                );
3100                self.text.lamport_clock.observe(lamport_timestamp);
3101                self.non_text_state_update_count += 1;
3102            }
3103            Operation::UpdateCompletionTriggers {
3104                triggers,
3105                lamport_timestamp,
3106                server_id,
3107            } => {
3108                if triggers.is_empty() {
3109                    self.completion_triggers_per_language_server
3110                        .remove(&server_id);
3111                    self.completion_triggers = self
3112                        .completion_triggers_per_language_server
3113                        .values()
3114                        .flat_map(|triggers| triggers.iter().cloned())
3115                        .collect();
3116                } else {
3117                    self.completion_triggers_per_language_server
3118                        .insert(server_id, triggers.iter().cloned().collect());
3119                    self.completion_triggers.extend(triggers);
3120                }
3121                self.text.lamport_clock.observe(lamport_timestamp);
3122            }
3123            Operation::UpdateLineEnding {
3124                line_ending,
3125                lamport_timestamp,
3126            } => {
3127                self.text.set_line_ending(line_ending);
3128                self.text.lamport_clock.observe(lamport_timestamp);
3129            }
3130        }
3131    }
3132
3133    fn apply_diagnostic_update(
3134        &mut self,
3135        server_id: LanguageServerId,
3136        diagnostics: DiagnosticSet,
3137        lamport_timestamp: clock::Lamport,
3138        cx: &mut Context<Self>,
3139    ) {
3140        if lamport_timestamp > self.diagnostics_timestamp {
3141            if diagnostics.is_empty() {
3142                self.diagnostics.remove(&server_id);
3143            } else {
3144                self.diagnostics.insert(server_id, diagnostics);
3145            }
3146            self.diagnostics_timestamp = lamport_timestamp;
3147            self.non_text_state_update_count += 1;
3148            self.text.lamport_clock.observe(lamport_timestamp);
3149            cx.notify();
3150            cx.emit(BufferEvent::DiagnosticsUpdated);
3151        }
3152    }
3153
3154    fn send_operation(&mut self, operation: Operation, is_local: bool, cx: &mut Context<Self>) {
3155        self.was_changed();
3156        cx.emit(BufferEvent::Operation {
3157            operation,
3158            is_local,
3159        });
3160    }
3161
3162    /// Removes the selections for a given peer.
3163    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut Context<Self>) {
3164        self.remote_selections.remove(&replica_id);
3165        cx.notify();
3166    }
3167
3168    /// Undoes the most recent transaction.
3169    pub fn undo(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
3170        let was_dirty = self.is_dirty();
3171        let old_version = self.version.clone();
3172
3173        if let Some((transaction_id, operation)) = self.text.undo() {
3174            self.send_operation(Operation::Buffer(operation), true, cx);
3175            self.did_edit(&old_version, was_dirty, true, cx);
3176            self.restore_encoding_for_transaction(transaction_id, was_dirty);
3177            Some(transaction_id)
3178        } else {
3179            None
3180        }
3181    }
3182
3183    /// Manually undoes a specific transaction in the buffer's undo history.
3184    pub fn undo_transaction(
3185        &mut self,
3186        transaction_id: TransactionId,
3187        cx: &mut Context<Self>,
3188    ) -> bool {
3189        let was_dirty = self.is_dirty();
3190        let old_version = self.version.clone();
3191        if let Some(operation) = self.text.undo_transaction(transaction_id) {
3192            self.send_operation(Operation::Buffer(operation), true, cx);
3193            self.did_edit(&old_version, was_dirty, true, cx);
3194            true
3195        } else {
3196            false
3197        }
3198    }
3199
3200    /// Manually undoes all changes after a given transaction in the buffer's undo history.
3201    pub fn undo_to_transaction(
3202        &mut self,
3203        transaction_id: TransactionId,
3204        cx: &mut Context<Self>,
3205    ) -> bool {
3206        let was_dirty = self.is_dirty();
3207        let old_version = self.version.clone();
3208
3209        let operations = self.text.undo_to_transaction(transaction_id);
3210        let undone = !operations.is_empty();
3211        for operation in operations {
3212            self.send_operation(Operation::Buffer(operation), true, cx);
3213        }
3214        if undone {
3215            self.did_edit(&old_version, was_dirty, true, cx)
3216        }
3217        undone
3218    }
3219
3220    pub fn undo_operations(&mut self, counts: HashMap<Lamport, u32>, cx: &mut Context<Buffer>) {
3221        let was_dirty = self.is_dirty();
3222        let operation = self.text.undo_operations(counts);
3223        let old_version = self.version.clone();
3224        self.send_operation(Operation::Buffer(operation), true, cx);
3225        self.did_edit(&old_version, was_dirty, true, cx);
3226    }
3227
3228    /// Manually redoes a specific transaction in the buffer's redo history.
3229    pub fn redo(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
3230        let was_dirty = self.is_dirty();
3231        let old_version = self.version.clone();
3232
3233        if let Some((transaction_id, operation)) = self.text.redo() {
3234            self.send_operation(Operation::Buffer(operation), true, cx);
3235            self.did_edit(&old_version, was_dirty, true, cx);
3236            self.restore_encoding_for_transaction(transaction_id, was_dirty);
3237            Some(transaction_id)
3238        } else {
3239            None
3240        }
3241    }
3242
3243    fn restore_encoding_for_transaction(&mut self, transaction_id: TransactionId, was_dirty: bool) {
3244        if let Some((old_encoding, old_has_bom)) =
3245            self.reload_with_encoding_txns.get(&transaction_id)
3246        {
3247            let current_encoding = self.encoding;
3248            let current_has_bom = self.has_bom;
3249            self.encoding = *old_encoding;
3250            self.has_bom = *old_has_bom;
3251            if !was_dirty {
3252                self.saved_version = self.version.clone();
3253                self.has_unsaved_edits
3254                    .set((self.saved_version.clone(), false));
3255            }
3256            self.reload_with_encoding_txns
3257                .insert(transaction_id, (current_encoding, current_has_bom));
3258        }
3259    }
3260
3261    /// Manually undoes all changes until a given transaction in the buffer's redo history.
3262    pub fn redo_to_transaction(
3263        &mut self,
3264        transaction_id: TransactionId,
3265        cx: &mut Context<Self>,
3266    ) -> bool {
3267        let was_dirty = self.is_dirty();
3268        let old_version = self.version.clone();
3269
3270        let operations = self.text.redo_to_transaction(transaction_id);
3271        let redone = !operations.is_empty();
3272        for operation in operations {
3273            self.send_operation(Operation::Buffer(operation), true, cx);
3274        }
3275        if redone {
3276            self.did_edit(&old_version, was_dirty, true, cx)
3277        }
3278        redone
3279    }
3280
3281    /// Override current completion triggers with the user-provided completion triggers.
3282    pub fn set_completion_triggers(
3283        &mut self,
3284        server_id: LanguageServerId,
3285        triggers: BTreeSet<String>,
3286        cx: &mut Context<Self>,
3287    ) {
3288        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
3289        if triggers.is_empty() {
3290            self.completion_triggers_per_language_server
3291                .remove(&server_id);
3292            self.completion_triggers = self
3293                .completion_triggers_per_language_server
3294                .values()
3295                .flat_map(|triggers| triggers.iter().cloned())
3296                .collect();
3297        } else {
3298            self.completion_triggers_per_language_server
3299                .insert(server_id, triggers.clone());
3300            self.completion_triggers.extend(triggers.iter().cloned());
3301        }
3302        self.send_operation(
3303            Operation::UpdateCompletionTriggers {
3304                triggers: triggers.into_iter().collect(),
3305                lamport_timestamp: self.completion_triggers_timestamp,
3306                server_id,
3307            },
3308            true,
3309            cx,
3310        );
3311        cx.notify();
3312    }
3313
3314    /// Returns a list of strings which trigger a completion menu for this language.
3315    /// Usually this is driven by LSP server which returns a list of trigger characters for completions.
3316    pub fn completion_triggers(&self) -> &BTreeSet<String> {
3317        &self.completion_triggers
3318    }
3319
3320    /// Call this directly after performing edits to prevent the preview tab
3321    /// from being dismissed by those edits. It causes `should_dismiss_preview`
3322    /// to return false until there are additional edits.
3323    pub fn refresh_preview(&mut self) {
3324        self.preview_version = self.version.clone();
3325    }
3326
3327    /// Whether we should preserve the preview status of a tab containing this buffer.
3328    pub fn preserve_preview(&self) -> bool {
3329        !self.has_edits_since(&self.preview_version)
3330    }
3331}
3332
3333#[doc(hidden)]
3334#[cfg(any(test, feature = "test-support"))]
3335impl Buffer {
3336    pub fn edit_via_marked_text(
3337        &mut self,
3338        marked_string: &str,
3339        autoindent_mode: Option<AutoindentMode>,
3340        cx: &mut Context<Self>,
3341    ) {
3342        let edits = self.edits_for_marked_text(marked_string);
3343        self.edit(edits, autoindent_mode, cx);
3344    }
3345
3346    pub fn set_group_interval(&mut self, group_interval: Duration) {
3347        self.text.set_group_interval(group_interval);
3348    }
3349
3350    pub fn randomly_edit<T>(&mut self, rng: &mut T, old_range_count: usize, cx: &mut Context<Self>)
3351    where
3352        T: rand::Rng,
3353    {
3354        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
3355        let mut last_end = None;
3356        for _ in 0..old_range_count {
3357            if last_end.is_some_and(|last_end| last_end >= self.len()) {
3358                break;
3359            }
3360
3361            let new_start = last_end.map_or(0, |last_end| last_end + 1);
3362            let mut range = self.random_byte_range(new_start, rng);
3363            if rng.random_bool(0.2) {
3364                mem::swap(&mut range.start, &mut range.end);
3365            }
3366            last_end = Some(range.end);
3367
3368            let new_text_len = rng.random_range(0..10);
3369            let mut new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
3370            new_text = new_text.to_uppercase();
3371
3372            edits.push((range, new_text));
3373        }
3374        log::info!("mutating buffer {:?} with {:?}", self.replica_id(), edits);
3375        self.edit(edits, None, cx);
3376    }
3377
3378    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut Context<Self>) {
3379        let was_dirty = self.is_dirty();
3380        let old_version = self.version.clone();
3381
3382        let ops = self.text.randomly_undo_redo(rng);
3383        if !ops.is_empty() {
3384            for op in ops {
3385                self.send_operation(Operation::Buffer(op), true, cx);
3386                self.did_edit(&old_version, was_dirty, true, cx);
3387            }
3388        }
3389    }
3390}
3391
3392impl EventEmitter<BufferEvent> for Buffer {}
3393
3394fn offset_in_sub_ranges(
3395    sub_ranges: &[Range<Anchor>],
3396    offset: usize,
3397    snapshot: &TextBufferSnapshot,
3398) -> bool {
3399    let start_anchor = snapshot.anchor_before(offset);
3400    let end_anchor = snapshot.anchor_after(offset);
3401
3402    sub_ranges.iter().any(|sub_range| {
3403        let is_before_start = sub_range.end.cmp(&start_anchor, snapshot).is_lt();
3404        let is_after_end = sub_range.start.cmp(&end_anchor, snapshot).is_gt();
3405        !is_before_start && !is_after_end
3406    })
3407}
3408
3409impl Deref for Buffer {
3410    type Target = TextBuffer;
3411
3412    fn deref(&self) -> &Self::Target {
3413        &self.text
3414    }
3415}
3416
3417impl BufferSnapshot {
3418    /// Returns [`IndentSize`] for a given line that respects user settings and
3419    /// language preferences.
3420    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
3421        indent_size_for_line(self, row)
3422    }
3423
3424    /// Returns [`IndentSize`] for a given position that respects user settings
3425    /// and language preferences.
3426    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &App) -> IndentSize {
3427        let settings = self.settings_at(position, cx);
3428        if settings.hard_tabs {
3429            IndentSize::tab()
3430        } else {
3431            IndentSize::spaces(settings.tab_size.get())
3432        }
3433    }
3434
3435    /// Retrieve the suggested indent size for all of the given rows. The unit of indentation
3436    /// is passed in as `single_indent_size`.
3437    pub fn suggested_indents(
3438        &self,
3439        rows: impl Iterator<Item = u32>,
3440        single_indent_size: IndentSize,
3441    ) -> BTreeMap<u32, IndentSize> {
3442        let mut result = BTreeMap::new();
3443
3444        for row_range in contiguous_ranges(rows, 10) {
3445            let suggestions = match self.suggest_autoindents(row_range.clone()) {
3446                Some(suggestions) => suggestions,
3447                _ => break,
3448            };
3449
3450            for (row, suggestion) in row_range.zip(suggestions) {
3451                let indent_size = if let Some(suggestion) = suggestion {
3452                    result
3453                        .get(&suggestion.basis_row)
3454                        .copied()
3455                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
3456                        .with_delta(suggestion.delta, single_indent_size)
3457                } else {
3458                    self.indent_size_for_line(row)
3459                };
3460
3461                result.insert(row, indent_size);
3462            }
3463        }
3464
3465        result
3466    }
3467
3468    fn suggest_autoindents(
3469        &self,
3470        row_range: Range<u32>,
3471    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
3472        let config = &self.language.as_ref()?.config;
3473        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
3474
3475        #[derive(Debug, Clone)]
3476        struct StartPosition {
3477            start: Point,
3478            suffix: SharedString,
3479            language: Arc<Language>,
3480        }
3481
3482        // Find the suggested indentation ranges based on the syntax tree.
3483        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
3484        let end = Point::new(row_range.end, 0);
3485        let range = (start..end).to_offset(&self.text);
3486        let mut matches = self.syntax.matches_with_options(
3487            range.clone(),
3488            &self.text,
3489            TreeSitterOptions {
3490                max_bytes_to_query: Some(MAX_BYTES_TO_QUERY),
3491                max_start_depth: None,
3492            },
3493            |grammar| Some(&grammar.indents_config.as_ref()?.query),
3494        );
3495        let indent_configs = matches
3496            .grammars()
3497            .iter()
3498            .map(|grammar| grammar.indents_config.as_ref().unwrap())
3499            .collect::<Vec<_>>();
3500
3501        let mut indent_ranges = Vec::<Range<Point>>::new();
3502        let mut start_positions = Vec::<StartPosition>::new();
3503        let mut outdent_positions = Vec::<Point>::new();
3504        while let Some(mat) = matches.peek() {
3505            let mut start: Option<Point> = None;
3506            let mut end: Option<Point> = None;
3507
3508            let config = indent_configs[mat.grammar_index];
3509            for capture in mat.captures {
3510                if capture.index == config.indent_capture_ix {
3511                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
3512                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
3513                } else if Some(capture.index) == config.start_capture_ix {
3514                    start = Some(Point::from_ts_point(capture.node.end_position()));
3515                } else if Some(capture.index) == config.end_capture_ix {
3516                    end = Some(Point::from_ts_point(capture.node.start_position()));
3517                } else if Some(capture.index) == config.outdent_capture_ix {
3518                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
3519                } else if let Some(suffix) = config.suffixed_start_captures.get(&capture.index) {
3520                    start_positions.push(StartPosition {
3521                        start: Point::from_ts_point(capture.node.start_position()),
3522                        suffix: suffix.clone(),
3523                        language: mat.language.clone(),
3524                    });
3525                }
3526            }
3527
3528            matches.advance();
3529            if let Some((start, end)) = start.zip(end) {
3530                if start.row == end.row {
3531                    continue;
3532                }
3533                let range = start..end;
3534                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
3535                    Err(ix) => indent_ranges.insert(ix, range),
3536                    Ok(ix) => {
3537                        let prev_range = &mut indent_ranges[ix];
3538                        prev_range.end = prev_range.end.max(range.end);
3539                    }
3540                }
3541            }
3542        }
3543
3544        let mut error_ranges = Vec::<Range<Point>>::new();
3545        let mut matches = self
3546            .syntax
3547            .matches(range, &self.text, |grammar| grammar.error_query.as_ref());
3548        while let Some(mat) = matches.peek() {
3549            let node = mat.captures[0].node;
3550            let start = Point::from_ts_point(node.start_position());
3551            let end = Point::from_ts_point(node.end_position());
3552            let range = start..end;
3553            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
3554                Ok(ix) | Err(ix) => ix,
3555            };
3556            let mut end_ix = ix;
3557            while let Some(existing_range) = error_ranges.get(end_ix) {
3558                if existing_range.end < end {
3559                    end_ix += 1;
3560                } else {
3561                    break;
3562                }
3563            }
3564            error_ranges.splice(ix..end_ix, [range]);
3565            matches.advance();
3566        }
3567
3568        outdent_positions.sort();
3569        for outdent_position in outdent_positions {
3570            // find the innermost indent range containing this outdent_position
3571            // set its end to the outdent position
3572            if let Some(range_to_truncate) = indent_ranges
3573                .iter_mut()
3574                .rfind(|indent_range| indent_range.contains(&outdent_position))
3575            {
3576                range_to_truncate.end = outdent_position;
3577            }
3578        }
3579
3580        start_positions.sort_by_key(|b| b.start);
3581
3582        // Find the suggested indentation increases and decreased based on regexes.
3583        let mut regex_outdent_map = HashMap::default();
3584        let mut last_seen_suffix: HashMap<String, Vec<StartPosition>> = HashMap::default();
3585        let mut start_positions_iter = start_positions.iter().peekable();
3586
3587        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
3588        self.for_each_line(
3589            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
3590                ..Point::new(row_range.end, 0),
3591            |row, line| {
3592                let indent_len = self.indent_size_for_line(row).len;
3593                let row_language = self.language_at(Point::new(row, indent_len)).cloned();
3594                let row_language_config = row_language
3595                    .as_ref()
3596                    .map(|lang| lang.config())
3597                    .unwrap_or(config);
3598
3599                if row_language_config
3600                    .decrease_indent_pattern
3601                    .as_ref()
3602                    .is_some_and(|regex| regex.is_match(line))
3603                {
3604                    indent_change_rows.push((row, Ordering::Less));
3605                }
3606                if row_language_config
3607                    .increase_indent_pattern
3608                    .as_ref()
3609                    .is_some_and(|regex| regex.is_match(line))
3610                {
3611                    indent_change_rows.push((row + 1, Ordering::Greater));
3612                }
3613                while let Some(pos) = start_positions_iter.peek() {
3614                    if pos.start.row < row {
3615                        let pos = start_positions_iter.next().unwrap().clone();
3616                        last_seen_suffix
3617                            .entry(pos.suffix.to_string())
3618                            .or_default()
3619                            .push(pos);
3620                    } else {
3621                        break;
3622                    }
3623                }
3624                for rule in &row_language_config.decrease_indent_patterns {
3625                    if rule.pattern.as_ref().is_some_and(|r| r.is_match(line)) {
3626                        let row_start_column = self.indent_size_for_line(row).len;
3627                        let basis_row = rule
3628                            .valid_after
3629                            .iter()
3630                            .filter_map(|valid_suffix| last_seen_suffix.get(valid_suffix))
3631                            .flatten()
3632                            .filter(|pos| {
3633                                row_language
3634                                    .as_ref()
3635                                    .or(self.language.as_ref())
3636                                    .is_some_and(|lang| Arc::ptr_eq(lang, &pos.language))
3637                            })
3638                            .filter(|pos| pos.start.column <= row_start_column)
3639                            .max_by_key(|pos| pos.start.row);
3640                        if let Some(outdent_to) = basis_row {
3641                            regex_outdent_map.insert(row, outdent_to.start.row);
3642                        }
3643                        break;
3644                    }
3645                }
3646            },
3647        );
3648
3649        let mut indent_changes = indent_change_rows.into_iter().peekable();
3650        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
3651            prev_non_blank_row.unwrap_or(0)
3652        } else {
3653            row_range.start.saturating_sub(1)
3654        };
3655
3656        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
3657        Some(row_range.map(move |row| {
3658            let row_start = Point::new(row, self.indent_size_for_line(row).len);
3659
3660            let mut indent_from_prev_row = false;
3661            let mut outdent_from_prev_row = false;
3662            let mut outdent_to_row = u32::MAX;
3663            let mut from_regex = false;
3664
3665            while let Some((indent_row, delta)) = indent_changes.peek() {
3666                match indent_row.cmp(&row) {
3667                    Ordering::Equal => match delta {
3668                        Ordering::Less => {
3669                            from_regex = true;
3670                            outdent_from_prev_row = true
3671                        }
3672                        Ordering::Greater => {
3673                            indent_from_prev_row = true;
3674                            from_regex = true
3675                        }
3676                        _ => {}
3677                    },
3678
3679                    Ordering::Greater => break,
3680                    Ordering::Less => {}
3681                }
3682
3683                indent_changes.next();
3684            }
3685
3686            for range in &indent_ranges {
3687                if range.start.row >= row {
3688                    break;
3689                }
3690                if range.start.row == prev_row && range.end > row_start {
3691                    indent_from_prev_row = true;
3692                }
3693                if range.end > prev_row_start && range.end <= row_start {
3694                    outdent_to_row = outdent_to_row.min(range.start.row);
3695                }
3696            }
3697
3698            if let Some(basis_row) = regex_outdent_map.get(&row) {
3699                indent_from_prev_row = false;
3700                outdent_to_row = *basis_row;
3701                from_regex = true;
3702            }
3703
3704            let within_error = error_ranges
3705                .iter()
3706                .any(|e| e.start.row < row && e.end > row_start);
3707
3708            let suggestion = if outdent_to_row == prev_row
3709                || (outdent_from_prev_row && indent_from_prev_row)
3710            {
3711                Some(IndentSuggestion {
3712                    basis_row: prev_row,
3713                    delta: Ordering::Equal,
3714                    within_error: within_error && !from_regex,
3715                })
3716            } else if indent_from_prev_row {
3717                Some(IndentSuggestion {
3718                    basis_row: prev_row,
3719                    delta: Ordering::Greater,
3720                    within_error: within_error && !from_regex,
3721                })
3722            } else if outdent_to_row < prev_row {
3723                Some(IndentSuggestion {
3724                    basis_row: outdent_to_row,
3725                    delta: Ordering::Equal,
3726                    within_error: within_error && !from_regex,
3727                })
3728            } else if outdent_from_prev_row {
3729                Some(IndentSuggestion {
3730                    basis_row: prev_row,
3731                    delta: Ordering::Less,
3732                    within_error: within_error && !from_regex,
3733                })
3734            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
3735            {
3736                Some(IndentSuggestion {
3737                    basis_row: prev_row,
3738                    delta: Ordering::Equal,
3739                    within_error: within_error && !from_regex,
3740                })
3741            } else {
3742                None
3743            };
3744
3745            prev_row = row;
3746            prev_row_start = row_start;
3747            suggestion
3748        }))
3749    }
3750
3751    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
3752        while row > 0 {
3753            row -= 1;
3754            if !self.is_line_blank(row) {
3755                return Some(row);
3756            }
3757        }
3758        None
3759    }
3760
3761    pub fn captures(
3762        &self,
3763        range: Range<usize>,
3764        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
3765    ) -> SyntaxMapCaptures<'_> {
3766        self.syntax.captures(range, &self.text, query)
3767    }
3768
3769    #[ztracing::instrument(skip_all)]
3770    fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures<'_>, Vec<HighlightMap>) {
3771        let captures = self.syntax.captures(range, &self.text, |grammar| {
3772            grammar
3773                .highlights_config
3774                .as_ref()
3775                .map(|config| &config.query)
3776        });
3777        let highlight_maps = captures
3778            .grammars()
3779            .iter()
3780            .map(|grammar| grammar.highlight_map())
3781            .collect();
3782        (captures, highlight_maps)
3783    }
3784
3785    /// Iterates over chunks of text in the given range of the buffer. Text is chunked
3786    /// in an arbitrary way due to being stored in a [`Rope`](text::Rope). The text is also
3787    /// returned in chunks where each chunk has a single syntax highlighting style and
3788    /// diagnostic status.
3789    #[ztracing::instrument(skip_all)]
3790    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks<'_> {
3791        let range = range.start.to_offset(self)..range.end.to_offset(self);
3792
3793        let mut syntax = None;
3794        if language_aware {
3795            syntax = Some(self.get_highlights(range.clone()));
3796        }
3797        // We want to look at diagnostic spans only when iterating over language-annotated chunks.
3798        let diagnostics = language_aware;
3799        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostics, Some(self))
3800    }
3801
3802    pub fn highlighted_text_for_range<T: ToOffset>(
3803        &self,
3804        range: Range<T>,
3805        override_style: Option<HighlightStyle>,
3806        syntax_theme: &SyntaxTheme,
3807    ) -> HighlightedText {
3808        HighlightedText::from_buffer_range(
3809            range,
3810            &self.text,
3811            &self.syntax,
3812            override_style,
3813            syntax_theme,
3814        )
3815    }
3816
3817    /// Invokes the given callback for each line of text in the given range of the buffer.
3818    /// Uses callback to avoid allocating a string for each line.
3819    fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
3820        let mut line = String::new();
3821        let mut row = range.start.row;
3822        for chunk in self
3823            .as_rope()
3824            .chunks_in_range(range.to_offset(self))
3825            .chain(["\n"])
3826        {
3827            for (newline_ix, text) in chunk.split('\n').enumerate() {
3828                if newline_ix > 0 {
3829                    callback(row, &line);
3830                    row += 1;
3831                    line.clear();
3832                }
3833                line.push_str(text);
3834            }
3835        }
3836    }
3837
3838    /// Iterates over every [`SyntaxLayer`] in the buffer.
3839    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayer<'_>> + '_ {
3840        self.syntax_layers_for_range(0..self.len(), true)
3841    }
3842
3843    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayer<'_>> {
3844        let offset = position.to_offset(self);
3845        self.syntax_layers_for_range(offset..offset, false)
3846            .filter(|l| {
3847                if let Some(ranges) = l.included_sub_ranges {
3848                    ranges.iter().any(|range| {
3849                        let start = range.start.to_offset(self);
3850                        start <= offset && {
3851                            let end = range.end.to_offset(self);
3852                            offset < end
3853                        }
3854                    })
3855                } else {
3856                    l.node().start_byte() <= offset && l.node().end_byte() > offset
3857                }
3858            })
3859            .last()
3860    }
3861
3862    pub fn syntax_layers_for_range<D: ToOffset>(
3863        &self,
3864        range: Range<D>,
3865        include_hidden: bool,
3866    ) -> impl Iterator<Item = SyntaxLayer<'_>> + '_ {
3867        self.syntax
3868            .layers_for_range(range, &self.text, include_hidden)
3869    }
3870
3871    pub fn syntax_layers_languages(&self) -> impl Iterator<Item = &Arc<Language>> {
3872        self.syntax.languages(&self, true)
3873    }
3874
3875    pub fn smallest_syntax_layer_containing<D: ToOffset>(
3876        &self,
3877        range: Range<D>,
3878    ) -> Option<SyntaxLayer<'_>> {
3879        let range = range.to_offset(self);
3880        self.syntax
3881            .layers_for_range(range, &self.text, false)
3882            .max_by(|a, b| {
3883                if a.depth != b.depth {
3884                    a.depth.cmp(&b.depth)
3885                } else if a.offset.0 != b.offset.0 {
3886                    a.offset.0.cmp(&b.offset.0)
3887                } else {
3888                    a.node().end_byte().cmp(&b.node().end_byte()).reverse()
3889                }
3890            })
3891    }
3892
3893    /// Returns the [`ModelineSettings`].
3894    pub fn modeline(&self) -> Option<&Arc<ModelineSettings>> {
3895        self.modeline.as_ref()
3896    }
3897
3898    /// Returns the main [`Language`].
3899    pub fn language(&self) -> Option<&Arc<Language>> {
3900        self.language.as_ref()
3901    }
3902
3903    /// Returns the [`Language`] at the given location.
3904    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
3905        self.syntax_layer_at(position)
3906            .map(|info| info.language)
3907            .or(self.language.as_ref())
3908    }
3909
3910    /// Returns the settings for the language at the given location.
3911    pub fn settings_at<'a, D: ToOffset>(
3912        &'a self,
3913        position: D,
3914        cx: &'a App,
3915    ) -> Cow<'a, LanguageSettings> {
3916        LanguageSettings::for_buffer_snapshot(self, Some(position.to_offset(self)), cx)
3917    }
3918
3919    pub fn char_classifier_at<T: ToOffset>(&self, point: T) -> CharClassifier {
3920        CharClassifier::new(self.language_scope_at(point))
3921    }
3922
3923    /// Returns the [`LanguageScope`] at the given location.
3924    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
3925        let offset = position.to_offset(self);
3926        let mut scope = None;
3927        let mut smallest_range_and_depth: Option<(Range<usize>, usize)> = None;
3928        let text: &TextBufferSnapshot = self;
3929
3930        // Use the layer that has the smallest node intersecting the given point.
3931        for layer in self
3932            .syntax
3933            .layers_for_range(offset..offset, &self.text, false)
3934        {
3935            if let Some(ranges) = layer.included_sub_ranges
3936                && !offset_in_sub_ranges(ranges, offset, text)
3937            {
3938                continue;
3939            }
3940
3941            let mut cursor = layer.node().walk();
3942
3943            let mut range = None;
3944            loop {
3945                let child_range = cursor.node().byte_range();
3946                if !child_range.contains(&offset) {
3947                    break;
3948                }
3949
3950                range = Some(child_range);
3951                if cursor.goto_first_child_for_byte(offset).is_none() {
3952                    break;
3953                }
3954            }
3955
3956            if let Some(range) = range
3957                && smallest_range_and_depth.as_ref().is_none_or(
3958                    |(smallest_range, smallest_range_depth)| {
3959                        if layer.depth > *smallest_range_depth {
3960                            true
3961                        } else if layer.depth == *smallest_range_depth {
3962                            range.len() < smallest_range.len()
3963                        } else {
3964                            false
3965                        }
3966                    },
3967                )
3968            {
3969                smallest_range_and_depth = Some((range, layer.depth));
3970                scope = Some(LanguageScope {
3971                    language: layer.language.clone(),
3972                    override_id: layer.override_id(offset, &self.text),
3973                });
3974            }
3975        }
3976
3977        scope.or_else(|| {
3978            self.language.clone().map(|language| LanguageScope {
3979                language,
3980                override_id: None,
3981            })
3982        })
3983    }
3984
3985    /// Returns a tuple of the range and character kind of the word
3986    /// surrounding the given position.
3987    pub fn surrounding_word<T: ToOffset>(
3988        &self,
3989        start: T,
3990        scope_context: Option<CharScopeContext>,
3991    ) -> (Range<usize>, Option<CharKind>) {
3992        let mut start = start.to_offset(self);
3993        let mut end = start;
3994        let mut next_chars = self.chars_at(start).take(128).peekable();
3995        let mut prev_chars = self.reversed_chars_at(start).take(128).peekable();
3996
3997        let classifier = self.char_classifier_at(start).scope_context(scope_context);
3998        let word_kind = cmp::max(
3999            prev_chars.peek().copied().map(|c| classifier.kind(c)),
4000            next_chars.peek().copied().map(|c| classifier.kind(c)),
4001        );
4002
4003        for ch in prev_chars {
4004            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
4005                start -= ch.len_utf8();
4006            } else {
4007                break;
4008            }
4009        }
4010
4011        for ch in next_chars {
4012            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
4013                end += ch.len_utf8();
4014            } else {
4015                break;
4016            }
4017        }
4018
4019        (start..end, word_kind)
4020    }
4021
4022    /// Moves the TreeCursor to the smallest descendant or ancestor syntax node enclosing the given
4023    /// range. When `require_larger` is true, the node found must be larger than the query range.
4024    ///
4025    /// Returns true if a node was found, and false otherwise. In the `false` case the cursor will
4026    /// be moved to the root of the tree.
4027    fn goto_node_enclosing_range(
4028        cursor: &mut tree_sitter::TreeCursor,
4029        query_range: &Range<usize>,
4030        require_larger: bool,
4031    ) -> bool {
4032        let mut ascending = false;
4033        loop {
4034            let mut range = cursor.node().byte_range();
4035            if query_range.is_empty() {
4036                // When the query range is empty and the current node starts after it, move to the
4037                // previous sibling to find the node the containing node.
4038                if range.start > query_range.start {
4039                    cursor.goto_previous_sibling();
4040                    range = cursor.node().byte_range();
4041                }
4042            } else {
4043                // When the query range is non-empty and the current node ends exactly at the start,
4044                // move to the next sibling to find a node that extends beyond the start.
4045                if range.end == query_range.start {
4046                    cursor.goto_next_sibling();
4047                    range = cursor.node().byte_range();
4048                }
4049            }
4050
4051            let encloses = range.contains_inclusive(query_range)
4052                && (!require_larger || range.len() > query_range.len());
4053            if !encloses {
4054                ascending = true;
4055                if !cursor.goto_parent() {
4056                    return false;
4057                }
4058                continue;
4059            } else if ascending {
4060                return true;
4061            }
4062
4063            // Descend into the current node.
4064            if cursor
4065                .goto_first_child_for_byte(query_range.start)
4066                .is_none()
4067            {
4068                return true;
4069            }
4070        }
4071    }
4072
4073    pub fn syntax_ancestor<'a, T: ToOffset>(
4074        &'a self,
4075        range: Range<T>,
4076    ) -> Option<tree_sitter::Node<'a>> {
4077        let range = range.start.to_offset(self)..range.end.to_offset(self);
4078        let mut result: Option<tree_sitter::Node<'a>> = None;
4079        for layer in self
4080            .syntax
4081            .layers_for_range(range.clone(), &self.text, true)
4082        {
4083            let mut cursor = layer.node().walk();
4084
4085            // Find the node that both contains the range and is larger than it.
4086            if !Self::goto_node_enclosing_range(&mut cursor, &range, true) {
4087                continue;
4088            }
4089
4090            let left_node = cursor.node();
4091            let mut layer_result = left_node;
4092
4093            // For an empty range, try to find another node immediately to the right of the range.
4094            if left_node.end_byte() == range.start {
4095                let mut right_node = None;
4096                while !cursor.goto_next_sibling() {
4097                    if !cursor.goto_parent() {
4098                        break;
4099                    }
4100                }
4101
4102                while cursor.node().start_byte() == range.start {
4103                    right_node = Some(cursor.node());
4104                    if !cursor.goto_first_child() {
4105                        break;
4106                    }
4107                }
4108
4109                // If there is a candidate node on both sides of the (empty) range, then
4110                // decide between the two by favoring a named node over an anonymous token.
4111                // If both nodes are the same in that regard, favor the right one.
4112                if let Some(right_node) = right_node
4113                    && (right_node.is_named() || !left_node.is_named())
4114                {
4115                    layer_result = right_node;
4116                }
4117            }
4118
4119            if let Some(previous_result) = &result
4120                && previous_result.byte_range().len() < layer_result.byte_range().len()
4121            {
4122                continue;
4123            }
4124            result = Some(layer_result);
4125        }
4126
4127        result
4128    }
4129
4130    /// Find the previous sibling syntax node at the given range.
4131    ///
4132    /// This function locates the syntax node that precedes the node containing
4133    /// the given range. It searches hierarchically by:
4134    /// 1. Finding the node that contains the given range
4135    /// 2. Looking for the previous sibling at the same tree level
4136    /// 3. If no sibling is found, moving up to parent levels and searching for siblings
4137    ///
4138    /// Returns `None` if there is no previous sibling at any ancestor level.
4139    pub fn syntax_prev_sibling<'a, T: ToOffset>(
4140        &'a self,
4141        range: Range<T>,
4142    ) -> Option<tree_sitter::Node<'a>> {
4143        let range = range.start.to_offset(self)..range.end.to_offset(self);
4144        let mut result: Option<tree_sitter::Node<'a>> = None;
4145
4146        for layer in self
4147            .syntax
4148            .layers_for_range(range.clone(), &self.text, true)
4149        {
4150            let mut cursor = layer.node().walk();
4151
4152            // Find the node that contains the range
4153            if !Self::goto_node_enclosing_range(&mut cursor, &range, false) {
4154                continue;
4155            }
4156
4157            // Look for the previous sibling, moving up ancestor levels if needed
4158            loop {
4159                if cursor.goto_previous_sibling() {
4160                    let layer_result = cursor.node();
4161
4162                    if let Some(previous_result) = &result {
4163                        if previous_result.byte_range().end < layer_result.byte_range().end {
4164                            continue;
4165                        }
4166                    }
4167                    result = Some(layer_result);
4168                    break;
4169                }
4170
4171                // No sibling found at this level, try moving up to parent
4172                if !cursor.goto_parent() {
4173                    break;
4174                }
4175            }
4176        }
4177
4178        result
4179    }
4180
4181    /// Find the next sibling syntax node at the given range.
4182    ///
4183    /// This function locates the syntax node that follows the node containing
4184    /// the given range. It searches hierarchically by:
4185    /// 1. Finding the node that contains the given range
4186    /// 2. Looking for the next sibling at the same tree level
4187    /// 3. If no sibling is found, moving up to parent levels and searching for siblings
4188    ///
4189    /// Returns `None` if there is no next sibling at any ancestor level.
4190    pub fn syntax_next_sibling<'a, T: ToOffset>(
4191        &'a self,
4192        range: Range<T>,
4193    ) -> Option<tree_sitter::Node<'a>> {
4194        let range = range.start.to_offset(self)..range.end.to_offset(self);
4195        let mut result: Option<tree_sitter::Node<'a>> = None;
4196
4197        for layer in self
4198            .syntax
4199            .layers_for_range(range.clone(), &self.text, true)
4200        {
4201            let mut cursor = layer.node().walk();
4202
4203            // Find the node that contains the range
4204            if !Self::goto_node_enclosing_range(&mut cursor, &range, false) {
4205                continue;
4206            }
4207
4208            // Look for the next sibling, moving up ancestor levels if needed
4209            loop {
4210                if cursor.goto_next_sibling() {
4211                    let layer_result = cursor.node();
4212
4213                    if let Some(previous_result) = &result {
4214                        if previous_result.byte_range().start > layer_result.byte_range().start {
4215                            continue;
4216                        }
4217                    }
4218                    result = Some(layer_result);
4219                    break;
4220                }
4221
4222                // No sibling found at this level, try moving up to parent
4223                if !cursor.goto_parent() {
4224                    break;
4225                }
4226            }
4227        }
4228
4229        result
4230    }
4231
4232    /// Returns the root syntax node within the given row
4233    pub fn syntax_root_ancestor(&self, position: Anchor) -> Option<tree_sitter::Node<'_>> {
4234        let start_offset = position.to_offset(self);
4235
4236        let row = self.summary_for_anchor::<text::PointUtf16>(&position).row as usize;
4237
4238        let layer = self
4239            .syntax
4240            .layers_for_range(start_offset..start_offset, &self.text, true)
4241            .next()?;
4242
4243        let mut cursor = layer.node().walk();
4244
4245        // Descend to the first leaf that touches the start of the range.
4246        while cursor.goto_first_child_for_byte(start_offset).is_some() {
4247            if cursor.node().end_byte() == start_offset {
4248                cursor.goto_next_sibling();
4249            }
4250        }
4251
4252        // Ascend to the root node within the same row.
4253        while cursor.goto_parent() {
4254            if cursor.node().start_position().row != row {
4255                break;
4256            }
4257        }
4258
4259        Some(cursor.node())
4260    }
4261
4262    /// Returns the outline for the buffer.
4263    ///
4264    /// This method allows passing an optional [`SyntaxTheme`] to
4265    /// syntax-highlight the returned symbols.
4266    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Outline<Anchor> {
4267        Outline::new(self.outline_items_containing(0..self.len(), true, theme))
4268    }
4269
4270    /// Returns all the symbols that contain the given position.
4271    ///
4272    /// This method allows passing an optional [`SyntaxTheme`] to
4273    /// syntax-highlight the returned symbols.
4274    pub fn symbols_containing<T: ToOffset>(
4275        &self,
4276        position: T,
4277        theme: Option<&SyntaxTheme>,
4278    ) -> Vec<OutlineItem<Anchor>> {
4279        let position = position.to_offset(self);
4280        let start = self.clip_offset(position.saturating_sub(1), Bias::Left);
4281        let end = self.clip_offset(position + 1, Bias::Right);
4282        let mut items = self.outline_items_containing(start..end, false, theme);
4283        let mut prev_depth = None;
4284        items.retain(|item| {
4285            let result = prev_depth.is_none_or(|prev_depth| item.depth > prev_depth);
4286            prev_depth = Some(item.depth);
4287            result
4288        });
4289        items
4290    }
4291
4292    pub fn outline_range_containing<T: ToOffset>(&self, range: Range<T>) -> Option<Range<Point>> {
4293        let range = range.to_offset(self);
4294        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
4295            grammar.outline_config.as_ref().map(|c| &c.query)
4296        });
4297        let configs = matches
4298            .grammars()
4299            .iter()
4300            .map(|g| g.outline_config.as_ref().unwrap())
4301            .collect::<Vec<_>>();
4302
4303        while let Some(mat) = matches.peek() {
4304            let config = &configs[mat.grammar_index];
4305            let containing_item_node = maybe!({
4306                let item_node = mat.captures.iter().find_map(|cap| {
4307                    if cap.index == config.item_capture_ix {
4308                        Some(cap.node)
4309                    } else {
4310                        None
4311                    }
4312                })?;
4313
4314                let item_byte_range = item_node.byte_range();
4315                if item_byte_range.end < range.start || item_byte_range.start > range.end {
4316                    None
4317                } else {
4318                    Some(item_node)
4319                }
4320            });
4321
4322            if let Some(item_node) = containing_item_node {
4323                return Some(
4324                    Point::from_ts_point(item_node.start_position())
4325                        ..Point::from_ts_point(item_node.end_position()),
4326                );
4327            }
4328
4329            matches.advance();
4330        }
4331        None
4332    }
4333
4334    pub fn outline_items_containing<T: ToOffset>(
4335        &self,
4336        range: Range<T>,
4337        include_extra_context: bool,
4338        theme: Option<&SyntaxTheme>,
4339    ) -> Vec<OutlineItem<Anchor>> {
4340        self.outline_items_containing_internal(
4341            range,
4342            include_extra_context,
4343            theme,
4344            |this, range| this.anchor_after(range.start)..this.anchor_before(range.end),
4345        )
4346    }
4347
4348    pub fn outline_items_as_points_containing<T: ToOffset>(
4349        &self,
4350        range: Range<T>,
4351        include_extra_context: bool,
4352        theme: Option<&SyntaxTheme>,
4353    ) -> Vec<OutlineItem<Point>> {
4354        self.outline_items_containing_internal(range, include_extra_context, theme, |_, range| {
4355            range
4356        })
4357    }
4358
4359    pub fn outline_items_as_offsets_containing<T: ToOffset>(
4360        &self,
4361        range: Range<T>,
4362        include_extra_context: bool,
4363        theme: Option<&SyntaxTheme>,
4364    ) -> Vec<OutlineItem<usize>> {
4365        self.outline_items_containing_internal(
4366            range,
4367            include_extra_context,
4368            theme,
4369            |buffer, range| range.to_offset(buffer),
4370        )
4371    }
4372
4373    fn outline_items_containing_internal<T: ToOffset, U>(
4374        &self,
4375        range: Range<T>,
4376        include_extra_context: bool,
4377        theme: Option<&SyntaxTheme>,
4378        range_callback: fn(&Self, Range<Point>) -> Range<U>,
4379    ) -> Vec<OutlineItem<U>> {
4380        let range = range.to_offset(self);
4381        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
4382            grammar.outline_config.as_ref().map(|c| &c.query)
4383        });
4384
4385        let mut items = Vec::new();
4386        let mut annotation_row_ranges: Vec<Range<u32>> = Vec::new();
4387        while let Some(mat) = matches.peek() {
4388            let config = matches.grammars()[mat.grammar_index]
4389                .outline_config
4390                .as_ref()
4391                .unwrap();
4392            if let Some(item) =
4393                self.next_outline_item(config, &mat, &range, include_extra_context, theme)
4394            {
4395                items.push(item);
4396            } else if let Some(capture) = mat
4397                .captures
4398                .iter()
4399                .find(|capture| Some(capture.index) == config.annotation_capture_ix)
4400            {
4401                let capture_range = capture.node.start_position()..capture.node.end_position();
4402                let mut capture_row_range =
4403                    capture_range.start.row as u32..capture_range.end.row as u32;
4404                if capture_range.end.row > capture_range.start.row && capture_range.end.column == 0
4405                {
4406                    capture_row_range.end -= 1;
4407                }
4408                if let Some(last_row_range) = annotation_row_ranges.last_mut() {
4409                    if last_row_range.end >= capture_row_range.start.saturating_sub(1) {
4410                        last_row_range.end = capture_row_range.end;
4411                    } else {
4412                        annotation_row_ranges.push(capture_row_range);
4413                    }
4414                } else {
4415                    annotation_row_ranges.push(capture_row_range);
4416                }
4417            }
4418            matches.advance();
4419        }
4420
4421        items.sort_by_key(|item| (item.range.start, Reverse(item.range.end)));
4422
4423        // Assign depths based on containment relationships and convert to anchors.
4424        let mut item_ends_stack = Vec::<Point>::new();
4425        let mut anchor_items = Vec::new();
4426        let mut annotation_row_ranges = annotation_row_ranges.into_iter().peekable();
4427        for item in items {
4428            while let Some(last_end) = item_ends_stack.last().copied() {
4429                if last_end < item.range.end {
4430                    item_ends_stack.pop();
4431                } else {
4432                    break;
4433                }
4434            }
4435
4436            let mut annotation_row_range = None;
4437            while let Some(next_annotation_row_range) = annotation_row_ranges.peek() {
4438                let row_preceding_item = item.range.start.row.saturating_sub(1);
4439                if next_annotation_row_range.end < row_preceding_item {
4440                    annotation_row_ranges.next();
4441                } else {
4442                    if next_annotation_row_range.end == row_preceding_item {
4443                        annotation_row_range = Some(next_annotation_row_range.clone());
4444                        annotation_row_ranges.next();
4445                    }
4446                    break;
4447                }
4448            }
4449
4450            anchor_items.push(OutlineItem {
4451                depth: item_ends_stack.len(),
4452                range: range_callback(self, item.range.clone()),
4453                source_range_for_text: range_callback(self, item.source_range_for_text.clone()),
4454                text: item.text,
4455                highlight_ranges: item.highlight_ranges,
4456                name_ranges: item.name_ranges,
4457                body_range: item.body_range.map(|r| range_callback(self, r)),
4458                annotation_range: annotation_row_range.map(|annotation_range| {
4459                    let point_range = Point::new(annotation_range.start, 0)
4460                        ..Point::new(annotation_range.end, self.line_len(annotation_range.end));
4461                    range_callback(self, point_range)
4462                }),
4463            });
4464            item_ends_stack.push(item.range.end);
4465        }
4466
4467        anchor_items
4468    }
4469
4470    fn next_outline_item(
4471        &self,
4472        config: &OutlineConfig,
4473        mat: &SyntaxMapMatch,
4474        range: &Range<usize>,
4475        include_extra_context: bool,
4476        theme: Option<&SyntaxTheme>,
4477    ) -> Option<OutlineItem<Point>> {
4478        let item_node = mat.captures.iter().find_map(|cap| {
4479            if cap.index == config.item_capture_ix {
4480                Some(cap.node)
4481            } else {
4482                None
4483            }
4484        })?;
4485
4486        let item_byte_range = item_node.byte_range();
4487        if item_byte_range.end < range.start || item_byte_range.start > range.end {
4488            return None;
4489        }
4490        let item_point_range = Point::from_ts_point(item_node.start_position())
4491            ..Point::from_ts_point(item_node.end_position());
4492
4493        let mut open_point = None;
4494        let mut close_point = None;
4495
4496        let mut buffer_ranges = Vec::new();
4497        let mut add_to_buffer_ranges = |node: tree_sitter::Node, node_is_name| {
4498            let mut range = node.start_byte()..node.end_byte();
4499            let start = node.start_position();
4500            if node.end_position().row > start.row {
4501                range.end = range.start + self.line_len(start.row as u32) as usize - start.column;
4502            }
4503
4504            if !range.is_empty() {
4505                buffer_ranges.push((range, node_is_name));
4506            }
4507        };
4508
4509        for capture in mat.captures {
4510            if capture.index == config.name_capture_ix {
4511                add_to_buffer_ranges(capture.node, true);
4512            } else if Some(capture.index) == config.context_capture_ix
4513                || (Some(capture.index) == config.extra_context_capture_ix && include_extra_context)
4514            {
4515                add_to_buffer_ranges(capture.node, false);
4516            } else {
4517                if Some(capture.index) == config.open_capture_ix {
4518                    open_point = Some(Point::from_ts_point(capture.node.end_position()));
4519                } else if Some(capture.index) == config.close_capture_ix {
4520                    close_point = Some(Point::from_ts_point(capture.node.start_position()));
4521                }
4522            }
4523        }
4524
4525        if buffer_ranges.is_empty() {
4526            return None;
4527        }
4528        let source_range_for_text =
4529            buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end;
4530
4531        let mut text = String::new();
4532        let mut highlight_ranges = Vec::new();
4533        let mut name_ranges = Vec::new();
4534        let mut chunks = self.chunks(source_range_for_text.clone(), true);
4535        let mut last_buffer_range_end = 0;
4536        for (buffer_range, is_name) in buffer_ranges {
4537            let space_added = !text.is_empty() && buffer_range.start > last_buffer_range_end;
4538            if space_added {
4539                text.push(' ');
4540            }
4541            let before_append_len = text.len();
4542            let mut offset = buffer_range.start;
4543            chunks.seek(buffer_range.clone());
4544            for mut chunk in chunks.by_ref() {
4545                if chunk.text.len() > buffer_range.end - offset {
4546                    chunk.text = &chunk.text[0..(buffer_range.end - offset)];
4547                    offset = buffer_range.end;
4548                } else {
4549                    offset += chunk.text.len();
4550                }
4551                let style = chunk
4552                    .syntax_highlight_id
4553                    .zip(theme)
4554                    .and_then(|(highlight, theme)| highlight.style(theme));
4555                if let Some(style) = style {
4556                    let start = text.len();
4557                    let end = start + chunk.text.len();
4558                    highlight_ranges.push((start..end, style));
4559                }
4560                text.push_str(chunk.text);
4561                if offset >= buffer_range.end {
4562                    break;
4563                }
4564            }
4565            if is_name {
4566                let after_append_len = text.len();
4567                let start = if space_added && !name_ranges.is_empty() {
4568                    before_append_len - 1
4569                } else {
4570                    before_append_len
4571                };
4572                name_ranges.push(start..after_append_len);
4573            }
4574            last_buffer_range_end = buffer_range.end;
4575        }
4576
4577        Some(OutlineItem {
4578            depth: 0, // We'll calculate the depth later
4579            range: item_point_range,
4580            source_range_for_text: source_range_for_text.to_point(self),
4581            text,
4582            highlight_ranges,
4583            name_ranges,
4584            body_range: open_point.zip(close_point).map(|(start, end)| start..end),
4585            annotation_range: None,
4586        })
4587    }
4588
4589    pub fn function_body_fold_ranges<T: ToOffset>(
4590        &self,
4591        within: Range<T>,
4592    ) -> impl Iterator<Item = Range<usize>> + '_ {
4593        self.text_object_ranges(within, TreeSitterOptions::default())
4594            .filter_map(|(range, obj)| (obj == TextObject::InsideFunction).then_some(range))
4595    }
4596
4597    /// For each grammar in the language, runs the provided
4598    /// [`tree_sitter::Query`] against the given range.
4599    pub fn matches(
4600        &self,
4601        range: Range<usize>,
4602        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
4603    ) -> SyntaxMapMatches<'_> {
4604        self.syntax.matches(range, self, query)
4605    }
4606
4607    /// Finds all [`RowChunks`] applicable to the given range, then returns all bracket pairs that intersect with those chunks.
4608    /// Hence, may return more bracket pairs than the range contains.
4609    ///
4610    /// Will omit known chunks.
4611    /// The resulting bracket match collections are not ordered.
4612    pub fn fetch_bracket_ranges(
4613        &self,
4614        range: Range<usize>,
4615        known_chunks: Option<&HashSet<Range<BufferRow>>>,
4616    ) -> HashMap<Range<BufferRow>, Vec<BracketMatch<usize>>> {
4617        let mut all_bracket_matches = HashMap::default();
4618
4619        for chunk in self
4620            .tree_sitter_data
4621            .chunks
4622            .applicable_chunks(&[range.to_point(self)])
4623        {
4624            if known_chunks.is_some_and(|chunks| chunks.contains(&chunk.row_range())) {
4625                continue;
4626            }
4627            let chunk_range = chunk.anchor_range();
4628            let chunk_range = chunk_range.to_offset(&self);
4629
4630            if let Some(cached_brackets) =
4631                &self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]
4632            {
4633                all_bracket_matches.insert(chunk.row_range(), cached_brackets.clone());
4634                continue;
4635            }
4636
4637            let mut all_brackets: Vec<(BracketMatch<usize>, bool)> = Vec::new();
4638            let mut opens = Vec::new();
4639            let mut color_pairs = Vec::new();
4640
4641            let mut matches = self.syntax.matches_with_options(
4642                chunk_range.clone(),
4643                &self.text,
4644                TreeSitterOptions {
4645                    max_bytes_to_query: Some(MAX_BYTES_TO_QUERY),
4646                    max_start_depth: None,
4647                },
4648                |grammar| grammar.brackets_config.as_ref().map(|c| &c.query),
4649            );
4650            let configs = matches
4651                .grammars()
4652                .iter()
4653                .map(|grammar| grammar.brackets_config.as_ref().unwrap())
4654                .collect::<Vec<_>>();
4655
4656            // Group matches by open range so we can either trust grammar output
4657            // or repair it by picking a single closest close per open.
4658            let mut open_to_close_ranges = BTreeMap::new();
4659            while let Some(mat) = matches.peek() {
4660                let mut open = None;
4661                let mut close = None;
4662                let syntax_layer_depth = mat.depth;
4663                let config = configs[mat.grammar_index];
4664                let pattern = &config.patterns[mat.pattern_index];
4665                for capture in mat.captures {
4666                    if capture.index == config.open_capture_ix {
4667                        open = Some(capture.node.byte_range());
4668                    } else if capture.index == config.close_capture_ix {
4669                        close = Some(capture.node.byte_range());
4670                    }
4671                }
4672
4673                matches.advance();
4674
4675                let Some((open_range, close_range)) = open.zip(close) else {
4676                    continue;
4677                };
4678
4679                let bracket_range = open_range.start..=close_range.end;
4680                if !bracket_range.overlaps(&chunk_range) {
4681                    continue;
4682                }
4683
4684                open_to_close_ranges
4685                    .entry((open_range.start, open_range.end))
4686                    .or_insert_with(BTreeMap::new)
4687                    .insert(
4688                        (close_range.start, close_range.end),
4689                        BracketMatch {
4690                            open_range: open_range.clone(),
4691                            close_range: close_range.clone(),
4692                            syntax_layer_depth,
4693                            newline_only: pattern.newline_only,
4694                            color_index: None,
4695                        },
4696                    );
4697
4698                all_brackets.push((
4699                    BracketMatch {
4700                        open_range,
4701                        close_range,
4702                        syntax_layer_depth,
4703                        newline_only: pattern.newline_only,
4704                        color_index: None,
4705                    },
4706                    pattern.rainbow_exclude,
4707                ));
4708            }
4709
4710            let has_bogus_matches = open_to_close_ranges
4711                .iter()
4712                .any(|(_, end_ranges)| end_ranges.len() > 1);
4713            if has_bogus_matches {
4714                // Grammar is producing bogus matches where one open is paired with multiple
4715                // closes. Build a valid stack by walking through positions in order.
4716                // For each close, we know the expected open_len from tree-sitter matches.
4717
4718                // Map each close to its expected open length (for inferring opens)
4719                let close_to_open_len: HashMap<(usize, usize), usize> = all_brackets
4720                    .iter()
4721                    .map(|(m, _)| ((m.close_range.start, m.close_range.end), m.open_range.len()))
4722                    .collect();
4723
4724                // Collect unique opens and closes within this chunk
4725                let mut unique_opens: HashSet<(usize, usize)> = all_brackets
4726                    .iter()
4727                    .map(|(m, _)| (m.open_range.start, m.open_range.end))
4728                    .filter(|(start, _)| chunk_range.contains(start))
4729                    .collect();
4730
4731                let mut unique_closes: Vec<(usize, usize)> = all_brackets
4732                    .iter()
4733                    .map(|(m, _)| (m.close_range.start, m.close_range.end))
4734                    .filter(|(start, _)| chunk_range.contains(start))
4735                    .collect();
4736                unique_closes.sort();
4737                unique_closes.dedup();
4738
4739                // Build valid pairs by walking through closes in order
4740                let mut unique_opens_vec: Vec<_> = unique_opens.iter().copied().collect();
4741                unique_opens_vec.sort();
4742
4743                let mut valid_pairs: HashSet<((usize, usize), (usize, usize))> = HashSet::default();
4744                let mut open_stack: Vec<(usize, usize)> = Vec::new();
4745                let mut open_idx = 0;
4746
4747                for close in &unique_closes {
4748                    // Push all opens before this close onto stack
4749                    while open_idx < unique_opens_vec.len()
4750                        && unique_opens_vec[open_idx].0 < close.0
4751                    {
4752                        open_stack.push(unique_opens_vec[open_idx]);
4753                        open_idx += 1;
4754                    }
4755
4756                    // Try to match with most recent open
4757                    if let Some(open) = open_stack.pop() {
4758                        valid_pairs.insert((open, *close));
4759                    } else if let Some(&open_len) = close_to_open_len.get(close) {
4760                        // No open on stack - infer one based on expected open_len
4761                        if close.0 >= open_len {
4762                            let inferred = (close.0 - open_len, close.0);
4763                            unique_opens.insert(inferred);
4764                            valid_pairs.insert((inferred, *close));
4765                            all_brackets.push((
4766                                BracketMatch {
4767                                    open_range: inferred.0..inferred.1,
4768                                    close_range: close.0..close.1,
4769                                    newline_only: false,
4770                                    syntax_layer_depth: 0,
4771                                    color_index: None,
4772                                },
4773                                false,
4774                            ));
4775                        }
4776                    }
4777                }
4778
4779                all_brackets.retain(|(m, _)| {
4780                    let open = (m.open_range.start, m.open_range.end);
4781                    let close = (m.close_range.start, m.close_range.end);
4782                    valid_pairs.contains(&(open, close))
4783                });
4784            }
4785
4786            let mut all_brackets = all_brackets
4787                .into_iter()
4788                .enumerate()
4789                .map(|(index, (bracket_match, rainbow_exclude))| {
4790                    // Certain languages have "brackets" that are not brackets, e.g. tags. and such
4791                    // bracket will match the entire tag with all text inside.
4792                    // For now, avoid highlighting any pair that has more than single char in each bracket.
4793                    // We need to  colorize `<Element/>` bracket pairs, so cannot make this check stricter.
4794                    let should_color = !rainbow_exclude
4795                        && (bracket_match.open_range.len() == 1
4796                            || bracket_match.close_range.len() == 1);
4797                    if should_color {
4798                        opens.push(bracket_match.open_range.clone());
4799                        color_pairs.push((
4800                            bracket_match.open_range.clone(),
4801                            bracket_match.close_range.clone(),
4802                            index,
4803                        ));
4804                    }
4805                    bracket_match
4806                })
4807                .collect::<Vec<_>>();
4808
4809            opens.sort_by_key(|r| (r.start, r.end));
4810            opens.dedup_by(|a, b| a.start == b.start && a.end == b.end);
4811            color_pairs.sort_by_key(|(_, close, _)| close.end);
4812
4813            let mut open_stack = Vec::new();
4814            let mut open_index = 0;
4815            for (open, close, index) in color_pairs {
4816                while open_index < opens.len() && opens[open_index].start < close.start {
4817                    open_stack.push(opens[open_index].clone());
4818                    open_index += 1;
4819                }
4820
4821                if open_stack.last() == Some(&open) {
4822                    let depth_index = open_stack.len() - 1;
4823                    all_brackets[index].color_index = Some(depth_index);
4824                    open_stack.pop();
4825                }
4826            }
4827
4828            all_brackets.sort_by_key(|bracket_match| {
4829                (bracket_match.open_range.start, bracket_match.open_range.end)
4830            });
4831
4832            if let empty_slot @ None =
4833                &mut self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]
4834            {
4835                *empty_slot = Some(all_brackets.clone());
4836            }
4837            all_bracket_matches.insert(chunk.row_range(), all_brackets);
4838        }
4839
4840        all_bracket_matches
4841    }
4842
4843    pub fn all_bracket_ranges(
4844        &self,
4845        range: Range<usize>,
4846    ) -> impl Iterator<Item = BracketMatch<usize>> {
4847        self.fetch_bracket_ranges(range.clone(), None)
4848            .into_values()
4849            .flatten()
4850            .filter(move |bracket_match| {
4851                let bracket_range = bracket_match.open_range.start..bracket_match.close_range.end;
4852                bracket_range.overlaps(&range)
4853            })
4854    }
4855
4856    /// Returns bracket range pairs overlapping or adjacent to `range`
4857    pub fn bracket_ranges<T: ToOffset>(
4858        &self,
4859        range: Range<T>,
4860    ) -> impl Iterator<Item = BracketMatch<usize>> + '_ {
4861        // Find bracket pairs that *inclusively* contain the given range.
4862        let range = range.start.to_previous_offset(self)..range.end.to_next_offset(self);
4863        self.all_bracket_ranges(range)
4864            .filter(|pair| !pair.newline_only)
4865    }
4866
4867    pub fn debug_variables_query<T: ToOffset>(
4868        &self,
4869        range: Range<T>,
4870    ) -> impl Iterator<Item = (Range<usize>, DebuggerTextObject)> + '_ {
4871        let range = range.start.to_previous_offset(self)..range.end.to_next_offset(self);
4872
4873        let mut matches = self.syntax.matches_with_options(
4874            range.clone(),
4875            &self.text,
4876            TreeSitterOptions::default(),
4877            |grammar| grammar.debug_variables_config.as_ref().map(|c| &c.query),
4878        );
4879
4880        let configs = matches
4881            .grammars()
4882            .iter()
4883            .map(|grammar| grammar.debug_variables_config.as_ref())
4884            .collect::<Vec<_>>();
4885
4886        let mut captures = Vec::<(Range<usize>, DebuggerTextObject)>::new();
4887
4888        iter::from_fn(move || {
4889            loop {
4890                while let Some(capture) = captures.pop() {
4891                    if capture.0.overlaps(&range) {
4892                        return Some(capture);
4893                    }
4894                }
4895
4896                let mat = matches.peek()?;
4897
4898                let Some(config) = configs[mat.grammar_index].as_ref() else {
4899                    matches.advance();
4900                    continue;
4901                };
4902
4903                for capture in mat.captures {
4904                    let Some(ix) = config
4905                        .objects_by_capture_ix
4906                        .binary_search_by_key(&capture.index, |e| e.0)
4907                        .ok()
4908                    else {
4909                        continue;
4910                    };
4911                    let text_object = config.objects_by_capture_ix[ix].1;
4912                    let byte_range = capture.node.byte_range();
4913
4914                    let mut found = false;
4915                    for (range, existing) in captures.iter_mut() {
4916                        if existing == &text_object {
4917                            range.start = range.start.min(byte_range.start);
4918                            range.end = range.end.max(byte_range.end);
4919                            found = true;
4920                            break;
4921                        }
4922                    }
4923
4924                    if !found {
4925                        captures.push((byte_range, text_object));
4926                    }
4927                }
4928
4929                matches.advance();
4930            }
4931        })
4932    }
4933
4934    pub fn text_object_ranges<T: ToOffset>(
4935        &self,
4936        range: Range<T>,
4937        options: TreeSitterOptions,
4938    ) -> impl Iterator<Item = (Range<usize>, TextObject)> + '_ {
4939        let range =
4940            range.start.to_previous_offset(self)..self.len().min(range.end.to_next_offset(self));
4941
4942        let mut matches =
4943            self.syntax
4944                .matches_with_options(range.clone(), &self.text, options, |grammar| {
4945                    grammar.text_object_config.as_ref().map(|c| &c.query)
4946                });
4947
4948        let configs = matches
4949            .grammars()
4950            .iter()
4951            .map(|grammar| grammar.text_object_config.as_ref())
4952            .collect::<Vec<_>>();
4953
4954        let mut captures = Vec::<(Range<usize>, TextObject)>::new();
4955
4956        iter::from_fn(move || {
4957            loop {
4958                while let Some(capture) = captures.pop() {
4959                    if capture.0.overlaps(&range) {
4960                        return Some(capture);
4961                    }
4962                }
4963
4964                let mat = matches.peek()?;
4965
4966                let Some(config) = configs[mat.grammar_index].as_ref() else {
4967                    matches.advance();
4968                    continue;
4969                };
4970
4971                for capture in mat.captures {
4972                    let Some(ix) = config
4973                        .text_objects_by_capture_ix
4974                        .binary_search_by_key(&capture.index, |e| e.0)
4975                        .ok()
4976                    else {
4977                        continue;
4978                    };
4979                    let text_object = config.text_objects_by_capture_ix[ix].1;
4980                    let byte_range = capture.node.byte_range();
4981
4982                    let mut found = false;
4983                    for (range, existing) in captures.iter_mut() {
4984                        if existing == &text_object {
4985                            range.start = range.start.min(byte_range.start);
4986                            range.end = range.end.max(byte_range.end);
4987                            found = true;
4988                            break;
4989                        }
4990                    }
4991
4992                    if !found {
4993                        captures.push((byte_range, text_object));
4994                    }
4995                }
4996
4997                matches.advance();
4998            }
4999        })
5000    }
5001
5002    /// Returns enclosing bracket ranges containing the given range
5003    pub fn enclosing_bracket_ranges<T: ToOffset>(
5004        &self,
5005        range: Range<T>,
5006    ) -> impl Iterator<Item = BracketMatch<usize>> + '_ {
5007        let range = range.start.to_offset(self)..range.end.to_offset(self);
5008
5009        let result: Vec<_> = self.bracket_ranges(range.clone()).collect();
5010        let max_depth = result
5011            .iter()
5012            .map(|mat| mat.syntax_layer_depth)
5013            .max()
5014            .unwrap_or(0);
5015        result.into_iter().filter(move |pair| {
5016            pair.open_range.start <= range.start
5017                && pair.close_range.end >= range.end
5018                && pair.syntax_layer_depth == max_depth
5019        })
5020    }
5021
5022    /// Returns the smallest enclosing bracket ranges containing the given range or None if no brackets contain range
5023    ///
5024    /// Can optionally pass a range_filter to filter the ranges of brackets to consider
5025    pub fn innermost_enclosing_bracket_ranges<T: ToOffset>(
5026        &self,
5027        range: Range<T>,
5028        range_filter: Option<&dyn Fn(Range<usize>, Range<usize>) -> bool>,
5029    ) -> Option<(Range<usize>, Range<usize>)> {
5030        let range = range.start.to_offset(self)..range.end.to_offset(self);
5031
5032        // Get the ranges of the innermost pair of brackets.
5033        let mut result: Option<(Range<usize>, Range<usize>)> = None;
5034
5035        for pair in self.enclosing_bracket_ranges(range) {
5036            if let Some(range_filter) = range_filter
5037                && !range_filter(pair.open_range.clone(), pair.close_range.clone())
5038            {
5039                continue;
5040            }
5041
5042            let len = pair.close_range.end - pair.open_range.start;
5043
5044            if let Some((existing_open, existing_close)) = &result {
5045                let existing_len = existing_close.end - existing_open.start;
5046                if len > existing_len {
5047                    continue;
5048                }
5049            }
5050
5051            result = Some((pair.open_range, pair.close_range));
5052        }
5053
5054        result
5055    }
5056
5057    /// Returns anchor ranges for any matches of the redaction query.
5058    /// The buffer can be associated with multiple languages, and the redaction query associated with each
5059    /// will be run on the relevant section of the buffer.
5060    pub fn redacted_ranges<T: ToOffset>(
5061        &self,
5062        range: Range<T>,
5063    ) -> impl Iterator<Item = Range<usize>> + '_ {
5064        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
5065        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
5066            grammar
5067                .redactions_config
5068                .as_ref()
5069                .map(|config| &config.query)
5070        });
5071
5072        let configs = syntax_matches
5073            .grammars()
5074            .iter()
5075            .map(|grammar| grammar.redactions_config.as_ref())
5076            .collect::<Vec<_>>();
5077
5078        iter::from_fn(move || {
5079            let redacted_range = syntax_matches
5080                .peek()
5081                .and_then(|mat| {
5082                    configs[mat.grammar_index].and_then(|config| {
5083                        mat.captures
5084                            .iter()
5085                            .find(|capture| capture.index == config.redaction_capture_ix)
5086                    })
5087                })
5088                .map(|mat| mat.node.byte_range());
5089            syntax_matches.advance();
5090            redacted_range
5091        })
5092    }
5093
5094    pub fn injections_intersecting_range<T: ToOffset>(
5095        &self,
5096        range: Range<T>,
5097    ) -> impl Iterator<Item = (Range<usize>, &Arc<Language>)> + '_ {
5098        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
5099
5100        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
5101            grammar
5102                .injection_config
5103                .as_ref()
5104                .map(|config| &config.query)
5105        });
5106
5107        let configs = syntax_matches
5108            .grammars()
5109            .iter()
5110            .map(|grammar| grammar.injection_config.as_ref())
5111            .collect::<Vec<_>>();
5112
5113        iter::from_fn(move || {
5114            let ranges = syntax_matches.peek().and_then(|mat| {
5115                let config = &configs[mat.grammar_index]?;
5116                let content_capture_range = mat.captures.iter().find_map(|capture| {
5117                    if capture.index == config.content_capture_ix {
5118                        Some(capture.node.byte_range())
5119                    } else {
5120                        None
5121                    }
5122                })?;
5123                let language = self.language_at(content_capture_range.start)?;
5124                Some((content_capture_range, language))
5125            });
5126            syntax_matches.advance();
5127            ranges
5128        })
5129    }
5130
5131    pub fn runnable_ranges(
5132        &self,
5133        offset_range: Range<usize>,
5134    ) -> impl Iterator<Item = RunnableRange> + '_ {
5135        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
5136            grammar.runnable_config.as_ref().map(|config| &config.query)
5137        });
5138
5139        let test_configs = syntax_matches
5140            .grammars()
5141            .iter()
5142            .map(|grammar| grammar.runnable_config.as_ref())
5143            .collect::<Vec<_>>();
5144
5145        iter::from_fn(move || {
5146            loop {
5147                let mat = syntax_matches.peek()?;
5148
5149                let test_range = test_configs[mat.grammar_index].and_then(|test_configs| {
5150                    let mut run_range = None;
5151                    let full_range = mat.captures.iter().fold(
5152                        Range {
5153                            start: usize::MAX,
5154                            end: 0,
5155                        },
5156                        |mut acc, next| {
5157                            let byte_range = next.node.byte_range();
5158                            if acc.start > byte_range.start {
5159                                acc.start = byte_range.start;
5160                            }
5161                            if acc.end < byte_range.end {
5162                                acc.end = byte_range.end;
5163                            }
5164                            acc
5165                        },
5166                    );
5167                    if full_range.start > full_range.end {
5168                        // We did not find a full spanning range of this match.
5169                        return None;
5170                    }
5171                    let extra_captures: SmallVec<[_; 1]> =
5172                        SmallVec::from_iter(mat.captures.iter().filter_map(|capture| {
5173                            test_configs
5174                                .extra_captures
5175                                .get(capture.index as usize)
5176                                .cloned()
5177                                .and_then(|tag_name| match tag_name {
5178                                    RunnableCapture::Named(name) => {
5179                                        Some((capture.node.byte_range(), name))
5180                                    }
5181                                    RunnableCapture::Run => {
5182                                        let _ = run_range.insert(capture.node.byte_range());
5183                                        None
5184                                    }
5185                                })
5186                        }));
5187                    let run_range = run_range?;
5188                    let tags = test_configs
5189                        .query
5190                        .property_settings(mat.pattern_index)
5191                        .iter()
5192                        .filter_map(|property| {
5193                            if *property.key == *"tag" {
5194                                property
5195                                    .value
5196                                    .as_ref()
5197                                    .map(|value| RunnableTag(value.to_string().into()))
5198                            } else {
5199                                None
5200                            }
5201                        })
5202                        .collect();
5203                    let extra_captures = extra_captures
5204                        .into_iter()
5205                        .map(|(range, name)| {
5206                            (
5207                                name.to_string(),
5208                                self.text_for_range(range).collect::<String>(),
5209                            )
5210                        })
5211                        .collect();
5212                    // All tags should have the same range.
5213                    Some(RunnableRange {
5214                        run_range,
5215                        full_range,
5216                        runnable: Runnable {
5217                            tags,
5218                            language: mat.language,
5219                            buffer: self.remote_id(),
5220                        },
5221                        extra_captures,
5222                        buffer_id: self.remote_id(),
5223                    })
5224                });
5225
5226                syntax_matches.advance();
5227                if test_range.is_some() {
5228                    // It's fine for us to short-circuit on .peek()? returning None. We don't want to return None from this iter if we
5229                    // had a capture that did not contain a run marker, hence we'll just loop around for the next capture.
5230                    return test_range;
5231                }
5232            }
5233        })
5234    }
5235
5236    /// Returns selections for remote peers intersecting the given range.
5237    #[allow(clippy::type_complexity)]
5238    pub fn selections_in_range(
5239        &self,
5240        range: Range<Anchor>,
5241        include_local: bool,
5242    ) -> impl Iterator<
5243        Item = (
5244            ReplicaId,
5245            bool,
5246            CursorShape,
5247            impl Iterator<Item = &Selection<Anchor>> + '_,
5248        ),
5249    > + '_ {
5250        self.remote_selections
5251            .iter()
5252            .filter(move |(replica_id, set)| {
5253                (include_local || **replica_id != self.text.replica_id())
5254                    && !set.selections.is_empty()
5255            })
5256            .map(move |(replica_id, set)| {
5257                let start_ix = match set.selections.binary_search_by(|probe| {
5258                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
5259                }) {
5260                    Ok(ix) | Err(ix) => ix,
5261                };
5262                let end_ix = match set.selections.binary_search_by(|probe| {
5263                    probe.start.cmp(&range.end, self).then(Ordering::Less)
5264                }) {
5265                    Ok(ix) | Err(ix) => ix,
5266                };
5267
5268                (
5269                    *replica_id,
5270                    set.line_mode,
5271                    set.cursor_shape,
5272                    set.selections[start_ix..end_ix].iter(),
5273                )
5274            })
5275    }
5276
5277    /// Returns if the buffer contains any diagnostics.
5278    pub fn has_diagnostics(&self) -> bool {
5279        !self.diagnostics.is_empty()
5280    }
5281
5282    /// Returns all the diagnostics intersecting the given range.
5283    pub fn diagnostics_in_range<'a, T, O>(
5284        &'a self,
5285        search_range: Range<T>,
5286        reversed: bool,
5287    ) -> impl 'a + Iterator<Item = DiagnosticEntryRef<'a, O>>
5288    where
5289        T: 'a + Clone + ToOffset,
5290        O: 'a + FromAnchor,
5291    {
5292        let mut iterators: Vec<_> = self
5293            .diagnostics
5294            .iter()
5295            .map(|(_, collection)| {
5296                collection
5297                    .range::<T, text::Anchor>(search_range.clone(), self, true, reversed)
5298                    .peekable()
5299            })
5300            .collect();
5301
5302        std::iter::from_fn(move || {
5303            let (next_ix, _) = iterators
5304                .iter_mut()
5305                .enumerate()
5306                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
5307                .min_by(|(_, a), (_, b)| {
5308                    let cmp = a
5309                        .range
5310                        .start
5311                        .cmp(&b.range.start, self)
5312                        // when range is equal, sort by diagnostic severity
5313                        .then(a.diagnostic.severity.cmp(&b.diagnostic.severity))
5314                        // and stabilize order with group_id
5315                        .then(a.diagnostic.group_id.cmp(&b.diagnostic.group_id));
5316                    if reversed { cmp.reverse() } else { cmp }
5317                })?;
5318            iterators[next_ix]
5319                .next()
5320                .map(
5321                    |DiagnosticEntryRef { range, diagnostic }| DiagnosticEntryRef {
5322                        diagnostic,
5323                        range: FromAnchor::from_anchor(&range.start, self)
5324                            ..FromAnchor::from_anchor(&range.end, self),
5325                    },
5326                )
5327        })
5328    }
5329
5330    /// Returns all the diagnostic groups associated with the given
5331    /// language server ID. If no language server ID is provided,
5332    /// all diagnostics groups are returned.
5333    pub fn diagnostic_groups(
5334        &self,
5335        language_server_id: Option<LanguageServerId>,
5336    ) -> Vec<(LanguageServerId, DiagnosticGroup<'_, Anchor>)> {
5337        let mut groups = Vec::new();
5338
5339        if let Some(language_server_id) = language_server_id {
5340            if let Some(set) = self.diagnostics.get(&language_server_id) {
5341                set.groups(language_server_id, &mut groups, self);
5342            }
5343        } else {
5344            for (language_server_id, diagnostics) in self.diagnostics.iter() {
5345                diagnostics.groups(*language_server_id, &mut groups, self);
5346            }
5347        }
5348
5349        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
5350            let a_start = &group_a.entries[group_a.primary_ix].range.start;
5351            let b_start = &group_b.entries[group_b.primary_ix].range.start;
5352            a_start.cmp(b_start, self).then_with(|| id_a.cmp(id_b))
5353        });
5354
5355        groups
5356    }
5357
5358    /// Returns an iterator over the diagnostics for the given group.
5359    pub fn diagnostic_group<O>(
5360        &self,
5361        group_id: usize,
5362    ) -> impl Iterator<Item = DiagnosticEntryRef<'_, O>> + use<'_, O>
5363    where
5364        O: FromAnchor + 'static,
5365    {
5366        self.diagnostics
5367            .iter()
5368            .flat_map(move |(_, set)| set.group(group_id, self))
5369    }
5370
5371    /// An integer version number that accounts for all updates besides
5372    /// the buffer's text itself (which is versioned via a version vector).
5373    pub fn non_text_state_update_count(&self) -> usize {
5374        self.non_text_state_update_count
5375    }
5376
5377    /// An integer version that changes when the buffer's syntax changes.
5378    pub fn syntax_update_count(&self) -> usize {
5379        self.syntax.update_count()
5380    }
5381
5382    /// Returns a snapshot of underlying file.
5383    pub fn file(&self) -> Option<&Arc<dyn File>> {
5384        self.file.as_ref()
5385    }
5386
5387    pub fn resolve_file_path(&self, include_root: bool, cx: &App) -> Option<String> {
5388        if let Some(file) = self.file() {
5389            if file.path().file_name().is_none() || include_root {
5390                Some(file.full_path(cx).to_string_lossy().into_owned())
5391            } else {
5392                Some(file.path().display(file.path_style(cx)).to_string())
5393            }
5394        } else {
5395            None
5396        }
5397    }
5398
5399    pub fn words_in_range(&self, query: WordsQuery) -> BTreeMap<String, Range<Anchor>> {
5400        let query_str = query.fuzzy_contents;
5401        if query_str.is_some_and(|query| query.is_empty()) {
5402            return BTreeMap::default();
5403        }
5404
5405        let classifier = CharClassifier::new(self.language.clone().map(|language| LanguageScope {
5406            language,
5407            override_id: None,
5408        }));
5409
5410        let mut query_ix = 0;
5411        let query_chars = query_str.map(|query| query.chars().collect::<Vec<_>>());
5412        let query_len = query_chars.as_ref().map_or(0, |query| query.len());
5413
5414        let mut words = BTreeMap::default();
5415        let mut current_word_start_ix = None;
5416        let mut chunk_ix = query.range.start;
5417        for chunk in self.chunks(query.range, false) {
5418            for (i, c) in chunk.text.char_indices() {
5419                let ix = chunk_ix + i;
5420                if classifier.is_word(c) {
5421                    if current_word_start_ix.is_none() {
5422                        current_word_start_ix = Some(ix);
5423                    }
5424
5425                    if let Some(query_chars) = &query_chars
5426                        && query_ix < query_len
5427                        && c.to_lowercase().eq(query_chars[query_ix].to_lowercase())
5428                    {
5429                        query_ix += 1;
5430                    }
5431                    continue;
5432                } else if let Some(word_start) = current_word_start_ix.take()
5433                    && query_ix == query_len
5434                {
5435                    let word_range = self.anchor_before(word_start)..self.anchor_after(ix);
5436                    let mut word_text = self.text_for_range(word_start..ix).peekable();
5437                    let first_char = word_text
5438                        .peek()
5439                        .and_then(|first_chunk| first_chunk.chars().next());
5440                    // Skip empty and "words" starting with digits as a heuristic to reduce useless completions
5441                    if !query.skip_digits
5442                        || first_char.is_none_or(|first_char| !first_char.is_digit(10))
5443                    {
5444                        words.insert(word_text.collect(), word_range);
5445                    }
5446                }
5447                query_ix = 0;
5448            }
5449            chunk_ix += chunk.text.len();
5450        }
5451
5452        words
5453    }
5454}
5455
5456pub struct WordsQuery<'a> {
5457    /// Only returns words with all chars from the fuzzy string in them.
5458    pub fuzzy_contents: Option<&'a str>,
5459    /// Skips words that start with a digit.
5460    pub skip_digits: bool,
5461    /// Buffer offset range, to look for words.
5462    pub range: Range<usize>,
5463}
5464
5465fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
5466    indent_size_for_text(text.chars_at(Point::new(row, 0)))
5467}
5468
5469fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
5470    let mut result = IndentSize::spaces(0);
5471    for c in text {
5472        let kind = match c {
5473            ' ' => IndentKind::Space,
5474            '\t' => IndentKind::Tab,
5475            _ => break,
5476        };
5477        if result.len == 0 {
5478            result.kind = kind;
5479        }
5480        result.len += 1;
5481    }
5482    result
5483}
5484
5485impl Clone for BufferSnapshot {
5486    fn clone(&self) -> Self {
5487        Self {
5488            text: self.text.clone(),
5489            syntax: self.syntax.clone(),
5490            file: self.file.clone(),
5491            remote_selections: self.remote_selections.clone(),
5492            diagnostics: self.diagnostics.clone(),
5493            language: self.language.clone(),
5494            tree_sitter_data: self.tree_sitter_data.clone(),
5495            non_text_state_update_count: self.non_text_state_update_count,
5496            capability: self.capability,
5497            modeline: self.modeline.clone(),
5498        }
5499    }
5500}
5501
5502impl Deref for BufferSnapshot {
5503    type Target = text::BufferSnapshot;
5504
5505    fn deref(&self) -> &Self::Target {
5506        &self.text
5507    }
5508}
5509
5510unsafe impl Send for BufferChunks<'_> {}
5511
5512impl<'a> BufferChunks<'a> {
5513    pub(crate) fn new(
5514        text: &'a Rope,
5515        range: Range<usize>,
5516        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
5517        diagnostics: bool,
5518        buffer_snapshot: Option<&'a BufferSnapshot>,
5519    ) -> Self {
5520        let mut highlights = None;
5521        if let Some((captures, highlight_maps)) = syntax {
5522            highlights = Some(BufferChunkHighlights {
5523                captures,
5524                next_capture: None,
5525                stack: Default::default(),
5526                highlight_maps,
5527            })
5528        }
5529
5530        let diagnostic_endpoints = diagnostics.then(|| Vec::new().into_iter().peekable());
5531        let chunks = text.chunks_in_range(range.clone());
5532
5533        let mut this = BufferChunks {
5534            range,
5535            buffer_snapshot,
5536            chunks,
5537            diagnostic_endpoints,
5538            error_depth: 0,
5539            warning_depth: 0,
5540            information_depth: 0,
5541            hint_depth: 0,
5542            unnecessary_depth: 0,
5543            underline: true,
5544            highlights,
5545        };
5546        this.initialize_diagnostic_endpoints();
5547        this
5548    }
5549
5550    /// Seeks to the given byte offset in the buffer.
5551    pub fn seek(&mut self, range: Range<usize>) {
5552        let old_range = std::mem::replace(&mut self.range, range.clone());
5553        self.chunks.set_range(self.range.clone());
5554        if let Some(highlights) = self.highlights.as_mut() {
5555            if old_range.start <= self.range.start && old_range.end >= self.range.end {
5556                // Reuse existing highlights stack, as the new range is a subrange of the old one.
5557                highlights
5558                    .stack
5559                    .retain(|(end_offset, _)| *end_offset > range.start);
5560                if let Some(capture) = &highlights.next_capture
5561                    && range.start >= capture.node.start_byte()
5562                {
5563                    let next_capture_end = capture.node.end_byte();
5564                    if range.start < next_capture_end {
5565                        highlights.stack.push((
5566                            next_capture_end,
5567                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
5568                        ));
5569                    }
5570                    highlights.next_capture.take();
5571                }
5572            } else if let Some(snapshot) = self.buffer_snapshot {
5573                let (captures, highlight_maps) = snapshot.get_highlights(self.range.clone());
5574                *highlights = BufferChunkHighlights {
5575                    captures,
5576                    next_capture: None,
5577                    stack: Default::default(),
5578                    highlight_maps,
5579                };
5580            } else {
5581                // We cannot obtain new highlights for a language-aware buffer iterator, as we don't have a buffer snapshot.
5582                // Seeking such BufferChunks is not supported.
5583                debug_assert!(
5584                    false,
5585                    "Attempted to seek on a language-aware buffer iterator without associated buffer snapshot"
5586                );
5587            }
5588
5589            highlights.captures.set_byte_range(self.range.clone());
5590            self.initialize_diagnostic_endpoints();
5591        }
5592    }
5593
5594    fn initialize_diagnostic_endpoints(&mut self) {
5595        if let Some(diagnostics) = self.diagnostic_endpoints.as_mut()
5596            && let Some(buffer) = self.buffer_snapshot
5597        {
5598            let mut diagnostic_endpoints = Vec::new();
5599            for entry in buffer.diagnostics_in_range::<_, usize>(self.range.clone(), false) {
5600                diagnostic_endpoints.push(DiagnosticEndpoint {
5601                    offset: entry.range.start,
5602                    is_start: true,
5603                    severity: entry.diagnostic.severity,
5604                    is_unnecessary: entry.diagnostic.is_unnecessary,
5605                    underline: entry.diagnostic.underline,
5606                });
5607                diagnostic_endpoints.push(DiagnosticEndpoint {
5608                    offset: entry.range.end,
5609                    is_start: false,
5610                    severity: entry.diagnostic.severity,
5611                    is_unnecessary: entry.diagnostic.is_unnecessary,
5612                    underline: entry.diagnostic.underline,
5613                });
5614            }
5615            diagnostic_endpoints
5616                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
5617            *diagnostics = diagnostic_endpoints.into_iter().peekable();
5618            self.hint_depth = 0;
5619            self.error_depth = 0;
5620            self.warning_depth = 0;
5621            self.information_depth = 0;
5622        }
5623    }
5624
5625    /// The current byte offset in the buffer.
5626    pub fn offset(&self) -> usize {
5627        self.range.start
5628    }
5629
5630    pub fn range(&self) -> Range<usize> {
5631        self.range.clone()
5632    }
5633
5634    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
5635        let depth = match endpoint.severity {
5636            DiagnosticSeverity::ERROR => &mut self.error_depth,
5637            DiagnosticSeverity::WARNING => &mut self.warning_depth,
5638            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
5639            DiagnosticSeverity::HINT => &mut self.hint_depth,
5640            _ => return,
5641        };
5642        if endpoint.is_start {
5643            *depth += 1;
5644        } else {
5645            *depth -= 1;
5646        }
5647
5648        if endpoint.is_unnecessary {
5649            if endpoint.is_start {
5650                self.unnecessary_depth += 1;
5651            } else {
5652                self.unnecessary_depth -= 1;
5653            }
5654        }
5655    }
5656
5657    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
5658        if self.error_depth > 0 {
5659            Some(DiagnosticSeverity::ERROR)
5660        } else if self.warning_depth > 0 {
5661            Some(DiagnosticSeverity::WARNING)
5662        } else if self.information_depth > 0 {
5663            Some(DiagnosticSeverity::INFORMATION)
5664        } else if self.hint_depth > 0 {
5665            Some(DiagnosticSeverity::HINT)
5666        } else {
5667            None
5668        }
5669    }
5670
5671    fn current_code_is_unnecessary(&self) -> bool {
5672        self.unnecessary_depth > 0
5673    }
5674}
5675
5676impl<'a> Iterator for BufferChunks<'a> {
5677    type Item = Chunk<'a>;
5678
5679    fn next(&mut self) -> Option<Self::Item> {
5680        let mut next_capture_start = usize::MAX;
5681        let mut next_diagnostic_endpoint = usize::MAX;
5682
5683        if let Some(highlights) = self.highlights.as_mut() {
5684            while let Some((parent_capture_end, _)) = highlights.stack.last() {
5685                if *parent_capture_end <= self.range.start {
5686                    highlights.stack.pop();
5687                } else {
5688                    break;
5689                }
5690            }
5691
5692            if highlights.next_capture.is_none() {
5693                highlights.next_capture = highlights.captures.next();
5694            }
5695
5696            while let Some(capture) = highlights.next_capture.as_ref() {
5697                if self.range.start < capture.node.start_byte() {
5698                    next_capture_start = capture.node.start_byte();
5699                    break;
5700                } else {
5701                    let highlight_id =
5702                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
5703                    highlights
5704                        .stack
5705                        .push((capture.node.end_byte(), highlight_id));
5706                    highlights.next_capture = highlights.captures.next();
5707                }
5708            }
5709        }
5710
5711        let mut diagnostic_endpoints = std::mem::take(&mut self.diagnostic_endpoints);
5712        if let Some(diagnostic_endpoints) = diagnostic_endpoints.as_mut() {
5713            while let Some(endpoint) = diagnostic_endpoints.peek().copied() {
5714                if endpoint.offset <= self.range.start {
5715                    self.update_diagnostic_depths(endpoint);
5716                    diagnostic_endpoints.next();
5717                    self.underline = endpoint.underline;
5718                } else {
5719                    next_diagnostic_endpoint = endpoint.offset;
5720                    break;
5721                }
5722            }
5723        }
5724        self.diagnostic_endpoints = diagnostic_endpoints;
5725
5726        if let Some(ChunkBitmaps {
5727            text: chunk,
5728            chars: chars_map,
5729            tabs,
5730            newlines,
5731        }) = self.chunks.peek_with_bitmaps()
5732        {
5733            let chunk_start = self.range.start;
5734            let mut chunk_end = (self.chunks.offset() + chunk.len())
5735                .min(next_capture_start)
5736                .min(next_diagnostic_endpoint);
5737            let mut highlight_id = None;
5738            if let Some(highlights) = self.highlights.as_ref()
5739                && let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last()
5740            {
5741                chunk_end = chunk_end.min(*parent_capture_end);
5742                highlight_id = Some(*parent_highlight_id);
5743            }
5744            let bit_start = chunk_start - self.chunks.offset();
5745            let bit_end = chunk_end - self.chunks.offset();
5746
5747            let slice = &chunk[bit_start..bit_end];
5748
5749            let mask = 1u128.unbounded_shl(bit_end as u32).wrapping_sub(1);
5750            let tabs = (tabs >> bit_start) & mask;
5751            let chars = (chars_map >> bit_start) & mask;
5752            let newlines = (newlines >> bit_start) & mask;
5753
5754            self.range.start = chunk_end;
5755            if self.range.start == self.chunks.offset() + chunk.len() {
5756                self.chunks.next().unwrap();
5757            }
5758
5759            Some(Chunk {
5760                text: slice,
5761                syntax_highlight_id: highlight_id,
5762                underline: self.underline,
5763                diagnostic_severity: self.current_diagnostic_severity(),
5764                is_unnecessary: self.current_code_is_unnecessary(),
5765                tabs,
5766                chars,
5767                newlines,
5768                ..Chunk::default()
5769            })
5770        } else {
5771            None
5772        }
5773    }
5774}
5775
5776impl operation_queue::Operation for Operation {
5777    fn lamport_timestamp(&self) -> clock::Lamport {
5778        match self {
5779            Operation::Buffer(_) => {
5780                unreachable!("buffer operations should never be deferred at this layer")
5781            }
5782            Operation::UpdateDiagnostics {
5783                lamport_timestamp, ..
5784            }
5785            | Operation::UpdateSelections {
5786                lamport_timestamp, ..
5787            }
5788            | Operation::UpdateCompletionTriggers {
5789                lamport_timestamp, ..
5790            }
5791            | Operation::UpdateLineEnding {
5792                lamport_timestamp, ..
5793            } => *lamport_timestamp,
5794        }
5795    }
5796}
5797
5798impl Default for Diagnostic {
5799    fn default() -> Self {
5800        Self {
5801            source: Default::default(),
5802            source_kind: DiagnosticSourceKind::Other,
5803            code: None,
5804            code_description: None,
5805            severity: DiagnosticSeverity::ERROR,
5806            message: Default::default(),
5807            markdown: None,
5808            group_id: 0,
5809            is_primary: false,
5810            is_disk_based: false,
5811            is_unnecessary: false,
5812            underline: true,
5813            data: None,
5814            registration_id: None,
5815        }
5816    }
5817}
5818
5819impl IndentSize {
5820    /// Returns an [`IndentSize`] representing the given spaces.
5821    pub fn spaces(len: u32) -> Self {
5822        Self {
5823            len,
5824            kind: IndentKind::Space,
5825        }
5826    }
5827
5828    /// Returns an [`IndentSize`] representing a tab.
5829    pub fn tab() -> Self {
5830        Self {
5831            len: 1,
5832            kind: IndentKind::Tab,
5833        }
5834    }
5835
5836    /// An iterator over the characters represented by this [`IndentSize`].
5837    pub fn chars(&self) -> impl Iterator<Item = char> {
5838        iter::repeat(self.char()).take(self.len as usize)
5839    }
5840
5841    /// The character representation of this [`IndentSize`].
5842    pub fn char(&self) -> char {
5843        match self.kind {
5844            IndentKind::Space => ' ',
5845            IndentKind::Tab => '\t',
5846        }
5847    }
5848
5849    /// Consumes the current [`IndentSize`] and returns a new one that has
5850    /// been shrunk or enlarged by the given size along the given direction.
5851    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
5852        match direction {
5853            Ordering::Less => {
5854                if self.kind == size.kind && self.len >= size.len {
5855                    self.len -= size.len;
5856                }
5857            }
5858            Ordering::Equal => {}
5859            Ordering::Greater => {
5860                if self.len == 0 {
5861                    self = size;
5862                } else if self.kind == size.kind {
5863                    self.len += size.len;
5864                }
5865            }
5866        }
5867        self
5868    }
5869
5870    pub fn len_with_expanded_tabs(&self, tab_size: NonZeroU32) -> usize {
5871        match self.kind {
5872            IndentKind::Space => self.len as usize,
5873            IndentKind::Tab => self.len as usize * tab_size.get() as usize,
5874        }
5875    }
5876}
5877
5878#[cfg(any(test, feature = "test-support"))]
5879pub struct TestFile {
5880    pub path: Arc<RelPath>,
5881    pub root_name: String,
5882    pub local_root: Option<PathBuf>,
5883}
5884
5885#[cfg(any(test, feature = "test-support"))]
5886impl File for TestFile {
5887    fn path(&self) -> &Arc<RelPath> {
5888        &self.path
5889    }
5890
5891    fn full_path(&self, _: &gpui::App) -> PathBuf {
5892        PathBuf::from(self.root_name.clone()).join(self.path.as_std_path())
5893    }
5894
5895    fn as_local(&self) -> Option<&dyn LocalFile> {
5896        if self.local_root.is_some() {
5897            Some(self)
5898        } else {
5899            None
5900        }
5901    }
5902
5903    fn disk_state(&self) -> DiskState {
5904        unimplemented!()
5905    }
5906
5907    fn file_name<'a>(&'a self, _: &'a gpui::App) -> &'a str {
5908        self.path().file_name().unwrap_or(self.root_name.as_ref())
5909    }
5910
5911    fn worktree_id(&self, _: &App) -> WorktreeId {
5912        WorktreeId::from_usize(0)
5913    }
5914
5915    fn to_proto(&self, _: &App) -> rpc::proto::File {
5916        unimplemented!()
5917    }
5918
5919    fn is_private(&self) -> bool {
5920        false
5921    }
5922
5923    fn path_style(&self, _cx: &App) -> PathStyle {
5924        PathStyle::local()
5925    }
5926}
5927
5928#[cfg(any(test, feature = "test-support"))]
5929impl LocalFile for TestFile {
5930    fn abs_path(&self, _cx: &App) -> PathBuf {
5931        PathBuf::from(self.local_root.as_ref().unwrap())
5932            .join(&self.root_name)
5933            .join(self.path.as_std_path())
5934    }
5935
5936    fn load(&self, _cx: &App) -> Task<Result<String>> {
5937        unimplemented!()
5938    }
5939
5940    fn load_bytes(&self, _cx: &App) -> Task<Result<Vec<u8>>> {
5941        unimplemented!()
5942    }
5943}
5944
5945pub(crate) fn contiguous_ranges(
5946    values: impl Iterator<Item = u32>,
5947    max_len: usize,
5948) -> impl Iterator<Item = Range<u32>> {
5949    let mut values = values;
5950    let mut current_range: Option<Range<u32>> = None;
5951    std::iter::from_fn(move || {
5952        loop {
5953            if let Some(value) = values.next() {
5954                if let Some(range) = &mut current_range
5955                    && value == range.end
5956                    && range.len() < max_len
5957                {
5958                    range.end += 1;
5959                    continue;
5960                }
5961
5962                let prev_range = current_range.clone();
5963                current_range = Some(value..(value + 1));
5964                if prev_range.is_some() {
5965                    return prev_range;
5966                }
5967            } else {
5968                return current_range.take();
5969            }
5970        }
5971    })
5972}
5973
5974#[derive(Default, Debug)]
5975pub struct CharClassifier {
5976    scope: Option<LanguageScope>,
5977    scope_context: Option<CharScopeContext>,
5978    ignore_punctuation: bool,
5979}
5980
5981impl CharClassifier {
5982    pub fn new(scope: Option<LanguageScope>) -> Self {
5983        Self {
5984            scope,
5985            scope_context: None,
5986            ignore_punctuation: false,
5987        }
5988    }
5989
5990    pub fn scope_context(self, scope_context: Option<CharScopeContext>) -> Self {
5991        Self {
5992            scope_context,
5993            ..self
5994        }
5995    }
5996
5997    pub fn ignore_punctuation(self, ignore_punctuation: bool) -> Self {
5998        Self {
5999            ignore_punctuation,
6000            ..self
6001        }
6002    }
6003
6004    pub fn is_whitespace(&self, c: char) -> bool {
6005        self.kind(c) == CharKind::Whitespace
6006    }
6007
6008    pub fn is_word(&self, c: char) -> bool {
6009        self.kind(c) == CharKind::Word
6010    }
6011
6012    pub fn is_punctuation(&self, c: char) -> bool {
6013        self.kind(c) == CharKind::Punctuation
6014    }
6015
6016    pub fn kind_with(&self, c: char, ignore_punctuation: bool) -> CharKind {
6017        if c.is_alphanumeric() || c == '_' {
6018            return CharKind::Word;
6019        }
6020
6021        if let Some(scope) = &self.scope {
6022            let characters = match self.scope_context {
6023                Some(CharScopeContext::Completion) => scope.completion_query_characters(),
6024                Some(CharScopeContext::LinkedEdit) => scope.linked_edit_characters(),
6025                None => scope.word_characters(),
6026            };
6027            if let Some(characters) = characters
6028                && characters.contains(&c)
6029            {
6030                return CharKind::Word;
6031            }
6032        }
6033
6034        if c.is_whitespace() {
6035            return CharKind::Whitespace;
6036        }
6037
6038        if ignore_punctuation {
6039            CharKind::Word
6040        } else {
6041            CharKind::Punctuation
6042        }
6043    }
6044
6045    pub fn kind(&self, c: char) -> CharKind {
6046        self.kind_with(c, self.ignore_punctuation)
6047    }
6048}
6049
6050/// Find all of the ranges of whitespace that occur at the ends of lines
6051/// in the given rope.
6052///
6053/// This could also be done with a regex search, but this implementation
6054/// avoids copying text.
6055pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
6056    let mut ranges = Vec::new();
6057
6058    let mut offset = 0;
6059    let mut prev_chunk_trailing_whitespace_range = 0..0;
6060    for chunk in rope.chunks() {
6061        let mut prev_line_trailing_whitespace_range = 0..0;
6062        for (i, line) in chunk.split('\n').enumerate() {
6063            let line_end_offset = offset + line.len();
6064            let trimmed_line_len = line.trim_end_matches([' ', '\t']).len();
6065            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
6066
6067            if i == 0 && trimmed_line_len == 0 {
6068                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
6069            }
6070            if !prev_line_trailing_whitespace_range.is_empty() {
6071                ranges.push(prev_line_trailing_whitespace_range);
6072            }
6073
6074            offset = line_end_offset + 1;
6075            prev_line_trailing_whitespace_range = trailing_whitespace_range;
6076        }
6077
6078        offset -= 1;
6079        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
6080    }
6081
6082    if !prev_chunk_trailing_whitespace_range.is_empty() {
6083        ranges.push(prev_chunk_trailing_whitespace_range);
6084    }
6085
6086    ranges
6087}