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