buffer.rs

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