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 indents_query = grammar.indents_query.as_ref()?;
1527        let mut query_cursor = QueryCursorHandle::new();
1528        let indent_capture_ix = indents_query.capture_index_for_name("indent");
1529        let end_capture_ix = indents_query.capture_index_for_name("end");
1530        query_cursor.set_point_range(
1531            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1532                ..Point::new(row_range.end, 0).to_ts_point(),
1533        );
1534        let mut indentation_ranges = Vec::<Range<Point>>::new();
1535        for mat in query_cursor.matches(
1536            indents_query,
1537            self.tree.as_ref()?.root_node(),
1538            TextProvider(self.as_rope()),
1539        ) {
1540            let mut start: Option<Point> = None;
1541            let mut end: Option<Point> = None;
1542            for capture in mat.captures {
1543                if Some(capture.index) == indent_capture_ix {
1544                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1545                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1546                } else if Some(capture.index) == end_capture_ix {
1547                    end = Some(Point::from_ts_point(capture.node.start_position().into()));
1548                }
1549            }
1550
1551            if let Some((start, end)) = start.zip(end) {
1552                if start.row == end.row {
1553                    continue;
1554                }
1555
1556                let range = start..end;
1557                match indentation_ranges.binary_search_by_key(&range.start, |r| r.start) {
1558                    Err(ix) => indentation_ranges.insert(ix, range),
1559                    Ok(ix) => {
1560                        let prev_range = &mut indentation_ranges[ix];
1561                        prev_range.end = prev_range.end.max(range.end);
1562                    }
1563                }
1564            }
1565        }
1566
1567        let mut prev_row = prev_non_blank_row.unwrap_or(0);
1568        Some(row_range.map(move |row| {
1569            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1570
1571            let mut indent_from_prev_row = false;
1572            let mut outdent_to_row = u32::MAX;
1573            for range in &indentation_ranges {
1574                if range.start.row >= row {
1575                    break;
1576                }
1577
1578                if range.start.row == prev_row && range.end > row_start {
1579                    indent_from_prev_row = true;
1580                }
1581                if range.end.row >= prev_row && range.end <= row_start {
1582                    outdent_to_row = outdent_to_row.min(range.start.row);
1583                }
1584            }
1585
1586            let suggestion = if outdent_to_row == prev_row {
1587                IndentSuggestion {
1588                    basis_row: prev_row,
1589                    indent: false,
1590                }
1591            } else if indent_from_prev_row {
1592                IndentSuggestion {
1593                    basis_row: prev_row,
1594                    indent: true,
1595                }
1596            } else if outdent_to_row < prev_row {
1597                IndentSuggestion {
1598                    basis_row: outdent_to_row,
1599                    indent: false,
1600                }
1601            } else {
1602                IndentSuggestion {
1603                    basis_row: prev_row,
1604                    indent: false,
1605                }
1606            };
1607
1608            prev_row = row;
1609            suggestion
1610        }))
1611    }
1612
1613    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1614        while row > 0 {
1615            row -= 1;
1616            if !self.is_line_blank(row) {
1617                return Some(row);
1618            }
1619        }
1620        None
1621    }
1622
1623    pub fn chunks<'a, T: ToOffset>(
1624        &'a self,
1625        range: Range<T>,
1626        language_aware: bool,
1627    ) -> BufferChunks<'a> {
1628        let range = range.start.to_offset(self)..range.end.to_offset(self);
1629
1630        let mut tree = None;
1631        let mut diagnostic_endpoints = Vec::new();
1632        if language_aware {
1633            tree = self.tree.as_ref();
1634            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1635                diagnostic_endpoints.push(DiagnosticEndpoint {
1636                    offset: entry.range.start,
1637                    is_start: true,
1638                    severity: entry.diagnostic.severity,
1639                    is_unnecessary: entry.diagnostic.is_unnecessary,
1640                });
1641                diagnostic_endpoints.push(DiagnosticEndpoint {
1642                    offset: entry.range.end,
1643                    is_start: false,
1644                    severity: entry.diagnostic.severity,
1645                    is_unnecessary: entry.diagnostic.is_unnecessary,
1646                });
1647            }
1648            diagnostic_endpoints
1649                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1650        }
1651
1652        BufferChunks::new(
1653            self.text.as_rope(),
1654            range,
1655            tree,
1656            self.grammar(),
1657            diagnostic_endpoints,
1658        )
1659    }
1660
1661    pub fn language(&self) -> Option<&Arc<Language>> {
1662        self.language.as_ref()
1663    }
1664
1665    fn grammar(&self) -> Option<&Arc<Grammar>> {
1666        self.language
1667            .as_ref()
1668            .and_then(|language| language.grammar.as_ref())
1669    }
1670
1671    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
1672        let mut start = start.to_offset(self);
1673        let mut end = start;
1674        let mut next_chars = self.chars_at(start).peekable();
1675        let mut prev_chars = self.reversed_chars_at(start).peekable();
1676        let word_kind = cmp::max(
1677            prev_chars.peek().copied().map(char_kind),
1678            next_chars.peek().copied().map(char_kind),
1679        );
1680
1681        for ch in prev_chars {
1682            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1683                start -= ch.len_utf8();
1684            } else {
1685                break;
1686            }
1687        }
1688
1689        for ch in next_chars {
1690            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1691                end += ch.len_utf8();
1692            } else {
1693                break;
1694            }
1695        }
1696
1697        (start..end, word_kind)
1698    }
1699
1700    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1701        let tree = self.tree.as_ref()?;
1702        let range = range.start.to_offset(self)..range.end.to_offset(self);
1703        let mut cursor = tree.root_node().walk();
1704
1705        // Descend to the first leaf that touches the start of the range,
1706        // and if the range is non-empty, extends beyond the start.
1707        while cursor.goto_first_child_for_byte(range.start).is_some() {
1708            if !range.is_empty() && cursor.node().end_byte() == range.start {
1709                cursor.goto_next_sibling();
1710            }
1711        }
1712
1713        // Ascend to the smallest ancestor that strictly contains the range.
1714        loop {
1715            let node_range = cursor.node().byte_range();
1716            if node_range.start <= range.start
1717                && node_range.end >= range.end
1718                && node_range.len() > range.len()
1719            {
1720                break;
1721            }
1722            if !cursor.goto_parent() {
1723                break;
1724            }
1725        }
1726
1727        let left_node = cursor.node();
1728
1729        // For an empty range, try to find another node immediately to the right of the range.
1730        if left_node.end_byte() == range.start {
1731            let mut right_node = None;
1732            while !cursor.goto_next_sibling() {
1733                if !cursor.goto_parent() {
1734                    break;
1735                }
1736            }
1737
1738            while cursor.node().start_byte() == range.start {
1739                right_node = Some(cursor.node());
1740                if !cursor.goto_first_child() {
1741                    break;
1742                }
1743            }
1744
1745            // If there is a candidate node on both sides of the (empty) range, then
1746            // decide between the two by favoring a named node over an anonymous token.
1747            // If both nodes are the same in that regard, favor the right one.
1748            if let Some(right_node) = right_node {
1749                if right_node.is_named() || !left_node.is_named() {
1750                    return Some(right_node.byte_range());
1751                }
1752            }
1753        }
1754
1755        Some(left_node.byte_range())
1756    }
1757
1758    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1759        self.outline_items_containing(0..self.len(), theme)
1760            .map(Outline::new)
1761    }
1762
1763    pub fn symbols_containing<T: ToOffset>(
1764        &self,
1765        position: T,
1766        theme: Option<&SyntaxTheme>,
1767    ) -> Option<Vec<OutlineItem<Anchor>>> {
1768        let position = position.to_offset(&self);
1769        let mut items =
1770            self.outline_items_containing(position.saturating_sub(1)..position + 1, theme)?;
1771        let mut prev_depth = None;
1772        items.retain(|item| {
1773            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
1774            prev_depth = Some(item.depth);
1775            result
1776        });
1777        Some(items)
1778    }
1779
1780    fn outline_items_containing(
1781        &self,
1782        range: Range<usize>,
1783        theme: Option<&SyntaxTheme>,
1784    ) -> Option<Vec<OutlineItem<Anchor>>> {
1785        let tree = self.tree.as_ref()?;
1786        let grammar = self
1787            .language
1788            .as_ref()
1789            .and_then(|language| language.grammar.as_ref())?;
1790
1791        let outline_query = grammar.outline_query.as_ref()?;
1792        let mut cursor = QueryCursorHandle::new();
1793        cursor.set_byte_range(range.clone());
1794        let matches = cursor.matches(
1795            outline_query,
1796            tree.root_node(),
1797            TextProvider(self.as_rope()),
1798        );
1799
1800        let mut chunks = self.chunks(0..self.len(), true);
1801
1802        let item_capture_ix = outline_query.capture_index_for_name("item")?;
1803        let name_capture_ix = outline_query.capture_index_for_name("name")?;
1804        let context_capture_ix = outline_query
1805            .capture_index_for_name("context")
1806            .unwrap_or(u32::MAX);
1807
1808        let mut stack = Vec::<Range<usize>>::new();
1809        let items = matches
1810            .filter_map(|mat| {
1811                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1812                let item_range = item_node.start_byte()..item_node.end_byte();
1813                if item_range.end < range.start || item_range.start > range.end {
1814                    return None;
1815                }
1816                let mut text = String::new();
1817                let mut name_ranges = Vec::new();
1818                let mut highlight_ranges = Vec::new();
1819
1820                for capture in mat.captures {
1821                    let node_is_name;
1822                    if capture.index == name_capture_ix {
1823                        node_is_name = true;
1824                    } else if capture.index == context_capture_ix {
1825                        node_is_name = false;
1826                    } else {
1827                        continue;
1828                    }
1829
1830                    let range = capture.node.start_byte()..capture.node.end_byte();
1831                    if !text.is_empty() {
1832                        text.push(' ');
1833                    }
1834                    if node_is_name {
1835                        let mut start = text.len();
1836                        let end = start + range.len();
1837
1838                        // When multiple names are captured, then the matcheable text
1839                        // includes the whitespace in between the names.
1840                        if !name_ranges.is_empty() {
1841                            start -= 1;
1842                        }
1843
1844                        name_ranges.push(start..end);
1845                    }
1846
1847                    let mut offset = range.start;
1848                    chunks.seek(offset);
1849                    while let Some(mut chunk) = chunks.next() {
1850                        if chunk.text.len() > range.end - offset {
1851                            chunk.text = &chunk.text[0..(range.end - offset)];
1852                            offset = range.end;
1853                        } else {
1854                            offset += chunk.text.len();
1855                        }
1856                        let style = chunk
1857                            .syntax_highlight_id
1858                            .zip(theme)
1859                            .and_then(|(highlight, theme)| highlight.style(theme));
1860                        if let Some(style) = style {
1861                            let start = text.len();
1862                            let end = start + chunk.text.len();
1863                            highlight_ranges.push((start..end, style));
1864                        }
1865                        text.push_str(chunk.text);
1866                        if offset >= range.end {
1867                            break;
1868                        }
1869                    }
1870                }
1871
1872                while stack.last().map_or(false, |prev_range| {
1873                    !prev_range.contains(&item_range.start) || !prev_range.contains(&item_range.end)
1874                }) {
1875                    stack.pop();
1876                }
1877                stack.push(item_range.clone());
1878
1879                Some(OutlineItem {
1880                    depth: stack.len() - 1,
1881                    range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
1882                    text,
1883                    highlight_ranges,
1884                    name_ranges,
1885                })
1886            })
1887            .collect::<Vec<_>>();
1888        Some(items)
1889    }
1890
1891    pub fn enclosing_bracket_ranges<T: ToOffset>(
1892        &self,
1893        range: Range<T>,
1894    ) -> Option<(Range<usize>, Range<usize>)> {
1895        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1896        let brackets_query = grammar.brackets_query.as_ref()?;
1897        let open_capture_ix = brackets_query.capture_index_for_name("open")?;
1898        let close_capture_ix = brackets_query.capture_index_for_name("close")?;
1899
1900        // Find bracket pairs that *inclusively* contain the given range.
1901        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1902        let mut cursor = QueryCursorHandle::new();
1903        let matches = cursor.set_byte_range(range).matches(
1904            &brackets_query,
1905            tree.root_node(),
1906            TextProvider(self.as_rope()),
1907        );
1908
1909        // Get the ranges of the innermost pair of brackets.
1910        matches
1911            .filter_map(|mat| {
1912                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1913                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1914                Some((open.byte_range(), close.byte_range()))
1915            })
1916            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1917    }
1918
1919    pub fn remote_selections_in_range<'a>(
1920        &'a self,
1921        range: Range<Anchor>,
1922    ) -> impl 'a
1923           + Iterator<
1924        Item = (
1925            ReplicaId,
1926            bool,
1927            impl 'a + Iterator<Item = &'a Selection<Anchor>>,
1928        ),
1929    > {
1930        self.remote_selections
1931            .iter()
1932            .filter(|(replica_id, set)| {
1933                **replica_id != self.text.replica_id() && !set.selections.is_empty()
1934            })
1935            .map(move |(replica_id, set)| {
1936                let start_ix = match set.selections.binary_search_by(|probe| {
1937                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
1938                }) {
1939                    Ok(ix) | Err(ix) => ix,
1940                };
1941                let end_ix = match set.selections.binary_search_by(|probe| {
1942                    probe.start.cmp(&range.end, self).then(Ordering::Less)
1943                }) {
1944                    Ok(ix) | Err(ix) => ix,
1945                };
1946
1947                (
1948                    *replica_id,
1949                    set.line_mode,
1950                    set.selections[start_ix..end_ix].iter(),
1951                )
1952            })
1953    }
1954
1955    pub fn diagnostics_in_range<'a, T, O>(
1956        &'a self,
1957        search_range: Range<T>,
1958        reversed: bool,
1959    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1960    where
1961        T: 'a + Clone + ToOffset,
1962        O: 'a + FromAnchor,
1963    {
1964        self.diagnostics
1965            .range(search_range.clone(), self, true, reversed)
1966    }
1967
1968    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1969        let mut groups = Vec::new();
1970        self.diagnostics.groups(&mut groups, self);
1971        groups
1972    }
1973
1974    pub fn diagnostic_group<'a, O>(
1975        &'a self,
1976        group_id: usize,
1977    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1978    where
1979        O: 'a + FromAnchor,
1980    {
1981        self.diagnostics.group(group_id, self)
1982    }
1983
1984    pub fn diagnostics_update_count(&self) -> usize {
1985        self.diagnostics_update_count
1986    }
1987
1988    pub fn parse_count(&self) -> usize {
1989        self.parse_count
1990    }
1991
1992    pub fn selections_update_count(&self) -> usize {
1993        self.selections_update_count
1994    }
1995
1996    pub fn file(&self) -> Option<&dyn File> {
1997        self.file.as_deref()
1998    }
1999
2000    pub fn file_update_count(&self) -> usize {
2001        self.file_update_count
2002    }
2003}
2004
2005pub fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2006    let mut result = IndentSize::spaces(0);
2007    for c in text.chars_at(Point::new(row, 0)) {
2008        let kind = match c {
2009            ' ' => IndentKind::Space,
2010            '\t' => IndentKind::Tab,
2011            _ => break,
2012        };
2013        if result.len == 0 {
2014            result.kind = kind;
2015        }
2016        result.len += 1;
2017    }
2018    result
2019}
2020
2021impl Clone for BufferSnapshot {
2022    fn clone(&self) -> Self {
2023        Self {
2024            text: self.text.clone(),
2025            tree: self.tree.clone(),
2026            file: self.file.clone(),
2027            remote_selections: self.remote_selections.clone(),
2028            diagnostics: self.diagnostics.clone(),
2029            selections_update_count: self.selections_update_count,
2030            diagnostics_update_count: self.diagnostics_update_count,
2031            file_update_count: self.file_update_count,
2032            language: self.language.clone(),
2033            parse_count: self.parse_count,
2034        }
2035    }
2036}
2037
2038impl Deref for BufferSnapshot {
2039    type Target = text::BufferSnapshot;
2040
2041    fn deref(&self) -> &Self::Target {
2042        &self.text
2043    }
2044}
2045
2046impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
2047    type I = ByteChunks<'a>;
2048
2049    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
2050        ByteChunks(self.0.chunks_in_range(node.byte_range()))
2051    }
2052}
2053
2054pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
2055
2056impl<'a> Iterator for ByteChunks<'a> {
2057    type Item = &'a [u8];
2058
2059    fn next(&mut self) -> Option<Self::Item> {
2060        self.0.next().map(str::as_bytes)
2061    }
2062}
2063
2064unsafe impl<'a> Send for BufferChunks<'a> {}
2065
2066impl<'a> BufferChunks<'a> {
2067    pub(crate) fn new(
2068        text: &'a Rope,
2069        range: Range<usize>,
2070        tree: Option<&'a Tree>,
2071        grammar: Option<&'a Arc<Grammar>>,
2072        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2073    ) -> Self {
2074        let mut highlights = None;
2075        if let Some((grammar, tree)) = grammar.zip(tree) {
2076            if let Some(highlights_query) = grammar.highlights_query.as_ref() {
2077                let mut query_cursor = QueryCursorHandle::new();
2078
2079                // TODO - add a Tree-sitter API to remove the need for this.
2080                let cursor = unsafe {
2081                    std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
2082                };
2083                let captures = cursor.set_byte_range(range.clone()).captures(
2084                    highlights_query,
2085                    tree.root_node(),
2086                    TextProvider(text),
2087                );
2088                highlights = Some(BufferChunkHighlights {
2089                    captures,
2090                    next_capture: None,
2091                    stack: Default::default(),
2092                    highlight_map: grammar.highlight_map(),
2093                    _query_cursor: query_cursor,
2094                })
2095            }
2096        }
2097
2098        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2099        let chunks = text.chunks_in_range(range.clone());
2100
2101        BufferChunks {
2102            range,
2103            chunks,
2104            diagnostic_endpoints,
2105            error_depth: 0,
2106            warning_depth: 0,
2107            information_depth: 0,
2108            hint_depth: 0,
2109            unnecessary_depth: 0,
2110            highlights,
2111        }
2112    }
2113
2114    pub fn seek(&mut self, offset: usize) {
2115        self.range.start = offset;
2116        self.chunks.seek(self.range.start);
2117        if let Some(highlights) = self.highlights.as_mut() {
2118            highlights
2119                .stack
2120                .retain(|(end_offset, _)| *end_offset > offset);
2121            if let Some((mat, capture_ix)) = &highlights.next_capture {
2122                let capture = mat.captures[*capture_ix as usize];
2123                if offset >= capture.node.start_byte() {
2124                    let next_capture_end = capture.node.end_byte();
2125                    if offset < next_capture_end {
2126                        highlights.stack.push((
2127                            next_capture_end,
2128                            highlights.highlight_map.get(capture.index),
2129                        ));
2130                    }
2131                    highlights.next_capture.take();
2132                }
2133            }
2134            highlights.captures.set_byte_range(self.range.clone());
2135        }
2136    }
2137
2138    pub fn offset(&self) -> usize {
2139        self.range.start
2140    }
2141
2142    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2143        let depth = match endpoint.severity {
2144            DiagnosticSeverity::ERROR => &mut self.error_depth,
2145            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2146            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2147            DiagnosticSeverity::HINT => &mut self.hint_depth,
2148            _ => return,
2149        };
2150        if endpoint.is_start {
2151            *depth += 1;
2152        } else {
2153            *depth -= 1;
2154        }
2155
2156        if endpoint.is_unnecessary {
2157            if endpoint.is_start {
2158                self.unnecessary_depth += 1;
2159            } else {
2160                self.unnecessary_depth -= 1;
2161            }
2162        }
2163    }
2164
2165    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2166        if self.error_depth > 0 {
2167            Some(DiagnosticSeverity::ERROR)
2168        } else if self.warning_depth > 0 {
2169            Some(DiagnosticSeverity::WARNING)
2170        } else if self.information_depth > 0 {
2171            Some(DiagnosticSeverity::INFORMATION)
2172        } else if self.hint_depth > 0 {
2173            Some(DiagnosticSeverity::HINT)
2174        } else {
2175            None
2176        }
2177    }
2178
2179    fn current_code_is_unnecessary(&self) -> bool {
2180        self.unnecessary_depth > 0
2181    }
2182}
2183
2184impl<'a> Iterator for BufferChunks<'a> {
2185    type Item = Chunk<'a>;
2186
2187    fn next(&mut self) -> Option<Self::Item> {
2188        let mut next_capture_start = usize::MAX;
2189        let mut next_diagnostic_endpoint = usize::MAX;
2190
2191        if let Some(highlights) = self.highlights.as_mut() {
2192            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2193                if *parent_capture_end <= self.range.start {
2194                    highlights.stack.pop();
2195                } else {
2196                    break;
2197                }
2198            }
2199
2200            if highlights.next_capture.is_none() {
2201                highlights.next_capture = highlights.captures.next();
2202            }
2203
2204            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2205                let capture = mat.captures[*capture_ix as usize];
2206                if self.range.start < capture.node.start_byte() {
2207                    next_capture_start = capture.node.start_byte();
2208                    break;
2209                } else {
2210                    let highlight_id = highlights.highlight_map.get(capture.index);
2211                    highlights
2212                        .stack
2213                        .push((capture.node.end_byte(), highlight_id));
2214                    highlights.next_capture = highlights.captures.next();
2215                }
2216            }
2217        }
2218
2219        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2220            if endpoint.offset <= self.range.start {
2221                self.update_diagnostic_depths(endpoint);
2222                self.diagnostic_endpoints.next();
2223            } else {
2224                next_diagnostic_endpoint = endpoint.offset;
2225                break;
2226            }
2227        }
2228
2229        if let Some(chunk) = self.chunks.peek() {
2230            let chunk_start = self.range.start;
2231            let mut chunk_end = (self.chunks.offset() + chunk.len())
2232                .min(next_capture_start)
2233                .min(next_diagnostic_endpoint);
2234            let mut highlight_id = None;
2235            if let Some(highlights) = self.highlights.as_ref() {
2236                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2237                    chunk_end = chunk_end.min(*parent_capture_end);
2238                    highlight_id = Some(*parent_highlight_id);
2239                }
2240            }
2241
2242            let slice =
2243                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2244            self.range.start = chunk_end;
2245            if self.range.start == self.chunks.offset() + chunk.len() {
2246                self.chunks.next().unwrap();
2247            }
2248
2249            Some(Chunk {
2250                text: slice,
2251                syntax_highlight_id: highlight_id,
2252                highlight_style: None,
2253                diagnostic_severity: self.current_diagnostic_severity(),
2254                is_unnecessary: self.current_code_is_unnecessary(),
2255            })
2256        } else {
2257            None
2258        }
2259    }
2260}
2261
2262impl QueryCursorHandle {
2263    pub(crate) fn new() -> Self {
2264        QueryCursorHandle(Some(
2265            QUERY_CURSORS
2266                .lock()
2267                .pop()
2268                .unwrap_or_else(|| QueryCursor::new()),
2269        ))
2270    }
2271}
2272
2273impl Deref for QueryCursorHandle {
2274    type Target = QueryCursor;
2275
2276    fn deref(&self) -> &Self::Target {
2277        self.0.as_ref().unwrap()
2278    }
2279}
2280
2281impl DerefMut for QueryCursorHandle {
2282    fn deref_mut(&mut self) -> &mut Self::Target {
2283        self.0.as_mut().unwrap()
2284    }
2285}
2286
2287impl Drop for QueryCursorHandle {
2288    fn drop(&mut self) {
2289        let mut cursor = self.0.take().unwrap();
2290        cursor.set_byte_range(0..usize::MAX);
2291        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2292        QUERY_CURSORS.lock().push(cursor)
2293    }
2294}
2295
2296trait ToTreeSitterPoint {
2297    fn to_ts_point(self) -> tree_sitter::Point;
2298    fn from_ts_point(point: tree_sitter::Point) -> Self;
2299}
2300
2301impl ToTreeSitterPoint for Point {
2302    fn to_ts_point(self) -> tree_sitter::Point {
2303        tree_sitter::Point::new(self.row as usize, self.column as usize)
2304    }
2305
2306    fn from_ts_point(point: tree_sitter::Point) -> Self {
2307        Point::new(point.row as u32, point.column as u32)
2308    }
2309}
2310
2311impl operation_queue::Operation for Operation {
2312    fn lamport_timestamp(&self) -> clock::Lamport {
2313        match self {
2314            Operation::Buffer(_) => {
2315                unreachable!("buffer operations should never be deferred at this layer")
2316            }
2317            Operation::UpdateDiagnostics {
2318                lamport_timestamp, ..
2319            }
2320            | Operation::UpdateSelections {
2321                lamport_timestamp, ..
2322            }
2323            | Operation::UpdateCompletionTriggers {
2324                lamport_timestamp, ..
2325            } => *lamport_timestamp,
2326        }
2327    }
2328}
2329
2330impl Default for Diagnostic {
2331    fn default() -> Self {
2332        Self {
2333            code: Default::default(),
2334            severity: DiagnosticSeverity::ERROR,
2335            message: Default::default(),
2336            group_id: Default::default(),
2337            is_primary: Default::default(),
2338            is_valid: true,
2339            is_disk_based: false,
2340            is_unnecessary: false,
2341        }
2342    }
2343}
2344
2345impl IndentSize {
2346    pub fn spaces(len: u32) -> Self {
2347        Self {
2348            len,
2349            kind: IndentKind::Space,
2350        }
2351    }
2352
2353    pub fn tab() -> Self {
2354        Self {
2355            len: 1,
2356            kind: IndentKind::Tab,
2357        }
2358    }
2359
2360    pub fn chars(&self) -> impl Iterator<Item = char> {
2361        iter::repeat(self.char()).take(self.len as usize)
2362    }
2363
2364    pub fn char(&self) -> char {
2365        match self.kind {
2366            IndentKind::Space => ' ',
2367            IndentKind::Tab => '\t',
2368        }
2369    }
2370}
2371
2372impl std::ops::AddAssign for IndentSize {
2373    fn add_assign(&mut self, other: IndentSize) {
2374        if self.len == 0 {
2375            *self = other;
2376        } else if self.kind == other.kind {
2377            self.len += other.len;
2378        }
2379    }
2380}
2381
2382impl Completion {
2383    pub fn sort_key(&self) -> (usize, &str) {
2384        let kind_key = match self.lsp_completion.kind {
2385            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2386            _ => 1,
2387        };
2388        (kind_key, &self.label.text[self.label.filter_range.clone()])
2389    }
2390
2391    pub fn is_snippet(&self) -> bool {
2392        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2393    }
2394}
2395
2396pub fn contiguous_ranges(
2397    values: impl Iterator<Item = u32>,
2398    max_len: usize,
2399) -> impl Iterator<Item = Range<u32>> {
2400    let mut values = values.into_iter();
2401    let mut current_range: Option<Range<u32>> = None;
2402    std::iter::from_fn(move || loop {
2403        if let Some(value) = values.next() {
2404            if let Some(range) = &mut current_range {
2405                if value == range.end && range.len() < max_len {
2406                    range.end += 1;
2407                    continue;
2408                }
2409            }
2410
2411            let prev_range = current_range.clone();
2412            current_range = Some(value..(value + 1));
2413            if prev_range.is_some() {
2414                return prev_range;
2415            }
2416        } else {
2417            return current_range.take();
2418        }
2419    })
2420}
2421
2422pub fn char_kind(c: char) -> CharKind {
2423    if c.is_whitespace() {
2424        CharKind::Whitespace
2425    } else if c.is_alphanumeric() || c == '_' {
2426        CharKind::Word
2427    } else {
2428        CharKind::Punctuation
2429    }
2430}