buffer.rs

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