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 { local: bool },
 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, true, 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        local: bool,
1164        cx: &mut ModelContext<Self>,
1165    ) {
1166        if self.edits_since::<usize>(old_version).next().is_none() {
1167            return;
1168        }
1169
1170        self.reparse(cx);
1171
1172        cx.emit(Event::Edited { local });
1173        if !was_dirty {
1174            cx.emit(Event::Dirtied);
1175        }
1176        cx.notify();
1177    }
1178
1179    fn grammar(&self) -> Option<&Arc<Grammar>> {
1180        self.language.as_ref().and_then(|l| l.grammar.as_ref())
1181    }
1182
1183    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1184        &mut self,
1185        ops: I,
1186        cx: &mut ModelContext<Self>,
1187    ) -> Result<()> {
1188        self.pending_autoindent.take();
1189        let was_dirty = self.is_dirty();
1190        let old_version = self.version.clone();
1191        let mut deferred_ops = Vec::new();
1192        let buffer_ops = ops
1193            .into_iter()
1194            .filter_map(|op| match op {
1195                Operation::Buffer(op) => Some(op),
1196                _ => {
1197                    if self.can_apply_op(&op) {
1198                        self.apply_op(op, cx);
1199                    } else {
1200                        deferred_ops.push(op);
1201                    }
1202                    None
1203                }
1204            })
1205            .collect::<Vec<_>>();
1206        self.text.apply_ops(buffer_ops)?;
1207        self.deferred_ops.insert(deferred_ops);
1208        self.flush_deferred_ops(cx);
1209        self.did_edit(&old_version, was_dirty, false, cx);
1210        // Notify independently of whether the buffer was edited as the operations could include a
1211        // selection update.
1212        cx.notify();
1213        Ok(())
1214    }
1215
1216    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1217        let mut deferred_ops = Vec::new();
1218        for op in self.deferred_ops.drain().iter().cloned() {
1219            if self.can_apply_op(&op) {
1220                self.apply_op(op, cx);
1221            } else {
1222                deferred_ops.push(op);
1223            }
1224        }
1225        self.deferred_ops.insert(deferred_ops);
1226    }
1227
1228    fn can_apply_op(&self, operation: &Operation) -> bool {
1229        match operation {
1230            Operation::Buffer(_) => {
1231                unreachable!("buffer operations should never be applied at this layer")
1232            }
1233            Operation::UpdateDiagnostics {
1234                diagnostics: diagnostic_set,
1235                ..
1236            } => diagnostic_set.iter().all(|diagnostic| {
1237                self.text.can_resolve(&diagnostic.range.start)
1238                    && self.text.can_resolve(&diagnostic.range.end)
1239            }),
1240            Operation::UpdateSelections { selections, .. } => selections
1241                .iter()
1242                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1243            Operation::UpdateCompletionTriggers { .. } => true,
1244        }
1245    }
1246
1247    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1248        match operation {
1249            Operation::Buffer(_) => {
1250                unreachable!("buffer operations should never be applied at this layer")
1251            }
1252            Operation::UpdateDiagnostics {
1253                diagnostics: diagnostic_set,
1254                lamport_timestamp,
1255            } => {
1256                let snapshot = self.snapshot();
1257                self.apply_diagnostic_update(
1258                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1259                    lamport_timestamp,
1260                    cx,
1261                );
1262            }
1263            Operation::UpdateSelections {
1264                selections,
1265                lamport_timestamp,
1266            } => {
1267                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1268                    if set.lamport_timestamp > lamport_timestamp {
1269                        return;
1270                    }
1271                }
1272
1273                self.remote_selections.insert(
1274                    lamport_timestamp.replica_id,
1275                    SelectionSet {
1276                        selections,
1277                        lamport_timestamp,
1278                    },
1279                );
1280                self.text.lamport_clock.observe(lamport_timestamp);
1281                self.selections_update_count += 1;
1282            }
1283            Operation::UpdateCompletionTriggers {
1284                triggers,
1285                lamport_timestamp,
1286            } => {
1287                self.completion_triggers = triggers;
1288                self.text.lamport_clock.observe(lamport_timestamp);
1289            }
1290        }
1291    }
1292
1293    fn apply_diagnostic_update(
1294        &mut self,
1295        diagnostics: DiagnosticSet,
1296        lamport_timestamp: clock::Lamport,
1297        cx: &mut ModelContext<Self>,
1298    ) {
1299        if lamport_timestamp > self.diagnostics_timestamp {
1300            self.diagnostics = diagnostics;
1301            self.diagnostics_timestamp = lamport_timestamp;
1302            self.diagnostics_update_count += 1;
1303            self.text.lamport_clock.observe(lamport_timestamp);
1304            cx.notify();
1305            cx.emit(Event::DiagnosticsUpdated);
1306        }
1307    }
1308
1309    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1310        cx.emit(Event::Operation(operation));
1311    }
1312
1313    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1314        self.remote_selections.remove(&replica_id);
1315        cx.notify();
1316    }
1317
1318    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1319        let was_dirty = self.is_dirty();
1320        let old_version = self.version.clone();
1321
1322        if let Some((transaction_id, operation)) = self.text.undo() {
1323            self.send_operation(Operation::Buffer(operation), cx);
1324            self.did_edit(&old_version, was_dirty, true, cx);
1325            Some(transaction_id)
1326        } else {
1327            None
1328        }
1329    }
1330
1331    pub fn undo_to_transaction(
1332        &mut self,
1333        transaction_id: TransactionId,
1334        cx: &mut ModelContext<Self>,
1335    ) -> bool {
1336        let was_dirty = self.is_dirty();
1337        let old_version = self.version.clone();
1338
1339        let operations = self.text.undo_to_transaction(transaction_id);
1340        let undone = !operations.is_empty();
1341        for operation in operations {
1342            self.send_operation(Operation::Buffer(operation), cx);
1343        }
1344        if undone {
1345            self.did_edit(&old_version, was_dirty, true, cx)
1346        }
1347        undone
1348    }
1349
1350    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1351        let was_dirty = self.is_dirty();
1352        let old_version = self.version.clone();
1353
1354        if let Some((transaction_id, operation)) = self.text.redo() {
1355            self.send_operation(Operation::Buffer(operation), cx);
1356            self.did_edit(&old_version, was_dirty, true, cx);
1357            Some(transaction_id)
1358        } else {
1359            None
1360        }
1361    }
1362
1363    pub fn redo_to_transaction(
1364        &mut self,
1365        transaction_id: TransactionId,
1366        cx: &mut ModelContext<Self>,
1367    ) -> bool {
1368        let was_dirty = self.is_dirty();
1369        let old_version = self.version.clone();
1370
1371        let operations = self.text.redo_to_transaction(transaction_id);
1372        let redone = !operations.is_empty();
1373        for operation in operations {
1374            self.send_operation(Operation::Buffer(operation), cx);
1375        }
1376        if redone {
1377            self.did_edit(&old_version, was_dirty, true, cx)
1378        }
1379        redone
1380    }
1381
1382    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1383        self.completion_triggers = triggers.clone();
1384        let lamport_timestamp = self.text.lamport_clock.tick();
1385        self.send_operation(
1386            Operation::UpdateCompletionTriggers {
1387                triggers,
1388                lamport_timestamp,
1389            },
1390            cx,
1391        );
1392        cx.notify();
1393    }
1394
1395    pub fn completion_triggers(&self) -> &[String] {
1396        &self.completion_triggers
1397    }
1398}
1399
1400#[cfg(any(test, feature = "test-support"))]
1401impl Buffer {
1402    pub fn set_group_interval(&mut self, group_interval: Duration) {
1403        self.text.set_group_interval(group_interval);
1404    }
1405
1406    pub fn randomly_edit<T>(
1407        &mut self,
1408        rng: &mut T,
1409        old_range_count: usize,
1410        cx: &mut ModelContext<Self>,
1411    ) where
1412        T: rand::Rng,
1413    {
1414        let mut old_ranges: Vec<Range<usize>> = Vec::new();
1415        for _ in 0..old_range_count {
1416            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
1417            if last_end > self.len() {
1418                break;
1419            }
1420            old_ranges.push(self.text.random_byte_range(last_end, rng));
1421        }
1422        let new_text_len = rng.gen_range(0..10);
1423        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1424            .take(new_text_len)
1425            .collect();
1426        log::info!(
1427            "mutating buffer {} at {:?}: {:?}",
1428            self.replica_id(),
1429            old_ranges,
1430            new_text
1431        );
1432        self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
1433    }
1434
1435    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1436        let was_dirty = self.is_dirty();
1437        let old_version = self.version.clone();
1438
1439        let ops = self.text.randomly_undo_redo(rng);
1440        if !ops.is_empty() {
1441            for op in ops {
1442                self.send_operation(Operation::Buffer(op), cx);
1443                self.did_edit(&old_version, was_dirty, true, cx);
1444            }
1445        }
1446    }
1447}
1448
1449impl Entity for Buffer {
1450    type Event = Event;
1451}
1452
1453impl Deref for Buffer {
1454    type Target = TextBuffer;
1455
1456    fn deref(&self) -> &Self::Target {
1457        &self.text
1458    }
1459}
1460
1461impl BufferSnapshot {
1462    fn suggest_autoindents<'a>(
1463        &'a self,
1464        row_range: Range<u32>,
1465    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1466        let mut query_cursor = QueryCursorHandle::new();
1467        if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
1468            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1469
1470            // Get the "indentation ranges" that intersect this row range.
1471            let indent_capture_ix = grammar.indents_query.capture_index_for_name("indent");
1472            let end_capture_ix = grammar.indents_query.capture_index_for_name("end");
1473            query_cursor.set_point_range(
1474                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1475                    ..Point::new(row_range.end, 0).to_ts_point(),
1476            );
1477            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1478            for mat in query_cursor.matches(
1479                &grammar.indents_query,
1480                tree.root_node(),
1481                TextProvider(self.as_rope()),
1482            ) {
1483                let mut node_kind = "";
1484                let mut start: Option<Point> = None;
1485                let mut end: Option<Point> = None;
1486                for capture in mat.captures {
1487                    if Some(capture.index) == indent_capture_ix {
1488                        node_kind = capture.node.kind();
1489                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1490                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1491                    } else if Some(capture.index) == end_capture_ix {
1492                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
1493                    }
1494                }
1495
1496                if let Some((start, end)) = start.zip(end) {
1497                    if start.row == end.row {
1498                        continue;
1499                    }
1500
1501                    let range = start..end;
1502                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1503                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1504                        Ok(ix) => {
1505                            let prev_range = &mut indentation_ranges[ix];
1506                            prev_range.0.end = prev_range.0.end.max(range.end);
1507                        }
1508                    }
1509                }
1510            }
1511
1512            let mut prev_row = prev_non_blank_row.unwrap_or(0);
1513            Some(row_range.map(move |row| {
1514                let row_start = Point::new(row, self.indent_column_for_line(row));
1515
1516                let mut indent_from_prev_row = false;
1517                let mut outdent_to_row = u32::MAX;
1518                for (range, _node_kind) in &indentation_ranges {
1519                    if range.start.row >= row {
1520                        break;
1521                    }
1522
1523                    if range.start.row == prev_row && range.end > row_start {
1524                        indent_from_prev_row = true;
1525                    }
1526                    if range.end.row >= prev_row && range.end <= row_start {
1527                        outdent_to_row = outdent_to_row.min(range.start.row);
1528                    }
1529                }
1530
1531                let suggestion = if outdent_to_row == prev_row {
1532                    IndentSuggestion {
1533                        basis_row: prev_row,
1534                        indent: false,
1535                    }
1536                } else if indent_from_prev_row {
1537                    IndentSuggestion {
1538                        basis_row: prev_row,
1539                        indent: true,
1540                    }
1541                } else if outdent_to_row < prev_row {
1542                    IndentSuggestion {
1543                        basis_row: outdent_to_row,
1544                        indent: false,
1545                    }
1546                } else {
1547                    IndentSuggestion {
1548                        basis_row: prev_row,
1549                        indent: false,
1550                    }
1551                };
1552
1553                prev_row = row;
1554                suggestion
1555            }))
1556        } else {
1557            None
1558        }
1559    }
1560
1561    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1562        while row > 0 {
1563            row -= 1;
1564            if !self.is_line_blank(row) {
1565                return Some(row);
1566            }
1567        }
1568        None
1569    }
1570
1571    pub fn chunks<'a, T: ToOffset>(
1572        &'a self,
1573        range: Range<T>,
1574        language_aware: bool,
1575    ) -> BufferChunks<'a> {
1576        let range = range.start.to_offset(self)..range.end.to_offset(self);
1577
1578        let mut tree = None;
1579        let mut diagnostic_endpoints = Vec::new();
1580        if language_aware {
1581            tree = self.tree.as_ref();
1582            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1583                diagnostic_endpoints.push(DiagnosticEndpoint {
1584                    offset: entry.range.start,
1585                    is_start: true,
1586                    severity: entry.diagnostic.severity,
1587                    is_unnecessary: entry.diagnostic.is_unnecessary,
1588                });
1589                diagnostic_endpoints.push(DiagnosticEndpoint {
1590                    offset: entry.range.end,
1591                    is_start: false,
1592                    severity: entry.diagnostic.severity,
1593                    is_unnecessary: entry.diagnostic.is_unnecessary,
1594                });
1595            }
1596            diagnostic_endpoints
1597                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1598        }
1599
1600        BufferChunks::new(
1601            self.text.as_rope(),
1602            range,
1603            tree,
1604            self.grammar(),
1605            diagnostic_endpoints,
1606        )
1607    }
1608
1609    pub fn language(&self) -> Option<&Arc<Language>> {
1610        self.language.as_ref()
1611    }
1612
1613    fn grammar(&self) -> Option<&Arc<Grammar>> {
1614        self.language
1615            .as_ref()
1616            .and_then(|language| language.grammar.as_ref())
1617    }
1618
1619    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1620        let tree = self.tree.as_ref()?;
1621        let range = range.start.to_offset(self)..range.end.to_offset(self);
1622        let mut cursor = tree.root_node().walk();
1623
1624        // Descend to smallest leaf that touches or exceeds the start of the range.
1625        while cursor.goto_first_child_for_byte(range.start).is_some() {}
1626
1627        // Ascend to the smallest ancestor that strictly contains the range.
1628        loop {
1629            let node_range = cursor.node().byte_range();
1630            if node_range.start <= range.start
1631                && node_range.end >= range.end
1632                && node_range.len() > range.len()
1633            {
1634                break;
1635            }
1636            if !cursor.goto_parent() {
1637                break;
1638            }
1639        }
1640
1641        let left_node = cursor.node();
1642
1643        // For an empty range, try to find another node immediately to the right of the range.
1644        if left_node.end_byte() == range.start {
1645            let mut right_node = None;
1646            while !cursor.goto_next_sibling() {
1647                if !cursor.goto_parent() {
1648                    break;
1649                }
1650            }
1651
1652            while cursor.node().start_byte() == range.start {
1653                right_node = Some(cursor.node());
1654                if !cursor.goto_first_child() {
1655                    break;
1656                }
1657            }
1658
1659            if let Some(right_node) = right_node {
1660                if right_node.is_named() || !left_node.is_named() {
1661                    return Some(right_node.byte_range());
1662                }
1663            }
1664        }
1665
1666        Some(left_node.byte_range())
1667    }
1668
1669    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1670        let tree = self.tree.as_ref()?;
1671        let grammar = self
1672            .language
1673            .as_ref()
1674            .and_then(|language| language.grammar.as_ref())?;
1675
1676        let mut cursor = QueryCursorHandle::new();
1677        let matches = cursor.matches(
1678            &grammar.outline_query,
1679            tree.root_node(),
1680            TextProvider(self.as_rope()),
1681        );
1682
1683        let mut chunks = self.chunks(0..self.len(), true);
1684
1685        let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
1686        let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
1687        let context_capture_ix = grammar
1688            .outline_query
1689            .capture_index_for_name("context")
1690            .unwrap_or(u32::MAX);
1691
1692        let mut stack = Vec::<Range<usize>>::new();
1693        let items = matches
1694            .filter_map(|mat| {
1695                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1696                let range = item_node.start_byte()..item_node.end_byte();
1697                let mut text = String::new();
1698                let mut name_ranges = Vec::new();
1699                let mut highlight_ranges = Vec::new();
1700
1701                for capture in mat.captures {
1702                    let node_is_name;
1703                    if capture.index == name_capture_ix {
1704                        node_is_name = true;
1705                    } else if capture.index == context_capture_ix {
1706                        node_is_name = false;
1707                    } else {
1708                        continue;
1709                    }
1710
1711                    let range = capture.node.start_byte()..capture.node.end_byte();
1712                    if !text.is_empty() {
1713                        text.push(' ');
1714                    }
1715                    if node_is_name {
1716                        let mut start = text.len();
1717                        let end = start + range.len();
1718
1719                        // When multiple names are captured, then the matcheable text
1720                        // includes the whitespace in between the names.
1721                        if !name_ranges.is_empty() {
1722                            start -= 1;
1723                        }
1724
1725                        name_ranges.push(start..end);
1726                    }
1727
1728                    let mut offset = range.start;
1729                    chunks.seek(offset);
1730                    while let Some(mut chunk) = chunks.next() {
1731                        if chunk.text.len() > range.end - offset {
1732                            chunk.text = &chunk.text[0..(range.end - offset)];
1733                            offset = range.end;
1734                        } else {
1735                            offset += chunk.text.len();
1736                        }
1737                        let style = chunk
1738                            .syntax_highlight_id
1739                            .zip(theme)
1740                            .and_then(|(highlight, theme)| highlight.style(theme));
1741                        if let Some(style) = style {
1742                            let start = text.len();
1743                            let end = start + chunk.text.len();
1744                            highlight_ranges.push((start..end, style));
1745                        }
1746                        text.push_str(chunk.text);
1747                        if offset >= range.end {
1748                            break;
1749                        }
1750                    }
1751                }
1752
1753                while stack.last().map_or(false, |prev_range| {
1754                    !prev_range.contains(&range.start) || !prev_range.contains(&range.end)
1755                }) {
1756                    stack.pop();
1757                }
1758                stack.push(range.clone());
1759
1760                Some(OutlineItem {
1761                    depth: stack.len() - 1,
1762                    range: self.anchor_after(range.start)..self.anchor_before(range.end),
1763                    text,
1764                    highlight_ranges,
1765                    name_ranges,
1766                })
1767            })
1768            .collect::<Vec<_>>();
1769
1770        if items.is_empty() {
1771            None
1772        } else {
1773            Some(Outline::new(items))
1774        }
1775    }
1776
1777    pub fn enclosing_bracket_ranges<T: ToOffset>(
1778        &self,
1779        range: Range<T>,
1780    ) -> Option<(Range<usize>, Range<usize>)> {
1781        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1782        let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
1783        let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
1784
1785        // Find bracket pairs that *inclusively* contain the given range.
1786        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1787        let mut cursor = QueryCursorHandle::new();
1788        let matches = cursor.set_byte_range(range).matches(
1789            &grammar.brackets_query,
1790            tree.root_node(),
1791            TextProvider(self.as_rope()),
1792        );
1793
1794        // Get the ranges of the innermost pair of brackets.
1795        matches
1796            .filter_map(|mat| {
1797                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1798                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1799                Some((open.byte_range(), close.byte_range()))
1800            })
1801            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1802    }
1803
1804    pub fn remote_selections_in_range<'a>(
1805        &'a self,
1806        range: Range<Anchor>,
1807    ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
1808    {
1809        self.remote_selections
1810            .iter()
1811            .filter(|(replica_id, set)| {
1812                **replica_id != self.text.replica_id() && !set.selections.is_empty()
1813            })
1814            .map(move |(replica_id, set)| {
1815                let start_ix = match set.selections.binary_search_by(|probe| {
1816                    probe
1817                        .end
1818                        .cmp(&range.start, self)
1819                        .unwrap()
1820                        .then(Ordering::Greater)
1821                }) {
1822                    Ok(ix) | Err(ix) => ix,
1823                };
1824                let end_ix = match set.selections.binary_search_by(|probe| {
1825                    probe
1826                        .start
1827                        .cmp(&range.end, self)
1828                        .unwrap()
1829                        .then(Ordering::Less)
1830                }) {
1831                    Ok(ix) | Err(ix) => ix,
1832                };
1833
1834                (*replica_id, set.selections[start_ix..end_ix].iter())
1835            })
1836    }
1837
1838    pub fn diagnostics_in_range<'a, T, O>(
1839        &'a self,
1840        search_range: Range<T>,
1841        reversed: bool,
1842    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1843    where
1844        T: 'a + Clone + ToOffset,
1845        O: 'a + FromAnchor,
1846    {
1847        self.diagnostics
1848            .range(search_range.clone(), self, true, reversed)
1849    }
1850
1851    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1852        let mut groups = Vec::new();
1853        self.diagnostics.groups(&mut groups, self);
1854        groups
1855    }
1856
1857    pub fn diagnostic_group<'a, O>(
1858        &'a self,
1859        group_id: usize,
1860    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1861    where
1862        O: 'a + FromAnchor,
1863    {
1864        self.diagnostics.group(group_id, self)
1865    }
1866
1867    pub fn diagnostics_update_count(&self) -> usize {
1868        self.diagnostics_update_count
1869    }
1870
1871    pub fn parse_count(&self) -> usize {
1872        self.parse_count
1873    }
1874
1875    pub fn selections_update_count(&self) -> usize {
1876        self.selections_update_count
1877    }
1878
1879    pub fn path(&self) -> Option<&Arc<Path>> {
1880        self.path.as_ref()
1881    }
1882
1883    pub fn file_update_count(&self) -> usize {
1884        self.file_update_count
1885    }
1886
1887    pub fn indent_size(&self) -> u32 {
1888        self.indent_size
1889    }
1890}
1891
1892impl Clone for BufferSnapshot {
1893    fn clone(&self) -> Self {
1894        Self {
1895            text: self.text.clone(),
1896            tree: self.tree.clone(),
1897            path: self.path.clone(),
1898            remote_selections: self.remote_selections.clone(),
1899            diagnostics: self.diagnostics.clone(),
1900            selections_update_count: self.selections_update_count,
1901            diagnostics_update_count: self.diagnostics_update_count,
1902            file_update_count: self.file_update_count,
1903            language: self.language.clone(),
1904            parse_count: self.parse_count,
1905            indent_size: self.indent_size,
1906        }
1907    }
1908}
1909
1910impl Deref for BufferSnapshot {
1911    type Target = text::BufferSnapshot;
1912
1913    fn deref(&self) -> &Self::Target {
1914        &self.text
1915    }
1916}
1917
1918impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1919    type I = ByteChunks<'a>;
1920
1921    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1922        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1923    }
1924}
1925
1926pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
1927
1928impl<'a> Iterator for ByteChunks<'a> {
1929    type Item = &'a [u8];
1930
1931    fn next(&mut self) -> Option<Self::Item> {
1932        self.0.next().map(str::as_bytes)
1933    }
1934}
1935
1936unsafe impl<'a> Send for BufferChunks<'a> {}
1937
1938impl<'a> BufferChunks<'a> {
1939    pub(crate) fn new(
1940        text: &'a Rope,
1941        range: Range<usize>,
1942        tree: Option<&'a Tree>,
1943        grammar: Option<&'a Arc<Grammar>>,
1944        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
1945    ) -> Self {
1946        let mut highlights = None;
1947        if let Some((grammar, tree)) = grammar.zip(tree) {
1948            let mut query_cursor = QueryCursorHandle::new();
1949
1950            // TODO - add a Tree-sitter API to remove the need for this.
1951            let cursor = unsafe {
1952                std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
1953            };
1954            let captures = cursor.set_byte_range(range.clone()).captures(
1955                &grammar.highlights_query,
1956                tree.root_node(),
1957                TextProvider(text),
1958            );
1959            highlights = Some(BufferChunkHighlights {
1960                captures,
1961                next_capture: None,
1962                stack: Default::default(),
1963                highlight_map: grammar.highlight_map(),
1964                _query_cursor: query_cursor,
1965            })
1966        }
1967
1968        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1969        let chunks = text.chunks_in_range(range.clone());
1970
1971        BufferChunks {
1972            range,
1973            chunks,
1974            diagnostic_endpoints,
1975            error_depth: 0,
1976            warning_depth: 0,
1977            information_depth: 0,
1978            hint_depth: 0,
1979            unnecessary_depth: 0,
1980            highlights,
1981        }
1982    }
1983
1984    pub fn seek(&mut self, offset: usize) {
1985        self.range.start = offset;
1986        self.chunks.seek(self.range.start);
1987        if let Some(highlights) = self.highlights.as_mut() {
1988            highlights
1989                .stack
1990                .retain(|(end_offset, _)| *end_offset > offset);
1991            if let Some((mat, capture_ix)) = &highlights.next_capture {
1992                let capture = mat.captures[*capture_ix as usize];
1993                if offset >= capture.node.start_byte() {
1994                    let next_capture_end = capture.node.end_byte();
1995                    if offset < next_capture_end {
1996                        highlights.stack.push((
1997                            next_capture_end,
1998                            highlights.highlight_map.get(capture.index),
1999                        ));
2000                    }
2001                    highlights.next_capture.take();
2002                }
2003            }
2004            highlights.captures.set_byte_range(self.range.clone());
2005        }
2006    }
2007
2008    pub fn offset(&self) -> usize {
2009        self.range.start
2010    }
2011
2012    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2013        let depth = match endpoint.severity {
2014            DiagnosticSeverity::ERROR => &mut self.error_depth,
2015            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2016            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2017            DiagnosticSeverity::HINT => &mut self.hint_depth,
2018            _ => return,
2019        };
2020        if endpoint.is_start {
2021            *depth += 1;
2022        } else {
2023            *depth -= 1;
2024        }
2025
2026        if endpoint.is_unnecessary {
2027            if endpoint.is_start {
2028                self.unnecessary_depth += 1;
2029            } else {
2030                self.unnecessary_depth -= 1;
2031            }
2032        }
2033    }
2034
2035    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2036        if self.error_depth > 0 {
2037            Some(DiagnosticSeverity::ERROR)
2038        } else if self.warning_depth > 0 {
2039            Some(DiagnosticSeverity::WARNING)
2040        } else if self.information_depth > 0 {
2041            Some(DiagnosticSeverity::INFORMATION)
2042        } else if self.hint_depth > 0 {
2043            Some(DiagnosticSeverity::HINT)
2044        } else {
2045            None
2046        }
2047    }
2048
2049    fn current_code_is_unnecessary(&self) -> bool {
2050        self.unnecessary_depth > 0
2051    }
2052}
2053
2054impl<'a> Iterator for BufferChunks<'a> {
2055    type Item = Chunk<'a>;
2056
2057    fn next(&mut self) -> Option<Self::Item> {
2058        let mut next_capture_start = usize::MAX;
2059        let mut next_diagnostic_endpoint = usize::MAX;
2060
2061        if let Some(highlights) = self.highlights.as_mut() {
2062            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2063                if *parent_capture_end <= self.range.start {
2064                    highlights.stack.pop();
2065                } else {
2066                    break;
2067                }
2068            }
2069
2070            if highlights.next_capture.is_none() {
2071                highlights.next_capture = highlights.captures.next();
2072            }
2073
2074            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2075                let capture = mat.captures[*capture_ix as usize];
2076                if self.range.start < capture.node.start_byte() {
2077                    next_capture_start = capture.node.start_byte();
2078                    break;
2079                } else {
2080                    let highlight_id = highlights.highlight_map.get(capture.index);
2081                    highlights
2082                        .stack
2083                        .push((capture.node.end_byte(), highlight_id));
2084                    highlights.next_capture = highlights.captures.next();
2085                }
2086            }
2087        }
2088
2089        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2090            if endpoint.offset <= self.range.start {
2091                self.update_diagnostic_depths(endpoint);
2092                self.diagnostic_endpoints.next();
2093            } else {
2094                next_diagnostic_endpoint = endpoint.offset;
2095                break;
2096            }
2097        }
2098
2099        if let Some(chunk) = self.chunks.peek() {
2100            let chunk_start = self.range.start;
2101            let mut chunk_end = (self.chunks.offset() + chunk.len())
2102                .min(next_capture_start)
2103                .min(next_diagnostic_endpoint);
2104            let mut highlight_id = None;
2105            if let Some(highlights) = self.highlights.as_ref() {
2106                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2107                    chunk_end = chunk_end.min(*parent_capture_end);
2108                    highlight_id = Some(*parent_highlight_id);
2109                }
2110            }
2111
2112            let slice =
2113                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2114            self.range.start = chunk_end;
2115            if self.range.start == self.chunks.offset() + chunk.len() {
2116                self.chunks.next().unwrap();
2117            }
2118
2119            Some(Chunk {
2120                text: slice,
2121                syntax_highlight_id: highlight_id,
2122                highlight_style: None,
2123                diagnostic_severity: self.current_diagnostic_severity(),
2124                is_unnecessary: self.current_code_is_unnecessary(),
2125            })
2126        } else {
2127            None
2128        }
2129    }
2130}
2131
2132impl QueryCursorHandle {
2133    pub(crate) fn new() -> Self {
2134        QueryCursorHandle(Some(
2135            QUERY_CURSORS
2136                .lock()
2137                .pop()
2138                .unwrap_or_else(|| QueryCursor::new()),
2139        ))
2140    }
2141}
2142
2143impl Deref for QueryCursorHandle {
2144    type Target = QueryCursor;
2145
2146    fn deref(&self) -> &Self::Target {
2147        self.0.as_ref().unwrap()
2148    }
2149}
2150
2151impl DerefMut for QueryCursorHandle {
2152    fn deref_mut(&mut self) -> &mut Self::Target {
2153        self.0.as_mut().unwrap()
2154    }
2155}
2156
2157impl Drop for QueryCursorHandle {
2158    fn drop(&mut self) {
2159        let mut cursor = self.0.take().unwrap();
2160        cursor.set_byte_range(0..usize::MAX);
2161        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2162        QUERY_CURSORS.lock().push(cursor)
2163    }
2164}
2165
2166trait ToTreeSitterPoint {
2167    fn to_ts_point(self) -> tree_sitter::Point;
2168    fn from_ts_point(point: tree_sitter::Point) -> Self;
2169}
2170
2171impl ToTreeSitterPoint for Point {
2172    fn to_ts_point(self) -> tree_sitter::Point {
2173        tree_sitter::Point::new(self.row as usize, self.column as usize)
2174    }
2175
2176    fn from_ts_point(point: tree_sitter::Point) -> Self {
2177        Point::new(point.row as u32, point.column as u32)
2178    }
2179}
2180
2181impl operation_queue::Operation for Operation {
2182    fn lamport_timestamp(&self) -> clock::Lamport {
2183        match self {
2184            Operation::Buffer(_) => {
2185                unreachable!("buffer operations should never be deferred at this layer")
2186            }
2187            Operation::UpdateDiagnostics {
2188                lamport_timestamp, ..
2189            }
2190            | Operation::UpdateSelections {
2191                lamport_timestamp, ..
2192            }
2193            | Operation::UpdateCompletionTriggers {
2194                lamport_timestamp, ..
2195            } => *lamport_timestamp,
2196        }
2197    }
2198}
2199
2200impl Default for Diagnostic {
2201    fn default() -> Self {
2202        Self {
2203            code: Default::default(),
2204            severity: DiagnosticSeverity::ERROR,
2205            message: Default::default(),
2206            group_id: Default::default(),
2207            is_primary: Default::default(),
2208            is_valid: true,
2209            is_disk_based: false,
2210            is_unnecessary: false,
2211        }
2212    }
2213}
2214
2215impl Completion {
2216    pub fn sort_key(&self) -> (usize, &str) {
2217        let kind_key = match self.lsp_completion.kind {
2218            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2219            _ => 1,
2220        };
2221        (kind_key, &self.label.text[self.label.filter_range.clone()])
2222    }
2223
2224    pub fn is_snippet(&self) -> bool {
2225        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2226    }
2227}
2228
2229pub fn contiguous_ranges(
2230    values: impl Iterator<Item = u32>,
2231    max_len: usize,
2232) -> impl Iterator<Item = Range<u32>> {
2233    let mut values = values.into_iter();
2234    let mut current_range: Option<Range<u32>> = None;
2235    std::iter::from_fn(move || loop {
2236        if let Some(value) = values.next() {
2237            if let Some(range) = &mut current_range {
2238                if value == range.end && range.len() < max_len {
2239                    range.end += 1;
2240                    continue;
2241                }
2242            }
2243
2244            let prev_range = current_range.clone();
2245            current_range = Some(value..(value + 1));
2246            if prev_range.is_some() {
2247                return prev_range;
2248            }
2249        } else {
2250            return current_range.take();
2251        }
2252    })
2253}
2254
2255pub fn char_kind(c: char) -> CharKind {
2256    if c.is_whitespace() {
2257        CharKind::Whitespace
2258    } else if c.is_alphanumeric() || c == '_' {
2259        CharKind::Word
2260    } else {
2261        CharKind::Punctuation
2262    }
2263}