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: TreeMap<LanguageServerId, DiagnosticSet>,
 126    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 127    diagnostics_timestamp: clock::Lamport,
 128    completion_triggers: BTreeSet<String>,
 129    completion_triggers_per_language_server: HashMap<LanguageServerId, BTreeSet<String>>,
 130    completion_triggers_timestamp: clock::Lamport,
 131    deferred_ops: OperationQueue<Operation>,
 132    capability: Capability,
 133    has_conflict: bool,
 134    /// Memoize calls to has_changes_since(saved_version).
 135    /// The contents of a cell are (self.version, has_changes) at the time of a last call.
 136    has_unsaved_edits: Cell<(clock::Global, bool)>,
 137    change_bits: Vec<rc::Weak<Cell<bool>>>,
 138    _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: TreeMap<LanguageServerId, DiagnosticSet>,
 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.iter() {
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        if language == self.language {
1484            return;
1485        }
1486        self.non_text_state_update_count += 1;
1487        self.syntax_map.lock().clear(&self.text);
1488        let old_language = std::mem::replace(&mut self.language, language);
1489        self.was_changed();
1490        self.reparse(cx, may_block);
1491        let has_fresh_language =
1492            self.language.is_some() && old_language.is_none_or(|old| old == *PLAIN_TEXT);
1493        cx.emit(BufferEvent::LanguageChanged(has_fresh_language));
1494    }
1495
1496    /// Assign a language registry to the buffer. This allows the buffer to retrieve
1497    /// other languages if parts of the buffer are written in different languages.
1498    pub fn set_language_registry(&self, language_registry: Arc<LanguageRegistry>) {
1499        self.syntax_map
1500            .lock()
1501            .set_language_registry(language_registry);
1502    }
1503
1504    pub fn language_registry(&self) -> Option<Arc<LanguageRegistry>> {
1505        self.syntax_map.lock().language_registry()
1506    }
1507
1508    /// Assign the line ending type to the buffer.
1509    pub fn set_line_ending(&mut self, line_ending: LineEnding, cx: &mut Context<Self>) {
1510        self.text.set_line_ending(line_ending);
1511
1512        let lamport_timestamp = self.text.lamport_clock.tick();
1513        self.send_operation(
1514            Operation::UpdateLineEnding {
1515                line_ending,
1516                lamport_timestamp,
1517            },
1518            true,
1519            cx,
1520        );
1521    }
1522
1523    /// Assign the buffer a new [`Capability`].
1524    pub fn set_capability(&mut self, capability: Capability, cx: &mut Context<Self>) {
1525        if self.capability != capability {
1526            self.capability = capability;
1527            cx.emit(BufferEvent::CapabilityChanged)
1528        }
1529    }
1530
1531    /// This method is called to signal that the buffer has been saved.
1532    pub fn did_save(
1533        &mut self,
1534        version: clock::Global,
1535        mtime: Option<MTime>,
1536        cx: &mut Context<Self>,
1537    ) {
1538        self.saved_version = version.clone();
1539        self.has_unsaved_edits.set((version, false));
1540        self.has_conflict = false;
1541        self.saved_mtime = mtime;
1542        self.was_changed();
1543        cx.emit(BufferEvent::Saved);
1544        cx.notify();
1545    }
1546
1547    /// Reloads the contents of the buffer from disk.
1548    pub fn reload(&mut self, cx: &Context<Self>) -> oneshot::Receiver<Option<Transaction>> {
1549        self.reload_impl(None, cx)
1550    }
1551
1552    /// Reloads the contents of the buffer from disk using the specified encoding.
1553    ///
1554    /// This bypasses automatic encoding detection heuristics (like BOM checks) for non-Unicode encodings,
1555    /// allowing users to force a specific interpretation of the bytes.
1556    pub fn reload_with_encoding(
1557        &mut self,
1558        encoding: &'static Encoding,
1559        cx: &Context<Self>,
1560    ) -> oneshot::Receiver<Option<Transaction>> {
1561        self.reload_impl(Some(encoding), cx)
1562    }
1563
1564    fn reload_impl(
1565        &mut self,
1566        force_encoding: Option<&'static Encoding>,
1567        cx: &Context<Self>,
1568    ) -> oneshot::Receiver<Option<Transaction>> {
1569        let (tx, rx) = futures::channel::oneshot::channel();
1570        let prev_version = self.text.version();
1571
1572        self.reload_task = Some(cx.spawn(async move |this, cx| {
1573            let Some((new_mtime, load_bytes_task, current_encoding)) =
1574                this.update(cx, |this, cx| {
1575                    let file = this.file.as_ref()?.as_local()?;
1576                    Some((
1577                        file.disk_state().mtime(),
1578                        file.load_bytes(cx),
1579                        this.encoding,
1580                    ))
1581                })?
1582            else {
1583                return Ok(());
1584            };
1585
1586            let target_encoding = force_encoding.unwrap_or(current_encoding);
1587
1588            let is_unicode = target_encoding == encoding_rs::UTF_8
1589                || target_encoding == encoding_rs::UTF_16LE
1590                || target_encoding == encoding_rs::UTF_16BE;
1591
1592            let (new_text, has_bom, encoding_used) = if force_encoding.is_some() && !is_unicode {
1593                let bytes = load_bytes_task.await?;
1594                let (cow, _had_errors) = target_encoding.decode_without_bom_handling(&bytes);
1595                (cow.into_owned(), false, target_encoding)
1596            } else {
1597                let bytes = load_bytes_task.await?;
1598                let (cow, used_enc, _had_errors) = target_encoding.decode(&bytes);
1599
1600                let actual_has_bom = if used_enc == encoding_rs::UTF_8 {
1601                    bytes.starts_with(&[0xEF, 0xBB, 0xBF])
1602                } else if used_enc == encoding_rs::UTF_16LE {
1603                    bytes.starts_with(&[0xFF, 0xFE])
1604                } else if used_enc == encoding_rs::UTF_16BE {
1605                    bytes.starts_with(&[0xFE, 0xFF])
1606                } else {
1607                    false
1608                };
1609                (cow.into_owned(), actual_has_bom, used_enc)
1610            };
1611
1612            let diff = this.update(cx, |this, cx| this.diff(new_text, cx))?.await;
1613            this.update(cx, |this, cx| {
1614                if this.version() == diff.base_version {
1615                    this.finalize_last_transaction();
1616                    let old_encoding = this.encoding;
1617                    let old_has_bom = this.has_bom;
1618                    this.apply_diff(diff, cx);
1619                    this.encoding = encoding_used;
1620                    this.has_bom = has_bom;
1621                    let transaction = this.finalize_last_transaction().cloned();
1622                    if let Some(ref txn) = transaction {
1623                        if old_encoding != encoding_used || old_has_bom != has_bom {
1624                            this.reload_with_encoding_txns
1625                                .insert(txn.id, (old_encoding, old_has_bom));
1626                        }
1627                    }
1628                    tx.send(transaction).ok();
1629                    this.has_conflict = false;
1630                    this.did_reload(this.version(), this.line_ending(), new_mtime, cx);
1631                } else {
1632                    if !diff.edits.is_empty()
1633                        || this
1634                            .edits_since::<usize>(&diff.base_version)
1635                            .next()
1636                            .is_some()
1637                    {
1638                        this.has_conflict = true;
1639                    }
1640
1641                    this.did_reload(prev_version, this.line_ending(), this.saved_mtime, cx);
1642                }
1643
1644                this.reload_task.take();
1645            })
1646        }));
1647        rx
1648    }
1649
1650    /// This method is called to signal that the buffer has been reloaded.
1651    pub fn did_reload(
1652        &mut self,
1653        version: clock::Global,
1654        line_ending: LineEnding,
1655        mtime: Option<MTime>,
1656        cx: &mut Context<Self>,
1657    ) {
1658        self.saved_version = version;
1659        self.has_unsaved_edits
1660            .set((self.saved_version.clone(), false));
1661        self.text.set_line_ending(line_ending);
1662        self.saved_mtime = mtime;
1663        cx.emit(BufferEvent::Reloaded);
1664        cx.notify();
1665    }
1666
1667    /// Updates the [`File`] backing this buffer. This should be called when
1668    /// the file has changed or has been deleted.
1669    pub fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut Context<Self>) {
1670        let was_dirty = self.is_dirty();
1671        let mut file_changed = false;
1672
1673        if let Some(old_file) = self.file.as_ref() {
1674            if new_file.path() != old_file.path() {
1675                file_changed = true;
1676            }
1677
1678            let old_state = old_file.disk_state();
1679            let new_state = new_file.disk_state();
1680            if old_state != new_state {
1681                file_changed = true;
1682                if !was_dirty && matches!(new_state, DiskState::Present { .. }) {
1683                    cx.emit(BufferEvent::ReloadNeeded)
1684                }
1685            }
1686        } else {
1687            file_changed = true;
1688        };
1689
1690        self.file = Some(new_file);
1691        if file_changed {
1692            self.was_changed();
1693            self.non_text_state_update_count += 1;
1694            if was_dirty != self.is_dirty() {
1695                cx.emit(BufferEvent::DirtyChanged);
1696            }
1697            cx.emit(BufferEvent::FileHandleChanged);
1698            cx.notify();
1699        }
1700    }
1701
1702    pub fn base_buffer(&self) -> Option<Entity<Self>> {
1703        Some(self.branch_state.as_ref()?.base_buffer.clone())
1704    }
1705
1706    /// Returns the primary [`Language`] assigned to this [`Buffer`].
1707    pub fn language(&self) -> Option<&Arc<Language>> {
1708        self.language.as_ref()
1709    }
1710
1711    /// Returns the [`Language`] at the given location.
1712    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
1713        let offset = position.to_offset(self);
1714        let mut is_first = true;
1715        let start_anchor = self.anchor_before(offset);
1716        let end_anchor = self.anchor_after(offset);
1717        self.syntax_map
1718            .lock()
1719            .layers_for_range(offset..offset, &self.text, false)
1720            .filter(|layer| {
1721                if is_first {
1722                    is_first = false;
1723                    return true;
1724                }
1725
1726                layer
1727                    .included_sub_ranges
1728                    .map(|sub_ranges| {
1729                        sub_ranges.iter().any(|sub_range| {
1730                            let is_before_start = sub_range.end.cmp(&start_anchor, self).is_lt();
1731                            let is_after_end = sub_range.start.cmp(&end_anchor, self).is_gt();
1732                            !is_before_start && !is_after_end
1733                        })
1734                    })
1735                    .unwrap_or(true)
1736            })
1737            .last()
1738            .map(|info| info.language.clone())
1739            .or_else(|| self.language.clone())
1740    }
1741
1742    /// Returns each [`Language`] for the active syntax layers at the given location.
1743    pub fn languages_at<D: ToOffset>(&self, position: D) -> Vec<Arc<Language>> {
1744        let offset = position.to_offset(self);
1745        let mut languages: Vec<Arc<Language>> = self
1746            .syntax_map
1747            .lock()
1748            .layers_for_range(offset..offset, &self.text, false)
1749            .map(|info| info.language.clone())
1750            .collect();
1751
1752        if languages.is_empty()
1753            && let Some(buffer_language) = self.language()
1754        {
1755            languages.push(buffer_language.clone());
1756        }
1757
1758        languages
1759    }
1760
1761    /// An integer version number that accounts for all updates besides
1762    /// the buffer's text itself (which is versioned via a version vector).
1763    pub fn non_text_state_update_count(&self) -> usize {
1764        self.non_text_state_update_count
1765    }
1766
1767    /// Whether the buffer is being parsed in the background.
1768    #[cfg(any(test, feature = "test-support"))]
1769    pub fn is_parsing(&self) -> bool {
1770        self.reparse.is_some()
1771    }
1772
1773    /// Indicates whether the buffer contains any regions that may be
1774    /// written in a language that hasn't been loaded yet.
1775    pub fn contains_unknown_injections(&self) -> bool {
1776        self.syntax_map.lock().contains_unknown_injections()
1777    }
1778
1779    #[cfg(any(test, feature = "test-support"))]
1780    pub fn set_sync_parse_timeout(&mut self, timeout: Option<Duration>) {
1781        self.sync_parse_timeout = timeout;
1782    }
1783
1784    fn invalidate_tree_sitter_data(&mut self, snapshot: text::BufferSnapshot) {
1785        match Arc::get_mut(&mut self.tree_sitter_data) {
1786            Some(tree_sitter_data) => tree_sitter_data.clear(snapshot),
1787            None => {
1788                let tree_sitter_data = TreeSitterData::new(snapshot);
1789                self.tree_sitter_data = Arc::new(tree_sitter_data)
1790            }
1791        }
1792    }
1793
1794    /// Called after an edit to synchronize the buffer's main parse tree with
1795    /// the buffer's new underlying state.
1796    ///
1797    /// Locks the syntax map and interpolates the edits since the last reparse
1798    /// into the foreground syntax tree.
1799    ///
1800    /// Then takes a stable snapshot of the syntax map before unlocking it.
1801    /// The snapshot with the interpolated edits is sent to a background thread,
1802    /// where we ask Tree-sitter to perform an incremental parse.
1803    ///
1804    /// Meanwhile, in the foreground if `may_block` is true, we block the main
1805    /// thread for up to 1ms waiting on the parse to complete. As soon as it
1806    /// completes, we proceed synchronously, unless a 1ms timeout elapses.
1807    ///
1808    /// If we time out waiting on the parse, we spawn a second task waiting
1809    /// until the parse does complete and return with the interpolated tree still
1810    /// in the foreground. When the background parse completes, call back into
1811    /// the main thread and assign the foreground parse state.
1812    ///
1813    /// If the buffer or grammar changed since the start of the background parse,
1814    /// initiate an additional reparse recursively. To avoid concurrent parses
1815    /// for the same buffer, we only initiate a new parse if we are not already
1816    /// parsing in the background.
1817    #[ztracing::instrument(skip_all)]
1818    pub fn reparse(&mut self, cx: &mut Context<Self>, may_block: bool) {
1819        if self.text.version() != *self.tree_sitter_data.version() {
1820            self.invalidate_tree_sitter_data(self.text.snapshot());
1821        }
1822        if self.reparse.is_some() {
1823            return;
1824        }
1825        let language = if let Some(language) = self.language.clone() {
1826            language
1827        } else {
1828            return;
1829        };
1830
1831        let text = self.text_snapshot();
1832        let parsed_version = self.version();
1833
1834        let mut syntax_map = self.syntax_map.lock();
1835        syntax_map.interpolate(&text);
1836        let language_registry = syntax_map.language_registry();
1837        let mut syntax_snapshot = syntax_map.snapshot();
1838        drop(syntax_map);
1839
1840        self.parse_status.0.send(ParseStatus::Parsing).unwrap();
1841        if may_block && let Some(sync_parse_timeout) = self.sync_parse_timeout {
1842            if let Ok(()) = syntax_snapshot.reparse_with_timeout(
1843                &text,
1844                language_registry.clone(),
1845                language.clone(),
1846                sync_parse_timeout,
1847            ) {
1848                self.did_finish_parsing(syntax_snapshot, Some(Duration::from_millis(300)), cx);
1849                self.reparse = None;
1850                return;
1851            }
1852        }
1853
1854        let parse_task = cx.background_spawn({
1855            let language = language.clone();
1856            let language_registry = language_registry.clone();
1857            async move {
1858                syntax_snapshot.reparse(&text, language_registry, language);
1859                syntax_snapshot
1860            }
1861        });
1862
1863        self.reparse = Some(cx.spawn(async move |this, cx| {
1864            let new_syntax_map = parse_task.await;
1865            this.update(cx, move |this, cx| {
1866                let grammar_changed = || {
1867                    this.language
1868                        .as_ref()
1869                        .is_none_or(|current_language| !Arc::ptr_eq(&language, current_language))
1870                };
1871                let language_registry_changed = || {
1872                    new_syntax_map.contains_unknown_injections()
1873                        && language_registry.is_some_and(|registry| {
1874                            registry.version() != new_syntax_map.language_registry_version()
1875                        })
1876                };
1877                let parse_again = this.version.changed_since(&parsed_version)
1878                    || language_registry_changed()
1879                    || grammar_changed();
1880                this.did_finish_parsing(new_syntax_map, None, cx);
1881                this.reparse = None;
1882                if parse_again {
1883                    this.reparse(cx, false);
1884                }
1885            })
1886            .ok();
1887        }));
1888    }
1889
1890    fn did_finish_parsing(
1891        &mut self,
1892        syntax_snapshot: SyntaxSnapshot,
1893        block_budget: Option<Duration>,
1894        cx: &mut Context<Self>,
1895    ) {
1896        self.non_text_state_update_count += 1;
1897        self.syntax_map.lock().did_parse(syntax_snapshot);
1898        self.was_changed();
1899        self.request_autoindent(cx, block_budget);
1900        self.parse_status.0.send(ParseStatus::Idle).unwrap();
1901        self.invalidate_tree_sitter_data(self.text.snapshot());
1902        cx.emit(BufferEvent::Reparsed);
1903        cx.notify();
1904    }
1905
1906    pub fn parse_status(&self) -> watch::Receiver<ParseStatus> {
1907        self.parse_status.1.clone()
1908    }
1909
1910    /// Wait until the buffer is no longer parsing
1911    pub fn parsing_idle(&self) -> impl Future<Output = ()> + use<> {
1912        let mut parse_status = self.parse_status();
1913        async move {
1914            while *parse_status.borrow() != ParseStatus::Idle {
1915                if parse_status.changed().await.is_err() {
1916                    break;
1917                }
1918            }
1919        }
1920    }
1921
1922    /// Assign to the buffer a set of diagnostics created by a given language server.
1923    pub fn update_diagnostics(
1924        &mut self,
1925        server_id: LanguageServerId,
1926        diagnostics: DiagnosticSet,
1927        cx: &mut Context<Self>,
1928    ) {
1929        let lamport_timestamp = self.text.lamport_clock.tick();
1930        let op = Operation::UpdateDiagnostics {
1931            server_id,
1932            diagnostics: diagnostics.iter().cloned().collect(),
1933            lamport_timestamp,
1934        };
1935
1936        self.apply_diagnostic_update(server_id, diagnostics, lamport_timestamp, cx);
1937        self.send_operation(op, true, cx);
1938    }
1939
1940    pub fn buffer_diagnostics(
1941        &self,
1942        for_server: Option<LanguageServerId>,
1943    ) -> Vec<&DiagnosticEntry<Anchor>> {
1944        match for_server {
1945            Some(server_id) => self
1946                .diagnostics
1947                .get(&server_id)
1948                .map_or_else(Vec::new, |diagnostics| diagnostics.iter().collect()),
1949            None => self
1950                .diagnostics
1951                .iter()
1952                .flat_map(|(_, diagnostic_set)| diagnostic_set.iter())
1953                .collect(),
1954        }
1955    }
1956
1957    fn request_autoindent(&mut self, cx: &mut Context<Self>, block_budget: Option<Duration>) {
1958        if let Some(indent_sizes) = self.compute_autoindents() {
1959            let indent_sizes = cx.background_spawn(indent_sizes);
1960            let Some(block_budget) = block_budget else {
1961                self.pending_autoindent = Some(cx.spawn(async move |this, cx| {
1962                    let indent_sizes = indent_sizes.await;
1963                    this.update(cx, |this, cx| {
1964                        this.apply_autoindents(indent_sizes, cx);
1965                    })
1966                    .ok();
1967                }));
1968                return;
1969            };
1970            match cx
1971                .foreground_executor()
1972                .block_with_timeout(block_budget, indent_sizes)
1973            {
1974                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
1975                Err(indent_sizes) => {
1976                    self.pending_autoindent = Some(cx.spawn(async move |this, cx| {
1977                        let indent_sizes = indent_sizes.await;
1978                        this.update(cx, |this, cx| {
1979                            this.apply_autoindents(indent_sizes, cx);
1980                        })
1981                        .ok();
1982                    }));
1983                }
1984            }
1985        } else {
1986            self.autoindent_requests.clear();
1987            for tx in self.wait_for_autoindent_txs.drain(..) {
1988                tx.send(()).ok();
1989            }
1990        }
1991    }
1992
1993    fn compute_autoindents(
1994        &self,
1995    ) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>> + use<>> {
1996        let max_rows_between_yields = 100;
1997        let snapshot = self.snapshot();
1998        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
1999            return None;
2000        }
2001
2002        let autoindent_requests = self.autoindent_requests.clone();
2003        Some(async move {
2004            let mut indent_sizes = BTreeMap::<u32, (IndentSize, bool)>::new();
2005            for request in autoindent_requests {
2006                // Resolve each edited range to its row in the current buffer and in the
2007                // buffer before this batch of edits.
2008                let mut row_ranges = Vec::new();
2009                let mut old_to_new_rows = BTreeMap::new();
2010                let mut language_indent_sizes_by_new_row = Vec::new();
2011                for entry in &request.entries {
2012                    let position = entry.range.start;
2013                    let new_row = position.to_point(&snapshot).row;
2014                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
2015                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
2016
2017                    if let Some(old_row) = entry.old_row {
2018                        old_to_new_rows.insert(old_row, new_row);
2019                    }
2020                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
2021                }
2022
2023                // Build a map containing the suggested indentation for each of the edited lines
2024                // with respect to the state of the buffer before these edits. This map is keyed
2025                // by the rows for these lines in the current state of the buffer.
2026                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
2027                let old_edited_ranges =
2028                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
2029                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
2030                let mut language_indent_size = IndentSize::default();
2031                for old_edited_range in old_edited_ranges {
2032                    let suggestions = request
2033                        .before_edit
2034                        .suggest_autoindents(old_edited_range.clone())
2035                        .into_iter()
2036                        .flatten();
2037                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
2038                        if let Some(suggestion) = suggestion {
2039                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
2040
2041                            // Find the indent size based on the language for this row.
2042                            while let Some((row, size)) = language_indent_sizes.peek() {
2043                                if *row > new_row {
2044                                    break;
2045                                }
2046                                language_indent_size = *size;
2047                                language_indent_sizes.next();
2048                            }
2049
2050                            let suggested_indent = old_to_new_rows
2051                                .get(&suggestion.basis_row)
2052                                .and_then(|from_row| {
2053                                    Some(old_suggestions.get(from_row).copied()?.0)
2054                                })
2055                                .unwrap_or_else(|| {
2056                                    request
2057                                        .before_edit
2058                                        .indent_size_for_line(suggestion.basis_row)
2059                                })
2060                                .with_delta(suggestion.delta, language_indent_size);
2061                            old_suggestions
2062                                .insert(new_row, (suggested_indent, suggestion.within_error));
2063                        }
2064                    }
2065                    yield_now().await;
2066                }
2067
2068                // Compute new suggestions for each line, but only include them in the result
2069                // if they differ from the old suggestion for that line.
2070                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
2071                let mut language_indent_size = IndentSize::default();
2072                for (row_range, original_indent_column) in row_ranges {
2073                    let new_edited_row_range = if request.is_block_mode {
2074                        row_range.start..row_range.start + 1
2075                    } else {
2076                        row_range.clone()
2077                    };
2078
2079                    let suggestions = snapshot
2080                        .suggest_autoindents(new_edited_row_range.clone())
2081                        .into_iter()
2082                        .flatten();
2083                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
2084                        if let Some(suggestion) = suggestion {
2085                            // Find the indent size based on the language for this row.
2086                            while let Some((row, size)) = language_indent_sizes.peek() {
2087                                if *row > new_row {
2088                                    break;
2089                                }
2090                                language_indent_size = *size;
2091                                language_indent_sizes.next();
2092                            }
2093
2094                            let suggested_indent = indent_sizes
2095                                .get(&suggestion.basis_row)
2096                                .copied()
2097                                .map(|e| e.0)
2098                                .unwrap_or_else(|| {
2099                                    snapshot.indent_size_for_line(suggestion.basis_row)
2100                                })
2101                                .with_delta(suggestion.delta, language_indent_size);
2102
2103                            if old_suggestions.get(&new_row).is_none_or(
2104                                |(old_indentation, was_within_error)| {
2105                                    suggested_indent != *old_indentation
2106                                        && (!suggestion.within_error || *was_within_error)
2107                                },
2108                            ) {
2109                                indent_sizes.insert(
2110                                    new_row,
2111                                    (suggested_indent, request.ignore_empty_lines),
2112                                );
2113                            }
2114                        }
2115                    }
2116
2117                    if let (true, Some(original_indent_column)) =
2118                        (request.is_block_mode, original_indent_column)
2119                    {
2120                        let new_indent =
2121                            if let Some((indent, _)) = indent_sizes.get(&row_range.start) {
2122                                *indent
2123                            } else {
2124                                snapshot.indent_size_for_line(row_range.start)
2125                            };
2126                        let delta = new_indent.len as i64 - original_indent_column as i64;
2127                        if delta != 0 {
2128                            for row in row_range.skip(1) {
2129                                indent_sizes.entry(row).or_insert_with(|| {
2130                                    let mut size = snapshot.indent_size_for_line(row);
2131                                    if size.kind == new_indent.kind {
2132                                        match delta.cmp(&0) {
2133                                            Ordering::Greater => size.len += delta as u32,
2134                                            Ordering::Less => {
2135                                                size.len = size.len.saturating_sub(-delta as u32)
2136                                            }
2137                                            Ordering::Equal => {}
2138                                        }
2139                                    }
2140                                    (size, request.ignore_empty_lines)
2141                                });
2142                            }
2143                        }
2144                    }
2145
2146                    yield_now().await;
2147                }
2148            }
2149
2150            indent_sizes
2151                .into_iter()
2152                .filter_map(|(row, (indent, ignore_empty_lines))| {
2153                    if ignore_empty_lines && snapshot.line_len(row) == 0 {
2154                        None
2155                    } else {
2156                        Some((row, indent))
2157                    }
2158                })
2159                .collect()
2160        })
2161    }
2162
2163    fn apply_autoindents(
2164        &mut self,
2165        indent_sizes: BTreeMap<u32, IndentSize>,
2166        cx: &mut Context<Self>,
2167    ) {
2168        self.autoindent_requests.clear();
2169        for tx in self.wait_for_autoindent_txs.drain(..) {
2170            tx.send(()).ok();
2171        }
2172
2173        let edits: Vec<_> = indent_sizes
2174            .into_iter()
2175            .filter_map(|(row, indent_size)| {
2176                let current_size = indent_size_for_line(self, row);
2177                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
2178            })
2179            .collect();
2180
2181        let preserve_preview = self.preserve_preview();
2182        self.edit(edits, None, cx);
2183        if preserve_preview {
2184            self.refresh_preview();
2185        }
2186    }
2187
2188    /// Create a minimal edit that will cause the given row to be indented
2189    /// with the given size. After applying this edit, the length of the line
2190    /// will always be at least `new_size.len`.
2191    pub fn edit_for_indent_size_adjustment(
2192        row: u32,
2193        current_size: IndentSize,
2194        new_size: IndentSize,
2195    ) -> Option<(Range<Point>, String)> {
2196        if new_size.kind == current_size.kind {
2197            match new_size.len.cmp(&current_size.len) {
2198                Ordering::Greater => {
2199                    let point = Point::new(row, 0);
2200                    Some((
2201                        point..point,
2202                        iter::repeat(new_size.char())
2203                            .take((new_size.len - current_size.len) as usize)
2204                            .collect::<String>(),
2205                    ))
2206                }
2207
2208                Ordering::Less => Some((
2209                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
2210                    String::new(),
2211                )),
2212
2213                Ordering::Equal => None,
2214            }
2215        } else {
2216            Some((
2217                Point::new(row, 0)..Point::new(row, current_size.len),
2218                iter::repeat(new_size.char())
2219                    .take(new_size.len as usize)
2220                    .collect::<String>(),
2221            ))
2222        }
2223    }
2224
2225    /// Spawns a background task that asynchronously computes a `Diff` between the buffer's text
2226    /// and the given new text.
2227    pub fn diff<T>(&self, new_text: T, cx: &App) -> Task<Diff>
2228    where
2229        T: AsRef<str> + Send + 'static,
2230    {
2231        let old_text = self.as_rope().clone();
2232        let base_version = self.version();
2233        cx.background_spawn(async move {
2234            let old_text = old_text.to_string();
2235            let mut new_text = new_text.as_ref().to_owned();
2236            let line_ending = LineEnding::detect(&new_text);
2237            LineEnding::normalize(&mut new_text);
2238            let edits = text_diff(&old_text, &new_text);
2239            Diff {
2240                base_version,
2241                line_ending,
2242                edits,
2243            }
2244        })
2245    }
2246
2247    /// Spawns a background task that searches the buffer for any whitespace
2248    /// at the ends of a lines, and returns a `Diff` that removes that whitespace.
2249    pub fn remove_trailing_whitespace(&self, cx: &App) -> Task<Diff> {
2250        let old_text = self.as_rope().clone();
2251        let line_ending = self.line_ending();
2252        let base_version = self.version();
2253        cx.background_spawn(async move {
2254            let ranges = trailing_whitespace_ranges(&old_text);
2255            let empty = Arc::<str>::from("");
2256            Diff {
2257                base_version,
2258                line_ending,
2259                edits: ranges
2260                    .into_iter()
2261                    .map(|range| (range, empty.clone()))
2262                    .collect(),
2263            }
2264        })
2265    }
2266
2267    /// Ensures that the buffer ends with a single newline character, and
2268    /// no other whitespace. Skips if the buffer is empty.
2269    pub fn ensure_final_newline(&mut self, cx: &mut Context<Self>) {
2270        let len = self.len();
2271        if len == 0 {
2272            return;
2273        }
2274        let mut offset = len;
2275        for chunk in self.as_rope().reversed_chunks_in_range(0..len) {
2276            let non_whitespace_len = chunk
2277                .trim_end_matches(|c: char| c.is_ascii_whitespace())
2278                .len();
2279            offset -= chunk.len();
2280            offset += non_whitespace_len;
2281            if non_whitespace_len != 0 {
2282                if offset == len - 1 && chunk.get(non_whitespace_len..) == Some("\n") {
2283                    return;
2284                }
2285                break;
2286            }
2287        }
2288        self.edit([(offset..len, "\n")], None, cx);
2289    }
2290
2291    /// Applies a diff to the buffer. If the buffer has changed since the given diff was
2292    /// calculated, then adjust the diff to account for those changes, and discard any
2293    /// parts of the diff that conflict with those changes.
2294    pub fn apply_diff(&mut self, diff: Diff, cx: &mut Context<Self>) -> Option<TransactionId> {
2295        let snapshot = self.snapshot();
2296        let mut edits_since = snapshot.edits_since::<usize>(&diff.base_version).peekable();
2297        let mut delta = 0;
2298        let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
2299            while let Some(edit_since) = edits_since.peek() {
2300                // If the edit occurs after a diff hunk, then it does not
2301                // affect that hunk.
2302                if edit_since.old.start > range.end {
2303                    break;
2304                }
2305                // If the edit precedes the diff hunk, then adjust the hunk
2306                // to reflect the edit.
2307                else if edit_since.old.end < range.start {
2308                    delta += edit_since.new_len() as i64 - edit_since.old_len() as i64;
2309                    edits_since.next();
2310                }
2311                // If the edit intersects a diff hunk, then discard that hunk.
2312                else {
2313                    return None;
2314                }
2315            }
2316
2317            let start = (range.start as i64 + delta) as usize;
2318            let end = (range.end as i64 + delta) as usize;
2319            Some((start..end, new_text))
2320        });
2321
2322        self.start_transaction();
2323        self.text.set_line_ending(diff.line_ending);
2324        self.edit(adjusted_edits, None, cx);
2325        self.end_transaction(cx)
2326    }
2327
2328    pub fn has_unsaved_edits(&self) -> bool {
2329        let (last_version, has_unsaved_edits) = self.has_unsaved_edits.take();
2330
2331        if last_version == self.version {
2332            self.has_unsaved_edits
2333                .set((last_version, has_unsaved_edits));
2334            return has_unsaved_edits;
2335        }
2336
2337        let has_edits = self.has_edits_since(&self.saved_version);
2338        self.has_unsaved_edits
2339            .set((self.version.clone(), has_edits));
2340        has_edits
2341    }
2342
2343    /// Checks if the buffer has unsaved changes.
2344    pub fn is_dirty(&self) -> bool {
2345        if self.capability == Capability::ReadOnly {
2346            return false;
2347        }
2348        if self.has_conflict {
2349            return true;
2350        }
2351        match self.file.as_ref().map(|f| f.disk_state()) {
2352            Some(DiskState::New) | Some(DiskState::Deleted) => {
2353                !self.is_empty() && self.has_unsaved_edits()
2354            }
2355            _ => self.has_unsaved_edits(),
2356        }
2357    }
2358
2359    /// Marks the buffer as having a conflict regardless of current buffer state.
2360    pub fn set_conflict(&mut self) {
2361        self.has_conflict = true;
2362    }
2363
2364    /// Checks if the buffer and its file have both changed since the buffer
2365    /// was last saved or reloaded.
2366    pub fn has_conflict(&self) -> bool {
2367        if self.has_conflict {
2368            return true;
2369        }
2370        let Some(file) = self.file.as_ref() else {
2371            return false;
2372        };
2373        match file.disk_state() {
2374            DiskState::New => false,
2375            DiskState::Present { mtime } => match self.saved_mtime {
2376                Some(saved_mtime) => {
2377                    mtime.bad_is_greater_than(saved_mtime) && self.has_unsaved_edits()
2378                }
2379                None => true,
2380            },
2381            DiskState::Deleted => false,
2382            DiskState::Historic { .. } => false,
2383        }
2384    }
2385
2386    /// Gets a [`Subscription`] that tracks all of the changes to the buffer's text.
2387    pub fn subscribe(&mut self) -> Subscription<usize> {
2388        self.text.subscribe()
2389    }
2390
2391    /// Adds a bit to the list of bits that are set when the buffer's text changes.
2392    ///
2393    /// This allows downstream code to check if the buffer's text has changed without
2394    /// waiting for an effect cycle, which would be required if using eents.
2395    pub fn record_changes(&mut self, bit: rc::Weak<Cell<bool>>) {
2396        if let Err(ix) = self
2397            .change_bits
2398            .binary_search_by_key(&rc::Weak::as_ptr(&bit), rc::Weak::as_ptr)
2399        {
2400            self.change_bits.insert(ix, bit);
2401        }
2402    }
2403
2404    /// Set the change bit for all "listeners".
2405    fn was_changed(&mut self) {
2406        self.change_bits.retain(|change_bit| {
2407            change_bit
2408                .upgrade()
2409                .inspect(|bit| {
2410                    _ = bit.replace(true);
2411                })
2412                .is_some()
2413        });
2414    }
2415
2416    /// Starts a transaction, if one is not already in-progress. When undoing or
2417    /// redoing edits, all of the edits performed within a transaction are undone
2418    /// or redone together.
2419    pub fn start_transaction(&mut self) -> Option<TransactionId> {
2420        self.start_transaction_at(Instant::now())
2421    }
2422
2423    /// Starts a transaction, providing the current time. Subsequent transactions
2424    /// that occur within a short period of time will be grouped together. This
2425    /// is controlled by the buffer's undo grouping duration.
2426    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
2427        self.transaction_depth += 1;
2428        if self.was_dirty_before_starting_transaction.is_none() {
2429            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
2430        }
2431        self.text.start_transaction_at(now)
2432    }
2433
2434    /// Terminates the current transaction, if this is the outermost transaction.
2435    pub fn end_transaction(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
2436        self.end_transaction_at(Instant::now(), cx)
2437    }
2438
2439    /// Terminates the current transaction, providing the current time. Subsequent transactions
2440    /// that occur within a short period of time will be grouped together. This
2441    /// is controlled by the buffer's undo grouping duration.
2442    pub fn end_transaction_at(
2443        &mut self,
2444        now: Instant,
2445        cx: &mut Context<Self>,
2446    ) -> Option<TransactionId> {
2447        assert!(self.transaction_depth > 0);
2448        self.transaction_depth -= 1;
2449        let was_dirty = if self.transaction_depth == 0 {
2450            self.was_dirty_before_starting_transaction.take().unwrap()
2451        } else {
2452            false
2453        };
2454        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
2455            self.did_edit(&start_version, was_dirty, cx);
2456            Some(transaction_id)
2457        } else {
2458            None
2459        }
2460    }
2461
2462    /// Manually add a transaction to the buffer's undo history.
2463    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
2464        self.text.push_transaction(transaction, now);
2465    }
2466
2467    /// Differs from `push_transaction` in that it does not clear the redo
2468    /// stack. Intended to be used to create a parent transaction to merge
2469    /// potential child transactions into.
2470    ///
2471    /// The caller is responsible for removing it from the undo history using
2472    /// `forget_transaction` if no edits are merged into it. Otherwise, if edits
2473    /// are merged into this transaction, the caller is responsible for ensuring
2474    /// the redo stack is cleared. The easiest way to ensure the redo stack is
2475    /// cleared is to create transactions with the usual `start_transaction` and
2476    /// `end_transaction` methods and merging the resulting transactions into
2477    /// the transaction created by this method
2478    pub fn push_empty_transaction(&mut self, now: Instant) -> TransactionId {
2479        self.text.push_empty_transaction(now)
2480    }
2481
2482    /// Prevent the last transaction from being grouped with any subsequent transactions,
2483    /// even if they occur with the buffer's undo grouping duration.
2484    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
2485        self.text.finalize_last_transaction()
2486    }
2487
2488    /// Manually group all changes since a given transaction.
2489    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
2490        self.text.group_until_transaction(transaction_id);
2491    }
2492
2493    /// Manually remove a transaction from the buffer's undo history
2494    pub fn forget_transaction(&mut self, transaction_id: TransactionId) -> Option<Transaction> {
2495        self.text.forget_transaction(transaction_id)
2496    }
2497
2498    /// Retrieve a transaction from the buffer's undo history
2499    pub fn get_transaction(&self, transaction_id: TransactionId) -> Option<&Transaction> {
2500        self.text.get_transaction(transaction_id)
2501    }
2502
2503    /// Manually merge two transactions in the buffer's undo history.
2504    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
2505        self.text.merge_transactions(transaction, destination);
2506    }
2507
2508    /// Waits for the buffer to receive operations with the given timestamps.
2509    pub fn wait_for_edits<It: IntoIterator<Item = clock::Lamport>>(
2510        &mut self,
2511        edit_ids: It,
2512    ) -> impl Future<Output = Result<()>> + use<It> {
2513        self.text.wait_for_edits(edit_ids)
2514    }
2515
2516    /// Waits for the buffer to receive the operations necessary for resolving the given anchors.
2517    pub fn wait_for_anchors<It: IntoIterator<Item = Anchor>>(
2518        &mut self,
2519        anchors: It,
2520    ) -> impl 'static + Future<Output = Result<()>> + use<It> {
2521        self.text.wait_for_anchors(anchors)
2522    }
2523
2524    /// Waits for the buffer to receive operations up to the given version.
2525    pub fn wait_for_version(
2526        &mut self,
2527        version: clock::Global,
2528    ) -> impl Future<Output = Result<()>> + use<> {
2529        self.text.wait_for_version(version)
2530    }
2531
2532    /// Forces all futures returned by [`Buffer::wait_for_version`], [`Buffer::wait_for_edits`], or
2533    /// [`Buffer::wait_for_version`] to resolve with an error.
2534    pub fn give_up_waiting(&mut self) {
2535        self.text.give_up_waiting();
2536    }
2537
2538    pub fn wait_for_autoindent_applied(&mut self) -> Option<oneshot::Receiver<()>> {
2539        let mut rx = None;
2540        if !self.autoindent_requests.is_empty() {
2541            let channel = oneshot::channel();
2542            self.wait_for_autoindent_txs.push(channel.0);
2543            rx = Some(channel.1);
2544        }
2545        rx
2546    }
2547
2548    /// Stores a set of selections that should be broadcasted to all of the buffer's replicas.
2549    pub fn set_active_selections(
2550        &mut self,
2551        selections: Arc<[Selection<Anchor>]>,
2552        line_mode: bool,
2553        cursor_shape: CursorShape,
2554        cx: &mut Context<Self>,
2555    ) {
2556        let lamport_timestamp = self.text.lamport_clock.tick();
2557        self.remote_selections.insert(
2558            self.text.replica_id(),
2559            SelectionSet {
2560                selections: selections.clone(),
2561                lamport_timestamp,
2562                line_mode,
2563                cursor_shape,
2564            },
2565        );
2566        self.send_operation(
2567            Operation::UpdateSelections {
2568                selections,
2569                line_mode,
2570                lamport_timestamp,
2571                cursor_shape,
2572            },
2573            true,
2574            cx,
2575        );
2576        self.non_text_state_update_count += 1;
2577        cx.notify();
2578    }
2579
2580    /// Clears the selections, so that other replicas of the buffer do not see any selections for
2581    /// this replica.
2582    pub fn remove_active_selections(&mut self, cx: &mut Context<Self>) {
2583        if self
2584            .remote_selections
2585            .get(&self.text.replica_id())
2586            .is_none_or(|set| !set.selections.is_empty())
2587        {
2588            self.set_active_selections(Arc::default(), false, Default::default(), cx);
2589        }
2590    }
2591
2592    pub fn set_agent_selections(
2593        &mut self,
2594        selections: Arc<[Selection<Anchor>]>,
2595        line_mode: bool,
2596        cursor_shape: CursorShape,
2597        cx: &mut Context<Self>,
2598    ) {
2599        let lamport_timestamp = self.text.lamport_clock.tick();
2600        self.remote_selections.insert(
2601            ReplicaId::AGENT,
2602            SelectionSet {
2603                selections,
2604                lamport_timestamp,
2605                line_mode,
2606                cursor_shape,
2607            },
2608        );
2609        self.non_text_state_update_count += 1;
2610        cx.notify();
2611    }
2612
2613    pub fn remove_agent_selections(&mut self, cx: &mut Context<Self>) {
2614        self.set_agent_selections(Arc::default(), false, Default::default(), cx);
2615    }
2616
2617    /// Replaces the buffer's entire text.
2618    pub fn set_text<T>(&mut self, text: T, cx: &mut Context<Self>) -> Option<clock::Lamport>
2619    where
2620        T: Into<Arc<str>>,
2621    {
2622        self.autoindent_requests.clear();
2623        self.edit([(0..self.len(), text)], None, cx)
2624    }
2625
2626    /// Appends the given text to the end of the buffer.
2627    pub fn append<T>(&mut self, text: T, cx: &mut Context<Self>) -> Option<clock::Lamport>
2628    where
2629        T: Into<Arc<str>>,
2630    {
2631        self.edit([(self.len()..self.len(), text)], None, cx)
2632    }
2633
2634    /// Applies the given edits to the buffer. Each edit is specified as a range of text to
2635    /// delete, and a string of text to insert at that location. Adjacent edits are coalesced.
2636    ///
2637    /// If an [`AutoindentMode`] is provided, then the buffer will enqueue an auto-indent
2638    /// request for the edited ranges, which will be processed when the buffer finishes
2639    /// parsing.
2640    ///
2641    /// Parsing takes place at the end of a transaction, and may compute synchronously
2642    /// or asynchronously, depending on the changes.
2643    pub fn edit<I, S, T>(
2644        &mut self,
2645        edits_iter: I,
2646        autoindent_mode: Option<AutoindentMode>,
2647        cx: &mut Context<Self>,
2648    ) -> Option<clock::Lamport>
2649    where
2650        I: IntoIterator<Item = (Range<S>, T)>,
2651        S: ToOffset,
2652        T: Into<Arc<str>>,
2653    {
2654        self.edit_internal(edits_iter, autoindent_mode, true, cx)
2655    }
2656
2657    /// Like [`edit`](Self::edit), but does not coalesce adjacent edits.
2658    pub fn edit_non_coalesce<I, S, T>(
2659        &mut self,
2660        edits_iter: I,
2661        autoindent_mode: Option<AutoindentMode>,
2662        cx: &mut Context<Self>,
2663    ) -> Option<clock::Lamport>
2664    where
2665        I: IntoIterator<Item = (Range<S>, T)>,
2666        S: ToOffset,
2667        T: Into<Arc<str>>,
2668    {
2669        self.edit_internal(edits_iter, autoindent_mode, false, cx)
2670    }
2671
2672    fn edit_internal<I, S, T>(
2673        &mut self,
2674        edits_iter: I,
2675        autoindent_mode: Option<AutoindentMode>,
2676        coalesce_adjacent: bool,
2677        cx: &mut Context<Self>,
2678    ) -> Option<clock::Lamport>
2679    where
2680        I: IntoIterator<Item = (Range<S>, T)>,
2681        S: ToOffset,
2682        T: Into<Arc<str>>,
2683    {
2684        // Skip invalid edits and coalesce contiguous ones.
2685        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
2686
2687        for (range, new_text) in edits_iter {
2688            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
2689
2690            if range.start > range.end {
2691                mem::swap(&mut range.start, &mut range.end);
2692            }
2693            let new_text = new_text.into();
2694            if !new_text.is_empty() || !range.is_empty() {
2695                let prev_edit = edits.last_mut();
2696                let should_coalesce = prev_edit.as_ref().is_some_and(|(prev_range, _)| {
2697                    if coalesce_adjacent {
2698                        prev_range.end >= range.start
2699                    } else {
2700                        prev_range.end > range.start
2701                    }
2702                });
2703
2704                if let Some((prev_range, prev_text)) = prev_edit
2705                    && should_coalesce
2706                {
2707                    prev_range.end = cmp::max(prev_range.end, range.end);
2708                    *prev_text = format!("{prev_text}{new_text}").into();
2709                } else {
2710                    edits.push((range, new_text));
2711                }
2712            }
2713        }
2714        if edits.is_empty() {
2715            return None;
2716        }
2717
2718        self.start_transaction();
2719        self.pending_autoindent.take();
2720        let autoindent_request = autoindent_mode
2721            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
2722
2723        let edit_operation = self.text.edit(edits.iter().cloned());
2724        let edit_id = edit_operation.timestamp();
2725
2726        if let Some((before_edit, mode)) = autoindent_request {
2727            let mut delta = 0isize;
2728            let mut previous_setting = None;
2729            let entries: Vec<_> = edits
2730                .into_iter()
2731                .enumerate()
2732                .zip(&edit_operation.as_edit().unwrap().new_text)
2733                .filter(|((_, (range, _)), _)| {
2734                    let language = before_edit.language_at(range.start);
2735                    let language_id = language.map(|l| l.id());
2736                    if let Some((cached_language_id, auto_indent)) = previous_setting
2737                        && cached_language_id == language_id
2738                    {
2739                        auto_indent
2740                    } else {
2741                        // The auto-indent setting is not present in editorconfigs, hence
2742                        // we can avoid passing the file here.
2743                        let auto_indent =
2744                            language_settings(language.map(|l| l.name()), None, cx).auto_indent;
2745                        previous_setting = Some((language_id, auto_indent));
2746                        auto_indent
2747                    }
2748                })
2749                .map(|((ix, (range, _)), new_text)| {
2750                    let new_text_length = new_text.len();
2751                    let old_start = range.start.to_point(&before_edit);
2752                    let new_start = (delta + range.start as isize) as usize;
2753                    let range_len = range.end - range.start;
2754                    delta += new_text_length as isize - range_len as isize;
2755
2756                    // Decide what range of the insertion to auto-indent, and whether
2757                    // the first line of the insertion should be considered a newly-inserted line
2758                    // or an edit to an existing line.
2759                    let mut range_of_insertion_to_indent = 0..new_text_length;
2760                    let mut first_line_is_new = true;
2761
2762                    let old_line_start = before_edit.indent_size_for_line(old_start.row).len;
2763                    let old_line_end = before_edit.line_len(old_start.row);
2764
2765                    if old_start.column > old_line_start {
2766                        first_line_is_new = false;
2767                    }
2768
2769                    if !new_text.contains('\n')
2770                        && (old_start.column + (range_len as u32) < old_line_end
2771                            || old_line_end == old_line_start)
2772                    {
2773                        first_line_is_new = false;
2774                    }
2775
2776                    // When inserting text starting with a newline, avoid auto-indenting the
2777                    // previous line.
2778                    if new_text.starts_with('\n') {
2779                        range_of_insertion_to_indent.start += 1;
2780                        first_line_is_new = true;
2781                    }
2782
2783                    let mut original_indent_column = None;
2784                    if let AutoindentMode::Block {
2785                        original_indent_columns,
2786                    } = &mode
2787                    {
2788                        original_indent_column = Some(if new_text.starts_with('\n') {
2789                            indent_size_for_text(
2790                                new_text[range_of_insertion_to_indent.clone()].chars(),
2791                            )
2792                            .len
2793                        } else {
2794                            original_indent_columns
2795                                .get(ix)
2796                                .copied()
2797                                .flatten()
2798                                .unwrap_or_else(|| {
2799                                    indent_size_for_text(
2800                                        new_text[range_of_insertion_to_indent.clone()].chars(),
2801                                    )
2802                                    .len
2803                                })
2804                        });
2805
2806                        // Avoid auto-indenting the line after the edit.
2807                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
2808                            range_of_insertion_to_indent.end -= 1;
2809                        }
2810                    }
2811
2812                    AutoindentRequestEntry {
2813                        original_indent_column,
2814                        old_row: if first_line_is_new {
2815                            None
2816                        } else {
2817                            Some(old_start.row)
2818                        },
2819                        indent_size: before_edit.language_indent_size_at(range.start, cx),
2820                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
2821                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
2822                    }
2823                })
2824                .collect();
2825
2826            if !entries.is_empty() {
2827                self.autoindent_requests.push(Arc::new(AutoindentRequest {
2828                    before_edit,
2829                    entries,
2830                    is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
2831                    ignore_empty_lines: false,
2832                }));
2833            }
2834        }
2835
2836        self.end_transaction(cx);
2837        self.send_operation(Operation::Buffer(edit_operation), true, cx);
2838        Some(edit_id)
2839    }
2840
2841    fn did_edit(&mut self, old_version: &clock::Global, was_dirty: bool, cx: &mut Context<Self>) {
2842        self.was_changed();
2843
2844        if self.edits_since::<usize>(old_version).next().is_none() {
2845            return;
2846        }
2847
2848        self.reparse(cx, true);
2849        cx.emit(BufferEvent::Edited);
2850        if was_dirty != self.is_dirty() {
2851            cx.emit(BufferEvent::DirtyChanged);
2852        }
2853        cx.notify();
2854    }
2855
2856    pub fn autoindent_ranges<I, T>(&mut self, ranges: I, cx: &mut Context<Self>)
2857    where
2858        I: IntoIterator<Item = Range<T>>,
2859        T: ToOffset + Copy,
2860    {
2861        let before_edit = self.snapshot();
2862        let entries = ranges
2863            .into_iter()
2864            .map(|range| AutoindentRequestEntry {
2865                range: before_edit.anchor_before(range.start)..before_edit.anchor_after(range.end),
2866                old_row: None,
2867                indent_size: before_edit.language_indent_size_at(range.start, cx),
2868                original_indent_column: None,
2869            })
2870            .collect();
2871        self.autoindent_requests.push(Arc::new(AutoindentRequest {
2872            before_edit,
2873            entries,
2874            is_block_mode: false,
2875            ignore_empty_lines: true,
2876        }));
2877        self.request_autoindent(cx, Some(Duration::from_micros(300)));
2878    }
2879
2880    // Inserts newlines at the given position to create an empty line, returning the start of the new line.
2881    // You can also request the insertion of empty lines above and below the line starting at the returned point.
2882    pub fn insert_empty_line(
2883        &mut self,
2884        position: impl ToPoint,
2885        space_above: bool,
2886        space_below: bool,
2887        cx: &mut Context<Self>,
2888    ) -> Point {
2889        let mut position = position.to_point(self);
2890
2891        self.start_transaction();
2892
2893        self.edit(
2894            [(position..position, "\n")],
2895            Some(AutoindentMode::EachLine),
2896            cx,
2897        );
2898
2899        if position.column > 0 {
2900            position += Point::new(1, 0);
2901        }
2902
2903        if !self.is_line_blank(position.row) {
2904            self.edit(
2905                [(position..position, "\n")],
2906                Some(AutoindentMode::EachLine),
2907                cx,
2908            );
2909        }
2910
2911        if space_above && position.row > 0 && !self.is_line_blank(position.row - 1) {
2912            self.edit(
2913                [(position..position, "\n")],
2914                Some(AutoindentMode::EachLine),
2915                cx,
2916            );
2917            position.row += 1;
2918        }
2919
2920        if space_below
2921            && (position.row == self.max_point().row || !self.is_line_blank(position.row + 1))
2922        {
2923            self.edit(
2924                [(position..position, "\n")],
2925                Some(AutoindentMode::EachLine),
2926                cx,
2927            );
2928        }
2929
2930        self.end_transaction(cx);
2931
2932        position
2933    }
2934
2935    /// Applies the given remote operations to the buffer.
2936    pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I, cx: &mut Context<Self>) {
2937        self.pending_autoindent.take();
2938        let was_dirty = self.is_dirty();
2939        let old_version = self.version.clone();
2940        let mut deferred_ops = Vec::new();
2941        let buffer_ops = ops
2942            .into_iter()
2943            .filter_map(|op| match op {
2944                Operation::Buffer(op) => Some(op),
2945                _ => {
2946                    if self.can_apply_op(&op) {
2947                        self.apply_op(op, cx);
2948                    } else {
2949                        deferred_ops.push(op);
2950                    }
2951                    None
2952                }
2953            })
2954            .collect::<Vec<_>>();
2955        for operation in buffer_ops.iter() {
2956            self.send_operation(Operation::Buffer(operation.clone()), false, cx);
2957        }
2958        self.text.apply_ops(buffer_ops);
2959        self.deferred_ops.insert(deferred_ops);
2960        self.flush_deferred_ops(cx);
2961        self.did_edit(&old_version, was_dirty, cx);
2962        // Notify independently of whether the buffer was edited as the operations could include a
2963        // selection update.
2964        cx.notify();
2965    }
2966
2967    fn flush_deferred_ops(&mut self, cx: &mut Context<Self>) {
2968        let mut deferred_ops = Vec::new();
2969        for op in self.deferred_ops.drain().iter().cloned() {
2970            if self.can_apply_op(&op) {
2971                self.apply_op(op, cx);
2972            } else {
2973                deferred_ops.push(op);
2974            }
2975        }
2976        self.deferred_ops.insert(deferred_ops);
2977    }
2978
2979    pub fn has_deferred_ops(&self) -> bool {
2980        !self.deferred_ops.is_empty() || self.text.has_deferred_ops()
2981    }
2982
2983    fn can_apply_op(&self, operation: &Operation) -> bool {
2984        match operation {
2985            Operation::Buffer(_) => {
2986                unreachable!("buffer operations should never be applied at this layer")
2987            }
2988            Operation::UpdateDiagnostics {
2989                diagnostics: diagnostic_set,
2990                ..
2991            } => diagnostic_set.iter().all(|diagnostic| {
2992                self.text.can_resolve(&diagnostic.range.start)
2993                    && self.text.can_resolve(&diagnostic.range.end)
2994            }),
2995            Operation::UpdateSelections { selections, .. } => selections
2996                .iter()
2997                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
2998            Operation::UpdateCompletionTriggers { .. } | Operation::UpdateLineEnding { .. } => true,
2999        }
3000    }
3001
3002    fn apply_op(&mut self, operation: Operation, cx: &mut Context<Self>) {
3003        match operation {
3004            Operation::Buffer(_) => {
3005                unreachable!("buffer operations should never be applied at this layer")
3006            }
3007            Operation::UpdateDiagnostics {
3008                server_id,
3009                diagnostics: diagnostic_set,
3010                lamport_timestamp,
3011            } => {
3012                let snapshot = self.snapshot();
3013                self.apply_diagnostic_update(
3014                    server_id,
3015                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
3016                    lamport_timestamp,
3017                    cx,
3018                );
3019            }
3020            Operation::UpdateSelections {
3021                selections,
3022                lamport_timestamp,
3023                line_mode,
3024                cursor_shape,
3025            } => {
3026                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id)
3027                    && set.lamport_timestamp > lamport_timestamp
3028                {
3029                    return;
3030                }
3031
3032                self.remote_selections.insert(
3033                    lamport_timestamp.replica_id,
3034                    SelectionSet {
3035                        selections,
3036                        lamport_timestamp,
3037                        line_mode,
3038                        cursor_shape,
3039                    },
3040                );
3041                self.text.lamport_clock.observe(lamport_timestamp);
3042                self.non_text_state_update_count += 1;
3043            }
3044            Operation::UpdateCompletionTriggers {
3045                triggers,
3046                lamport_timestamp,
3047                server_id,
3048            } => {
3049                if triggers.is_empty() {
3050                    self.completion_triggers_per_language_server
3051                        .remove(&server_id);
3052                    self.completion_triggers = self
3053                        .completion_triggers_per_language_server
3054                        .values()
3055                        .flat_map(|triggers| triggers.iter().cloned())
3056                        .collect();
3057                } else {
3058                    self.completion_triggers_per_language_server
3059                        .insert(server_id, triggers.iter().cloned().collect());
3060                    self.completion_triggers.extend(triggers);
3061                }
3062                self.text.lamport_clock.observe(lamport_timestamp);
3063            }
3064            Operation::UpdateLineEnding {
3065                line_ending,
3066                lamport_timestamp,
3067            } => {
3068                self.text.set_line_ending(line_ending);
3069                self.text.lamport_clock.observe(lamport_timestamp);
3070            }
3071        }
3072    }
3073
3074    fn apply_diagnostic_update(
3075        &mut self,
3076        server_id: LanguageServerId,
3077        diagnostics: DiagnosticSet,
3078        lamport_timestamp: clock::Lamport,
3079        cx: &mut Context<Self>,
3080    ) {
3081        if lamport_timestamp > self.diagnostics_timestamp {
3082            if diagnostics.is_empty() {
3083                self.diagnostics.remove(&server_id);
3084            } else {
3085                self.diagnostics.insert(server_id, diagnostics);
3086            }
3087            self.diagnostics_timestamp = lamport_timestamp;
3088            self.non_text_state_update_count += 1;
3089            self.text.lamport_clock.observe(lamport_timestamp);
3090            cx.notify();
3091            cx.emit(BufferEvent::DiagnosticsUpdated);
3092        }
3093    }
3094
3095    fn send_operation(&mut self, operation: Operation, is_local: bool, cx: &mut Context<Self>) {
3096        self.was_changed();
3097        cx.emit(BufferEvent::Operation {
3098            operation,
3099            is_local,
3100        });
3101    }
3102
3103    /// Removes the selections for a given peer.
3104    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut Context<Self>) {
3105        self.remote_selections.remove(&replica_id);
3106        cx.notify();
3107    }
3108
3109    /// Undoes the most recent transaction.
3110    pub fn undo(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
3111        let was_dirty = self.is_dirty();
3112        let old_version = self.version.clone();
3113
3114        if let Some((transaction_id, operation)) = self.text.undo() {
3115            self.send_operation(Operation::Buffer(operation), true, cx);
3116            self.did_edit(&old_version, was_dirty, cx);
3117            self.restore_encoding_for_transaction(transaction_id, was_dirty);
3118            Some(transaction_id)
3119        } else {
3120            None
3121        }
3122    }
3123
3124    /// Manually undoes a specific transaction in the buffer's undo history.
3125    pub fn undo_transaction(
3126        &mut self,
3127        transaction_id: TransactionId,
3128        cx: &mut Context<Self>,
3129    ) -> bool {
3130        let was_dirty = self.is_dirty();
3131        let old_version = self.version.clone();
3132        if let Some(operation) = self.text.undo_transaction(transaction_id) {
3133            self.send_operation(Operation::Buffer(operation), true, cx);
3134            self.did_edit(&old_version, was_dirty, cx);
3135            true
3136        } else {
3137            false
3138        }
3139    }
3140
3141    /// Manually undoes all changes after a given transaction in the buffer's undo history.
3142    pub fn undo_to_transaction(
3143        &mut self,
3144        transaction_id: TransactionId,
3145        cx: &mut Context<Self>,
3146    ) -> bool {
3147        let was_dirty = self.is_dirty();
3148        let old_version = self.version.clone();
3149
3150        let operations = self.text.undo_to_transaction(transaction_id);
3151        let undone = !operations.is_empty();
3152        for operation in operations {
3153            self.send_operation(Operation::Buffer(operation), true, cx);
3154        }
3155        if undone {
3156            self.did_edit(&old_version, was_dirty, cx)
3157        }
3158        undone
3159    }
3160
3161    pub fn undo_operations(&mut self, counts: HashMap<Lamport, u32>, cx: &mut Context<Buffer>) {
3162        let was_dirty = self.is_dirty();
3163        let operation = self.text.undo_operations(counts);
3164        let old_version = self.version.clone();
3165        self.send_operation(Operation::Buffer(operation), true, cx);
3166        self.did_edit(&old_version, was_dirty, cx);
3167    }
3168
3169    /// Manually redoes a specific transaction in the buffer's redo history.
3170    pub fn redo(&mut self, cx: &mut Context<Self>) -> Option<TransactionId> {
3171        let was_dirty = self.is_dirty();
3172        let old_version = self.version.clone();
3173
3174        if let Some((transaction_id, operation)) = self.text.redo() {
3175            self.send_operation(Operation::Buffer(operation), true, cx);
3176            self.did_edit(&old_version, was_dirty, cx);
3177            self.restore_encoding_for_transaction(transaction_id, was_dirty);
3178            Some(transaction_id)
3179        } else {
3180            None
3181        }
3182    }
3183
3184    fn restore_encoding_for_transaction(&mut self, transaction_id: TransactionId, was_dirty: bool) {
3185        if let Some((old_encoding, old_has_bom)) =
3186            self.reload_with_encoding_txns.get(&transaction_id)
3187        {
3188            let current_encoding = self.encoding;
3189            let current_has_bom = self.has_bom;
3190            self.encoding = *old_encoding;
3191            self.has_bom = *old_has_bom;
3192            if !was_dirty {
3193                self.saved_version = self.version.clone();
3194                self.has_unsaved_edits
3195                    .set((self.saved_version.clone(), false));
3196            }
3197            self.reload_with_encoding_txns
3198                .insert(transaction_id, (current_encoding, current_has_bom));
3199        }
3200    }
3201
3202    /// Manually undoes all changes until a given transaction in the buffer's redo history.
3203    pub fn redo_to_transaction(
3204        &mut self,
3205        transaction_id: TransactionId,
3206        cx: &mut Context<Self>,
3207    ) -> bool {
3208        let was_dirty = self.is_dirty();
3209        let old_version = self.version.clone();
3210
3211        let operations = self.text.redo_to_transaction(transaction_id);
3212        let redone = !operations.is_empty();
3213        for operation in operations {
3214            self.send_operation(Operation::Buffer(operation), true, cx);
3215        }
3216        if redone {
3217            self.did_edit(&old_version, was_dirty, cx)
3218        }
3219        redone
3220    }
3221
3222    /// Override current completion triggers with the user-provided completion triggers.
3223    pub fn set_completion_triggers(
3224        &mut self,
3225        server_id: LanguageServerId,
3226        triggers: BTreeSet<String>,
3227        cx: &mut Context<Self>,
3228    ) {
3229        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
3230        if triggers.is_empty() {
3231            self.completion_triggers_per_language_server
3232                .remove(&server_id);
3233            self.completion_triggers = self
3234                .completion_triggers_per_language_server
3235                .values()
3236                .flat_map(|triggers| triggers.iter().cloned())
3237                .collect();
3238        } else {
3239            self.completion_triggers_per_language_server
3240                .insert(server_id, triggers.clone());
3241            self.completion_triggers.extend(triggers.iter().cloned());
3242        }
3243        self.send_operation(
3244            Operation::UpdateCompletionTriggers {
3245                triggers: triggers.into_iter().collect(),
3246                lamport_timestamp: self.completion_triggers_timestamp,
3247                server_id,
3248            },
3249            true,
3250            cx,
3251        );
3252        cx.notify();
3253    }
3254
3255    /// Returns a list of strings which trigger a completion menu for this language.
3256    /// Usually this is driven by LSP server which returns a list of trigger characters for completions.
3257    pub fn completion_triggers(&self) -> &BTreeSet<String> {
3258        &self.completion_triggers
3259    }
3260
3261    /// Call this directly after performing edits to prevent the preview tab
3262    /// from being dismissed by those edits. It causes `should_dismiss_preview`
3263    /// to return false until there are additional edits.
3264    pub fn refresh_preview(&mut self) {
3265        self.preview_version = self.version.clone();
3266    }
3267
3268    /// Whether we should preserve the preview status of a tab containing this buffer.
3269    pub fn preserve_preview(&self) -> bool {
3270        !self.has_edits_since(&self.preview_version)
3271    }
3272}
3273
3274#[doc(hidden)]
3275#[cfg(any(test, feature = "test-support"))]
3276impl Buffer {
3277    pub fn edit_via_marked_text(
3278        &mut self,
3279        marked_string: &str,
3280        autoindent_mode: Option<AutoindentMode>,
3281        cx: &mut Context<Self>,
3282    ) {
3283        let edits = self.edits_for_marked_text(marked_string);
3284        self.edit(edits, autoindent_mode, cx);
3285    }
3286
3287    pub fn set_group_interval(&mut self, group_interval: Duration) {
3288        self.text.set_group_interval(group_interval);
3289    }
3290
3291    pub fn randomly_edit<T>(&mut self, rng: &mut T, old_range_count: usize, cx: &mut Context<Self>)
3292    where
3293        T: rand::Rng,
3294    {
3295        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
3296        let mut last_end = None;
3297        for _ in 0..old_range_count {
3298            if last_end.is_some_and(|last_end| last_end >= self.len()) {
3299                break;
3300            }
3301
3302            let new_start = last_end.map_or(0, |last_end| last_end + 1);
3303            let mut range = self.random_byte_range(new_start, rng);
3304            if rng.random_bool(0.2) {
3305                mem::swap(&mut range.start, &mut range.end);
3306            }
3307            last_end = Some(range.end);
3308
3309            let new_text_len = rng.random_range(0..10);
3310            let mut new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
3311            new_text = new_text.to_uppercase();
3312
3313            edits.push((range, new_text));
3314        }
3315        log::info!("mutating buffer {:?} with {:?}", self.replica_id(), edits);
3316        self.edit(edits, None, cx);
3317    }
3318
3319    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut Context<Self>) {
3320        let was_dirty = self.is_dirty();
3321        let old_version = self.version.clone();
3322
3323        let ops = self.text.randomly_undo_redo(rng);
3324        if !ops.is_empty() {
3325            for op in ops {
3326                self.send_operation(Operation::Buffer(op), true, cx);
3327                self.did_edit(&old_version, was_dirty, cx);
3328            }
3329        }
3330    }
3331}
3332
3333impl EventEmitter<BufferEvent> for Buffer {}
3334
3335impl Deref for Buffer {
3336    type Target = TextBuffer;
3337
3338    fn deref(&self) -> &Self::Target {
3339        &self.text
3340    }
3341}
3342
3343impl BufferSnapshot {
3344    /// Returns [`IndentSize`] for a given line that respects user settings and
3345    /// language preferences.
3346    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
3347        indent_size_for_line(self, row)
3348    }
3349
3350    /// Returns [`IndentSize`] for a given position that respects user settings
3351    /// and language preferences.
3352    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &App) -> IndentSize {
3353        let settings = language_settings(
3354            self.language_at(position).map(|l| l.name()),
3355            self.file(),
3356            cx,
3357        );
3358        if settings.hard_tabs {
3359            IndentSize::tab()
3360        } else {
3361            IndentSize::spaces(settings.tab_size.get())
3362        }
3363    }
3364
3365    /// Retrieve the suggested indent size for all of the given rows. The unit of indentation
3366    /// is passed in as `single_indent_size`.
3367    pub fn suggested_indents(
3368        &self,
3369        rows: impl Iterator<Item = u32>,
3370        single_indent_size: IndentSize,
3371    ) -> BTreeMap<u32, IndentSize> {
3372        let mut result = BTreeMap::new();
3373
3374        for row_range in contiguous_ranges(rows, 10) {
3375            let suggestions = match self.suggest_autoindents(row_range.clone()) {
3376                Some(suggestions) => suggestions,
3377                _ => break,
3378            };
3379
3380            for (row, suggestion) in row_range.zip(suggestions) {
3381                let indent_size = if let Some(suggestion) = suggestion {
3382                    result
3383                        .get(&suggestion.basis_row)
3384                        .copied()
3385                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
3386                        .with_delta(suggestion.delta, single_indent_size)
3387                } else {
3388                    self.indent_size_for_line(row)
3389                };
3390
3391                result.insert(row, indent_size);
3392            }
3393        }
3394
3395        result
3396    }
3397
3398    fn suggest_autoindents(
3399        &self,
3400        row_range: Range<u32>,
3401    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
3402        let config = &self.language.as_ref()?.config;
3403        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
3404
3405        #[derive(Debug, Clone)]
3406        struct StartPosition {
3407            start: Point,
3408            suffix: SharedString,
3409            language: Arc<Language>,
3410        }
3411
3412        // Find the suggested indentation ranges based on the syntax tree.
3413        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
3414        let end = Point::new(row_range.end, 0);
3415        let range = (start..end).to_offset(&self.text);
3416        let mut matches = self.syntax.matches_with_options(
3417            range.clone(),
3418            &self.text,
3419            TreeSitterOptions {
3420                max_bytes_to_query: Some(MAX_BYTES_TO_QUERY),
3421                max_start_depth: None,
3422            },
3423            |grammar| Some(&grammar.indents_config.as_ref()?.query),
3424        );
3425        let indent_configs = matches
3426            .grammars()
3427            .iter()
3428            .map(|grammar| grammar.indents_config.as_ref().unwrap())
3429            .collect::<Vec<_>>();
3430
3431        let mut indent_ranges = Vec::<Range<Point>>::new();
3432        let mut start_positions = Vec::<StartPosition>::new();
3433        let mut outdent_positions = Vec::<Point>::new();
3434        while let Some(mat) = matches.peek() {
3435            let mut start: Option<Point> = None;
3436            let mut end: Option<Point> = None;
3437
3438            let config = indent_configs[mat.grammar_index];
3439            for capture in mat.captures {
3440                if capture.index == config.indent_capture_ix {
3441                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
3442                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
3443                } else if Some(capture.index) == config.start_capture_ix {
3444                    start = Some(Point::from_ts_point(capture.node.end_position()));
3445                } else if Some(capture.index) == config.end_capture_ix {
3446                    end = Some(Point::from_ts_point(capture.node.start_position()));
3447                } else if Some(capture.index) == config.outdent_capture_ix {
3448                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
3449                } else if let Some(suffix) = config.suffixed_start_captures.get(&capture.index) {
3450                    start_positions.push(StartPosition {
3451                        start: Point::from_ts_point(capture.node.start_position()),
3452                        suffix: suffix.clone(),
3453                        language: mat.language.clone(),
3454                    });
3455                }
3456            }
3457
3458            matches.advance();
3459            if let Some((start, end)) = start.zip(end) {
3460                if start.row == end.row {
3461                    continue;
3462                }
3463                let range = start..end;
3464                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
3465                    Err(ix) => indent_ranges.insert(ix, range),
3466                    Ok(ix) => {
3467                        let prev_range = &mut indent_ranges[ix];
3468                        prev_range.end = prev_range.end.max(range.end);
3469                    }
3470                }
3471            }
3472        }
3473
3474        let mut error_ranges = Vec::<Range<Point>>::new();
3475        let mut matches = self
3476            .syntax
3477            .matches(range, &self.text, |grammar| grammar.error_query.as_ref());
3478        while let Some(mat) = matches.peek() {
3479            let node = mat.captures[0].node;
3480            let start = Point::from_ts_point(node.start_position());
3481            let end = Point::from_ts_point(node.end_position());
3482            let range = start..end;
3483            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
3484                Ok(ix) | Err(ix) => ix,
3485            };
3486            let mut end_ix = ix;
3487            while let Some(existing_range) = error_ranges.get(end_ix) {
3488                if existing_range.end < end {
3489                    end_ix += 1;
3490                } else {
3491                    break;
3492                }
3493            }
3494            error_ranges.splice(ix..end_ix, [range]);
3495            matches.advance();
3496        }
3497
3498        outdent_positions.sort();
3499        for outdent_position in outdent_positions {
3500            // find the innermost indent range containing this outdent_position
3501            // set its end to the outdent position
3502            if let Some(range_to_truncate) = indent_ranges
3503                .iter_mut()
3504                .rfind(|indent_range| indent_range.contains(&outdent_position))
3505            {
3506                range_to_truncate.end = outdent_position;
3507            }
3508        }
3509
3510        start_positions.sort_by_key(|b| b.start);
3511
3512        // Find the suggested indentation increases and decreased based on regexes.
3513        let mut regex_outdent_map = HashMap::default();
3514        let mut last_seen_suffix: HashMap<String, Vec<StartPosition>> = HashMap::default();
3515        let mut start_positions_iter = start_positions.iter().peekable();
3516
3517        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
3518        self.for_each_line(
3519            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
3520                ..Point::new(row_range.end, 0),
3521            |row, line| {
3522                let indent_len = self.indent_size_for_line(row).len;
3523                let row_language = self.language_at(Point::new(row, indent_len)).cloned();
3524                let row_language_config = row_language
3525                    .as_ref()
3526                    .map(|lang| lang.config())
3527                    .unwrap_or(config);
3528
3529                if row_language_config
3530                    .decrease_indent_pattern
3531                    .as_ref()
3532                    .is_some_and(|regex| regex.is_match(line))
3533                {
3534                    indent_change_rows.push((row, Ordering::Less));
3535                }
3536                if row_language_config
3537                    .increase_indent_pattern
3538                    .as_ref()
3539                    .is_some_and(|regex| regex.is_match(line))
3540                {
3541                    indent_change_rows.push((row + 1, Ordering::Greater));
3542                }
3543                while let Some(pos) = start_positions_iter.peek() {
3544                    if pos.start.row < row {
3545                        let pos = start_positions_iter.next().unwrap().clone();
3546                        last_seen_suffix
3547                            .entry(pos.suffix.to_string())
3548                            .or_default()
3549                            .push(pos);
3550                    } else {
3551                        break;
3552                    }
3553                }
3554                for rule in &row_language_config.decrease_indent_patterns {
3555                    if rule.pattern.as_ref().is_some_and(|r| r.is_match(line)) {
3556                        let row_start_column = self.indent_size_for_line(row).len;
3557                        let basis_row = rule
3558                            .valid_after
3559                            .iter()
3560                            .filter_map(|valid_suffix| last_seen_suffix.get(valid_suffix))
3561                            .flatten()
3562                            .filter(|pos| {
3563                                row_language
3564                                    .as_ref()
3565                                    .or(self.language.as_ref())
3566                                    .is_some_and(|lang| Arc::ptr_eq(lang, &pos.language))
3567                            })
3568                            .filter(|pos| pos.start.column <= row_start_column)
3569                            .max_by_key(|pos| pos.start.row);
3570                        if let Some(outdent_to) = basis_row {
3571                            regex_outdent_map.insert(row, outdent_to.start.row);
3572                        }
3573                        break;
3574                    }
3575                }
3576            },
3577        );
3578
3579        let mut indent_changes = indent_change_rows.into_iter().peekable();
3580        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
3581            prev_non_blank_row.unwrap_or(0)
3582        } else {
3583            row_range.start.saturating_sub(1)
3584        };
3585
3586        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
3587        Some(row_range.map(move |row| {
3588            let row_start = Point::new(row, self.indent_size_for_line(row).len);
3589
3590            let mut indent_from_prev_row = false;
3591            let mut outdent_from_prev_row = false;
3592            let mut outdent_to_row = u32::MAX;
3593            let mut from_regex = false;
3594
3595            while let Some((indent_row, delta)) = indent_changes.peek() {
3596                match indent_row.cmp(&row) {
3597                    Ordering::Equal => match delta {
3598                        Ordering::Less => {
3599                            from_regex = true;
3600                            outdent_from_prev_row = true
3601                        }
3602                        Ordering::Greater => {
3603                            indent_from_prev_row = true;
3604                            from_regex = true
3605                        }
3606                        _ => {}
3607                    },
3608
3609                    Ordering::Greater => break,
3610                    Ordering::Less => {}
3611                }
3612
3613                indent_changes.next();
3614            }
3615
3616            for range in &indent_ranges {
3617                if range.start.row >= row {
3618                    break;
3619                }
3620                if range.start.row == prev_row && range.end > row_start {
3621                    indent_from_prev_row = true;
3622                }
3623                if range.end > prev_row_start && range.end <= row_start {
3624                    outdent_to_row = outdent_to_row.min(range.start.row);
3625                }
3626            }
3627
3628            if let Some(basis_row) = regex_outdent_map.get(&row) {
3629                indent_from_prev_row = false;
3630                outdent_to_row = *basis_row;
3631                from_regex = true;
3632            }
3633
3634            let within_error = error_ranges
3635                .iter()
3636                .any(|e| e.start.row < row && e.end > row_start);
3637
3638            let suggestion = if outdent_to_row == prev_row
3639                || (outdent_from_prev_row && indent_from_prev_row)
3640            {
3641                Some(IndentSuggestion {
3642                    basis_row: prev_row,
3643                    delta: Ordering::Equal,
3644                    within_error: within_error && !from_regex,
3645                })
3646            } else if indent_from_prev_row {
3647                Some(IndentSuggestion {
3648                    basis_row: prev_row,
3649                    delta: Ordering::Greater,
3650                    within_error: within_error && !from_regex,
3651                })
3652            } else if outdent_to_row < prev_row {
3653                Some(IndentSuggestion {
3654                    basis_row: outdent_to_row,
3655                    delta: Ordering::Equal,
3656                    within_error: within_error && !from_regex,
3657                })
3658            } else if outdent_from_prev_row {
3659                Some(IndentSuggestion {
3660                    basis_row: prev_row,
3661                    delta: Ordering::Less,
3662                    within_error: within_error && !from_regex,
3663                })
3664            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
3665            {
3666                Some(IndentSuggestion {
3667                    basis_row: prev_row,
3668                    delta: Ordering::Equal,
3669                    within_error: within_error && !from_regex,
3670                })
3671            } else {
3672                None
3673            };
3674
3675            prev_row = row;
3676            prev_row_start = row_start;
3677            suggestion
3678        }))
3679    }
3680
3681    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
3682        while row > 0 {
3683            row -= 1;
3684            if !self.is_line_blank(row) {
3685                return Some(row);
3686            }
3687        }
3688        None
3689    }
3690
3691    #[ztracing::instrument(skip_all)]
3692    fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures<'_>, Vec<HighlightMap>) {
3693        let captures = self.syntax.captures(range, &self.text, |grammar| {
3694            grammar
3695                .highlights_config
3696                .as_ref()
3697                .map(|config| &config.query)
3698        });
3699        let highlight_maps = captures
3700            .grammars()
3701            .iter()
3702            .map(|grammar| grammar.highlight_map())
3703            .collect();
3704        (captures, highlight_maps)
3705    }
3706
3707    /// Iterates over chunks of text in the given range of the buffer. Text is chunked
3708    /// in an arbitrary way due to being stored in a [`Rope`](text::Rope). The text is also
3709    /// returned in chunks where each chunk has a single syntax highlighting style and
3710    /// diagnostic status.
3711    #[ztracing::instrument(skip_all)]
3712    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks<'_> {
3713        let range = range.start.to_offset(self)..range.end.to_offset(self);
3714
3715        let mut syntax = None;
3716        if language_aware {
3717            syntax = Some(self.get_highlights(range.clone()));
3718        }
3719        // We want to look at diagnostic spans only when iterating over language-annotated chunks.
3720        let diagnostics = language_aware;
3721        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostics, Some(self))
3722    }
3723
3724    pub fn highlighted_text_for_range<T: ToOffset>(
3725        &self,
3726        range: Range<T>,
3727        override_style: Option<HighlightStyle>,
3728        syntax_theme: &SyntaxTheme,
3729    ) -> HighlightedText {
3730        HighlightedText::from_buffer_range(
3731            range,
3732            &self.text,
3733            &self.syntax,
3734            override_style,
3735            syntax_theme,
3736        )
3737    }
3738
3739    /// Invokes the given callback for each line of text in the given range of the buffer.
3740    /// Uses callback to avoid allocating a string for each line.
3741    fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
3742        let mut line = String::new();
3743        let mut row = range.start.row;
3744        for chunk in self
3745            .as_rope()
3746            .chunks_in_range(range.to_offset(self))
3747            .chain(["\n"])
3748        {
3749            for (newline_ix, text) in chunk.split('\n').enumerate() {
3750                if newline_ix > 0 {
3751                    callback(row, &line);
3752                    row += 1;
3753                    line.clear();
3754                }
3755                line.push_str(text);
3756            }
3757        }
3758    }
3759
3760    /// Iterates over every [`SyntaxLayer`] in the buffer.
3761    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayer<'_>> + '_ {
3762        self.syntax_layers_for_range(0..self.len(), true)
3763    }
3764
3765    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayer<'_>> {
3766        let offset = position.to_offset(self);
3767        self.syntax_layers_for_range(offset..offset, false)
3768            .filter(|l| {
3769                if let Some(ranges) = l.included_sub_ranges {
3770                    ranges.iter().any(|range| {
3771                        let start = range.start.to_offset(self);
3772                        start <= offset && {
3773                            let end = range.end.to_offset(self);
3774                            offset < end
3775                        }
3776                    })
3777                } else {
3778                    l.node().start_byte() <= offset && l.node().end_byte() > offset
3779                }
3780            })
3781            .last()
3782    }
3783
3784    pub fn syntax_layers_for_range<D: ToOffset>(
3785        &self,
3786        range: Range<D>,
3787        include_hidden: bool,
3788    ) -> impl Iterator<Item = SyntaxLayer<'_>> + '_ {
3789        self.syntax
3790            .layers_for_range(range, &self.text, include_hidden)
3791    }
3792
3793    pub fn smallest_syntax_layer_containing<D: ToOffset>(
3794        &self,
3795        range: Range<D>,
3796    ) -> Option<SyntaxLayer<'_>> {
3797        let range = range.to_offset(self);
3798        self.syntax
3799            .layers_for_range(range, &self.text, false)
3800            .max_by(|a, b| {
3801                if a.depth != b.depth {
3802                    a.depth.cmp(&b.depth)
3803                } else if a.offset.0 != b.offset.0 {
3804                    a.offset.0.cmp(&b.offset.0)
3805                } else {
3806                    a.node().end_byte().cmp(&b.node().end_byte()).reverse()
3807                }
3808            })
3809    }
3810
3811    /// Returns the main [`Language`].
3812    pub fn language(&self) -> Option<&Arc<Language>> {
3813        self.language.as_ref()
3814    }
3815
3816    /// Returns the [`Language`] at the given location.
3817    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
3818        self.syntax_layer_at(position)
3819            .map(|info| info.language)
3820            .or(self.language.as_ref())
3821    }
3822
3823    /// Returns the settings for the language at the given location.
3824    pub fn settings_at<'a, D: ToOffset>(
3825        &'a self,
3826        position: D,
3827        cx: &'a App,
3828    ) -> Cow<'a, LanguageSettings> {
3829        language_settings(
3830            self.language_at(position).map(|l| l.name()),
3831            self.file.as_ref(),
3832            cx,
3833        )
3834    }
3835
3836    pub fn char_classifier_at<T: ToOffset>(&self, point: T) -> CharClassifier {
3837        CharClassifier::new(self.language_scope_at(point))
3838    }
3839
3840    /// Returns the [`LanguageScope`] at the given location.
3841    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
3842        let offset = position.to_offset(self);
3843        let mut scope = None;
3844        let mut smallest_range_and_depth: Option<(Range<usize>, usize)> = None;
3845
3846        // Use the layer that has the smallest node intersecting the given point.
3847        for layer in self
3848            .syntax
3849            .layers_for_range(offset..offset, &self.text, false)
3850        {
3851            let mut cursor = layer.node().walk();
3852
3853            let mut range = None;
3854            loop {
3855                let child_range = cursor.node().byte_range();
3856                if !child_range.contains(&offset) {
3857                    break;
3858                }
3859
3860                range = Some(child_range);
3861                if cursor.goto_first_child_for_byte(offset).is_none() {
3862                    break;
3863                }
3864            }
3865
3866            if let Some(range) = range
3867                && smallest_range_and_depth.as_ref().is_none_or(
3868                    |(smallest_range, smallest_range_depth)| {
3869                        if layer.depth > *smallest_range_depth {
3870                            true
3871                        } else if layer.depth == *smallest_range_depth {
3872                            range.len() < smallest_range.len()
3873                        } else {
3874                            false
3875                        }
3876                    },
3877                )
3878            {
3879                smallest_range_and_depth = Some((range, layer.depth));
3880                scope = Some(LanguageScope {
3881                    language: layer.language.clone(),
3882                    override_id: layer.override_id(offset, &self.text),
3883                });
3884            }
3885        }
3886
3887        scope.or_else(|| {
3888            self.language.clone().map(|language| LanguageScope {
3889                language,
3890                override_id: None,
3891            })
3892        })
3893    }
3894
3895    /// Returns a tuple of the range and character kind of the word
3896    /// surrounding the given position.
3897    pub fn surrounding_word<T: ToOffset>(
3898        &self,
3899        start: T,
3900        scope_context: Option<CharScopeContext>,
3901    ) -> (Range<usize>, Option<CharKind>) {
3902        let mut start = start.to_offset(self);
3903        let mut end = start;
3904        let mut next_chars = self.chars_at(start).take(128).peekable();
3905        let mut prev_chars = self.reversed_chars_at(start).take(128).peekable();
3906
3907        let classifier = self.char_classifier_at(start).scope_context(scope_context);
3908        let word_kind = cmp::max(
3909            prev_chars.peek().copied().map(|c| classifier.kind(c)),
3910            next_chars.peek().copied().map(|c| classifier.kind(c)),
3911        );
3912
3913        for ch in prev_chars {
3914            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
3915                start -= ch.len_utf8();
3916            } else {
3917                break;
3918            }
3919        }
3920
3921        for ch in next_chars {
3922            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
3923                end += ch.len_utf8();
3924            } else {
3925                break;
3926            }
3927        }
3928
3929        (start..end, word_kind)
3930    }
3931
3932    /// Moves the TreeCursor to the smallest descendant or ancestor syntax node enclosing the given
3933    /// range. When `require_larger` is true, the node found must be larger than the query range.
3934    ///
3935    /// Returns true if a node was found, and false otherwise. In the `false` case the cursor will
3936    /// be moved to the root of the tree.
3937    fn goto_node_enclosing_range(
3938        cursor: &mut tree_sitter::TreeCursor,
3939        query_range: &Range<usize>,
3940        require_larger: bool,
3941    ) -> bool {
3942        let mut ascending = false;
3943        loop {
3944            let mut range = cursor.node().byte_range();
3945            if query_range.is_empty() {
3946                // When the query range is empty and the current node starts after it, move to the
3947                // previous sibling to find the node the containing node.
3948                if range.start > query_range.start {
3949                    cursor.goto_previous_sibling();
3950                    range = cursor.node().byte_range();
3951                }
3952            } else {
3953                // When the query range is non-empty and the current node ends exactly at the start,
3954                // move to the next sibling to find a node that extends beyond the start.
3955                if range.end == query_range.start {
3956                    cursor.goto_next_sibling();
3957                    range = cursor.node().byte_range();
3958                }
3959            }
3960
3961            let encloses = range.contains_inclusive(query_range)
3962                && (!require_larger || range.len() > query_range.len());
3963            if !encloses {
3964                ascending = true;
3965                if !cursor.goto_parent() {
3966                    return false;
3967                }
3968                continue;
3969            } else if ascending {
3970                return true;
3971            }
3972
3973            // Descend into the current node.
3974            if cursor
3975                .goto_first_child_for_byte(query_range.start)
3976                .is_none()
3977            {
3978                return true;
3979            }
3980        }
3981    }
3982
3983    pub fn syntax_ancestor<'a, T: ToOffset>(
3984        &'a self,
3985        range: Range<T>,
3986    ) -> Option<tree_sitter::Node<'a>> {
3987        let range = range.start.to_offset(self)..range.end.to_offset(self);
3988        let mut result: Option<tree_sitter::Node<'a>> = None;
3989        for layer in self
3990            .syntax
3991            .layers_for_range(range.clone(), &self.text, true)
3992        {
3993            let mut cursor = layer.node().walk();
3994
3995            // Find the node that both contains the range and is larger than it.
3996            if !Self::goto_node_enclosing_range(&mut cursor, &range, true) {
3997                continue;
3998            }
3999
4000            let left_node = cursor.node();
4001            let mut layer_result = left_node;
4002
4003            // For an empty range, try to find another node immediately to the right of the range.
4004            if left_node.end_byte() == range.start {
4005                let mut right_node = None;
4006                while !cursor.goto_next_sibling() {
4007                    if !cursor.goto_parent() {
4008                        break;
4009                    }
4010                }
4011
4012                while cursor.node().start_byte() == range.start {
4013                    right_node = Some(cursor.node());
4014                    if !cursor.goto_first_child() {
4015                        break;
4016                    }
4017                }
4018
4019                // If there is a candidate node on both sides of the (empty) range, then
4020                // decide between the two by favoring a named node over an anonymous token.
4021                // If both nodes are the same in that regard, favor the right one.
4022                if let Some(right_node) = right_node
4023                    && (right_node.is_named() || !left_node.is_named())
4024                {
4025                    layer_result = right_node;
4026                }
4027            }
4028
4029            if let Some(previous_result) = &result
4030                && previous_result.byte_range().len() < layer_result.byte_range().len()
4031            {
4032                continue;
4033            }
4034            result = Some(layer_result);
4035        }
4036
4037        result
4038    }
4039
4040    /// Find the previous sibling syntax node at the given range.
4041    ///
4042    /// This function locates the syntax node that precedes the node containing
4043    /// the given range. It searches hierarchically by:
4044    /// 1. Finding the node that contains the given range
4045    /// 2. Looking for the previous sibling at the same tree level
4046    /// 3. If no sibling is found, moving up to parent levels and searching for siblings
4047    ///
4048    /// Returns `None` if there is no previous sibling at any ancestor level.
4049    pub fn syntax_prev_sibling<'a, T: ToOffset>(
4050        &'a self,
4051        range: Range<T>,
4052    ) -> Option<tree_sitter::Node<'a>> {
4053        let range = range.start.to_offset(self)..range.end.to_offset(self);
4054        let mut result: Option<tree_sitter::Node<'a>> = None;
4055
4056        for layer in self
4057            .syntax
4058            .layers_for_range(range.clone(), &self.text, true)
4059        {
4060            let mut cursor = layer.node().walk();
4061
4062            // Find the node that contains the range
4063            if !Self::goto_node_enclosing_range(&mut cursor, &range, false) {
4064                continue;
4065            }
4066
4067            // Look for the previous sibling, moving up ancestor levels if needed
4068            loop {
4069                if cursor.goto_previous_sibling() {
4070                    let layer_result = cursor.node();
4071
4072                    if let Some(previous_result) = &result {
4073                        if previous_result.byte_range().end < layer_result.byte_range().end {
4074                            continue;
4075                        }
4076                    }
4077                    result = Some(layer_result);
4078                    break;
4079                }
4080
4081                // No sibling found at this level, try moving up to parent
4082                if !cursor.goto_parent() {
4083                    break;
4084                }
4085            }
4086        }
4087
4088        result
4089    }
4090
4091    /// Find the next sibling syntax node at the given range.
4092    ///
4093    /// This function locates the syntax node that follows the node containing
4094    /// the given range. It searches hierarchically by:
4095    /// 1. Finding the node that contains the given range
4096    /// 2. Looking for the next sibling at the same tree level
4097    /// 3. If no sibling is found, moving up to parent levels and searching for siblings
4098    ///
4099    /// Returns `None` if there is no next sibling at any ancestor level.
4100    pub fn syntax_next_sibling<'a, T: ToOffset>(
4101        &'a self,
4102        range: Range<T>,
4103    ) -> Option<tree_sitter::Node<'a>> {
4104        let range = range.start.to_offset(self)..range.end.to_offset(self);
4105        let mut result: Option<tree_sitter::Node<'a>> = None;
4106
4107        for layer in self
4108            .syntax
4109            .layers_for_range(range.clone(), &self.text, true)
4110        {
4111            let mut cursor = layer.node().walk();
4112
4113            // Find the node that contains the range
4114            if !Self::goto_node_enclosing_range(&mut cursor, &range, false) {
4115                continue;
4116            }
4117
4118            // Look for the next sibling, moving up ancestor levels if needed
4119            loop {
4120                if cursor.goto_next_sibling() {
4121                    let layer_result = cursor.node();
4122
4123                    if let Some(previous_result) = &result {
4124                        if previous_result.byte_range().start > layer_result.byte_range().start {
4125                            continue;
4126                        }
4127                    }
4128                    result = Some(layer_result);
4129                    break;
4130                }
4131
4132                // No sibling found at this level, try moving up to parent
4133                if !cursor.goto_parent() {
4134                    break;
4135                }
4136            }
4137        }
4138
4139        result
4140    }
4141
4142    /// Returns the root syntax node within the given row
4143    pub fn syntax_root_ancestor(&self, position: Anchor) -> Option<tree_sitter::Node<'_>> {
4144        let start_offset = position.to_offset(self);
4145
4146        let row = self.summary_for_anchor::<text::PointUtf16>(&position).row as usize;
4147
4148        let layer = self
4149            .syntax
4150            .layers_for_range(start_offset..start_offset, &self.text, true)
4151            .next()?;
4152
4153        let mut cursor = layer.node().walk();
4154
4155        // Descend to the first leaf that touches the start of the range.
4156        while cursor.goto_first_child_for_byte(start_offset).is_some() {
4157            if cursor.node().end_byte() == start_offset {
4158                cursor.goto_next_sibling();
4159            }
4160        }
4161
4162        // Ascend to the root node within the same row.
4163        while cursor.goto_parent() {
4164            if cursor.node().start_position().row != row {
4165                break;
4166            }
4167        }
4168
4169        Some(cursor.node())
4170    }
4171
4172    /// Returns the outline for the buffer.
4173    ///
4174    /// This method allows passing an optional [`SyntaxTheme`] to
4175    /// syntax-highlight the returned symbols.
4176    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Outline<Anchor> {
4177        Outline::new(self.outline_items_containing(0..self.len(), true, theme))
4178    }
4179
4180    /// Returns all the symbols that contain the given position.
4181    ///
4182    /// This method allows passing an optional [`SyntaxTheme`] to
4183    /// syntax-highlight the returned symbols.
4184    pub fn symbols_containing<T: ToOffset>(
4185        &self,
4186        position: T,
4187        theme: Option<&SyntaxTheme>,
4188    ) -> Vec<OutlineItem<Anchor>> {
4189        let position = position.to_offset(self);
4190        let start = self.clip_offset(position.saturating_sub(1), Bias::Left);
4191        let end = self.clip_offset(position + 1, Bias::Right);
4192        let mut items = self.outline_items_containing(start..end, false, theme);
4193        let mut prev_depth = None;
4194        items.retain(|item| {
4195            let result = prev_depth.is_none_or(|prev_depth| item.depth > prev_depth);
4196            prev_depth = Some(item.depth);
4197            result
4198        });
4199        items
4200    }
4201
4202    pub fn outline_range_containing<T: ToOffset>(&self, range: Range<T>) -> Option<Range<Point>> {
4203        let range = range.to_offset(self);
4204        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
4205            grammar.outline_config.as_ref().map(|c| &c.query)
4206        });
4207        let configs = matches
4208            .grammars()
4209            .iter()
4210            .map(|g| g.outline_config.as_ref().unwrap())
4211            .collect::<Vec<_>>();
4212
4213        while let Some(mat) = matches.peek() {
4214            let config = &configs[mat.grammar_index];
4215            let containing_item_node = maybe!({
4216                let item_node = mat.captures.iter().find_map(|cap| {
4217                    if cap.index == config.item_capture_ix {
4218                        Some(cap.node)
4219                    } else {
4220                        None
4221                    }
4222                })?;
4223
4224                let item_byte_range = item_node.byte_range();
4225                if item_byte_range.end < range.start || item_byte_range.start > range.end {
4226                    None
4227                } else {
4228                    Some(item_node)
4229                }
4230            });
4231
4232            if let Some(item_node) = containing_item_node {
4233                return Some(
4234                    Point::from_ts_point(item_node.start_position())
4235                        ..Point::from_ts_point(item_node.end_position()),
4236                );
4237            }
4238
4239            matches.advance();
4240        }
4241        None
4242    }
4243
4244    pub fn outline_items_containing<T: ToOffset>(
4245        &self,
4246        range: Range<T>,
4247        include_extra_context: bool,
4248        theme: Option<&SyntaxTheme>,
4249    ) -> Vec<OutlineItem<Anchor>> {
4250        self.outline_items_containing_internal(
4251            range,
4252            include_extra_context,
4253            theme,
4254            |this, range| this.anchor_after(range.start)..this.anchor_before(range.end),
4255        )
4256    }
4257
4258    pub fn outline_items_as_points_containing<T: ToOffset>(
4259        &self,
4260        range: Range<T>,
4261        include_extra_context: bool,
4262        theme: Option<&SyntaxTheme>,
4263    ) -> Vec<OutlineItem<Point>> {
4264        self.outline_items_containing_internal(range, include_extra_context, theme, |_, range| {
4265            range
4266        })
4267    }
4268
4269    pub fn outline_items_as_offsets_containing<T: ToOffset>(
4270        &self,
4271        range: Range<T>,
4272        include_extra_context: bool,
4273        theme: Option<&SyntaxTheme>,
4274    ) -> Vec<OutlineItem<usize>> {
4275        self.outline_items_containing_internal(
4276            range,
4277            include_extra_context,
4278            theme,
4279            |buffer, range| range.to_offset(buffer),
4280        )
4281    }
4282
4283    fn outline_items_containing_internal<T: ToOffset, U>(
4284        &self,
4285        range: Range<T>,
4286        include_extra_context: bool,
4287        theme: Option<&SyntaxTheme>,
4288        range_callback: fn(&Self, Range<Point>) -> Range<U>,
4289    ) -> Vec<OutlineItem<U>> {
4290        let range = range.to_offset(self);
4291        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
4292            grammar.outline_config.as_ref().map(|c| &c.query)
4293        });
4294
4295        let mut items = Vec::new();
4296        let mut annotation_row_ranges: Vec<Range<u32>> = Vec::new();
4297        while let Some(mat) = matches.peek() {
4298            let config = matches.grammars()[mat.grammar_index]
4299                .outline_config
4300                .as_ref()
4301                .unwrap();
4302            if let Some(item) =
4303                self.next_outline_item(config, &mat, &range, include_extra_context, theme)
4304            {
4305                items.push(item);
4306            } else if let Some(capture) = mat
4307                .captures
4308                .iter()
4309                .find(|capture| Some(capture.index) == config.annotation_capture_ix)
4310            {
4311                let capture_range = capture.node.start_position()..capture.node.end_position();
4312                let mut capture_row_range =
4313                    capture_range.start.row as u32..capture_range.end.row as u32;
4314                if capture_range.end.row > capture_range.start.row && capture_range.end.column == 0
4315                {
4316                    capture_row_range.end -= 1;
4317                }
4318                if let Some(last_row_range) = annotation_row_ranges.last_mut() {
4319                    if last_row_range.end >= capture_row_range.start.saturating_sub(1) {
4320                        last_row_range.end = capture_row_range.end;
4321                    } else {
4322                        annotation_row_ranges.push(capture_row_range);
4323                    }
4324                } else {
4325                    annotation_row_ranges.push(capture_row_range);
4326                }
4327            }
4328            matches.advance();
4329        }
4330
4331        items.sort_by_key(|item| (item.range.start, Reverse(item.range.end)));
4332
4333        // Assign depths based on containment relationships and convert to anchors.
4334        let mut item_ends_stack = Vec::<Point>::new();
4335        let mut anchor_items = Vec::new();
4336        let mut annotation_row_ranges = annotation_row_ranges.into_iter().peekable();
4337        for item in items {
4338            while let Some(last_end) = item_ends_stack.last().copied() {
4339                if last_end < item.range.end {
4340                    item_ends_stack.pop();
4341                } else {
4342                    break;
4343                }
4344            }
4345
4346            let mut annotation_row_range = None;
4347            while let Some(next_annotation_row_range) = annotation_row_ranges.peek() {
4348                let row_preceding_item = item.range.start.row.saturating_sub(1);
4349                if next_annotation_row_range.end < row_preceding_item {
4350                    annotation_row_ranges.next();
4351                } else {
4352                    if next_annotation_row_range.end == row_preceding_item {
4353                        annotation_row_range = Some(next_annotation_row_range.clone());
4354                        annotation_row_ranges.next();
4355                    }
4356                    break;
4357                }
4358            }
4359
4360            anchor_items.push(OutlineItem {
4361                depth: item_ends_stack.len(),
4362                range: range_callback(self, item.range.clone()),
4363                source_range_for_text: range_callback(self, item.source_range_for_text.clone()),
4364                text: item.text,
4365                highlight_ranges: item.highlight_ranges,
4366                name_ranges: item.name_ranges,
4367                body_range: item.body_range.map(|r| range_callback(self, r)),
4368                annotation_range: annotation_row_range.map(|annotation_range| {
4369                    let point_range = Point::new(annotation_range.start, 0)
4370                        ..Point::new(annotation_range.end, self.line_len(annotation_range.end));
4371                    range_callback(self, point_range)
4372                }),
4373            });
4374            item_ends_stack.push(item.range.end);
4375        }
4376
4377        anchor_items
4378    }
4379
4380    fn next_outline_item(
4381        &self,
4382        config: &OutlineConfig,
4383        mat: &SyntaxMapMatch,
4384        range: &Range<usize>,
4385        include_extra_context: bool,
4386        theme: Option<&SyntaxTheme>,
4387    ) -> Option<OutlineItem<Point>> {
4388        let item_node = mat.captures.iter().find_map(|cap| {
4389            if cap.index == config.item_capture_ix {
4390                Some(cap.node)
4391            } else {
4392                None
4393            }
4394        })?;
4395
4396        let item_byte_range = item_node.byte_range();
4397        if item_byte_range.end < range.start || item_byte_range.start > range.end {
4398            return None;
4399        }
4400        let item_point_range = Point::from_ts_point(item_node.start_position())
4401            ..Point::from_ts_point(item_node.end_position());
4402
4403        let mut open_point = None;
4404        let mut close_point = None;
4405
4406        let mut buffer_ranges = Vec::new();
4407        let mut add_to_buffer_ranges = |node: tree_sitter::Node, node_is_name| {
4408            let mut range = node.start_byte()..node.end_byte();
4409            let start = node.start_position();
4410            if node.end_position().row > start.row {
4411                range.end = range.start + self.line_len(start.row as u32) as usize - start.column;
4412            }
4413
4414            if !range.is_empty() {
4415                buffer_ranges.push((range, node_is_name));
4416            }
4417        };
4418
4419        for capture in mat.captures {
4420            if capture.index == config.name_capture_ix {
4421                add_to_buffer_ranges(capture.node, true);
4422            } else if Some(capture.index) == config.context_capture_ix
4423                || (Some(capture.index) == config.extra_context_capture_ix && include_extra_context)
4424            {
4425                add_to_buffer_ranges(capture.node, false);
4426            } else {
4427                if Some(capture.index) == config.open_capture_ix {
4428                    open_point = Some(Point::from_ts_point(capture.node.end_position()));
4429                } else if Some(capture.index) == config.close_capture_ix {
4430                    close_point = Some(Point::from_ts_point(capture.node.start_position()));
4431                }
4432            }
4433        }
4434
4435        if buffer_ranges.is_empty() {
4436            return None;
4437        }
4438        let source_range_for_text =
4439            buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end;
4440
4441        let mut text = String::new();
4442        let mut highlight_ranges = Vec::new();
4443        let mut name_ranges = Vec::new();
4444        let mut chunks = self.chunks(source_range_for_text.clone(), true);
4445        let mut last_buffer_range_end = 0;
4446        for (buffer_range, is_name) in buffer_ranges {
4447            let space_added = !text.is_empty() && buffer_range.start > last_buffer_range_end;
4448            if space_added {
4449                text.push(' ');
4450            }
4451            let before_append_len = text.len();
4452            let mut offset = buffer_range.start;
4453            chunks.seek(buffer_range.clone());
4454            for mut chunk in chunks.by_ref() {
4455                if chunk.text.len() > buffer_range.end - offset {
4456                    chunk.text = &chunk.text[0..(buffer_range.end - offset)];
4457                    offset = buffer_range.end;
4458                } else {
4459                    offset += chunk.text.len();
4460                }
4461                let style = chunk
4462                    .syntax_highlight_id
4463                    .zip(theme)
4464                    .and_then(|(highlight, theme)| highlight.style(theme));
4465                if let Some(style) = style {
4466                    let start = text.len();
4467                    let end = start + chunk.text.len();
4468                    highlight_ranges.push((start..end, style));
4469                }
4470                text.push_str(chunk.text);
4471                if offset >= buffer_range.end {
4472                    break;
4473                }
4474            }
4475            if is_name {
4476                let after_append_len = text.len();
4477                let start = if space_added && !name_ranges.is_empty() {
4478                    before_append_len - 1
4479                } else {
4480                    before_append_len
4481                };
4482                name_ranges.push(start..after_append_len);
4483            }
4484            last_buffer_range_end = buffer_range.end;
4485        }
4486
4487        Some(OutlineItem {
4488            depth: 0, // We'll calculate the depth later
4489            range: item_point_range,
4490            source_range_for_text: source_range_for_text.to_point(self),
4491            text,
4492            highlight_ranges,
4493            name_ranges,
4494            body_range: open_point.zip(close_point).map(|(start, end)| start..end),
4495            annotation_range: None,
4496        })
4497    }
4498
4499    pub fn function_body_fold_ranges<T: ToOffset>(
4500        &self,
4501        within: Range<T>,
4502    ) -> impl Iterator<Item = Range<usize>> + '_ {
4503        self.text_object_ranges(within, TreeSitterOptions::default())
4504            .filter_map(|(range, obj)| (obj == TextObject::InsideFunction).then_some(range))
4505    }
4506
4507    /// For each grammar in the language, runs the provided
4508    /// [`tree_sitter::Query`] against the given range.
4509    pub fn matches(
4510        &self,
4511        range: Range<usize>,
4512        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
4513    ) -> SyntaxMapMatches<'_> {
4514        self.syntax.matches(range, self, query)
4515    }
4516
4517    /// Finds all [`RowChunks`] applicable to the given range, then returns all bracket pairs that intersect with those chunks.
4518    /// Hence, may return more bracket pairs than the range contains.
4519    ///
4520    /// Will omit known chunks.
4521    /// The resulting bracket match collections are not ordered.
4522    pub fn fetch_bracket_ranges(
4523        &self,
4524        range: Range<usize>,
4525        known_chunks: Option<&HashSet<Range<BufferRow>>>,
4526    ) -> HashMap<Range<BufferRow>, Vec<BracketMatch<usize>>> {
4527        let mut all_bracket_matches = HashMap::default();
4528
4529        for chunk in self
4530            .tree_sitter_data
4531            .chunks
4532            .applicable_chunks(&[range.to_point(self)])
4533        {
4534            if known_chunks.is_some_and(|chunks| chunks.contains(&chunk.row_range())) {
4535                continue;
4536            }
4537            let chunk_range = chunk.anchor_range();
4538            let chunk_range = chunk_range.to_offset(&self);
4539
4540            if let Some(cached_brackets) =
4541                &self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]
4542            {
4543                all_bracket_matches.insert(chunk.row_range(), cached_brackets.clone());
4544                continue;
4545            }
4546
4547            let mut all_brackets: Vec<(BracketMatch<usize>, bool)> = Vec::new();
4548            let mut opens = Vec::new();
4549            let mut color_pairs = Vec::new();
4550
4551            let mut matches = self.syntax.matches_with_options(
4552                chunk_range.clone(),
4553                &self.text,
4554                TreeSitterOptions {
4555                    max_bytes_to_query: Some(MAX_BYTES_TO_QUERY),
4556                    max_start_depth: None,
4557                },
4558                |grammar| grammar.brackets_config.as_ref().map(|c| &c.query),
4559            );
4560            let configs = matches
4561                .grammars()
4562                .iter()
4563                .map(|grammar| grammar.brackets_config.as_ref().unwrap())
4564                .collect::<Vec<_>>();
4565
4566            // Group matches by open range so we can either trust grammar output
4567            // or repair it by picking a single closest close per open.
4568            let mut open_to_close_ranges = BTreeMap::new();
4569            while let Some(mat) = matches.peek() {
4570                let mut open = None;
4571                let mut close = None;
4572                let syntax_layer_depth = mat.depth;
4573                let config = configs[mat.grammar_index];
4574                let pattern = &config.patterns[mat.pattern_index];
4575                for capture in mat.captures {
4576                    if capture.index == config.open_capture_ix {
4577                        open = Some(capture.node.byte_range());
4578                    } else if capture.index == config.close_capture_ix {
4579                        close = Some(capture.node.byte_range());
4580                    }
4581                }
4582
4583                matches.advance();
4584
4585                let Some((open_range, close_range)) = open.zip(close) else {
4586                    continue;
4587                };
4588
4589                let bracket_range = open_range.start..=close_range.end;
4590                if !bracket_range.overlaps(&chunk_range) {
4591                    continue;
4592                }
4593
4594                open_to_close_ranges
4595                    .entry((open_range.start, open_range.end))
4596                    .or_insert_with(BTreeMap::new)
4597                    .insert(
4598                        (close_range.start, close_range.end),
4599                        BracketMatch {
4600                            open_range: open_range.clone(),
4601                            close_range: close_range.clone(),
4602                            syntax_layer_depth,
4603                            newline_only: pattern.newline_only,
4604                            color_index: None,
4605                        },
4606                    );
4607
4608                all_brackets.push((
4609                    BracketMatch {
4610                        open_range,
4611                        close_range,
4612                        syntax_layer_depth,
4613                        newline_only: pattern.newline_only,
4614                        color_index: None,
4615                    },
4616                    pattern.rainbow_exclude,
4617                ));
4618            }
4619
4620            let has_bogus_matches = open_to_close_ranges
4621                .iter()
4622                .any(|(_, end_ranges)| end_ranges.len() > 1);
4623            if has_bogus_matches {
4624                // Grammar is producing bogus matches where one open is paired with multiple
4625                // closes. Build a valid stack by walking through positions in order.
4626                // For each close, we know the expected open_len from tree-sitter matches.
4627
4628                // Map each close to its expected open length (for inferring opens)
4629                let close_to_open_len: HashMap<(usize, usize), usize> = all_brackets
4630                    .iter()
4631                    .map(|(m, _)| ((m.close_range.start, m.close_range.end), m.open_range.len()))
4632                    .collect();
4633
4634                // Collect unique opens and closes within this chunk
4635                let mut unique_opens: HashSet<(usize, usize)> = all_brackets
4636                    .iter()
4637                    .map(|(m, _)| (m.open_range.start, m.open_range.end))
4638                    .filter(|(start, _)| chunk_range.contains(start))
4639                    .collect();
4640
4641                let mut unique_closes: Vec<(usize, usize)> = all_brackets
4642                    .iter()
4643                    .map(|(m, _)| (m.close_range.start, m.close_range.end))
4644                    .filter(|(start, _)| chunk_range.contains(start))
4645                    .collect();
4646                unique_closes.sort();
4647                unique_closes.dedup();
4648
4649                // Build valid pairs by walking through closes in order
4650                let mut unique_opens_vec: Vec<_> = unique_opens.iter().copied().collect();
4651                unique_opens_vec.sort();
4652
4653                let mut valid_pairs: HashSet<((usize, usize), (usize, usize))> = HashSet::default();
4654                let mut open_stack: Vec<(usize, usize)> = Vec::new();
4655                let mut open_idx = 0;
4656
4657                for close in &unique_closes {
4658                    // Push all opens before this close onto stack
4659                    while open_idx < unique_opens_vec.len()
4660                        && unique_opens_vec[open_idx].0 < close.0
4661                    {
4662                        open_stack.push(unique_opens_vec[open_idx]);
4663                        open_idx += 1;
4664                    }
4665
4666                    // Try to match with most recent open
4667                    if let Some(open) = open_stack.pop() {
4668                        valid_pairs.insert((open, *close));
4669                    } else if let Some(&open_len) = close_to_open_len.get(close) {
4670                        // No open on stack - infer one based on expected open_len
4671                        if close.0 >= open_len {
4672                            let inferred = (close.0 - open_len, close.0);
4673                            unique_opens.insert(inferred);
4674                            valid_pairs.insert((inferred, *close));
4675                            all_brackets.push((
4676                                BracketMatch {
4677                                    open_range: inferred.0..inferred.1,
4678                                    close_range: close.0..close.1,
4679                                    newline_only: false,
4680                                    syntax_layer_depth: 0,
4681                                    color_index: None,
4682                                },
4683                                false,
4684                            ));
4685                        }
4686                    }
4687                }
4688
4689                all_brackets.retain(|(m, _)| {
4690                    let open = (m.open_range.start, m.open_range.end);
4691                    let close = (m.close_range.start, m.close_range.end);
4692                    valid_pairs.contains(&(open, close))
4693                });
4694            }
4695
4696            let mut all_brackets = all_brackets
4697                .into_iter()
4698                .enumerate()
4699                .map(|(index, (bracket_match, rainbow_exclude))| {
4700                    // Certain languages have "brackets" that are not brackets, e.g. tags. and such
4701                    // bracket will match the entire tag with all text inside.
4702                    // For now, avoid highlighting any pair that has more than single char in each bracket.
4703                    // We need to  colorize `<Element/>` bracket pairs, so cannot make this check stricter.
4704                    let should_color = !rainbow_exclude
4705                        && (bracket_match.open_range.len() == 1
4706                            || bracket_match.close_range.len() == 1);
4707                    if should_color {
4708                        opens.push(bracket_match.open_range.clone());
4709                        color_pairs.push((
4710                            bracket_match.open_range.clone(),
4711                            bracket_match.close_range.clone(),
4712                            index,
4713                        ));
4714                    }
4715                    bracket_match
4716                })
4717                .collect::<Vec<_>>();
4718
4719            opens.sort_by_key(|r| (r.start, r.end));
4720            opens.dedup_by(|a, b| a.start == b.start && a.end == b.end);
4721            color_pairs.sort_by_key(|(_, close, _)| close.end);
4722
4723            let mut open_stack = Vec::new();
4724            let mut open_index = 0;
4725            for (open, close, index) in color_pairs {
4726                while open_index < opens.len() && opens[open_index].start < close.start {
4727                    open_stack.push(opens[open_index].clone());
4728                    open_index += 1;
4729                }
4730
4731                if open_stack.last() == Some(&open) {
4732                    let depth_index = open_stack.len() - 1;
4733                    all_brackets[index].color_index = Some(depth_index);
4734                    open_stack.pop();
4735                }
4736            }
4737
4738            all_brackets.sort_by_key(|bracket_match| {
4739                (bracket_match.open_range.start, bracket_match.open_range.end)
4740            });
4741
4742            if let empty_slot @ None =
4743                &mut self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]
4744            {
4745                *empty_slot = Some(all_brackets.clone());
4746            }
4747            all_bracket_matches.insert(chunk.row_range(), all_brackets);
4748        }
4749
4750        all_bracket_matches
4751    }
4752
4753    pub fn all_bracket_ranges(
4754        &self,
4755        range: Range<usize>,
4756    ) -> impl Iterator<Item = BracketMatch<usize>> {
4757        self.fetch_bracket_ranges(range.clone(), None)
4758            .into_values()
4759            .flatten()
4760            .filter(move |bracket_match| {
4761                let bracket_range = bracket_match.open_range.start..bracket_match.close_range.end;
4762                bracket_range.overlaps(&range)
4763            })
4764    }
4765
4766    /// Returns bracket range pairs overlapping or adjacent to `range`
4767    pub fn bracket_ranges<T: ToOffset>(
4768        &self,
4769        range: Range<T>,
4770    ) -> impl Iterator<Item = BracketMatch<usize>> + '_ {
4771        // Find bracket pairs that *inclusively* contain the given range.
4772        let range = range.start.to_previous_offset(self)..range.end.to_next_offset(self);
4773        self.all_bracket_ranges(range)
4774            .filter(|pair| !pair.newline_only)
4775    }
4776
4777    pub fn debug_variables_query<T: ToOffset>(
4778        &self,
4779        range: Range<T>,
4780    ) -> impl Iterator<Item = (Range<usize>, DebuggerTextObject)> + '_ {
4781        let range = range.start.to_previous_offset(self)..range.end.to_next_offset(self);
4782
4783        let mut matches = self.syntax.matches_with_options(
4784            range.clone(),
4785            &self.text,
4786            TreeSitterOptions::default(),
4787            |grammar| grammar.debug_variables_config.as_ref().map(|c| &c.query),
4788        );
4789
4790        let configs = matches
4791            .grammars()
4792            .iter()
4793            .map(|grammar| grammar.debug_variables_config.as_ref())
4794            .collect::<Vec<_>>();
4795
4796        let mut captures = Vec::<(Range<usize>, DebuggerTextObject)>::new();
4797
4798        iter::from_fn(move || {
4799            loop {
4800                while let Some(capture) = captures.pop() {
4801                    if capture.0.overlaps(&range) {
4802                        return Some(capture);
4803                    }
4804                }
4805
4806                let mat = matches.peek()?;
4807
4808                let Some(config) = configs[mat.grammar_index].as_ref() else {
4809                    matches.advance();
4810                    continue;
4811                };
4812
4813                for capture in mat.captures {
4814                    let Some(ix) = config
4815                        .objects_by_capture_ix
4816                        .binary_search_by_key(&capture.index, |e| e.0)
4817                        .ok()
4818                    else {
4819                        continue;
4820                    };
4821                    let text_object = config.objects_by_capture_ix[ix].1;
4822                    let byte_range = capture.node.byte_range();
4823
4824                    let mut found = false;
4825                    for (range, existing) in captures.iter_mut() {
4826                        if existing == &text_object {
4827                            range.start = range.start.min(byte_range.start);
4828                            range.end = range.end.max(byte_range.end);
4829                            found = true;
4830                            break;
4831                        }
4832                    }
4833
4834                    if !found {
4835                        captures.push((byte_range, text_object));
4836                    }
4837                }
4838
4839                matches.advance();
4840            }
4841        })
4842    }
4843
4844    pub fn text_object_ranges<T: ToOffset>(
4845        &self,
4846        range: Range<T>,
4847        options: TreeSitterOptions,
4848    ) -> impl Iterator<Item = (Range<usize>, TextObject)> + '_ {
4849        let range =
4850            range.start.to_previous_offset(self)..self.len().min(range.end.to_next_offset(self));
4851
4852        let mut matches =
4853            self.syntax
4854                .matches_with_options(range.clone(), &self.text, options, |grammar| {
4855                    grammar.text_object_config.as_ref().map(|c| &c.query)
4856                });
4857
4858        let configs = matches
4859            .grammars()
4860            .iter()
4861            .map(|grammar| grammar.text_object_config.as_ref())
4862            .collect::<Vec<_>>();
4863
4864        let mut captures = Vec::<(Range<usize>, TextObject)>::new();
4865
4866        iter::from_fn(move || {
4867            loop {
4868                while let Some(capture) = captures.pop() {
4869                    if capture.0.overlaps(&range) {
4870                        return Some(capture);
4871                    }
4872                }
4873
4874                let mat = matches.peek()?;
4875
4876                let Some(config) = configs[mat.grammar_index].as_ref() else {
4877                    matches.advance();
4878                    continue;
4879                };
4880
4881                for capture in mat.captures {
4882                    let Some(ix) = config
4883                        .text_objects_by_capture_ix
4884                        .binary_search_by_key(&capture.index, |e| e.0)
4885                        .ok()
4886                    else {
4887                        continue;
4888                    };
4889                    let text_object = config.text_objects_by_capture_ix[ix].1;
4890                    let byte_range = capture.node.byte_range();
4891
4892                    let mut found = false;
4893                    for (range, existing) in captures.iter_mut() {
4894                        if existing == &text_object {
4895                            range.start = range.start.min(byte_range.start);
4896                            range.end = range.end.max(byte_range.end);
4897                            found = true;
4898                            break;
4899                        }
4900                    }
4901
4902                    if !found {
4903                        captures.push((byte_range, text_object));
4904                    }
4905                }
4906
4907                matches.advance();
4908            }
4909        })
4910    }
4911
4912    /// Returns enclosing bracket ranges containing the given range
4913    pub fn enclosing_bracket_ranges<T: ToOffset>(
4914        &self,
4915        range: Range<T>,
4916    ) -> impl Iterator<Item = BracketMatch<usize>> + '_ {
4917        let range = range.start.to_offset(self)..range.end.to_offset(self);
4918
4919        let result: Vec<_> = self.bracket_ranges(range.clone()).collect();
4920        let max_depth = result
4921            .iter()
4922            .map(|mat| mat.syntax_layer_depth)
4923            .max()
4924            .unwrap_or(0);
4925        result.into_iter().filter(move |pair| {
4926            pair.open_range.start <= range.start
4927                && pair.close_range.end >= range.end
4928                && pair.syntax_layer_depth == max_depth
4929        })
4930    }
4931
4932    /// Returns the smallest enclosing bracket ranges containing the given range or None if no brackets contain range
4933    ///
4934    /// Can optionally pass a range_filter to filter the ranges of brackets to consider
4935    pub fn innermost_enclosing_bracket_ranges<T: ToOffset>(
4936        &self,
4937        range: Range<T>,
4938        range_filter: Option<&dyn Fn(Range<usize>, Range<usize>) -> bool>,
4939    ) -> Option<(Range<usize>, Range<usize>)> {
4940        let range = range.start.to_offset(self)..range.end.to_offset(self);
4941
4942        // Get the ranges of the innermost pair of brackets.
4943        let mut result: Option<(Range<usize>, Range<usize>)> = None;
4944
4945        for pair in self.enclosing_bracket_ranges(range) {
4946            if let Some(range_filter) = range_filter
4947                && !range_filter(pair.open_range.clone(), pair.close_range.clone())
4948            {
4949                continue;
4950            }
4951
4952            let len = pair.close_range.end - pair.open_range.start;
4953
4954            if let Some((existing_open, existing_close)) = &result {
4955                let existing_len = existing_close.end - existing_open.start;
4956                if len > existing_len {
4957                    continue;
4958                }
4959            }
4960
4961            result = Some((pair.open_range, pair.close_range));
4962        }
4963
4964        result
4965    }
4966
4967    /// Returns anchor ranges for any matches of the redaction query.
4968    /// The buffer can be associated with multiple languages, and the redaction query associated with each
4969    /// will be run on the relevant section of the buffer.
4970    pub fn redacted_ranges<T: ToOffset>(
4971        &self,
4972        range: Range<T>,
4973    ) -> impl Iterator<Item = Range<usize>> + '_ {
4974        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
4975        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
4976            grammar
4977                .redactions_config
4978                .as_ref()
4979                .map(|config| &config.query)
4980        });
4981
4982        let configs = syntax_matches
4983            .grammars()
4984            .iter()
4985            .map(|grammar| grammar.redactions_config.as_ref())
4986            .collect::<Vec<_>>();
4987
4988        iter::from_fn(move || {
4989            let redacted_range = syntax_matches
4990                .peek()
4991                .and_then(|mat| {
4992                    configs[mat.grammar_index].and_then(|config| {
4993                        mat.captures
4994                            .iter()
4995                            .find(|capture| capture.index == config.redaction_capture_ix)
4996                    })
4997                })
4998                .map(|mat| mat.node.byte_range());
4999            syntax_matches.advance();
5000            redacted_range
5001        })
5002    }
5003
5004    pub fn injections_intersecting_range<T: ToOffset>(
5005        &self,
5006        range: Range<T>,
5007    ) -> impl Iterator<Item = (Range<usize>, &Arc<Language>)> + '_ {
5008        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
5009
5010        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
5011            grammar
5012                .injection_config
5013                .as_ref()
5014                .map(|config| &config.query)
5015        });
5016
5017        let configs = syntax_matches
5018            .grammars()
5019            .iter()
5020            .map(|grammar| grammar.injection_config.as_ref())
5021            .collect::<Vec<_>>();
5022
5023        iter::from_fn(move || {
5024            let ranges = syntax_matches.peek().and_then(|mat| {
5025                let config = &configs[mat.grammar_index]?;
5026                let content_capture_range = mat.captures.iter().find_map(|capture| {
5027                    if capture.index == config.content_capture_ix {
5028                        Some(capture.node.byte_range())
5029                    } else {
5030                        None
5031                    }
5032                })?;
5033                let language = self.language_at(content_capture_range.start)?;
5034                Some((content_capture_range, language))
5035            });
5036            syntax_matches.advance();
5037            ranges
5038        })
5039    }
5040
5041    pub fn runnable_ranges(
5042        &self,
5043        offset_range: Range<usize>,
5044    ) -> impl Iterator<Item = RunnableRange> + '_ {
5045        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
5046            grammar.runnable_config.as_ref().map(|config| &config.query)
5047        });
5048
5049        let test_configs = syntax_matches
5050            .grammars()
5051            .iter()
5052            .map(|grammar| grammar.runnable_config.as_ref())
5053            .collect::<Vec<_>>();
5054
5055        iter::from_fn(move || {
5056            loop {
5057                let mat = syntax_matches.peek()?;
5058
5059                let test_range = test_configs[mat.grammar_index].and_then(|test_configs| {
5060                    let mut run_range = None;
5061                    let full_range = mat.captures.iter().fold(
5062                        Range {
5063                            start: usize::MAX,
5064                            end: 0,
5065                        },
5066                        |mut acc, next| {
5067                            let byte_range = next.node.byte_range();
5068                            if acc.start > byte_range.start {
5069                                acc.start = byte_range.start;
5070                            }
5071                            if acc.end < byte_range.end {
5072                                acc.end = byte_range.end;
5073                            }
5074                            acc
5075                        },
5076                    );
5077                    if full_range.start > full_range.end {
5078                        // We did not find a full spanning range of this match.
5079                        return None;
5080                    }
5081                    let extra_captures: SmallVec<[_; 1]> =
5082                        SmallVec::from_iter(mat.captures.iter().filter_map(|capture| {
5083                            test_configs
5084                                .extra_captures
5085                                .get(capture.index as usize)
5086                                .cloned()
5087                                .and_then(|tag_name| match tag_name {
5088                                    RunnableCapture::Named(name) => {
5089                                        Some((capture.node.byte_range(), name))
5090                                    }
5091                                    RunnableCapture::Run => {
5092                                        let _ = run_range.insert(capture.node.byte_range());
5093                                        None
5094                                    }
5095                                })
5096                        }));
5097                    let run_range = run_range?;
5098                    let tags = test_configs
5099                        .query
5100                        .property_settings(mat.pattern_index)
5101                        .iter()
5102                        .filter_map(|property| {
5103                            if *property.key == *"tag" {
5104                                property
5105                                    .value
5106                                    .as_ref()
5107                                    .map(|value| RunnableTag(value.to_string().into()))
5108                            } else {
5109                                None
5110                            }
5111                        })
5112                        .collect();
5113                    let extra_captures = extra_captures
5114                        .into_iter()
5115                        .map(|(range, name)| {
5116                            (
5117                                name.to_string(),
5118                                self.text_for_range(range).collect::<String>(),
5119                            )
5120                        })
5121                        .collect();
5122                    // All tags should have the same range.
5123                    Some(RunnableRange {
5124                        run_range,
5125                        full_range,
5126                        runnable: Runnable {
5127                            tags,
5128                            language: mat.language,
5129                            buffer: self.remote_id(),
5130                        },
5131                        extra_captures,
5132                        buffer_id: self.remote_id(),
5133                    })
5134                });
5135
5136                syntax_matches.advance();
5137                if test_range.is_some() {
5138                    // It's fine for us to short-circuit on .peek()? returning None. We don't want to return None from this iter if we
5139                    // had a capture that did not contain a run marker, hence we'll just loop around for the next capture.
5140                    return test_range;
5141                }
5142            }
5143        })
5144    }
5145
5146    /// Returns selections for remote peers intersecting the given range.
5147    #[allow(clippy::type_complexity)]
5148    pub fn selections_in_range(
5149        &self,
5150        range: Range<Anchor>,
5151        include_local: bool,
5152    ) -> impl Iterator<
5153        Item = (
5154            ReplicaId,
5155            bool,
5156            CursorShape,
5157            impl Iterator<Item = &Selection<Anchor>> + '_,
5158        ),
5159    > + '_ {
5160        self.remote_selections
5161            .iter()
5162            .filter(move |(replica_id, set)| {
5163                (include_local || **replica_id != self.text.replica_id())
5164                    && !set.selections.is_empty()
5165            })
5166            .map(move |(replica_id, set)| {
5167                let start_ix = match set.selections.binary_search_by(|probe| {
5168                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
5169                }) {
5170                    Ok(ix) | Err(ix) => ix,
5171                };
5172                let end_ix = match set.selections.binary_search_by(|probe| {
5173                    probe.start.cmp(&range.end, self).then(Ordering::Less)
5174                }) {
5175                    Ok(ix) | Err(ix) => ix,
5176                };
5177
5178                (
5179                    *replica_id,
5180                    set.line_mode,
5181                    set.cursor_shape,
5182                    set.selections[start_ix..end_ix].iter(),
5183                )
5184            })
5185    }
5186
5187    /// Returns if the buffer contains any diagnostics.
5188    pub fn has_diagnostics(&self) -> bool {
5189        !self.diagnostics.is_empty()
5190    }
5191
5192    /// Returns all the diagnostics intersecting the given range.
5193    pub fn diagnostics_in_range<'a, T, O>(
5194        &'a self,
5195        search_range: Range<T>,
5196        reversed: bool,
5197    ) -> impl 'a + Iterator<Item = DiagnosticEntryRef<'a, O>>
5198    where
5199        T: 'a + Clone + ToOffset,
5200        O: 'a + FromAnchor,
5201    {
5202        let mut iterators: Vec<_> = self
5203            .diagnostics
5204            .iter()
5205            .map(|(_, collection)| {
5206                collection
5207                    .range::<T, text::Anchor>(search_range.clone(), self, true, reversed)
5208                    .peekable()
5209            })
5210            .collect();
5211
5212        std::iter::from_fn(move || {
5213            let (next_ix, _) = iterators
5214                .iter_mut()
5215                .enumerate()
5216                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
5217                .min_by(|(_, a), (_, b)| {
5218                    let cmp = a
5219                        .range
5220                        .start
5221                        .cmp(&b.range.start, self)
5222                        // when range is equal, sort by diagnostic severity
5223                        .then(a.diagnostic.severity.cmp(&b.diagnostic.severity))
5224                        // and stabilize order with group_id
5225                        .then(a.diagnostic.group_id.cmp(&b.diagnostic.group_id));
5226                    if reversed { cmp.reverse() } else { cmp }
5227                })?;
5228            iterators[next_ix]
5229                .next()
5230                .map(
5231                    |DiagnosticEntryRef { range, diagnostic }| DiagnosticEntryRef {
5232                        diagnostic,
5233                        range: FromAnchor::from_anchor(&range.start, self)
5234                            ..FromAnchor::from_anchor(&range.end, self),
5235                    },
5236                )
5237        })
5238    }
5239
5240    /// Returns all the diagnostic groups associated with the given
5241    /// language server ID. If no language server ID is provided,
5242    /// all diagnostics groups are returned.
5243    pub fn diagnostic_groups(
5244        &self,
5245        language_server_id: Option<LanguageServerId>,
5246    ) -> Vec<(LanguageServerId, DiagnosticGroup<'_, Anchor>)> {
5247        let mut groups = Vec::new();
5248
5249        if let Some(language_server_id) = language_server_id {
5250            if let Some(set) = self.diagnostics.get(&language_server_id) {
5251                set.groups(language_server_id, &mut groups, self);
5252            }
5253        } else {
5254            for (language_server_id, diagnostics) in self.diagnostics.iter() {
5255                diagnostics.groups(*language_server_id, &mut groups, self);
5256            }
5257        }
5258
5259        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
5260            let a_start = &group_a.entries[group_a.primary_ix].range.start;
5261            let b_start = &group_b.entries[group_b.primary_ix].range.start;
5262            a_start.cmp(b_start, self).then_with(|| id_a.cmp(id_b))
5263        });
5264
5265        groups
5266    }
5267
5268    /// Returns an iterator over the diagnostics for the given group.
5269    pub fn diagnostic_group<O>(
5270        &self,
5271        group_id: usize,
5272    ) -> impl Iterator<Item = DiagnosticEntryRef<'_, O>> + use<'_, O>
5273    where
5274        O: FromAnchor + 'static,
5275    {
5276        self.diagnostics
5277            .iter()
5278            .flat_map(move |(_, set)| set.group(group_id, self))
5279    }
5280
5281    /// An integer version number that accounts for all updates besides
5282    /// the buffer's text itself (which is versioned via a version vector).
5283    pub fn non_text_state_update_count(&self) -> usize {
5284        self.non_text_state_update_count
5285    }
5286
5287    /// An integer version that changes when the buffer's syntax changes.
5288    pub fn syntax_update_count(&self) -> usize {
5289        self.syntax.update_count()
5290    }
5291
5292    /// Returns a snapshot of underlying file.
5293    pub fn file(&self) -> Option<&Arc<dyn File>> {
5294        self.file.as_ref()
5295    }
5296
5297    pub fn resolve_file_path(&self, include_root: bool, cx: &App) -> Option<String> {
5298        if let Some(file) = self.file() {
5299            if file.path().file_name().is_none() || include_root {
5300                Some(file.full_path(cx).to_string_lossy().into_owned())
5301            } else {
5302                Some(file.path().display(file.path_style(cx)).to_string())
5303            }
5304        } else {
5305            None
5306        }
5307    }
5308
5309    pub fn words_in_range(&self, query: WordsQuery) -> BTreeMap<String, Range<Anchor>> {
5310        let query_str = query.fuzzy_contents;
5311        if query_str.is_some_and(|query| query.is_empty()) {
5312            return BTreeMap::default();
5313        }
5314
5315        let classifier = CharClassifier::new(self.language.clone().map(|language| LanguageScope {
5316            language,
5317            override_id: None,
5318        }));
5319
5320        let mut query_ix = 0;
5321        let query_chars = query_str.map(|query| query.chars().collect::<Vec<_>>());
5322        let query_len = query_chars.as_ref().map_or(0, |query| query.len());
5323
5324        let mut words = BTreeMap::default();
5325        let mut current_word_start_ix = None;
5326        let mut chunk_ix = query.range.start;
5327        for chunk in self.chunks(query.range, false) {
5328            for (i, c) in chunk.text.char_indices() {
5329                let ix = chunk_ix + i;
5330                if classifier.is_word(c) {
5331                    if current_word_start_ix.is_none() {
5332                        current_word_start_ix = Some(ix);
5333                    }
5334
5335                    if let Some(query_chars) = &query_chars
5336                        && query_ix < query_len
5337                        && c.to_lowercase().eq(query_chars[query_ix].to_lowercase())
5338                    {
5339                        query_ix += 1;
5340                    }
5341                    continue;
5342                } else if let Some(word_start) = current_word_start_ix.take()
5343                    && query_ix == query_len
5344                {
5345                    let word_range = self.anchor_before(word_start)..self.anchor_after(ix);
5346                    let mut word_text = self.text_for_range(word_start..ix).peekable();
5347                    let first_char = word_text
5348                        .peek()
5349                        .and_then(|first_chunk| first_chunk.chars().next());
5350                    // Skip empty and "words" starting with digits as a heuristic to reduce useless completions
5351                    if !query.skip_digits
5352                        || first_char.is_none_or(|first_char| !first_char.is_digit(10))
5353                    {
5354                        words.insert(word_text.collect(), word_range);
5355                    }
5356                }
5357                query_ix = 0;
5358            }
5359            chunk_ix += chunk.text.len();
5360        }
5361
5362        words
5363    }
5364}
5365
5366pub struct WordsQuery<'a> {
5367    /// Only returns words with all chars from the fuzzy string in them.
5368    pub fuzzy_contents: Option<&'a str>,
5369    /// Skips words that start with a digit.
5370    pub skip_digits: bool,
5371    /// Buffer offset range, to look for words.
5372    pub range: Range<usize>,
5373}
5374
5375fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
5376    indent_size_for_text(text.chars_at(Point::new(row, 0)))
5377}
5378
5379fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
5380    let mut result = IndentSize::spaces(0);
5381    for c in text {
5382        let kind = match c {
5383            ' ' => IndentKind::Space,
5384            '\t' => IndentKind::Tab,
5385            _ => break,
5386        };
5387        if result.len == 0 {
5388            result.kind = kind;
5389        }
5390        result.len += 1;
5391    }
5392    result
5393}
5394
5395impl Clone for BufferSnapshot {
5396    fn clone(&self) -> Self {
5397        Self {
5398            text: self.text.clone(),
5399            syntax: self.syntax.clone(),
5400            file: self.file.clone(),
5401            remote_selections: self.remote_selections.clone(),
5402            diagnostics: self.diagnostics.clone(),
5403            language: self.language.clone(),
5404            tree_sitter_data: self.tree_sitter_data.clone(),
5405            non_text_state_update_count: self.non_text_state_update_count,
5406            capability: self.capability,
5407        }
5408    }
5409}
5410
5411impl Deref for BufferSnapshot {
5412    type Target = text::BufferSnapshot;
5413
5414    fn deref(&self) -> &Self::Target {
5415        &self.text
5416    }
5417}
5418
5419unsafe impl Send for BufferChunks<'_> {}
5420
5421impl<'a> BufferChunks<'a> {
5422    pub(crate) fn new(
5423        text: &'a Rope,
5424        range: Range<usize>,
5425        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
5426        diagnostics: bool,
5427        buffer_snapshot: Option<&'a BufferSnapshot>,
5428    ) -> Self {
5429        let mut highlights = None;
5430        if let Some((captures, highlight_maps)) = syntax {
5431            highlights = Some(BufferChunkHighlights {
5432                captures,
5433                next_capture: None,
5434                stack: Default::default(),
5435                highlight_maps,
5436            })
5437        }
5438
5439        let diagnostic_endpoints = diagnostics.then(|| Vec::new().into_iter().peekable());
5440        let chunks = text.chunks_in_range(range.clone());
5441
5442        let mut this = BufferChunks {
5443            range,
5444            buffer_snapshot,
5445            chunks,
5446            diagnostic_endpoints,
5447            error_depth: 0,
5448            warning_depth: 0,
5449            information_depth: 0,
5450            hint_depth: 0,
5451            unnecessary_depth: 0,
5452            underline: true,
5453            highlights,
5454        };
5455        this.initialize_diagnostic_endpoints();
5456        this
5457    }
5458
5459    /// Seeks to the given byte offset in the buffer.
5460    pub fn seek(&mut self, range: Range<usize>) {
5461        let old_range = std::mem::replace(&mut self.range, range.clone());
5462        self.chunks.set_range(self.range.clone());
5463        if let Some(highlights) = self.highlights.as_mut() {
5464            if old_range.start <= self.range.start && old_range.end >= self.range.end {
5465                // Reuse existing highlights stack, as the new range is a subrange of the old one.
5466                highlights
5467                    .stack
5468                    .retain(|(end_offset, _)| *end_offset > range.start);
5469                if let Some(capture) = &highlights.next_capture
5470                    && range.start >= capture.node.start_byte()
5471                {
5472                    let next_capture_end = capture.node.end_byte();
5473                    if range.start < next_capture_end {
5474                        highlights.stack.push((
5475                            next_capture_end,
5476                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
5477                        ));
5478                    }
5479                    highlights.next_capture.take();
5480                }
5481            } else if let Some(snapshot) = self.buffer_snapshot {
5482                let (captures, highlight_maps) = snapshot.get_highlights(self.range.clone());
5483                *highlights = BufferChunkHighlights {
5484                    captures,
5485                    next_capture: None,
5486                    stack: Default::default(),
5487                    highlight_maps,
5488                };
5489            } else {
5490                // We cannot obtain new highlights for a language-aware buffer iterator, as we don't have a buffer snapshot.
5491                // Seeking such BufferChunks is not supported.
5492                debug_assert!(
5493                    false,
5494                    "Attempted to seek on a language-aware buffer iterator without associated buffer snapshot"
5495                );
5496            }
5497
5498            highlights.captures.set_byte_range(self.range.clone());
5499            self.initialize_diagnostic_endpoints();
5500        }
5501    }
5502
5503    fn initialize_diagnostic_endpoints(&mut self) {
5504        if let Some(diagnostics) = self.diagnostic_endpoints.as_mut()
5505            && let Some(buffer) = self.buffer_snapshot
5506        {
5507            let mut diagnostic_endpoints = Vec::new();
5508            for entry in buffer.diagnostics_in_range::<_, usize>(self.range.clone(), false) {
5509                diagnostic_endpoints.push(DiagnosticEndpoint {
5510                    offset: entry.range.start,
5511                    is_start: true,
5512                    severity: entry.diagnostic.severity,
5513                    is_unnecessary: entry.diagnostic.is_unnecessary,
5514                    underline: entry.diagnostic.underline,
5515                });
5516                diagnostic_endpoints.push(DiagnosticEndpoint {
5517                    offset: entry.range.end,
5518                    is_start: false,
5519                    severity: entry.diagnostic.severity,
5520                    is_unnecessary: entry.diagnostic.is_unnecessary,
5521                    underline: entry.diagnostic.underline,
5522                });
5523            }
5524            diagnostic_endpoints
5525                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
5526            *diagnostics = diagnostic_endpoints.into_iter().peekable();
5527            self.hint_depth = 0;
5528            self.error_depth = 0;
5529            self.warning_depth = 0;
5530            self.information_depth = 0;
5531        }
5532    }
5533
5534    /// The current byte offset in the buffer.
5535    pub fn offset(&self) -> usize {
5536        self.range.start
5537    }
5538
5539    pub fn range(&self) -> Range<usize> {
5540        self.range.clone()
5541    }
5542
5543    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
5544        let depth = match endpoint.severity {
5545            DiagnosticSeverity::ERROR => &mut self.error_depth,
5546            DiagnosticSeverity::WARNING => &mut self.warning_depth,
5547            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
5548            DiagnosticSeverity::HINT => &mut self.hint_depth,
5549            _ => return,
5550        };
5551        if endpoint.is_start {
5552            *depth += 1;
5553        } else {
5554            *depth -= 1;
5555        }
5556
5557        if endpoint.is_unnecessary {
5558            if endpoint.is_start {
5559                self.unnecessary_depth += 1;
5560            } else {
5561                self.unnecessary_depth -= 1;
5562            }
5563        }
5564    }
5565
5566    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
5567        if self.error_depth > 0 {
5568            Some(DiagnosticSeverity::ERROR)
5569        } else if self.warning_depth > 0 {
5570            Some(DiagnosticSeverity::WARNING)
5571        } else if self.information_depth > 0 {
5572            Some(DiagnosticSeverity::INFORMATION)
5573        } else if self.hint_depth > 0 {
5574            Some(DiagnosticSeverity::HINT)
5575        } else {
5576            None
5577        }
5578    }
5579
5580    fn current_code_is_unnecessary(&self) -> bool {
5581        self.unnecessary_depth > 0
5582    }
5583}
5584
5585impl<'a> Iterator for BufferChunks<'a> {
5586    type Item = Chunk<'a>;
5587
5588    fn next(&mut self) -> Option<Self::Item> {
5589        let mut next_capture_start = usize::MAX;
5590        let mut next_diagnostic_endpoint = usize::MAX;
5591
5592        if let Some(highlights) = self.highlights.as_mut() {
5593            while let Some((parent_capture_end, _)) = highlights.stack.last() {
5594                if *parent_capture_end <= self.range.start {
5595                    highlights.stack.pop();
5596                } else {
5597                    break;
5598                }
5599            }
5600
5601            if highlights.next_capture.is_none() {
5602                highlights.next_capture = highlights.captures.next();
5603            }
5604
5605            while let Some(capture) = highlights.next_capture.as_ref() {
5606                if self.range.start < capture.node.start_byte() {
5607                    next_capture_start = capture.node.start_byte();
5608                    break;
5609                } else {
5610                    let highlight_id =
5611                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
5612                    highlights
5613                        .stack
5614                        .push((capture.node.end_byte(), highlight_id));
5615                    highlights.next_capture = highlights.captures.next();
5616                }
5617            }
5618        }
5619
5620        let mut diagnostic_endpoints = std::mem::take(&mut self.diagnostic_endpoints);
5621        if let Some(diagnostic_endpoints) = diagnostic_endpoints.as_mut() {
5622            while let Some(endpoint) = diagnostic_endpoints.peek().copied() {
5623                if endpoint.offset <= self.range.start {
5624                    self.update_diagnostic_depths(endpoint);
5625                    diagnostic_endpoints.next();
5626                    self.underline = endpoint.underline;
5627                } else {
5628                    next_diagnostic_endpoint = endpoint.offset;
5629                    break;
5630                }
5631            }
5632        }
5633        self.diagnostic_endpoints = diagnostic_endpoints;
5634
5635        if let Some(ChunkBitmaps {
5636            text: chunk,
5637            chars: chars_map,
5638            tabs,
5639        }) = self.chunks.peek_with_bitmaps()
5640        {
5641            let chunk_start = self.range.start;
5642            let mut chunk_end = (self.chunks.offset() + chunk.len())
5643                .min(next_capture_start)
5644                .min(next_diagnostic_endpoint);
5645            let mut highlight_id = None;
5646            if let Some(highlights) = self.highlights.as_ref()
5647                && let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last()
5648            {
5649                chunk_end = chunk_end.min(*parent_capture_end);
5650                highlight_id = Some(*parent_highlight_id);
5651            }
5652            let bit_start = chunk_start - self.chunks.offset();
5653            let bit_end = chunk_end - self.chunks.offset();
5654
5655            let slice = &chunk[bit_start..bit_end];
5656
5657            let mask = 1u128.unbounded_shl(bit_end as u32).wrapping_sub(1);
5658            let tabs = (tabs >> bit_start) & mask;
5659            let chars = (chars_map >> bit_start) & mask;
5660
5661            self.range.start = chunk_end;
5662            if self.range.start == self.chunks.offset() + chunk.len() {
5663                self.chunks.next().unwrap();
5664            }
5665
5666            Some(Chunk {
5667                text: slice,
5668                syntax_highlight_id: highlight_id,
5669                underline: self.underline,
5670                diagnostic_severity: self.current_diagnostic_severity(),
5671                is_unnecessary: self.current_code_is_unnecessary(),
5672                tabs,
5673                chars,
5674                ..Chunk::default()
5675            })
5676        } else {
5677            None
5678        }
5679    }
5680}
5681
5682impl operation_queue::Operation for Operation {
5683    fn lamport_timestamp(&self) -> clock::Lamport {
5684        match self {
5685            Operation::Buffer(_) => {
5686                unreachable!("buffer operations should never be deferred at this layer")
5687            }
5688            Operation::UpdateDiagnostics {
5689                lamport_timestamp, ..
5690            }
5691            | Operation::UpdateSelections {
5692                lamport_timestamp, ..
5693            }
5694            | Operation::UpdateCompletionTriggers {
5695                lamport_timestamp, ..
5696            }
5697            | Operation::UpdateLineEnding {
5698                lamport_timestamp, ..
5699            } => *lamport_timestamp,
5700        }
5701    }
5702}
5703
5704impl Default for Diagnostic {
5705    fn default() -> Self {
5706        Self {
5707            source: Default::default(),
5708            source_kind: DiagnosticSourceKind::Other,
5709            code: None,
5710            code_description: None,
5711            severity: DiagnosticSeverity::ERROR,
5712            message: Default::default(),
5713            markdown: None,
5714            group_id: 0,
5715            is_primary: false,
5716            is_disk_based: false,
5717            is_unnecessary: false,
5718            underline: true,
5719            data: None,
5720            registration_id: None,
5721        }
5722    }
5723}
5724
5725impl IndentSize {
5726    /// Returns an [`IndentSize`] representing the given spaces.
5727    pub fn spaces(len: u32) -> Self {
5728        Self {
5729            len,
5730            kind: IndentKind::Space,
5731        }
5732    }
5733
5734    /// Returns an [`IndentSize`] representing a tab.
5735    pub fn tab() -> Self {
5736        Self {
5737            len: 1,
5738            kind: IndentKind::Tab,
5739        }
5740    }
5741
5742    /// An iterator over the characters represented by this [`IndentSize`].
5743    pub fn chars(&self) -> impl Iterator<Item = char> {
5744        iter::repeat(self.char()).take(self.len as usize)
5745    }
5746
5747    /// The character representation of this [`IndentSize`].
5748    pub fn char(&self) -> char {
5749        match self.kind {
5750            IndentKind::Space => ' ',
5751            IndentKind::Tab => '\t',
5752        }
5753    }
5754
5755    /// Consumes the current [`IndentSize`] and returns a new one that has
5756    /// been shrunk or enlarged by the given size along the given direction.
5757    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
5758        match direction {
5759            Ordering::Less => {
5760                if self.kind == size.kind && self.len >= size.len {
5761                    self.len -= size.len;
5762                }
5763            }
5764            Ordering::Equal => {}
5765            Ordering::Greater => {
5766                if self.len == 0 {
5767                    self = size;
5768                } else if self.kind == size.kind {
5769                    self.len += size.len;
5770                }
5771            }
5772        }
5773        self
5774    }
5775
5776    pub fn len_with_expanded_tabs(&self, tab_size: NonZeroU32) -> usize {
5777        match self.kind {
5778            IndentKind::Space => self.len as usize,
5779            IndentKind::Tab => self.len as usize * tab_size.get() as usize,
5780        }
5781    }
5782}
5783
5784#[cfg(any(test, feature = "test-support"))]
5785pub struct TestFile {
5786    pub path: Arc<RelPath>,
5787    pub root_name: String,
5788    pub local_root: Option<PathBuf>,
5789}
5790
5791#[cfg(any(test, feature = "test-support"))]
5792impl File for TestFile {
5793    fn path(&self) -> &Arc<RelPath> {
5794        &self.path
5795    }
5796
5797    fn full_path(&self, _: &gpui::App) -> PathBuf {
5798        PathBuf::from(self.root_name.clone()).join(self.path.as_std_path())
5799    }
5800
5801    fn as_local(&self) -> Option<&dyn LocalFile> {
5802        if self.local_root.is_some() {
5803            Some(self)
5804        } else {
5805            None
5806        }
5807    }
5808
5809    fn disk_state(&self) -> DiskState {
5810        unimplemented!()
5811    }
5812
5813    fn file_name<'a>(&'a self, _: &'a gpui::App) -> &'a str {
5814        self.path().file_name().unwrap_or(self.root_name.as_ref())
5815    }
5816
5817    fn worktree_id(&self, _: &App) -> WorktreeId {
5818        WorktreeId::from_usize(0)
5819    }
5820
5821    fn to_proto(&self, _: &App) -> rpc::proto::File {
5822        unimplemented!()
5823    }
5824
5825    fn is_private(&self) -> bool {
5826        false
5827    }
5828
5829    fn path_style(&self, _cx: &App) -> PathStyle {
5830        PathStyle::local()
5831    }
5832}
5833
5834#[cfg(any(test, feature = "test-support"))]
5835impl LocalFile for TestFile {
5836    fn abs_path(&self, _cx: &App) -> PathBuf {
5837        PathBuf::from(self.local_root.as_ref().unwrap())
5838            .join(&self.root_name)
5839            .join(self.path.as_std_path())
5840    }
5841
5842    fn load(&self, _cx: &App) -> Task<Result<String>> {
5843        unimplemented!()
5844    }
5845
5846    fn load_bytes(&self, _cx: &App) -> Task<Result<Vec<u8>>> {
5847        unimplemented!()
5848    }
5849}
5850
5851pub(crate) fn contiguous_ranges(
5852    values: impl Iterator<Item = u32>,
5853    max_len: usize,
5854) -> impl Iterator<Item = Range<u32>> {
5855    let mut values = values;
5856    let mut current_range: Option<Range<u32>> = None;
5857    std::iter::from_fn(move || {
5858        loop {
5859            if let Some(value) = values.next() {
5860                if let Some(range) = &mut current_range
5861                    && value == range.end
5862                    && range.len() < max_len
5863                {
5864                    range.end += 1;
5865                    continue;
5866                }
5867
5868                let prev_range = current_range.clone();
5869                current_range = Some(value..(value + 1));
5870                if prev_range.is_some() {
5871                    return prev_range;
5872                }
5873            } else {
5874                return current_range.take();
5875            }
5876        }
5877    })
5878}
5879
5880#[derive(Default, Debug)]
5881pub struct CharClassifier {
5882    scope: Option<LanguageScope>,
5883    scope_context: Option<CharScopeContext>,
5884    ignore_punctuation: bool,
5885}
5886
5887impl CharClassifier {
5888    pub fn new(scope: Option<LanguageScope>) -> Self {
5889        Self {
5890            scope,
5891            scope_context: None,
5892            ignore_punctuation: false,
5893        }
5894    }
5895
5896    pub fn scope_context(self, scope_context: Option<CharScopeContext>) -> Self {
5897        Self {
5898            scope_context,
5899            ..self
5900        }
5901    }
5902
5903    pub fn ignore_punctuation(self, ignore_punctuation: bool) -> Self {
5904        Self {
5905            ignore_punctuation,
5906            ..self
5907        }
5908    }
5909
5910    pub fn is_whitespace(&self, c: char) -> bool {
5911        self.kind(c) == CharKind::Whitespace
5912    }
5913
5914    pub fn is_word(&self, c: char) -> bool {
5915        self.kind(c) == CharKind::Word
5916    }
5917
5918    pub fn is_punctuation(&self, c: char) -> bool {
5919        self.kind(c) == CharKind::Punctuation
5920    }
5921
5922    pub fn kind_with(&self, c: char, ignore_punctuation: bool) -> CharKind {
5923        if c.is_alphanumeric() || c == '_' {
5924            return CharKind::Word;
5925        }
5926
5927        if let Some(scope) = &self.scope {
5928            let characters = match self.scope_context {
5929                Some(CharScopeContext::Completion) => scope.completion_query_characters(),
5930                Some(CharScopeContext::LinkedEdit) => scope.linked_edit_characters(),
5931                None => scope.word_characters(),
5932            };
5933            if let Some(characters) = characters
5934                && characters.contains(&c)
5935            {
5936                return CharKind::Word;
5937            }
5938        }
5939
5940        if c.is_whitespace() {
5941            return CharKind::Whitespace;
5942        }
5943
5944        if ignore_punctuation {
5945            CharKind::Word
5946        } else {
5947            CharKind::Punctuation
5948        }
5949    }
5950
5951    pub fn kind(&self, c: char) -> CharKind {
5952        self.kind_with(c, self.ignore_punctuation)
5953    }
5954}
5955
5956/// Find all of the ranges of whitespace that occur at the ends of lines
5957/// in the given rope.
5958///
5959/// This could also be done with a regex search, but this implementation
5960/// avoids copying text.
5961pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
5962    let mut ranges = Vec::new();
5963
5964    let mut offset = 0;
5965    let mut prev_chunk_trailing_whitespace_range = 0..0;
5966    for chunk in rope.chunks() {
5967        let mut prev_line_trailing_whitespace_range = 0..0;
5968        for (i, line) in chunk.split('\n').enumerate() {
5969            let line_end_offset = offset + line.len();
5970            let trimmed_line_len = line.trim_end_matches([' ', '\t']).len();
5971            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
5972
5973            if i == 0 && trimmed_line_len == 0 {
5974                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
5975            }
5976            if !prev_line_trailing_whitespace_range.is_empty() {
5977                ranges.push(prev_line_trailing_whitespace_range);
5978            }
5979
5980            offset = line_end_offset + 1;
5981            prev_line_trailing_whitespace_range = trailing_whitespace_range;
5982        }
5983
5984        offset -= 1;
5985        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
5986    }
5987
5988    if !prev_chunk_trailing_whitespace_range.is_empty() {
5989        ranges.push(prev_chunk_trailing_whitespace_range);
5990    }
5991
5992    ranges
5993}