buffer.rs

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