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    #[cfg(any(test, feature = "test-support"))]
 686    pub fn diff_base(&self) -> Option<&str> {
 687        self.diff_base.as_deref()
 688    }
 689
 690    pub fn set_diff_base(&mut self, diff_base: Option<String>, cx: &mut ModelContext<Self>) {
 691        self.diff_base = diff_base;
 692        self.git_diff_recalc(cx);
 693    }
 694
 695    pub fn needs_git_diff_recalc(&self) -> bool {
 696        self.git_diff_status.diff.needs_update(self)
 697    }
 698
 699    pub fn git_diff_recalc(&mut self, cx: &mut ModelContext<Self>) {
 700        if self.git_diff_status.update_in_progress {
 701            self.git_diff_status.update_requested = true;
 702            return;
 703        }
 704
 705        if let Some(diff_base) = &self.diff_base {
 706            let snapshot = self.snapshot();
 707            let diff_base = diff_base.clone();
 708
 709            let mut diff = self.git_diff_status.diff.clone();
 710            let diff = cx.background().spawn(async move {
 711                diff.update(&diff_base, &snapshot).await;
 712                diff
 713            });
 714
 715            cx.spawn_weak(|this, mut cx| async move {
 716                let buffer_diff = diff.await;
 717                if let Some(this) = this.upgrade(&cx) {
 718                    this.update(&mut cx, |this, cx| {
 719                        this.git_diff_status.diff = buffer_diff;
 720                        this.git_diff_update_count += 1;
 721                        cx.notify();
 722
 723                        this.git_diff_status.update_in_progress = false;
 724                        if this.git_diff_status.update_requested {
 725                            this.git_diff_recalc(cx);
 726                        }
 727                    })
 728                }
 729            })
 730            .detach()
 731        } else {
 732            let snapshot = self.snapshot();
 733            self.git_diff_status.diff.clear(&snapshot);
 734            self.git_diff_update_count += 1;
 735            cx.notify();
 736        }
 737    }
 738
 739    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 740        cx.emit(Event::Closed);
 741    }
 742
 743    pub fn language(&self) -> Option<&Arc<Language>> {
 744        self.language.as_ref()
 745    }
 746
 747    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
 748        let offset = position.to_offset(self);
 749        self.syntax_map
 750            .lock()
 751            .layers_for_range(offset..offset, &self.text)
 752            .last()
 753            .map(|info| info.language.clone())
 754            .or_else(|| self.language.clone())
 755    }
 756
 757    pub fn parse_count(&self) -> usize {
 758        self.parse_count
 759    }
 760
 761    pub fn selections_update_count(&self) -> usize {
 762        self.selections_update_count
 763    }
 764
 765    pub fn diagnostics_update_count(&self) -> usize {
 766        self.diagnostics_update_count
 767    }
 768
 769    pub fn file_update_count(&self) -> usize {
 770        self.file_update_count
 771    }
 772
 773    pub fn git_diff_update_count(&self) -> usize {
 774        self.git_diff_update_count
 775    }
 776
 777    #[cfg(any(test, feature = "test-support"))]
 778    pub fn is_parsing(&self) -> bool {
 779        self.parsing_in_background
 780    }
 781
 782    #[cfg(test)]
 783    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 784        self.sync_parse_timeout = timeout;
 785    }
 786
 787    fn reparse(&mut self, cx: &mut ModelContext<Self>) {
 788        if self.parsing_in_background {
 789            return;
 790        }
 791        let language = if let Some(language) = self.language.clone() {
 792            language
 793        } else {
 794            return;
 795        };
 796
 797        let text = self.text_snapshot();
 798        let parsed_version = self.version();
 799
 800        let mut syntax_map = self.syntax_map.lock();
 801        syntax_map.interpolate(&text);
 802        let language_registry = syntax_map.language_registry();
 803        let mut syntax_snapshot = syntax_map.snapshot();
 804        let syntax_map_version = syntax_map.parsed_version();
 805        drop(syntax_map);
 806
 807        let parse_task = cx.background().spawn({
 808            let language = language.clone();
 809            async move {
 810                syntax_snapshot.reparse(&syntax_map_version, &text, language_registry, language);
 811                syntax_snapshot
 812            }
 813        });
 814
 815        match cx
 816            .background()
 817            .block_with_timeout(self.sync_parse_timeout, parse_task)
 818        {
 819            Ok(new_syntax_snapshot) => {
 820                self.did_finish_parsing(new_syntax_snapshot, parsed_version, cx);
 821                return;
 822            }
 823            Err(parse_task) => {
 824                self.parsing_in_background = true;
 825                cx.spawn(move |this, mut cx| async move {
 826                    let new_syntax_map = parse_task.await;
 827                    this.update(&mut cx, move |this, cx| {
 828                        let grammar_changed =
 829                            this.language.as_ref().map_or(true, |current_language| {
 830                                !Arc::ptr_eq(&language, current_language)
 831                            });
 832                        let parse_again =
 833                            this.version.changed_since(&parsed_version) || grammar_changed;
 834                        this.did_finish_parsing(new_syntax_map, parsed_version, cx);
 835                        this.parsing_in_background = false;
 836                        if parse_again {
 837                            this.reparse(cx);
 838                        }
 839                    });
 840                })
 841                .detach();
 842            }
 843        }
 844    }
 845
 846    fn did_finish_parsing(
 847        &mut self,
 848        syntax_snapshot: SyntaxSnapshot,
 849        version: clock::Global,
 850        cx: &mut ModelContext<Self>,
 851    ) {
 852        self.parse_count += 1;
 853        self.syntax_map.lock().did_parse(syntax_snapshot, version);
 854        self.request_autoindent(cx);
 855        cx.emit(Event::Reparsed);
 856        cx.notify();
 857    }
 858
 859    pub fn update_diagnostics(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
 860        let lamport_timestamp = self.text.lamport_clock.tick();
 861        let op = Operation::UpdateDiagnostics {
 862            diagnostics: diagnostics.iter().cloned().collect(),
 863            lamport_timestamp,
 864        };
 865        self.apply_diagnostic_update(diagnostics, lamport_timestamp, cx);
 866        self.send_operation(op, cx);
 867    }
 868
 869    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 870        if let Some(indent_sizes) = self.compute_autoindents() {
 871            let indent_sizes = cx.background().spawn(indent_sizes);
 872            match cx
 873                .background()
 874                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 875            {
 876                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
 877                Err(indent_sizes) => {
 878                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 879                        let indent_sizes = indent_sizes.await;
 880                        this.update(&mut cx, |this, cx| {
 881                            this.apply_autoindents(indent_sizes, cx);
 882                        });
 883                    }));
 884                }
 885            }
 886        } else {
 887            self.autoindent_requests.clear();
 888        }
 889    }
 890
 891    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
 892        let max_rows_between_yields = 100;
 893        let snapshot = self.snapshot();
 894        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
 895            return None;
 896        }
 897
 898        let autoindent_requests = self.autoindent_requests.clone();
 899        Some(async move {
 900            let mut indent_sizes = BTreeMap::new();
 901            for request in autoindent_requests {
 902                // Resolve each edited range to its row in the current buffer and in the
 903                // buffer before this batch of edits.
 904                let mut row_ranges = Vec::new();
 905                let mut old_to_new_rows = BTreeMap::new();
 906                let mut language_indent_sizes_by_new_row = Vec::new();
 907                for entry in &request.entries {
 908                    let position = entry.range.start;
 909                    let new_row = position.to_point(&snapshot).row;
 910                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
 911                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
 912
 913                    if !entry.first_line_is_new {
 914                        let old_row = position.to_point(&request.before_edit).row;
 915                        old_to_new_rows.insert(old_row, new_row);
 916                    }
 917                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
 918                }
 919
 920                // Build a map containing the suggested indentation for each of the edited lines
 921                // with respect to the state of the buffer before these edits. This map is keyed
 922                // by the rows for these lines in the current state of the buffer.
 923                let mut old_suggestions = BTreeMap::<u32, IndentSize>::default();
 924                let old_edited_ranges =
 925                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 926                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 927                let mut language_indent_size = IndentSize::default();
 928                for old_edited_range in old_edited_ranges {
 929                    let suggestions = request
 930                        .before_edit
 931                        .suggest_autoindents(old_edited_range.clone())
 932                        .into_iter()
 933                        .flatten();
 934                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 935                        if let Some(suggestion) = suggestion {
 936                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
 937
 938                            // Find the indent size based on the language for this row.
 939                            while let Some((row, size)) = language_indent_sizes.peek() {
 940                                if *row > new_row {
 941                                    break;
 942                                }
 943                                language_indent_size = *size;
 944                                language_indent_sizes.next();
 945                            }
 946
 947                            let suggested_indent = old_to_new_rows
 948                                .get(&suggestion.basis_row)
 949                                .and_then(|from_row| old_suggestions.get(from_row).copied())
 950                                .unwrap_or_else(|| {
 951                                    request
 952                                        .before_edit
 953                                        .indent_size_for_line(suggestion.basis_row)
 954                                })
 955                                .with_delta(suggestion.delta, language_indent_size);
 956                            old_suggestions.insert(new_row, suggested_indent);
 957                        }
 958                    }
 959                    yield_now().await;
 960                }
 961
 962                // In block mode, only compute indentation suggestions for the first line
 963                // of each insertion. Otherwise, compute suggestions for every inserted line.
 964                let new_edited_row_ranges = contiguous_ranges(
 965                    row_ranges.iter().flat_map(|(range, _)| {
 966                        if request.is_block_mode {
 967                            range.start..range.start + 1
 968                        } else {
 969                            range.clone()
 970                        }
 971                    }),
 972                    max_rows_between_yields,
 973                );
 974
 975                // Compute new suggestions for each line, but only include them in the result
 976                // if they differ from the old suggestion for that line.
 977                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 978                let mut language_indent_size = IndentSize::default();
 979                for new_edited_row_range in new_edited_row_ranges {
 980                    let suggestions = snapshot
 981                        .suggest_autoindents(new_edited_row_range.clone())
 982                        .into_iter()
 983                        .flatten();
 984                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 985                        if let Some(suggestion) = suggestion {
 986                            // Find the indent size based on the language for this row.
 987                            while let Some((row, size)) = language_indent_sizes.peek() {
 988                                if *row > new_row {
 989                                    break;
 990                                }
 991                                language_indent_size = *size;
 992                                language_indent_sizes.next();
 993                            }
 994
 995                            let suggested_indent = indent_sizes
 996                                .get(&suggestion.basis_row)
 997                                .copied()
 998                                .unwrap_or_else(|| {
 999                                    snapshot.indent_size_for_line(suggestion.basis_row)
1000                                })
1001                                .with_delta(suggestion.delta, language_indent_size);
1002                            if old_suggestions
1003                                .get(&new_row)
1004                                .map_or(true, |old_indentation| {
1005                                    suggested_indent != *old_indentation
1006                                })
1007                            {
1008                                indent_sizes.insert(new_row, suggested_indent);
1009                            }
1010                        }
1011                    }
1012                    yield_now().await;
1013                }
1014
1015                // For each block of inserted text, adjust the indentation of the remaining
1016                // lines of the block by the same amount as the first line was adjusted.
1017                if request.is_block_mode {
1018                    for (row_range, original_indent_column) in
1019                        row_ranges
1020                            .into_iter()
1021                            .filter_map(|(range, original_indent_column)| {
1022                                if range.len() > 1 {
1023                                    Some((range, original_indent_column?))
1024                                } else {
1025                                    None
1026                                }
1027                            })
1028                    {
1029                        let new_indent = indent_sizes
1030                            .get(&row_range.start)
1031                            .copied()
1032                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
1033                        let delta = new_indent.len as i64 - original_indent_column as i64;
1034                        if delta != 0 {
1035                            for row in row_range.skip(1) {
1036                                indent_sizes.entry(row).or_insert_with(|| {
1037                                    let mut size = snapshot.indent_size_for_line(row);
1038                                    if size.kind == new_indent.kind {
1039                                        match delta.cmp(&0) {
1040                                            Ordering::Greater => size.len += delta as u32,
1041                                            Ordering::Less => {
1042                                                size.len = size.len.saturating_sub(-delta as u32)
1043                                            }
1044                                            Ordering::Equal => {}
1045                                        }
1046                                    }
1047                                    size
1048                                });
1049                            }
1050                        }
1051                    }
1052                }
1053            }
1054
1055            indent_sizes
1056        })
1057    }
1058
1059    fn apply_autoindents(
1060        &mut self,
1061        indent_sizes: BTreeMap<u32, IndentSize>,
1062        cx: &mut ModelContext<Self>,
1063    ) {
1064        self.autoindent_requests.clear();
1065
1066        let edits: Vec<_> = indent_sizes
1067            .into_iter()
1068            .filter_map(|(row, indent_size)| {
1069                let current_size = indent_size_for_line(self, row);
1070                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
1071            })
1072            .collect();
1073
1074        self.edit(edits, None, cx);
1075    }
1076
1077    // Create a minimal edit that will cause the the given row to be indented
1078    // with the given size. After applying this edit, the length of the line
1079    // will always be at least `new_size.len`.
1080    pub fn edit_for_indent_size_adjustment(
1081        row: u32,
1082        current_size: IndentSize,
1083        new_size: IndentSize,
1084    ) -> Option<(Range<Point>, String)> {
1085        if new_size.kind != current_size.kind {
1086            Some((
1087                Point::new(row, 0)..Point::new(row, current_size.len),
1088                iter::repeat(new_size.char())
1089                    .take(new_size.len as usize)
1090                    .collect::<String>(),
1091            ))
1092        } else {
1093            match new_size.len.cmp(&current_size.len) {
1094                Ordering::Greater => {
1095                    let point = Point::new(row, 0);
1096                    Some((
1097                        point..point,
1098                        iter::repeat(new_size.char())
1099                            .take((new_size.len - current_size.len) as usize)
1100                            .collect::<String>(),
1101                    ))
1102                }
1103
1104                Ordering::Less => Some((
1105                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
1106                    String::new(),
1107                )),
1108
1109                Ordering::Equal => None,
1110            }
1111        }
1112    }
1113
1114    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
1115        let old_text = self.as_rope().clone();
1116        let base_version = self.version();
1117        cx.background().spawn(async move {
1118            let old_text = old_text.to_string();
1119            let line_ending = LineEnding::detect(&new_text);
1120            LineEnding::normalize(&mut new_text);
1121            let diff = TextDiff::from_chars(old_text.as_str(), new_text.as_str());
1122            let mut edits = Vec::new();
1123            let mut offset = 0;
1124            let empty: Arc<str> = "".into();
1125            for change in diff.iter_all_changes() {
1126                let value = change.value();
1127                let end_offset = offset + value.len();
1128                match change.tag() {
1129                    ChangeTag::Equal => {
1130                        offset = end_offset;
1131                    }
1132                    ChangeTag::Delete => {
1133                        edits.push((offset..end_offset, empty.clone()));
1134                        offset = end_offset;
1135                    }
1136                    ChangeTag::Insert => {
1137                        edits.push((offset..offset, value.into()));
1138                    }
1139                }
1140            }
1141            Diff {
1142                base_version,
1143                line_ending,
1144                edits,
1145            }
1146        })
1147    }
1148
1149    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<&Transaction> {
1150        if self.version == diff.base_version {
1151            self.finalize_last_transaction();
1152            self.start_transaction();
1153            self.text.set_line_ending(diff.line_ending);
1154            self.edit(diff.edits, None, cx);
1155            if self.end_transaction(cx).is_some() {
1156                self.finalize_last_transaction()
1157            } else {
1158                None
1159            }
1160        } else {
1161            None
1162        }
1163    }
1164
1165    pub fn is_dirty(&self) -> bool {
1166        self.saved_version_fingerprint != self.as_rope().fingerprint()
1167            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1168    }
1169
1170    pub fn has_conflict(&self) -> bool {
1171        self.saved_version_fingerprint != self.as_rope().fingerprint()
1172            && self
1173                .file
1174                .as_ref()
1175                .map_or(false, |file| file.mtime() > self.saved_mtime)
1176    }
1177
1178    pub fn subscribe(&mut self) -> Subscription {
1179        self.text.subscribe()
1180    }
1181
1182    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1183        self.start_transaction_at(Instant::now())
1184    }
1185
1186    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1187        self.transaction_depth += 1;
1188        if self.was_dirty_before_starting_transaction.is_none() {
1189            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1190        }
1191        self.text.start_transaction_at(now)
1192    }
1193
1194    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1195        self.end_transaction_at(Instant::now(), cx)
1196    }
1197
1198    pub fn end_transaction_at(
1199        &mut self,
1200        now: Instant,
1201        cx: &mut ModelContext<Self>,
1202    ) -> Option<TransactionId> {
1203        assert!(self.transaction_depth > 0);
1204        self.transaction_depth -= 1;
1205        let was_dirty = if self.transaction_depth == 0 {
1206            self.was_dirty_before_starting_transaction.take().unwrap()
1207        } else {
1208            false
1209        };
1210        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1211            self.did_edit(&start_version, was_dirty, cx);
1212            Some(transaction_id)
1213        } else {
1214            None
1215        }
1216    }
1217
1218    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1219        self.text.push_transaction(transaction, now);
1220    }
1221
1222    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1223        self.text.finalize_last_transaction()
1224    }
1225
1226    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1227        self.text.group_until_transaction(transaction_id);
1228    }
1229
1230    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1231        self.text.forget_transaction(transaction_id);
1232    }
1233
1234    pub fn wait_for_edits(
1235        &mut self,
1236        edit_ids: impl IntoIterator<Item = clock::Local>,
1237    ) -> impl Future<Output = ()> {
1238        self.text.wait_for_edits(edit_ids)
1239    }
1240
1241    pub fn wait_for_anchors<'a>(
1242        &mut self,
1243        anchors: impl IntoIterator<Item = &'a Anchor>,
1244    ) -> impl Future<Output = ()> {
1245        self.text.wait_for_anchors(anchors)
1246    }
1247
1248    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1249        self.text.wait_for_version(version)
1250    }
1251
1252    pub fn set_active_selections(
1253        &mut self,
1254        selections: Arc<[Selection<Anchor>]>,
1255        line_mode: bool,
1256        cursor_shape: CursorShape,
1257        cx: &mut ModelContext<Self>,
1258    ) {
1259        let lamport_timestamp = self.text.lamport_clock.tick();
1260        self.remote_selections.insert(
1261            self.text.replica_id(),
1262            SelectionSet {
1263                selections: selections.clone(),
1264                lamport_timestamp,
1265                line_mode,
1266                cursor_shape,
1267            },
1268        );
1269        self.send_operation(
1270            Operation::UpdateSelections {
1271                selections,
1272                line_mode,
1273                lamport_timestamp,
1274                cursor_shape,
1275            },
1276            cx,
1277        );
1278    }
1279
1280    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1281        self.set_active_selections(Arc::from([]), false, Default::default(), cx);
1282    }
1283
1284    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
1285    where
1286        T: Into<Arc<str>>,
1287    {
1288        self.edit([(0..self.len(), text)], None, cx)
1289    }
1290
1291    pub fn edit<I, S, T>(
1292        &mut self,
1293        edits_iter: I,
1294        autoindent_mode: Option<AutoindentMode>,
1295        cx: &mut ModelContext<Self>,
1296    ) -> Option<clock::Local>
1297    where
1298        I: IntoIterator<Item = (Range<S>, T)>,
1299        S: ToOffset,
1300        T: Into<Arc<str>>,
1301    {
1302        // Skip invalid edits and coalesce contiguous ones.
1303        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1304        for (range, new_text) in edits_iter {
1305            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1306            if range.start > range.end {
1307                mem::swap(&mut range.start, &mut range.end);
1308            }
1309            let new_text = new_text.into();
1310            if !new_text.is_empty() || !range.is_empty() {
1311                if let Some((prev_range, prev_text)) = edits.last_mut() {
1312                    if prev_range.end >= range.start {
1313                        prev_range.end = cmp::max(prev_range.end, range.end);
1314                        *prev_text = format!("{prev_text}{new_text}").into();
1315                    } else {
1316                        edits.push((range, new_text));
1317                    }
1318                } else {
1319                    edits.push((range, new_text));
1320                }
1321            }
1322        }
1323        if edits.is_empty() {
1324            return None;
1325        }
1326
1327        self.start_transaction();
1328        self.pending_autoindent.take();
1329        let autoindent_request = autoindent_mode
1330            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1331
1332        let edit_operation = self.text.edit(edits.iter().cloned());
1333        let edit_id = edit_operation.local_timestamp();
1334
1335        if let Some((before_edit, mode)) = autoindent_request {
1336            let (start_columns, is_block_mode) = match mode {
1337                AutoindentMode::Block {
1338                    original_indent_columns: start_columns,
1339                } => (start_columns, true),
1340                AutoindentMode::EachLine => (Default::default(), false),
1341            };
1342
1343            let mut delta = 0isize;
1344            let entries = edits
1345                .into_iter()
1346                .enumerate()
1347                .zip(&edit_operation.as_edit().unwrap().new_text)
1348                .map(|((ix, (range, _)), new_text)| {
1349                    let new_text_len = new_text.len();
1350                    let old_start = range.start.to_point(&before_edit);
1351                    let new_start = (delta + range.start as isize) as usize;
1352                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1353
1354                    let mut range_of_insertion_to_indent = 0..new_text_len;
1355                    let mut first_line_is_new = false;
1356                    let mut start_column = None;
1357
1358                    // When inserting an entire line at the beginning of an existing line,
1359                    // treat the insertion as new.
1360                    if new_text.contains('\n')
1361                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1362                    {
1363                        first_line_is_new = true;
1364                    }
1365
1366                    // When inserting text starting with a newline, avoid auto-indenting the
1367                    // previous line.
1368                    if new_text[range_of_insertion_to_indent.clone()].starts_with('\n') {
1369                        range_of_insertion_to_indent.start += 1;
1370                        first_line_is_new = true;
1371                    }
1372
1373                    // Avoid auto-indenting after the insertion.
1374                    if is_block_mode {
1375                        start_column = start_columns.get(ix).copied();
1376                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1377                            range_of_insertion_to_indent.end -= 1;
1378                        }
1379                    }
1380
1381                    AutoindentRequestEntry {
1382                        first_line_is_new,
1383                        original_indent_column: start_column,
1384                        indent_size: before_edit.language_indent_size_at(range.start, cx),
1385                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1386                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1387                    }
1388                })
1389                .collect();
1390
1391            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1392                before_edit,
1393                entries,
1394                is_block_mode,
1395            }));
1396        }
1397
1398        self.end_transaction(cx);
1399        self.send_operation(Operation::Buffer(edit_operation), cx);
1400        Some(edit_id)
1401    }
1402
1403    fn did_edit(
1404        &mut self,
1405        old_version: &clock::Global,
1406        was_dirty: bool,
1407        cx: &mut ModelContext<Self>,
1408    ) {
1409        if self.edits_since::<usize>(old_version).next().is_none() {
1410            return;
1411        }
1412
1413        self.reparse(cx);
1414
1415        cx.emit(Event::Edited);
1416        if was_dirty != self.is_dirty() {
1417            cx.emit(Event::DirtyChanged);
1418        }
1419        cx.notify();
1420    }
1421
1422    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1423        &mut self,
1424        ops: I,
1425        cx: &mut ModelContext<Self>,
1426    ) -> Result<()> {
1427        self.pending_autoindent.take();
1428        let was_dirty = self.is_dirty();
1429        let old_version = self.version.clone();
1430        let mut deferred_ops = Vec::new();
1431        let buffer_ops = ops
1432            .into_iter()
1433            .filter_map(|op| match op {
1434                Operation::Buffer(op) => Some(op),
1435                _ => {
1436                    if self.can_apply_op(&op) {
1437                        self.apply_op(op, cx);
1438                    } else {
1439                        deferred_ops.push(op);
1440                    }
1441                    None
1442                }
1443            })
1444            .collect::<Vec<_>>();
1445        self.text.apply_ops(buffer_ops)?;
1446        self.deferred_ops.insert(deferred_ops);
1447        self.flush_deferred_ops(cx);
1448        self.did_edit(&old_version, was_dirty, cx);
1449        // Notify independently of whether the buffer was edited as the operations could include a
1450        // selection update.
1451        cx.notify();
1452        Ok(())
1453    }
1454
1455    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1456        let mut deferred_ops = Vec::new();
1457        for op in self.deferred_ops.drain().iter().cloned() {
1458            if self.can_apply_op(&op) {
1459                self.apply_op(op, cx);
1460            } else {
1461                deferred_ops.push(op);
1462            }
1463        }
1464        self.deferred_ops.insert(deferred_ops);
1465    }
1466
1467    fn can_apply_op(&self, operation: &Operation) -> bool {
1468        match operation {
1469            Operation::Buffer(_) => {
1470                unreachable!("buffer operations should never be applied at this layer")
1471            }
1472            Operation::UpdateDiagnostics {
1473                diagnostics: diagnostic_set,
1474                ..
1475            } => diagnostic_set.iter().all(|diagnostic| {
1476                self.text.can_resolve(&diagnostic.range.start)
1477                    && self.text.can_resolve(&diagnostic.range.end)
1478            }),
1479            Operation::UpdateSelections { selections, .. } => selections
1480                .iter()
1481                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1482            Operation::UpdateCompletionTriggers { .. } => true,
1483        }
1484    }
1485
1486    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1487        match operation {
1488            Operation::Buffer(_) => {
1489                unreachable!("buffer operations should never be applied at this layer")
1490            }
1491            Operation::UpdateDiagnostics {
1492                diagnostics: diagnostic_set,
1493                lamport_timestamp,
1494            } => {
1495                let snapshot = self.snapshot();
1496                self.apply_diagnostic_update(
1497                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1498                    lamport_timestamp,
1499                    cx,
1500                );
1501            }
1502            Operation::UpdateSelections {
1503                selections,
1504                lamport_timestamp,
1505                line_mode,
1506                cursor_shape,
1507            } => {
1508                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1509                    if set.lamport_timestamp > lamport_timestamp {
1510                        return;
1511                    }
1512                }
1513
1514                self.remote_selections.insert(
1515                    lamport_timestamp.replica_id,
1516                    SelectionSet {
1517                        selections,
1518                        lamport_timestamp,
1519                        line_mode,
1520                        cursor_shape,
1521                    },
1522                );
1523                self.text.lamport_clock.observe(lamport_timestamp);
1524                self.selections_update_count += 1;
1525            }
1526            Operation::UpdateCompletionTriggers {
1527                triggers,
1528                lamport_timestamp,
1529            } => {
1530                self.completion_triggers = triggers;
1531                self.text.lamport_clock.observe(lamport_timestamp);
1532            }
1533        }
1534    }
1535
1536    fn apply_diagnostic_update(
1537        &mut self,
1538        diagnostics: DiagnosticSet,
1539        lamport_timestamp: clock::Lamport,
1540        cx: &mut ModelContext<Self>,
1541    ) {
1542        if lamport_timestamp > self.diagnostics_timestamp {
1543            self.diagnostics = diagnostics;
1544            self.diagnostics_timestamp = lamport_timestamp;
1545            self.diagnostics_update_count += 1;
1546            self.text.lamport_clock.observe(lamport_timestamp);
1547            cx.notify();
1548            cx.emit(Event::DiagnosticsUpdated);
1549        }
1550    }
1551
1552    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1553        cx.emit(Event::Operation(operation));
1554    }
1555
1556    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1557        self.remote_selections.remove(&replica_id);
1558        cx.notify();
1559    }
1560
1561    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1562        let was_dirty = self.is_dirty();
1563        let old_version = self.version.clone();
1564
1565        if let Some((transaction_id, operation)) = self.text.undo() {
1566            self.send_operation(Operation::Buffer(operation), cx);
1567            self.did_edit(&old_version, was_dirty, cx);
1568            Some(transaction_id)
1569        } else {
1570            None
1571        }
1572    }
1573
1574    pub fn undo_to_transaction(
1575        &mut self,
1576        transaction_id: TransactionId,
1577        cx: &mut ModelContext<Self>,
1578    ) -> bool {
1579        let was_dirty = self.is_dirty();
1580        let old_version = self.version.clone();
1581
1582        let operations = self.text.undo_to_transaction(transaction_id);
1583        let undone = !operations.is_empty();
1584        for operation in operations {
1585            self.send_operation(Operation::Buffer(operation), cx);
1586        }
1587        if undone {
1588            self.did_edit(&old_version, was_dirty, cx)
1589        }
1590        undone
1591    }
1592
1593    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1594        let was_dirty = self.is_dirty();
1595        let old_version = self.version.clone();
1596
1597        if let Some((transaction_id, operation)) = self.text.redo() {
1598            self.send_operation(Operation::Buffer(operation), cx);
1599            self.did_edit(&old_version, was_dirty, cx);
1600            Some(transaction_id)
1601        } else {
1602            None
1603        }
1604    }
1605
1606    pub fn redo_to_transaction(
1607        &mut self,
1608        transaction_id: TransactionId,
1609        cx: &mut ModelContext<Self>,
1610    ) -> bool {
1611        let was_dirty = self.is_dirty();
1612        let old_version = self.version.clone();
1613
1614        let operations = self.text.redo_to_transaction(transaction_id);
1615        let redone = !operations.is_empty();
1616        for operation in operations {
1617            self.send_operation(Operation::Buffer(operation), cx);
1618        }
1619        if redone {
1620            self.did_edit(&old_version, was_dirty, cx)
1621        }
1622        redone
1623    }
1624
1625    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1626        self.completion_triggers = triggers.clone();
1627        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1628        self.send_operation(
1629            Operation::UpdateCompletionTriggers {
1630                triggers,
1631                lamport_timestamp: self.completion_triggers_timestamp,
1632            },
1633            cx,
1634        );
1635        cx.notify();
1636    }
1637
1638    pub fn completion_triggers(&self) -> &[String] {
1639        &self.completion_triggers
1640    }
1641}
1642
1643#[cfg(any(test, feature = "test-support"))]
1644impl Buffer {
1645    pub fn set_group_interval(&mut self, group_interval: Duration) {
1646        self.text.set_group_interval(group_interval);
1647    }
1648
1649    pub fn randomly_edit<T>(
1650        &mut self,
1651        rng: &mut T,
1652        old_range_count: usize,
1653        cx: &mut ModelContext<Self>,
1654    ) where
1655        T: rand::Rng,
1656    {
1657        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1658        let mut last_end = None;
1659        for _ in 0..old_range_count {
1660            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1661                break;
1662            }
1663
1664            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1665            let mut range = self.random_byte_range(new_start, rng);
1666            if rng.gen_bool(0.2) {
1667                mem::swap(&mut range.start, &mut range.end);
1668            }
1669            last_end = Some(range.end);
1670
1671            let new_text_len = rng.gen_range(0..10);
1672            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1673
1674            edits.push((range, new_text));
1675        }
1676        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1677        self.edit(edits, None, cx);
1678    }
1679
1680    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1681        let was_dirty = self.is_dirty();
1682        let old_version = self.version.clone();
1683
1684        let ops = self.text.randomly_undo_redo(rng);
1685        if !ops.is_empty() {
1686            for op in ops {
1687                self.send_operation(Operation::Buffer(op), cx);
1688                self.did_edit(&old_version, was_dirty, cx);
1689            }
1690        }
1691    }
1692}
1693
1694impl Entity for Buffer {
1695    type Event = Event;
1696}
1697
1698impl Deref for Buffer {
1699    type Target = TextBuffer;
1700
1701    fn deref(&self) -> &Self::Target {
1702        &self.text
1703    }
1704}
1705
1706impl BufferSnapshot {
1707    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1708        indent_size_for_line(self, row)
1709    }
1710
1711    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1712        let language_name = self.language_at(position).map(|language| language.name());
1713        let settings = cx.global::<Settings>();
1714        if settings.hard_tabs(language_name.as_deref()) {
1715            IndentSize::tab()
1716        } else {
1717            IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
1718        }
1719    }
1720
1721    pub fn suggested_indents(
1722        &self,
1723        rows: impl Iterator<Item = u32>,
1724        single_indent_size: IndentSize,
1725    ) -> BTreeMap<u32, IndentSize> {
1726        let mut result = BTreeMap::new();
1727
1728        for row_range in contiguous_ranges(rows, 10) {
1729            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1730                Some(suggestions) => suggestions,
1731                _ => break,
1732            };
1733
1734            for (row, suggestion) in row_range.zip(suggestions) {
1735                let indent_size = if let Some(suggestion) = suggestion {
1736                    result
1737                        .get(&suggestion.basis_row)
1738                        .copied()
1739                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1740                        .with_delta(suggestion.delta, single_indent_size)
1741                } else {
1742                    self.indent_size_for_line(row)
1743                };
1744
1745                result.insert(row, indent_size);
1746            }
1747        }
1748
1749        result
1750    }
1751
1752    fn suggest_autoindents(
1753        &self,
1754        row_range: Range<u32>,
1755    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1756        let config = &self.language.as_ref()?.config;
1757        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1758
1759        // Find the suggested indentation ranges based on the syntax tree.
1760        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1761        let end = Point::new(row_range.end, 0);
1762        let range = (start..end).to_offset(&self.text);
1763        let mut matches = self.syntax.matches(range, &self.text, |grammar| {
1764            Some(&grammar.indents_config.as_ref()?.query)
1765        });
1766        let indent_configs = matches
1767            .grammars()
1768            .iter()
1769            .map(|grammar| grammar.indents_config.as_ref().unwrap())
1770            .collect::<Vec<_>>();
1771
1772        let mut indent_ranges = Vec::<Range<Point>>::new();
1773        let mut outdent_positions = Vec::<Point>::new();
1774        while let Some(mat) = matches.peek() {
1775            let mut start: Option<Point> = None;
1776            let mut end: Option<Point> = None;
1777
1778            let config = &indent_configs[mat.grammar_index];
1779            for capture in mat.captures {
1780                if capture.index == config.indent_capture_ix {
1781                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1782                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1783                } else if Some(capture.index) == config.start_capture_ix {
1784                    start = Some(Point::from_ts_point(capture.node.end_position()));
1785                } else if Some(capture.index) == config.end_capture_ix {
1786                    end = Some(Point::from_ts_point(capture.node.start_position()));
1787                } else if Some(capture.index) == config.outdent_capture_ix {
1788                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1789                }
1790            }
1791
1792            matches.advance();
1793            if let Some((start, end)) = start.zip(end) {
1794                if start.row == end.row {
1795                    continue;
1796                }
1797
1798                let range = start..end;
1799                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1800                    Err(ix) => indent_ranges.insert(ix, range),
1801                    Ok(ix) => {
1802                        let prev_range = &mut indent_ranges[ix];
1803                        prev_range.end = prev_range.end.max(range.end);
1804                    }
1805                }
1806            }
1807        }
1808
1809        outdent_positions.sort();
1810        for outdent_position in outdent_positions {
1811            // find the innermost indent range containing this outdent_position
1812            // set its end to the outdent position
1813            if let Some(range_to_truncate) = indent_ranges
1814                .iter_mut()
1815                .filter(|indent_range| indent_range.contains(&outdent_position))
1816                .last()
1817            {
1818                range_to_truncate.end = outdent_position;
1819            }
1820        }
1821
1822        // Find the suggested indentation increases and decreased based on regexes.
1823        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1824        self.for_each_line(
1825            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1826                ..Point::new(row_range.end, 0),
1827            |row, line| {
1828                if config
1829                    .decrease_indent_pattern
1830                    .as_ref()
1831                    .map_or(false, |regex| regex.is_match(line))
1832                {
1833                    indent_change_rows.push((row, Ordering::Less));
1834                }
1835                if config
1836                    .increase_indent_pattern
1837                    .as_ref()
1838                    .map_or(false, |regex| regex.is_match(line))
1839                {
1840                    indent_change_rows.push((row + 1, Ordering::Greater));
1841                }
1842            },
1843        );
1844
1845        let mut indent_changes = indent_change_rows.into_iter().peekable();
1846        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1847            prev_non_blank_row.unwrap_or(0)
1848        } else {
1849            row_range.start.saturating_sub(1)
1850        };
1851        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1852        Some(row_range.map(move |row| {
1853            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1854
1855            let mut indent_from_prev_row = false;
1856            let mut outdent_from_prev_row = false;
1857            let mut outdent_to_row = u32::MAX;
1858
1859            while let Some((indent_row, delta)) = indent_changes.peek() {
1860                match indent_row.cmp(&row) {
1861                    Ordering::Equal => match delta {
1862                        Ordering::Less => outdent_from_prev_row = true,
1863                        Ordering::Greater => indent_from_prev_row = true,
1864                        _ => {}
1865                    },
1866
1867                    Ordering::Greater => break,
1868                    Ordering::Less => {}
1869                }
1870
1871                indent_changes.next();
1872            }
1873
1874            for range in &indent_ranges {
1875                if range.start.row >= row {
1876                    break;
1877                }
1878                if range.start.row == prev_row && range.end > row_start {
1879                    indent_from_prev_row = true;
1880                }
1881                if range.end > prev_row_start && range.end <= row_start {
1882                    outdent_to_row = outdent_to_row.min(range.start.row);
1883                }
1884            }
1885
1886            let suggestion = if outdent_to_row == prev_row
1887                || (outdent_from_prev_row && indent_from_prev_row)
1888            {
1889                Some(IndentSuggestion {
1890                    basis_row: prev_row,
1891                    delta: Ordering::Equal,
1892                })
1893            } else if indent_from_prev_row {
1894                Some(IndentSuggestion {
1895                    basis_row: prev_row,
1896                    delta: Ordering::Greater,
1897                })
1898            } else if outdent_to_row < prev_row {
1899                Some(IndentSuggestion {
1900                    basis_row: outdent_to_row,
1901                    delta: Ordering::Equal,
1902                })
1903            } else if outdent_from_prev_row {
1904                Some(IndentSuggestion {
1905                    basis_row: prev_row,
1906                    delta: Ordering::Less,
1907                })
1908            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
1909            {
1910                Some(IndentSuggestion {
1911                    basis_row: prev_row,
1912                    delta: Ordering::Equal,
1913                })
1914            } else {
1915                None
1916            };
1917
1918            prev_row = row;
1919            prev_row_start = row_start;
1920            suggestion
1921        }))
1922    }
1923
1924    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1925        while row > 0 {
1926            row -= 1;
1927            if !self.is_line_blank(row) {
1928                return Some(row);
1929            }
1930        }
1931        None
1932    }
1933
1934    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
1935        let range = range.start.to_offset(self)..range.end.to_offset(self);
1936
1937        let mut syntax = None;
1938        let mut diagnostic_endpoints = Vec::new();
1939        if language_aware {
1940            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
1941                grammar.highlights_query.as_ref()
1942            });
1943            let highlight_maps = captures
1944                .grammars()
1945                .into_iter()
1946                .map(|grammar| grammar.highlight_map())
1947                .collect();
1948            syntax = Some((captures, highlight_maps));
1949            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1950                diagnostic_endpoints.push(DiagnosticEndpoint {
1951                    offset: entry.range.start,
1952                    is_start: true,
1953                    severity: entry.diagnostic.severity,
1954                    is_unnecessary: entry.diagnostic.is_unnecessary,
1955                });
1956                diagnostic_endpoints.push(DiagnosticEndpoint {
1957                    offset: entry.range.end,
1958                    is_start: false,
1959                    severity: entry.diagnostic.severity,
1960                    is_unnecessary: entry.diagnostic.is_unnecessary,
1961                });
1962            }
1963            diagnostic_endpoints
1964                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1965        }
1966
1967        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
1968    }
1969
1970    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
1971        let mut line = String::new();
1972        let mut row = range.start.row;
1973        for chunk in self
1974            .as_rope()
1975            .chunks_in_range(range.to_offset(self))
1976            .chain(["\n"])
1977        {
1978            for (newline_ix, text) in chunk.split('\n').enumerate() {
1979                if newline_ix > 0 {
1980                    callback(row, &line);
1981                    row += 1;
1982                    line.clear();
1983                }
1984                line.push_str(text);
1985            }
1986        }
1987    }
1988
1989    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
1990        let offset = position.to_offset(self);
1991        self.syntax
1992            .layers_for_range(offset..offset, &self.text)
1993            .filter(|l| l.node.end_byte() > offset)
1994            .last()
1995            .map(|info| info.language)
1996            .or(self.language.as_ref())
1997    }
1998
1999    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2000        let mut start = start.to_offset(self);
2001        let mut end = start;
2002        let mut next_chars = self.chars_at(start).peekable();
2003        let mut prev_chars = self.reversed_chars_at(start).peekable();
2004        let word_kind = cmp::max(
2005            prev_chars.peek().copied().map(char_kind),
2006            next_chars.peek().copied().map(char_kind),
2007        );
2008
2009        for ch in prev_chars {
2010            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2011                start -= ch.len_utf8();
2012            } else {
2013                break;
2014            }
2015        }
2016
2017        for ch in next_chars {
2018            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2019                end += ch.len_utf8();
2020            } else {
2021                break;
2022            }
2023        }
2024
2025        (start..end, word_kind)
2026    }
2027
2028    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2029        let range = range.start.to_offset(self)..range.end.to_offset(self);
2030        let mut result: Option<Range<usize>> = None;
2031        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2032            let mut cursor = layer.node.walk();
2033
2034            // Descend to the first leaf that touches the start of the range,
2035            // and if the range is non-empty, extends beyond the start.
2036            while cursor.goto_first_child_for_byte(range.start).is_some() {
2037                if !range.is_empty() && cursor.node().end_byte() == range.start {
2038                    cursor.goto_next_sibling();
2039                }
2040            }
2041
2042            // Ascend to the smallest ancestor that strictly contains the range.
2043            loop {
2044                let node_range = cursor.node().byte_range();
2045                if node_range.start <= range.start
2046                    && node_range.end >= range.end
2047                    && node_range.len() > range.len()
2048                {
2049                    break;
2050                }
2051                if !cursor.goto_parent() {
2052                    continue 'outer;
2053                }
2054            }
2055
2056            let left_node = cursor.node();
2057            let mut layer_result = left_node.byte_range();
2058
2059            // For an empty range, try to find another node immediately to the right of the range.
2060            if left_node.end_byte() == range.start {
2061                let mut right_node = None;
2062                while !cursor.goto_next_sibling() {
2063                    if !cursor.goto_parent() {
2064                        break;
2065                    }
2066                }
2067
2068                while cursor.node().start_byte() == range.start {
2069                    right_node = Some(cursor.node());
2070                    if !cursor.goto_first_child() {
2071                        break;
2072                    }
2073                }
2074
2075                // If there is a candidate node on both sides of the (empty) range, then
2076                // decide between the two by favoring a named node over an anonymous token.
2077                // If both nodes are the same in that regard, favor the right one.
2078                if let Some(right_node) = right_node {
2079                    if right_node.is_named() || !left_node.is_named() {
2080                        layer_result = right_node.byte_range();
2081                    }
2082                }
2083            }
2084
2085            if let Some(previous_result) = &result {
2086                if previous_result.len() < layer_result.len() {
2087                    continue;
2088                }
2089            }
2090            result = Some(layer_result);
2091        }
2092
2093        result
2094    }
2095
2096    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2097        self.outline_items_containing(0..self.len(), theme)
2098            .map(Outline::new)
2099    }
2100
2101    pub fn symbols_containing<T: ToOffset>(
2102        &self,
2103        position: T,
2104        theme: Option<&SyntaxTheme>,
2105    ) -> Option<Vec<OutlineItem<Anchor>>> {
2106        let position = position.to_offset(self);
2107        let mut items = self.outline_items_containing(
2108            position.saturating_sub(1)..self.len().min(position + 1),
2109            theme,
2110        )?;
2111        let mut prev_depth = None;
2112        items.retain(|item| {
2113            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2114            prev_depth = Some(item.depth);
2115            result
2116        });
2117        Some(items)
2118    }
2119
2120    fn outline_items_containing(
2121        &self,
2122        range: Range<usize>,
2123        theme: Option<&SyntaxTheme>,
2124    ) -> Option<Vec<OutlineItem<Anchor>>> {
2125        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2126            grammar.outline_config.as_ref().map(|c| &c.query)
2127        });
2128        let configs = matches
2129            .grammars()
2130            .iter()
2131            .map(|g| g.outline_config.as_ref().unwrap())
2132            .collect::<Vec<_>>();
2133
2134        let mut chunks = self.chunks(0..self.len(), true);
2135        let mut stack = Vec::<Range<usize>>::new();
2136        let mut items = Vec::new();
2137        while let Some(mat) = matches.peek() {
2138            let config = &configs[mat.grammar_index];
2139            let item_node = mat.captures.iter().find_map(|cap| {
2140                if cap.index == config.item_capture_ix {
2141                    Some(cap.node)
2142                } else {
2143                    None
2144                }
2145            })?;
2146
2147            let item_range = item_node.byte_range();
2148            if item_range.end < range.start || item_range.start > range.end {
2149                matches.advance();
2150                continue;
2151            }
2152
2153            // TODO - move later, after processing captures
2154
2155            let mut text = String::new();
2156            let mut name_ranges = Vec::new();
2157            let mut highlight_ranges = Vec::new();
2158            for capture in mat.captures {
2159                let node_is_name;
2160                if capture.index == config.name_capture_ix {
2161                    node_is_name = true;
2162                } else if Some(capture.index) == config.context_capture_ix {
2163                    node_is_name = false;
2164                } else {
2165                    continue;
2166                }
2167
2168                let range = capture.node.start_byte()..capture.node.end_byte();
2169                if !text.is_empty() {
2170                    text.push(' ');
2171                }
2172                if node_is_name {
2173                    let mut start = text.len();
2174                    let end = start + range.len();
2175
2176                    // When multiple names are captured, then the matcheable text
2177                    // includes the whitespace in between the names.
2178                    if !name_ranges.is_empty() {
2179                        start -= 1;
2180                    }
2181
2182                    name_ranges.push(start..end);
2183                }
2184
2185                let mut offset = range.start;
2186                chunks.seek(offset);
2187                for mut chunk in chunks.by_ref() {
2188                    if chunk.text.len() > range.end - offset {
2189                        chunk.text = &chunk.text[0..(range.end - offset)];
2190                        offset = range.end;
2191                    } else {
2192                        offset += chunk.text.len();
2193                    }
2194                    let style = chunk
2195                        .syntax_highlight_id
2196                        .zip(theme)
2197                        .and_then(|(highlight, theme)| highlight.style(theme));
2198                    if let Some(style) = style {
2199                        let start = text.len();
2200                        let end = start + chunk.text.len();
2201                        highlight_ranges.push((start..end, style));
2202                    }
2203                    text.push_str(chunk.text);
2204                    if offset >= range.end {
2205                        break;
2206                    }
2207                }
2208            }
2209
2210            matches.advance();
2211            while stack.last().map_or(false, |prev_range| {
2212                prev_range.start > item_range.start || prev_range.end < item_range.end
2213            }) {
2214                stack.pop();
2215            }
2216            stack.push(item_range.clone());
2217
2218            items.push(OutlineItem {
2219                depth: stack.len() - 1,
2220                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2221                text,
2222                highlight_ranges,
2223                name_ranges,
2224            })
2225        }
2226        Some(items)
2227    }
2228
2229    pub fn enclosing_bracket_ranges<T: ToOffset>(
2230        &self,
2231        range: Range<T>,
2232    ) -> Option<(Range<usize>, Range<usize>)> {
2233        // Find bracket pairs that *inclusively* contain the given range.
2234        let range = range.start.to_offset(self)..range.end.to_offset(self);
2235        let mut matches = self.syntax.matches(
2236            range.start.saturating_sub(1)..self.len().min(range.end + 1),
2237            &self.text,
2238            |grammar| grammar.brackets_config.as_ref().map(|c| &c.query),
2239        );
2240        let configs = matches
2241            .grammars()
2242            .iter()
2243            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2244            .collect::<Vec<_>>();
2245
2246        // Get the ranges of the innermost pair of brackets.
2247        let mut result: Option<(Range<usize>, Range<usize>)> = None;
2248        while let Some(mat) = matches.peek() {
2249            let mut open = None;
2250            let mut close = None;
2251            let config = &configs[mat.grammar_index];
2252            for capture in mat.captures {
2253                if capture.index == config.open_capture_ix {
2254                    open = Some(capture.node.byte_range());
2255                } else if capture.index == config.close_capture_ix {
2256                    close = Some(capture.node.byte_range());
2257                }
2258            }
2259
2260            matches.advance();
2261
2262            let Some((open, close)) = open.zip(close) else { continue };
2263            if open.start > range.start || close.end < range.end {
2264                continue;
2265            }
2266            let len = close.end - open.start;
2267
2268            if let Some((existing_open, existing_close)) = &result {
2269                let existing_len = existing_close.end - existing_open.start;
2270                if len > existing_len {
2271                    continue;
2272                }
2273            }
2274
2275            result = Some((open, close));
2276        }
2277
2278        result
2279    }
2280
2281    #[allow(clippy::type_complexity)]
2282    pub fn remote_selections_in_range(
2283        &self,
2284        range: Range<Anchor>,
2285    ) -> impl Iterator<
2286        Item = (
2287            ReplicaId,
2288            bool,
2289            CursorShape,
2290            impl Iterator<Item = &Selection<Anchor>> + '_,
2291        ),
2292    > + '_ {
2293        self.remote_selections
2294            .iter()
2295            .filter(|(replica_id, set)| {
2296                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2297            })
2298            .map(move |(replica_id, set)| {
2299                let start_ix = match set.selections.binary_search_by(|probe| {
2300                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2301                }) {
2302                    Ok(ix) | Err(ix) => ix,
2303                };
2304                let end_ix = match set.selections.binary_search_by(|probe| {
2305                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2306                }) {
2307                    Ok(ix) | Err(ix) => ix,
2308                };
2309
2310                (
2311                    *replica_id,
2312                    set.line_mode,
2313                    set.cursor_shape,
2314                    set.selections[start_ix..end_ix].iter(),
2315                )
2316            })
2317    }
2318
2319    pub fn git_diff_hunks_in_row_range<'a>(
2320        &'a self,
2321        range: Range<u32>,
2322        reversed: bool,
2323    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2324        self.git_diff.hunks_in_row_range(range, self, reversed)
2325    }
2326
2327    pub fn git_diff_hunks_intersecting_range<'a>(
2328        &'a self,
2329        range: Range<Anchor>,
2330        reversed: bool,
2331    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2332        self.git_diff
2333            .hunks_intersecting_range(range, self, reversed)
2334    }
2335
2336    pub fn diagnostics_in_range<'a, T, O>(
2337        &'a self,
2338        search_range: Range<T>,
2339        reversed: bool,
2340    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2341    where
2342        T: 'a + Clone + ToOffset,
2343        O: 'a + FromAnchor,
2344    {
2345        self.diagnostics.range(search_range, self, true, reversed)
2346    }
2347
2348    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2349        let mut groups = Vec::new();
2350        self.diagnostics.groups(&mut groups, self);
2351        groups
2352    }
2353
2354    pub fn diagnostic_group<'a, O>(
2355        &'a self,
2356        group_id: usize,
2357    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2358    where
2359        O: 'a + FromAnchor,
2360    {
2361        self.diagnostics.group(group_id, self)
2362    }
2363
2364    pub fn diagnostics_update_count(&self) -> usize {
2365        self.diagnostics_update_count
2366    }
2367
2368    pub fn parse_count(&self) -> usize {
2369        self.parse_count
2370    }
2371
2372    pub fn selections_update_count(&self) -> usize {
2373        self.selections_update_count
2374    }
2375
2376    pub fn file(&self) -> Option<&Arc<dyn File>> {
2377        self.file.as_ref()
2378    }
2379
2380    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2381        if let Some(file) = self.file() {
2382            if file.path().file_name().is_none() || include_root {
2383                Some(file.full_path(cx))
2384            } else {
2385                Some(file.path().to_path_buf())
2386            }
2387        } else {
2388            None
2389        }
2390    }
2391
2392    pub fn file_update_count(&self) -> usize {
2393        self.file_update_count
2394    }
2395
2396    pub fn git_diff_update_count(&self) -> usize {
2397        self.git_diff_update_count
2398    }
2399}
2400
2401pub fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2402    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2403}
2404
2405pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2406    let mut result = IndentSize::spaces(0);
2407    for c in text {
2408        let kind = match c {
2409            ' ' => IndentKind::Space,
2410            '\t' => IndentKind::Tab,
2411            _ => break,
2412        };
2413        if result.len == 0 {
2414            result.kind = kind;
2415        }
2416        result.len += 1;
2417    }
2418    result
2419}
2420
2421impl Clone for BufferSnapshot {
2422    fn clone(&self) -> Self {
2423        Self {
2424            text: self.text.clone(),
2425            git_diff: self.git_diff.clone(),
2426            syntax: self.syntax.clone(),
2427            file: self.file.clone(),
2428            remote_selections: self.remote_selections.clone(),
2429            diagnostics: self.diagnostics.clone(),
2430            selections_update_count: self.selections_update_count,
2431            diagnostics_update_count: self.diagnostics_update_count,
2432            file_update_count: self.file_update_count,
2433            git_diff_update_count: self.git_diff_update_count,
2434            language: self.language.clone(),
2435            parse_count: self.parse_count,
2436        }
2437    }
2438}
2439
2440impl Deref for BufferSnapshot {
2441    type Target = text::BufferSnapshot;
2442
2443    fn deref(&self) -> &Self::Target {
2444        &self.text
2445    }
2446}
2447
2448unsafe impl<'a> Send for BufferChunks<'a> {}
2449
2450impl<'a> BufferChunks<'a> {
2451    pub(crate) fn new(
2452        text: &'a Rope,
2453        range: Range<usize>,
2454        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2455        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2456    ) -> Self {
2457        let mut highlights = None;
2458        if let Some((captures, highlight_maps)) = syntax {
2459            highlights = Some(BufferChunkHighlights {
2460                captures,
2461                next_capture: None,
2462                stack: Default::default(),
2463                highlight_maps,
2464            })
2465        }
2466
2467        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2468        let chunks = text.chunks_in_range(range.clone());
2469
2470        BufferChunks {
2471            range,
2472            chunks,
2473            diagnostic_endpoints,
2474            error_depth: 0,
2475            warning_depth: 0,
2476            information_depth: 0,
2477            hint_depth: 0,
2478            unnecessary_depth: 0,
2479            highlights,
2480        }
2481    }
2482
2483    pub fn seek(&mut self, offset: usize) {
2484        self.range.start = offset;
2485        self.chunks.seek(self.range.start);
2486        if let Some(highlights) = self.highlights.as_mut() {
2487            highlights
2488                .stack
2489                .retain(|(end_offset, _)| *end_offset > offset);
2490            if let Some(capture) = &highlights.next_capture {
2491                if offset >= capture.node.start_byte() {
2492                    let next_capture_end = capture.node.end_byte();
2493                    if offset < next_capture_end {
2494                        highlights.stack.push((
2495                            next_capture_end,
2496                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2497                        ));
2498                    }
2499                    highlights.next_capture.take();
2500                }
2501            }
2502            highlights.captures.set_byte_range(self.range.clone());
2503        }
2504    }
2505
2506    pub fn offset(&self) -> usize {
2507        self.range.start
2508    }
2509
2510    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2511        let depth = match endpoint.severity {
2512            DiagnosticSeverity::ERROR => &mut self.error_depth,
2513            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2514            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2515            DiagnosticSeverity::HINT => &mut self.hint_depth,
2516            _ => return,
2517        };
2518        if endpoint.is_start {
2519            *depth += 1;
2520        } else {
2521            *depth -= 1;
2522        }
2523
2524        if endpoint.is_unnecessary {
2525            if endpoint.is_start {
2526                self.unnecessary_depth += 1;
2527            } else {
2528                self.unnecessary_depth -= 1;
2529            }
2530        }
2531    }
2532
2533    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2534        if self.error_depth > 0 {
2535            Some(DiagnosticSeverity::ERROR)
2536        } else if self.warning_depth > 0 {
2537            Some(DiagnosticSeverity::WARNING)
2538        } else if self.information_depth > 0 {
2539            Some(DiagnosticSeverity::INFORMATION)
2540        } else if self.hint_depth > 0 {
2541            Some(DiagnosticSeverity::HINT)
2542        } else {
2543            None
2544        }
2545    }
2546
2547    fn current_code_is_unnecessary(&self) -> bool {
2548        self.unnecessary_depth > 0
2549    }
2550}
2551
2552impl<'a> Iterator for BufferChunks<'a> {
2553    type Item = Chunk<'a>;
2554
2555    fn next(&mut self) -> Option<Self::Item> {
2556        let mut next_capture_start = usize::MAX;
2557        let mut next_diagnostic_endpoint = usize::MAX;
2558
2559        if let Some(highlights) = self.highlights.as_mut() {
2560            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2561                if *parent_capture_end <= self.range.start {
2562                    highlights.stack.pop();
2563                } else {
2564                    break;
2565                }
2566            }
2567
2568            if highlights.next_capture.is_none() {
2569                highlights.next_capture = highlights.captures.next();
2570            }
2571
2572            while let Some(capture) = highlights.next_capture.as_ref() {
2573                if self.range.start < capture.node.start_byte() {
2574                    next_capture_start = capture.node.start_byte();
2575                    break;
2576                } else {
2577                    let highlight_id =
2578                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2579                    highlights
2580                        .stack
2581                        .push((capture.node.end_byte(), highlight_id));
2582                    highlights.next_capture = highlights.captures.next();
2583                }
2584            }
2585        }
2586
2587        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2588            if endpoint.offset <= self.range.start {
2589                self.update_diagnostic_depths(endpoint);
2590                self.diagnostic_endpoints.next();
2591            } else {
2592                next_diagnostic_endpoint = endpoint.offset;
2593                break;
2594            }
2595        }
2596
2597        if let Some(chunk) = self.chunks.peek() {
2598            let chunk_start = self.range.start;
2599            let mut chunk_end = (self.chunks.offset() + chunk.len())
2600                .min(next_capture_start)
2601                .min(next_diagnostic_endpoint);
2602            let mut highlight_id = None;
2603            if let Some(highlights) = self.highlights.as_ref() {
2604                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2605                    chunk_end = chunk_end.min(*parent_capture_end);
2606                    highlight_id = Some(*parent_highlight_id);
2607                }
2608            }
2609
2610            let slice =
2611                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2612            self.range.start = chunk_end;
2613            if self.range.start == self.chunks.offset() + chunk.len() {
2614                self.chunks.next().unwrap();
2615            }
2616
2617            Some(Chunk {
2618                text: slice,
2619                syntax_highlight_id: highlight_id,
2620                highlight_style: None,
2621                diagnostic_severity: self.current_diagnostic_severity(),
2622                is_unnecessary: self.current_code_is_unnecessary(),
2623            })
2624        } else {
2625            None
2626        }
2627    }
2628}
2629
2630impl operation_queue::Operation for Operation {
2631    fn lamport_timestamp(&self) -> clock::Lamport {
2632        match self {
2633            Operation::Buffer(_) => {
2634                unreachable!("buffer operations should never be deferred at this layer")
2635            }
2636            Operation::UpdateDiagnostics {
2637                lamport_timestamp, ..
2638            }
2639            | Operation::UpdateSelections {
2640                lamport_timestamp, ..
2641            }
2642            | Operation::UpdateCompletionTriggers {
2643                lamport_timestamp, ..
2644            } => *lamport_timestamp,
2645        }
2646    }
2647}
2648
2649impl Default for Diagnostic {
2650    fn default() -> Self {
2651        Self {
2652            code: None,
2653            severity: DiagnosticSeverity::ERROR,
2654            message: Default::default(),
2655            group_id: 0,
2656            is_primary: false,
2657            is_valid: true,
2658            is_disk_based: false,
2659            is_unnecessary: false,
2660        }
2661    }
2662}
2663
2664impl IndentSize {
2665    pub fn spaces(len: u32) -> Self {
2666        Self {
2667            len,
2668            kind: IndentKind::Space,
2669        }
2670    }
2671
2672    pub fn tab() -> Self {
2673        Self {
2674            len: 1,
2675            kind: IndentKind::Tab,
2676        }
2677    }
2678
2679    pub fn chars(&self) -> impl Iterator<Item = char> {
2680        iter::repeat(self.char()).take(self.len as usize)
2681    }
2682
2683    pub fn char(&self) -> char {
2684        match self.kind {
2685            IndentKind::Space => ' ',
2686            IndentKind::Tab => '\t',
2687        }
2688    }
2689
2690    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2691        match direction {
2692            Ordering::Less => {
2693                if self.kind == size.kind && self.len >= size.len {
2694                    self.len -= size.len;
2695                }
2696            }
2697            Ordering::Equal => {}
2698            Ordering::Greater => {
2699                if self.len == 0 {
2700                    self = size;
2701                } else if self.kind == size.kind {
2702                    self.len += size.len;
2703                }
2704            }
2705        }
2706        self
2707    }
2708}
2709
2710impl Completion {
2711    pub fn sort_key(&self) -> (usize, &str) {
2712        let kind_key = match self.lsp_completion.kind {
2713            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2714            _ => 1,
2715        };
2716        (kind_key, &self.label.text[self.label.filter_range.clone()])
2717    }
2718
2719    pub fn is_snippet(&self) -> bool {
2720        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2721    }
2722}
2723
2724pub fn contiguous_ranges(
2725    values: impl Iterator<Item = u32>,
2726    max_len: usize,
2727) -> impl Iterator<Item = Range<u32>> {
2728    let mut values = values;
2729    let mut current_range: Option<Range<u32>> = None;
2730    std::iter::from_fn(move || loop {
2731        if let Some(value) = values.next() {
2732            if let Some(range) = &mut current_range {
2733                if value == range.end && range.len() < max_len {
2734                    range.end += 1;
2735                    continue;
2736                }
2737            }
2738
2739            let prev_range = current_range.clone();
2740            current_range = Some(value..(value + 1));
2741            if prev_range.is_some() {
2742                return prev_range;
2743            }
2744        } else {
2745            return current_range.take();
2746        }
2747    })
2748}
2749
2750pub fn char_kind(c: char) -> CharKind {
2751    if c.is_whitespace() {
2752        CharKind::Whitespace
2753    } else if c.is_alphanumeric() || c == '_' {
2754        CharKind::Word
2755    } else {
2756        CharKind::Punctuation
2757    }
2758}