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 for Buffer {
1819    type Event = Event;
1820}
1821
1822impl Deref for Buffer {
1823    type Target = TextBuffer;
1824
1825    fn deref(&self) -> &Self::Target {
1826        &self.text
1827    }
1828}
1829
1830impl BufferSnapshot {
1831    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1832        indent_size_for_line(self, row)
1833    }
1834
1835    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1836        let settings = language_settings(self.language_at(position), self.file(), cx);
1837        if settings.hard_tabs {
1838            IndentSize::tab()
1839        } else {
1840            IndentSize::spaces(settings.tab_size.get())
1841        }
1842    }
1843
1844    pub fn suggested_indents(
1845        &self,
1846        rows: impl Iterator<Item = u32>,
1847        single_indent_size: IndentSize,
1848    ) -> BTreeMap<u32, IndentSize> {
1849        let mut result = BTreeMap::new();
1850
1851        for row_range in contiguous_ranges(rows, 10) {
1852            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1853                Some(suggestions) => suggestions,
1854                _ => break,
1855            };
1856
1857            for (row, suggestion) in row_range.zip(suggestions) {
1858                let indent_size = if let Some(suggestion) = suggestion {
1859                    result
1860                        .get(&suggestion.basis_row)
1861                        .copied()
1862                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1863                        .with_delta(suggestion.delta, single_indent_size)
1864                } else {
1865                    self.indent_size_for_line(row)
1866                };
1867
1868                result.insert(row, indent_size);
1869            }
1870        }
1871
1872        result
1873    }
1874
1875    fn suggest_autoindents(
1876        &self,
1877        row_range: Range<u32>,
1878    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1879        let config = &self.language.as_ref()?.config;
1880        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1881
1882        // Find the suggested indentation ranges based on the syntax tree.
1883        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1884        let end = Point::new(row_range.end, 0);
1885        let range = (start..end).to_offset(&self.text);
1886        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1887            Some(&grammar.indents_config.as_ref()?.query)
1888        });
1889        let indent_configs = matches
1890            .grammars()
1891            .iter()
1892            .map(|grammar| grammar.indents_config.as_ref().unwrap())
1893            .collect::<Vec<_>>();
1894
1895        let mut indent_ranges = Vec::<Range<Point>>::new();
1896        let mut outdent_positions = Vec::<Point>::new();
1897        while let Some(mat) = matches.peek() {
1898            let mut start: Option<Point> = None;
1899            let mut end: Option<Point> = None;
1900
1901            let config = &indent_configs[mat.grammar_index];
1902            for capture in mat.captures {
1903                if capture.index == config.indent_capture_ix {
1904                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1905                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1906                } else if Some(capture.index) == config.start_capture_ix {
1907                    start = Some(Point::from_ts_point(capture.node.end_position()));
1908                } else if Some(capture.index) == config.end_capture_ix {
1909                    end = Some(Point::from_ts_point(capture.node.start_position()));
1910                } else if Some(capture.index) == config.outdent_capture_ix {
1911                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1912                }
1913            }
1914
1915            matches.advance();
1916            if let Some((start, end)) = start.zip(end) {
1917                if start.row == end.row {
1918                    continue;
1919                }
1920
1921                let range = start..end;
1922                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1923                    Err(ix) => indent_ranges.insert(ix, range),
1924                    Ok(ix) => {
1925                        let prev_range = &mut indent_ranges[ix];
1926                        prev_range.end = prev_range.end.max(range.end);
1927                    }
1928                }
1929            }
1930        }
1931
1932        let mut error_ranges = Vec::<Range<Point>>::new();
1933        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1934            Some(&grammar.error_query)
1935        });
1936        while let Some(mat) = matches.peek() {
1937            let node = mat.captures[0].node;
1938            let start = Point::from_ts_point(node.start_position());
1939            let end = Point::from_ts_point(node.end_position());
1940            let range = start..end;
1941            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
1942                Ok(ix) | Err(ix) => ix,
1943            };
1944            let mut end_ix = ix;
1945            while let Some(existing_range) = error_ranges.get(end_ix) {
1946                if existing_range.end < end {
1947                    end_ix += 1;
1948                } else {
1949                    break;
1950                }
1951            }
1952            error_ranges.splice(ix..end_ix, [range]);
1953            matches.advance();
1954        }
1955
1956        outdent_positions.sort();
1957        for outdent_position in outdent_positions {
1958            // find the innermost indent range containing this outdent_position
1959            // set its end to the outdent position
1960            if let Some(range_to_truncate) = indent_ranges
1961                .iter_mut()
1962                .filter(|indent_range| indent_range.contains(&outdent_position))
1963                .last()
1964            {
1965                range_to_truncate.end = outdent_position;
1966            }
1967        }
1968
1969        // Find the suggested indentation increases and decreased based on regexes.
1970        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1971        self.for_each_line(
1972            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1973                ..Point::new(row_range.end, 0),
1974            |row, line| {
1975                if config
1976                    .decrease_indent_pattern
1977                    .as_ref()
1978                    .map_or(false, |regex| regex.is_match(line))
1979                {
1980                    indent_change_rows.push((row, Ordering::Less));
1981                }
1982                if config
1983                    .increase_indent_pattern
1984                    .as_ref()
1985                    .map_or(false, |regex| regex.is_match(line))
1986                {
1987                    indent_change_rows.push((row + 1, Ordering::Greater));
1988                }
1989            },
1990        );
1991
1992        let mut indent_changes = indent_change_rows.into_iter().peekable();
1993        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1994            prev_non_blank_row.unwrap_or(0)
1995        } else {
1996            row_range.start.saturating_sub(1)
1997        };
1998        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1999        Some(row_range.map(move |row| {
2000            let row_start = Point::new(row, self.indent_size_for_line(row).len);
2001
2002            let mut indent_from_prev_row = false;
2003            let mut outdent_from_prev_row = false;
2004            let mut outdent_to_row = u32::MAX;
2005
2006            while let Some((indent_row, delta)) = indent_changes.peek() {
2007                match indent_row.cmp(&row) {
2008                    Ordering::Equal => match delta {
2009                        Ordering::Less => outdent_from_prev_row = true,
2010                        Ordering::Greater => indent_from_prev_row = true,
2011                        _ => {}
2012                    },
2013
2014                    Ordering::Greater => break,
2015                    Ordering::Less => {}
2016                }
2017
2018                indent_changes.next();
2019            }
2020
2021            for range in &indent_ranges {
2022                if range.start.row >= row {
2023                    break;
2024                }
2025                if range.start.row == prev_row && range.end > row_start {
2026                    indent_from_prev_row = true;
2027                }
2028                if range.end > prev_row_start && range.end <= row_start {
2029                    outdent_to_row = outdent_to_row.min(range.start.row);
2030                }
2031            }
2032
2033            let within_error = error_ranges
2034                .iter()
2035                .any(|e| e.start.row < row && e.end > row_start);
2036
2037            let suggestion = if outdent_to_row == prev_row
2038                || (outdent_from_prev_row && indent_from_prev_row)
2039            {
2040                Some(IndentSuggestion {
2041                    basis_row: prev_row,
2042                    delta: Ordering::Equal,
2043                    within_error,
2044                })
2045            } else if indent_from_prev_row {
2046                Some(IndentSuggestion {
2047                    basis_row: prev_row,
2048                    delta: Ordering::Greater,
2049                    within_error,
2050                })
2051            } else if outdent_to_row < prev_row {
2052                Some(IndentSuggestion {
2053                    basis_row: outdent_to_row,
2054                    delta: Ordering::Equal,
2055                    within_error,
2056                })
2057            } else if outdent_from_prev_row {
2058                Some(IndentSuggestion {
2059                    basis_row: prev_row,
2060                    delta: Ordering::Less,
2061                    within_error,
2062                })
2063            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
2064            {
2065                Some(IndentSuggestion {
2066                    basis_row: prev_row,
2067                    delta: Ordering::Equal,
2068                    within_error,
2069                })
2070            } else {
2071                None
2072            };
2073
2074            prev_row = row;
2075            prev_row_start = row_start;
2076            suggestion
2077        }))
2078    }
2079
2080    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2081        while row > 0 {
2082            row -= 1;
2083            if !self.is_line_blank(row) {
2084                return Some(row);
2085            }
2086        }
2087        None
2088    }
2089
2090    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
2091        let range = range.start.to_offset(self)..range.end.to_offset(self);
2092
2093        let mut syntax = None;
2094        let mut diagnostic_endpoints = Vec::new();
2095        if language_aware {
2096            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
2097                grammar.highlights_query.as_ref()
2098            });
2099            let highlight_maps = captures
2100                .grammars()
2101                .into_iter()
2102                .map(|grammar| grammar.highlight_map())
2103                .collect();
2104            syntax = Some((captures, highlight_maps));
2105            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
2106                diagnostic_endpoints.push(DiagnosticEndpoint {
2107                    offset: entry.range.start,
2108                    is_start: true,
2109                    severity: entry.diagnostic.severity,
2110                    is_unnecessary: entry.diagnostic.is_unnecessary,
2111                });
2112                diagnostic_endpoints.push(DiagnosticEndpoint {
2113                    offset: entry.range.end,
2114                    is_start: false,
2115                    severity: entry.diagnostic.severity,
2116                    is_unnecessary: entry.diagnostic.is_unnecessary,
2117                });
2118            }
2119            diagnostic_endpoints
2120                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2121        }
2122
2123        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
2124    }
2125
2126    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2127        let mut line = String::new();
2128        let mut row = range.start.row;
2129        for chunk in self
2130            .as_rope()
2131            .chunks_in_range(range.to_offset(self))
2132            .chain(["\n"])
2133        {
2134            for (newline_ix, text) in chunk.split('\n').enumerate() {
2135                if newline_ix > 0 {
2136                    callback(row, &line);
2137                    row += 1;
2138                    line.clear();
2139                }
2140                line.push_str(text);
2141            }
2142        }
2143    }
2144
2145    pub fn syntax_layers(&self) -> impl Iterator<Item = SyntaxLayerInfo> + '_ {
2146        self.syntax.layers_for_range(0..self.len(), &self.text)
2147    }
2148
2149    pub fn syntax_layer_at<D: ToOffset>(&self, position: D) -> Option<SyntaxLayerInfo> {
2150        let offset = position.to_offset(self);
2151        self.syntax
2152            .layers_for_range(offset..offset, &self.text)
2153            .filter(|l| l.node().end_byte() > offset)
2154            .last()
2155    }
2156
2157    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2158        self.syntax_layer_at(position)
2159            .map(|info| info.language)
2160            .or(self.language.as_ref())
2161    }
2162
2163    pub fn settings_at<'a, D: ToOffset>(
2164        &self,
2165        position: D,
2166        cx: &'a AppContext,
2167    ) -> &'a LanguageSettings {
2168        language_settings(self.language_at(position), self.file.as_ref(), cx)
2169    }
2170
2171    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2172        let offset = position.to_offset(self);
2173        let mut scope = None;
2174        let mut smallest_range: Option<Range<usize>> = None;
2175
2176        // Use the layer that has the smallest node intersecting the given point.
2177        for layer in self.syntax.layers_for_range(offset..offset, &self.text) {
2178            let mut cursor = layer.node().walk();
2179
2180            let mut range = None;
2181            loop {
2182                let child_range = cursor.node().byte_range();
2183                if !child_range.to_inclusive().contains(&offset) {
2184                    break;
2185                }
2186
2187                range = Some(child_range);
2188                if cursor.goto_first_child_for_byte(offset).is_none() {
2189                    break;
2190                }
2191            }
2192
2193            if let Some(range) = range {
2194                if smallest_range
2195                    .as_ref()
2196                    .map_or(true, |smallest_range| range.len() < smallest_range.len())
2197                {
2198                    smallest_range = Some(range);
2199                    scope = Some(LanguageScope {
2200                        language: layer.language.clone(),
2201                        override_id: layer.override_id(offset, &self.text),
2202                    });
2203                }
2204            }
2205        }
2206
2207        scope.or_else(|| {
2208            self.language.clone().map(|language| LanguageScope {
2209                language,
2210                override_id: None,
2211            })
2212        })
2213    }
2214
2215    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2216        let mut start = start.to_offset(self);
2217        let mut end = start;
2218        let mut next_chars = self.chars_at(start).peekable();
2219        let mut prev_chars = self.reversed_chars_at(start).peekable();
2220
2221        let scope = self.language_scope_at(start);
2222        let kind = |c| char_kind(&scope, c);
2223        let word_kind = cmp::max(
2224            prev_chars.peek().copied().map(kind),
2225            next_chars.peek().copied().map(kind),
2226        );
2227
2228        for ch in prev_chars {
2229            if Some(kind(ch)) == word_kind && ch != '\n' {
2230                start -= ch.len_utf8();
2231            } else {
2232                break;
2233            }
2234        }
2235
2236        for ch in next_chars {
2237            if Some(kind(ch)) == word_kind && ch != '\n' {
2238                end += ch.len_utf8();
2239            } else {
2240                break;
2241            }
2242        }
2243
2244        (start..end, word_kind)
2245    }
2246
2247    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2248        let range = range.start.to_offset(self)..range.end.to_offset(self);
2249        let mut result: Option<Range<usize>> = None;
2250        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2251            let mut cursor = layer.node().walk();
2252
2253            // Descend to the first leaf that touches the start of the range,
2254            // and if the range is non-empty, extends beyond the start.
2255            while cursor.goto_first_child_for_byte(range.start).is_some() {
2256                if !range.is_empty() && cursor.node().end_byte() == range.start {
2257                    cursor.goto_next_sibling();
2258                }
2259            }
2260
2261            // Ascend to the smallest ancestor that strictly contains the range.
2262            loop {
2263                let node_range = cursor.node().byte_range();
2264                if node_range.start <= range.start
2265                    && node_range.end >= range.end
2266                    && node_range.len() > range.len()
2267                {
2268                    break;
2269                }
2270                if !cursor.goto_parent() {
2271                    continue 'outer;
2272                }
2273            }
2274
2275            let left_node = cursor.node();
2276            let mut layer_result = left_node.byte_range();
2277
2278            // For an empty range, try to find another node immediately to the right of the range.
2279            if left_node.end_byte() == range.start {
2280                let mut right_node = None;
2281                while !cursor.goto_next_sibling() {
2282                    if !cursor.goto_parent() {
2283                        break;
2284                    }
2285                }
2286
2287                while cursor.node().start_byte() == range.start {
2288                    right_node = Some(cursor.node());
2289                    if !cursor.goto_first_child() {
2290                        break;
2291                    }
2292                }
2293
2294                // If there is a candidate node on both sides of the (empty) range, then
2295                // decide between the two by favoring a named node over an anonymous token.
2296                // If both nodes are the same in that regard, favor the right one.
2297                if let Some(right_node) = right_node {
2298                    if right_node.is_named() || !left_node.is_named() {
2299                        layer_result = right_node.byte_range();
2300                    }
2301                }
2302            }
2303
2304            if let Some(previous_result) = &result {
2305                if previous_result.len() < layer_result.len() {
2306                    continue;
2307                }
2308            }
2309            result = Some(layer_result);
2310        }
2311
2312        result
2313    }
2314
2315    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2316        self.outline_items_containing(0..self.len(), true, theme)
2317            .map(Outline::new)
2318    }
2319
2320    pub fn symbols_containing<T: ToOffset>(
2321        &self,
2322        position: T,
2323        theme: Option<&SyntaxTheme>,
2324    ) -> Option<Vec<OutlineItem<Anchor>>> {
2325        let position = position.to_offset(self);
2326        let mut items = self.outline_items_containing(
2327            position.saturating_sub(1)..self.len().min(position + 1),
2328            false,
2329            theme,
2330        )?;
2331        let mut prev_depth = None;
2332        items.retain(|item| {
2333            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2334            prev_depth = Some(item.depth);
2335            result
2336        });
2337        Some(items)
2338    }
2339
2340    fn outline_items_containing(
2341        &self,
2342        range: Range<usize>,
2343        include_extra_context: bool,
2344        theme: Option<&SyntaxTheme>,
2345    ) -> Option<Vec<OutlineItem<Anchor>>> {
2346        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2347            grammar.outline_config.as_ref().map(|c| &c.query)
2348        });
2349        let configs = matches
2350            .grammars()
2351            .iter()
2352            .map(|g| g.outline_config.as_ref().unwrap())
2353            .collect::<Vec<_>>();
2354
2355        let mut stack = Vec::<Range<usize>>::new();
2356        let mut items = Vec::new();
2357        while let Some(mat) = matches.peek() {
2358            let config = &configs[mat.grammar_index];
2359            let item_node = mat.captures.iter().find_map(|cap| {
2360                if cap.index == config.item_capture_ix {
2361                    Some(cap.node)
2362                } else {
2363                    None
2364                }
2365            })?;
2366
2367            let item_range = item_node.byte_range();
2368            if item_range.end < range.start || item_range.start > range.end {
2369                matches.advance();
2370                continue;
2371            }
2372
2373            let mut buffer_ranges = Vec::new();
2374            for capture in mat.captures {
2375                let node_is_name;
2376                if capture.index == config.name_capture_ix {
2377                    node_is_name = true;
2378                } else if Some(capture.index) == config.context_capture_ix
2379                    || (Some(capture.index) == config.extra_context_capture_ix
2380                        && include_extra_context)
2381                {
2382                    node_is_name = false;
2383                } else {
2384                    continue;
2385                }
2386
2387                let mut range = capture.node.start_byte()..capture.node.end_byte();
2388                let start = capture.node.start_position();
2389                if capture.node.end_position().row > start.row {
2390                    range.end =
2391                        range.start + self.line_len(start.row as u32) as usize - start.column;
2392                }
2393
2394                buffer_ranges.push((range, node_is_name));
2395            }
2396
2397            if buffer_ranges.is_empty() {
2398                continue;
2399            }
2400
2401            let mut text = String::new();
2402            let mut highlight_ranges = Vec::new();
2403            let mut name_ranges = Vec::new();
2404            let mut chunks = self.chunks(
2405                buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end,
2406                true,
2407            );
2408            let mut last_buffer_range_end = 0;
2409            for (buffer_range, is_name) in buffer_ranges {
2410                if !text.is_empty() && buffer_range.start > last_buffer_range_end {
2411                    text.push(' ');
2412                }
2413                last_buffer_range_end = buffer_range.end;
2414                if is_name {
2415                    let mut start = text.len();
2416                    let end = start + buffer_range.len();
2417
2418                    // When multiple names are captured, then the matcheable text
2419                    // includes the whitespace in between the names.
2420                    if !name_ranges.is_empty() {
2421                        start -= 1;
2422                    }
2423
2424                    name_ranges.push(start..end);
2425                }
2426
2427                let mut offset = buffer_range.start;
2428                chunks.seek(offset);
2429                for mut chunk in chunks.by_ref() {
2430                    if chunk.text.len() > buffer_range.end - offset {
2431                        chunk.text = &chunk.text[0..(buffer_range.end - offset)];
2432                        offset = buffer_range.end;
2433                    } else {
2434                        offset += chunk.text.len();
2435                    }
2436                    let style = chunk
2437                        .syntax_highlight_id
2438                        .zip(theme)
2439                        .and_then(|(highlight, theme)| highlight.style(theme));
2440                    if let Some(style) = style {
2441                        let start = text.len();
2442                        let end = start + chunk.text.len();
2443                        highlight_ranges.push((start..end, style));
2444                    }
2445                    text.push_str(chunk.text);
2446                    if offset >= buffer_range.end {
2447                        break;
2448                    }
2449                }
2450            }
2451
2452            matches.advance();
2453            while stack.last().map_or(false, |prev_range| {
2454                prev_range.start > item_range.start || prev_range.end < item_range.end
2455            }) {
2456                stack.pop();
2457            }
2458            stack.push(item_range.clone());
2459
2460            items.push(OutlineItem {
2461                depth: stack.len() - 1,
2462                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2463                text,
2464                highlight_ranges,
2465                name_ranges,
2466            })
2467        }
2468        Some(items)
2469    }
2470
2471    pub fn matches(
2472        &self,
2473        range: Range<usize>,
2474        query: fn(&Grammar) -> Option<&tree_sitter::Query>,
2475    ) -> SyntaxMapMatches {
2476        self.syntax.matches(range, self, query)
2477    }
2478
2479    /// Returns bracket range pairs overlapping or adjacent to `range`
2480    pub fn bracket_ranges<'a, T: ToOffset>(
2481        &'a self,
2482        range: Range<T>,
2483    ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + 'a {
2484        // Find bracket pairs that *inclusively* contain the given range.
2485        let range = range.start.to_offset(self).saturating_sub(1)
2486            ..self.len().min(range.end.to_offset(self) + 1);
2487
2488        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2489            grammar.brackets_config.as_ref().map(|c| &c.query)
2490        });
2491        let configs = matches
2492            .grammars()
2493            .iter()
2494            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2495            .collect::<Vec<_>>();
2496
2497        iter::from_fn(move || {
2498            while let Some(mat) = matches.peek() {
2499                let mut open = None;
2500                let mut close = None;
2501                let config = &configs[mat.grammar_index];
2502                for capture in mat.captures {
2503                    if capture.index == config.open_capture_ix {
2504                        open = Some(capture.node.byte_range());
2505                    } else if capture.index == config.close_capture_ix {
2506                        close = Some(capture.node.byte_range());
2507                    }
2508                }
2509
2510                matches.advance();
2511
2512                let Some((open, close)) = open.zip(close) else {
2513                    continue;
2514                };
2515
2516                let bracket_range = open.start..=close.end;
2517                if !bracket_range.overlaps(&range) {
2518                    continue;
2519                }
2520
2521                return Some((open, close));
2522            }
2523            None
2524        })
2525    }
2526
2527    #[allow(clippy::type_complexity)]
2528    pub fn remote_selections_in_range(
2529        &self,
2530        range: Range<Anchor>,
2531    ) -> impl Iterator<
2532        Item = (
2533            ReplicaId,
2534            bool,
2535            CursorShape,
2536            impl Iterator<Item = &Selection<Anchor>> + '_,
2537        ),
2538    > + '_ {
2539        self.remote_selections
2540            .iter()
2541            .filter(|(replica_id, set)| {
2542                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2543            })
2544            .map(move |(replica_id, set)| {
2545                let start_ix = match set.selections.binary_search_by(|probe| {
2546                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2547                }) {
2548                    Ok(ix) | Err(ix) => ix,
2549                };
2550                let end_ix = match set.selections.binary_search_by(|probe| {
2551                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2552                }) {
2553                    Ok(ix) | Err(ix) => ix,
2554                };
2555
2556                (
2557                    *replica_id,
2558                    set.line_mode,
2559                    set.cursor_shape,
2560                    set.selections[start_ix..end_ix].iter(),
2561                )
2562            })
2563    }
2564
2565    pub fn git_diff_hunks_in_row_range<'a>(
2566        &'a self,
2567        range: Range<u32>,
2568    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2569        self.git_diff.hunks_in_row_range(range, self)
2570    }
2571
2572    pub fn git_diff_hunks_intersecting_range<'a>(
2573        &'a self,
2574        range: Range<Anchor>,
2575    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2576        self.git_diff.hunks_intersecting_range(range, self)
2577    }
2578
2579    pub fn git_diff_hunks_intersecting_range_rev<'a>(
2580        &'a self,
2581        range: Range<Anchor>,
2582    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2583        self.git_diff.hunks_intersecting_range_rev(range, self)
2584    }
2585
2586    pub fn diagnostics_in_range<'a, T, O>(
2587        &'a self,
2588        search_range: Range<T>,
2589        reversed: bool,
2590    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2591    where
2592        T: 'a + Clone + ToOffset,
2593        O: 'a + FromAnchor + Ord,
2594    {
2595        let mut iterators: Vec<_> = self
2596            .diagnostics
2597            .iter()
2598            .map(|(_, collection)| {
2599                collection
2600                    .range::<T, O>(search_range.clone(), self, true, reversed)
2601                    .peekable()
2602            })
2603            .collect();
2604
2605        std::iter::from_fn(move || {
2606            let (next_ix, _) = iterators
2607                .iter_mut()
2608                .enumerate()
2609                .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
2610                .min_by(|(_, a), (_, b)| a.range.start.cmp(&b.range.start))?;
2611            iterators[next_ix].next()
2612        })
2613    }
2614
2615    pub fn diagnostic_groups(
2616        &self,
2617        language_server_id: Option<LanguageServerId>,
2618    ) -> Vec<(LanguageServerId, DiagnosticGroup<Anchor>)> {
2619        let mut groups = Vec::new();
2620
2621        if let Some(language_server_id) = language_server_id {
2622            if let Ok(ix) = self
2623                .diagnostics
2624                .binary_search_by_key(&language_server_id, |e| e.0)
2625            {
2626                self.diagnostics[ix]
2627                    .1
2628                    .groups(language_server_id, &mut groups, self);
2629            }
2630        } else {
2631            for (language_server_id, diagnostics) in self.diagnostics.iter() {
2632                diagnostics.groups(*language_server_id, &mut groups, self);
2633            }
2634        }
2635
2636        groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
2637            let a_start = &group_a.entries[group_a.primary_ix].range.start;
2638            let b_start = &group_b.entries[group_b.primary_ix].range.start;
2639            a_start.cmp(b_start, self).then_with(|| id_a.cmp(&id_b))
2640        });
2641
2642        groups
2643    }
2644
2645    pub fn diagnostic_group<'a, O>(
2646        &'a self,
2647        group_id: usize,
2648    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2649    where
2650        O: 'a + FromAnchor,
2651    {
2652        self.diagnostics
2653            .iter()
2654            .flat_map(move |(_, set)| set.group(group_id, self))
2655    }
2656
2657    pub fn diagnostics_update_count(&self) -> usize {
2658        self.diagnostics_update_count
2659    }
2660
2661    pub fn parse_count(&self) -> usize {
2662        self.parse_count
2663    }
2664
2665    pub fn selections_update_count(&self) -> usize {
2666        self.selections_update_count
2667    }
2668
2669    pub fn file(&self) -> Option<&Arc<dyn File>> {
2670        self.file.as_ref()
2671    }
2672
2673    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2674        if let Some(file) = self.file() {
2675            if file.path().file_name().is_none() || include_root {
2676                Some(file.full_path(cx))
2677            } else {
2678                Some(file.path().to_path_buf())
2679            }
2680        } else {
2681            None
2682        }
2683    }
2684
2685    pub fn file_update_count(&self) -> usize {
2686        self.file_update_count
2687    }
2688
2689    pub fn git_diff_update_count(&self) -> usize {
2690        self.git_diff_update_count
2691    }
2692}
2693
2694fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2695    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2696}
2697
2698pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2699    let mut result = IndentSize::spaces(0);
2700    for c in text {
2701        let kind = match c {
2702            ' ' => IndentKind::Space,
2703            '\t' => IndentKind::Tab,
2704            _ => break,
2705        };
2706        if result.len == 0 {
2707            result.kind = kind;
2708        }
2709        result.len += 1;
2710    }
2711    result
2712}
2713
2714impl Clone for BufferSnapshot {
2715    fn clone(&self) -> Self {
2716        Self {
2717            text: self.text.clone(),
2718            git_diff: self.git_diff.clone(),
2719            syntax: self.syntax.clone(),
2720            file: self.file.clone(),
2721            remote_selections: self.remote_selections.clone(),
2722            diagnostics: self.diagnostics.clone(),
2723            selections_update_count: self.selections_update_count,
2724            diagnostics_update_count: self.diagnostics_update_count,
2725            file_update_count: self.file_update_count,
2726            git_diff_update_count: self.git_diff_update_count,
2727            language: self.language.clone(),
2728            parse_count: self.parse_count,
2729        }
2730    }
2731}
2732
2733impl Deref for BufferSnapshot {
2734    type Target = text::BufferSnapshot;
2735
2736    fn deref(&self) -> &Self::Target {
2737        &self.text
2738    }
2739}
2740
2741unsafe impl<'a> Send for BufferChunks<'a> {}
2742
2743impl<'a> BufferChunks<'a> {
2744    pub(crate) fn new(
2745        text: &'a Rope,
2746        range: Range<usize>,
2747        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2748        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2749    ) -> Self {
2750        let mut highlights = None;
2751        if let Some((captures, highlight_maps)) = syntax {
2752            highlights = Some(BufferChunkHighlights {
2753                captures,
2754                next_capture: None,
2755                stack: Default::default(),
2756                highlight_maps,
2757            })
2758        }
2759
2760        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2761        let chunks = text.chunks_in_range(range.clone());
2762
2763        BufferChunks {
2764            range,
2765            chunks,
2766            diagnostic_endpoints,
2767            error_depth: 0,
2768            warning_depth: 0,
2769            information_depth: 0,
2770            hint_depth: 0,
2771            unnecessary_depth: 0,
2772            highlights,
2773        }
2774    }
2775
2776    pub fn seek(&mut self, offset: usize) {
2777        self.range.start = offset;
2778        self.chunks.seek(self.range.start);
2779        if let Some(highlights) = self.highlights.as_mut() {
2780            highlights
2781                .stack
2782                .retain(|(end_offset, _)| *end_offset > offset);
2783            if let Some(capture) = &highlights.next_capture {
2784                if offset >= capture.node.start_byte() {
2785                    let next_capture_end = capture.node.end_byte();
2786                    if offset < next_capture_end {
2787                        highlights.stack.push((
2788                            next_capture_end,
2789                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2790                        ));
2791                    }
2792                    highlights.next_capture.take();
2793                }
2794            }
2795            highlights.captures.set_byte_range(self.range.clone());
2796        }
2797    }
2798
2799    pub fn offset(&self) -> usize {
2800        self.range.start
2801    }
2802
2803    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2804        let depth = match endpoint.severity {
2805            DiagnosticSeverity::ERROR => &mut self.error_depth,
2806            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2807            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2808            DiagnosticSeverity::HINT => &mut self.hint_depth,
2809            _ => return,
2810        };
2811        if endpoint.is_start {
2812            *depth += 1;
2813        } else {
2814            *depth -= 1;
2815        }
2816
2817        if endpoint.is_unnecessary {
2818            if endpoint.is_start {
2819                self.unnecessary_depth += 1;
2820            } else {
2821                self.unnecessary_depth -= 1;
2822            }
2823        }
2824    }
2825
2826    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2827        if self.error_depth > 0 {
2828            Some(DiagnosticSeverity::ERROR)
2829        } else if self.warning_depth > 0 {
2830            Some(DiagnosticSeverity::WARNING)
2831        } else if self.information_depth > 0 {
2832            Some(DiagnosticSeverity::INFORMATION)
2833        } else if self.hint_depth > 0 {
2834            Some(DiagnosticSeverity::HINT)
2835        } else {
2836            None
2837        }
2838    }
2839
2840    fn current_code_is_unnecessary(&self) -> bool {
2841        self.unnecessary_depth > 0
2842    }
2843}
2844
2845impl<'a> Iterator for BufferChunks<'a> {
2846    type Item = Chunk<'a>;
2847
2848    fn next(&mut self) -> Option<Self::Item> {
2849        let mut next_capture_start = usize::MAX;
2850        let mut next_diagnostic_endpoint = usize::MAX;
2851
2852        if let Some(highlights) = self.highlights.as_mut() {
2853            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2854                if *parent_capture_end <= self.range.start {
2855                    highlights.stack.pop();
2856                } else {
2857                    break;
2858                }
2859            }
2860
2861            if highlights.next_capture.is_none() {
2862                highlights.next_capture = highlights.captures.next();
2863            }
2864
2865            while let Some(capture) = highlights.next_capture.as_ref() {
2866                if self.range.start < capture.node.start_byte() {
2867                    next_capture_start = capture.node.start_byte();
2868                    break;
2869                } else {
2870                    let highlight_id =
2871                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2872                    highlights
2873                        .stack
2874                        .push((capture.node.end_byte(), highlight_id));
2875                    highlights.next_capture = highlights.captures.next();
2876                }
2877            }
2878        }
2879
2880        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2881            if endpoint.offset <= self.range.start {
2882                self.update_diagnostic_depths(endpoint);
2883                self.diagnostic_endpoints.next();
2884            } else {
2885                next_diagnostic_endpoint = endpoint.offset;
2886                break;
2887            }
2888        }
2889
2890        if let Some(chunk) = self.chunks.peek() {
2891            let chunk_start = self.range.start;
2892            let mut chunk_end = (self.chunks.offset() + chunk.len())
2893                .min(next_capture_start)
2894                .min(next_diagnostic_endpoint);
2895            let mut highlight_id = None;
2896            if let Some(highlights) = self.highlights.as_ref() {
2897                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2898                    chunk_end = chunk_end.min(*parent_capture_end);
2899                    highlight_id = Some(*parent_highlight_id);
2900                }
2901            }
2902
2903            let slice =
2904                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2905            self.range.start = chunk_end;
2906            if self.range.start == self.chunks.offset() + chunk.len() {
2907                self.chunks.next().unwrap();
2908            }
2909
2910            Some(Chunk {
2911                text: slice,
2912                syntax_highlight_id: highlight_id,
2913                diagnostic_severity: self.current_diagnostic_severity(),
2914                is_unnecessary: self.current_code_is_unnecessary(),
2915                ..Default::default()
2916            })
2917        } else {
2918            None
2919        }
2920    }
2921}
2922
2923impl operation_queue::Operation for Operation {
2924    fn lamport_timestamp(&self) -> clock::Lamport {
2925        match self {
2926            Operation::Buffer(_) => {
2927                unreachable!("buffer operations should never be deferred at this layer")
2928            }
2929            Operation::UpdateDiagnostics {
2930                lamport_timestamp, ..
2931            }
2932            | Operation::UpdateSelections {
2933                lamport_timestamp, ..
2934            }
2935            | Operation::UpdateCompletionTriggers {
2936                lamport_timestamp, ..
2937            } => *lamport_timestamp,
2938        }
2939    }
2940}
2941
2942impl Default for Diagnostic {
2943    fn default() -> Self {
2944        Self {
2945            source: Default::default(),
2946            code: None,
2947            severity: DiagnosticSeverity::ERROR,
2948            message: Default::default(),
2949            group_id: 0,
2950            is_primary: false,
2951            is_valid: true,
2952            is_disk_based: false,
2953            is_unnecessary: false,
2954        }
2955    }
2956}
2957
2958impl IndentSize {
2959    pub fn spaces(len: u32) -> Self {
2960        Self {
2961            len,
2962            kind: IndentKind::Space,
2963        }
2964    }
2965
2966    pub fn tab() -> Self {
2967        Self {
2968            len: 1,
2969            kind: IndentKind::Tab,
2970        }
2971    }
2972
2973    pub fn chars(&self) -> impl Iterator<Item = char> {
2974        iter::repeat(self.char()).take(self.len as usize)
2975    }
2976
2977    pub fn char(&self) -> char {
2978        match self.kind {
2979            IndentKind::Space => ' ',
2980            IndentKind::Tab => '\t',
2981        }
2982    }
2983
2984    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2985        match direction {
2986            Ordering::Less => {
2987                if self.kind == size.kind && self.len >= size.len {
2988                    self.len -= size.len;
2989                }
2990            }
2991            Ordering::Equal => {}
2992            Ordering::Greater => {
2993                if self.len == 0 {
2994                    self = size;
2995                } else if self.kind == size.kind {
2996                    self.len += size.len;
2997                }
2998            }
2999        }
3000        self
3001    }
3002}
3003
3004impl Completion {
3005    pub fn sort_key(&self) -> (usize, &str) {
3006        let kind_key = match self.lsp_completion.kind {
3007            Some(lsp::CompletionItemKind::VARIABLE) => 0,
3008            _ => 1,
3009        };
3010        (kind_key, &self.label.text[self.label.filter_range.clone()])
3011    }
3012
3013    pub fn is_snippet(&self) -> bool {
3014        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
3015    }
3016}
3017
3018pub fn contiguous_ranges(
3019    values: impl Iterator<Item = u32>,
3020    max_len: usize,
3021) -> impl Iterator<Item = Range<u32>> {
3022    let mut values = values;
3023    let mut current_range: Option<Range<u32>> = None;
3024    std::iter::from_fn(move || loop {
3025        if let Some(value) = values.next() {
3026            if let Some(range) = &mut current_range {
3027                if value == range.end && range.len() < max_len {
3028                    range.end += 1;
3029                    continue;
3030                }
3031            }
3032
3033            let prev_range = current_range.clone();
3034            current_range = Some(value..(value + 1));
3035            if prev_range.is_some() {
3036                return prev_range;
3037            }
3038        } else {
3039            return current_range.take();
3040        }
3041    })
3042}
3043
3044pub fn char_kind(scope: &Option<LanguageScope>, c: char) -> CharKind {
3045    if c.is_whitespace() {
3046        return CharKind::Whitespace;
3047    } else if c.is_alphanumeric() || c == '_' {
3048        return CharKind::Word;
3049    }
3050
3051    if let Some(scope) = scope {
3052        if let Some(characters) = scope.word_characters() {
3053            if characters.contains(&c) {
3054                return CharKind::Word;
3055            }
3056        }
3057    }
3058
3059    CharKind::Punctuation
3060}
3061
3062/// Find all of the ranges of whitespace that occur at the ends of lines
3063/// in the given rope.
3064///
3065/// This could also be done with a regex search, but this implementation
3066/// avoids copying text.
3067pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
3068    let mut ranges = Vec::new();
3069
3070    let mut offset = 0;
3071    let mut prev_chunk_trailing_whitespace_range = 0..0;
3072    for chunk in rope.chunks() {
3073        let mut prev_line_trailing_whitespace_range = 0..0;
3074        for (i, line) in chunk.split('\n').enumerate() {
3075            let line_end_offset = offset + line.len();
3076            let trimmed_line_len = line.trim_end_matches(|c| matches!(c, ' ' | '\t')).len();
3077            let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
3078
3079            if i == 0 && trimmed_line_len == 0 {
3080                trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
3081            }
3082            if !prev_line_trailing_whitespace_range.is_empty() {
3083                ranges.push(prev_line_trailing_whitespace_range);
3084            }
3085
3086            offset = line_end_offset + 1;
3087            prev_line_trailing_whitespace_range = trailing_whitespace_range;
3088        }
3089
3090        offset -= 1;
3091        prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
3092    }
3093
3094    if !prev_chunk_trailing_whitespace_range.is_empty() {
3095        ranges.push(prev_chunk_trailing_whitespace_range);
3096    }
3097
3098    ranges
3099}