buffer.rs

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