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