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