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