buffer.rs

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