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