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