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