buffer.rs

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