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