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