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