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