buffer.rs

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