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 the first leaf that touches the start of the range,
1625        // and if the range is non-empty, extends beyond the start.
1626        while cursor.goto_first_child_for_byte(range.start).is_some() {
1627            if !range.is_empty() && cursor.node().end_byte() == range.start {
1628                cursor.goto_next_sibling();
1629            }
1630        }
1631
1632        // Ascend to the smallest ancestor that strictly contains the range.
1633        loop {
1634            let node_range = cursor.node().byte_range();
1635            if node_range.start <= range.start
1636                && node_range.end >= range.end
1637                && node_range.len() > range.len()
1638            {
1639                break;
1640            }
1641            if !cursor.goto_parent() {
1642                break;
1643            }
1644        }
1645
1646        let left_node = cursor.node();
1647
1648        // For an empty range, try to find another node immediately to the right of the range.
1649        if left_node.end_byte() == range.start {
1650            let mut right_node = None;
1651            while !cursor.goto_next_sibling() {
1652                if !cursor.goto_parent() {
1653                    break;
1654                }
1655            }
1656
1657            while cursor.node().start_byte() == range.start {
1658                right_node = Some(cursor.node());
1659                if !cursor.goto_first_child() {
1660                    break;
1661                }
1662            }
1663
1664            // If there is a candidate node on both sides of the (empty) range, then
1665            // decide between the two by favoring a named node over an anonymous token.
1666            // If both nodes are the same in that regard, favor the right one.
1667            if let Some(right_node) = right_node {
1668                if right_node.is_named() || !left_node.is_named() {
1669                    return Some(right_node.byte_range());
1670                }
1671            }
1672        }
1673
1674        Some(left_node.byte_range())
1675    }
1676
1677    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1678        let tree = self.tree.as_ref()?;
1679        let grammar = self
1680            .language
1681            .as_ref()
1682            .and_then(|language| language.grammar.as_ref())?;
1683
1684        let mut cursor = QueryCursorHandle::new();
1685        let matches = cursor.matches(
1686            &grammar.outline_query,
1687            tree.root_node(),
1688            TextProvider(self.as_rope()),
1689        );
1690
1691        let mut chunks = self.chunks(0..self.len(), true);
1692
1693        let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
1694        let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
1695        let context_capture_ix = grammar
1696            .outline_query
1697            .capture_index_for_name("context")
1698            .unwrap_or(u32::MAX);
1699
1700        let mut stack = Vec::<Range<usize>>::new();
1701        let items = matches
1702            .filter_map(|mat| {
1703                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1704                let range = item_node.start_byte()..item_node.end_byte();
1705                let mut text = String::new();
1706                let mut name_ranges = Vec::new();
1707                let mut highlight_ranges = Vec::new();
1708
1709                for capture in mat.captures {
1710                    let node_is_name;
1711                    if capture.index == name_capture_ix {
1712                        node_is_name = true;
1713                    } else if capture.index == context_capture_ix {
1714                        node_is_name = false;
1715                    } else {
1716                        continue;
1717                    }
1718
1719                    let range = capture.node.start_byte()..capture.node.end_byte();
1720                    if !text.is_empty() {
1721                        text.push(' ');
1722                    }
1723                    if node_is_name {
1724                        let mut start = text.len();
1725                        let end = start + range.len();
1726
1727                        // When multiple names are captured, then the matcheable text
1728                        // includes the whitespace in between the names.
1729                        if !name_ranges.is_empty() {
1730                            start -= 1;
1731                        }
1732
1733                        name_ranges.push(start..end);
1734                    }
1735
1736                    let mut offset = range.start;
1737                    chunks.seek(offset);
1738                    while let Some(mut chunk) = chunks.next() {
1739                        if chunk.text.len() > range.end - offset {
1740                            chunk.text = &chunk.text[0..(range.end - offset)];
1741                            offset = range.end;
1742                        } else {
1743                            offset += chunk.text.len();
1744                        }
1745                        let style = chunk
1746                            .syntax_highlight_id
1747                            .zip(theme)
1748                            .and_then(|(highlight, theme)| highlight.style(theme));
1749                        if let Some(style) = style {
1750                            let start = text.len();
1751                            let end = start + chunk.text.len();
1752                            highlight_ranges.push((start..end, style));
1753                        }
1754                        text.push_str(chunk.text);
1755                        if offset >= range.end {
1756                            break;
1757                        }
1758                    }
1759                }
1760
1761                while stack.last().map_or(false, |prev_range| {
1762                    !prev_range.contains(&range.start) || !prev_range.contains(&range.end)
1763                }) {
1764                    stack.pop();
1765                }
1766                stack.push(range.clone());
1767
1768                Some(OutlineItem {
1769                    depth: stack.len() - 1,
1770                    range: self.anchor_after(range.start)..self.anchor_before(range.end),
1771                    text,
1772                    highlight_ranges,
1773                    name_ranges,
1774                })
1775            })
1776            .collect::<Vec<_>>();
1777
1778        if items.is_empty() {
1779            None
1780        } else {
1781            Some(Outline::new(items))
1782        }
1783    }
1784
1785    pub fn enclosing_bracket_ranges<T: ToOffset>(
1786        &self,
1787        range: Range<T>,
1788    ) -> Option<(Range<usize>, Range<usize>)> {
1789        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1790        let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
1791        let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
1792
1793        // Find bracket pairs that *inclusively* contain the given range.
1794        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1795        let mut cursor = QueryCursorHandle::new();
1796        let matches = cursor.set_byte_range(range).matches(
1797            &grammar.brackets_query,
1798            tree.root_node(),
1799            TextProvider(self.as_rope()),
1800        );
1801
1802        // Get the ranges of the innermost pair of brackets.
1803        matches
1804            .filter_map(|mat| {
1805                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1806                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1807                Some((open.byte_range(), close.byte_range()))
1808            })
1809            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1810    }
1811
1812    pub fn remote_selections_in_range<'a>(
1813        &'a self,
1814        range: Range<Anchor>,
1815    ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
1816    {
1817        self.remote_selections
1818            .iter()
1819            .filter(|(replica_id, set)| {
1820                **replica_id != self.text.replica_id() && !set.selections.is_empty()
1821            })
1822            .map(move |(replica_id, set)| {
1823                let start_ix = match set.selections.binary_search_by(|probe| {
1824                    probe
1825                        .end
1826                        .cmp(&range.start, self)
1827                        .unwrap()
1828                        .then(Ordering::Greater)
1829                }) {
1830                    Ok(ix) | Err(ix) => ix,
1831                };
1832                let end_ix = match set.selections.binary_search_by(|probe| {
1833                    probe
1834                        .start
1835                        .cmp(&range.end, self)
1836                        .unwrap()
1837                        .then(Ordering::Less)
1838                }) {
1839                    Ok(ix) | Err(ix) => ix,
1840                };
1841
1842                (*replica_id, set.selections[start_ix..end_ix].iter())
1843            })
1844    }
1845
1846    pub fn diagnostics_in_range<'a, T, O>(
1847        &'a self,
1848        search_range: Range<T>,
1849        reversed: bool,
1850    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1851    where
1852        T: 'a + Clone + ToOffset,
1853        O: 'a + FromAnchor,
1854    {
1855        self.diagnostics
1856            .range(search_range.clone(), self, true, reversed)
1857    }
1858
1859    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1860        let mut groups = Vec::new();
1861        self.diagnostics.groups(&mut groups, self);
1862        groups
1863    }
1864
1865    pub fn diagnostic_group<'a, O>(
1866        &'a self,
1867        group_id: usize,
1868    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1869    where
1870        O: 'a + FromAnchor,
1871    {
1872        self.diagnostics.group(group_id, self)
1873    }
1874
1875    pub fn diagnostics_update_count(&self) -> usize {
1876        self.diagnostics_update_count
1877    }
1878
1879    pub fn parse_count(&self) -> usize {
1880        self.parse_count
1881    }
1882
1883    pub fn selections_update_count(&self) -> usize {
1884        self.selections_update_count
1885    }
1886
1887    pub fn path(&self) -> Option<&Arc<Path>> {
1888        self.path.as_ref()
1889    }
1890
1891    pub fn file_update_count(&self) -> usize {
1892        self.file_update_count
1893    }
1894
1895    pub fn indent_size(&self) -> u32 {
1896        self.indent_size
1897    }
1898}
1899
1900impl Clone for BufferSnapshot {
1901    fn clone(&self) -> Self {
1902        Self {
1903            text: self.text.clone(),
1904            tree: self.tree.clone(),
1905            path: self.path.clone(),
1906            remote_selections: self.remote_selections.clone(),
1907            diagnostics: self.diagnostics.clone(),
1908            selections_update_count: self.selections_update_count,
1909            diagnostics_update_count: self.diagnostics_update_count,
1910            file_update_count: self.file_update_count,
1911            language: self.language.clone(),
1912            parse_count: self.parse_count,
1913            indent_size: self.indent_size,
1914        }
1915    }
1916}
1917
1918impl Deref for BufferSnapshot {
1919    type Target = text::BufferSnapshot;
1920
1921    fn deref(&self) -> &Self::Target {
1922        &self.text
1923    }
1924}
1925
1926impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1927    type I = ByteChunks<'a>;
1928
1929    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1930        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1931    }
1932}
1933
1934pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
1935
1936impl<'a> Iterator for ByteChunks<'a> {
1937    type Item = &'a [u8];
1938
1939    fn next(&mut self) -> Option<Self::Item> {
1940        self.0.next().map(str::as_bytes)
1941    }
1942}
1943
1944unsafe impl<'a> Send for BufferChunks<'a> {}
1945
1946impl<'a> BufferChunks<'a> {
1947    pub(crate) fn new(
1948        text: &'a Rope,
1949        range: Range<usize>,
1950        tree: Option<&'a Tree>,
1951        grammar: Option<&'a Arc<Grammar>>,
1952        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
1953    ) -> Self {
1954        let mut highlights = None;
1955        if let Some((grammar, tree)) = grammar.zip(tree) {
1956            let mut query_cursor = QueryCursorHandle::new();
1957
1958            // TODO - add a Tree-sitter API to remove the need for this.
1959            let cursor = unsafe {
1960                std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
1961            };
1962            let captures = cursor.set_byte_range(range.clone()).captures(
1963                &grammar.highlights_query,
1964                tree.root_node(),
1965                TextProvider(text),
1966            );
1967            highlights = Some(BufferChunkHighlights {
1968                captures,
1969                next_capture: None,
1970                stack: Default::default(),
1971                highlight_map: grammar.highlight_map(),
1972                _query_cursor: query_cursor,
1973            })
1974        }
1975
1976        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1977        let chunks = text.chunks_in_range(range.clone());
1978
1979        BufferChunks {
1980            range,
1981            chunks,
1982            diagnostic_endpoints,
1983            error_depth: 0,
1984            warning_depth: 0,
1985            information_depth: 0,
1986            hint_depth: 0,
1987            unnecessary_depth: 0,
1988            highlights,
1989        }
1990    }
1991
1992    pub fn seek(&mut self, offset: usize) {
1993        self.range.start = offset;
1994        self.chunks.seek(self.range.start);
1995        if let Some(highlights) = self.highlights.as_mut() {
1996            highlights
1997                .stack
1998                .retain(|(end_offset, _)| *end_offset > offset);
1999            if let Some((mat, capture_ix)) = &highlights.next_capture {
2000                let capture = mat.captures[*capture_ix as usize];
2001                if offset >= capture.node.start_byte() {
2002                    let next_capture_end = capture.node.end_byte();
2003                    if offset < next_capture_end {
2004                        highlights.stack.push((
2005                            next_capture_end,
2006                            highlights.highlight_map.get(capture.index),
2007                        ));
2008                    }
2009                    highlights.next_capture.take();
2010                }
2011            }
2012            highlights.captures.set_byte_range(self.range.clone());
2013        }
2014    }
2015
2016    pub fn offset(&self) -> usize {
2017        self.range.start
2018    }
2019
2020    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2021        let depth = match endpoint.severity {
2022            DiagnosticSeverity::ERROR => &mut self.error_depth,
2023            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2024            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2025            DiagnosticSeverity::HINT => &mut self.hint_depth,
2026            _ => return,
2027        };
2028        if endpoint.is_start {
2029            *depth += 1;
2030        } else {
2031            *depth -= 1;
2032        }
2033
2034        if endpoint.is_unnecessary {
2035            if endpoint.is_start {
2036                self.unnecessary_depth += 1;
2037            } else {
2038                self.unnecessary_depth -= 1;
2039            }
2040        }
2041    }
2042
2043    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2044        if self.error_depth > 0 {
2045            Some(DiagnosticSeverity::ERROR)
2046        } else if self.warning_depth > 0 {
2047            Some(DiagnosticSeverity::WARNING)
2048        } else if self.information_depth > 0 {
2049            Some(DiagnosticSeverity::INFORMATION)
2050        } else if self.hint_depth > 0 {
2051            Some(DiagnosticSeverity::HINT)
2052        } else {
2053            None
2054        }
2055    }
2056
2057    fn current_code_is_unnecessary(&self) -> bool {
2058        self.unnecessary_depth > 0
2059    }
2060}
2061
2062impl<'a> Iterator for BufferChunks<'a> {
2063    type Item = Chunk<'a>;
2064
2065    fn next(&mut self) -> Option<Self::Item> {
2066        let mut next_capture_start = usize::MAX;
2067        let mut next_diagnostic_endpoint = usize::MAX;
2068
2069        if let Some(highlights) = self.highlights.as_mut() {
2070            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2071                if *parent_capture_end <= self.range.start {
2072                    highlights.stack.pop();
2073                } else {
2074                    break;
2075                }
2076            }
2077
2078            if highlights.next_capture.is_none() {
2079                highlights.next_capture = highlights.captures.next();
2080            }
2081
2082            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2083                let capture = mat.captures[*capture_ix as usize];
2084                if self.range.start < capture.node.start_byte() {
2085                    next_capture_start = capture.node.start_byte();
2086                    break;
2087                } else {
2088                    let highlight_id = highlights.highlight_map.get(capture.index);
2089                    highlights
2090                        .stack
2091                        .push((capture.node.end_byte(), highlight_id));
2092                    highlights.next_capture = highlights.captures.next();
2093                }
2094            }
2095        }
2096
2097        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2098            if endpoint.offset <= self.range.start {
2099                self.update_diagnostic_depths(endpoint);
2100                self.diagnostic_endpoints.next();
2101            } else {
2102                next_diagnostic_endpoint = endpoint.offset;
2103                break;
2104            }
2105        }
2106
2107        if let Some(chunk) = self.chunks.peek() {
2108            let chunk_start = self.range.start;
2109            let mut chunk_end = (self.chunks.offset() + chunk.len())
2110                .min(next_capture_start)
2111                .min(next_diagnostic_endpoint);
2112            let mut highlight_id = None;
2113            if let Some(highlights) = self.highlights.as_ref() {
2114                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2115                    chunk_end = chunk_end.min(*parent_capture_end);
2116                    highlight_id = Some(*parent_highlight_id);
2117                }
2118            }
2119
2120            let slice =
2121                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2122            self.range.start = chunk_end;
2123            if self.range.start == self.chunks.offset() + chunk.len() {
2124                self.chunks.next().unwrap();
2125            }
2126
2127            Some(Chunk {
2128                text: slice,
2129                syntax_highlight_id: highlight_id,
2130                highlight_style: None,
2131                diagnostic_severity: self.current_diagnostic_severity(),
2132                is_unnecessary: self.current_code_is_unnecessary(),
2133            })
2134        } else {
2135            None
2136        }
2137    }
2138}
2139
2140impl QueryCursorHandle {
2141    pub(crate) fn new() -> Self {
2142        QueryCursorHandle(Some(
2143            QUERY_CURSORS
2144                .lock()
2145                .pop()
2146                .unwrap_or_else(|| QueryCursor::new()),
2147        ))
2148    }
2149}
2150
2151impl Deref for QueryCursorHandle {
2152    type Target = QueryCursor;
2153
2154    fn deref(&self) -> &Self::Target {
2155        self.0.as_ref().unwrap()
2156    }
2157}
2158
2159impl DerefMut for QueryCursorHandle {
2160    fn deref_mut(&mut self) -> &mut Self::Target {
2161        self.0.as_mut().unwrap()
2162    }
2163}
2164
2165impl Drop for QueryCursorHandle {
2166    fn drop(&mut self) {
2167        let mut cursor = self.0.take().unwrap();
2168        cursor.set_byte_range(0..usize::MAX);
2169        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2170        QUERY_CURSORS.lock().push(cursor)
2171    }
2172}
2173
2174trait ToTreeSitterPoint {
2175    fn to_ts_point(self) -> tree_sitter::Point;
2176    fn from_ts_point(point: tree_sitter::Point) -> Self;
2177}
2178
2179impl ToTreeSitterPoint for Point {
2180    fn to_ts_point(self) -> tree_sitter::Point {
2181        tree_sitter::Point::new(self.row as usize, self.column as usize)
2182    }
2183
2184    fn from_ts_point(point: tree_sitter::Point) -> Self {
2185        Point::new(point.row as u32, point.column as u32)
2186    }
2187}
2188
2189impl operation_queue::Operation for Operation {
2190    fn lamport_timestamp(&self) -> clock::Lamport {
2191        match self {
2192            Operation::Buffer(_) => {
2193                unreachable!("buffer operations should never be deferred at this layer")
2194            }
2195            Operation::UpdateDiagnostics {
2196                lamport_timestamp, ..
2197            }
2198            | Operation::UpdateSelections {
2199                lamport_timestamp, ..
2200            }
2201            | Operation::UpdateCompletionTriggers {
2202                lamport_timestamp, ..
2203            } => *lamport_timestamp,
2204        }
2205    }
2206}
2207
2208impl Default for Diagnostic {
2209    fn default() -> Self {
2210        Self {
2211            code: Default::default(),
2212            severity: DiagnosticSeverity::ERROR,
2213            message: Default::default(),
2214            group_id: Default::default(),
2215            is_primary: Default::default(),
2216            is_valid: true,
2217            is_disk_based: false,
2218            is_unnecessary: false,
2219        }
2220    }
2221}
2222
2223impl Completion {
2224    pub fn sort_key(&self) -> (usize, &str) {
2225        let kind_key = match self.lsp_completion.kind {
2226            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2227            _ => 1,
2228        };
2229        (kind_key, &self.label.text[self.label.filter_range.clone()])
2230    }
2231
2232    pub fn is_snippet(&self) -> bool {
2233        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2234    }
2235}
2236
2237pub fn contiguous_ranges(
2238    values: impl Iterator<Item = u32>,
2239    max_len: usize,
2240) -> impl Iterator<Item = Range<u32>> {
2241    let mut values = values.into_iter();
2242    let mut current_range: Option<Range<u32>> = None;
2243    std::iter::from_fn(move || loop {
2244        if let Some(value) = values.next() {
2245            if let Some(range) = &mut current_range {
2246                if value == range.end && range.len() < max_len {
2247                    range.end += 1;
2248                    continue;
2249                }
2250            }
2251
2252            let prev_range = current_range.clone();
2253            current_range = Some(value..(value + 1));
2254            if prev_range.is_some() {
2255                return prev_range;
2256            }
2257        } else {
2258            return current_range.take();
2259        }
2260    })
2261}
2262
2263pub fn char_kind(c: char) -> CharKind {
2264    if c.is_whitespace() {
2265        CharKind::Whitespace
2266    } else if c.is_alphanumeric() || c == '_' {
2267        CharKind::Word
2268    } else {
2269        CharKind::Punctuation
2270    }
2271}