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