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