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 mut delta = 0isize;
1336            let entries = edits
1337                .into_iter()
1338                .enumerate()
1339                .zip(&edit_operation.as_edit().unwrap().new_text)
1340                .map(|((ix, (range, _)), new_text)| {
1341                    let new_text_len = new_text.len();
1342                    let old_start = range.start.to_point(&before_edit);
1343                    let new_start = (delta + range.start as isize) as usize;
1344                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1345
1346                    let mut range_of_insertion_to_indent = 0..new_text_len;
1347                    let mut first_line_is_new = false;
1348                    let mut original_indent_column = None;
1349
1350                    // When inserting an entire line at the beginning of an existing line,
1351                    // treat the insertion as new.
1352                    if new_text.contains('\n')
1353                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1354                    {
1355                        first_line_is_new = true;
1356                    }
1357
1358                    // When inserting text starting with a newline, avoid auto-indenting the
1359                    // previous line.
1360                    if new_text.starts_with('\n') {
1361                        range_of_insertion_to_indent.start += 1;
1362                        first_line_is_new = true;
1363                    }
1364
1365                    // Avoid auto-indenting after the insertion.
1366                    if let AutoindentMode::Block {
1367                        original_indent_columns,
1368                    } = &mode
1369                    {
1370                        original_indent_column =
1371                            Some(original_indent_columns.get(ix).copied().unwrap_or_else(|| {
1372                                indent_size_for_text(
1373                                    new_text[range_of_insertion_to_indent.clone()].chars(),
1374                                )
1375                                .len
1376                            }));
1377                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1378                            range_of_insertion_to_indent.end -= 1;
1379                        }
1380                    }
1381
1382                    AutoindentRequestEntry {
1383                        first_line_is_new,
1384                        original_indent_column,
1385                        indent_size: before_edit.language_indent_size_at(range.start, cx),
1386                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1387                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1388                    }
1389                })
1390                .collect();
1391
1392            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1393                before_edit,
1394                entries,
1395                is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
1396            }));
1397        }
1398
1399        self.end_transaction(cx);
1400        self.send_operation(Operation::Buffer(edit_operation), cx);
1401        Some(edit_id)
1402    }
1403
1404    fn did_edit(
1405        &mut self,
1406        old_version: &clock::Global,
1407        was_dirty: bool,
1408        cx: &mut ModelContext<Self>,
1409    ) {
1410        if self.edits_since::<usize>(old_version).next().is_none() {
1411            return;
1412        }
1413
1414        self.reparse(cx);
1415
1416        cx.emit(Event::Edited);
1417        if was_dirty != self.is_dirty() {
1418            cx.emit(Event::DirtyChanged);
1419        }
1420        cx.notify();
1421    }
1422
1423    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1424        &mut self,
1425        ops: I,
1426        cx: &mut ModelContext<Self>,
1427    ) -> Result<()> {
1428        self.pending_autoindent.take();
1429        let was_dirty = self.is_dirty();
1430        let old_version = self.version.clone();
1431        let mut deferred_ops = Vec::new();
1432        let buffer_ops = ops
1433            .into_iter()
1434            .filter_map(|op| match op {
1435                Operation::Buffer(op) => Some(op),
1436                _ => {
1437                    if self.can_apply_op(&op) {
1438                        self.apply_op(op, cx);
1439                    } else {
1440                        deferred_ops.push(op);
1441                    }
1442                    None
1443                }
1444            })
1445            .collect::<Vec<_>>();
1446        self.text.apply_ops(buffer_ops)?;
1447        self.deferred_ops.insert(deferred_ops);
1448        self.flush_deferred_ops(cx);
1449        self.did_edit(&old_version, was_dirty, cx);
1450        // Notify independently of whether the buffer was edited as the operations could include a
1451        // selection update.
1452        cx.notify();
1453        Ok(())
1454    }
1455
1456    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1457        let mut deferred_ops = Vec::new();
1458        for op in self.deferred_ops.drain().iter().cloned() {
1459            if self.can_apply_op(&op) {
1460                self.apply_op(op, cx);
1461            } else {
1462                deferred_ops.push(op);
1463            }
1464        }
1465        self.deferred_ops.insert(deferred_ops);
1466    }
1467
1468    fn can_apply_op(&self, operation: &Operation) -> bool {
1469        match operation {
1470            Operation::Buffer(_) => {
1471                unreachable!("buffer operations should never be applied at this layer")
1472            }
1473            Operation::UpdateDiagnostics {
1474                diagnostics: diagnostic_set,
1475                ..
1476            } => diagnostic_set.iter().all(|diagnostic| {
1477                self.text.can_resolve(&diagnostic.range.start)
1478                    && self.text.can_resolve(&diagnostic.range.end)
1479            }),
1480            Operation::UpdateSelections { selections, .. } => selections
1481                .iter()
1482                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1483            Operation::UpdateCompletionTriggers { .. } => true,
1484        }
1485    }
1486
1487    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1488        match operation {
1489            Operation::Buffer(_) => {
1490                unreachable!("buffer operations should never be applied at this layer")
1491            }
1492            Operation::UpdateDiagnostics {
1493                diagnostics: diagnostic_set,
1494                lamport_timestamp,
1495            } => {
1496                let snapshot = self.snapshot();
1497                self.apply_diagnostic_update(
1498                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1499                    lamport_timestamp,
1500                    cx,
1501                );
1502            }
1503            Operation::UpdateSelections {
1504                selections,
1505                lamport_timestamp,
1506                line_mode,
1507                cursor_shape,
1508            } => {
1509                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1510                    if set.lamport_timestamp > lamport_timestamp {
1511                        return;
1512                    }
1513                }
1514
1515                self.remote_selections.insert(
1516                    lamport_timestamp.replica_id,
1517                    SelectionSet {
1518                        selections,
1519                        lamport_timestamp,
1520                        line_mode,
1521                        cursor_shape,
1522                    },
1523                );
1524                self.text.lamport_clock.observe(lamport_timestamp);
1525                self.selections_update_count += 1;
1526            }
1527            Operation::UpdateCompletionTriggers {
1528                triggers,
1529                lamport_timestamp,
1530            } => {
1531                self.completion_triggers = triggers;
1532                self.text.lamport_clock.observe(lamport_timestamp);
1533            }
1534        }
1535    }
1536
1537    fn apply_diagnostic_update(
1538        &mut self,
1539        diagnostics: DiagnosticSet,
1540        lamport_timestamp: clock::Lamport,
1541        cx: &mut ModelContext<Self>,
1542    ) {
1543        if lamport_timestamp > self.diagnostics_timestamp {
1544            self.diagnostics = diagnostics;
1545            self.diagnostics_timestamp = lamport_timestamp;
1546            self.diagnostics_update_count += 1;
1547            self.text.lamport_clock.observe(lamport_timestamp);
1548            cx.notify();
1549            cx.emit(Event::DiagnosticsUpdated);
1550        }
1551    }
1552
1553    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1554        cx.emit(Event::Operation(operation));
1555    }
1556
1557    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1558        self.remote_selections.remove(&replica_id);
1559        cx.notify();
1560    }
1561
1562    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1563        let was_dirty = self.is_dirty();
1564        let old_version = self.version.clone();
1565
1566        if let Some((transaction_id, operation)) = self.text.undo() {
1567            self.send_operation(Operation::Buffer(operation), cx);
1568            self.did_edit(&old_version, was_dirty, cx);
1569            Some(transaction_id)
1570        } else {
1571            None
1572        }
1573    }
1574
1575    pub fn undo_to_transaction(
1576        &mut self,
1577        transaction_id: TransactionId,
1578        cx: &mut ModelContext<Self>,
1579    ) -> bool {
1580        let was_dirty = self.is_dirty();
1581        let old_version = self.version.clone();
1582
1583        let operations = self.text.undo_to_transaction(transaction_id);
1584        let undone = !operations.is_empty();
1585        for operation in operations {
1586            self.send_operation(Operation::Buffer(operation), cx);
1587        }
1588        if undone {
1589            self.did_edit(&old_version, was_dirty, cx)
1590        }
1591        undone
1592    }
1593
1594    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1595        let was_dirty = self.is_dirty();
1596        let old_version = self.version.clone();
1597
1598        if let Some((transaction_id, operation)) = self.text.redo() {
1599            self.send_operation(Operation::Buffer(operation), cx);
1600            self.did_edit(&old_version, was_dirty, cx);
1601            Some(transaction_id)
1602        } else {
1603            None
1604        }
1605    }
1606
1607    pub fn redo_to_transaction(
1608        &mut self,
1609        transaction_id: TransactionId,
1610        cx: &mut ModelContext<Self>,
1611    ) -> bool {
1612        let was_dirty = self.is_dirty();
1613        let old_version = self.version.clone();
1614
1615        let operations = self.text.redo_to_transaction(transaction_id);
1616        let redone = !operations.is_empty();
1617        for operation in operations {
1618            self.send_operation(Operation::Buffer(operation), cx);
1619        }
1620        if redone {
1621            self.did_edit(&old_version, was_dirty, cx)
1622        }
1623        redone
1624    }
1625
1626    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1627        self.completion_triggers = triggers.clone();
1628        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1629        self.send_operation(
1630            Operation::UpdateCompletionTriggers {
1631                triggers,
1632                lamport_timestamp: self.completion_triggers_timestamp,
1633            },
1634            cx,
1635        );
1636        cx.notify();
1637    }
1638
1639    pub fn completion_triggers(&self) -> &[String] {
1640        &self.completion_triggers
1641    }
1642}
1643
1644#[cfg(any(test, feature = "test-support"))]
1645impl Buffer {
1646    pub fn set_group_interval(&mut self, group_interval: Duration) {
1647        self.text.set_group_interval(group_interval);
1648    }
1649
1650    pub fn randomly_edit<T>(
1651        &mut self,
1652        rng: &mut T,
1653        old_range_count: usize,
1654        cx: &mut ModelContext<Self>,
1655    ) where
1656        T: rand::Rng,
1657    {
1658        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1659        let mut last_end = None;
1660        for _ in 0..old_range_count {
1661            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1662                break;
1663            }
1664
1665            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1666            let mut range = self.random_byte_range(new_start, rng);
1667            if rng.gen_bool(0.2) {
1668                mem::swap(&mut range.start, &mut range.end);
1669            }
1670            last_end = Some(range.end);
1671
1672            let new_text_len = rng.gen_range(0..10);
1673            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1674
1675            edits.push((range, new_text));
1676        }
1677        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1678        self.edit(edits, None, cx);
1679    }
1680
1681    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1682        let was_dirty = self.is_dirty();
1683        let old_version = self.version.clone();
1684
1685        let ops = self.text.randomly_undo_redo(rng);
1686        if !ops.is_empty() {
1687            for op in ops {
1688                self.send_operation(Operation::Buffer(op), cx);
1689                self.did_edit(&old_version, was_dirty, cx);
1690            }
1691        }
1692    }
1693}
1694
1695impl Entity for Buffer {
1696    type Event = Event;
1697}
1698
1699impl Deref for Buffer {
1700    type Target = TextBuffer;
1701
1702    fn deref(&self) -> &Self::Target {
1703        &self.text
1704    }
1705}
1706
1707impl BufferSnapshot {
1708    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1709        indent_size_for_line(self, row)
1710    }
1711
1712    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1713        let language_name = self.language_at(position).map(|language| language.name());
1714        let settings = cx.global::<Settings>();
1715        if settings.hard_tabs(language_name.as_deref()) {
1716            IndentSize::tab()
1717        } else {
1718            IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
1719        }
1720    }
1721
1722    pub fn suggested_indents(
1723        &self,
1724        rows: impl Iterator<Item = u32>,
1725        single_indent_size: IndentSize,
1726    ) -> BTreeMap<u32, IndentSize> {
1727        let mut result = BTreeMap::new();
1728
1729        for row_range in contiguous_ranges(rows, 10) {
1730            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1731                Some(suggestions) => suggestions,
1732                _ => break,
1733            };
1734
1735            for (row, suggestion) in row_range.zip(suggestions) {
1736                let indent_size = if let Some(suggestion) = suggestion {
1737                    result
1738                        .get(&suggestion.basis_row)
1739                        .copied()
1740                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1741                        .with_delta(suggestion.delta, single_indent_size)
1742                } else {
1743                    self.indent_size_for_line(row)
1744                };
1745
1746                result.insert(row, indent_size);
1747            }
1748        }
1749
1750        result
1751    }
1752
1753    fn suggest_autoindents(
1754        &self,
1755        row_range: Range<u32>,
1756    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1757        let config = &self.language.as_ref()?.config;
1758        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1759
1760        // Find the suggested indentation ranges based on the syntax tree.
1761        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1762        let end = Point::new(row_range.end, 0);
1763        let range = (start..end).to_offset(&self.text);
1764        let mut matches = self.syntax.matches(range, &self.text, |grammar| {
1765            Some(&grammar.indents_config.as_ref()?.query)
1766        });
1767        let indent_configs = matches
1768            .grammars()
1769            .iter()
1770            .map(|grammar| grammar.indents_config.as_ref().unwrap())
1771            .collect::<Vec<_>>();
1772
1773        let mut indent_ranges = Vec::<Range<Point>>::new();
1774        let mut outdent_positions = Vec::<Point>::new();
1775        while let Some(mat) = matches.peek() {
1776            let mut start: Option<Point> = None;
1777            let mut end: Option<Point> = None;
1778
1779            let config = &indent_configs[mat.grammar_index];
1780            for capture in mat.captures {
1781                if capture.index == config.indent_capture_ix {
1782                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1783                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1784                } else if Some(capture.index) == config.start_capture_ix {
1785                    start = Some(Point::from_ts_point(capture.node.end_position()));
1786                } else if Some(capture.index) == config.end_capture_ix {
1787                    end = Some(Point::from_ts_point(capture.node.start_position()));
1788                } else if Some(capture.index) == config.outdent_capture_ix {
1789                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1790                }
1791            }
1792
1793            matches.advance();
1794            if let Some((start, end)) = start.zip(end) {
1795                if start.row == end.row {
1796                    continue;
1797                }
1798
1799                let range = start..end;
1800                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1801                    Err(ix) => indent_ranges.insert(ix, range),
1802                    Ok(ix) => {
1803                        let prev_range = &mut indent_ranges[ix];
1804                        prev_range.end = prev_range.end.max(range.end);
1805                    }
1806                }
1807            }
1808        }
1809
1810        outdent_positions.sort();
1811        for outdent_position in outdent_positions {
1812            // find the innermost indent range containing this outdent_position
1813            // set its end to the outdent position
1814            if let Some(range_to_truncate) = indent_ranges
1815                .iter_mut()
1816                .filter(|indent_range| indent_range.contains(&outdent_position))
1817                .last()
1818            {
1819                range_to_truncate.end = outdent_position;
1820            }
1821        }
1822
1823        // Find the suggested indentation increases and decreased based on regexes.
1824        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1825        self.for_each_line(
1826            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1827                ..Point::new(row_range.end, 0),
1828            |row, line| {
1829                if config
1830                    .decrease_indent_pattern
1831                    .as_ref()
1832                    .map_or(false, |regex| regex.is_match(line))
1833                {
1834                    indent_change_rows.push((row, Ordering::Less));
1835                }
1836                if config
1837                    .increase_indent_pattern
1838                    .as_ref()
1839                    .map_or(false, |regex| regex.is_match(line))
1840                {
1841                    indent_change_rows.push((row + 1, Ordering::Greater));
1842                }
1843            },
1844        );
1845
1846        let mut indent_changes = indent_change_rows.into_iter().peekable();
1847        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1848            prev_non_blank_row.unwrap_or(0)
1849        } else {
1850            row_range.start.saturating_sub(1)
1851        };
1852        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1853        Some(row_range.map(move |row| {
1854            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1855
1856            let mut indent_from_prev_row = false;
1857            let mut outdent_from_prev_row = false;
1858            let mut outdent_to_row = u32::MAX;
1859
1860            while let Some((indent_row, delta)) = indent_changes.peek() {
1861                match indent_row.cmp(&row) {
1862                    Ordering::Equal => match delta {
1863                        Ordering::Less => outdent_from_prev_row = true,
1864                        Ordering::Greater => indent_from_prev_row = true,
1865                        _ => {}
1866                    },
1867
1868                    Ordering::Greater => break,
1869                    Ordering::Less => {}
1870                }
1871
1872                indent_changes.next();
1873            }
1874
1875            for range in &indent_ranges {
1876                if range.start.row >= row {
1877                    break;
1878                }
1879                if range.start.row == prev_row && range.end > row_start {
1880                    indent_from_prev_row = true;
1881                }
1882                if range.end > prev_row_start && range.end <= row_start {
1883                    outdent_to_row = outdent_to_row.min(range.start.row);
1884                }
1885            }
1886
1887            let suggestion = if outdent_to_row == prev_row
1888                || (outdent_from_prev_row && indent_from_prev_row)
1889            {
1890                Some(IndentSuggestion {
1891                    basis_row: prev_row,
1892                    delta: Ordering::Equal,
1893                })
1894            } else if indent_from_prev_row {
1895                Some(IndentSuggestion {
1896                    basis_row: prev_row,
1897                    delta: Ordering::Greater,
1898                })
1899            } else if outdent_to_row < prev_row {
1900                Some(IndentSuggestion {
1901                    basis_row: outdent_to_row,
1902                    delta: Ordering::Equal,
1903                })
1904            } else if outdent_from_prev_row {
1905                Some(IndentSuggestion {
1906                    basis_row: prev_row,
1907                    delta: Ordering::Less,
1908                })
1909            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
1910            {
1911                Some(IndentSuggestion {
1912                    basis_row: prev_row,
1913                    delta: Ordering::Equal,
1914                })
1915            } else {
1916                None
1917            };
1918
1919            prev_row = row;
1920            prev_row_start = row_start;
1921            suggestion
1922        }))
1923    }
1924
1925    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1926        while row > 0 {
1927            row -= 1;
1928            if !self.is_line_blank(row) {
1929                return Some(row);
1930            }
1931        }
1932        None
1933    }
1934
1935    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
1936        let range = range.start.to_offset(self)..range.end.to_offset(self);
1937
1938        let mut syntax = None;
1939        let mut diagnostic_endpoints = Vec::new();
1940        if language_aware {
1941            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
1942                grammar.highlights_query.as_ref()
1943            });
1944            let highlight_maps = captures
1945                .grammars()
1946                .into_iter()
1947                .map(|grammar| grammar.highlight_map())
1948                .collect();
1949            syntax = Some((captures, highlight_maps));
1950            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1951                diagnostic_endpoints.push(DiagnosticEndpoint {
1952                    offset: entry.range.start,
1953                    is_start: true,
1954                    severity: entry.diagnostic.severity,
1955                    is_unnecessary: entry.diagnostic.is_unnecessary,
1956                });
1957                diagnostic_endpoints.push(DiagnosticEndpoint {
1958                    offset: entry.range.end,
1959                    is_start: false,
1960                    severity: entry.diagnostic.severity,
1961                    is_unnecessary: entry.diagnostic.is_unnecessary,
1962                });
1963            }
1964            diagnostic_endpoints
1965                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1966        }
1967
1968        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
1969    }
1970
1971    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
1972        let mut line = String::new();
1973        let mut row = range.start.row;
1974        for chunk in self
1975            .as_rope()
1976            .chunks_in_range(range.to_offset(self))
1977            .chain(["\n"])
1978        {
1979            for (newline_ix, text) in chunk.split('\n').enumerate() {
1980                if newline_ix > 0 {
1981                    callback(row, &line);
1982                    row += 1;
1983                    line.clear();
1984                }
1985                line.push_str(text);
1986            }
1987        }
1988    }
1989
1990    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
1991        let offset = position.to_offset(self);
1992        self.syntax
1993            .layers_for_range(offset..offset, &self.text)
1994            .filter(|l| l.node.end_byte() > offset)
1995            .last()
1996            .map(|info| info.language)
1997            .or(self.language.as_ref())
1998    }
1999
2000    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2001        let mut start = start.to_offset(self);
2002        let mut end = start;
2003        let mut next_chars = self.chars_at(start).peekable();
2004        let mut prev_chars = self.reversed_chars_at(start).peekable();
2005        let word_kind = cmp::max(
2006            prev_chars.peek().copied().map(char_kind),
2007            next_chars.peek().copied().map(char_kind),
2008        );
2009
2010        for ch in prev_chars {
2011            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2012                start -= ch.len_utf8();
2013            } else {
2014                break;
2015            }
2016        }
2017
2018        for ch in next_chars {
2019            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2020                end += ch.len_utf8();
2021            } else {
2022                break;
2023            }
2024        }
2025
2026        (start..end, word_kind)
2027    }
2028
2029    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2030        let range = range.start.to_offset(self)..range.end.to_offset(self);
2031        let mut result: Option<Range<usize>> = None;
2032        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2033            let mut cursor = layer.node.walk();
2034
2035            // Descend to the first leaf that touches the start of the range,
2036            // and if the range is non-empty, extends beyond the start.
2037            while cursor.goto_first_child_for_byte(range.start).is_some() {
2038                if !range.is_empty() && cursor.node().end_byte() == range.start {
2039                    cursor.goto_next_sibling();
2040                }
2041            }
2042
2043            // Ascend to the smallest ancestor that strictly contains the range.
2044            loop {
2045                let node_range = cursor.node().byte_range();
2046                if node_range.start <= range.start
2047                    && node_range.end >= range.end
2048                    && node_range.len() > range.len()
2049                {
2050                    break;
2051                }
2052                if !cursor.goto_parent() {
2053                    continue 'outer;
2054                }
2055            }
2056
2057            let left_node = cursor.node();
2058            let mut layer_result = left_node.byte_range();
2059
2060            // For an empty range, try to find another node immediately to the right of the range.
2061            if left_node.end_byte() == range.start {
2062                let mut right_node = None;
2063                while !cursor.goto_next_sibling() {
2064                    if !cursor.goto_parent() {
2065                        break;
2066                    }
2067                }
2068
2069                while cursor.node().start_byte() == range.start {
2070                    right_node = Some(cursor.node());
2071                    if !cursor.goto_first_child() {
2072                        break;
2073                    }
2074                }
2075
2076                // If there is a candidate node on both sides of the (empty) range, then
2077                // decide between the two by favoring a named node over an anonymous token.
2078                // If both nodes are the same in that regard, favor the right one.
2079                if let Some(right_node) = right_node {
2080                    if right_node.is_named() || !left_node.is_named() {
2081                        layer_result = right_node.byte_range();
2082                    }
2083                }
2084            }
2085
2086            if let Some(previous_result) = &result {
2087                if previous_result.len() < layer_result.len() {
2088                    continue;
2089                }
2090            }
2091            result = Some(layer_result);
2092        }
2093
2094        result
2095    }
2096
2097    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2098        self.outline_items_containing(0..self.len(), theme)
2099            .map(Outline::new)
2100    }
2101
2102    pub fn symbols_containing<T: ToOffset>(
2103        &self,
2104        position: T,
2105        theme: Option<&SyntaxTheme>,
2106    ) -> Option<Vec<OutlineItem<Anchor>>> {
2107        let position = position.to_offset(self);
2108        let mut items = self.outline_items_containing(
2109            position.saturating_sub(1)..self.len().min(position + 1),
2110            theme,
2111        )?;
2112        let mut prev_depth = None;
2113        items.retain(|item| {
2114            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2115            prev_depth = Some(item.depth);
2116            result
2117        });
2118        Some(items)
2119    }
2120
2121    fn outline_items_containing(
2122        &self,
2123        range: Range<usize>,
2124        theme: Option<&SyntaxTheme>,
2125    ) -> Option<Vec<OutlineItem<Anchor>>> {
2126        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2127            grammar.outline_config.as_ref().map(|c| &c.query)
2128        });
2129        let configs = matches
2130            .grammars()
2131            .iter()
2132            .map(|g| g.outline_config.as_ref().unwrap())
2133            .collect::<Vec<_>>();
2134
2135        let mut chunks = self.chunks(0..self.len(), true);
2136        let mut stack = Vec::<Range<usize>>::new();
2137        let mut items = Vec::new();
2138        while let Some(mat) = matches.peek() {
2139            let config = &configs[mat.grammar_index];
2140            let item_node = mat.captures.iter().find_map(|cap| {
2141                if cap.index == config.item_capture_ix {
2142                    Some(cap.node)
2143                } else {
2144                    None
2145                }
2146            })?;
2147
2148            let item_range = item_node.byte_range();
2149            if item_range.end < range.start || item_range.start > range.end {
2150                matches.advance();
2151                continue;
2152            }
2153
2154            // TODO - move later, after processing captures
2155
2156            let mut text = String::new();
2157            let mut name_ranges = Vec::new();
2158            let mut highlight_ranges = Vec::new();
2159            for capture in mat.captures {
2160                let node_is_name;
2161                if capture.index == config.name_capture_ix {
2162                    node_is_name = true;
2163                } else if Some(capture.index) == config.context_capture_ix {
2164                    node_is_name = false;
2165                } else {
2166                    continue;
2167                }
2168
2169                let range = capture.node.start_byte()..capture.node.end_byte();
2170                if !text.is_empty() {
2171                    text.push(' ');
2172                }
2173                if node_is_name {
2174                    let mut start = text.len();
2175                    let end = start + range.len();
2176
2177                    // When multiple names are captured, then the matcheable text
2178                    // includes the whitespace in between the names.
2179                    if !name_ranges.is_empty() {
2180                        start -= 1;
2181                    }
2182
2183                    name_ranges.push(start..end);
2184                }
2185
2186                let mut offset = range.start;
2187                chunks.seek(offset);
2188                for mut chunk in chunks.by_ref() {
2189                    if chunk.text.len() > range.end - offset {
2190                        chunk.text = &chunk.text[0..(range.end - offset)];
2191                        offset = range.end;
2192                    } else {
2193                        offset += chunk.text.len();
2194                    }
2195                    let style = chunk
2196                        .syntax_highlight_id
2197                        .zip(theme)
2198                        .and_then(|(highlight, theme)| highlight.style(theme));
2199                    if let Some(style) = style {
2200                        let start = text.len();
2201                        let end = start + chunk.text.len();
2202                        highlight_ranges.push((start..end, style));
2203                    }
2204                    text.push_str(chunk.text);
2205                    if offset >= range.end {
2206                        break;
2207                    }
2208                }
2209            }
2210
2211            matches.advance();
2212            while stack.last().map_or(false, |prev_range| {
2213                prev_range.start > item_range.start || prev_range.end < item_range.end
2214            }) {
2215                stack.pop();
2216            }
2217            stack.push(item_range.clone());
2218
2219            items.push(OutlineItem {
2220                depth: stack.len() - 1,
2221                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2222                text,
2223                highlight_ranges,
2224                name_ranges,
2225            })
2226        }
2227        Some(items)
2228    }
2229
2230    pub fn enclosing_bracket_ranges<T: ToOffset>(
2231        &self,
2232        range: Range<T>,
2233    ) -> Option<(Range<usize>, Range<usize>)> {
2234        // Find bracket pairs that *inclusively* contain the given range.
2235        let range = range.start.to_offset(self)..range.end.to_offset(self);
2236        let mut matches = self.syntax.matches(
2237            range.start.saturating_sub(1)..self.len().min(range.end + 1),
2238            &self.text,
2239            |grammar| grammar.brackets_config.as_ref().map(|c| &c.query),
2240        );
2241        let configs = matches
2242            .grammars()
2243            .iter()
2244            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2245            .collect::<Vec<_>>();
2246
2247        // Get the ranges of the innermost pair of brackets.
2248        let mut result: Option<(Range<usize>, Range<usize>)> = None;
2249        while let Some(mat) = matches.peek() {
2250            let mut open = None;
2251            let mut close = None;
2252            let config = &configs[mat.grammar_index];
2253            for capture in mat.captures {
2254                if capture.index == config.open_capture_ix {
2255                    open = Some(capture.node.byte_range());
2256                } else if capture.index == config.close_capture_ix {
2257                    close = Some(capture.node.byte_range());
2258                }
2259            }
2260
2261            matches.advance();
2262
2263            let Some((open, close)) = open.zip(close) else { continue };
2264            if open.start > range.start || close.end < range.end {
2265                continue;
2266            }
2267            let len = close.end - open.start;
2268
2269            if let Some((existing_open, existing_close)) = &result {
2270                let existing_len = existing_close.end - existing_open.start;
2271                if len > existing_len {
2272                    continue;
2273                }
2274            }
2275
2276            result = Some((open, close));
2277        }
2278
2279        result
2280    }
2281
2282    #[allow(clippy::type_complexity)]
2283    pub fn remote_selections_in_range(
2284        &self,
2285        range: Range<Anchor>,
2286    ) -> impl Iterator<
2287        Item = (
2288            ReplicaId,
2289            bool,
2290            CursorShape,
2291            impl Iterator<Item = &Selection<Anchor>> + '_,
2292        ),
2293    > + '_ {
2294        self.remote_selections
2295            .iter()
2296            .filter(|(replica_id, set)| {
2297                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2298            })
2299            .map(move |(replica_id, set)| {
2300                let start_ix = match set.selections.binary_search_by(|probe| {
2301                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2302                }) {
2303                    Ok(ix) | Err(ix) => ix,
2304                };
2305                let end_ix = match set.selections.binary_search_by(|probe| {
2306                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2307                }) {
2308                    Ok(ix) | Err(ix) => ix,
2309                };
2310
2311                (
2312                    *replica_id,
2313                    set.line_mode,
2314                    set.cursor_shape,
2315                    set.selections[start_ix..end_ix].iter(),
2316                )
2317            })
2318    }
2319
2320    pub fn git_diff_hunks_in_row_range<'a>(
2321        &'a self,
2322        range: Range<u32>,
2323        reversed: bool,
2324    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2325        self.git_diff.hunks_in_row_range(range, self, reversed)
2326    }
2327
2328    pub fn git_diff_hunks_intersecting_range<'a>(
2329        &'a self,
2330        range: Range<Anchor>,
2331        reversed: bool,
2332    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2333        self.git_diff
2334            .hunks_intersecting_range(range, self, reversed)
2335    }
2336
2337    pub fn diagnostics_in_range<'a, T, O>(
2338        &'a self,
2339        search_range: Range<T>,
2340        reversed: bool,
2341    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2342    where
2343        T: 'a + Clone + ToOffset,
2344        O: 'a + FromAnchor,
2345    {
2346        self.diagnostics.range(search_range, self, true, reversed)
2347    }
2348
2349    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2350        let mut groups = Vec::new();
2351        self.diagnostics.groups(&mut groups, self);
2352        groups
2353    }
2354
2355    pub fn diagnostic_group<'a, O>(
2356        &'a self,
2357        group_id: usize,
2358    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2359    where
2360        O: 'a + FromAnchor,
2361    {
2362        self.diagnostics.group(group_id, self)
2363    }
2364
2365    pub fn diagnostics_update_count(&self) -> usize {
2366        self.diagnostics_update_count
2367    }
2368
2369    pub fn parse_count(&self) -> usize {
2370        self.parse_count
2371    }
2372
2373    pub fn selections_update_count(&self) -> usize {
2374        self.selections_update_count
2375    }
2376
2377    pub fn file(&self) -> Option<&Arc<dyn File>> {
2378        self.file.as_ref()
2379    }
2380
2381    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2382        if let Some(file) = self.file() {
2383            if file.path().file_name().is_none() || include_root {
2384                Some(file.full_path(cx))
2385            } else {
2386                Some(file.path().to_path_buf())
2387            }
2388        } else {
2389            None
2390        }
2391    }
2392
2393    pub fn file_update_count(&self) -> usize {
2394        self.file_update_count
2395    }
2396
2397    pub fn git_diff_update_count(&self) -> usize {
2398        self.git_diff_update_count
2399    }
2400}
2401
2402fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2403    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2404}
2405
2406pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2407    let mut result = IndentSize::spaces(0);
2408    for c in text {
2409        let kind = match c {
2410            ' ' => IndentKind::Space,
2411            '\t' => IndentKind::Tab,
2412            _ => break,
2413        };
2414        if result.len == 0 {
2415            result.kind = kind;
2416        }
2417        result.len += 1;
2418    }
2419    result
2420}
2421
2422impl Clone for BufferSnapshot {
2423    fn clone(&self) -> Self {
2424        Self {
2425            text: self.text.clone(),
2426            git_diff: self.git_diff.clone(),
2427            syntax: self.syntax.clone(),
2428            file: self.file.clone(),
2429            remote_selections: self.remote_selections.clone(),
2430            diagnostics: self.diagnostics.clone(),
2431            selections_update_count: self.selections_update_count,
2432            diagnostics_update_count: self.diagnostics_update_count,
2433            file_update_count: self.file_update_count,
2434            git_diff_update_count: self.git_diff_update_count,
2435            language: self.language.clone(),
2436            parse_count: self.parse_count,
2437        }
2438    }
2439}
2440
2441impl Deref for BufferSnapshot {
2442    type Target = text::BufferSnapshot;
2443
2444    fn deref(&self) -> &Self::Target {
2445        &self.text
2446    }
2447}
2448
2449unsafe impl<'a> Send for BufferChunks<'a> {}
2450
2451impl<'a> BufferChunks<'a> {
2452    pub(crate) fn new(
2453        text: &'a Rope,
2454        range: Range<usize>,
2455        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2456        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2457    ) -> Self {
2458        let mut highlights = None;
2459        if let Some((captures, highlight_maps)) = syntax {
2460            highlights = Some(BufferChunkHighlights {
2461                captures,
2462                next_capture: None,
2463                stack: Default::default(),
2464                highlight_maps,
2465            })
2466        }
2467
2468        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2469        let chunks = text.chunks_in_range(range.clone());
2470
2471        BufferChunks {
2472            range,
2473            chunks,
2474            diagnostic_endpoints,
2475            error_depth: 0,
2476            warning_depth: 0,
2477            information_depth: 0,
2478            hint_depth: 0,
2479            unnecessary_depth: 0,
2480            highlights,
2481        }
2482    }
2483
2484    pub fn seek(&mut self, offset: usize) {
2485        self.range.start = offset;
2486        self.chunks.seek(self.range.start);
2487        if let Some(highlights) = self.highlights.as_mut() {
2488            highlights
2489                .stack
2490                .retain(|(end_offset, _)| *end_offset > offset);
2491            if let Some(capture) = &highlights.next_capture {
2492                if offset >= capture.node.start_byte() {
2493                    let next_capture_end = capture.node.end_byte();
2494                    if offset < next_capture_end {
2495                        highlights.stack.push((
2496                            next_capture_end,
2497                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2498                        ));
2499                    }
2500                    highlights.next_capture.take();
2501                }
2502            }
2503            highlights.captures.set_byte_range(self.range.clone());
2504        }
2505    }
2506
2507    pub fn offset(&self) -> usize {
2508        self.range.start
2509    }
2510
2511    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2512        let depth = match endpoint.severity {
2513            DiagnosticSeverity::ERROR => &mut self.error_depth,
2514            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2515            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2516            DiagnosticSeverity::HINT => &mut self.hint_depth,
2517            _ => return,
2518        };
2519        if endpoint.is_start {
2520            *depth += 1;
2521        } else {
2522            *depth -= 1;
2523        }
2524
2525        if endpoint.is_unnecessary {
2526            if endpoint.is_start {
2527                self.unnecessary_depth += 1;
2528            } else {
2529                self.unnecessary_depth -= 1;
2530            }
2531        }
2532    }
2533
2534    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2535        if self.error_depth > 0 {
2536            Some(DiagnosticSeverity::ERROR)
2537        } else if self.warning_depth > 0 {
2538            Some(DiagnosticSeverity::WARNING)
2539        } else if self.information_depth > 0 {
2540            Some(DiagnosticSeverity::INFORMATION)
2541        } else if self.hint_depth > 0 {
2542            Some(DiagnosticSeverity::HINT)
2543        } else {
2544            None
2545        }
2546    }
2547
2548    fn current_code_is_unnecessary(&self) -> bool {
2549        self.unnecessary_depth > 0
2550    }
2551}
2552
2553impl<'a> Iterator for BufferChunks<'a> {
2554    type Item = Chunk<'a>;
2555
2556    fn next(&mut self) -> Option<Self::Item> {
2557        let mut next_capture_start = usize::MAX;
2558        let mut next_diagnostic_endpoint = usize::MAX;
2559
2560        if let Some(highlights) = self.highlights.as_mut() {
2561            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2562                if *parent_capture_end <= self.range.start {
2563                    highlights.stack.pop();
2564                } else {
2565                    break;
2566                }
2567            }
2568
2569            if highlights.next_capture.is_none() {
2570                highlights.next_capture = highlights.captures.next();
2571            }
2572
2573            while let Some(capture) = highlights.next_capture.as_ref() {
2574                if self.range.start < capture.node.start_byte() {
2575                    next_capture_start = capture.node.start_byte();
2576                    break;
2577                } else {
2578                    let highlight_id =
2579                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2580                    highlights
2581                        .stack
2582                        .push((capture.node.end_byte(), highlight_id));
2583                    highlights.next_capture = highlights.captures.next();
2584                }
2585            }
2586        }
2587
2588        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2589            if endpoint.offset <= self.range.start {
2590                self.update_diagnostic_depths(endpoint);
2591                self.diagnostic_endpoints.next();
2592            } else {
2593                next_diagnostic_endpoint = endpoint.offset;
2594                break;
2595            }
2596        }
2597
2598        if let Some(chunk) = self.chunks.peek() {
2599            let chunk_start = self.range.start;
2600            let mut chunk_end = (self.chunks.offset() + chunk.len())
2601                .min(next_capture_start)
2602                .min(next_diagnostic_endpoint);
2603            let mut highlight_id = None;
2604            if let Some(highlights) = self.highlights.as_ref() {
2605                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2606                    chunk_end = chunk_end.min(*parent_capture_end);
2607                    highlight_id = Some(*parent_highlight_id);
2608                }
2609            }
2610
2611            let slice =
2612                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2613            self.range.start = chunk_end;
2614            if self.range.start == self.chunks.offset() + chunk.len() {
2615                self.chunks.next().unwrap();
2616            }
2617
2618            Some(Chunk {
2619                text: slice,
2620                syntax_highlight_id: highlight_id,
2621                highlight_style: None,
2622                diagnostic_severity: self.current_diagnostic_severity(),
2623                is_unnecessary: self.current_code_is_unnecessary(),
2624            })
2625        } else {
2626            None
2627        }
2628    }
2629}
2630
2631impl operation_queue::Operation for Operation {
2632    fn lamport_timestamp(&self) -> clock::Lamport {
2633        match self {
2634            Operation::Buffer(_) => {
2635                unreachable!("buffer operations should never be deferred at this layer")
2636            }
2637            Operation::UpdateDiagnostics {
2638                lamport_timestamp, ..
2639            }
2640            | Operation::UpdateSelections {
2641                lamport_timestamp, ..
2642            }
2643            | Operation::UpdateCompletionTriggers {
2644                lamport_timestamp, ..
2645            } => *lamport_timestamp,
2646        }
2647    }
2648}
2649
2650impl Default for Diagnostic {
2651    fn default() -> Self {
2652        Self {
2653            code: None,
2654            severity: DiagnosticSeverity::ERROR,
2655            message: Default::default(),
2656            group_id: 0,
2657            is_primary: false,
2658            is_valid: true,
2659            is_disk_based: false,
2660            is_unnecessary: false,
2661        }
2662    }
2663}
2664
2665impl IndentSize {
2666    pub fn spaces(len: u32) -> Self {
2667        Self {
2668            len,
2669            kind: IndentKind::Space,
2670        }
2671    }
2672
2673    pub fn tab() -> Self {
2674        Self {
2675            len: 1,
2676            kind: IndentKind::Tab,
2677        }
2678    }
2679
2680    pub fn chars(&self) -> impl Iterator<Item = char> {
2681        iter::repeat(self.char()).take(self.len as usize)
2682    }
2683
2684    pub fn char(&self) -> char {
2685        match self.kind {
2686            IndentKind::Space => ' ',
2687            IndentKind::Tab => '\t',
2688        }
2689    }
2690
2691    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2692        match direction {
2693            Ordering::Less => {
2694                if self.kind == size.kind && self.len >= size.len {
2695                    self.len -= size.len;
2696                }
2697            }
2698            Ordering::Equal => {}
2699            Ordering::Greater => {
2700                if self.len == 0 {
2701                    self = size;
2702                } else if self.kind == size.kind {
2703                    self.len += size.len;
2704                }
2705            }
2706        }
2707        self
2708    }
2709}
2710
2711impl Completion {
2712    pub fn sort_key(&self) -> (usize, &str) {
2713        let kind_key = match self.lsp_completion.kind {
2714            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2715            _ => 1,
2716        };
2717        (kind_key, &self.label.text[self.label.filter_range.clone()])
2718    }
2719
2720    pub fn is_snippet(&self) -> bool {
2721        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2722    }
2723}
2724
2725pub fn contiguous_ranges(
2726    values: impl Iterator<Item = u32>,
2727    max_len: usize,
2728) -> impl Iterator<Item = Range<u32>> {
2729    let mut values = values;
2730    let mut current_range: Option<Range<u32>> = None;
2731    std::iter::from_fn(move || loop {
2732        if let Some(value) = values.next() {
2733            if let Some(range) = &mut current_range {
2734                if value == range.end && range.len() < max_len {
2735                    range.end += 1;
2736                    continue;
2737                }
2738            }
2739
2740            let prev_range = current_range.clone();
2741            current_range = Some(value..(value + 1));
2742            if prev_range.is_some() {
2743                return prev_range;
2744            }
2745        } else {
2746            return current_range.take();
2747        }
2748    })
2749}
2750
2751pub fn char_kind(c: char) -> CharKind {
2752    if c.is_whitespace() {
2753        CharKind::Whitespace
2754    } else if c.is_alphanumeric() || c == '_' {
2755        CharKind::Word
2756    } else {
2757        CharKind::Punctuation
2758    }
2759}