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