buffer.rs

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