buffer.rs

   1pub use crate::{
   2    diagnostic_set::DiagnosticSet,
   3    highlight_map::{HighlightId, HighlightMap},
   4    markdown::ParsedMarkdown,
   5    proto, Grammar, Language, LanguageRegistry,
   6};
   7use crate::{
   8    diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
   9    language_settings::{language_settings, IndentGuideSettings, LanguageSettings},
  10    markdown::parse_markdown,
  11    outline::OutlineItem,
  12    syntax_map::{
  13        SyntaxLayer, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxMapMatch,
  14        SyntaxMapMatches, SyntaxSnapshot, ToTreeSitterPoint,
  15    },
  16    task_context::RunnableRange,
  17    LanguageScope, Outline, OutlineConfig, RunnableCapture, RunnableTag,
  18};
  19use anyhow::{anyhow, Context, Result};
  20use async_watch as watch;
  21pub use clock::ReplicaId;
  22use futures::channel::oneshot;
  23use gpui::{
  24    AnyElement, AppContext, Context as _, EventEmitter, HighlightStyle, Model, ModelContext,
  25    Pixels, Task, TaskLabel, WindowContext,
  26};
  27use lsp::LanguageServerId;
  28use parking_lot::Mutex;
  29use schemars::JsonSchema;
  30use serde::{Deserialize, Serialize};
  31use serde_json::Value;
  32use settings::WorktreeId;
  33use similar::{ChangeTag, TextDiff};
  34use smallvec::SmallVec;
  35use smol::future::yield_now;
  36use std::{
  37    any::Any,
  38    cell::Cell,
  39    cmp::{self, Ordering, Reverse},
  40    collections::BTreeMap,
  41    ffi::OsStr,
  42    fmt,
  43    future::Future,
  44    iter::{self, Iterator, Peekable},
  45    mem,
  46    ops::{Deref, DerefMut, Range},
  47    path::{Path, PathBuf},
  48    str,
  49    sync::{Arc, LazyLock},
  50    time::{Duration, Instant, SystemTime},
  51    vec,
  52};
  53use sum_tree::TreeMap;
  54use text::operation_queue::OperationQueue;
  55use text::*;
  56pub use text::{
  57    Anchor, Bias, Buffer as TextBuffer, BufferId, BufferSnapshot as TextBufferSnapshot, Edit,
  58    OffsetRangeExt, OffsetUtf16, Patch, Point, PointUtf16, Rope, Selection, SelectionGoal,
  59    Subscription, TextDimension, TextSummary, ToOffset, ToOffsetUtf16, ToPoint, ToPointUtf16,
  60    Transaction, TransactionId, Unclipped,
  61};
  62use theme::SyntaxTheme;
  63#[cfg(any(test, feature = "test-support"))]
  64use util::RandomCharIter;
  65use util::{debug_panic, RangeExt};
  66
  67#[cfg(any(test, feature = "test-support"))]
  68pub use {tree_sitter_rust, tree_sitter_typescript};
  69
  70pub use lsp::DiagnosticSeverity;
  71
  72/// A label for the background task spawned by the buffer to compute
  73/// a diff against the contents of its file.
  74pub static BUFFER_DIFF_TASK: LazyLock<TaskLabel> = LazyLock::new(TaskLabel::new);
  75
  76/// Indicate whether a [Buffer] has permissions to edit.
  77#[derive(PartialEq, Clone, Copy, Debug)]
  78pub enum Capability {
  79    /// The buffer is a mutable replica.
  80    ReadWrite,
  81    /// The buffer is a read-only replica.
  82    ReadOnly,
  83}
  84
  85pub type BufferRow = u32;
  86
  87#[derive(Clone)]
  88enum BufferDiffBase {
  89    Git(Rope),
  90    PastBufferVersion {
  91        buffer: Model<Buffer>,
  92        rope: Rope,
  93        operations_to_ignore: Vec<clock::Lamport>,
  94    },
  95}
  96
  97/// An in-memory representation of a source code file, including its text,
  98/// syntax trees, git status, and diagnostics.
  99pub struct Buffer {
 100    text: TextBuffer,
 101    diff_base: Option<BufferDiffBase>,
 102    git_diff: git::diff::BufferDiff,
 103    file: Option<Arc<dyn File>>,
 104    /// The mtime of the file when this buffer was last loaded from
 105    /// or saved to disk.
 106    saved_mtime: Option<SystemTime>,
 107    /// The version vector when this buffer was last loaded from
 108    /// or saved to disk.
 109    saved_version: clock::Global,
 110    preview_version: clock::Global,
 111    transaction_depth: usize,
 112    was_dirty_before_starting_transaction: Option<bool>,
 113    reload_task: Option<Task<Result<()>>>,
 114    language: Option<Arc<Language>>,
 115    autoindent_requests: Vec<Arc<AutoindentRequest>>,
 116    pending_autoindent: Option<Task<()>>,
 117    sync_parse_timeout: Duration,
 118    syntax_map: Mutex<SyntaxMap>,
 119    parsing_in_background: bool,
 120    parse_status: (watch::Sender<ParseStatus>, watch::Receiver<ParseStatus>),
 121    non_text_state_update_count: usize,
 122    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
 123    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 124    diagnostics_timestamp: clock::Lamport,
 125    completion_triggers: Vec<String>,
 126    completion_triggers_timestamp: clock::Lamport,
 127    deferred_ops: OperationQueue<Operation>,
 128    capability: Capability,
 129    has_conflict: bool,
 130    diff_base_version: usize,
 131    /// Memoize calls to has_changes_since(saved_version).
 132    /// The contents of a cell are (self.version, has_changes) at the time of a last call.
 133    has_unsaved_edits: Cell<(clock::Global, bool)>,
 134    _subscriptions: Vec<gpui::Subscription>,
 135}
 136
 137#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 138pub enum ParseStatus {
 139    Idle,
 140    Parsing,
 141}
 142
 143/// An immutable, cheaply cloneable representation of a fixed
 144/// state of a buffer.
 145pub struct BufferSnapshot {
 146    text: text::BufferSnapshot,
 147    git_diff: git::diff::BufferDiff,
 148    pub(crate) syntax: SyntaxSnapshot,
 149    file: Option<Arc<dyn File>>,
 150    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
 151    remote_selections: TreeMap<ReplicaId, SelectionSet>,
 152    language: Option<Arc<Language>>,
 153    non_text_state_update_count: usize,
 154}
 155
 156/// The kind and amount of indentation in a particular line. For now,
 157/// assumes that indentation is all the same character.
 158#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
 159pub struct IndentSize {
 160    /// The number of bytes that comprise the indentation.
 161    pub len: u32,
 162    /// The kind of whitespace used for indentation.
 163    pub kind: IndentKind,
 164}
 165
 166/// A whitespace character that's used for indentation.
 167#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
 168pub enum IndentKind {
 169    /// An ASCII space character.
 170    #[default]
 171    Space,
 172    /// An ASCII tab character.
 173    Tab,
 174}
 175
 176/// The shape of a selection cursor.
 177#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 178#[serde(rename_all = "snake_case")]
 179pub enum CursorShape {
 180    /// A vertical bar
 181    #[default]
 182    Bar,
 183    /// A block that surrounds the following character
 184    Block,
 185    /// An underline that runs along the following character
 186    Underscore,
 187    /// A box drawn around the following character
 188    Hollow,
 189}
 190
 191#[derive(Clone, Debug)]
 192struct SelectionSet {
 193    line_mode: bool,
 194    cursor_shape: CursorShape,
 195    selections: Arc<[Selection<Anchor>]>,
 196    lamport_timestamp: clock::Lamport,
 197}
 198
 199/// A diagnostic associated with a certain range of a buffer.
 200#[derive(Clone, Debug, PartialEq, Eq)]
 201pub struct Diagnostic {
 202    /// The name of the service that produced this diagnostic.
 203    pub source: Option<String>,
 204    /// A machine-readable code that identifies this diagnostic.
 205    pub code: Option<String>,
 206    /// Whether this diagnostic is a hint, warning, or error.
 207    pub severity: DiagnosticSeverity,
 208    /// The human-readable message associated with this diagnostic.
 209    pub message: String,
 210    /// An id that identifies the group to which this diagnostic belongs.
 211    ///
 212    /// When a language server produces a diagnostic with
 213    /// one or more associated diagnostics, those diagnostics are all
 214    /// assigned a single group id.
 215    pub group_id: usize,
 216    /// Whether this diagnostic is the primary diagnostic for its group.
 217    ///
 218    /// In a given group, the primary diagnostic is the top-level diagnostic
 219    /// returned by the language server. The non-primary diagnostics are the
 220    /// associated diagnostics.
 221    pub is_primary: bool,
 222    /// Whether this diagnostic is considered to originate from an analysis of
 223    /// files on disk, as opposed to any unsaved buffer contents. This is a
 224    /// property of a given diagnostic source, and is configured for a given
 225    /// language server via the [`LspAdapter::disk_based_diagnostic_sources`](crate::LspAdapter::disk_based_diagnostic_sources) method
 226    /// for the language server.
 227    pub is_disk_based: bool,
 228    /// Whether this diagnostic marks unnecessary code.
 229    pub is_unnecessary: bool,
 230    /// Data from language server that produced this diagnostic. Passed back to the LS when we request code actions for this diagnostic.
 231    pub data: Option<Value>,
 232}
 233
 234/// TODO - move this into the `project` crate and make it private.
 235pub async fn prepare_completion_documentation(
 236    documentation: &lsp::Documentation,
 237    language_registry: &Arc<LanguageRegistry>,
 238    language: Option<Arc<Language>>,
 239) -> Documentation {
 240    match documentation {
 241        lsp::Documentation::String(text) => {
 242            if text.lines().count() <= 1 {
 243                Documentation::SingleLine(text.clone())
 244            } else {
 245                Documentation::MultiLinePlainText(text.clone())
 246            }
 247        }
 248
 249        lsp::Documentation::MarkupContent(lsp::MarkupContent { kind, value }) => match kind {
 250            lsp::MarkupKind::PlainText => {
 251                if value.lines().count() <= 1 {
 252                    Documentation::SingleLine(value.clone())
 253                } else {
 254                    Documentation::MultiLinePlainText(value.clone())
 255                }
 256            }
 257
 258            lsp::MarkupKind::Markdown => {
 259                let parsed = parse_markdown(value, language_registry, language).await;
 260                Documentation::MultiLineMarkdown(parsed)
 261            }
 262        },
 263    }
 264}
 265
 266/// Documentation associated with a [`Completion`].
 267#[derive(Clone, Debug)]
 268pub enum Documentation {
 269    /// There is no documentation for this completion.
 270    Undocumented,
 271    /// A single line of documentation.
 272    SingleLine(String),
 273    /// Multiple lines of plain text documentation.
 274    MultiLinePlainText(String),
 275    /// Markdown documentation.
 276    MultiLineMarkdown(ParsedMarkdown),
 277}
 278
 279/// An operation used to synchronize this buffer with its other replicas.
 280#[derive(Clone, Debug, PartialEq)]
 281pub enum Operation {
 282    /// A text operation.
 283    Buffer(text::Operation),
 284
 285    /// An update to the buffer's diagnostics.
 286    UpdateDiagnostics {
 287        /// The id of the language server that produced the new diagnostics.
 288        server_id: LanguageServerId,
 289        /// The diagnostics.
 290        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 291        /// The buffer's lamport timestamp.
 292        lamport_timestamp: clock::Lamport,
 293    },
 294
 295    /// An update to the most recent selections in this buffer.
 296    UpdateSelections {
 297        /// The selections.
 298        selections: Arc<[Selection<Anchor>]>,
 299        /// The buffer's lamport timestamp.
 300        lamport_timestamp: clock::Lamport,
 301        /// Whether the selections are in 'line mode'.
 302        line_mode: bool,
 303        /// The [`CursorShape`] associated with these selections.
 304        cursor_shape: CursorShape,
 305    },
 306
 307    /// An update to the characters that should trigger autocompletion
 308    /// for this buffer.
 309    UpdateCompletionTriggers {
 310        /// The characters that trigger autocompletion.
 311        triggers: Vec<String>,
 312        /// The buffer's lamport timestamp.
 313        lamport_timestamp: clock::Lamport,
 314    },
 315}
 316
 317/// An event that occurs in a buffer.
 318#[derive(Clone, Debug, PartialEq)]
 319pub enum BufferEvent {
 320    /// The buffer was changed in a way that must be
 321    /// propagated to its other replicas.
 322    Operation {
 323        operation: Operation,
 324        is_local: bool,
 325    },
 326    /// The buffer was edited.
 327    Edited,
 328    /// The buffer's `dirty` bit changed.
 329    DirtyChanged,
 330    /// The buffer was saved.
 331    Saved,
 332    /// The buffer's file was changed on disk.
 333    FileHandleChanged,
 334    /// The buffer was reloaded.
 335    Reloaded,
 336    /// The buffer's diff_base changed.
 337    DiffBaseChanged,
 338    /// Buffer's excerpts for a certain diff base were recalculated.
 339    DiffUpdated,
 340    /// The buffer's language was changed.
 341    LanguageChanged,
 342    /// The buffer's syntax trees were updated.
 343    Reparsed,
 344    /// The buffer's diagnostics were updated.
 345    DiagnosticsUpdated,
 346    /// The buffer gained or lost editing capabilities.
 347    CapabilityChanged,
 348    /// The buffer was explicitly requested to close.
 349    Closed,
 350    /// The buffer was discarded when closing.
 351    Discarded,
 352}
 353
 354/// The file associated with a buffer.
 355pub trait File: Send + Sync {
 356    /// Returns the [`LocalFile`] associated with this file, if the
 357    /// file is local.
 358    fn as_local(&self) -> Option<&dyn LocalFile>;
 359
 360    /// Returns whether this file is local.
 361    fn is_local(&self) -> bool {
 362        self.as_local().is_some()
 363    }
 364
 365    /// Returns the file's mtime.
 366    fn mtime(&self) -> Option<SystemTime>;
 367
 368    /// Returns the path of this file relative to the worktree's root directory.
 369    fn path(&self) -> &Arc<Path>;
 370
 371    /// Returns the path of this file relative to the worktree's parent directory (this means it
 372    /// includes the name of the worktree's root folder).
 373    fn full_path(&self, cx: &AppContext) -> PathBuf;
 374
 375    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 376    /// of its worktree, then this method will return the name of the worktree itself.
 377    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
 378
 379    /// Returns the id of the worktree to which this file belongs.
 380    ///
 381    /// This is needed for looking up project-specific settings.
 382    fn worktree_id(&self, cx: &AppContext) -> WorktreeId;
 383
 384    /// Returns whether the file has been deleted.
 385    fn is_deleted(&self) -> bool;
 386
 387    /// Returns whether the file existed on disk at one point
 388    fn is_created(&self) -> bool {
 389        self.mtime().is_some()
 390    }
 391
 392    /// Converts this file into an [`Any`] trait object.
 393    fn as_any(&self) -> &dyn Any;
 394
 395    /// Converts this file into a protobuf message.
 396    fn to_proto(&self, cx: &AppContext) -> rpc::proto::File;
 397
 398    /// Return whether Zed considers this to be a private file.
 399    fn is_private(&self) -> bool;
 400}
 401
 402/// The file associated with a buffer, in the case where the file is on the local disk.
 403pub trait LocalFile: File {
 404    /// Returns the absolute path of this file
 405    fn abs_path(&self, cx: &AppContext) -> PathBuf;
 406
 407    /// Loads the file's contents from disk.
 408    fn load(&self, cx: &AppContext) -> Task<Result<String>>;
 409
 410    /// Returns true if the file should not be shared with collaborators.
 411    fn is_private(&self, _: &AppContext) -> bool {
 412        false
 413    }
 414}
 415
 416/// The auto-indent behavior associated with an editing operation.
 417/// For some editing operations, each affected line of text has its
 418/// indentation recomputed. For other operations, the entire block
 419/// of edited text is adjusted uniformly.
 420#[derive(Clone, Debug)]
 421pub enum AutoindentMode {
 422    /// Indent each line of inserted text.
 423    EachLine,
 424    /// Apply the same indentation adjustment to all of the lines
 425    /// in a given insertion.
 426    Block {
 427        /// The original indentation level of the first line of each
 428        /// insertion, if it has been copied.
 429        original_indent_columns: Vec<u32>,
 430    },
 431}
 432
 433#[derive(Clone)]
 434struct AutoindentRequest {
 435    before_edit: BufferSnapshot,
 436    entries: Vec<AutoindentRequestEntry>,
 437    is_block_mode: bool,
 438}
 439
 440#[derive(Clone)]
 441struct AutoindentRequestEntry {
 442    /// A range of the buffer whose indentation should be adjusted.
 443    range: Range<Anchor>,
 444    /// Whether or not these lines should be considered brand new, for the
 445    /// purpose of auto-indent. When text is not new, its indentation will
 446    /// only be adjusted if the suggested indentation level has *changed*
 447    /// since the edit was made.
 448    first_line_is_new: bool,
 449    indent_size: IndentSize,
 450    original_indent_column: Option<u32>,
 451}
 452
 453#[derive(Debug)]
 454struct IndentSuggestion {
 455    basis_row: u32,
 456    delta: Ordering,
 457    within_error: bool,
 458}
 459
 460struct BufferChunkHighlights<'a> {
 461    captures: SyntaxMapCaptures<'a>,
 462    next_capture: Option<SyntaxMapCapture<'a>>,
 463    stack: Vec<(usize, HighlightId)>,
 464    highlight_maps: Vec<HighlightMap>,
 465}
 466
 467/// An iterator that yields chunks of a buffer's text, along with their
 468/// syntax highlights and diagnostic status.
 469pub struct BufferChunks<'a> {
 470    buffer_snapshot: Option<&'a BufferSnapshot>,
 471    range: Range<usize>,
 472    chunks: text::Chunks<'a>,
 473    diagnostic_endpoints: Option<Peekable<vec::IntoIter<DiagnosticEndpoint>>>,
 474    error_depth: usize,
 475    warning_depth: usize,
 476    information_depth: usize,
 477    hint_depth: usize,
 478    unnecessary_depth: usize,
 479    highlights: Option<BufferChunkHighlights<'a>>,
 480}
 481
 482/// A chunk of a buffer's text, along with its syntax highlight and
 483/// diagnostic status.
 484#[derive(Clone, Debug, Default)]
 485pub struct Chunk<'a> {
 486    /// The text of the chunk.
 487    pub text: &'a str,
 488    /// The syntax highlighting style of the chunk.
 489    pub syntax_highlight_id: Option<HighlightId>,
 490    /// The highlight style that has been applied to this chunk in
 491    /// the editor.
 492    pub highlight_style: Option<HighlightStyle>,
 493    /// The severity of diagnostic associated with this chunk, if any.
 494    pub diagnostic_severity: Option<DiagnosticSeverity>,
 495    /// Whether this chunk of text is marked as unnecessary.
 496    pub is_unnecessary: bool,
 497    /// Whether this chunk of text was originally a tab character.
 498    pub is_tab: bool,
 499    /// An optional recipe for how the chunk should be presented.
 500    pub renderer: Option<ChunkRenderer>,
 501}
 502
 503/// A recipe for how the chunk should be presented.
 504#[derive(Clone)]
 505pub struct ChunkRenderer {
 506    /// creates a custom element to represent this chunk.
 507    pub render: Arc<dyn Send + Sync + Fn(&mut ChunkRendererContext) -> AnyElement>,
 508    /// If true, the element is constrained to the shaped width of the text.
 509    pub constrain_width: bool,
 510}
 511
 512pub struct ChunkRendererContext<'a, 'b> {
 513    pub context: &'a mut WindowContext<'b>,
 514    pub max_width: Pixels,
 515}
 516
 517impl fmt::Debug for ChunkRenderer {
 518    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 519        f.debug_struct("ChunkRenderer")
 520            .field("constrain_width", &self.constrain_width)
 521            .finish()
 522    }
 523}
 524
 525impl<'a, 'b> Deref for ChunkRendererContext<'a, 'b> {
 526    type Target = WindowContext<'b>;
 527
 528    fn deref(&self) -> &Self::Target {
 529        self.context
 530    }
 531}
 532
 533impl<'a, 'b> DerefMut for ChunkRendererContext<'a, 'b> {
 534    fn deref_mut(&mut self) -> &mut Self::Target {
 535        self.context
 536    }
 537}
 538
 539/// A set of edits to a given version of a buffer, computed asynchronously.
 540#[derive(Debug)]
 541pub struct Diff {
 542    pub(crate) base_version: clock::Global,
 543    line_ending: LineEnding,
 544    edits: Vec<(Range<usize>, Arc<str>)>,
 545}
 546
 547#[derive(Clone, Copy)]
 548pub(crate) struct DiagnosticEndpoint {
 549    offset: usize,
 550    is_start: bool,
 551    severity: DiagnosticSeverity,
 552    is_unnecessary: bool,
 553}
 554
 555/// A class of characters, used for characterizing a run of text.
 556#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 557pub enum CharKind {
 558    /// Whitespace.
 559    Whitespace,
 560    /// Punctuation.
 561    Punctuation,
 562    /// Word.
 563    Word,
 564}
 565
 566/// A runnable is a set of data about a region that could be resolved into a task
 567pub struct Runnable {
 568    pub tags: SmallVec<[RunnableTag; 1]>,
 569    pub language: Arc<Language>,
 570    pub buffer: BufferId,
 571}
 572
 573#[derive(Clone, Debug, PartialEq)]
 574pub struct IndentGuide {
 575    pub buffer_id: BufferId,
 576    pub start_row: BufferRow,
 577    pub end_row: BufferRow,
 578    pub depth: u32,
 579    pub tab_size: u32,
 580    pub settings: IndentGuideSettings,
 581}
 582
 583impl IndentGuide {
 584    pub fn indent_level(&self) -> u32 {
 585        self.depth * self.tab_size
 586    }
 587}
 588
 589impl Buffer {
 590    /// Create a new buffer with the given base text.
 591    pub fn local<T: Into<String>>(base_text: T, cx: &mut ModelContext<Self>) -> Self {
 592        Self::build(
 593            TextBuffer::new(0, cx.entity_id().as_non_zero_u64().into(), base_text.into()),
 594            None,
 595            None,
 596            Capability::ReadWrite,
 597        )
 598    }
 599
 600    /// Create a new buffer with the given base text that has proper line endings and other normalization applied.
 601    pub fn local_normalized(
 602        base_text_normalized: Rope,
 603        line_ending: LineEnding,
 604        cx: &mut ModelContext<Self>,
 605    ) -> Self {
 606        Self::build(
 607            TextBuffer::new_normalized(
 608                0,
 609                cx.entity_id().as_non_zero_u64().into(),
 610                line_ending,
 611                base_text_normalized,
 612            ),
 613            None,
 614            None,
 615            Capability::ReadWrite,
 616        )
 617    }
 618
 619    /// Create a new buffer that is a replica of a remote buffer.
 620    pub fn remote(
 621        remote_id: BufferId,
 622        replica_id: ReplicaId,
 623        capability: Capability,
 624        base_text: impl Into<String>,
 625    ) -> Self {
 626        Self::build(
 627            TextBuffer::new(replica_id, remote_id, base_text.into()),
 628            None,
 629            None,
 630            capability,
 631        )
 632    }
 633
 634    /// Create a new buffer that is a replica of a remote buffer, populating its
 635    /// state from the given protobuf message.
 636    pub fn from_proto(
 637        replica_id: ReplicaId,
 638        capability: Capability,
 639        message: proto::BufferState,
 640        file: Option<Arc<dyn File>>,
 641    ) -> Result<Self> {
 642        let buffer_id = BufferId::new(message.id)
 643            .with_context(|| anyhow!("Could not deserialize buffer_id"))?;
 644        let buffer = TextBuffer::new(replica_id, buffer_id, message.base_text);
 645        let mut this = Self::build(buffer, message.diff_base, file, capability);
 646        this.text.set_line_ending(proto::deserialize_line_ending(
 647            rpc::proto::LineEnding::from_i32(message.line_ending)
 648                .ok_or_else(|| anyhow!("missing line_ending"))?,
 649        ));
 650        this.saved_version = proto::deserialize_version(&message.saved_version);
 651        this.saved_mtime = message.saved_mtime.map(|time| time.into());
 652        Ok(this)
 653    }
 654
 655    /// Serialize the buffer's state to a protobuf message.
 656    pub fn to_proto(&self, cx: &AppContext) -> proto::BufferState {
 657        proto::BufferState {
 658            id: self.remote_id().into(),
 659            file: self.file.as_ref().map(|f| f.to_proto(cx)),
 660            base_text: self.base_text().to_string(),
 661            diff_base: self.diff_base().as_ref().map(|h| h.to_string()),
 662            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
 663            saved_version: proto::serialize_version(&self.saved_version),
 664            saved_mtime: self.saved_mtime.map(|time| time.into()),
 665        }
 666    }
 667
 668    /// Serialize as protobufs all of the changes to the buffer since the given version.
 669    pub fn serialize_ops(
 670        &self,
 671        since: Option<clock::Global>,
 672        cx: &AppContext,
 673    ) -> Task<Vec<proto::Operation>> {
 674        let mut operations = Vec::new();
 675        operations.extend(self.deferred_ops.iter().map(proto::serialize_operation));
 676
 677        operations.extend(self.remote_selections.iter().map(|(_, set)| {
 678            proto::serialize_operation(&Operation::UpdateSelections {
 679                selections: set.selections.clone(),
 680                lamport_timestamp: set.lamport_timestamp,
 681                line_mode: set.line_mode,
 682                cursor_shape: set.cursor_shape,
 683            })
 684        }));
 685
 686        for (server_id, diagnostics) in &self.diagnostics {
 687            operations.push(proto::serialize_operation(&Operation::UpdateDiagnostics {
 688                lamport_timestamp: self.diagnostics_timestamp,
 689                server_id: *server_id,
 690                diagnostics: diagnostics.iter().cloned().collect(),
 691            }));
 692        }
 693
 694        operations.push(proto::serialize_operation(
 695            &Operation::UpdateCompletionTriggers {
 696                triggers: self.completion_triggers.clone(),
 697                lamport_timestamp: self.completion_triggers_timestamp,
 698            },
 699        ));
 700
 701        let text_operations = self.text.operations().clone();
 702        cx.background_executor().spawn(async move {
 703            let since = since.unwrap_or_default();
 704            operations.extend(
 705                text_operations
 706                    .iter()
 707                    .filter(|(_, op)| !since.observed(op.timestamp()))
 708                    .map(|(_, op)| proto::serialize_operation(&Operation::Buffer(op.clone()))),
 709            );
 710            operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
 711            operations
 712        })
 713    }
 714
 715    /// Assign a language to the buffer, returning the buffer.
 716    pub fn with_language(mut self, language: Arc<Language>, cx: &mut ModelContext<Self>) -> Self {
 717        self.set_language(Some(language), cx);
 718        self
 719    }
 720
 721    /// Returns the [Capability] of this buffer.
 722    pub fn capability(&self) -> Capability {
 723        self.capability
 724    }
 725
 726    /// Whether this buffer can only be read.
 727    pub fn read_only(&self) -> bool {
 728        self.capability == Capability::ReadOnly
 729    }
 730
 731    /// Builds a [Buffer] with the given underlying [TextBuffer], diff base, [File] and [Capability].
 732    pub fn build(
 733        buffer: TextBuffer,
 734        diff_base: Option<String>,
 735        file: Option<Arc<dyn File>>,
 736        capability: Capability,
 737    ) -> Self {
 738        let saved_mtime = file.as_ref().and_then(|file| file.mtime());
 739        let snapshot = buffer.snapshot();
 740        let git_diff = git::diff::BufferDiff::new(&snapshot);
 741        let syntax_map = Mutex::new(SyntaxMap::new(&snapshot));
 742        Self {
 743            saved_mtime,
 744            saved_version: buffer.version(),
 745            preview_version: buffer.version(),
 746            reload_task: None,
 747            transaction_depth: 0,
 748            was_dirty_before_starting_transaction: None,
 749            has_unsaved_edits: Cell::new((buffer.version(), false)),
 750            text: buffer,
 751            diff_base: diff_base.map(|mut raw_diff_base| {
 752                LineEnding::normalize(&mut raw_diff_base);
 753                BufferDiffBase::Git(Rope::from(raw_diff_base))
 754            }),
 755            diff_base_version: 0,
 756            git_diff,
 757            file,
 758            capability,
 759            syntax_map,
 760            parsing_in_background: false,
 761            non_text_state_update_count: 0,
 762            sync_parse_timeout: Duration::from_millis(1),
 763            parse_status: async_watch::channel(ParseStatus::Idle),
 764            autoindent_requests: Default::default(),
 765            pending_autoindent: Default::default(),
 766            language: None,
 767            remote_selections: Default::default(),
 768            diagnostics: Default::default(),
 769            diagnostics_timestamp: Default::default(),
 770            completion_triggers: Default::default(),
 771            completion_triggers_timestamp: Default::default(),
 772            deferred_ops: OperationQueue::new(),
 773            has_conflict: false,
 774            _subscriptions: Vec::new(),
 775        }
 776    }
 777
 778    /// Retrieve a snapshot of the buffer's current state. This is computationally
 779    /// cheap, and allows reading from the buffer on a background thread.
 780    pub fn snapshot(&self) -> BufferSnapshot {
 781        let text = self.text.snapshot();
 782        let mut syntax_map = self.syntax_map.lock();
 783        syntax_map.interpolate(&text);
 784        let syntax = syntax_map.snapshot();
 785
 786        BufferSnapshot {
 787            text,
 788            syntax,
 789            git_diff: self.git_diff.clone(),
 790            file: self.file.clone(),
 791            remote_selections: self.remote_selections.clone(),
 792            diagnostics: self.diagnostics.clone(),
 793            language: self.language.clone(),
 794            non_text_state_update_count: self.non_text_state_update_count,
 795        }
 796    }
 797
 798    pub fn branch(&mut self, cx: &mut ModelContext<Self>) -> Model<Self> {
 799        let this = cx.handle();
 800        cx.new_model(|cx| {
 801            let mut branch = Self {
 802                diff_base: Some(BufferDiffBase::PastBufferVersion {
 803                    buffer: this.clone(),
 804                    rope: self.as_rope().clone(),
 805                    operations_to_ignore: Vec::new(),
 806                }),
 807                language: self.language.clone(),
 808                has_conflict: self.has_conflict,
 809                has_unsaved_edits: Cell::new(self.has_unsaved_edits.get_mut().clone()),
 810                _subscriptions: vec![cx.subscribe(&this, Self::on_base_buffer_event)],
 811                ..Self::build(
 812                    self.text.branch(),
 813                    None,
 814                    self.file.clone(),
 815                    self.capability(),
 816                )
 817            };
 818            if let Some(language_registry) = self.language_registry() {
 819                branch.set_language_registry(language_registry);
 820            }
 821
 822            branch
 823        })
 824    }
 825
 826    /// Applies all of the changes in this buffer that intersect the given `range`
 827    /// to its base buffer. This buffer must be a branch buffer to call this method.
 828    pub fn merge_into_base(&mut self, range: Option<Range<usize>>, cx: &mut ModelContext<Self>) {
 829        let Some(base_buffer) = self.diff_base_buffer() else {
 830            debug_panic!("not a branch buffer");
 831            return;
 832        };
 833
 834        base_buffer.update(cx, |base_buffer, cx| {
 835            let edits = self
 836                .edits_since::<usize>(&base_buffer.version)
 837                .filter_map(|edit| {
 838                    if range
 839                        .as_ref()
 840                        .map_or(true, |range| range.overlaps(&edit.new))
 841                    {
 842                        Some((edit.old, self.text_for_range(edit.new).collect::<String>()))
 843                    } else {
 844                        None
 845                    }
 846                })
 847                .collect::<Vec<_>>();
 848
 849            let operation = base_buffer.edit(edits, None, cx);
 850
 851            // Prevent this operation from being reapplied to the branch.
 852            if let Some(BufferDiffBase::PastBufferVersion {
 853                operations_to_ignore,
 854                ..
 855            }) = &mut self.diff_base
 856            {
 857                operations_to_ignore.extend(operation);
 858            }
 859
 860            cx.emit(BufferEvent::DiffBaseChanged);
 861        });
 862    }
 863
 864    fn on_base_buffer_event(
 865        &mut self,
 866        _: Model<Buffer>,
 867        event: &BufferEvent,
 868        cx: &mut ModelContext<Self>,
 869    ) {
 870        if let BufferEvent::Operation { operation, .. } = event {
 871            if let Some(BufferDiffBase::PastBufferVersion {
 872                operations_to_ignore,
 873                ..
 874            }) = &mut self.diff_base
 875            {
 876                let mut is_ignored = false;
 877                if let Operation::Buffer(text::Operation::Edit(buffer_operation)) = &operation {
 878                    operations_to_ignore.retain(|operation_to_ignore| {
 879                        match buffer_operation.timestamp.cmp(&operation_to_ignore) {
 880                            Ordering::Less => true,
 881                            Ordering::Equal => {
 882                                is_ignored = true;
 883                                false
 884                            }
 885                            Ordering::Greater => false,
 886                        }
 887                    });
 888                }
 889                if !is_ignored {
 890                    self.apply_ops([operation.clone()], cx);
 891                    self.diff_base_version += 1;
 892                }
 893            }
 894        }
 895    }
 896
 897    #[cfg(test)]
 898    pub(crate) fn as_text_snapshot(&self) -> &text::BufferSnapshot {
 899        &self.text
 900    }
 901
 902    /// Retrieve a snapshot of the buffer's raw text, without any
 903    /// language-related state like the syntax tree or diagnostics.
 904    pub fn text_snapshot(&self) -> text::BufferSnapshot {
 905        self.text.snapshot()
 906    }
 907
 908    /// The file associated with the buffer, if any.
 909    pub fn file(&self) -> Option<&Arc<dyn File>> {
 910        self.file.as_ref()
 911    }
 912
 913    /// The version of the buffer that was last saved or reloaded from disk.
 914    pub fn saved_version(&self) -> &clock::Global {
 915        &self.saved_version
 916    }
 917
 918    /// The mtime of the buffer's file when the buffer was last saved or reloaded from disk.
 919    pub fn saved_mtime(&self) -> Option<SystemTime> {
 920        self.saved_mtime
 921    }
 922
 923    /// Assign a language to the buffer.
 924    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 925        self.non_text_state_update_count += 1;
 926        self.syntax_map.lock().clear(&self.text);
 927        self.language = language;
 928        self.reparse(cx);
 929        cx.emit(BufferEvent::LanguageChanged);
 930    }
 931
 932    /// Assign a language registry to the buffer. This allows the buffer to retrieve
 933    /// other languages if parts of the buffer are written in different languages.
 934    pub fn set_language_registry(&mut self, language_registry: Arc<LanguageRegistry>) {
 935        self.syntax_map
 936            .lock()
 937            .set_language_registry(language_registry);
 938    }
 939
 940    pub fn language_registry(&self) -> Option<Arc<LanguageRegistry>> {
 941        self.syntax_map.lock().language_registry()
 942    }
 943
 944    /// Assign the buffer a new [Capability].
 945    pub fn set_capability(&mut self, capability: Capability, cx: &mut ModelContext<Self>) {
 946        self.capability = capability;
 947        cx.emit(BufferEvent::CapabilityChanged)
 948    }
 949
 950    /// This method is called to signal that the buffer has been saved.
 951    pub fn did_save(
 952        &mut self,
 953        version: clock::Global,
 954        mtime: Option<SystemTime>,
 955        cx: &mut ModelContext<Self>,
 956    ) {
 957        self.saved_version = version;
 958        self.has_unsaved_edits
 959            .set((self.saved_version().clone(), false));
 960        self.has_conflict = false;
 961        self.saved_mtime = mtime;
 962        cx.emit(BufferEvent::Saved);
 963        cx.notify();
 964    }
 965
 966    /// This method is called to signal that the buffer has been discarded.
 967    pub fn discarded(&mut self, cx: &mut ModelContext<Self>) {
 968        cx.emit(BufferEvent::Discarded);
 969        cx.notify();
 970    }
 971
 972    /// Reloads the contents of the buffer from disk.
 973    pub fn reload(
 974        &mut self,
 975        cx: &mut ModelContext<Self>,
 976    ) -> oneshot::Receiver<Option<Transaction>> {
 977        let (tx, rx) = futures::channel::oneshot::channel();
 978        let prev_version = self.text.version();
 979        self.reload_task = Some(cx.spawn(|this, mut cx| async move {
 980            let Some((new_mtime, new_text)) = this.update(&mut cx, |this, cx| {
 981                let file = this.file.as_ref()?.as_local()?;
 982                Some((file.mtime(), file.load(cx)))
 983            })?
 984            else {
 985                return Ok(());
 986            };
 987
 988            let new_text = new_text.await?;
 989            let diff = this
 990                .update(&mut cx, |this, cx| this.diff(new_text.clone(), cx))?
 991                .await;
 992            this.update(&mut cx, |this, cx| {
 993                if this.version() == diff.base_version {
 994                    this.finalize_last_transaction();
 995                    this.apply_diff(diff, cx);
 996                    tx.send(this.finalize_last_transaction().cloned()).ok();
 997                    this.has_conflict = false;
 998                    this.did_reload(this.version(), this.line_ending(), new_mtime, cx);
 999                } else {
1000                    if !diff.edits.is_empty()
1001                        || this
1002                            .edits_since::<usize>(&diff.base_version)
1003                            .next()
1004                            .is_some()
1005                    {
1006                        this.has_conflict = true;
1007                    }
1008
1009                    this.did_reload(prev_version, this.line_ending(), this.saved_mtime, cx);
1010                }
1011
1012                this.reload_task.take();
1013            })
1014        }));
1015        rx
1016    }
1017
1018    /// This method is called to signal that the buffer has been reloaded.
1019    pub fn did_reload(
1020        &mut self,
1021        version: clock::Global,
1022        line_ending: LineEnding,
1023        mtime: Option<SystemTime>,
1024        cx: &mut ModelContext<Self>,
1025    ) {
1026        self.saved_version = version;
1027        self.has_unsaved_edits
1028            .set((self.saved_version.clone(), false));
1029        self.text.set_line_ending(line_ending);
1030        self.saved_mtime = mtime;
1031        cx.emit(BufferEvent::Reloaded);
1032        cx.notify();
1033    }
1034
1035    /// Updates the [File] backing this buffer. This should be called when
1036    /// the file has changed or has been deleted.
1037    pub fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut ModelContext<Self>) {
1038        let mut file_changed = false;
1039
1040        if let Some(old_file) = self.file.as_ref() {
1041            if new_file.path() != old_file.path() {
1042                file_changed = true;
1043            }
1044
1045            if new_file.is_deleted() {
1046                if !old_file.is_deleted() {
1047                    file_changed = true;
1048                    if !self.is_dirty() {
1049                        cx.emit(BufferEvent::DirtyChanged);
1050                    }
1051                }
1052            } else {
1053                let new_mtime = new_file.mtime();
1054                if new_mtime != old_file.mtime() {
1055                    file_changed = true;
1056
1057                    if !self.is_dirty() {
1058                        self.reload(cx).close();
1059                    }
1060                }
1061            }
1062        } else {
1063            file_changed = true;
1064        };
1065
1066        self.file = Some(new_file);
1067        if file_changed {
1068            self.non_text_state_update_count += 1;
1069            cx.emit(BufferEvent::FileHandleChanged);
1070            cx.notify();
1071        }
1072    }
1073
1074    /// Returns the current diff base, see [Buffer::set_diff_base].
1075    pub fn diff_base(&self) -> Option<&Rope> {
1076        match self.diff_base.as_ref()? {
1077            BufferDiffBase::Git(rope) | BufferDiffBase::PastBufferVersion { rope, .. } => {
1078                Some(rope)
1079            }
1080        }
1081    }
1082
1083    /// Sets the text that will be used to compute a Git diff
1084    /// against the buffer text.
1085    pub fn set_diff_base(&mut self, diff_base: Option<String>, cx: &mut ModelContext<Self>) {
1086        self.diff_base = diff_base.map(|mut raw_diff_base| {
1087            LineEnding::normalize(&mut raw_diff_base);
1088            BufferDiffBase::Git(Rope::from(raw_diff_base))
1089        });
1090        self.diff_base_version += 1;
1091        if let Some(recalc_task) = self.recalculate_diff(cx) {
1092            cx.spawn(|buffer, mut cx| async move {
1093                recalc_task.await;
1094                buffer
1095                    .update(&mut cx, |_, cx| {
1096                        cx.emit(BufferEvent::DiffBaseChanged);
1097                    })
1098                    .ok();
1099            })
1100            .detach();
1101        }
1102    }
1103
1104    /// Returns a number, unique per diff base set to the buffer.
1105    pub fn diff_base_version(&self) -> usize {
1106        self.diff_base_version
1107    }
1108
1109    pub fn diff_base_buffer(&self) -> Option<Model<Self>> {
1110        match self.diff_base.as_ref()? {
1111            BufferDiffBase::Git(_) => None,
1112            BufferDiffBase::PastBufferVersion { buffer, .. } => Some(buffer.clone()),
1113        }
1114    }
1115
1116    /// Recomputes the diff.
1117    pub fn recalculate_diff(&mut self, cx: &mut ModelContext<Self>) -> Option<Task<()>> {
1118        let diff_base_rope = match self.diff_base.as_ref()? {
1119            BufferDiffBase::Git(rope) => rope.clone(),
1120            BufferDiffBase::PastBufferVersion { buffer, .. } => buffer.read(cx).as_rope().clone(),
1121        };
1122
1123        let snapshot = self.snapshot();
1124        let mut diff = self.git_diff.clone();
1125        let diff = cx.background_executor().spawn(async move {
1126            diff.update(&diff_base_rope, &snapshot).await;
1127            (diff, diff_base_rope)
1128        });
1129
1130        Some(cx.spawn(|this, mut cx| async move {
1131            let (buffer_diff, diff_base_rope) = diff.await;
1132            this.update(&mut cx, |this, cx| {
1133                this.git_diff = buffer_diff;
1134                this.non_text_state_update_count += 1;
1135                if let Some(BufferDiffBase::PastBufferVersion { rope, .. }) = &mut this.diff_base {
1136                    *rope = diff_base_rope;
1137                    cx.emit(BufferEvent::DiffBaseChanged);
1138                }
1139                cx.emit(BufferEvent::DiffUpdated);
1140            })
1141            .ok();
1142        }))
1143    }
1144
1145    /// Returns the primary [Language] assigned to this [Buffer].
1146    pub fn language(&self) -> Option<&Arc<Language>> {
1147        self.language.as_ref()
1148    }
1149
1150    /// Returns the [Language] at the given location.
1151    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
1152        let offset = position.to_offset(self);
1153        self.syntax_map
1154            .lock()
1155            .layers_for_range(offset..offset, &self.text, false)
1156            .last()
1157            .map(|info| info.language.clone())
1158            .or_else(|| self.language.clone())
1159    }
1160
1161    /// An integer version number that accounts for all updates besides
1162    /// the buffer's text itself (which is versioned via a version vector).
1163    pub fn non_text_state_update_count(&self) -> usize {
1164        self.non_text_state_update_count
1165    }
1166
1167    /// Whether the buffer is being parsed in the background.
1168    #[cfg(any(test, feature = "test-support"))]
1169    pub fn is_parsing(&self) -> bool {
1170        self.parsing_in_background
1171    }
1172
1173    /// Indicates whether the buffer contains any regions that may be
1174    /// written in a language that hasn't been loaded yet.
1175    pub fn contains_unknown_injections(&self) -> bool {
1176        self.syntax_map.lock().contains_unknown_injections()
1177    }
1178
1179    #[cfg(test)]
1180    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
1181        self.sync_parse_timeout = timeout;
1182    }
1183
1184    /// Called after an edit to synchronize the buffer's main parse tree with
1185    /// the buffer's new underlying state.
1186    ///
1187    /// Locks the syntax map and interpolates the edits since the last reparse
1188    /// into the foreground syntax tree.
1189    ///
1190    /// Then takes a stable snapshot of the syntax map before unlocking it.
1191    /// The snapshot with the interpolated edits is sent to a background thread,
1192    /// where we ask Tree-sitter to perform an incremental parse.
1193    ///
1194    /// Meanwhile, in the foreground, we block the main thread for up to 1ms
1195    /// waiting on the parse to complete. As soon as it completes, we proceed
1196    /// synchronously, unless a 1ms timeout elapses.
1197    ///
1198    /// If we time out waiting on the parse, we spawn a second task waiting
1199    /// until the parse does complete and return with the interpolated tree still
1200    /// in the foreground. When the background parse completes, call back into
1201    /// the main thread and assign the foreground parse state.
1202    ///
1203    /// If the buffer or grammar changed since the start of the background parse,
1204    /// initiate an additional reparse recursively. To avoid concurrent parses
1205    /// for the same buffer, we only initiate a new parse if we are not already
1206    /// parsing in the background.
1207    pub fn reparse(&mut self, cx: &mut ModelContext<Self>) {
1208        if self.parsing_in_background {
1209            return;
1210        }
1211        let language = if let Some(language) = self.language.clone() {
1212            language
1213        } else {
1214            return;
1215        };
1216
1217        let text = self.text_snapshot();
1218        let parsed_version = self.version();
1219
1220        let mut syntax_map = self.syntax_map.lock();
1221        syntax_map.interpolate(&text);
1222        let language_registry = syntax_map.language_registry();
1223        let mut syntax_snapshot = syntax_map.snapshot();
1224        drop(syntax_map);
1225
1226        let parse_task = cx.background_executor().spawn({
1227            let language = language.clone();
1228            let language_registry = language_registry.clone();
1229            async move {
1230                syntax_snapshot.reparse(&text, language_registry, language);
1231                syntax_snapshot
1232            }
1233        });
1234
1235        self.parse_status.0.send(ParseStatus::Parsing).unwrap();
1236        match cx
1237            .background_executor()
1238            .block_with_timeout(self.sync_parse_timeout, parse_task)
1239        {
1240            Ok(new_syntax_snapshot) => {
1241                self.did_finish_parsing(new_syntax_snapshot, cx);
1242            }
1243            Err(parse_task) => {
1244                self.parsing_in_background = true;
1245                cx.spawn(move |this, mut cx| async move {
1246                    let new_syntax_map = parse_task.await;
1247                    this.update(&mut cx, move |this, cx| {
1248                        let grammar_changed =
1249                            this.language.as_ref().map_or(true, |current_language| {
1250                                !Arc::ptr_eq(&language, current_language)
1251                            });
1252                        let language_registry_changed = new_syntax_map
1253                            .contains_unknown_injections()
1254                            && language_registry.map_or(false, |registry| {
1255                                registry.version() != new_syntax_map.language_registry_version()
1256                            });
1257                        let parse_again = language_registry_changed
1258                            || grammar_changed
1259                            || this.version.changed_since(&parsed_version);
1260                        this.did_finish_parsing(new_syntax_map, cx);
1261                        this.parsing_in_background = false;
1262                        if parse_again {
1263                            this.reparse(cx);
1264                        }
1265                    })
1266                    .ok();
1267                })
1268                .detach();
1269            }
1270        }
1271    }
1272
1273    fn did_finish_parsing(&mut self, syntax_snapshot: SyntaxSnapshot, cx: &mut ModelContext<Self>) {
1274        self.non_text_state_update_count += 1;
1275        self.syntax_map.lock().did_parse(syntax_snapshot);
1276        self.request_autoindent(cx);
1277        self.parse_status.0.send(ParseStatus::Idle).unwrap();
1278        cx.emit(BufferEvent::Reparsed);
1279        cx.notify();
1280    }
1281
1282    pub fn parse_status(&self) -> watch::Receiver<ParseStatus> {
1283        self.parse_status.1.clone()
1284    }
1285
1286    /// Assign to the buffer a set of diagnostics created by a given language server.
1287    pub fn update_diagnostics(
1288        &mut self,
1289        server_id: LanguageServerId,
1290        diagnostics: DiagnosticSet,
1291        cx: &mut ModelContext<Self>,
1292    ) {
1293        let lamport_timestamp = self.text.lamport_clock.tick();
1294        let op = Operation::UpdateDiagnostics {
1295            server_id,
1296            diagnostics: diagnostics.iter().cloned().collect(),
1297            lamport_timestamp,
1298        };
1299        self.apply_diagnostic_update(server_id, diagnostics, lamport_timestamp, cx);
1300        self.send_operation(op, true, cx);
1301    }
1302
1303    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
1304        if let Some(indent_sizes) = self.compute_autoindents() {
1305            let indent_sizes = cx.background_executor().spawn(indent_sizes);
1306            match cx
1307                .background_executor()
1308                .block_with_timeout(Duration::from_micros(500), indent_sizes)
1309            {
1310                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
1311                Err(indent_sizes) => {
1312                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
1313                        let indent_sizes = indent_sizes.await;
1314                        this.update(&mut cx, |this, cx| {
1315                            this.apply_autoindents(indent_sizes, cx);
1316                        })
1317                        .ok();
1318                    }));
1319                }
1320            }
1321        } else {
1322            self.autoindent_requests.clear();
1323        }
1324    }
1325
1326    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
1327        let max_rows_between_yields = 100;
1328        let snapshot = self.snapshot();
1329        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
1330            return None;
1331        }
1332
1333        let autoindent_requests = self.autoindent_requests.clone();
1334        Some(async move {
1335            let mut indent_sizes = BTreeMap::new();
1336            for request in autoindent_requests {
1337                // Resolve each edited range to its row in the current buffer and in the
1338                // buffer before this batch of edits.
1339                let mut row_ranges = Vec::new();
1340                let mut old_to_new_rows = BTreeMap::new();
1341                let mut language_indent_sizes_by_new_row = Vec::new();
1342                for entry in &request.entries {
1343                    let position = entry.range.start;
1344                    let new_row = position.to_point(&snapshot).row;
1345                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
1346                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
1347
1348                    if !entry.first_line_is_new {
1349                        let old_row = position.to_point(&request.before_edit).row;
1350                        old_to_new_rows.insert(old_row, new_row);
1351                    }
1352                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
1353                }
1354
1355                // Build a map containing the suggested indentation for each of the edited lines
1356                // with respect to the state of the buffer before these edits. This map is keyed
1357                // by the rows for these lines in the current state of the buffer.
1358                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
1359                let old_edited_ranges =
1360                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
1361                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
1362                let mut language_indent_size = IndentSize::default();
1363                for old_edited_range in old_edited_ranges {
1364                    let suggestions = request
1365                        .before_edit
1366                        .suggest_autoindents(old_edited_range.clone())
1367                        .into_iter()
1368                        .flatten();
1369                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
1370                        if let Some(suggestion) = suggestion {
1371                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
1372
1373                            // Find the indent size based on the language for this row.
1374                            while let Some((row, size)) = language_indent_sizes.peek() {
1375                                if *row > new_row {
1376                                    break;
1377                                }
1378                                language_indent_size = *size;
1379                                language_indent_sizes.next();
1380                            }
1381
1382                            let suggested_indent = old_to_new_rows
1383                                .get(&suggestion.basis_row)
1384                                .and_then(|from_row| {
1385                                    Some(old_suggestions.get(from_row).copied()?.0)
1386                                })
1387                                .unwrap_or_else(|| {
1388                                    request
1389                                        .before_edit
1390                                        .indent_size_for_line(suggestion.basis_row)
1391                                })
1392                                .with_delta(suggestion.delta, language_indent_size);
1393                            old_suggestions
1394                                .insert(new_row, (suggested_indent, suggestion.within_error));
1395                        }
1396                    }
1397                    yield_now().await;
1398                }
1399
1400                // In block mode, only compute indentation suggestions for the first line
1401                // of each insertion. Otherwise, compute suggestions for every inserted line.
1402                let new_edited_row_ranges = contiguous_ranges(
1403                    row_ranges.iter().flat_map(|(range, _)| {
1404                        if request.is_block_mode {
1405                            range.start..range.start + 1
1406                        } else {
1407                            range.clone()
1408                        }
1409                    }),
1410                    max_rows_between_yields,
1411                );
1412
1413                // Compute new suggestions for each line, but only include them in the result
1414                // if they differ from the old suggestion for that line.
1415                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
1416                let mut language_indent_size = IndentSize::default();
1417                for new_edited_row_range in new_edited_row_ranges {
1418                    let suggestions = snapshot
1419                        .suggest_autoindents(new_edited_row_range.clone())
1420                        .into_iter()
1421                        .flatten();
1422                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1423                        if let Some(suggestion) = suggestion {
1424                            // Find the indent size based on the language for this row.
1425                            while let Some((row, size)) = language_indent_sizes.peek() {
1426                                if *row > new_row {
1427                                    break;
1428                                }
1429                                language_indent_size = *size;
1430                                language_indent_sizes.next();
1431                            }
1432
1433                            let suggested_indent = indent_sizes
1434                                .get(&suggestion.basis_row)
1435                                .copied()
1436                                .unwrap_or_else(|| {
1437                                    snapshot.indent_size_for_line(suggestion.basis_row)
1438                                })
1439                                .with_delta(suggestion.delta, language_indent_size);
1440                            if old_suggestions.get(&new_row).map_or(
1441                                true,
1442                                |(old_indentation, was_within_error)| {
1443                                    suggested_indent != *old_indentation
1444                                        && (!suggestion.within_error || *was_within_error)
1445                                },
1446                            ) {
1447                                indent_sizes.insert(new_row, suggested_indent);
1448                            }
1449                        }
1450                    }
1451                    yield_now().await;
1452                }
1453
1454                // For each block of inserted text, adjust the indentation of the remaining
1455                // lines of the block by the same amount as the first line was adjusted.
1456                if request.is_block_mode {
1457                    for (row_range, original_indent_column) in
1458                        row_ranges
1459                            .into_iter()
1460                            .filter_map(|(range, original_indent_column)| {
1461                                if range.len() > 1 {
1462                                    Some((range, original_indent_column?))
1463                                } else {
1464                                    None
1465                                }
1466                            })
1467                    {
1468                        let new_indent = indent_sizes
1469                            .get(&row_range.start)
1470                            .copied()
1471                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
1472                        let delta = new_indent.len as i64 - original_indent_column as i64;
1473                        if delta != 0 {
1474                            for row in row_range.skip(1) {
1475                                indent_sizes.entry(row).or_insert_with(|| {
1476                                    let mut size = snapshot.indent_size_for_line(row);
1477                                    if size.kind == new_indent.kind {
1478                                        match delta.cmp(&0) {
1479                                            Ordering::Greater => size.len += delta as u32,
1480                                            Ordering::Less => {
1481                                                size.len = size.len.saturating_sub(-delta as u32)
1482                                            }
1483                                            Ordering::Equal => {}
1484                                        }
1485                                    }
1486                                    size
1487                                });
1488                            }
1489                        }
1490                    }
1491                }
1492            }
1493
1494            indent_sizes
1495        })
1496    }
1497
1498    fn apply_autoindents(
1499        &mut self,
1500        indent_sizes: BTreeMap<u32, IndentSize>,
1501        cx: &mut ModelContext<Self>,
1502    ) {
1503        self.autoindent_requests.clear();
1504
1505        let edits: Vec<_> = indent_sizes
1506            .into_iter()
1507            .filter_map(|(row, indent_size)| {
1508                let current_size = indent_size_for_line(self, row);
1509                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
1510            })
1511            .collect();
1512
1513        let preserve_preview = self.preserve_preview();
1514        self.edit(edits, None, cx);
1515        if preserve_preview {
1516            self.refresh_preview();
1517        }
1518    }
1519
1520    /// Create a minimal edit that will cause the given row to be indented
1521    /// with the given size. After applying this edit, the length of the line
1522    /// will always be at least `new_size.len`.
1523    pub fn edit_for_indent_size_adjustment(
1524        row: u32,
1525        current_size: IndentSize,
1526        new_size: IndentSize,
1527    ) -> Option<(Range<Point>, String)> {
1528        if new_size.kind == current_size.kind {
1529            match new_size.len.cmp(&current_size.len) {
1530                Ordering::Greater => {
1531                    let point = Point::new(row, 0);
1532                    Some((
1533                        point..point,
1534                        iter::repeat(new_size.char())
1535                            .take((new_size.len - current_size.len) as usize)
1536                            .collect::<String>(),
1537                    ))
1538                }
1539
1540                Ordering::Less => Some((
1541                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
1542                    String::new(),
1543                )),
1544
1545                Ordering::Equal => None,
1546            }
1547        } else {
1548            Some((
1549                Point::new(row, 0)..Point::new(row, current_size.len),
1550                iter::repeat(new_size.char())
1551                    .take(new_size.len as usize)
1552                    .collect::<String>(),
1553            ))
1554        }
1555    }
1556
1557    /// Spawns a background task that asynchronously computes a `Diff` between the buffer's text
1558    /// and the given new text.
1559    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
1560        let old_text = self.as_rope().clone();
1561        let base_version = self.version();
1562        cx.background_executor()
1563            .spawn_labeled(*BUFFER_DIFF_TASK, async move {
1564                let old_text = old_text.to_string();
1565                let line_ending = LineEnding::detect(&new_text);
1566                LineEnding::normalize(&mut new_text);
1567
1568                let diff = TextDiff::from_chars(old_text.as_str(), new_text.as_str());
1569                let empty: Arc<str> = Arc::default();
1570
1571                let mut edits = Vec::new();
1572                let mut old_offset = 0;
1573                let mut new_offset = 0;
1574                let mut last_edit: Option<(Range<usize>, Range<usize>)> = None;
1575                for change in diff.iter_all_changes().map(Some).chain([None]) {
1576                    if let Some(change) = &change {
1577                        let len = change.value().len();
1578                        match change.tag() {
1579                            ChangeTag::Equal => {
1580                                old_offset += len;
1581                                new_offset += len;
1582                            }
1583                            ChangeTag::Delete => {
1584                                let old_end_offset = old_offset + len;
1585                                if let Some((last_old_range, _)) = &mut last_edit {
1586                                    last_old_range.end = old_end_offset;
1587                                } else {
1588                                    last_edit =
1589                                        Some((old_offset..old_end_offset, new_offset..new_offset));
1590                                }
1591                                old_offset = old_end_offset;
1592                            }
1593                            ChangeTag::Insert => {
1594                                let new_end_offset = new_offset + len;
1595                                if let Some((_, last_new_range)) = &mut last_edit {
1596                                    last_new_range.end = new_end_offset;
1597                                } else {
1598                                    last_edit =
1599                                        Some((old_offset..old_offset, new_offset..new_end_offset));
1600                                }
1601                                new_offset = new_end_offset;
1602                            }
1603                        }
1604                    }
1605
1606                    if let Some((old_range, new_range)) = &last_edit {
1607                        if old_offset > old_range.end
1608                            || new_offset > new_range.end
1609                            || change.is_none()
1610                        {
1611                            let text = if new_range.is_empty() {
1612                                empty.clone()
1613                            } else {
1614                                new_text[new_range.clone()].into()
1615                            };
1616                            edits.push((old_range.clone(), text));
1617                            last_edit.take();
1618                        }
1619                    }
1620                }
1621
1622                Diff {
1623                    base_version,
1624                    line_ending,
1625                    edits,
1626                }
1627            })
1628    }
1629
1630    /// Spawns a background task that searches the buffer for any whitespace
1631    /// at the ends of a lines, and returns a `Diff` that removes that whitespace.
1632    pub fn remove_trailing_whitespace(&self, cx: &AppContext) -> Task<Diff> {
1633        let old_text = self.as_rope().clone();
1634        let line_ending = self.line_ending();
1635        let base_version = self.version();
1636        cx.background_executor().spawn(async move {
1637            let ranges = trailing_whitespace_ranges(&old_text);
1638            let empty = Arc::<str>::from("");
1639            Diff {
1640                base_version,
1641                line_ending,
1642                edits: ranges
1643                    .into_iter()
1644                    .map(|range| (range, empty.clone()))
1645                    .collect(),
1646            }
1647        })
1648    }
1649
1650    /// Ensures that the buffer ends with a single newline character, and
1651    /// no other whitespace.
1652    pub fn ensure_final_newline(&mut self, cx: &mut ModelContext<Self>) {
1653        let len = self.len();
1654        let mut offset = len;
1655        for chunk in self.as_rope().reversed_chunks_in_range(0..len) {
1656            let non_whitespace_len = chunk
1657                .trim_end_matches(|c: char| c.is_ascii_whitespace())
1658                .len();
1659            offset -= chunk.len();
1660            offset += non_whitespace_len;
1661            if non_whitespace_len != 0 {
1662                if offset == len - 1 && chunk.get(non_whitespace_len..) == Some("\n") {
1663                    return;
1664                }
1665                break;
1666            }
1667        }
1668        self.edit([(offset..len, "\n")], None, cx);
1669    }
1670
1671    /// Applies a diff to the buffer. If the buffer has changed since the given diff was
1672    /// calculated, then adjust the diff to account for those changes, and discard any
1673    /// parts of the diff that conflict with those changes.
1674    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1675        // Check for any edits to the buffer that have occurred since this diff
1676        // was computed.
1677        let snapshot = self.snapshot();
1678        let mut edits_since = snapshot.edits_since::<usize>(&diff.base_version).peekable();
1679        let mut delta = 0;
1680        let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
1681            while let Some(edit_since) = edits_since.peek() {
1682                // If the edit occurs after a diff hunk, then it does not
1683                // affect that hunk.
1684                if edit_since.old.start > range.end {
1685                    break;
1686                }
1687                // If the edit precedes the diff hunk, then adjust the hunk
1688                // to reflect the edit.
1689                else if edit_since.old.end < range.start {
1690                    delta += edit_since.new_len() as i64 - edit_since.old_len() as i64;
1691                    edits_since.next();
1692                }
1693                // If the edit intersects a diff hunk, then discard that hunk.
1694                else {
1695                    return None;
1696                }
1697            }
1698
1699            let start = (range.start as i64 + delta) as usize;
1700            let end = (range.end as i64 + delta) as usize;
1701            Some((start..end, new_text))
1702        });
1703
1704        self.start_transaction();
1705        self.text.set_line_ending(diff.line_ending);
1706        self.edit(adjusted_edits, None, cx);
1707        self.end_transaction(cx)
1708    }
1709
1710    fn has_unsaved_edits(&self) -> bool {
1711        let (last_version, has_unsaved_edits) = self.has_unsaved_edits.take();
1712
1713        if last_version == self.version {
1714            self.has_unsaved_edits
1715                .set((last_version, has_unsaved_edits));
1716            return has_unsaved_edits;
1717        }
1718
1719        let has_edits = self.has_edits_since(&self.saved_version);
1720        self.has_unsaved_edits
1721            .set((self.version.clone(), has_edits));
1722        has_edits
1723    }
1724
1725    /// Checks if the buffer has unsaved changes.
1726    pub fn is_dirty(&self) -> bool {
1727        self.capability != Capability::ReadOnly
1728            && (self.has_conflict
1729                || self.has_unsaved_edits()
1730                || self
1731                    .file
1732                    .as_ref()
1733                    .map_or(false, |file| file.is_deleted() || !file.is_created()))
1734    }
1735
1736    /// Checks if the buffer and its file have both changed since the buffer
1737    /// was last saved or reloaded.
1738    pub fn has_conflict(&self) -> bool {
1739        self.has_conflict
1740            || self.file.as_ref().map_or(false, |file| {
1741                file.mtime() > self.saved_mtime && self.has_unsaved_edits()
1742            })
1743    }
1744
1745    /// Gets a [`Subscription`] that tracks all of the changes to the buffer's text.
1746    pub fn subscribe(&mut self) -> Subscription {
1747        self.text.subscribe()
1748    }
1749
1750    /// Starts a transaction, if one is not already in-progress. When undoing or
1751    /// redoing edits, all of the edits performed within a transaction are undone
1752    /// or redone together.
1753    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1754        self.start_transaction_at(Instant::now())
1755    }
1756
1757    /// Starts a transaction, providing the current time. Subsequent transactions
1758    /// that occur within a short period of time will be grouped together. This
1759    /// is controlled by the buffer's undo grouping duration.
1760    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1761        self.transaction_depth += 1;
1762        if self.was_dirty_before_starting_transaction.is_none() {
1763            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1764        }
1765        self.text.start_transaction_at(now)
1766    }
1767
1768    /// Terminates the current transaction, if this is the outermost transaction.
1769    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1770        self.end_transaction_at(Instant::now(), cx)
1771    }
1772
1773    /// Terminates the current transaction, providing the current time. Subsequent transactions
1774    /// that occur within a short period of time will be grouped together. This
1775    /// is controlled by the buffer's undo grouping duration.
1776    pub fn end_transaction_at(
1777        &mut self,
1778        now: Instant,
1779        cx: &mut ModelContext<Self>,
1780    ) -> Option<TransactionId> {
1781        assert!(self.transaction_depth > 0);
1782        self.transaction_depth -= 1;
1783        let was_dirty = if self.transaction_depth == 0 {
1784            self.was_dirty_before_starting_transaction.take().unwrap()
1785        } else {
1786            false
1787        };
1788        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1789            self.did_edit(&start_version, was_dirty, cx);
1790            Some(transaction_id)
1791        } else {
1792            None
1793        }
1794    }
1795
1796    /// Manually add a transaction to the buffer's undo history.
1797    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1798        self.text.push_transaction(transaction, now);
1799    }
1800
1801    /// Prevent the last transaction from being grouped with any subsequent transactions,
1802    /// even if they occur with the buffer's undo grouping duration.
1803    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1804        self.text.finalize_last_transaction()
1805    }
1806
1807    /// Manually group all changes since a given transaction.
1808    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1809        self.text.group_until_transaction(transaction_id);
1810    }
1811
1812    /// Manually remove a transaction from the buffer's undo history
1813    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1814        self.text.forget_transaction(transaction_id);
1815    }
1816
1817    /// Manually merge two adjacent transactions in the buffer's undo history.
1818    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
1819        self.text.merge_transactions(transaction, destination);
1820    }
1821
1822    /// Waits for the buffer to receive operations with the given timestamps.
1823    pub fn wait_for_edits(
1824        &mut self,
1825        edit_ids: impl IntoIterator<Item = clock::Lamport>,
1826    ) -> impl Future<Output = Result<()>> {
1827        self.text.wait_for_edits(edit_ids)
1828    }
1829
1830    /// Waits for the buffer to receive the operations necessary for resolving the given anchors.
1831    pub fn wait_for_anchors(
1832        &mut self,
1833        anchors: impl IntoIterator<Item = Anchor>,
1834    ) -> impl 'static + Future<Output = Result<()>> {
1835        self.text.wait_for_anchors(anchors)
1836    }
1837
1838    /// Waits for the buffer to receive operations up to the given version.
1839    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = Result<()>> {
1840        self.text.wait_for_version(version)
1841    }
1842
1843    /// Forces all futures returned by [`Buffer::wait_for_version`], [`Buffer::wait_for_edits`], or
1844    /// [`Buffer::wait_for_version`] to resolve with an error.
1845    pub fn give_up_waiting(&mut self) {
1846        self.text.give_up_waiting();
1847    }
1848
1849    /// Stores a set of selections that should be broadcasted to all of the buffer's replicas.
1850    pub fn set_active_selections(
1851        &mut self,
1852        selections: Arc<[Selection<Anchor>]>,
1853        line_mode: bool,
1854        cursor_shape: CursorShape,
1855        cx: &mut ModelContext<Self>,
1856    ) {
1857        let lamport_timestamp = self.text.lamport_clock.tick();
1858        self.remote_selections.insert(
1859            self.text.replica_id(),
1860            SelectionSet {
1861                selections: selections.clone(),
1862                lamport_timestamp,
1863                line_mode,
1864                cursor_shape,
1865            },
1866        );
1867        self.send_operation(
1868            Operation::UpdateSelections {
1869                selections,
1870                line_mode,
1871                lamport_timestamp,
1872                cursor_shape,
1873            },
1874            true,
1875            cx,
1876        );
1877        self.non_text_state_update_count += 1;
1878        cx.notify();
1879    }
1880
1881    /// Clears the selections, so that other replicas of the buffer do not see any selections for
1882    /// this replica.
1883    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1884        if self
1885            .remote_selections
1886            .get(&self.text.replica_id())
1887            .map_or(true, |set| !set.selections.is_empty())
1888        {
1889            self.set_active_selections(Arc::default(), false, Default::default(), cx);
1890        }
1891    }
1892
1893    /// Replaces the buffer's entire text.
1894    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Lamport>
1895    where
1896        T: Into<Arc<str>>,
1897    {
1898        self.autoindent_requests.clear();
1899        self.edit([(0..self.len(), text)], None, cx)
1900    }
1901
1902    /// Applies the given edits to the buffer. Each edit is specified as a range of text to
1903    /// delete, and a string of text to insert at that location.
1904    ///
1905    /// If an [`AutoindentMode`] is provided, then the buffer will enqueue an auto-indent
1906    /// request for the edited ranges, which will be processed when the buffer finishes
1907    /// parsing.
1908    ///
1909    /// Parsing takes place at the end of a transaction, and may compute synchronously
1910    /// or asynchronously, depending on the changes.
1911    pub fn edit<I, S, T>(
1912        &mut self,
1913        edits_iter: I,
1914        autoindent_mode: Option<AutoindentMode>,
1915        cx: &mut ModelContext<Self>,
1916    ) -> Option<clock::Lamport>
1917    where
1918        I: IntoIterator<Item = (Range<S>, T)>,
1919        S: ToOffset,
1920        T: Into<Arc<str>>,
1921    {
1922        // Skip invalid edits and coalesce contiguous ones.
1923        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1924        for (range, new_text) in edits_iter {
1925            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1926            if range.start > range.end {
1927                mem::swap(&mut range.start, &mut range.end);
1928            }
1929            let new_text = new_text.into();
1930            if !new_text.is_empty() || !range.is_empty() {
1931                if let Some((prev_range, prev_text)) = edits.last_mut() {
1932                    if prev_range.end >= range.start {
1933                        prev_range.end = cmp::max(prev_range.end, range.end);
1934                        *prev_text = format!("{prev_text}{new_text}").into();
1935                    } else {
1936                        edits.push((range, new_text));
1937                    }
1938                } else {
1939                    edits.push((range, new_text));
1940                }
1941            }
1942        }
1943        if edits.is_empty() {
1944            return None;
1945        }
1946
1947        self.start_transaction();
1948        self.pending_autoindent.take();
1949        let autoindent_request = autoindent_mode
1950            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1951
1952        let edit_operation = self.text.edit(edits.iter().cloned());
1953        let edit_id = edit_operation.timestamp();
1954
1955        if let Some((before_edit, mode)) = autoindent_request {
1956            let mut delta = 0isize;
1957            let entries = edits
1958                .into_iter()
1959                .enumerate()
1960                .zip(&edit_operation.as_edit().unwrap().new_text)
1961                .map(|((ix, (range, _)), new_text)| {
1962                    let new_text_length = new_text.len();
1963                    let old_start = range.start.to_point(&before_edit);
1964                    let new_start = (delta + range.start as isize) as usize;
1965                    delta += new_text_length as isize - (range.end as isize - range.start as isize);
1966
1967                    let mut range_of_insertion_to_indent = 0..new_text_length;
1968                    let mut first_line_is_new = false;
1969                    let mut original_indent_column = None;
1970
1971                    // When inserting an entire line at the beginning of an existing line,
1972                    // treat the insertion as new.
1973                    if new_text.contains('\n')
1974                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1975                    {
1976                        first_line_is_new = true;
1977                    }
1978
1979                    // When inserting text starting with a newline, avoid auto-indenting the
1980                    // previous line.
1981                    if new_text.starts_with('\n') {
1982                        range_of_insertion_to_indent.start += 1;
1983                        first_line_is_new = true;
1984                    }
1985
1986                    // Avoid auto-indenting after the insertion.
1987                    if let AutoindentMode::Block {
1988                        original_indent_columns,
1989                    } = &mode
1990                    {
1991                        original_indent_column =
1992                            Some(original_indent_columns.get(ix).copied().unwrap_or_else(|| {
1993                                indent_size_for_text(
1994                                    new_text[range_of_insertion_to_indent.clone()].chars(),
1995                                )
1996                                .len
1997                            }));
1998                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1999                            range_of_insertion_to_indent.end -= 1;
2000                        }
2001                    }
2002
2003                    AutoindentRequestEntry {
2004                        first_line_is_new,
2005                        original_indent_column,
2006                        indent_size: before_edit.language_indent_size_at(range.start, cx),
2007                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
2008                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
2009                    }
2010                })
2011                .collect();
2012
2013            self.autoindent_requests.push(Arc::new(AutoindentRequest {
2014                before_edit,
2015                entries,
2016                is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
2017            }));
2018        }
2019
2020        self.end_transaction(cx);
2021        self.send_operation(Operation::Buffer(edit_operation), true, cx);
2022        Some(edit_id)
2023    }
2024
2025    fn did_edit(
2026        &mut self,
2027        old_version: &clock::Global,
2028        was_dirty: bool,
2029        cx: &mut ModelContext<Self>,
2030    ) {
2031        if self.edits_since::<usize>(old_version).next().is_none() {
2032            return;
2033        }
2034
2035        self.reparse(cx);
2036
2037        cx.emit(BufferEvent::Edited);
2038        if was_dirty != self.is_dirty() {
2039            cx.emit(BufferEvent::DirtyChanged);
2040        }
2041        cx.notify();
2042    }
2043
2044    // Inserts newlines at the given position to create an empty line, returning the start of the new line.
2045    // You can also request the insertion of empty lines above and below the line starting at the returned point.
2046    pub fn insert_empty_line(
2047        &mut self,
2048        position: impl ToPoint,
2049        space_above: bool,
2050        space_below: bool,
2051        cx: &mut ModelContext<Self>,
2052    ) -> Point {
2053        let mut position = position.to_point(self);
2054
2055        self.start_transaction();
2056
2057        self.edit(
2058            [(position..position, "\n")],
2059            Some(AutoindentMode::EachLine),
2060            cx,
2061        );
2062
2063        if position.column > 0 {
2064            position += Point::new(1, 0);
2065        }
2066
2067        if !self.is_line_blank(position.row) {
2068            self.edit(
2069                [(position..position, "\n")],
2070                Some(AutoindentMode::EachLine),
2071                cx,
2072            );
2073        }
2074
2075        if space_above && position.row > 0 && !self.is_line_blank(position.row - 1) {
2076            self.edit(
2077                [(position..position, "\n")],
2078                Some(AutoindentMode::EachLine),
2079                cx,
2080            );
2081            position.row += 1;
2082        }
2083
2084        if space_below
2085            && (position.row == self.max_point().row || !self.is_line_blank(position.row + 1))
2086        {
2087            self.edit(
2088                [(position..position, "\n")],
2089                Some(AutoindentMode::EachLine),
2090                cx,
2091            );
2092        }
2093
2094        self.end_transaction(cx);
2095
2096        position
2097    }
2098
2099    /// Applies the given remote operations to the buffer.
2100    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
2101        &mut self,
2102        ops: I,
2103        cx: &mut ModelContext<Self>,
2104    ) {
2105        self.pending_autoindent.take();
2106        let was_dirty = self.is_dirty();
2107        let old_version = self.version.clone();
2108        let mut deferred_ops = Vec::new();
2109        let buffer_ops = ops
2110            .into_iter()
2111            .filter_map(|op| match op {
2112                Operation::Buffer(op) => Some(op),
2113                _ => {
2114                    if self.can_apply_op(&op) {
2115                        self.apply_op(op, cx);
2116                    } else {
2117                        deferred_ops.push(op);
2118                    }
2119                    None
2120                }
2121            })
2122            .collect::<Vec<_>>();
2123        for operation in buffer_ops.iter() {
2124            self.send_operation(Operation::Buffer(operation.clone()), false, cx);
2125        }
2126        self.text.apply_ops(buffer_ops);
2127        self.deferred_ops.insert(deferred_ops);
2128        self.flush_deferred_ops(cx);
2129        self.did_edit(&old_version, was_dirty, cx);
2130        // Notify independently of whether the buffer was edited as the operations could include a
2131        // selection update.
2132        cx.notify();
2133    }
2134
2135    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
2136        let mut deferred_ops = Vec::new();
2137        for op in self.deferred_ops.drain().iter().cloned() {
2138            if self.can_apply_op(&op) {
2139                self.apply_op(op, cx);
2140            } else {
2141                deferred_ops.push(op);
2142            }
2143        }
2144        self.deferred_ops.insert(deferred_ops);
2145    }
2146
2147    pub fn has_deferred_ops(&self) -> bool {
2148        !self.deferred_ops.is_empty() || self.text.has_deferred_ops()
2149    }
2150
2151    fn can_apply_op(&self, operation: &Operation) -> bool {
2152        match operation {
2153            Operation::Buffer(_) => {
2154                unreachable!("buffer operations should never be applied at this layer")
2155            }
2156            Operation::UpdateDiagnostics {
2157                diagnostics: diagnostic_set,
2158                ..
2159            } => diagnostic_set.iter().all(|diagnostic| {
2160                self.text.can_resolve(&diagnostic.range.start)
2161                    && self.text.can_resolve(&diagnostic.range.end)
2162            }),
2163            Operation::UpdateSelections { selections, .. } => selections
2164                .iter()
2165                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
2166            Operation::UpdateCompletionTriggers { .. } => true,
2167        }
2168    }
2169
2170    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
2171        match operation {
2172            Operation::Buffer(_) => {
2173                unreachable!("buffer operations should never be applied at this layer")
2174            }
2175            Operation::UpdateDiagnostics {
2176                server_id,
2177                diagnostics: diagnostic_set,
2178                lamport_timestamp,
2179            } => {
2180                let snapshot = self.snapshot();
2181                self.apply_diagnostic_update(
2182                    server_id,
2183                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
2184                    lamport_timestamp,
2185                    cx,
2186                );
2187            }
2188            Operation::UpdateSelections {
2189                selections,
2190                lamport_timestamp,
2191                line_mode,
2192                cursor_shape,
2193            } => {
2194                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
2195                    if set.lamport_timestamp > lamport_timestamp {
2196                        return;
2197                    }
2198                }
2199
2200                self.remote_selections.insert(
2201                    lamport_timestamp.replica_id,
2202                    SelectionSet {
2203                        selections,
2204                        lamport_timestamp,
2205                        line_mode,
2206                        cursor_shape,
2207                    },
2208                );
2209                self.text.lamport_clock.observe(lamport_timestamp);
2210                self.non_text_state_update_count += 1;
2211            }
2212            Operation::UpdateCompletionTriggers {
2213                triggers,
2214                lamport_timestamp,
2215            } => {
2216                self.completion_triggers = triggers;
2217                self.text.lamport_clock.observe(lamport_timestamp);
2218            }
2219        }
2220    }
2221
2222    fn apply_diagnostic_update(
2223        &mut self,
2224        server_id: LanguageServerId,
2225        diagnostics: DiagnosticSet,
2226        lamport_timestamp: clock::Lamport,
2227        cx: &mut ModelContext<Self>,
2228    ) {
2229        if lamport_timestamp > self.diagnostics_timestamp {
2230            let ix = self.diagnostics.binary_search_by_key(&server_id, |e| e.0);
2231            if diagnostics.is_empty() {
2232                if let Ok(ix) = ix {
2233                    self.diagnostics.remove(ix);
2234                }
2235            } else {
2236                match ix {
2237                    Err(ix) => self.diagnostics.insert(ix, (server_id, diagnostics)),
2238                    Ok(ix) => self.diagnostics[ix].1 = diagnostics,
2239                };
2240            }
2241            self.diagnostics_timestamp = lamport_timestamp;
2242            self.non_text_state_update_count += 1;
2243            self.text.lamport_clock.observe(lamport_timestamp);
2244            cx.notify();
2245            cx.emit(BufferEvent::DiagnosticsUpdated);
2246        }
2247    }
2248
2249    fn send_operation(
2250        &mut self,
2251        operation: Operation,
2252        is_local: bool,
2253        cx: &mut ModelContext<Self>,
2254    ) {
2255        cx.emit(BufferEvent::Operation {
2256            operation,
2257            is_local,
2258        });
2259    }
2260
2261    /// Removes the selections for a given peer.
2262    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
2263        self.remote_selections.remove(&replica_id);
2264        cx.notify();
2265    }
2266
2267    /// Undoes the most recent transaction.
2268    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
2269        let was_dirty = self.is_dirty();
2270        let old_version = self.version.clone();
2271
2272        if let Some((transaction_id, operation)) = self.text.undo() {
2273            self.send_operation(Operation::Buffer(operation), true, cx);
2274            self.did_edit(&old_version, was_dirty, cx);
2275            Some(transaction_id)
2276        } else {
2277            None
2278        }
2279    }
2280
2281    /// Manually undoes a specific transaction in the buffer's undo history.
2282    pub fn undo_transaction(
2283        &mut self,
2284        transaction_id: TransactionId,
2285        cx: &mut ModelContext<Self>,
2286    ) -> bool {
2287        let was_dirty = self.is_dirty();
2288        let old_version = self.version.clone();
2289        if let Some(operation) = self.text.undo_transaction(transaction_id) {
2290            self.send_operation(Operation::Buffer(operation), true, cx);
2291            self.did_edit(&old_version, was_dirty, cx);
2292            true
2293        } else {
2294            false
2295        }
2296    }
2297
2298    /// Manually undoes all changes after a given transaction in the buffer's undo history.
2299    pub fn undo_to_transaction(
2300        &mut self,
2301        transaction_id: TransactionId,
2302        cx: &mut ModelContext<Self>,
2303    ) -> bool {
2304        let was_dirty = self.is_dirty();
2305        let old_version = self.version.clone();
2306
2307        let operations = self.text.undo_to_transaction(transaction_id);
2308        let undone = !operations.is_empty();
2309        for operation in operations {
2310            self.send_operation(Operation::Buffer(operation), true, cx);
2311        }
2312        if undone {
2313            self.did_edit(&old_version, was_dirty, cx)
2314        }
2315        undone
2316    }
2317
2318    /// Manually redoes a specific transaction in the buffer's redo history.
2319    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
2320        let was_dirty = self.is_dirty();
2321        let old_version = self.version.clone();
2322
2323        if let Some((transaction_id, operation)) = self.text.redo() {
2324            self.send_operation(Operation::Buffer(operation), true, cx);
2325            self.did_edit(&old_version, was_dirty, cx);
2326            Some(transaction_id)
2327        } else {
2328            None
2329        }
2330    }
2331
2332    /// Manually undoes all changes until a given transaction in the buffer's redo history.
2333    pub fn redo_to_transaction(
2334        &mut self,
2335        transaction_id: TransactionId,
2336        cx: &mut ModelContext<Self>,
2337    ) -> bool {
2338        let was_dirty = self.is_dirty();
2339        let old_version = self.version.clone();
2340
2341        let operations = self.text.redo_to_transaction(transaction_id);
2342        let redone = !operations.is_empty();
2343        for operation in operations {
2344            self.send_operation(Operation::Buffer(operation), true, cx);
2345        }
2346        if redone {
2347            self.did_edit(&old_version, was_dirty, cx)
2348        }
2349        redone
2350    }
2351
2352    /// Override current completion triggers with the user-provided completion triggers.
2353    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
2354        self.completion_triggers.clone_from(&triggers);
2355        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
2356        self.send_operation(
2357            Operation::UpdateCompletionTriggers {
2358                triggers,
2359                lamport_timestamp: self.completion_triggers_timestamp,
2360            },
2361            true,
2362            cx,
2363        );
2364        cx.notify();
2365    }
2366
2367    /// Returns a list of strings which trigger a completion menu for this language.
2368    /// Usually this is driven by LSP server which returns a list of trigger characters for completions.
2369    pub fn completion_triggers(&self) -> &[String] {
2370        &self.completion_triggers
2371    }
2372
2373    /// Call this directly after performing edits to prevent the preview tab
2374    /// from being dismissed by those edits. It causes `should_dismiss_preview`
2375    /// to return false until there are additional edits.
2376    pub fn refresh_preview(&mut self) {
2377        self.preview_version = self.version.clone();
2378    }
2379
2380    /// Whether we should preserve the preview status of a tab containing this buffer.
2381    pub fn preserve_preview(&self) -> bool {
2382        !self.has_edits_since(&self.preview_version)
2383    }
2384}
2385
2386#[doc(hidden)]
2387#[cfg(any(test, feature = "test-support"))]
2388impl Buffer {
2389    pub fn edit_via_marked_text(
2390        &mut self,
2391        marked_string: &str,
2392        autoindent_mode: Option<AutoindentMode>,
2393        cx: &mut ModelContext<Self>,
2394    ) {
2395        let edits = self.edits_for_marked_text(marked_string);
2396        self.edit(edits, autoindent_mode, cx);
2397    }
2398
2399    pub fn set_group_interval(&mut self, group_interval: Duration) {
2400        self.text.set_group_interval(group_interval);
2401    }
2402
2403    pub fn randomly_edit<T>(
2404        &mut self,
2405        rng: &mut T,
2406        old_range_count: usize,
2407        cx: &mut ModelContext<Self>,
2408    ) where
2409        T: rand::Rng,
2410    {
2411        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
2412        let mut last_end = None;
2413        for _ in 0..old_range_count {
2414            if last_end.map_or(false, |last_end| last_end >= self.len()) {
2415                break;
2416            }
2417
2418            let new_start = last_end.map_or(0, |last_end| last_end + 1);
2419            let mut range = self.random_byte_range(new_start, rng);
2420            if rng.gen_bool(0.2) {
2421                mem::swap(&mut range.start, &mut range.end);
2422            }
2423            last_end = Some(range.end);
2424
2425            let new_text_len = rng.gen_range(0..10);
2426            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
2427
2428            edits.push((range, new_text));
2429        }
2430        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
2431        self.edit(edits, None, cx);
2432    }
2433
2434    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
2435        let was_dirty = self.is_dirty();
2436        let old_version = self.version.clone();
2437
2438        let ops = self.text.randomly_undo_redo(rng);
2439        if !ops.is_empty() {
2440            for op in ops {
2441                self.send_operation(Operation::Buffer(op), true, cx);
2442                self.did_edit(&old_version, was_dirty, cx);
2443            }
2444        }
2445    }
2446}
2447
2448impl EventEmitter<BufferEvent> for Buffer {}
2449
2450impl Deref for Buffer {
2451    type Target = TextBuffer;
2452
2453    fn deref(&self) -> &Self::Target {
2454        &self.text
2455    }
2456}
2457
2458impl BufferSnapshot {
2459    /// Returns [`IndentSize`] for a given line that respects user settings and /// language preferences.
2460    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
2461        indent_size_for_line(self, row)
2462    }
2463    /// Returns [`IndentSize`] for a given position that respects user settings
2464    /// and language preferences.
2465    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
2466        let settings = language_settings(self.language_at(position), self.file(), cx);
2467        if settings.hard_tabs {
2468            IndentSize::tab()
2469        } else {
2470            IndentSize::spaces(settings.tab_size.get())
2471        }
2472    }
2473
2474    /// Retrieve the suggested indent size for all of the given rows. The unit of indentation
2475    /// is passed in as `single_indent_size`.
2476    pub fn suggested_indents(
2477        &self,
2478        rows: impl Iterator<Item = u32>,
2479        single_indent_size: IndentSize,
2480    ) -> BTreeMap<u32, IndentSize> {
2481        let mut result = BTreeMap::new();
2482
2483        for row_range in contiguous_ranges(rows, 10) {
2484            let suggestions = match self.suggest_autoindents(row_range.clone()) {
2485                Some(suggestions) => suggestions,
2486                _ => break,
2487            };
2488
2489            for (row, suggestion) in row_range.zip(suggestions) {
2490                let indent_size = if let Some(suggestion) = suggestion {
2491                    result
2492                        .get(&suggestion.basis_row)
2493                        .copied()
2494                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
2495                        .with_delta(suggestion.delta, single_indent_size)
2496                } else {
2497                    self.indent_size_for_line(row)
2498                };
2499
2500                result.insert(row, indent_size);
2501            }
2502        }
2503
2504        result
2505    }
2506
2507    fn suggest_autoindents(
2508        &self,
2509        row_range: Range<u32>,
2510    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
2511        let config = &self.language.as_ref()?.config;
2512        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2513
2514        // Find the suggested indentation ranges based on the syntax tree.
2515        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
2516        let end = Point::new(row_range.end, 0);
2517        let range = (start..end).to_offset(&self.text);
2518        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2519            Some(&grammar.indents_config.as_ref()?.query)
2520        });
2521        let indent_configs = matches
2522            .grammars()
2523            .iter()
2524            .map(|grammar| grammar.indents_config.as_ref().unwrap())
2525            .collect::<Vec<_>>();
2526
2527        let mut indent_ranges = Vec::<Range<Point>>::new();
2528        let mut outdent_positions = Vec::<Point>::new();
2529        while let Some(mat) = matches.peek() {
2530            let mut start: Option<Point> = None;
2531            let mut end: Option<Point> = None;
2532
2533            let config = &indent_configs[mat.grammar_index];
2534            for capture in mat.captures {
2535                if capture.index == config.indent_capture_ix {
2536                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
2537                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
2538                } else if Some(capture.index) == config.start_capture_ix {
2539                    start = Some(Point::from_ts_point(capture.node.end_position()));
2540                } else if Some(capture.index) == config.end_capture_ix {
2541                    end = Some(Point::from_ts_point(capture.node.start_position()));
2542                } else if Some(capture.index) == config.outdent_capture_ix {
2543                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
2544                }
2545            }
2546
2547            matches.advance();
2548            if let Some((start, end)) = start.zip(end) {
2549                if start.row == end.row {
2550                    continue;
2551                }
2552
2553                let range = start..end;
2554                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
2555                    Err(ix) => indent_ranges.insert(ix, range),
2556                    Ok(ix) => {
2557                        let prev_range = &mut indent_ranges[ix];
2558                        prev_range.end = prev_range.end.max(range.end);
2559                    }
2560                }
2561            }
2562        }
2563
2564        let mut error_ranges = Vec::<Range<Point>>::new();
2565        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2566            Some(&grammar.error_query)
2567        });
2568        while let Some(mat) = matches.peek() {
2569            let node = mat.captures[0].node;
2570            let start = Point::from_ts_point(node.start_position());
2571            let end = Point::from_ts_point(node.end_position());
2572            let range = start..end;
2573            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
2574                Ok(ix) | Err(ix) => ix,
2575            };
2576            let mut end_ix = ix;
2577            while let Some(existing_range) = error_ranges.get(end_ix) {
2578                if existing_range.end < end {
2579                    end_ix += 1;
2580                } else {
2581                    break;
2582                }
2583            }
2584            error_ranges.splice(ix..end_ix, [range]);
2585            matches.advance();
2586        }
2587
2588        outdent_positions.sort();
2589        for outdent_position in outdent_positions {
2590            // find the innermost indent range containing this outdent_position
2591            // set its end to the outdent position
2592            if let Some(range_to_truncate) = indent_ranges
2593                .iter_mut()
2594                .filter(|indent_range| indent_range.contains(&outdent_position))
2595                .last()
2596            {
2597                range_to_truncate.end = outdent_position;
2598            }
2599        }
2600
2601        // Find the suggested indentation increases and decreased based on regexes.
2602        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
2603        self.for_each_line(
2604            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
2605                ..Point::new(row_range.end, 0),
2606            |row, line| {
2607                if config
2608                    .decrease_indent_pattern
2609                    .as_ref()
2610                    .map_or(false, |regex| regex.is_match(line))
2611                {
2612                    indent_change_rows.push((row, Ordering::Less));
2613                }
2614                if config
2615                    .increase_indent_pattern
2616                    .as_ref()
2617                    .map_or(false, |regex| regex.is_match(line))
2618                {
2619                    indent_change_rows.push((row + 1, Ordering::Greater));
2620                }
2621            },
2622        );
2623
2624        let mut indent_changes = indent_change_rows.into_iter().peekable();
2625        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
2626            prev_non_blank_row.unwrap_or(0)
2627        } else {
2628            row_range.start.saturating_sub(1)
2629        };
2630        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
2631        Some(row_range.map(move |row| {
2632            let row_start = Point::new(row, self.indent_size_for_line(row).len);
2633
2634            let mut indent_from_prev_row = false;
2635            let mut outdent_from_prev_row = false;
2636            let mut outdent_to_row = u32::MAX;
2637
2638            while let Some((indent_row, delta)) = indent_changes.peek() {
2639                match indent_row.cmp(&row) {
2640                    Ordering::Equal => match delta {
2641                        Ordering::Less => outdent_from_prev_row = true,
2642                        Ordering::Greater => indent_from_prev_row = true,
2643                        _ => {}
2644                    },
2645
2646                    Ordering::Greater => break,
2647                    Ordering::Less => {}
2648                }
2649
2650                indent_changes.next();
2651            }
2652
2653            for range in &indent_ranges {
2654                if range.start.row >= row {
2655                    break;
2656                }
2657                if range.start.row == prev_row && range.end > row_start {
2658                    indent_from_prev_row = true;
2659                }
2660                if range.end > prev_row_start && range.end <= row_start {
2661                    outdent_to_row = outdent_to_row.min(range.start.row);
2662                }
2663            }
2664
2665            let within_error = error_ranges
2666                .iter()
2667                .any(|e| e.start.row < row && e.end > row_start);
2668
2669            let suggestion = if outdent_to_row == prev_row
2670                || (outdent_from_prev_row && indent_from_prev_row)
2671            {
2672                Some(IndentSuggestion {
2673                    basis_row: prev_row,
2674                    delta: Ordering::Equal,
2675                    within_error,
2676                })
2677            } else if indent_from_prev_row {
2678                Some(IndentSuggestion {
2679                    basis_row: prev_row,
2680                    delta: Ordering::Greater,
2681                    within_error,
2682                })
2683            } else if outdent_to_row < prev_row {
2684                Some(IndentSuggestion {
2685                    basis_row: outdent_to_row,
2686                    delta: Ordering::Equal,
2687                    within_error,
2688                })
2689            } else if outdent_from_prev_row {
2690                Some(IndentSuggestion {
2691                    basis_row: prev_row,
2692                    delta: Ordering::Less,
2693                    within_error,
2694                })
2695            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
2696            {
2697                Some(IndentSuggestion {
2698                    basis_row: prev_row,
2699                    delta: Ordering::Equal,
2700                    within_error,
2701                })
2702            } else {
2703                None
2704            };
2705
2706            prev_row = row;
2707            prev_row_start = row_start;
2708            suggestion
2709        }))
2710    }
2711
2712    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2713        while row > 0 {
2714            row -= 1;
2715            if !self.is_line_blank(row) {
2716                return Some(row);
2717            }
2718        }
2719        None
2720    }
2721
2722    fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures, Vec<HighlightMap>) {
2723        let captures = self.syntax.captures(range, &self.text, |grammar| {
2724            grammar.highlights_query.as_ref()
2725        });
2726        let highlight_maps = captures
2727            .grammars()
2728            .iter()
2729            .map(|grammar| grammar.highlight_map())
2730            .collect();
2731        (captures, highlight_maps)
2732    }
2733    /// Iterates over chunks of text in the given range of the buffer. Text is chunked
2734    /// in an arbitrary way due to being stored in a [`Rope`](text::Rope). The text is also
2735    /// returned in chunks where each chunk has a single syntax highlighting style and
2736    /// diagnostic status.
2737    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
2738        let range = range.start.to_offset(self)..range.end.to_offset(self);
2739
2740        let mut syntax = None;
2741        if language_aware {
2742            syntax = Some(self.get_highlights(range.clone()));
2743        }
2744        // We want to look at diagnostic spans only when iterating over language-annotated chunks.
2745        let diagnostics = language_aware;
2746        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostics, Some(self))
2747    }
2748
2749    /// Invokes the given callback for each line of text in the given range of the buffer.
2750    /// Uses callback to avoid allocating a string for each line.
2751    fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2752        let mut line = String::new();
2753        let mut row = range.start.row;
2754        for chunk in self
2755            .as_rope()
2756            .chunks_in_range(range.to_offset(self))
2757            .chain(["\n"])
2758        {
2759            for (newline_ix, text) in chunk.split('\n').enumerate() {
2760                if newline_ix > 0 {
2761                    callback(row, &line);
2762                    row += 1;
2763                    line.clear();
2764                }
2765                line.push_str(text);
2766            }
2767        }
2768    }
2769
2770    /// Iterates over every [`SyntaxLayer`] in the buffer.
2771    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayer> + '_ {
2772        self.syntax
2773            .layers_for_range(0..self.len(), &self.text, true)
2774    }
2775
2776    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayer> {
2777        let offset = position.to_offset(self);
2778        self.syntax
2779            .layers_for_range(offset..offset, &self.text, false)
2780            .filter(|l| l.node().end_byte() > offset)
2781            .last()
2782    }
2783
2784    /// Returns the main [Language]
2785    pub fn language(&self) -> Option<&Arc<Language>> {
2786        self.language.as_ref()
2787    }
2788
2789    /// Returns the [Language] at the given location.
2790    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2791        self.syntax_layer_at(position)
2792            .map(|info| info.language)
2793            .or(self.language.as_ref())
2794    }
2795
2796    /// Returns the settings for the language at the given location.
2797    pub fn settings_at<'a, D: ToOffset>(
2798        &self,
2799        position: D,
2800        cx: &'a AppContext,
2801    ) -> &'a LanguageSettings {
2802        language_settings(self.language_at(position), self.file.as_ref(), cx)
2803    }
2804
2805    pub fn char_classifier_at<T: ToOffset>(&self, point: T) -> CharClassifier {
2806        CharClassifier::new(self.language_scope_at(point))
2807    }
2808
2809    /// Returns the [LanguageScope] at the given location.
2810    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2811        let offset = position.to_offset(self);
2812        let mut scope = None;
2813        let mut smallest_range: Option<Range<usize>> = None;
2814
2815        // Use the layer that has the smallest node intersecting the given point.
2816        for layer in self
2817            .syntax
2818            .layers_for_range(offset..offset, &self.text, false)
2819        {
2820            let mut cursor = layer.node().walk();
2821
2822            let mut range = None;
2823            loop {
2824                let child_range = cursor.node().byte_range();
2825                if !child_range.to_inclusive().contains(&offset) {
2826                    break;
2827                }
2828
2829                range = Some(child_range);
2830                if cursor.goto_first_child_for_byte(offset).is_none() {
2831                    break;
2832                }
2833            }
2834
2835            if let Some(range) = range {
2836                if smallest_range
2837                    .as_ref()
2838                    .map_or(true, |smallest_range| range.len() < smallest_range.len())
2839                {
2840                    smallest_range = Some(range);
2841                    scope = Some(LanguageScope {
2842                        language: layer.language.clone(),
2843                        override_id: layer.override_id(offset, &self.text),
2844                    });
2845                }
2846            }
2847        }
2848
2849        scope.or_else(|| {
2850            self.language.clone().map(|language| LanguageScope {
2851                language,
2852                override_id: None,
2853            })
2854        })
2855    }
2856
2857    /// Returns a tuple of the range and character kind of the word
2858    /// surrounding the given position.
2859    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2860        let mut start = start.to_offset(self);
2861        let mut end = start;
2862        let mut next_chars = self.chars_at(start).peekable();
2863        let mut prev_chars = self.reversed_chars_at(start).peekable();
2864
2865        let classifier = self.char_classifier_at(start);
2866        let word_kind = cmp::max(
2867            prev_chars.peek().copied().map(|c| classifier.kind(c)),
2868            next_chars.peek().copied().map(|c| classifier.kind(c)),
2869        );
2870
2871        for ch in prev_chars {
2872            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
2873                start -= ch.len_utf8();
2874            } else {
2875                break;
2876            }
2877        }
2878
2879        for ch in next_chars {
2880            if Some(classifier.kind(ch)) == word_kind && ch != '\n' {
2881                end += ch.len_utf8();
2882            } else {
2883                break;
2884            }
2885        }
2886
2887        (start..end, word_kind)
2888    }
2889
2890    /// Returns the range for the closes syntax node enclosing the given range.
2891    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2892        let range = range.start.to_offset(self)..range.end.to_offset(self);
2893        let mut result: Option<Range<usize>> = None;
2894        'outer: for layer in self
2895            .syntax
2896            .layers_for_range(range.clone(), &self.text, true)
2897        {
2898            let mut cursor = layer.node().walk();
2899
2900            // Descend to the first leaf that touches the start of the range,
2901            // and if the range is non-empty, extends beyond the start.
2902            while cursor.goto_first_child_for_byte(range.start).is_some() {
2903                if !range.is_empty() && cursor.node().end_byte() == range.start {
2904                    cursor.goto_next_sibling();
2905                }
2906            }
2907
2908            // Ascend to the smallest ancestor that strictly contains the range.
2909            loop {
2910                let node_range = cursor.node().byte_range();
2911                if node_range.start <= range.start
2912                    && node_range.end >= range.end
2913                    && node_range.len() > range.len()
2914                {
2915                    break;
2916                }
2917                if !cursor.goto_parent() {
2918                    continue 'outer;
2919                }
2920            }
2921
2922            let left_node = cursor.node();
2923            let mut layer_result = left_node.byte_range();
2924
2925            // For an empty range, try to find another node immediately to the right of the range.
2926            if left_node.end_byte() == range.start {
2927                let mut right_node = None;
2928                while !cursor.goto_next_sibling() {
2929                    if !cursor.goto_parent() {
2930                        break;
2931                    }
2932                }
2933
2934                while cursor.node().start_byte() == range.start {
2935                    right_node = Some(cursor.node());
2936                    if !cursor.goto_first_child() {
2937                        break;
2938                    }
2939                }
2940
2941                // If there is a candidate node on both sides of the (empty) range, then
2942                // decide between the two by favoring a named node over an anonymous token.
2943                // If both nodes are the same in that regard, favor the right one.
2944                if let Some(right_node) = right_node {
2945                    if right_node.is_named() || !left_node.is_named() {
2946                        layer_result = right_node.byte_range();
2947                    }
2948                }
2949            }
2950
2951            if let Some(previous_result) = &result {
2952                if previous_result.len() < layer_result.len() {
2953                    continue;
2954                }
2955            }
2956            result = Some(layer_result);
2957        }
2958
2959        result
2960    }
2961
2962    /// Returns the outline for the buffer.
2963    ///
2964    /// This method allows passing an optional [SyntaxTheme] to
2965    /// syntax-highlight the returned symbols.
2966    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2967        self.outline_items_containing(0..self.len(), true, theme)
2968            .map(Outline::new)
2969    }
2970
2971    /// Returns all the symbols that contain the given position.
2972    ///
2973    /// This method allows passing an optional [SyntaxTheme] to
2974    /// syntax-highlight the returned symbols.
2975    pub fn symbols_containing<T: ToOffset>(
2976        &self,
2977        position: T,
2978        theme: Option<&SyntaxTheme>,
2979    ) -> Option<Vec<OutlineItem<Anchor>>> {
2980        let position = position.to_offset(self);
2981        let mut items = self.outline_items_containing(
2982            position.saturating_sub(1)..self.len().min(position + 1),
2983            false,
2984            theme,
2985        )?;
2986        let mut prev_depth = None;
2987        items.retain(|item| {
2988            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2989            prev_depth = Some(item.depth);
2990            result
2991        });
2992        Some(items)
2993    }
2994
2995    pub fn outline_items_containing<T: ToOffset>(
2996        &self,
2997        range: Range<T>,
2998        include_extra_context: bool,
2999        theme: Option<&SyntaxTheme>,
3000    ) -> Option<Vec<OutlineItem<Anchor>>> {
3001        let range = range.to_offset(self);
3002        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
3003            grammar.outline_config.as_ref().map(|c| &c.query)
3004        });
3005        let configs = matches
3006            .grammars()
3007            .iter()
3008            .map(|g| g.outline_config.as_ref().unwrap())
3009            .collect::<Vec<_>>();
3010
3011        let mut items = Vec::new();
3012        let mut annotation_row_ranges: Vec<Range<u32>> = Vec::new();
3013        while let Some(mat) = matches.peek() {
3014            let config = &configs[mat.grammar_index];
3015            if let Some(item) =
3016                self.next_outline_item(config, &mat, &range, include_extra_context, theme)
3017            {
3018                items.push(item);
3019            } else if let Some(capture) = mat
3020                .captures
3021                .iter()
3022                .find(|capture| Some(capture.index) == config.annotation_capture_ix)
3023            {
3024                let capture_range = capture.node.start_position()..capture.node.end_position();
3025                let mut capture_row_range =
3026                    capture_range.start.row as u32..capture_range.end.row as u32;
3027                if capture_range.end.row > capture_range.start.row && capture_range.end.column == 0
3028                {
3029                    capture_row_range.end -= 1;
3030                }
3031                if let Some(last_row_range) = annotation_row_ranges.last_mut() {
3032                    if last_row_range.end >= capture_row_range.start.saturating_sub(1) {
3033                        last_row_range.end = capture_row_range.end;
3034                    } else {
3035                        annotation_row_ranges.push(capture_row_range);
3036                    }
3037                } else {
3038                    annotation_row_ranges.push(capture_row_range);
3039                }
3040            }
3041            matches.advance();
3042        }
3043
3044        items.sort_by_key(|item| (item.range.start, Reverse(item.range.end)));
3045
3046        // Assign depths based on containment relationships and convert to anchors.
3047        let mut item_ends_stack = Vec::<Point>::new();
3048        let mut anchor_items = Vec::new();
3049        let mut annotation_row_ranges = annotation_row_ranges.into_iter().peekable();
3050        for item in items {
3051            while let Some(last_end) = item_ends_stack.last().copied() {
3052                if last_end < item.range.end {
3053                    item_ends_stack.pop();
3054                } else {
3055                    break;
3056                }
3057            }
3058
3059            let mut annotation_row_range = None;
3060            while let Some(next_annotation_row_range) = annotation_row_ranges.peek() {
3061                let row_preceding_item = item.range.start.row.saturating_sub(1);
3062                if next_annotation_row_range.end < row_preceding_item {
3063                    annotation_row_ranges.next();
3064                } else {
3065                    if next_annotation_row_range.end == row_preceding_item {
3066                        annotation_row_range = Some(next_annotation_row_range.clone());
3067                        annotation_row_ranges.next();
3068                    }
3069                    break;
3070                }
3071            }
3072
3073            anchor_items.push(OutlineItem {
3074                depth: item_ends_stack.len(),
3075                range: self.anchor_after(item.range.start)..self.anchor_before(item.range.end),
3076                text: item.text,
3077                highlight_ranges: item.highlight_ranges,
3078                name_ranges: item.name_ranges,
3079                body_range: item.body_range.map(|body_range| {
3080                    self.anchor_after(body_range.start)..self.anchor_before(body_range.end)
3081                }),
3082                annotation_range: annotation_row_range.map(|annotation_range| {
3083                    self.anchor_after(Point::new(annotation_range.start, 0))
3084                        ..self.anchor_before(Point::new(
3085                            annotation_range.end,
3086                            self.line_len(annotation_range.end),
3087                        ))
3088                }),
3089            });
3090            item_ends_stack.push(item.range.end);
3091        }
3092
3093        Some(anchor_items)
3094    }
3095
3096    fn next_outline_item(
3097        &self,
3098        config: &OutlineConfig,
3099        mat: &SyntaxMapMatch,
3100        range: &Range<usize>,
3101        include_extra_context: bool,
3102        theme: Option<&SyntaxTheme>,
3103    ) -> Option<OutlineItem<Point>> {
3104        let item_node = mat.captures.iter().find_map(|cap| {
3105            if cap.index == config.item_capture_ix {
3106                Some(cap.node)
3107            } else {
3108                None
3109            }
3110        })?;
3111
3112        let item_byte_range = item_node.byte_range();
3113        if item_byte_range.end < range.start || item_byte_range.start > range.end {
3114            return None;
3115        }
3116        let item_point_range = Point::from_ts_point(item_node.start_position())
3117            ..Point::from_ts_point(item_node.end_position());
3118
3119        let mut open_point = None;
3120        let mut close_point = None;
3121        let mut buffer_ranges = Vec::new();
3122        for capture in mat.captures {
3123            let node_is_name;
3124            if capture.index == config.name_capture_ix {
3125                node_is_name = true;
3126            } else if Some(capture.index) == config.context_capture_ix
3127                || (Some(capture.index) == config.extra_context_capture_ix && include_extra_context)
3128            {
3129                node_is_name = false;
3130            } else {
3131                if Some(capture.index) == config.open_capture_ix {
3132                    open_point = Some(Point::from_ts_point(capture.node.end_position()));
3133                } else if Some(capture.index) == config.close_capture_ix {
3134                    close_point = Some(Point::from_ts_point(capture.node.start_position()));
3135                }
3136
3137                continue;
3138            }
3139
3140            let mut range = capture.node.start_byte()..capture.node.end_byte();
3141            let start = capture.node.start_position();
3142            if capture.node.end_position().row > start.row {
3143                range.end = range.start + self.line_len(start.row as u32) as usize - start.column;
3144            }
3145
3146            if !range.is_empty() {
3147                buffer_ranges.push((range, node_is_name));
3148            }
3149        }
3150        if buffer_ranges.is_empty() {
3151            return None;
3152        }
3153        let mut text = String::new();
3154        let mut highlight_ranges = Vec::new();
3155        let mut name_ranges = Vec::new();
3156        let mut chunks = self.chunks(
3157            buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end,
3158            true,
3159        );
3160        let mut last_buffer_range_end = 0;
3161        for (buffer_range, is_name) in buffer_ranges {
3162            if !text.is_empty() && buffer_range.start > last_buffer_range_end {
3163                text.push(' ');
3164            }
3165            last_buffer_range_end = buffer_range.end;
3166            if is_name {
3167                let mut start = text.len();
3168                let end = start + buffer_range.len();
3169
3170                // When multiple names are captured, then the matchable text
3171                // includes the whitespace in between the names.
3172                if !name_ranges.is_empty() {
3173                    start -= 1;
3174                }
3175
3176                name_ranges.push(start..end);
3177            }
3178
3179            let mut offset = buffer_range.start;
3180            chunks.seek(buffer_range.clone());
3181            for mut chunk in chunks.by_ref() {
3182                if chunk.text.len() > buffer_range.end - offset {
3183                    chunk.text = &chunk.text[0..(buffer_range.end - offset)];
3184                    offset = buffer_range.end;
3185                } else {
3186                    offset += chunk.text.len();
3187                }
3188                let style = chunk
3189                    .syntax_highlight_id
3190                    .zip(theme)
3191                    .and_then(|(highlight, theme)| highlight.style(theme));
3192                if let Some(style) = style {
3193                    let start = text.len();
3194                    let end = start + chunk.text.len();
3195                    highlight_ranges.push((start..end, style));
3196                }
3197                text.push_str(chunk.text);
3198                if offset >= buffer_range.end {
3199                    break;
3200                }
3201            }
3202        }
3203
3204        Some(OutlineItem {
3205            depth: 0, // We'll calculate the depth later
3206            range: item_point_range,
3207            text,
3208            highlight_ranges,
3209            name_ranges,
3210            body_range: open_point.zip(close_point).map(|(start, end)| start..end),
3211            annotation_range: None,
3212        })
3213    }
3214
3215    /// For each grammar in the language, runs the provided
3216    /// [tree_sitter::Query] against the given range.
3217    pub fn matches(
3218        &self,
3219        range: Range<usize>,
3220        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
3221    ) -> SyntaxMapMatches {
3222        self.syntax.matches(range, self, query)
3223    }
3224
3225    /// Returns bracket range pairs overlapping or adjacent to `range`
3226    pub fn bracket_ranges<T: ToOffset>(
3227        &self,
3228        range: Range<T>,
3229    ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + '_ {
3230        // Find bracket pairs that *inclusively* contain the given range.
3231        let range = range.start.to_offset(self).saturating_sub(1)
3232            ..self.len().min(range.end.to_offset(self) + 1);
3233
3234        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
3235            grammar.brackets_config.as_ref().map(|c| &c.query)
3236        });
3237        let configs = matches
3238            .grammars()
3239            .iter()
3240            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
3241            .collect::<Vec<_>>();
3242
3243        iter::from_fn(move || {
3244            while let Some(mat) = matches.peek() {
3245                let mut open = None;
3246                let mut close = None;
3247                let config = &configs[mat.grammar_index];
3248                for capture in mat.captures {
3249                    if capture.index == config.open_capture_ix {
3250                        open = Some(capture.node.byte_range());
3251                    } else if capture.index == config.close_capture_ix {
3252                        close = Some(capture.node.byte_range());
3253                    }
3254                }
3255
3256                matches.advance();
3257
3258                let Some((open, close)) = open.zip(close) else {
3259                    continue;
3260                };
3261
3262                let bracket_range = open.start..=close.end;
3263                if !bracket_range.overlaps(&range) {
3264                    continue;
3265                }
3266
3267                return Some((open, close));
3268            }
3269            None
3270        })
3271    }
3272
3273    /// Returns enclosing bracket ranges containing the given range
3274    pub fn enclosing_bracket_ranges<T: ToOffset>(
3275        &self,
3276        range: Range<T>,
3277    ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + '_ {
3278        let range = range.start.to_offset(self)..range.end.to_offset(self);
3279
3280        self.bracket_ranges(range.clone())
3281            .filter(move |(open, close)| open.start <= range.start && close.end >= range.end)
3282    }
3283
3284    /// Returns the smallest enclosing bracket ranges containing the given range or None if no brackets contain range
3285    ///
3286    /// Can optionally pass a range_filter to filter the ranges of brackets to consider
3287    pub fn innermost_enclosing_bracket_ranges<T: ToOffset>(
3288        &self,
3289        range: Range<T>,
3290        range_filter: Option<&dyn Fn(Range<usize>, Range<usize>) -> bool>,
3291    ) -> Option<(Range<usize>, Range<usize>)> {
3292        let range = range.start.to_offset(self)..range.end.to_offset(self);
3293
3294        // Get the ranges of the innermost pair of brackets.
3295        let mut result: Option<(Range<usize>, Range<usize>)> = None;
3296
3297        for (open, close) in self.enclosing_bracket_ranges(range.clone()) {
3298            if let Some(range_filter) = range_filter {
3299                if !range_filter(open.clone(), close.clone()) {
3300                    continue;
3301                }
3302            }
3303
3304            let len = close.end - open.start;
3305
3306            if let Some((existing_open, existing_close)) = &result {
3307                let existing_len = existing_close.end - existing_open.start;
3308                if len > existing_len {
3309                    continue;
3310                }
3311            }
3312
3313            result = Some((open, close));
3314        }
3315
3316        result
3317    }
3318
3319    /// Returns anchor ranges for any matches of the redaction query.
3320    /// The buffer can be associated with multiple languages, and the redaction query associated with each
3321    /// will be run on the relevant section of the buffer.
3322    pub fn redacted_ranges<T: ToOffset>(
3323        &self,
3324        range: Range<T>,
3325    ) -> impl Iterator<Item = Range<usize>> + '_ {
3326        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
3327        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
3328            grammar
3329                .redactions_config
3330                .as_ref()
3331                .map(|config| &config.query)
3332        });
3333
3334        let configs = syntax_matches
3335            .grammars()
3336            .iter()
3337            .map(|grammar| grammar.redactions_config.as_ref())
3338            .collect::<Vec<_>>();
3339
3340        iter::from_fn(move || {
3341            let redacted_range = syntax_matches
3342                .peek()
3343                .and_then(|mat| {
3344                    configs[mat.grammar_index].and_then(|config| {
3345                        mat.captures
3346                            .iter()
3347                            .find(|capture| capture.index == config.redaction_capture_ix)
3348                    })
3349                })
3350                .map(|mat| mat.node.byte_range());
3351            syntax_matches.advance();
3352            redacted_range
3353        })
3354    }
3355
3356    pub fn injections_intersecting_range<T: ToOffset>(
3357        &self,
3358        range: Range<T>,
3359    ) -> impl Iterator<Item = (Range<usize>, &Arc<Language>)> + '_ {
3360        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
3361
3362        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
3363            grammar
3364                .injection_config
3365                .as_ref()
3366                .map(|config| &config.query)
3367        });
3368
3369        let configs = syntax_matches
3370            .grammars()
3371            .iter()
3372            .map(|grammar| grammar.injection_config.as_ref())
3373            .collect::<Vec<_>>();
3374
3375        iter::from_fn(move || {
3376            let ranges = syntax_matches.peek().and_then(|mat| {
3377                let config = &configs[mat.grammar_index]?;
3378                let content_capture_range = mat.captures.iter().find_map(|capture| {
3379                    if capture.index == config.content_capture_ix {
3380                        Some(capture.node.byte_range())
3381                    } else {
3382                        None
3383                    }
3384                })?;
3385                let language = self.language_at(content_capture_range.start)?;
3386                Some((content_capture_range, language))
3387            });
3388            syntax_matches.advance();
3389            ranges
3390        })
3391    }
3392
3393    pub fn runnable_ranges(
3394        &self,
3395        range: Range<Anchor>,
3396    ) -> impl Iterator<Item = RunnableRange> + '_ {
3397        let offset_range = range.start.to_offset(self)..range.end.to_offset(self);
3398
3399        let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
3400            grammar.runnable_config.as_ref().map(|config| &config.query)
3401        });
3402
3403        let test_configs = syntax_matches
3404            .grammars()
3405            .iter()
3406            .map(|grammar| grammar.runnable_config.as_ref())
3407            .collect::<Vec<_>>();
3408
3409        iter::from_fn(move || loop {
3410            let mat = syntax_matches.peek()?;
3411
3412            let test_range = test_configs[mat.grammar_index].and_then(|test_configs| {
3413                let mut run_range = None;
3414                let full_range = mat.captures.iter().fold(
3415                    Range {
3416                        start: usize::MAX,
3417                        end: 0,
3418                    },
3419                    |mut acc, next| {
3420                        let byte_range = next.node.byte_range();
3421                        if acc.start > byte_range.start {
3422                            acc.start = byte_range.start;
3423                        }
3424                        if acc.end < byte_range.end {
3425                            acc.end = byte_range.end;
3426                        }
3427                        acc
3428                    },
3429                );
3430                if full_range.start > full_range.end {
3431                    // We did not find a full spanning range of this match.
3432                    return None;
3433                }
3434                let extra_captures: SmallVec<[_; 1]> =
3435                    SmallVec::from_iter(mat.captures.iter().filter_map(|capture| {
3436                        test_configs
3437                            .extra_captures
3438                            .get(capture.index as usize)
3439                            .cloned()
3440                            .and_then(|tag_name| match tag_name {
3441                                RunnableCapture::Named(name) => {
3442                                    Some((capture.node.byte_range(), name))
3443                                }
3444                                RunnableCapture::Run => {
3445                                    let _ = run_range.insert(capture.node.byte_range());
3446                                    None
3447                                }
3448                            })
3449                    }));
3450                let run_range = run_range?;
3451                let tags = test_configs
3452                    .query
3453                    .property_settings(mat.pattern_index)
3454                    .iter()
3455                    .filter_map(|property| {
3456                        if *property.key == *"tag" {
3457                            property
3458                                .value
3459                                .as_ref()
3460                                .map(|value| RunnableTag(value.to_string().into()))
3461                        } else {
3462                            None
3463                        }
3464                    })
3465                    .collect();
3466                let extra_captures = extra_captures
3467                    .into_iter()
3468                    .map(|(range, name)| {
3469                        (
3470                            name.to_string(),
3471                            self.text_for_range(range.clone()).collect::<String>(),
3472                        )
3473                    })
3474                    .collect();
3475                // All tags should have the same range.
3476                Some(RunnableRange {
3477                    run_range,
3478                    full_range,
3479                    runnable: Runnable {
3480                        tags,
3481                        language: mat.language,
3482                        buffer: self.remote_id(),
3483                    },
3484                    extra_captures,
3485                    buffer_id: self.remote_id(),
3486                })
3487            });
3488
3489            syntax_matches.advance();
3490            if test_range.is_some() {
3491                // It's fine for us to short-circuit on .peek()? returning None. We don't want to return None from this iter if we
3492                // had a capture that did not contain a run marker, hence we'll just loop around for the next capture.
3493                return test_range;
3494            }
3495        })
3496    }
3497
3498    pub fn indent_guides_in_range(
3499        &self,
3500        range: Range<Anchor>,
3501        ignore_disabled_for_language: bool,
3502        cx: &AppContext,
3503    ) -> Vec<IndentGuide> {
3504        let language_settings = language_settings(self.language(), self.file.as_ref(), cx);
3505        let settings = language_settings.indent_guides;
3506        if !ignore_disabled_for_language && !settings.enabled {
3507            return Vec::new();
3508        }
3509        let tab_size = language_settings.tab_size.get() as u32;
3510
3511        let start_row = range.start.to_point(self).row;
3512        let end_row = range.end.to_point(self).row;
3513        let row_range = start_row..end_row + 1;
3514
3515        let mut row_indents = self.line_indents_in_row_range(row_range.clone());
3516
3517        let mut result_vec = Vec::new();
3518        let mut indent_stack = SmallVec::<[IndentGuide; 8]>::new();
3519
3520        while let Some((first_row, mut line_indent)) = row_indents.next() {
3521            let current_depth = indent_stack.len() as u32;
3522
3523            // When encountering empty, continue until found useful line indent
3524            // then add to the indent stack with the depth found
3525            let mut found_indent = false;
3526            let mut last_row = first_row;
3527            if line_indent.is_line_empty() {
3528                let mut trailing_row = end_row;
3529                while !found_indent {
3530                    let (target_row, new_line_indent) =
3531                        if let Some(display_row) = row_indents.next() {
3532                            display_row
3533                        } else {
3534                            // This means we reached the end of the given range and found empty lines at the end.
3535                            // We need to traverse further until we find a non-empty line to know if we need to add
3536                            // an indent guide for the last visible indent.
3537                            trailing_row += 1;
3538
3539                            const TRAILING_ROW_SEARCH_LIMIT: u32 = 25;
3540                            if trailing_row > self.max_point().row
3541                                || trailing_row > end_row + TRAILING_ROW_SEARCH_LIMIT
3542                            {
3543                                break;
3544                            }
3545                            let new_line_indent = self.line_indent_for_row(trailing_row);
3546                            (trailing_row, new_line_indent)
3547                        };
3548
3549                    if new_line_indent.is_line_empty() {
3550                        continue;
3551                    }
3552                    last_row = target_row.min(end_row);
3553                    line_indent = new_line_indent;
3554                    found_indent = true;
3555                    break;
3556                }
3557            } else {
3558                found_indent = true
3559            }
3560
3561            let depth = if found_indent {
3562                line_indent.len(tab_size) / tab_size
3563                    + ((line_indent.len(tab_size) % tab_size) > 0) as u32
3564            } else {
3565                current_depth
3566            };
3567
3568            match depth.cmp(&current_depth) {
3569                Ordering::Less => {
3570                    for _ in 0..(current_depth - depth) {
3571                        let mut indent = indent_stack.pop().unwrap();
3572                        if last_row != first_row {
3573                            // In this case, we landed on an empty row, had to seek forward,
3574                            // and discovered that the indent we where on is ending.
3575                            // This means that the last display row must
3576                            // be on line that ends this indent range, so we
3577                            // should display the range up to the first non-empty line
3578                            indent.end_row = first_row.saturating_sub(1);
3579                        }
3580
3581                        result_vec.push(indent)
3582                    }
3583                }
3584                Ordering::Greater => {
3585                    for next_depth in current_depth..depth {
3586                        indent_stack.push(IndentGuide {
3587                            buffer_id: self.remote_id(),
3588                            start_row: first_row,
3589                            end_row: last_row,
3590                            depth: next_depth,
3591                            tab_size,
3592                            settings,
3593                        });
3594                    }
3595                }
3596                _ => {}
3597            }
3598
3599            for indent in indent_stack.iter_mut() {
3600                indent.end_row = last_row;
3601            }
3602        }
3603
3604        result_vec.extend(indent_stack);
3605
3606        result_vec
3607    }
3608
3609    pub async fn enclosing_indent(
3610        &self,
3611        mut buffer_row: BufferRow,
3612    ) -> Option<(Range<BufferRow>, LineIndent)> {
3613        let max_row = self.max_point().row;
3614        if buffer_row >= max_row {
3615            return None;
3616        }
3617
3618        let mut target_indent = self.line_indent_for_row(buffer_row);
3619
3620        // If the current row is at the start of an indented block, we want to return this
3621        // block as the enclosing indent.
3622        if !target_indent.is_line_empty() && buffer_row < max_row {
3623            let next_line_indent = self.line_indent_for_row(buffer_row + 1);
3624            if !next_line_indent.is_line_empty()
3625                && target_indent.raw_len() < next_line_indent.raw_len()
3626            {
3627                target_indent = next_line_indent;
3628                buffer_row += 1;
3629            }
3630        }
3631
3632        const SEARCH_ROW_LIMIT: u32 = 25000;
3633        const SEARCH_WHITESPACE_ROW_LIMIT: u32 = 2500;
3634        const YIELD_INTERVAL: u32 = 100;
3635
3636        let mut accessed_row_counter = 0;
3637
3638        // If there is a blank line at the current row, search for the next non indented lines
3639        if target_indent.is_line_empty() {
3640            let start = buffer_row.saturating_sub(SEARCH_WHITESPACE_ROW_LIMIT);
3641            let end = (max_row + 1).min(buffer_row + SEARCH_WHITESPACE_ROW_LIMIT);
3642
3643            let mut non_empty_line_above = None;
3644            for (row, indent) in self
3645                .text
3646                .reversed_line_indents_in_row_range(start..buffer_row)
3647            {
3648                accessed_row_counter += 1;
3649                if accessed_row_counter == YIELD_INTERVAL {
3650                    accessed_row_counter = 0;
3651                    yield_now().await;
3652                }
3653                if !indent.is_line_empty() {
3654                    non_empty_line_above = Some((row, indent));
3655                    break;
3656                }
3657            }
3658
3659            let mut non_empty_line_below = None;
3660            for (row, indent) in self.text.line_indents_in_row_range((buffer_row + 1)..end) {
3661                accessed_row_counter += 1;
3662                if accessed_row_counter == YIELD_INTERVAL {
3663                    accessed_row_counter = 0;
3664                    yield_now().await;
3665                }
3666                if !indent.is_line_empty() {
3667                    non_empty_line_below = Some((row, indent));
3668                    break;
3669                }
3670            }
3671
3672            let (row, indent) = match (non_empty_line_above, non_empty_line_below) {
3673                (Some((above_row, above_indent)), Some((below_row, below_indent))) => {
3674                    if above_indent.raw_len() >= below_indent.raw_len() {
3675                        (above_row, above_indent)
3676                    } else {
3677                        (below_row, below_indent)
3678                    }
3679                }
3680                (Some(above), None) => above,
3681                (None, Some(below)) => below,
3682                _ => return None,
3683            };
3684
3685            target_indent = indent;
3686            buffer_row = row;
3687        }
3688
3689        let start = buffer_row.saturating_sub(SEARCH_ROW_LIMIT);
3690        let end = (max_row + 1).min(buffer_row + SEARCH_ROW_LIMIT);
3691
3692        let mut start_indent = None;
3693        for (row, indent) in self
3694            .text
3695            .reversed_line_indents_in_row_range(start..buffer_row)
3696        {
3697            accessed_row_counter += 1;
3698            if accessed_row_counter == YIELD_INTERVAL {
3699                accessed_row_counter = 0;
3700                yield_now().await;
3701            }
3702            if !indent.is_line_empty() && indent.raw_len() < target_indent.raw_len() {
3703                start_indent = Some((row, indent));
3704                break;
3705            }
3706        }
3707        let (start_row, start_indent_size) = start_indent?;
3708
3709        let mut end_indent = (end, None);
3710        for (row, indent) in self.text.line_indents_in_row_range((buffer_row + 1)..end) {
3711            accessed_row_counter += 1;
3712            if accessed_row_counter == YIELD_INTERVAL {
3713                accessed_row_counter = 0;
3714                yield_now().await;
3715            }
3716            if !indent.is_line_empty() && indent.raw_len() < target_indent.raw_len() {
3717                end_indent = (row.saturating_sub(1), Some(indent));
3718                break;
3719            }
3720        }
3721        let (end_row, end_indent_size) = end_indent;
3722
3723        let indent = if let Some(end_indent_size) = end_indent_size {
3724            if start_indent_size.raw_len() > end_indent_size.raw_len() {
3725                start_indent_size
3726            } else {
3727                end_indent_size
3728            }
3729        } else {
3730            start_indent_size
3731        };
3732
3733        Some((start_row..end_row, indent))
3734    }
3735
3736    /// Returns selections for remote peers intersecting the given range.
3737    #[allow(clippy::type_complexity)]
3738    pub fn selections_in_range(
3739        &self,
3740        range: Range<Anchor>,
3741        include_local: bool,
3742    ) -> impl Iterator<
3743        Item = (
3744            ReplicaId,
3745            bool,
3746            CursorShape,
3747            impl Iterator<Item = &Selection<Anchor>> + '_,
3748        ),
3749    > + '_ {
3750        self.remote_selections
3751            .iter()
3752            .filter(move |(replica_id, set)| {
3753                (include_local || **replica_id != self.text.replica_id())
3754                    && !set.selections.is_empty()
3755            })
3756            .map(move |(replica_id, set)| {
3757                let start_ix = match set.selections.binary_search_by(|probe| {
3758                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
3759                }) {
3760                    Ok(ix) | Err(ix) => ix,
3761                };
3762                let end_ix = match set.selections.binary_search_by(|probe| {
3763                    probe.start.cmp(&range.end, self).then(Ordering::Less)
3764                }) {
3765                    Ok(ix) | Err(ix) => ix,
3766                };
3767
3768                (
3769                    *replica_id,
3770                    set.line_mode,
3771                    set.cursor_shape,
3772                    set.selections[start_ix..end_ix].iter(),
3773                )
3774            })
3775    }
3776
3777    /// Whether the buffer contains any git changes.
3778    pub fn has_git_diff(&self) -> bool {
3779        !self.git_diff.is_empty()
3780    }
3781
3782    /// Returns all the Git diff hunks intersecting the given row range.
3783    #[cfg(any(test, feature = "test-support"))]
3784    pub fn git_diff_hunks_in_row_range(
3785        &self,
3786        range: Range<BufferRow>,
3787    ) -> impl '_ + Iterator<Item = git::diff::DiffHunk> {
3788        self.git_diff.hunks_in_row_range(range, self)
3789    }
3790
3791    /// Returns all the Git diff hunks intersecting the given
3792    /// range.
3793    pub fn git_diff_hunks_intersecting_range(
3794        &self,
3795        range: Range<Anchor>,
3796    ) -> impl '_ + Iterator<Item = git::diff::DiffHunk> {
3797        self.git_diff.hunks_intersecting_range(range, self)
3798    }
3799
3800    /// Returns all the Git diff hunks intersecting the given
3801    /// range, in reverse order.
3802    pub fn git_diff_hunks_intersecting_range_rev(
3803        &self,
3804        range: Range<Anchor>,
3805    ) -> impl '_ + Iterator<Item = git::diff::DiffHunk> {
3806        self.git_diff.hunks_intersecting_range_rev(range, self)
3807    }
3808
3809    /// Returns if the buffer contains any diagnostics.
3810    pub fn has_diagnostics(&self) -> bool {
3811        !self.diagnostics.is_empty()
3812    }
3813
3814    /// Returns all the diagnostics intersecting the given range.
3815    pub fn diagnostics_in_range<'a, T, O>(
3816        &'a self,
3817        search_range: Range<T>,
3818        reversed: bool,
3819    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
3820    where
3821        T: 'a + Clone + ToOffset,
3822        O: 'a + FromAnchor + Ord,
3823    {
3824        let mut iterators: Vec<_> = self
3825            .diagnostics
3826            .iter()
3827            .map(|(_, collection)| {
3828                collection
3829                    .range::<T, O>(search_range.clone(), self, true, reversed)
3830                    .peekable()
3831            })
3832            .collect();
3833
3834        std::iter::from_fn(move || {
3835            let (next_ix, _) = iterators
3836                .iter_mut()
3837                .enumerate()
3838                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
3839                .min_by(|(_, a), (_, b)| {
3840                    let cmp = a
3841                        .range
3842                        .start
3843                        .cmp(&b.range.start)
3844                        // when range is equal, sort by diagnostic severity
3845                        .then(a.diagnostic.severity.cmp(&b.diagnostic.severity))
3846                        // and stabilize order with group_id
3847                        .then(a.diagnostic.group_id.cmp(&b.diagnostic.group_id));
3848                    if reversed {
3849                        cmp.reverse()
3850                    } else {
3851                        cmp
3852                    }
3853                })?;
3854            iterators[next_ix].next()
3855        })
3856    }
3857
3858    /// Returns all the diagnostic groups associated with the given
3859    /// language server id. If no language server id is provided,
3860    /// all diagnostics groups are returned.
3861    pub fn diagnostic_groups(
3862        &self,
3863        language_server_id: Option<LanguageServerId>,
3864    ) -> Vec<(LanguageServerId, DiagnosticGroup<Anchor>)> {
3865        let mut groups = Vec::new();
3866
3867        if let Some(language_server_id) = language_server_id {
3868            if let Ok(ix) = self
3869                .diagnostics
3870                .binary_search_by_key(&language_server_id, |e| e.0)
3871            {
3872                self.diagnostics[ix]
3873                    .1
3874                    .groups(language_server_id, &mut groups, self);
3875            }
3876        } else {
3877            for (language_server_id, diagnostics) in self.diagnostics.iter() {
3878                diagnostics.groups(*language_server_id, &mut groups, self);
3879            }
3880        }
3881
3882        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
3883            let a_start = &group_a.entries[group_a.primary_ix].range.start;
3884            let b_start = &group_b.entries[group_b.primary_ix].range.start;
3885            a_start.cmp(b_start, self).then_with(|| id_a.cmp(id_b))
3886        });
3887
3888        groups
3889    }
3890
3891    /// Returns an iterator over the diagnostics for the given group.
3892    pub fn diagnostic_group<'a, O>(
3893        &'a self,
3894        group_id: usize,
3895    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
3896    where
3897        O: 'a + FromAnchor,
3898    {
3899        self.diagnostics
3900            .iter()
3901            .flat_map(move |(_, set)| set.group(group_id, self))
3902    }
3903
3904    /// An integer version number that accounts for all updates besides
3905    /// the buffer's text itself (which is versioned via a version vector).
3906    pub fn non_text_state_update_count(&self) -> usize {
3907        self.non_text_state_update_count
3908    }
3909
3910    /// Returns a snapshot of underlying file.
3911    pub fn file(&self) -> Option<&Arc<dyn File>> {
3912        self.file.as_ref()
3913    }
3914
3915    /// Resolves the file path (relative to the worktree root) associated with the underlying file.
3916    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
3917        if let Some(file) = self.file() {
3918            if file.path().file_name().is_none() || include_root {
3919                Some(file.full_path(cx))
3920            } else {
3921                Some(file.path().to_path_buf())
3922            }
3923        } else {
3924            None
3925        }
3926    }
3927}
3928
3929fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
3930    indent_size_for_text(text.chars_at(Point::new(row, 0)))
3931}
3932
3933fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
3934    let mut result = IndentSize::spaces(0);
3935    for c in text {
3936        let kind = match c {
3937            ' ' => IndentKind::Space,
3938            '\t' => IndentKind::Tab,
3939            _ => break,
3940        };
3941        if result.len == 0 {
3942            result.kind = kind;
3943        }
3944        result.len += 1;
3945    }
3946    result
3947}
3948
3949impl Clone for BufferSnapshot {
3950    fn clone(&self) -> Self {
3951        Self {
3952            text: self.text.clone(),
3953            git_diff: self.git_diff.clone(),
3954            syntax: self.syntax.clone(),
3955            file: self.file.clone(),
3956            remote_selections: self.remote_selections.clone(),
3957            diagnostics: self.diagnostics.clone(),
3958            language: self.language.clone(),
3959            non_text_state_update_count: self.non_text_state_update_count,
3960        }
3961    }
3962}
3963
3964impl Deref for BufferSnapshot {
3965    type Target = text::BufferSnapshot;
3966
3967    fn deref(&self) -> &Self::Target {
3968        &self.text
3969    }
3970}
3971
3972unsafe impl<'a> Send for BufferChunks<'a> {}
3973
3974impl<'a> BufferChunks<'a> {
3975    pub(crate) fn new(
3976        text: &'a Rope,
3977        range: Range<usize>,
3978        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
3979        diagnostics: bool,
3980        buffer_snapshot: Option<&'a BufferSnapshot>,
3981    ) -> Self {
3982        let mut highlights = None;
3983        if let Some((captures, highlight_maps)) = syntax {
3984            highlights = Some(BufferChunkHighlights {
3985                captures,
3986                next_capture: None,
3987                stack: Default::default(),
3988                highlight_maps,
3989            })
3990        }
3991
3992        let diagnostic_endpoints = diagnostics.then(|| Vec::new().into_iter().peekable());
3993        let chunks = text.chunks_in_range(range.clone());
3994
3995        let mut this = BufferChunks {
3996            range,
3997            buffer_snapshot,
3998            chunks,
3999            diagnostic_endpoints,
4000            error_depth: 0,
4001            warning_depth: 0,
4002            information_depth: 0,
4003            hint_depth: 0,
4004            unnecessary_depth: 0,
4005            highlights,
4006        };
4007        this.initialize_diagnostic_endpoints();
4008        this
4009    }
4010
4011    /// Seeks to the given byte offset in the buffer.
4012    pub fn seek(&mut self, range: Range<usize>) {
4013        let old_range = std::mem::replace(&mut self.range, range.clone());
4014        self.chunks.set_range(self.range.clone());
4015        if let Some(highlights) = self.highlights.as_mut() {
4016            if old_range.start >= self.range.start && old_range.end <= self.range.end {
4017                // Reuse existing highlights stack, as the new range is a subrange of the old one.
4018                highlights
4019                    .stack
4020                    .retain(|(end_offset, _)| *end_offset > range.start);
4021                if let Some(capture) = &highlights.next_capture {
4022                    if range.start >= capture.node.start_byte() {
4023                        let next_capture_end = capture.node.end_byte();
4024                        if range.start < next_capture_end {
4025                            highlights.stack.push((
4026                                next_capture_end,
4027                                highlights.highlight_maps[capture.grammar_index].get(capture.index),
4028                            ));
4029                        }
4030                        highlights.next_capture.take();
4031                    }
4032                }
4033            } else if let Some(snapshot) = self.buffer_snapshot {
4034                let (captures, highlight_maps) = snapshot.get_highlights(self.range.clone());
4035                *highlights = BufferChunkHighlights {
4036                    captures,
4037                    next_capture: None,
4038                    stack: Default::default(),
4039                    highlight_maps,
4040                };
4041            } else {
4042                // We cannot obtain new highlights for a language-aware buffer iterator, as we don't have a buffer snapshot.
4043                // Seeking such BufferChunks is not supported.
4044                debug_assert!(false, "Attempted to seek on a language-aware buffer iterator without associated buffer snapshot");
4045            }
4046
4047            highlights.captures.set_byte_range(self.range.clone());
4048            self.initialize_diagnostic_endpoints();
4049        }
4050    }
4051
4052    fn initialize_diagnostic_endpoints(&mut self) {
4053        if let Some(diagnostics) = self.diagnostic_endpoints.as_mut() {
4054            if let Some(buffer) = self.buffer_snapshot {
4055                let mut diagnostic_endpoints = Vec::new();
4056                for entry in buffer.diagnostics_in_range::<_, usize>(self.range.clone(), false) {
4057                    diagnostic_endpoints.push(DiagnosticEndpoint {
4058                        offset: entry.range.start,
4059                        is_start: true,
4060                        severity: entry.diagnostic.severity,
4061                        is_unnecessary: entry.diagnostic.is_unnecessary,
4062                    });
4063                    diagnostic_endpoints.push(DiagnosticEndpoint {
4064                        offset: entry.range.end,
4065                        is_start: false,
4066                        severity: entry.diagnostic.severity,
4067                        is_unnecessary: entry.diagnostic.is_unnecessary,
4068                    });
4069                }
4070                diagnostic_endpoints
4071                    .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
4072                *diagnostics = diagnostic_endpoints.into_iter().peekable();
4073            }
4074        }
4075    }
4076
4077    /// The current byte offset in the buffer.
4078    pub fn offset(&self) -> usize {
4079        self.range.start
4080    }
4081
4082    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
4083        let depth = match endpoint.severity {
4084            DiagnosticSeverity::ERROR => &mut self.error_depth,
4085            DiagnosticSeverity::WARNING => &mut self.warning_depth,
4086            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
4087            DiagnosticSeverity::HINT => &mut self.hint_depth,
4088            _ => return,
4089        };
4090        if endpoint.is_start {
4091            *depth += 1;
4092        } else {
4093            *depth -= 1;
4094        }
4095
4096        if endpoint.is_unnecessary {
4097            if endpoint.is_start {
4098                self.unnecessary_depth += 1;
4099            } else {
4100                self.unnecessary_depth -= 1;
4101            }
4102        }
4103    }
4104
4105    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
4106        if self.error_depth > 0 {
4107            Some(DiagnosticSeverity::ERROR)
4108        } else if self.warning_depth > 0 {
4109            Some(DiagnosticSeverity::WARNING)
4110        } else if self.information_depth > 0 {
4111            Some(DiagnosticSeverity::INFORMATION)
4112        } else if self.hint_depth > 0 {
4113            Some(DiagnosticSeverity::HINT)
4114        } else {
4115            None
4116        }
4117    }
4118
4119    fn current_code_is_unnecessary(&self) -> bool {
4120        self.unnecessary_depth > 0
4121    }
4122}
4123
4124impl<'a> Iterator for BufferChunks<'a> {
4125    type Item = Chunk<'a>;
4126
4127    fn next(&mut self) -> Option<Self::Item> {
4128        let mut next_capture_start = usize::MAX;
4129        let mut next_diagnostic_endpoint = usize::MAX;
4130
4131        if let Some(highlights) = self.highlights.as_mut() {
4132            while let Some((parent_capture_end, _)) = highlights.stack.last() {
4133                if *parent_capture_end <= self.range.start {
4134                    highlights.stack.pop();
4135                } else {
4136                    break;
4137                }
4138            }
4139
4140            if highlights.next_capture.is_none() {
4141                highlights.next_capture = highlights.captures.next();
4142            }
4143
4144            while let Some(capture) = highlights.next_capture.as_ref() {
4145                if self.range.start < capture.node.start_byte() {
4146                    next_capture_start = capture.node.start_byte();
4147                    break;
4148                } else {
4149                    let highlight_id =
4150                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
4151                    highlights
4152                        .stack
4153                        .push((capture.node.end_byte(), highlight_id));
4154                    highlights.next_capture = highlights.captures.next();
4155                }
4156            }
4157        }
4158
4159        let mut diagnostic_endpoints = std::mem::take(&mut self.diagnostic_endpoints);
4160        if let Some(diagnostic_endpoints) = diagnostic_endpoints.as_mut() {
4161            while let Some(endpoint) = diagnostic_endpoints.peek().copied() {
4162                if endpoint.offset <= self.range.start {
4163                    self.update_diagnostic_depths(endpoint);
4164                    diagnostic_endpoints.next();
4165                } else {
4166                    next_diagnostic_endpoint = endpoint.offset;
4167                    break;
4168                }
4169            }
4170        }
4171        self.diagnostic_endpoints = diagnostic_endpoints;
4172
4173        if let Some(chunk) = self.chunks.peek() {
4174            let chunk_start = self.range.start;
4175            let mut chunk_end = (self.chunks.offset() + chunk.len())
4176                .min(next_capture_start)
4177                .min(next_diagnostic_endpoint);
4178            let mut highlight_id = None;
4179            if let Some(highlights) = self.highlights.as_ref() {
4180                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
4181                    chunk_end = chunk_end.min(*parent_capture_end);
4182                    highlight_id = Some(*parent_highlight_id);
4183                }
4184            }
4185
4186            let slice =
4187                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
4188            self.range.start = chunk_end;
4189            if self.range.start == self.chunks.offset() + chunk.len() {
4190                self.chunks.next().unwrap();
4191            }
4192
4193            Some(Chunk {
4194                text: slice,
4195                syntax_highlight_id: highlight_id,
4196                diagnostic_severity: self.current_diagnostic_severity(),
4197                is_unnecessary: self.current_code_is_unnecessary(),
4198                ..Default::default()
4199            })
4200        } else {
4201            None
4202        }
4203    }
4204}
4205
4206impl operation_queue::Operation for Operation {
4207    fn lamport_timestamp(&self) -> clock::Lamport {
4208        match self {
4209            Operation::Buffer(_) => {
4210                unreachable!("buffer operations should never be deferred at this layer")
4211            }
4212            Operation::UpdateDiagnostics {
4213                lamport_timestamp, ..
4214            }
4215            | Operation::UpdateSelections {
4216                lamport_timestamp, ..
4217            }
4218            | Operation::UpdateCompletionTriggers {
4219                lamport_timestamp, ..
4220            } => *lamport_timestamp,
4221        }
4222    }
4223}
4224
4225impl Default for Diagnostic {
4226    fn default() -> Self {
4227        Self {
4228            source: Default::default(),
4229            code: None,
4230            severity: DiagnosticSeverity::ERROR,
4231            message: Default::default(),
4232            group_id: 0,
4233            is_primary: false,
4234            is_disk_based: false,
4235            is_unnecessary: false,
4236            data: None,
4237        }
4238    }
4239}
4240
4241impl IndentSize {
4242    /// Returns an [IndentSize] representing the given spaces.
4243    pub fn spaces(len: u32) -> Self {
4244        Self {
4245            len,
4246            kind: IndentKind::Space,
4247        }
4248    }
4249
4250    /// Returns an [IndentSize] representing a tab.
4251    pub fn tab() -> Self {
4252        Self {
4253            len: 1,
4254            kind: IndentKind::Tab,
4255        }
4256    }
4257
4258    /// An iterator over the characters represented by this [IndentSize].
4259    pub fn chars(&self) -> impl Iterator<Item = char> {
4260        iter::repeat(self.char()).take(self.len as usize)
4261    }
4262
4263    /// The character representation of this [IndentSize].
4264    pub fn char(&self) -> char {
4265        match self.kind {
4266            IndentKind::Space => ' ',
4267            IndentKind::Tab => '\t',
4268        }
4269    }
4270
4271    /// Consumes the current [IndentSize] and returns a new one that has
4272    /// been shrunk or enlarged by the given size along the given direction.
4273    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
4274        match direction {
4275            Ordering::Less => {
4276                if self.kind == size.kind && self.len >= size.len {
4277                    self.len -= size.len;
4278                }
4279            }
4280            Ordering::Equal => {}
4281            Ordering::Greater => {
4282                if self.len == 0 {
4283                    self = size;
4284                } else if self.kind == size.kind {
4285                    self.len += size.len;
4286                }
4287            }
4288        }
4289        self
4290    }
4291}
4292
4293#[cfg(any(test, feature = "test-support"))]
4294pub struct TestFile {
4295    pub path: Arc<Path>,
4296    pub root_name: String,
4297}
4298
4299#[cfg(any(test, feature = "test-support"))]
4300impl File for TestFile {
4301    fn path(&self) -> &Arc<Path> {
4302        &self.path
4303    }
4304
4305    fn full_path(&self, _: &gpui::AppContext) -> PathBuf {
4306        PathBuf::from(&self.root_name).join(self.path.as_ref())
4307    }
4308
4309    fn as_local(&self) -> Option<&dyn LocalFile> {
4310        None
4311    }
4312
4313    fn mtime(&self) -> Option<SystemTime> {
4314        unimplemented!()
4315    }
4316
4317    fn file_name<'a>(&'a self, _: &'a gpui::AppContext) -> &'a std::ffi::OsStr {
4318        self.path().file_name().unwrap_or(self.root_name.as_ref())
4319    }
4320
4321    fn worktree_id(&self, _: &AppContext) -> WorktreeId {
4322        WorktreeId::from_usize(0)
4323    }
4324
4325    fn is_deleted(&self) -> bool {
4326        unimplemented!()
4327    }
4328
4329    fn as_any(&self) -> &dyn std::any::Any {
4330        unimplemented!()
4331    }
4332
4333    fn to_proto(&self, _: &AppContext) -> rpc::proto::File {
4334        unimplemented!()
4335    }
4336
4337    fn is_private(&self) -> bool {
4338        false
4339    }
4340}
4341
4342pub(crate) fn contiguous_ranges(
4343    values: impl Iterator<Item = u32>,
4344    max_len: usize,
4345) -> impl Iterator<Item = Range<u32>> {
4346    let mut values = values;
4347    let mut current_range: Option<Range<u32>> = None;
4348    std::iter::from_fn(move || loop {
4349        if let Some(value) = values.next() {
4350            if let Some(range) = &mut current_range {
4351                if value == range.end && range.len() < max_len {
4352                    range.end += 1;
4353                    continue;
4354                }
4355            }
4356
4357            let prev_range = current_range.clone();
4358            current_range = Some(value..(value + 1));
4359            if prev_range.is_some() {
4360                return prev_range;
4361            }
4362        } else {
4363            return current_range.take();
4364        }
4365    })
4366}
4367
4368#[derive(Default, Debug)]
4369pub struct CharClassifier {
4370    scope: Option<LanguageScope>,
4371    for_completion: bool,
4372    ignore_punctuation: bool,
4373}
4374
4375impl CharClassifier {
4376    pub fn new(scope: Option<LanguageScope>) -> Self {
4377        Self {
4378            scope,
4379            for_completion: false,
4380            ignore_punctuation: false,
4381        }
4382    }
4383
4384    pub fn for_completion(self, for_completion: bool) -> Self {
4385        Self {
4386            for_completion,
4387            ..self
4388        }
4389    }
4390
4391    pub fn ignore_punctuation(self, ignore_punctuation: bool) -> Self {
4392        Self {
4393            ignore_punctuation,
4394            ..self
4395        }
4396    }
4397
4398    pub fn is_whitespace(&self, c: char) -> bool {
4399        self.kind(c) == CharKind::Whitespace
4400    }
4401
4402    pub fn is_word(&self, c: char) -> bool {
4403        self.kind(c) == CharKind::Word
4404    }
4405
4406    pub fn is_punctuation(&self, c: char) -> bool {
4407        self.kind(c) == CharKind::Punctuation
4408    }
4409
4410    pub fn kind(&self, c: char) -> CharKind {
4411        if c.is_whitespace() {
4412            return CharKind::Whitespace;
4413        } else if c.is_alphanumeric() || c == '_' {
4414            return CharKind::Word;
4415        }
4416
4417        if let Some(scope) = &self.scope {
4418            if let Some(characters) = scope.word_characters() {
4419                if characters.contains(&c) {
4420                    if c == '-' && !self.for_completion && !self.ignore_punctuation {
4421                        return CharKind::Punctuation;
4422                    }
4423                    return CharKind::Word;
4424                }
4425            }
4426        }
4427
4428        if self.ignore_punctuation {
4429            CharKind::Word
4430        } else {
4431            CharKind::Punctuation
4432        }
4433    }
4434}
4435
4436/// Find all of the ranges of whitespace that occur at the ends of lines
4437/// in the given rope.
4438///
4439/// This could also be done with a regex search, but this implementation
4440/// avoids copying text.
4441pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
4442    let mut ranges = Vec::new();
4443
4444    let mut offset = 0;
4445    let mut prev_chunk_trailing_whitespace_range = 0..0;
4446    for chunk in rope.chunks() {
4447        let mut prev_line_trailing_whitespace_range = 0..0;
4448        for (i, line) in chunk.split('\n').enumerate() {
4449            let line_end_offset = offset + line.len();
4450            let trimmed_line_len = line.trim_end_matches([' ', '\t']).len();
4451            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
4452
4453            if i == 0 && trimmed_line_len == 0 {
4454                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
4455            }
4456            if !prev_line_trailing_whitespace_range.is_empty() {
4457                ranges.push(prev_line_trailing_whitespace_range);
4458            }
4459
4460            offset = line_end_offset + 1;
4461            prev_line_trailing_whitespace_range = trailing_whitespace_range;
4462        }
4463
4464        offset -= 1;
4465        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
4466    }
4467
4468    if !prev_chunk_trailing_whitespace_range.is_empty() {
4469        ranges.push(prev_chunk_trailing_whitespace_range);
4470    }
4471
4472    ranges
4473}