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