buffer.rs

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