buffer.rs

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