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