buffer.rs

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