buffer.rs

   1pub use crate::{
   2    diagnostic_set::DiagnosticSet,
   3    highlight_map::{HighlightId, HighlightMap},
   4    markdown::ParsedMarkdown,
   5    proto, BracketPair, Grammar, Language, LanguageConfig, LanguageRegistry, PLAIN_TEXT,
   6};
   7use crate::{
   8    diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
   9    language_settings::{language_settings, LanguageSettings},
  10    outline::OutlineItem,
  11    syntax_map::{
  12        SyntaxLayerInfo, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxMapMatches,
  13        SyntaxSnapshot, ToTreeSitterPoint,
  14    },
  15    CodeLabel, LanguageScope, Outline,
  16};
  17use anyhow::{anyhow, Result};
  18pub use clock::ReplicaId;
  19use futures::FutureExt as _;
  20use gpui::{AppContext, EventEmitter, HighlightStyle, ModelContext, Task};
  21use lsp::LanguageServerId;
  22use parking_lot::Mutex;
  23use similar::{ChangeTag, TextDiff};
  24use smallvec::SmallVec;
  25use smol::future::yield_now;
  26use std::{
  27    any::Any,
  28    cmp::{self, Ordering},
  29    collections::BTreeMap,
  30    ffi::OsStr,
  31    future::Future,
  32    iter::{self, Iterator, Peekable},
  33    mem,
  34    ops::{Deref, Range},
  35    path::{Path, PathBuf},
  36    str,
  37    sync::Arc,
  38    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  39    vec,
  40};
  41use sum_tree::TreeMap;
  42use text::operation_queue::OperationQueue;
  43pub use text::{Buffer as TextBuffer, BufferSnapshot as TextBufferSnapshot, *};
  44use theme::SyntaxTheme;
  45#[cfg(any(test, feature = "test-support"))]
  46use util::RandomCharIter;
  47use util::{RangeExt, TryFutureExt as _};
  48
  49#[cfg(any(test, feature = "test-support"))]
  50pub use {tree_sitter_rust, tree_sitter_typescript};
  51
  52pub use lsp::DiagnosticSeverity;
  53
  54pub struct Buffer {
  55    text: TextBuffer,
  56    diff_base: Option<String>,
  57    git_diff: git::diff::BufferDiff,
  58    file: Option<Arc<dyn File>>,
  59    saved_version: clock::Global,
  60    saved_version_fingerprint: RopeFingerprint,
  61    saved_mtime: SystemTime,
  62    transaction_depth: usize,
  63    was_dirty_before_starting_transaction: Option<bool>,
  64    language: Option<Arc<Language>>,
  65    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  66    pending_autoindent: Option<Task<()>>,
  67    sync_parse_timeout: Duration,
  68    syntax_map: Mutex<SyntaxMap>,
  69    parsing_in_background: bool,
  70    parse_count: usize,
  71    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
  72    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  73    selections_update_count: usize,
  74    diagnostics_update_count: usize,
  75    diagnostics_timestamp: clock::Lamport,
  76    file_update_count: usize,
  77    git_diff_update_count: usize,
  78    completion_triggers: Vec<String>,
  79    completion_triggers_timestamp: clock::Lamport,
  80    deferred_ops: OperationQueue<Operation>,
  81}
  82
  83pub struct BufferSnapshot {
  84    text: text::BufferSnapshot,
  85    pub git_diff: git::diff::BufferDiff,
  86    pub(crate) syntax: SyntaxSnapshot,
  87    file: Option<Arc<dyn File>>,
  88    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
  89    diagnostics_update_count: usize,
  90    file_update_count: usize,
  91    git_diff_update_count: usize,
  92    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  93    selections_update_count: usize,
  94    language: Option<Arc<Language>>,
  95    parse_count: usize,
  96}
  97
  98#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
  99pub struct IndentSize {
 100    pub len: u32,
 101    pub kind: IndentKind,
 102}
 103
 104#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
 105pub enum IndentKind {
 106    #[default]
 107    Space,
 108    Tab,
 109}
 110
 111#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 112pub enum CursorShape {
 113    #[default]
 114    Bar,
 115    Block,
 116    Underscore,
 117    Hollow,
 118}
 119
 120#[derive(Clone, Debug)]
 121struct SelectionSet {
 122    line_mode: bool,
 123    cursor_shape: CursorShape,
 124    selections: Arc<[Selection<Anchor>]>,
 125    lamport_timestamp: clock::Lamport,
 126}
 127
 128#[derive(Clone, Debug, PartialEq, Eq)]
 129pub struct GroupId {
 130    source: Arc<str>,
 131    id: usize,
 132}
 133
 134#[derive(Clone, Debug, PartialEq, Eq)]
 135pub struct Diagnostic {
 136    pub source: Option<String>,
 137    pub code: Option<String>,
 138    pub severity: DiagnosticSeverity,
 139    pub message: String,
 140    pub group_id: usize,
 141    pub is_valid: bool,
 142    pub is_primary: bool,
 143    pub is_disk_based: bool,
 144    pub is_unnecessary: bool,
 145}
 146
 147#[derive(Clone, Debug)]
 148pub struct Completion {
 149    pub old_range: Range<Anchor>,
 150    pub new_text: String,
 151    pub label: CodeLabel,
 152    pub server_id: LanguageServerId,
 153    pub lsp_completion: lsp::CompletionItem,
 154}
 155
 156#[derive(Clone, Debug)]
 157pub struct CodeAction {
 158    pub server_id: LanguageServerId,
 159    pub range: Range<Anchor>,
 160    pub lsp_action: lsp::CodeAction,
 161}
 162
 163#[derive(Clone, Debug, PartialEq)]
 164pub enum Operation {
 165    Buffer(text::Operation),
 166
 167    UpdateDiagnostics {
 168        server_id: LanguageServerId,
 169        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 170        lamport_timestamp: clock::Lamport,
 171    },
 172
 173    UpdateSelections {
 174        selections: Arc<[Selection<Anchor>]>,
 175        lamport_timestamp: clock::Lamport,
 176        line_mode: bool,
 177        cursor_shape: CursorShape,
 178    },
 179
 180    UpdateCompletionTriggers {
 181        triggers: Vec<String>,
 182        lamport_timestamp: clock::Lamport,
 183    },
 184}
 185
 186#[derive(Clone, Debug, PartialEq)]
 187pub enum Event {
 188    Operation(Operation),
 189    Edited,
 190    DirtyChanged,
 191    Saved,
 192    FileHandleChanged,
 193    Reloaded,
 194    DiffBaseChanged,
 195    LanguageChanged,
 196    Reparsed,
 197    DiagnosticsUpdated,
 198    Closed,
 199}
 200
 201pub trait File: Send + Sync {
 202    fn as_local(&self) -> Option<&dyn LocalFile>;
 203
 204    fn is_local(&self) -> bool {
 205        self.as_local().is_some()
 206    }
 207
 208    fn mtime(&self) -> SystemTime;
 209
 210    /// Returns the path of this file relative to the worktree's root directory.
 211    fn path(&self) -> &Arc<Path>;
 212
 213    /// Returns the path of this file relative to the worktree's parent directory (this means it
 214    /// includes the name of the worktree's root folder).
 215    fn full_path(&self, cx: &AppContext) -> PathBuf;
 216
 217    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 218    /// of its worktree, then this method will return the name of the worktree itself.
 219    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
 220
 221    /// Returns the id of the worktree to which this file belongs.
 222    ///
 223    /// This is needed for looking up project-specific settings.
 224    fn worktree_id(&self) -> usize;
 225
 226    fn is_deleted(&self) -> bool;
 227
 228    fn as_any(&self) -> &dyn Any;
 229
 230    fn to_proto(&self) -> rpc::proto::File;
 231}
 232
 233pub trait LocalFile: File {
 234    /// Returns the absolute path of this file.
 235    fn abs_path(&self, cx: &AppContext) -> PathBuf;
 236
 237    fn load(&self, cx: &AppContext) -> Task<Result<String>>;
 238
 239    fn buffer_reloaded(
 240        &self,
 241        buffer_id: u64,
 242        version: &clock::Global,
 243        fingerprint: RopeFingerprint,
 244        line_ending: LineEnding,
 245        mtime: SystemTime,
 246        cx: &mut AppContext,
 247    );
 248}
 249
 250#[derive(Clone, Debug)]
 251pub enum AutoindentMode {
 252    /// Indent each line of inserted text.
 253    EachLine,
 254    /// Apply the same indentation adjustment to all of the lines
 255    /// in a given insertion.
 256    Block {
 257        /// The original indentation level of the first line of each
 258        /// insertion, if it has been copied.
 259        original_indent_columns: Vec<u32>,
 260    },
 261}
 262
 263#[derive(Clone)]
 264struct AutoindentRequest {
 265    before_edit: BufferSnapshot,
 266    entries: Vec<AutoindentRequestEntry>,
 267    is_block_mode: bool,
 268}
 269
 270#[derive(Clone)]
 271struct AutoindentRequestEntry {
 272    /// A range of the buffer whose indentation should be adjusted.
 273    range: Range<Anchor>,
 274    /// Whether or not these lines should be considered brand new, for the
 275    /// purpose of auto-indent. When text is not new, its indentation will
 276    /// only be adjusted if the suggested indentation level has *changed*
 277    /// since the edit was made.
 278    first_line_is_new: bool,
 279    indent_size: IndentSize,
 280    original_indent_column: Option<u32>,
 281}
 282
 283#[derive(Debug)]
 284struct IndentSuggestion {
 285    basis_row: u32,
 286    delta: Ordering,
 287    within_error: bool,
 288}
 289
 290struct BufferChunkHighlights<'a> {
 291    captures: SyntaxMapCaptures<'a>,
 292    next_capture: Option<SyntaxMapCapture<'a>>,
 293    stack: Vec<(usize, HighlightId)>,
 294    highlight_maps: Vec<HighlightMap>,
 295}
 296
 297pub struct BufferChunks<'a> {
 298    range: Range<usize>,
 299    chunks: text::Chunks<'a>,
 300    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 301    error_depth: usize,
 302    warning_depth: usize,
 303    information_depth: usize,
 304    hint_depth: usize,
 305    unnecessary_depth: usize,
 306    highlights: Option<BufferChunkHighlights<'a>>,
 307}
 308
 309#[derive(Clone, Copy, Debug, Default)]
 310pub struct Chunk<'a> {
 311    pub text: &'a str,
 312    pub syntax_highlight_id: Option<HighlightId>,
 313    pub highlight_style: Option<HighlightStyle>,
 314    pub diagnostic_severity: Option<DiagnosticSeverity>,
 315    pub is_unnecessary: bool,
 316    pub is_tab: bool,
 317}
 318
 319pub struct Diff {
 320    pub(crate) base_version: clock::Global,
 321    line_ending: LineEnding,
 322    edits: Vec<(Range<usize>, Arc<str>)>,
 323}
 324
 325#[derive(Clone, Copy)]
 326pub(crate) struct DiagnosticEndpoint {
 327    offset: usize,
 328    is_start: bool,
 329    severity: DiagnosticSeverity,
 330    is_unnecessary: bool,
 331}
 332
 333#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 334pub enum CharKind {
 335    Whitespace,
 336    Punctuation,
 337    Word,
 338}
 339
 340impl CharKind {
 341    pub fn coerce_punctuation(self, treat_punctuation_as_word: bool) -> Self {
 342        if treat_punctuation_as_word && self == CharKind::Punctuation {
 343            CharKind::Word
 344        } else {
 345            self
 346        }
 347    }
 348}
 349
 350impl Buffer {
 351    pub fn new<T: Into<String>>(replica_id: ReplicaId, id: u64, base_text: T) -> Self {
 352        Self::build(
 353            TextBuffer::new(replica_id, id, base_text.into()),
 354            None,
 355            None,
 356        )
 357    }
 358
 359    pub fn remote(remote_id: u64, replica_id: ReplicaId, base_text: String) -> Self {
 360        Self::build(
 361            TextBuffer::new(replica_id, remote_id, base_text),
 362            None,
 363            None,
 364        )
 365    }
 366
 367    pub fn from_proto(
 368        replica_id: ReplicaId,
 369        message: proto::BufferState,
 370        file: Option<Arc<dyn File>>,
 371    ) -> Result<Self> {
 372        let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
 373        let mut this = Self::build(
 374            buffer,
 375            message.diff_base.map(|text| text.into_boxed_str().into()),
 376            file,
 377        );
 378        this.text.set_line_ending(proto::deserialize_line_ending(
 379            rpc::proto::LineEnding::from_i32(message.line_ending)
 380                .ok_or_else(|| anyhow!("missing line_ending"))?,
 381        ));
 382        this.saved_version = proto::deserialize_version(&message.saved_version);
 383        this.saved_version_fingerprint =
 384            proto::deserialize_fingerprint(&message.saved_version_fingerprint)?;
 385        this.saved_mtime = message
 386            .saved_mtime
 387            .ok_or_else(|| anyhow!("invalid saved_mtime"))?
 388            .into();
 389        Ok(this)
 390    }
 391
 392    pub fn to_proto(&self) -> proto::BufferState {
 393        proto::BufferState {
 394            id: self.remote_id(),
 395            file: self.file.as_ref().map(|f| f.to_proto()),
 396            base_text: self.base_text().to_string(),
 397            diff_base: self.diff_base.as_ref().map(|h| h.to_string()),
 398            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
 399            saved_version: proto::serialize_version(&self.saved_version),
 400            saved_version_fingerprint: proto::serialize_fingerprint(self.saved_version_fingerprint),
 401            saved_mtime: Some(self.saved_mtime.into()),
 402        }
 403    }
 404
 405    pub fn serialize_ops(
 406        &self,
 407        since: Option<clock::Global>,
 408        cx: &AppContext,
 409    ) -> Task<Vec<proto::Operation>> {
 410        let mut operations = Vec::new();
 411        operations.extend(self.deferred_ops.iter().map(proto::serialize_operation));
 412
 413        operations.extend(self.remote_selections.iter().map(|(_, set)| {
 414            proto::serialize_operation(&Operation::UpdateSelections {
 415                selections: set.selections.clone(),
 416                lamport_timestamp: set.lamport_timestamp,
 417                line_mode: set.line_mode,
 418                cursor_shape: set.cursor_shape,
 419            })
 420        }));
 421
 422        for (server_id, diagnostics) in &self.diagnostics {
 423            operations.push(proto::serialize_operation(&Operation::UpdateDiagnostics {
 424                lamport_timestamp: self.diagnostics_timestamp,
 425                server_id: *server_id,
 426                diagnostics: diagnostics.iter().cloned().collect(),
 427            }));
 428        }
 429
 430        operations.push(proto::serialize_operation(
 431            &Operation::UpdateCompletionTriggers {
 432                triggers: self.completion_triggers.clone(),
 433                lamport_timestamp: self.completion_triggers_timestamp,
 434            },
 435        ));
 436
 437        let text_operations = self.text.operations().clone();
 438        cx.background_executor().spawn(async move {
 439            let since = since.unwrap_or_default();
 440            operations.extend(
 441                text_operations
 442                    .iter()
 443                    .filter(|(_, op)| !since.observed(op.timestamp()))
 444                    .map(|(_, op)| proto::serialize_operation(&Operation::Buffer(op.clone()))),
 445            );
 446            operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
 447            operations
 448        })
 449    }
 450
 451    pub fn with_language(mut self, language: Arc<Language>, cx: &mut ModelContext<Self>) -> Self {
 452        self.set_language(Some(language), cx);
 453        self
 454    }
 455
 456    pub fn build(
 457        buffer: TextBuffer,
 458        diff_base: Option<String>,
 459        file: Option<Arc<dyn File>>,
 460    ) -> Self {
 461        let saved_mtime = if let Some(file) = file.as_ref() {
 462            file.mtime()
 463        } else {
 464            UNIX_EPOCH
 465        };
 466
 467        Self {
 468            saved_mtime,
 469            saved_version: buffer.version(),
 470            saved_version_fingerprint: buffer.as_rope().fingerprint(),
 471            transaction_depth: 0,
 472            was_dirty_before_starting_transaction: None,
 473            text: buffer,
 474            diff_base,
 475            git_diff: git::diff::BufferDiff::new(),
 476            file,
 477            syntax_map: Mutex::new(SyntaxMap::new()),
 478            parsing_in_background: false,
 479            parse_count: 0,
 480            sync_parse_timeout: Duration::from_millis(1),
 481            autoindent_requests: Default::default(),
 482            pending_autoindent: Default::default(),
 483            language: None,
 484            remote_selections: Default::default(),
 485            selections_update_count: 0,
 486            diagnostics: Default::default(),
 487            diagnostics_update_count: 0,
 488            diagnostics_timestamp: Default::default(),
 489            file_update_count: 0,
 490            git_diff_update_count: 0,
 491            completion_triggers: Default::default(),
 492            completion_triggers_timestamp: Default::default(),
 493            deferred_ops: OperationQueue::new(),
 494        }
 495    }
 496
 497    pub fn snapshot(&self) -> BufferSnapshot {
 498        let text = self.text.snapshot();
 499        let mut syntax_map = self.syntax_map.lock();
 500        syntax_map.interpolate(&text);
 501        let syntax = syntax_map.snapshot();
 502
 503        BufferSnapshot {
 504            text,
 505            syntax,
 506            git_diff: self.git_diff.clone(),
 507            file: self.file.clone(),
 508            remote_selections: self.remote_selections.clone(),
 509            diagnostics: self.diagnostics.clone(),
 510            diagnostics_update_count: self.diagnostics_update_count,
 511            file_update_count: self.file_update_count,
 512            git_diff_update_count: self.git_diff_update_count,
 513            language: self.language.clone(),
 514            parse_count: self.parse_count,
 515            selections_update_count: self.selections_update_count,
 516        }
 517    }
 518
 519    pub fn as_text_snapshot(&self) -> &text::BufferSnapshot {
 520        &self.text
 521    }
 522
 523    pub fn text_snapshot(&self) -> text::BufferSnapshot {
 524        self.text.snapshot()
 525    }
 526
 527    pub fn file(&self) -> Option<&Arc<dyn File>> {
 528        self.file.as_ref()
 529    }
 530
 531    pub fn saved_version(&self) -> &clock::Global {
 532        &self.saved_version
 533    }
 534
 535    pub fn saved_version_fingerprint(&self) -> RopeFingerprint {
 536        self.saved_version_fingerprint
 537    }
 538
 539    pub fn saved_mtime(&self) -> SystemTime {
 540        self.saved_mtime
 541    }
 542
 543    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 544        self.syntax_map.lock().clear();
 545        self.language = language;
 546        self.reparse(cx);
 547        cx.emit(Event::LanguageChanged);
 548    }
 549
 550    pub fn set_language_registry(&mut self, language_registry: Arc<LanguageRegistry>) {
 551        self.syntax_map
 552            .lock()
 553            .set_language_registry(language_registry);
 554    }
 555
 556    pub fn did_save(
 557        &mut self,
 558        version: clock::Global,
 559        fingerprint: RopeFingerprint,
 560        mtime: SystemTime,
 561        cx: &mut ModelContext<Self>,
 562    ) {
 563        self.saved_version = version;
 564        self.saved_version_fingerprint = fingerprint;
 565        self.saved_mtime = mtime;
 566        cx.emit(Event::Saved);
 567        cx.notify();
 568    }
 569
 570    pub fn reload(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<Option<Transaction>>> {
 571        cx.spawn(|this, mut cx| async move {
 572            if let Some((new_mtime, new_text)) = this.update(&mut cx, |this, cx| {
 573                let file = this.file.as_ref()?.as_local()?;
 574                Some((file.mtime(), file.load(cx)))
 575            })? {
 576                let new_text = new_text.await?;
 577                let diff = this
 578                    .update(&mut cx, |this, cx| this.diff(new_text, cx))?
 579                    .await;
 580                this.update(&mut cx, |this, cx| {
 581                    if this.version() == diff.base_version {
 582                        this.finalize_last_transaction();
 583                        this.apply_diff(diff, cx);
 584                        if let Some(transaction) = this.finalize_last_transaction().cloned() {
 585                            this.did_reload(
 586                                this.version(),
 587                                this.as_rope().fingerprint(),
 588                                this.line_ending(),
 589                                new_mtime,
 590                                cx,
 591                            );
 592                            return Some(transaction);
 593                        }
 594                    }
 595                    None
 596                })
 597            } else {
 598                Ok(None)
 599            }
 600        })
 601    }
 602
 603    pub fn did_reload(
 604        &mut self,
 605        version: clock::Global,
 606        fingerprint: RopeFingerprint,
 607        line_ending: LineEnding,
 608        mtime: SystemTime,
 609        cx: &mut ModelContext<Self>,
 610    ) {
 611        self.saved_version = version;
 612        self.saved_version_fingerprint = fingerprint;
 613        self.text.set_line_ending(line_ending);
 614        self.saved_mtime = mtime;
 615        if let Some(file) = self.file.as_ref().and_then(|f| f.as_local()) {
 616            file.buffer_reloaded(
 617                self.remote_id(),
 618                &self.saved_version,
 619                self.saved_version_fingerprint,
 620                self.line_ending(),
 621                self.saved_mtime,
 622                cx,
 623            );
 624        }
 625        cx.emit(Event::Reloaded);
 626        cx.notify();
 627    }
 628
 629    pub fn file_updated(
 630        &mut self,
 631        new_file: Arc<dyn File>,
 632        cx: &mut ModelContext<Self>,
 633    ) -> Task<()> {
 634        let mut file_changed = false;
 635        let mut task = Task::ready(());
 636
 637        if let Some(old_file) = self.file.as_ref() {
 638            if new_file.path() != old_file.path() {
 639                file_changed = true;
 640            }
 641
 642            if new_file.is_deleted() {
 643                if !old_file.is_deleted() {
 644                    file_changed = true;
 645                    if !self.is_dirty() {
 646                        cx.emit(Event::DirtyChanged);
 647                    }
 648                }
 649            } else {
 650                let new_mtime = new_file.mtime();
 651                if new_mtime != old_file.mtime() {
 652                    file_changed = true;
 653
 654                    if !self.is_dirty() {
 655                        let reload = self.reload(cx).log_err().map(drop);
 656                        task = cx.background_executor().spawn(reload);
 657                    }
 658                }
 659            }
 660        } else {
 661            file_changed = true;
 662        };
 663
 664        self.file = Some(new_file);
 665        if file_changed {
 666            self.file_update_count += 1;
 667            cx.emit(Event::FileHandleChanged);
 668            cx.notify();
 669        }
 670        task
 671    }
 672
 673    pub fn diff_base(&self) -> Option<&str> {
 674        self.diff_base.as_deref()
 675    }
 676
 677    pub fn set_diff_base(&mut self, diff_base: Option<String>, cx: &mut ModelContext<Self>) {
 678        self.diff_base = diff_base;
 679        self.git_diff_recalc(cx);
 680        cx.emit(Event::DiffBaseChanged);
 681    }
 682
 683    pub fn git_diff_recalc(&mut self, cx: &mut ModelContext<Self>) -> Option<Task<()>> {
 684        let diff_base = self.diff_base.clone()?; // TODO: Make this an Arc
 685        let snapshot = self.snapshot();
 686
 687        let mut diff = self.git_diff.clone();
 688        let diff = cx.background_executor().spawn(async move {
 689            diff.update(&diff_base, &snapshot).await;
 690            diff
 691        });
 692
 693        Some(cx.spawn(|this, mut cx| async move {
 694            let buffer_diff = diff.await;
 695            this.update(&mut cx, |this, _| {
 696                this.git_diff = buffer_diff;
 697                this.git_diff_update_count += 1;
 698            })
 699            .ok();
 700        }))
 701    }
 702
 703    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 704        cx.emit(Event::Closed);
 705    }
 706
 707    pub fn language(&self) -> Option<&Arc<Language>> {
 708        self.language.as_ref()
 709    }
 710
 711    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
 712        let offset = position.to_offset(self);
 713        self.syntax_map
 714            .lock()
 715            .layers_for_range(offset..offset, &self.text)
 716            .last()
 717            .map(|info| info.language.clone())
 718            .or_else(|| self.language.clone())
 719    }
 720
 721    pub fn parse_count(&self) -> usize {
 722        self.parse_count
 723    }
 724
 725    pub fn selections_update_count(&self) -> usize {
 726        self.selections_update_count
 727    }
 728
 729    pub fn diagnostics_update_count(&self) -> usize {
 730        self.diagnostics_update_count
 731    }
 732
 733    pub fn file_update_count(&self) -> usize {
 734        self.file_update_count
 735    }
 736
 737    pub fn git_diff_update_count(&self) -> usize {
 738        self.git_diff_update_count
 739    }
 740
 741    #[cfg(any(test, feature = "test-support"))]
 742    pub fn is_parsing(&self) -> bool {
 743        self.parsing_in_background
 744    }
 745
 746    pub fn contains_unknown_injections(&self) -> bool {
 747        self.syntax_map.lock().contains_unknown_injections()
 748    }
 749
 750    #[cfg(test)]
 751    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 752        self.sync_parse_timeout = timeout;
 753    }
 754
 755    /// Called after an edit to synchronize the buffer's main parse tree with
 756    /// the buffer's new underlying state.
 757    ///
 758    /// Locks the syntax map and interpolates the edits since the last reparse
 759    /// into the foreground syntax tree.
 760    ///
 761    /// Then takes a stable snapshot of the syntax map before unlocking it.
 762    /// The snapshot with the interpolated edits is sent to a background thread,
 763    /// where we ask Tree-sitter to perform an incremental parse.
 764    ///
 765    /// Meanwhile, in the foreground, we block the main thread for up to 1ms
 766    /// waiting on the parse to complete. As soon as it completes, we proceed
 767    /// synchronously, unless a 1ms timeout elapses.
 768    ///
 769    /// If we time out waiting on the parse, we spawn a second task waiting
 770    /// until the parse does complete and return with the interpolated tree still
 771    /// in the foreground. When the background parse completes, call back into
 772    /// the main thread and assign the foreground parse state.
 773    ///
 774    /// If the buffer or grammar changed since the start of the background parse,
 775    /// initiate an additional reparse recursively. To avoid concurrent parses
 776    /// for the same buffer, we only initiate a new parse if we are not already
 777    /// parsing in the background.
 778    pub fn reparse(&mut self, cx: &mut ModelContext<Self>) {
 779        if self.parsing_in_background {
 780            return;
 781        }
 782        let language = if let Some(language) = self.language.clone() {
 783            language
 784        } else {
 785            return;
 786        };
 787
 788        let text = self.text_snapshot();
 789        let parsed_version = self.version();
 790
 791        let mut syntax_map = self.syntax_map.lock();
 792        syntax_map.interpolate(&text);
 793        let language_registry = syntax_map.language_registry();
 794        let mut syntax_snapshot = syntax_map.snapshot();
 795        drop(syntax_map);
 796
 797        let parse_task = cx.background_executor().spawn({
 798            let language = language.clone();
 799            let language_registry = language_registry.clone();
 800            async move {
 801                syntax_snapshot.reparse(&text, language_registry, language);
 802                syntax_snapshot
 803            }
 804        });
 805
 806        match cx
 807            .background_executor()
 808            .block_with_timeout(self.sync_parse_timeout, parse_task)
 809        {
 810            Ok(new_syntax_snapshot) => {
 811                self.did_finish_parsing(new_syntax_snapshot, cx);
 812                return;
 813            }
 814            Err(parse_task) => {
 815                self.parsing_in_background = true;
 816                cx.spawn(move |this, mut cx| async move {
 817                    let new_syntax_map = parse_task.await;
 818                    this.update(&mut cx, move |this, cx| {
 819                        let grammar_changed =
 820                            this.language.as_ref().map_or(true, |current_language| {
 821                                !Arc::ptr_eq(&language, current_language)
 822                            });
 823                        let language_registry_changed = new_syntax_map
 824                            .contains_unknown_injections()
 825                            && language_registry.map_or(false, |registry| {
 826                                registry.version() != new_syntax_map.language_registry_version()
 827                            });
 828                        let parse_again = language_registry_changed
 829                            || grammar_changed
 830                            || this.version.changed_since(&parsed_version);
 831                        this.did_finish_parsing(new_syntax_map, cx);
 832                        this.parsing_in_background = false;
 833                        if parse_again {
 834                            this.reparse(cx);
 835                        }
 836                    })
 837                    .ok();
 838                })
 839                .detach();
 840            }
 841        }
 842    }
 843
 844    fn did_finish_parsing(&mut self, syntax_snapshot: SyntaxSnapshot, cx: &mut ModelContext<Self>) {
 845        self.parse_count += 1;
 846        self.syntax_map.lock().did_parse(syntax_snapshot);
 847        self.request_autoindent(cx);
 848        cx.emit(Event::Reparsed);
 849        cx.notify();
 850    }
 851
 852    pub fn update_diagnostics(
 853        &mut self,
 854        server_id: LanguageServerId,
 855        diagnostics: DiagnosticSet,
 856        cx: &mut ModelContext<Self>,
 857    ) {
 858        let lamport_timestamp = self.text.lamport_clock.tick();
 859        let op = Operation::UpdateDiagnostics {
 860            server_id,
 861            diagnostics: diagnostics.iter().cloned().collect(),
 862            lamport_timestamp,
 863        };
 864        self.apply_diagnostic_update(server_id, diagnostics, lamport_timestamp, cx);
 865        self.send_operation(op, cx);
 866    }
 867
 868    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 869        if let Some(indent_sizes) = self.compute_autoindents() {
 870            let indent_sizes = cx.background_executor().spawn(indent_sizes);
 871            match cx
 872                .background_executor()
 873                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 874            {
 875                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
 876                Err(indent_sizes) => {
 877                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 878                        let indent_sizes = indent_sizes.await;
 879                        this.update(&mut cx, |this, cx| {
 880                            this.apply_autoindents(indent_sizes, cx);
 881                        })
 882                        .ok();
 883                    }));
 884                }
 885            }
 886        } else {
 887            self.autoindent_requests.clear();
 888        }
 889    }
 890
 891    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
 892        let max_rows_between_yields = 100;
 893        let snapshot = self.snapshot();
 894        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
 895            return None;
 896        }
 897
 898        let autoindent_requests = self.autoindent_requests.clone();
 899        Some(async move {
 900            let mut indent_sizes = BTreeMap::new();
 901            for request in autoindent_requests {
 902                // Resolve each edited range to its row in the current buffer and in the
 903                // buffer before this batch of edits.
 904                let mut row_ranges = Vec::new();
 905                let mut old_to_new_rows = BTreeMap::new();
 906                let mut language_indent_sizes_by_new_row = Vec::new();
 907                for entry in &request.entries {
 908                    let position = entry.range.start;
 909                    let new_row = position.to_point(&snapshot).row;
 910                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
 911                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
 912
 913                    if !entry.first_line_is_new {
 914                        let old_row = position.to_point(&request.before_edit).row;
 915                        old_to_new_rows.insert(old_row, new_row);
 916                    }
 917                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
 918                }
 919
 920                // Build a map containing the suggested indentation for each of the edited lines
 921                // with respect to the state of the buffer before these edits. This map is keyed
 922                // by the rows for these lines in the current state of the buffer.
 923                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
 924                let old_edited_ranges =
 925                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 926                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 927                let mut language_indent_size = IndentSize::default();
 928                for old_edited_range in old_edited_ranges {
 929                    let suggestions = request
 930                        .before_edit
 931                        .suggest_autoindents(old_edited_range.clone())
 932                        .into_iter()
 933                        .flatten();
 934                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 935                        if let Some(suggestion) = suggestion {
 936                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
 937
 938                            // Find the indent size based on the language for this row.
 939                            while let Some((row, size)) = language_indent_sizes.peek() {
 940                                if *row > new_row {
 941                                    break;
 942                                }
 943                                language_indent_size = *size;
 944                                language_indent_sizes.next();
 945                            }
 946
 947                            let suggested_indent = old_to_new_rows
 948                                .get(&suggestion.basis_row)
 949                                .and_then(|from_row| {
 950                                    Some(old_suggestions.get(from_row).copied()?.0)
 951                                })
 952                                .unwrap_or_else(|| {
 953                                    request
 954                                        .before_edit
 955                                        .indent_size_for_line(suggestion.basis_row)
 956                                })
 957                                .with_delta(suggestion.delta, language_indent_size);
 958                            old_suggestions
 959                                .insert(new_row, (suggested_indent, suggestion.within_error));
 960                        }
 961                    }
 962                    yield_now().await;
 963                }
 964
 965                // In block mode, only compute indentation suggestions for the first line
 966                // of each insertion. Otherwise, compute suggestions for every inserted line.
 967                let new_edited_row_ranges = contiguous_ranges(
 968                    row_ranges.iter().flat_map(|(range, _)| {
 969                        if request.is_block_mode {
 970                            range.start..range.start + 1
 971                        } else {
 972                            range.clone()
 973                        }
 974                    }),
 975                    max_rows_between_yields,
 976                );
 977
 978                // Compute new suggestions for each line, but only include them in the result
 979                // if they differ from the old suggestion for that line.
 980                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 981                let mut language_indent_size = IndentSize::default();
 982                for new_edited_row_range in new_edited_row_ranges {
 983                    let suggestions = snapshot
 984                        .suggest_autoindents(new_edited_row_range.clone())
 985                        .into_iter()
 986                        .flatten();
 987                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 988                        if let Some(suggestion) = suggestion {
 989                            // Find the indent size based on the language for this row.
 990                            while let Some((row, size)) = language_indent_sizes.peek() {
 991                                if *row > new_row {
 992                                    break;
 993                                }
 994                                language_indent_size = *size;
 995                                language_indent_sizes.next();
 996                            }
 997
 998                            let suggested_indent = indent_sizes
 999                                .get(&suggestion.basis_row)
1000                                .copied()
1001                                .unwrap_or_else(|| {
1002                                    snapshot.indent_size_for_line(suggestion.basis_row)
1003                                })
1004                                .with_delta(suggestion.delta, language_indent_size);
1005                            if old_suggestions.get(&new_row).map_or(
1006                                true,
1007                                |(old_indentation, was_within_error)| {
1008                                    suggested_indent != *old_indentation
1009                                        && (!suggestion.within_error || *was_within_error)
1010                                },
1011                            ) {
1012                                indent_sizes.insert(new_row, suggested_indent);
1013                            }
1014                        }
1015                    }
1016                    yield_now().await;
1017                }
1018
1019                // For each block of inserted text, adjust the indentation of the remaining
1020                // lines of the block by the same amount as the first line was adjusted.
1021                if request.is_block_mode {
1022                    for (row_range, original_indent_column) in
1023                        row_ranges
1024                            .into_iter()
1025                            .filter_map(|(range, original_indent_column)| {
1026                                if range.len() > 1 {
1027                                    Some((range, original_indent_column?))
1028                                } else {
1029                                    None
1030                                }
1031                            })
1032                    {
1033                        let new_indent = indent_sizes
1034                            .get(&row_range.start)
1035                            .copied()
1036                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
1037                        let delta = new_indent.len as i64 - original_indent_column as i64;
1038                        if delta != 0 {
1039                            for row in row_range.skip(1) {
1040                                indent_sizes.entry(row).or_insert_with(|| {
1041                                    let mut size = snapshot.indent_size_for_line(row);
1042                                    if size.kind == new_indent.kind {
1043                                        match delta.cmp(&0) {
1044                                            Ordering::Greater => size.len += delta as u32,
1045                                            Ordering::Less => {
1046                                                size.len = size.len.saturating_sub(-delta as u32)
1047                                            }
1048                                            Ordering::Equal => {}
1049                                        }
1050                                    }
1051                                    size
1052                                });
1053                            }
1054                        }
1055                    }
1056                }
1057            }
1058
1059            indent_sizes
1060        })
1061    }
1062
1063    fn apply_autoindents(
1064        &mut self,
1065        indent_sizes: BTreeMap<u32, IndentSize>,
1066        cx: &mut ModelContext<Self>,
1067    ) {
1068        self.autoindent_requests.clear();
1069
1070        let edits: Vec<_> = indent_sizes
1071            .into_iter()
1072            .filter_map(|(row, indent_size)| {
1073                let current_size = indent_size_for_line(self, row);
1074                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
1075            })
1076            .collect();
1077
1078        self.edit(edits, None, cx);
1079    }
1080
1081    // Create a minimal edit that will cause the the given row to be indented
1082    // with the given size. After applying this edit, the length of the line
1083    // will always be at least `new_size.len`.
1084    pub fn edit_for_indent_size_adjustment(
1085        row: u32,
1086        current_size: IndentSize,
1087        new_size: IndentSize,
1088    ) -> Option<(Range<Point>, String)> {
1089        if new_size.kind != current_size.kind {
1090            Some((
1091                Point::new(row, 0)..Point::new(row, current_size.len),
1092                iter::repeat(new_size.char())
1093                    .take(new_size.len as usize)
1094                    .collect::<String>(),
1095            ))
1096        } else {
1097            match new_size.len.cmp(&current_size.len) {
1098                Ordering::Greater => {
1099                    let point = Point::new(row, 0);
1100                    Some((
1101                        point..point,
1102                        iter::repeat(new_size.char())
1103                            .take((new_size.len - current_size.len) as usize)
1104                            .collect::<String>(),
1105                    ))
1106                }
1107
1108                Ordering::Less => Some((
1109                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
1110                    String::new(),
1111                )),
1112
1113                Ordering::Equal => None,
1114            }
1115        }
1116    }
1117
1118    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
1119        let old_text = self.as_rope().clone();
1120        let base_version = self.version();
1121        cx.background_executor().spawn(async move {
1122            let old_text = old_text.to_string();
1123            let line_ending = LineEnding::detect(&new_text);
1124            LineEnding::normalize(&mut new_text);
1125            let diff = TextDiff::from_chars(old_text.as_str(), new_text.as_str());
1126            let mut edits = Vec::new();
1127            let mut offset = 0;
1128            let empty: Arc<str> = "".into();
1129            for change in diff.iter_all_changes() {
1130                let value = change.value();
1131                let end_offset = offset + value.len();
1132                match change.tag() {
1133                    ChangeTag::Equal => {
1134                        offset = end_offset;
1135                    }
1136                    ChangeTag::Delete => {
1137                        edits.push((offset..end_offset, empty.clone()));
1138                        offset = end_offset;
1139                    }
1140                    ChangeTag::Insert => {
1141                        edits.push((offset..offset, value.into()));
1142                    }
1143                }
1144            }
1145            Diff {
1146                base_version,
1147                line_ending,
1148                edits,
1149            }
1150        })
1151    }
1152
1153    /// Spawn a background task that searches the buffer for any whitespace
1154    /// at the ends of a lines, and returns a `Diff` that removes that whitespace.
1155    pub fn remove_trailing_whitespace(&self, cx: &AppContext) -> Task<Diff> {
1156        let old_text = self.as_rope().clone();
1157        let line_ending = self.line_ending();
1158        let base_version = self.version();
1159        cx.background_executor().spawn(async move {
1160            let ranges = trailing_whitespace_ranges(&old_text);
1161            let empty = Arc::<str>::from("");
1162            Diff {
1163                base_version,
1164                line_ending,
1165                edits: ranges
1166                    .into_iter()
1167                    .map(|range| (range, empty.clone()))
1168                    .collect(),
1169            }
1170        })
1171    }
1172
1173    /// Ensure that the buffer ends with a single newline character, and
1174    /// no other whitespace.
1175    pub fn ensure_final_newline(&mut self, cx: &mut ModelContext<Self>) {
1176        let len = self.len();
1177        let mut offset = len;
1178        for chunk in self.as_rope().reversed_chunks_in_range(0..len) {
1179            let non_whitespace_len = chunk
1180                .trim_end_matches(|c: char| c.is_ascii_whitespace())
1181                .len();
1182            offset -= chunk.len();
1183            offset += non_whitespace_len;
1184            if non_whitespace_len != 0 {
1185                if offset == len - 1 && chunk.get(non_whitespace_len..) == Some("\n") {
1186                    return;
1187                }
1188                break;
1189            }
1190        }
1191        self.edit([(offset..len, "\n")], None, cx);
1192    }
1193
1194    /// Apply a diff to the buffer. If the buffer has changed since the given diff was
1195    /// calculated, then adjust the diff to account for those changes, and discard any
1196    /// parts of the diff that conflict with those changes.
1197    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1198        // Check for any edits to the buffer that have occurred since this diff
1199        // was computed.
1200        let snapshot = self.snapshot();
1201        let mut edits_since = snapshot.edits_since::<usize>(&diff.base_version).peekable();
1202        let mut delta = 0;
1203        let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
1204            while let Some(edit_since) = edits_since.peek() {
1205                // If the edit occurs after a diff hunk, then it does not
1206                // affect that hunk.
1207                if edit_since.old.start > range.end {
1208                    break;
1209                }
1210                // If the edit precedes the diff hunk, then adjust the hunk
1211                // to reflect the edit.
1212                else if edit_since.old.end < range.start {
1213                    delta += edit_since.new_len() as i64 - edit_since.old_len() as i64;
1214                    edits_since.next();
1215                }
1216                // If the edit intersects a diff hunk, then discard that hunk.
1217                else {
1218                    return None;
1219                }
1220            }
1221
1222            let start = (range.start as i64 + delta) as usize;
1223            let end = (range.end as i64 + delta) as usize;
1224            Some((start..end, new_text))
1225        });
1226
1227        self.start_transaction();
1228        self.text.set_line_ending(diff.line_ending);
1229        self.edit(adjusted_edits, None, cx);
1230        self.end_transaction(cx)
1231    }
1232
1233    pub fn is_dirty(&self) -> bool {
1234        self.saved_version_fingerprint != self.as_rope().fingerprint()
1235            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1236    }
1237
1238    pub fn has_conflict(&self) -> bool {
1239        self.saved_version_fingerprint != self.as_rope().fingerprint()
1240            && self
1241                .file
1242                .as_ref()
1243                .map_or(false, |file| file.mtime() > self.saved_mtime)
1244    }
1245
1246    pub fn subscribe(&mut self) -> Subscription {
1247        self.text.subscribe()
1248    }
1249
1250    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1251        self.start_transaction_at(Instant::now())
1252    }
1253
1254    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1255        self.transaction_depth += 1;
1256        if self.was_dirty_before_starting_transaction.is_none() {
1257            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1258        }
1259        self.text.start_transaction_at(now)
1260    }
1261
1262    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1263        self.end_transaction_at(Instant::now(), cx)
1264    }
1265
1266    pub fn end_transaction_at(
1267        &mut self,
1268        now: Instant,
1269        cx: &mut ModelContext<Self>,
1270    ) -> Option<TransactionId> {
1271        assert!(self.transaction_depth > 0);
1272        self.transaction_depth -= 1;
1273        let was_dirty = if self.transaction_depth == 0 {
1274            self.was_dirty_before_starting_transaction.take().unwrap()
1275        } else {
1276            false
1277        };
1278        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1279            self.did_edit(&start_version, was_dirty, cx);
1280            Some(transaction_id)
1281        } else {
1282            None
1283        }
1284    }
1285
1286    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1287        self.text.push_transaction(transaction, now);
1288    }
1289
1290    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1291        self.text.finalize_last_transaction()
1292    }
1293
1294    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1295        self.text.group_until_transaction(transaction_id);
1296    }
1297
1298    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1299        self.text.forget_transaction(transaction_id);
1300    }
1301
1302    pub fn merge_transactions(&mut self, transaction: TransactionId, destination: TransactionId) {
1303        self.text.merge_transactions(transaction, destination);
1304    }
1305
1306    pub fn wait_for_edits(
1307        &mut self,
1308        edit_ids: impl IntoIterator<Item = clock::Lamport>,
1309    ) -> impl Future<Output = Result<()>> {
1310        self.text.wait_for_edits(edit_ids)
1311    }
1312
1313    pub fn wait_for_anchors(
1314        &mut self,
1315        anchors: impl IntoIterator<Item = Anchor>,
1316    ) -> impl 'static + Future<Output = Result<()>> {
1317        self.text.wait_for_anchors(anchors)
1318    }
1319
1320    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = Result<()>> {
1321        self.text.wait_for_version(version)
1322    }
1323
1324    pub fn give_up_waiting(&mut self) {
1325        self.text.give_up_waiting();
1326    }
1327
1328    pub fn set_active_selections(
1329        &mut self,
1330        selections: Arc<[Selection<Anchor>]>,
1331        line_mode: bool,
1332        cursor_shape: CursorShape,
1333        cx: &mut ModelContext<Self>,
1334    ) {
1335        let lamport_timestamp = self.text.lamport_clock.tick();
1336        self.remote_selections.insert(
1337            self.text.replica_id(),
1338            SelectionSet {
1339                selections: selections.clone(),
1340                lamport_timestamp,
1341                line_mode,
1342                cursor_shape,
1343            },
1344        );
1345        self.send_operation(
1346            Operation::UpdateSelections {
1347                selections,
1348                line_mode,
1349                lamport_timestamp,
1350                cursor_shape,
1351            },
1352            cx,
1353        );
1354    }
1355
1356    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1357        if self
1358            .remote_selections
1359            .get(&self.text.replica_id())
1360            .map_or(true, |set| !set.selections.is_empty())
1361        {
1362            self.set_active_selections(Arc::from([]), false, Default::default(), cx);
1363        }
1364    }
1365
1366    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Lamport>
1367    where
1368        T: Into<Arc<str>>,
1369    {
1370        self.autoindent_requests.clear();
1371        self.edit([(0..self.len(), text)], None, cx)
1372    }
1373
1374    pub fn edit<I, S, T>(
1375        &mut self,
1376        edits_iter: I,
1377        autoindent_mode: Option<AutoindentMode>,
1378        cx: &mut ModelContext<Self>,
1379    ) -> Option<clock::Lamport>
1380    where
1381        I: IntoIterator<Item = (Range<S>, T)>,
1382        S: ToOffset,
1383        T: Into<Arc<str>>,
1384    {
1385        // Skip invalid edits and coalesce contiguous ones.
1386        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1387        for (range, new_text) in edits_iter {
1388            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1389            if range.start > range.end {
1390                mem::swap(&mut range.start, &mut range.end);
1391            }
1392            let new_text = new_text.into();
1393            if !new_text.is_empty() || !range.is_empty() {
1394                if let Some((prev_range, prev_text)) = edits.last_mut() {
1395                    if prev_range.end >= range.start {
1396                        prev_range.end = cmp::max(prev_range.end, range.end);
1397                        *prev_text = format!("{prev_text}{new_text}").into();
1398                    } else {
1399                        edits.push((range, new_text));
1400                    }
1401                } else {
1402                    edits.push((range, new_text));
1403                }
1404            }
1405        }
1406        if edits.is_empty() {
1407            return None;
1408        }
1409
1410        self.start_transaction();
1411        self.pending_autoindent.take();
1412        let autoindent_request = autoindent_mode
1413            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1414
1415        let edit_operation = self.text.edit(edits.iter().cloned());
1416        let edit_id = edit_operation.timestamp();
1417
1418        if let Some((before_edit, mode)) = autoindent_request {
1419            let mut delta = 0isize;
1420            let entries = edits
1421                .into_iter()
1422                .enumerate()
1423                .zip(&edit_operation.as_edit().unwrap().new_text)
1424                .map(|((ix, (range, _)), new_text)| {
1425                    let new_text_length = new_text.len();
1426                    let old_start = range.start.to_point(&before_edit);
1427                    let new_start = (delta + range.start as isize) as usize;
1428                    delta += new_text_length as isize - (range.end as isize - range.start as isize);
1429
1430                    let mut range_of_insertion_to_indent = 0..new_text_length;
1431                    let mut first_line_is_new = false;
1432                    let mut original_indent_column = None;
1433
1434                    // When inserting an entire line at the beginning of an existing line,
1435                    // treat the insertion as new.
1436                    if new_text.contains('\n')
1437                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1438                    {
1439                        first_line_is_new = true;
1440                    }
1441
1442                    // When inserting text starting with a newline, avoid auto-indenting the
1443                    // previous line.
1444                    if new_text.starts_with('\n') {
1445                        range_of_insertion_to_indent.start += 1;
1446                        first_line_is_new = true;
1447                    }
1448
1449                    // Avoid auto-indenting after the insertion.
1450                    if let AutoindentMode::Block {
1451                        original_indent_columns,
1452                    } = &mode
1453                    {
1454                        original_indent_column =
1455                            Some(original_indent_columns.get(ix).copied().unwrap_or_else(|| {
1456                                indent_size_for_text(
1457                                    new_text[range_of_insertion_to_indent.clone()].chars(),
1458                                )
1459                                .len
1460                            }));
1461                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1462                            range_of_insertion_to_indent.end -= 1;
1463                        }
1464                    }
1465
1466                    AutoindentRequestEntry {
1467                        first_line_is_new,
1468                        original_indent_column,
1469                        indent_size: before_edit.language_indent_size_at(range.start, cx),
1470                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1471                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1472                    }
1473                })
1474                .collect();
1475
1476            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1477                before_edit,
1478                entries,
1479                is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
1480            }));
1481        }
1482
1483        self.end_transaction(cx);
1484        self.send_operation(Operation::Buffer(edit_operation), cx);
1485        Some(edit_id)
1486    }
1487
1488    fn did_edit(
1489        &mut self,
1490        old_version: &clock::Global,
1491        was_dirty: bool,
1492        cx: &mut ModelContext<Self>,
1493    ) {
1494        if self.edits_since::<usize>(old_version).next().is_none() {
1495            return;
1496        }
1497
1498        self.reparse(cx);
1499
1500        cx.emit(Event::Edited);
1501        if was_dirty != self.is_dirty() {
1502            cx.emit(Event::DirtyChanged);
1503        }
1504        cx.notify();
1505    }
1506
1507    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1508        &mut self,
1509        ops: I,
1510        cx: &mut ModelContext<Self>,
1511    ) -> Result<()> {
1512        self.pending_autoindent.take();
1513        let was_dirty = self.is_dirty();
1514        let old_version = self.version.clone();
1515        let mut deferred_ops = Vec::new();
1516        let buffer_ops = ops
1517            .into_iter()
1518            .filter_map(|op| match op {
1519                Operation::Buffer(op) => Some(op),
1520                _ => {
1521                    if self.can_apply_op(&op) {
1522                        self.apply_op(op, cx);
1523                    } else {
1524                        deferred_ops.push(op);
1525                    }
1526                    None
1527                }
1528            })
1529            .collect::<Vec<_>>();
1530        self.text.apply_ops(buffer_ops)?;
1531        self.deferred_ops.insert(deferred_ops);
1532        self.flush_deferred_ops(cx);
1533        self.did_edit(&old_version, was_dirty, cx);
1534        // Notify independently of whether the buffer was edited as the operations could include a
1535        // selection update.
1536        cx.notify();
1537        Ok(())
1538    }
1539
1540    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1541        let mut deferred_ops = Vec::new();
1542        for op in self.deferred_ops.drain().iter().cloned() {
1543            if self.can_apply_op(&op) {
1544                self.apply_op(op, cx);
1545            } else {
1546                deferred_ops.push(op);
1547            }
1548        }
1549        self.deferred_ops.insert(deferred_ops);
1550    }
1551
1552    fn can_apply_op(&self, operation: &Operation) -> bool {
1553        match operation {
1554            Operation::Buffer(_) => {
1555                unreachable!("buffer operations should never be applied at this layer")
1556            }
1557            Operation::UpdateDiagnostics {
1558                diagnostics: diagnostic_set,
1559                ..
1560            } => diagnostic_set.iter().all(|diagnostic| {
1561                self.text.can_resolve(&diagnostic.range.start)
1562                    && self.text.can_resolve(&diagnostic.range.end)
1563            }),
1564            Operation::UpdateSelections { selections, .. } => selections
1565                .iter()
1566                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1567            Operation::UpdateCompletionTriggers { .. } => true,
1568        }
1569    }
1570
1571    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1572        match operation {
1573            Operation::Buffer(_) => {
1574                unreachable!("buffer operations should never be applied at this layer")
1575            }
1576            Operation::UpdateDiagnostics {
1577                server_id,
1578                diagnostics: diagnostic_set,
1579                lamport_timestamp,
1580            } => {
1581                let snapshot = self.snapshot();
1582                self.apply_diagnostic_update(
1583                    server_id,
1584                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1585                    lamport_timestamp,
1586                    cx,
1587                );
1588            }
1589            Operation::UpdateSelections {
1590                selections,
1591                lamport_timestamp,
1592                line_mode,
1593                cursor_shape,
1594            } => {
1595                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1596                    if set.lamport_timestamp > lamport_timestamp {
1597                        return;
1598                    }
1599                }
1600
1601                self.remote_selections.insert(
1602                    lamport_timestamp.replica_id,
1603                    SelectionSet {
1604                        selections,
1605                        lamport_timestamp,
1606                        line_mode,
1607                        cursor_shape,
1608                    },
1609                );
1610                self.text.lamport_clock.observe(lamport_timestamp);
1611                self.selections_update_count += 1;
1612            }
1613            Operation::UpdateCompletionTriggers {
1614                triggers,
1615                lamport_timestamp,
1616            } => {
1617                self.completion_triggers = triggers;
1618                self.text.lamport_clock.observe(lamport_timestamp);
1619            }
1620        }
1621    }
1622
1623    fn apply_diagnostic_update(
1624        &mut self,
1625        server_id: LanguageServerId,
1626        diagnostics: DiagnosticSet,
1627        lamport_timestamp: clock::Lamport,
1628        cx: &mut ModelContext<Self>,
1629    ) {
1630        if lamport_timestamp > self.diagnostics_timestamp {
1631            let ix = self.diagnostics.binary_search_by_key(&server_id, |e| e.0);
1632            if diagnostics.len() == 0 {
1633                if let Ok(ix) = ix {
1634                    self.diagnostics.remove(ix);
1635                }
1636            } else {
1637                match ix {
1638                    Err(ix) => self.diagnostics.insert(ix, (server_id, diagnostics)),
1639                    Ok(ix) => self.diagnostics[ix].1 = diagnostics,
1640                };
1641            }
1642            self.diagnostics_timestamp = lamport_timestamp;
1643            self.diagnostics_update_count += 1;
1644            self.text.lamport_clock.observe(lamport_timestamp);
1645            cx.notify();
1646            cx.emit(Event::DiagnosticsUpdated);
1647        }
1648    }
1649
1650    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1651        cx.emit(Event::Operation(operation));
1652    }
1653
1654    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1655        self.remote_selections.remove(&replica_id);
1656        cx.notify();
1657    }
1658
1659    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1660        let was_dirty = self.is_dirty();
1661        let old_version = self.version.clone();
1662
1663        if let Some((transaction_id, operation)) = self.text.undo() {
1664            self.send_operation(Operation::Buffer(operation), cx);
1665            self.did_edit(&old_version, was_dirty, cx);
1666            Some(transaction_id)
1667        } else {
1668            None
1669        }
1670    }
1671
1672    pub fn undo_transaction(
1673        &mut self,
1674        transaction_id: TransactionId,
1675        cx: &mut ModelContext<Self>,
1676    ) -> bool {
1677        let was_dirty = self.is_dirty();
1678        let old_version = self.version.clone();
1679        if let Some(operation) = self.text.undo_transaction(transaction_id) {
1680            self.send_operation(Operation::Buffer(operation), cx);
1681            self.did_edit(&old_version, was_dirty, cx);
1682            true
1683        } else {
1684            false
1685        }
1686    }
1687
1688    pub fn undo_to_transaction(
1689        &mut self,
1690        transaction_id: TransactionId,
1691        cx: &mut ModelContext<Self>,
1692    ) -> bool {
1693        let was_dirty = self.is_dirty();
1694        let old_version = self.version.clone();
1695
1696        let operations = self.text.undo_to_transaction(transaction_id);
1697        let undone = !operations.is_empty();
1698        for operation in operations {
1699            self.send_operation(Operation::Buffer(operation), cx);
1700        }
1701        if undone {
1702            self.did_edit(&old_version, was_dirty, cx)
1703        }
1704        undone
1705    }
1706
1707    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1708        let was_dirty = self.is_dirty();
1709        let old_version = self.version.clone();
1710
1711        if let Some((transaction_id, operation)) = self.text.redo() {
1712            self.send_operation(Operation::Buffer(operation), cx);
1713            self.did_edit(&old_version, was_dirty, cx);
1714            Some(transaction_id)
1715        } else {
1716            None
1717        }
1718    }
1719
1720    pub fn redo_to_transaction(
1721        &mut self,
1722        transaction_id: TransactionId,
1723        cx: &mut ModelContext<Self>,
1724    ) -> bool {
1725        let was_dirty = self.is_dirty();
1726        let old_version = self.version.clone();
1727
1728        let operations = self.text.redo_to_transaction(transaction_id);
1729        let redone = !operations.is_empty();
1730        for operation in operations {
1731            self.send_operation(Operation::Buffer(operation), cx);
1732        }
1733        if redone {
1734            self.did_edit(&old_version, was_dirty, cx)
1735        }
1736        redone
1737    }
1738
1739    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1740        self.completion_triggers = triggers.clone();
1741        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1742        self.send_operation(
1743            Operation::UpdateCompletionTriggers {
1744                triggers,
1745                lamport_timestamp: self.completion_triggers_timestamp,
1746            },
1747            cx,
1748        );
1749        cx.notify();
1750    }
1751
1752    pub fn completion_triggers(&self) -> &[String] {
1753        &self.completion_triggers
1754    }
1755}
1756
1757#[cfg(any(test, feature = "test-support"))]
1758impl Buffer {
1759    pub fn edit_via_marked_text(
1760        &mut self,
1761        marked_string: &str,
1762        autoindent_mode: Option<AutoindentMode>,
1763        cx: &mut ModelContext<Self>,
1764    ) {
1765        let edits = self.edits_for_marked_text(marked_string);
1766        self.edit(edits, autoindent_mode, cx);
1767    }
1768
1769    pub fn set_group_interval(&mut self, group_interval: Duration) {
1770        self.text.set_group_interval(group_interval);
1771    }
1772
1773    pub fn randomly_edit<T>(
1774        &mut self,
1775        rng: &mut T,
1776        old_range_count: usize,
1777        cx: &mut ModelContext<Self>,
1778    ) where
1779        T: rand::Rng,
1780    {
1781        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1782        let mut last_end = None;
1783        for _ in 0..old_range_count {
1784            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1785                break;
1786            }
1787
1788            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1789            let mut range = self.random_byte_range(new_start, rng);
1790            if rng.gen_bool(0.2) {
1791                mem::swap(&mut range.start, &mut range.end);
1792            }
1793            last_end = Some(range.end);
1794
1795            let new_text_len = rng.gen_range(0..10);
1796            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1797
1798            edits.push((range, new_text));
1799        }
1800        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1801        self.edit(edits, None, cx);
1802    }
1803
1804    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1805        let was_dirty = self.is_dirty();
1806        let old_version = self.version.clone();
1807
1808        let ops = self.text.randomly_undo_redo(rng);
1809        if !ops.is_empty() {
1810            for op in ops {
1811                self.send_operation(Operation::Buffer(op), cx);
1812                self.did_edit(&old_version, was_dirty, cx);
1813            }
1814        }
1815    }
1816}
1817
1818impl EventEmitter<Event> for Buffer {}
1819
1820impl Deref for Buffer {
1821    type Target = TextBuffer;
1822
1823    fn deref(&self) -> &Self::Target {
1824        &self.text
1825    }
1826}
1827
1828impl BufferSnapshot {
1829    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1830        indent_size_for_line(self, row)
1831    }
1832
1833    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1834        let settings = language_settings(self.language_at(position), self.file(), cx);
1835        if settings.hard_tabs {
1836            IndentSize::tab()
1837        } else {
1838            IndentSize::spaces(settings.tab_size.get())
1839        }
1840    }
1841
1842    pub fn suggested_indents(
1843        &self,
1844        rows: impl Iterator<Item = u32>,
1845        single_indent_size: IndentSize,
1846    ) -> BTreeMap<u32, IndentSize> {
1847        let mut result = BTreeMap::new();
1848
1849        for row_range in contiguous_ranges(rows, 10) {
1850            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1851                Some(suggestions) => suggestions,
1852                _ => break,
1853            };
1854
1855            for (row, suggestion) in row_range.zip(suggestions) {
1856                let indent_size = if let Some(suggestion) = suggestion {
1857                    result
1858                        .get(&suggestion.basis_row)
1859                        .copied()
1860                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1861                        .with_delta(suggestion.delta, single_indent_size)
1862                } else {
1863                    self.indent_size_for_line(row)
1864                };
1865
1866                result.insert(row, indent_size);
1867            }
1868        }
1869
1870        result
1871    }
1872
1873    fn suggest_autoindents(
1874        &self,
1875        row_range: Range<u32>,
1876    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1877        let config = &self.language.as_ref()?.config;
1878        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1879
1880        // Find the suggested indentation ranges based on the syntax tree.
1881        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1882        let end = Point::new(row_range.end, 0);
1883        let range = (start..end).to_offset(&self.text);
1884        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1885            Some(&grammar.indents_config.as_ref()?.query)
1886        });
1887        let indent_configs = matches
1888            .grammars()
1889            .iter()
1890            .map(|grammar| grammar.indents_config.as_ref().unwrap())
1891            .collect::<Vec<_>>();
1892
1893        let mut indent_ranges = Vec::<Range<Point>>::new();
1894        let mut outdent_positions = Vec::<Point>::new();
1895        while let Some(mat) = matches.peek() {
1896            let mut start: Option<Point> = None;
1897            let mut end: Option<Point> = None;
1898
1899            let config = &indent_configs[mat.grammar_index];
1900            for capture in mat.captures {
1901                if capture.index == config.indent_capture_ix {
1902                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1903                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1904                } else if Some(capture.index) == config.start_capture_ix {
1905                    start = Some(Point::from_ts_point(capture.node.end_position()));
1906                } else if Some(capture.index) == config.end_capture_ix {
1907                    end = Some(Point::from_ts_point(capture.node.start_position()));
1908                } else if Some(capture.index) == config.outdent_capture_ix {
1909                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1910                }
1911            }
1912
1913            matches.advance();
1914            if let Some((start, end)) = start.zip(end) {
1915                if start.row == end.row {
1916                    continue;
1917                }
1918
1919                let range = start..end;
1920                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1921                    Err(ix) => indent_ranges.insert(ix, range),
1922                    Ok(ix) => {
1923                        let prev_range = &mut indent_ranges[ix];
1924                        prev_range.end = prev_range.end.max(range.end);
1925                    }
1926                }
1927            }
1928        }
1929
1930        let mut error_ranges = Vec::<Range<Point>>::new();
1931        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1932            Some(&grammar.error_query)
1933        });
1934        while let Some(mat) = matches.peek() {
1935            let node = mat.captures[0].node;
1936            let start = Point::from_ts_point(node.start_position());
1937            let end = Point::from_ts_point(node.end_position());
1938            let range = start..end;
1939            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
1940                Ok(ix) | Err(ix) => ix,
1941            };
1942            let mut end_ix = ix;
1943            while let Some(existing_range) = error_ranges.get(end_ix) {
1944                if existing_range.end < end {
1945                    end_ix += 1;
1946                } else {
1947                    break;
1948                }
1949            }
1950            error_ranges.splice(ix..end_ix, [range]);
1951            matches.advance();
1952        }
1953
1954        outdent_positions.sort();
1955        for outdent_position in outdent_positions {
1956            // find the innermost indent range containing this outdent_position
1957            // set its end to the outdent position
1958            if let Some(range_to_truncate) = indent_ranges
1959                .iter_mut()
1960                .filter(|indent_range| indent_range.contains(&outdent_position))
1961                .last()
1962            {
1963                range_to_truncate.end = outdent_position;
1964            }
1965        }
1966
1967        // Find the suggested indentation increases and decreased based on regexes.
1968        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1969        self.for_each_line(
1970            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1971                ..Point::new(row_range.end, 0),
1972            |row, line| {
1973                if config
1974                    .decrease_indent_pattern
1975                    .as_ref()
1976                    .map_or(false, |regex| regex.is_match(line))
1977                {
1978                    indent_change_rows.push((row, Ordering::Less));
1979                }
1980                if config
1981                    .increase_indent_pattern
1982                    .as_ref()
1983                    .map_or(false, |regex| regex.is_match(line))
1984                {
1985                    indent_change_rows.push((row + 1, Ordering::Greater));
1986                }
1987            },
1988        );
1989
1990        let mut indent_changes = indent_change_rows.into_iter().peekable();
1991        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1992            prev_non_blank_row.unwrap_or(0)
1993        } else {
1994            row_range.start.saturating_sub(1)
1995        };
1996        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1997        Some(row_range.map(move |row| {
1998            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1999
2000            let mut indent_from_prev_row = false;
2001            let mut outdent_from_prev_row = false;
2002            let mut outdent_to_row = u32::MAX;
2003
2004            while let Some((indent_row, delta)) = indent_changes.peek() {
2005                match indent_row.cmp(&row) {
2006                    Ordering::Equal => match delta {
2007                        Ordering::Less => outdent_from_prev_row = true,
2008                        Ordering::Greater => indent_from_prev_row = true,
2009                        _ => {}
2010                    },
2011
2012                    Ordering::Greater => break,
2013                    Ordering::Less => {}
2014                }
2015
2016                indent_changes.next();
2017            }
2018
2019            for range in &indent_ranges {
2020                if range.start.row >= row {
2021                    break;
2022                }
2023                if range.start.row == prev_row && range.end > row_start {
2024                    indent_from_prev_row = true;
2025                }
2026                if range.end > prev_row_start && range.end <= row_start {
2027                    outdent_to_row = outdent_to_row.min(range.start.row);
2028                }
2029            }
2030
2031            let within_error = error_ranges
2032                .iter()
2033                .any(|e| e.start.row < row && e.end > row_start);
2034
2035            let suggestion = if outdent_to_row == prev_row
2036                || (outdent_from_prev_row && indent_from_prev_row)
2037            {
2038                Some(IndentSuggestion {
2039                    basis_row: prev_row,
2040                    delta: Ordering::Equal,
2041                    within_error,
2042                })
2043            } else if indent_from_prev_row {
2044                Some(IndentSuggestion {
2045                    basis_row: prev_row,
2046                    delta: Ordering::Greater,
2047                    within_error,
2048                })
2049            } else if outdent_to_row < prev_row {
2050                Some(IndentSuggestion {
2051                    basis_row: outdent_to_row,
2052                    delta: Ordering::Equal,
2053                    within_error,
2054                })
2055            } else if outdent_from_prev_row {
2056                Some(IndentSuggestion {
2057                    basis_row: prev_row,
2058                    delta: Ordering::Less,
2059                    within_error,
2060                })
2061            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
2062            {
2063                Some(IndentSuggestion {
2064                    basis_row: prev_row,
2065                    delta: Ordering::Equal,
2066                    within_error,
2067                })
2068            } else {
2069                None
2070            };
2071
2072            prev_row = row;
2073            prev_row_start = row_start;
2074            suggestion
2075        }))
2076    }
2077
2078    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2079        while row > 0 {
2080            row -= 1;
2081            if !self.is_line_blank(row) {
2082                return Some(row);
2083            }
2084        }
2085        None
2086    }
2087
2088    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
2089        let range = range.start.to_offset(self)..range.end.to_offset(self);
2090
2091        let mut syntax = None;
2092        let mut diagnostic_endpoints = Vec::new();
2093        if language_aware {
2094            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
2095                grammar.highlights_query.as_ref()
2096            });
2097            let highlight_maps = captures
2098                .grammars()
2099                .into_iter()
2100                .map(|grammar| grammar.highlight_map())
2101                .collect();
2102            syntax = Some((captures, highlight_maps));
2103            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
2104                diagnostic_endpoints.push(DiagnosticEndpoint {
2105                    offset: entry.range.start,
2106                    is_start: true,
2107                    severity: entry.diagnostic.severity,
2108                    is_unnecessary: entry.diagnostic.is_unnecessary,
2109                });
2110                diagnostic_endpoints.push(DiagnosticEndpoint {
2111                    offset: entry.range.end,
2112                    is_start: false,
2113                    severity: entry.diagnostic.severity,
2114                    is_unnecessary: entry.diagnostic.is_unnecessary,
2115                });
2116            }
2117            diagnostic_endpoints
2118                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2119        }
2120
2121        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
2122    }
2123
2124    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2125        let mut line = String::new();
2126        let mut row = range.start.row;
2127        for chunk in self
2128            .as_rope()
2129            .chunks_in_range(range.to_offset(self))
2130            .chain(["\n"])
2131        {
2132            for (newline_ix, text) in chunk.split('\n').enumerate() {
2133                if newline_ix > 0 {
2134                    callback(row, &line);
2135                    row += 1;
2136                    line.clear();
2137                }
2138                line.push_str(text);
2139            }
2140        }
2141    }
2142
2143    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayerInfo> + '_ {
2144        self.syntax.layers_for_range(0..self.len(), &self.text)
2145    }
2146
2147    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayerInfo> {
2148        let offset = position.to_offset(self);
2149        self.syntax
2150            .layers_for_range(offset..offset, &self.text)
2151            .filter(|l| l.node().end_byte() > offset)
2152            .last()
2153    }
2154
2155    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2156        self.syntax_layer_at(position)
2157            .map(|info| info.language)
2158            .or(self.language.as_ref())
2159    }
2160
2161    pub fn settings_at<'a, D: ToOffset>(
2162        &self,
2163        position: D,
2164        cx: &'a AppContext,
2165    ) -> &'a LanguageSettings {
2166        language_settings(self.language_at(position), self.file.as_ref(), cx)
2167    }
2168
2169    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2170        let offset = position.to_offset(self);
2171        let mut scope = None;
2172        let mut smallest_range: Option<Range<usize>> = None;
2173
2174        // Use the layer that has the smallest node intersecting the given point.
2175        for layer in self.syntax.layers_for_range(offset..offset, &self.text) {
2176            let mut cursor = layer.node().walk();
2177
2178            let mut range = None;
2179            loop {
2180                let child_range = cursor.node().byte_range();
2181                if !child_range.to_inclusive().contains(&offset) {
2182                    break;
2183                }
2184
2185                range = Some(child_range);
2186                if cursor.goto_first_child_for_byte(offset).is_none() {
2187                    break;
2188                }
2189            }
2190
2191            if let Some(range) = range {
2192                if smallest_range
2193                    .as_ref()
2194                    .map_or(true, |smallest_range| range.len() < smallest_range.len())
2195                {
2196                    smallest_range = Some(range);
2197                    scope = Some(LanguageScope {
2198                        language: layer.language.clone(),
2199                        override_id: layer.override_id(offset, &self.text),
2200                    });
2201                }
2202            }
2203        }
2204
2205        scope.or_else(|| {
2206            self.language.clone().map(|language| LanguageScope {
2207                language,
2208                override_id: None,
2209            })
2210        })
2211    }
2212
2213    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2214        let mut start = start.to_offset(self);
2215        let mut end = start;
2216        let mut next_chars = self.chars_at(start).peekable();
2217        let mut prev_chars = self.reversed_chars_at(start).peekable();
2218
2219        let scope = self.language_scope_at(start);
2220        let kind = |c| char_kind(&scope, c);
2221        let word_kind = cmp::max(
2222            prev_chars.peek().copied().map(kind),
2223            next_chars.peek().copied().map(kind),
2224        );
2225
2226        for ch in prev_chars {
2227            if Some(kind(ch)) == word_kind && ch != '\n' {
2228                start -= ch.len_utf8();
2229            } else {
2230                break;
2231            }
2232        }
2233
2234        for ch in next_chars {
2235            if Some(kind(ch)) == word_kind && ch != '\n' {
2236                end += ch.len_utf8();
2237            } else {
2238                break;
2239            }
2240        }
2241
2242        (start..end, word_kind)
2243    }
2244
2245    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2246        let range = range.start.to_offset(self)..range.end.to_offset(self);
2247        let mut result: Option<Range<usize>> = None;
2248        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2249            let mut cursor = layer.node().walk();
2250
2251            // Descend to the first leaf that touches the start of the range,
2252            // and if the range is non-empty, extends beyond the start.
2253            while cursor.goto_first_child_for_byte(range.start).is_some() {
2254                if !range.is_empty() && cursor.node().end_byte() == range.start {
2255                    cursor.goto_next_sibling();
2256                }
2257            }
2258
2259            // Ascend to the smallest ancestor that strictly contains the range.
2260            loop {
2261                let node_range = cursor.node().byte_range();
2262                if node_range.start <= range.start
2263                    && node_range.end >= range.end
2264                    && node_range.len() > range.len()
2265                {
2266                    break;
2267                }
2268                if !cursor.goto_parent() {
2269                    continue 'outer;
2270                }
2271            }
2272
2273            let left_node = cursor.node();
2274            let mut layer_result = left_node.byte_range();
2275
2276            // For an empty range, try to find another node immediately to the right of the range.
2277            if left_node.end_byte() == range.start {
2278                let mut right_node = None;
2279                while !cursor.goto_next_sibling() {
2280                    if !cursor.goto_parent() {
2281                        break;
2282                    }
2283                }
2284
2285                while cursor.node().start_byte() == range.start {
2286                    right_node = Some(cursor.node());
2287                    if !cursor.goto_first_child() {
2288                        break;
2289                    }
2290                }
2291
2292                // If there is a candidate node on both sides of the (empty) range, then
2293                // decide between the two by favoring a named node over an anonymous token.
2294                // If both nodes are the same in that regard, favor the right one.
2295                if let Some(right_node) = right_node {
2296                    if right_node.is_named() || !left_node.is_named() {
2297                        layer_result = right_node.byte_range();
2298                    }
2299                }
2300            }
2301
2302            if let Some(previous_result) = &result {
2303                if previous_result.len() < layer_result.len() {
2304                    continue;
2305                }
2306            }
2307            result = Some(layer_result);
2308        }
2309
2310        result
2311    }
2312
2313    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2314        self.outline_items_containing(0..self.len(), true, theme)
2315            .map(Outline::new)
2316    }
2317
2318    pub fn symbols_containing<T: ToOffset>(
2319        &self,
2320        position: T,
2321        theme: Option<&SyntaxTheme>,
2322    ) -> Option<Vec<OutlineItem<Anchor>>> {
2323        let position = position.to_offset(self);
2324        let mut items = self.outline_items_containing(
2325            position.saturating_sub(1)..self.len().min(position + 1),
2326            false,
2327            theme,
2328        )?;
2329        let mut prev_depth = None;
2330        items.retain(|item| {
2331            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2332            prev_depth = Some(item.depth);
2333            result
2334        });
2335        Some(items)
2336    }
2337
2338    fn outline_items_containing(
2339        &self,
2340        range: Range<usize>,
2341        include_extra_context: bool,
2342        theme: Option<&SyntaxTheme>,
2343    ) -> Option<Vec<OutlineItem<Anchor>>> {
2344        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2345            grammar.outline_config.as_ref().map(|c| &c.query)
2346        });
2347        let configs = matches
2348            .grammars()
2349            .iter()
2350            .map(|g| g.outline_config.as_ref().unwrap())
2351            .collect::<Vec<_>>();
2352
2353        let mut stack = Vec::<Range<usize>>::new();
2354        let mut items = Vec::new();
2355        while let Some(mat) = matches.peek() {
2356            let config = &configs[mat.grammar_index];
2357            let item_node = mat.captures.iter().find_map(|cap| {
2358                if cap.index == config.item_capture_ix {
2359                    Some(cap.node)
2360                } else {
2361                    None
2362                }
2363            })?;
2364
2365            let item_range = item_node.byte_range();
2366            if item_range.end < range.start || item_range.start > range.end {
2367                matches.advance();
2368                continue;
2369            }
2370
2371            let mut buffer_ranges = Vec::new();
2372            for capture in mat.captures {
2373                let node_is_name;
2374                if capture.index == config.name_capture_ix {
2375                    node_is_name = true;
2376                } else if Some(capture.index) == config.context_capture_ix
2377                    || (Some(capture.index) == config.extra_context_capture_ix
2378                        && include_extra_context)
2379                {
2380                    node_is_name = false;
2381                } else {
2382                    continue;
2383                }
2384
2385                let mut range = capture.node.start_byte()..capture.node.end_byte();
2386                let start = capture.node.start_position();
2387                if capture.node.end_position().row > start.row {
2388                    range.end =
2389                        range.start + self.line_len(start.row as u32) as usize - start.column;
2390                }
2391
2392                buffer_ranges.push((range, node_is_name));
2393            }
2394
2395            if buffer_ranges.is_empty() {
2396                continue;
2397            }
2398
2399            let mut text = String::new();
2400            let mut highlight_ranges = Vec::new();
2401            let mut name_ranges = Vec::new();
2402            let mut chunks = self.chunks(
2403                buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end,
2404                true,
2405            );
2406            let mut last_buffer_range_end = 0;
2407            for (buffer_range, is_name) in buffer_ranges {
2408                if !text.is_empty() && buffer_range.start > last_buffer_range_end {
2409                    text.push(' ');
2410                }
2411                last_buffer_range_end = buffer_range.end;
2412                if is_name {
2413                    let mut start = text.len();
2414                    let end = start + buffer_range.len();
2415
2416                    // When multiple names are captured, then the matcheable text
2417                    // includes the whitespace in between the names.
2418                    if !name_ranges.is_empty() {
2419                        start -= 1;
2420                    }
2421
2422                    name_ranges.push(start..end);
2423                }
2424
2425                let mut offset = buffer_range.start;
2426                chunks.seek(offset);
2427                for mut chunk in chunks.by_ref() {
2428                    if chunk.text.len() > buffer_range.end - offset {
2429                        chunk.text = &chunk.text[0..(buffer_range.end - offset)];
2430                        offset = buffer_range.end;
2431                    } else {
2432                        offset += chunk.text.len();
2433                    }
2434                    let style = chunk
2435                        .syntax_highlight_id
2436                        .zip(theme)
2437                        .and_then(|(highlight, theme)| highlight.style(theme));
2438                    if let Some(style) = style {
2439                        let start = text.len();
2440                        let end = start + chunk.text.len();
2441                        highlight_ranges.push((start..end, style));
2442                    }
2443                    text.push_str(chunk.text);
2444                    if offset >= buffer_range.end {
2445                        break;
2446                    }
2447                }
2448            }
2449
2450            matches.advance();
2451            while stack.last().map_or(false, |prev_range| {
2452                prev_range.start > item_range.start || prev_range.end < item_range.end
2453            }) {
2454                stack.pop();
2455            }
2456            stack.push(item_range.clone());
2457
2458            items.push(OutlineItem {
2459                depth: stack.len() - 1,
2460                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2461                text,
2462                highlight_ranges,
2463                name_ranges,
2464            })
2465        }
2466        Some(items)
2467    }
2468
2469    pub fn matches(
2470        &self,
2471        range: Range<usize>,
2472        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
2473    ) -> SyntaxMapMatches {
2474        self.syntax.matches(range, self, query)
2475    }
2476
2477    /// Returns bracket range pairs overlapping or adjacent to `range`
2478    pub fn bracket_ranges<'a, T: ToOffset>(
2479        &'a self,
2480        range: Range<T>,
2481    ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + 'a {
2482        // Find bracket pairs that *inclusively* contain the given range.
2483        let range = range.start.to_offset(self).saturating_sub(1)
2484            ..self.len().min(range.end.to_offset(self) + 1);
2485
2486        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2487            grammar.brackets_config.as_ref().map(|c| &c.query)
2488        });
2489        let configs = matches
2490            .grammars()
2491            .iter()
2492            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2493            .collect::<Vec<_>>();
2494
2495        iter::from_fn(move || {
2496            while let Some(mat) = matches.peek() {
2497                let mut open = None;
2498                let mut close = None;
2499                let config = &configs[mat.grammar_index];
2500                for capture in mat.captures {
2501                    if capture.index == config.open_capture_ix {
2502                        open = Some(capture.node.byte_range());
2503                    } else if capture.index == config.close_capture_ix {
2504                        close = Some(capture.node.byte_range());
2505                    }
2506                }
2507
2508                matches.advance();
2509
2510                let Some((open, close)) = open.zip(close) else {
2511                    continue;
2512                };
2513
2514                let bracket_range = open.start..=close.end;
2515                if !bracket_range.overlaps(&range) {
2516                    continue;
2517                }
2518
2519                return Some((open, close));
2520            }
2521            None
2522        })
2523    }
2524
2525    #[allow(clippy::type_complexity)]
2526    pub fn remote_selections_in_range(
2527        &self,
2528        range: Range<Anchor>,
2529    ) -> impl Iterator<
2530        Item = (
2531            ReplicaId,
2532            bool,
2533            CursorShape,
2534            impl Iterator<Item = &Selection<Anchor>> + '_,
2535        ),
2536    > + '_ {
2537        self.remote_selections
2538            .iter()
2539            .filter(|(replica_id, set)| {
2540                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2541            })
2542            .map(move |(replica_id, set)| {
2543                let start_ix = match set.selections.binary_search_by(|probe| {
2544                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2545                }) {
2546                    Ok(ix) | Err(ix) => ix,
2547                };
2548                let end_ix = match set.selections.binary_search_by(|probe| {
2549                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2550                }) {
2551                    Ok(ix) | Err(ix) => ix,
2552                };
2553
2554                (
2555                    *replica_id,
2556                    set.line_mode,
2557                    set.cursor_shape,
2558                    set.selections[start_ix..end_ix].iter(),
2559                )
2560            })
2561    }
2562
2563    pub fn git_diff_hunks_in_row_range<'a>(
2564        &'a self,
2565        range: Range<u32>,
2566    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2567        self.git_diff.hunks_in_row_range(range, self)
2568    }
2569
2570    pub fn git_diff_hunks_intersecting_range<'a>(
2571        &'a self,
2572        range: Range<Anchor>,
2573    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2574        self.git_diff.hunks_intersecting_range(range, self)
2575    }
2576
2577    pub fn git_diff_hunks_intersecting_range_rev<'a>(
2578        &'a self,
2579        range: Range<Anchor>,
2580    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2581        self.git_diff.hunks_intersecting_range_rev(range, self)
2582    }
2583
2584    pub fn diagnostics_in_range<'a, T, O>(
2585        &'a self,
2586        search_range: Range<T>,
2587        reversed: bool,
2588    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2589    where
2590        T: 'a + Clone + ToOffset,
2591        O: 'a + FromAnchor + Ord,
2592    {
2593        let mut iterators: Vec<_> = self
2594            .diagnostics
2595            .iter()
2596            .map(|(_, collection)| {
2597                collection
2598                    .range::<T, O>(search_range.clone(), self, true, reversed)
2599                    .peekable()
2600            })
2601            .collect();
2602
2603        std::iter::from_fn(move || {
2604            let (next_ix, _) = iterators
2605                .iter_mut()
2606                .enumerate()
2607                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
2608                .min_by(|(_, a), (_, b)| a.range.start.cmp(&b.range.start))?;
2609            iterators[next_ix].next()
2610        })
2611    }
2612
2613    pub fn diagnostic_groups(
2614        &self,
2615        language_server_id: Option<LanguageServerId>,
2616    ) -> Vec<(LanguageServerId, DiagnosticGroup<Anchor>)> {
2617        let mut groups = Vec::new();
2618
2619        if let Some(language_server_id) = language_server_id {
2620            if let Ok(ix) = self
2621                .diagnostics
2622                .binary_search_by_key(&language_server_id, |e| e.0)
2623            {
2624                self.diagnostics[ix]
2625                    .1
2626                    .groups(language_server_id, &mut groups, self);
2627            }
2628        } else {
2629            for (language_server_id, diagnostics) in self.diagnostics.iter() {
2630                diagnostics.groups(*language_server_id, &mut groups, self);
2631            }
2632        }
2633
2634        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
2635            let a_start = &group_a.entries[group_a.primary_ix].range.start;
2636            let b_start = &group_b.entries[group_b.primary_ix].range.start;
2637            a_start.cmp(b_start, self).then_with(|| id_a.cmp(&id_b))
2638        });
2639
2640        groups
2641    }
2642
2643    pub fn diagnostic_group<'a, O>(
2644        &'a self,
2645        group_id: usize,
2646    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2647    where
2648        O: 'a + FromAnchor,
2649    {
2650        self.diagnostics
2651            .iter()
2652            .flat_map(move |(_, set)| set.group(group_id, self))
2653    }
2654
2655    pub fn diagnostics_update_count(&self) -> usize {
2656        self.diagnostics_update_count
2657    }
2658
2659    pub fn parse_count(&self) -> usize {
2660        self.parse_count
2661    }
2662
2663    pub fn selections_update_count(&self) -> usize {
2664        self.selections_update_count
2665    }
2666
2667    pub fn file(&self) -> Option<&Arc<dyn File>> {
2668        self.file.as_ref()
2669    }
2670
2671    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2672        if let Some(file) = self.file() {
2673            if file.path().file_name().is_none() || include_root {
2674                Some(file.full_path(cx))
2675            } else {
2676                Some(file.path().to_path_buf())
2677            }
2678        } else {
2679            None
2680        }
2681    }
2682
2683    pub fn file_update_count(&self) -> usize {
2684        self.file_update_count
2685    }
2686
2687    pub fn git_diff_update_count(&self) -> usize {
2688        self.git_diff_update_count
2689    }
2690}
2691
2692fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2693    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2694}
2695
2696pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2697    let mut result = IndentSize::spaces(0);
2698    for c in text {
2699        let kind = match c {
2700            ' ' => IndentKind::Space,
2701            '\t' => IndentKind::Tab,
2702            _ => break,
2703        };
2704        if result.len == 0 {
2705            result.kind = kind;
2706        }
2707        result.len += 1;
2708    }
2709    result
2710}
2711
2712impl Clone for BufferSnapshot {
2713    fn clone(&self) -> Self {
2714        Self {
2715            text: self.text.clone(),
2716            git_diff: self.git_diff.clone(),
2717            syntax: self.syntax.clone(),
2718            file: self.file.clone(),
2719            remote_selections: self.remote_selections.clone(),
2720            diagnostics: self.diagnostics.clone(),
2721            selections_update_count: self.selections_update_count,
2722            diagnostics_update_count: self.diagnostics_update_count,
2723            file_update_count: self.file_update_count,
2724            git_diff_update_count: self.git_diff_update_count,
2725            language: self.language.clone(),
2726            parse_count: self.parse_count,
2727        }
2728    }
2729}
2730
2731impl Deref for BufferSnapshot {
2732    type Target = text::BufferSnapshot;
2733
2734    fn deref(&self) -> &Self::Target {
2735        &self.text
2736    }
2737}
2738
2739unsafe impl<'a> Send for BufferChunks<'a> {}
2740
2741impl<'a> BufferChunks<'a> {
2742    pub(crate) fn new(
2743        text: &'a Rope,
2744        range: Range<usize>,
2745        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2746        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2747    ) -> Self {
2748        let mut highlights = None;
2749        if let Some((captures, highlight_maps)) = syntax {
2750            highlights = Some(BufferChunkHighlights {
2751                captures,
2752                next_capture: None,
2753                stack: Default::default(),
2754                highlight_maps,
2755            })
2756        }
2757
2758        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2759        let chunks = text.chunks_in_range(range.clone());
2760
2761        BufferChunks {
2762            range,
2763            chunks,
2764            diagnostic_endpoints,
2765            error_depth: 0,
2766            warning_depth: 0,
2767            information_depth: 0,
2768            hint_depth: 0,
2769            unnecessary_depth: 0,
2770            highlights,
2771        }
2772    }
2773
2774    pub fn seek(&mut self, offset: usize) {
2775        self.range.start = offset;
2776        self.chunks.seek(self.range.start);
2777        if let Some(highlights) = self.highlights.as_mut() {
2778            highlights
2779                .stack
2780                .retain(|(end_offset, _)| *end_offset > offset);
2781            if let Some(capture) = &highlights.next_capture {
2782                if offset >= capture.node.start_byte() {
2783                    let next_capture_end = capture.node.end_byte();
2784                    if offset < next_capture_end {
2785                        highlights.stack.push((
2786                            next_capture_end,
2787                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2788                        ));
2789                    }
2790                    highlights.next_capture.take();
2791                }
2792            }
2793            highlights.captures.set_byte_range(self.range.clone());
2794        }
2795    }
2796
2797    pub fn offset(&self) -> usize {
2798        self.range.start
2799    }
2800
2801    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2802        let depth = match endpoint.severity {
2803            DiagnosticSeverity::ERROR => &mut self.error_depth,
2804            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2805            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2806            DiagnosticSeverity::HINT => &mut self.hint_depth,
2807            _ => return,
2808        };
2809        if endpoint.is_start {
2810            *depth += 1;
2811        } else {
2812            *depth -= 1;
2813        }
2814
2815        if endpoint.is_unnecessary {
2816            if endpoint.is_start {
2817                self.unnecessary_depth += 1;
2818            } else {
2819                self.unnecessary_depth -= 1;
2820            }
2821        }
2822    }
2823
2824    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2825        if self.error_depth > 0 {
2826            Some(DiagnosticSeverity::ERROR)
2827        } else if self.warning_depth > 0 {
2828            Some(DiagnosticSeverity::WARNING)
2829        } else if self.information_depth > 0 {
2830            Some(DiagnosticSeverity::INFORMATION)
2831        } else if self.hint_depth > 0 {
2832            Some(DiagnosticSeverity::HINT)
2833        } else {
2834            None
2835        }
2836    }
2837
2838    fn current_code_is_unnecessary(&self) -> bool {
2839        self.unnecessary_depth > 0
2840    }
2841}
2842
2843impl<'a> Iterator for BufferChunks<'a> {
2844    type Item = Chunk<'a>;
2845
2846    fn next(&mut self) -> Option<Self::Item> {
2847        let mut next_capture_start = usize::MAX;
2848        let mut next_diagnostic_endpoint = usize::MAX;
2849
2850        if let Some(highlights) = self.highlights.as_mut() {
2851            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2852                if *parent_capture_end <= self.range.start {
2853                    highlights.stack.pop();
2854                } else {
2855                    break;
2856                }
2857            }
2858
2859            if highlights.next_capture.is_none() {
2860                highlights.next_capture = highlights.captures.next();
2861            }
2862
2863            while let Some(capture) = highlights.next_capture.as_ref() {
2864                if self.range.start < capture.node.start_byte() {
2865                    next_capture_start = capture.node.start_byte();
2866                    break;
2867                } else {
2868                    let highlight_id =
2869                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2870                    highlights
2871                        .stack
2872                        .push((capture.node.end_byte(), highlight_id));
2873                    highlights.next_capture = highlights.captures.next();
2874                }
2875            }
2876        }
2877
2878        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2879            if endpoint.offset <= self.range.start {
2880                self.update_diagnostic_depths(endpoint);
2881                self.diagnostic_endpoints.next();
2882            } else {
2883                next_diagnostic_endpoint = endpoint.offset;
2884                break;
2885            }
2886        }
2887
2888        if let Some(chunk) = self.chunks.peek() {
2889            let chunk_start = self.range.start;
2890            let mut chunk_end = (self.chunks.offset() + chunk.len())
2891                .min(next_capture_start)
2892                .min(next_diagnostic_endpoint);
2893            let mut highlight_id = None;
2894            if let Some(highlights) = self.highlights.as_ref() {
2895                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2896                    chunk_end = chunk_end.min(*parent_capture_end);
2897                    highlight_id = Some(*parent_highlight_id);
2898                }
2899            }
2900
2901            let slice =
2902                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2903            self.range.start = chunk_end;
2904            if self.range.start == self.chunks.offset() + chunk.len() {
2905                self.chunks.next().unwrap();
2906            }
2907
2908            Some(Chunk {
2909                text: slice,
2910                syntax_highlight_id: highlight_id,
2911                diagnostic_severity: self.current_diagnostic_severity(),
2912                is_unnecessary: self.current_code_is_unnecessary(),
2913                ..Default::default()
2914            })
2915        } else {
2916            None
2917        }
2918    }
2919}
2920
2921impl operation_queue::Operation for Operation {
2922    fn lamport_timestamp(&self) -> clock::Lamport {
2923        match self {
2924            Operation::Buffer(_) => {
2925                unreachable!("buffer operations should never be deferred at this layer")
2926            }
2927            Operation::UpdateDiagnostics {
2928                lamport_timestamp, ..
2929            }
2930            | Operation::UpdateSelections {
2931                lamport_timestamp, ..
2932            }
2933            | Operation::UpdateCompletionTriggers {
2934                lamport_timestamp, ..
2935            } => *lamport_timestamp,
2936        }
2937    }
2938}
2939
2940impl Default for Diagnostic {
2941    fn default() -> Self {
2942        Self {
2943            source: Default::default(),
2944            code: None,
2945            severity: DiagnosticSeverity::ERROR,
2946            message: Default::default(),
2947            group_id: 0,
2948            is_primary: false,
2949            is_valid: true,
2950            is_disk_based: false,
2951            is_unnecessary: false,
2952        }
2953    }
2954}
2955
2956impl IndentSize {
2957    pub fn spaces(len: u32) -> Self {
2958        Self {
2959            len,
2960            kind: IndentKind::Space,
2961        }
2962    }
2963
2964    pub fn tab() -> Self {
2965        Self {
2966            len: 1,
2967            kind: IndentKind::Tab,
2968        }
2969    }
2970
2971    pub fn chars(&self) -> impl Iterator<Item = char> {
2972        iter::repeat(self.char()).take(self.len as usize)
2973    }
2974
2975    pub fn char(&self) -> char {
2976        match self.kind {
2977            IndentKind::Space => ' ',
2978            IndentKind::Tab => '\t',
2979        }
2980    }
2981
2982    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2983        match direction {
2984            Ordering::Less => {
2985                if self.kind == size.kind && self.len >= size.len {
2986                    self.len -= size.len;
2987                }
2988            }
2989            Ordering::Equal => {}
2990            Ordering::Greater => {
2991                if self.len == 0 {
2992                    self = size;
2993                } else if self.kind == size.kind {
2994                    self.len += size.len;
2995                }
2996            }
2997        }
2998        self
2999    }
3000}
3001
3002impl Completion {
3003    pub fn sort_key(&self) -> (usize, &str) {
3004        let kind_key = match self.lsp_completion.kind {
3005            Some(lsp::CompletionItemKind::VARIABLE) => 0,
3006            _ => 1,
3007        };
3008        (kind_key, &self.label.text[self.label.filter_range.clone()])
3009    }
3010
3011    pub fn is_snippet(&self) -> bool {
3012        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
3013    }
3014}
3015
3016pub fn contiguous_ranges(
3017    values: impl Iterator<Item = u32>,
3018    max_len: usize,
3019) -> impl Iterator<Item = Range<u32>> {
3020    let mut values = values;
3021    let mut current_range: Option<Range<u32>> = None;
3022    std::iter::from_fn(move || loop {
3023        if let Some(value) = values.next() {
3024            if let Some(range) = &mut current_range {
3025                if value == range.end && range.len() < max_len {
3026                    range.end += 1;
3027                    continue;
3028                }
3029            }
3030
3031            let prev_range = current_range.clone();
3032            current_range = Some(value..(value + 1));
3033            if prev_range.is_some() {
3034                return prev_range;
3035            }
3036        } else {
3037            return current_range.take();
3038        }
3039    })
3040}
3041
3042pub fn char_kind(scope: &Option<LanguageScope>, c: char) -> CharKind {
3043    if c.is_whitespace() {
3044        return CharKind::Whitespace;
3045    } else if c.is_alphanumeric() || c == '_' {
3046        return CharKind::Word;
3047    }
3048
3049    if let Some(scope) = scope {
3050        if let Some(characters) = scope.word_characters() {
3051            if characters.contains(&c) {
3052                return CharKind::Word;
3053            }
3054        }
3055    }
3056
3057    CharKind::Punctuation
3058}
3059
3060/// Find all of the ranges of whitespace that occur at the ends of lines
3061/// in the given rope.
3062///
3063/// This could also be done with a regex search, but this implementation
3064/// avoids copying text.
3065pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
3066    let mut ranges = Vec::new();
3067
3068    let mut offset = 0;
3069    let mut prev_chunk_trailing_whitespace_range = 0..0;
3070    for chunk in rope.chunks() {
3071        let mut prev_line_trailing_whitespace_range = 0..0;
3072        for (i, line) in chunk.split('\n').enumerate() {
3073            let line_end_offset = offset + line.len();
3074            let trimmed_line_len = line.trim_end_matches(|c| matches!(c, ' ' | '\t')).len();
3075            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
3076
3077            if i == 0 && trimmed_line_len == 0 {
3078                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
3079            }
3080            if !prev_line_trailing_whitespace_range.is_empty() {
3081                ranges.push(prev_line_trailing_whitespace_range);
3082            }
3083
3084            offset = line_end_offset + 1;
3085            prev_line_trailing_whitespace_range = trailing_whitespace_range;
3086        }
3087
3088        offset -= 1;
3089        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
3090    }
3091
3092    if !prev_chunk_trailing_whitespace_range.is_empty() {
3093        ranges.push(prev_chunk_trailing_whitespace_range);
3094    }
3095
3096    ranges
3097}