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                offset..offset,
 884                " ".repeat((column - current_column) as usize),
 885                cx,
 886            );
 887        } else if column < current_column {
 888            self.edit(
 889                Point::new(row, 0)..Point::new(row, current_column - column),
 890                "",
 891                cx,
 892            );
 893        }
 894    }
 895
 896    pub(crate) fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
 897        // TODO: it would be nice to not allocate here.
 898        let old_text = self.text();
 899        let base_version = self.version();
 900        cx.background().spawn(async move {
 901            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
 902                .iter_all_changes()
 903                .map(|c| (c.tag(), c.value().len()))
 904                .collect::<Vec<_>>();
 905            Diff {
 906                base_version,
 907                new_text,
 908                changes,
 909                start_offset: 0,
 910            }
 911        })
 912    }
 913
 914    pub(crate) fn apply_diff(
 915        &mut self,
 916        diff: Diff,
 917        cx: &mut ModelContext<Self>,
 918    ) -> Option<&Transaction> {
 919        if self.version == diff.base_version {
 920            self.finalize_last_transaction();
 921            self.start_transaction();
 922            let mut offset = diff.start_offset;
 923            for (tag, len) in diff.changes {
 924                let range = offset..(offset + len);
 925                match tag {
 926                    ChangeTag::Equal => offset += len,
 927                    ChangeTag::Delete => {
 928                        self.edit(range, "", cx);
 929                    }
 930                    ChangeTag::Insert => {
 931                        self.edit(
 932                            offset..offset,
 933                            &diff.new_text
 934                                [range.start - diff.start_offset..range.end - diff.start_offset],
 935                            cx,
 936                        );
 937                        offset += len;
 938                    }
 939                }
 940            }
 941            if self.end_transaction(cx).is_some() {
 942                self.finalize_last_transaction()
 943            } else {
 944                None
 945            }
 946        } else {
 947            None
 948        }
 949    }
 950
 951    pub fn is_dirty(&self) -> bool {
 952        !self.saved_version.observed_all(&self.version)
 953            || self.file.as_ref().map_or(false, |file| file.is_deleted())
 954    }
 955
 956    pub fn has_conflict(&self) -> bool {
 957        !self.saved_version.observed_all(&self.version)
 958            && self
 959                .file
 960                .as_ref()
 961                .map_or(false, |file| file.mtime() > self.saved_mtime)
 962    }
 963
 964    pub fn subscribe(&mut self) -> Subscription {
 965        self.text.subscribe()
 966    }
 967
 968    pub fn start_transaction(&mut self) -> Option<TransactionId> {
 969        self.start_transaction_at(Instant::now())
 970    }
 971
 972    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
 973        self.text.start_transaction_at(now)
 974    }
 975
 976    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
 977        self.end_transaction_at(Instant::now(), cx)
 978    }
 979
 980    pub fn end_transaction_at(
 981        &mut self,
 982        now: Instant,
 983        cx: &mut ModelContext<Self>,
 984    ) -> Option<TransactionId> {
 985        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
 986            let was_dirty = start_version != self.saved_version;
 987            self.did_edit(&start_version, was_dirty, cx);
 988            Some(transaction_id)
 989        } else {
 990            None
 991        }
 992    }
 993
 994    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
 995        self.text.push_transaction(transaction, now);
 996    }
 997
 998    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
 999        self.text.finalize_last_transaction()
1000    }
1001
1002    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1003        self.text.forget_transaction(transaction_id);
1004    }
1005
1006    pub fn wait_for_edits(
1007        &mut self,
1008        edit_ids: impl IntoIterator<Item = clock::Local>,
1009    ) -> impl Future<Output = ()> {
1010        self.text.wait_for_edits(edit_ids)
1011    }
1012
1013    pub fn wait_for_anchors<'a>(
1014        &mut self,
1015        anchors: impl IntoIterator<Item = &'a Anchor>,
1016    ) -> impl Future<Output = ()> {
1017        self.text.wait_for_anchors(anchors)
1018    }
1019
1020    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1021        self.text.wait_for_version(version)
1022    }
1023
1024    pub fn set_active_selections(
1025        &mut self,
1026        selections: Arc<[Selection<Anchor>]>,
1027        cx: &mut ModelContext<Self>,
1028    ) {
1029        let lamport_timestamp = self.text.lamport_clock.tick();
1030        self.remote_selections.insert(
1031            self.text.replica_id(),
1032            SelectionSet {
1033                selections: selections.clone(),
1034                lamport_timestamp,
1035            },
1036        );
1037        self.send_operation(
1038            Operation::UpdateSelections {
1039                selections,
1040                lamport_timestamp,
1041            },
1042            cx,
1043        );
1044    }
1045
1046    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1047        self.set_active_selections(Arc::from([]), cx);
1048    }
1049
1050    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
1051    where
1052        T: Into<Arc<str>>,
1053    {
1054        self.edit_internal([(0..self.len(), text)], None, cx)
1055    }
1056
1057    pub fn edit<S, T>(
1058        &mut self,
1059        range: Range<S>,
1060        new_text: T,
1061        cx: &mut ModelContext<Self>,
1062    ) -> Option<clock::Local>
1063    where
1064        S: ToOffset,
1065        T: Into<Arc<str>>,
1066    {
1067        self.edit_batched([(range, new_text)], cx)
1068    }
1069
1070    pub fn edit_batched<I, S, T>(
1071        &mut self,
1072        edits_iter: I,
1073        cx: &mut ModelContext<Self>,
1074    ) -> Option<clock::Local>
1075    where
1076        I: IntoIterator<Item = (Range<S>, T)>,
1077        S: ToOffset,
1078        T: Into<Arc<str>>,
1079    {
1080        self.edit_internal(edits_iter, None, cx)
1081    }
1082
1083    pub fn edit_with_autoindent<S, T>(
1084        &mut self,
1085        range: Range<S>,
1086        new_text: T,
1087        indent_size: u32,
1088        cx: &mut ModelContext<Self>,
1089    ) -> Option<clock::Local>
1090    where
1091        S: ToOffset,
1092        T: Into<Arc<str>>,
1093    {
1094        self.edit_with_autoindent_batched([(range, new_text)], indent_size, cx)
1095    }
1096
1097    pub fn edit_with_autoindent_batched<I, S, T>(
1098        &mut self,
1099        edits_iter: I,
1100        indent_size: u32,
1101        cx: &mut ModelContext<Self>,
1102    ) -> Option<clock::Local>
1103    where
1104        I: IntoIterator<Item = (Range<S>, T)>,
1105        S: ToOffset,
1106        T: Into<Arc<str>>,
1107    {
1108        self.edit_internal(edits_iter, Some(indent_size), cx)
1109    }
1110
1111    pub fn edit_internal<I, S, T>(
1112        &mut self,
1113        edits_iter: I,
1114        autoindent_size: Option<u32>,
1115        cx: &mut ModelContext<Self>,
1116    ) -> Option<clock::Local>
1117    where
1118        I: IntoIterator<Item = (Range<S>, T)>,
1119        S: ToOffset,
1120        T: Into<Arc<str>>,
1121    {
1122        // Skip invalid edits and coalesce contiguous ones.
1123        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1124        for (range, new_text) in edits_iter {
1125            let range = range.start.to_offset(self)..range.end.to_offset(self);
1126            let new_text = new_text.into();
1127            if !new_text.is_empty() || !range.is_empty() {
1128                if let Some((prev_range, prev_text)) = edits.last_mut() {
1129                    if prev_range.end >= range.start {
1130                        prev_range.end = cmp::max(prev_range.end, range.end);
1131                        *prev_text = format!("{prev_text}{new_text}").into();
1132                    } else {
1133                        edits.push((range, new_text));
1134                    }
1135                } else {
1136                    edits.push((range, new_text));
1137                }
1138            }
1139        }
1140        if edits.is_empty() {
1141            return None;
1142        }
1143
1144        self.start_transaction();
1145        self.pending_autoindent.take();
1146        let autoindent_request =
1147            self.language
1148                .as_ref()
1149                .and_then(|_| autoindent_size)
1150                .map(|autoindent_size| {
1151                    let before_edit = self.snapshot();
1152                    let edited = edits
1153                        .iter()
1154                        .filter_map(|(range, new_text)| {
1155                            let start = range.start.to_point(self);
1156                            if new_text.starts_with('\n')
1157                                && start.column == self.line_len(start.row)
1158                            {
1159                                None
1160                            } else {
1161                                Some(self.anchor_before(range.start))
1162                            }
1163                        })
1164                        .collect();
1165                    (before_edit, edited, autoindent_size)
1166                });
1167
1168        let edit_operation = self.text.edit_batched(edits.iter().cloned());
1169        let edit_id = edit_operation.local_timestamp();
1170
1171        if let Some((before_edit, edited, size)) = autoindent_request {
1172            let mut delta = 0isize;
1173
1174            let inserted_ranges = edits
1175                .into_iter()
1176                .filter_map(|(range, new_text)| {
1177                    let first_newline_ix = new_text.find('\n')?;
1178                    let new_text_len = new_text.len();
1179                    let start = (delta + range.start as isize) as usize + first_newline_ix + 1;
1180                    let end = (delta + range.start as isize) as usize + new_text_len;
1181                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1182                    dbg!(&range, new_text, start, end, delta);
1183                    Some(self.anchor_before(start)..self.anchor_after(end))
1184                })
1185                .collect::<Vec<Range<Anchor>>>();
1186
1187            let inserted = if inserted_ranges.is_empty() {
1188                None
1189            } else {
1190                Some(inserted_ranges)
1191            };
1192
1193            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1194                before_edit,
1195                edited,
1196                inserted,
1197                indent_size: size,
1198            }));
1199        }
1200
1201        self.end_transaction(cx);
1202        self.send_operation(Operation::Buffer(edit_operation), cx);
1203        Some(edit_id)
1204    }
1205
1206    fn did_edit(
1207        &mut self,
1208        old_version: &clock::Global,
1209        was_dirty: bool,
1210        cx: &mut ModelContext<Self>,
1211    ) {
1212        if self.edits_since::<usize>(old_version).next().is_none() {
1213            return;
1214        }
1215
1216        self.reparse(cx);
1217
1218        cx.emit(Event::Edited);
1219        if !was_dirty {
1220            cx.emit(Event::Dirtied);
1221        }
1222        cx.notify();
1223    }
1224
1225    fn grammar(&self) -> Option<&Arc<Grammar>> {
1226        self.language.as_ref().and_then(|l| l.grammar.as_ref())
1227    }
1228
1229    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1230        &mut self,
1231        ops: I,
1232        cx: &mut ModelContext<Self>,
1233    ) -> Result<()> {
1234        self.pending_autoindent.take();
1235        let was_dirty = self.is_dirty();
1236        let old_version = self.version.clone();
1237        let mut deferred_ops = Vec::new();
1238        let buffer_ops = ops
1239            .into_iter()
1240            .filter_map(|op| match op {
1241                Operation::Buffer(op) => Some(op),
1242                _ => {
1243                    if self.can_apply_op(&op) {
1244                        self.apply_op(op, cx);
1245                    } else {
1246                        deferred_ops.push(op);
1247                    }
1248                    None
1249                }
1250            })
1251            .collect::<Vec<_>>();
1252        self.text.apply_ops(buffer_ops)?;
1253        self.deferred_ops.insert(deferred_ops);
1254        self.flush_deferred_ops(cx);
1255        self.did_edit(&old_version, was_dirty, cx);
1256        // Notify independently of whether the buffer was edited as the operations could include a
1257        // selection update.
1258        cx.notify();
1259        Ok(())
1260    }
1261
1262    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1263        let mut deferred_ops = Vec::new();
1264        for op in self.deferred_ops.drain().iter().cloned() {
1265            if self.can_apply_op(&op) {
1266                self.apply_op(op, cx);
1267            } else {
1268                deferred_ops.push(op);
1269            }
1270        }
1271        self.deferred_ops.insert(deferred_ops);
1272    }
1273
1274    fn can_apply_op(&self, operation: &Operation) -> bool {
1275        match operation {
1276            Operation::Buffer(_) => {
1277                unreachable!("buffer operations should never be applied at this layer")
1278            }
1279            Operation::UpdateDiagnostics {
1280                diagnostics: diagnostic_set,
1281                ..
1282            } => diagnostic_set.iter().all(|diagnostic| {
1283                self.text.can_resolve(&diagnostic.range.start)
1284                    && self.text.can_resolve(&diagnostic.range.end)
1285            }),
1286            Operation::UpdateSelections { selections, .. } => selections
1287                .iter()
1288                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1289            Operation::UpdateCompletionTriggers { .. } => true,
1290        }
1291    }
1292
1293    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1294        match operation {
1295            Operation::Buffer(_) => {
1296                unreachable!("buffer operations should never be applied at this layer")
1297            }
1298            Operation::UpdateDiagnostics {
1299                diagnostics: diagnostic_set,
1300                lamport_timestamp,
1301            } => {
1302                let snapshot = self.snapshot();
1303                self.apply_diagnostic_update(
1304                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1305                    lamport_timestamp,
1306                    cx,
1307                );
1308            }
1309            Operation::UpdateSelections {
1310                selections,
1311                lamport_timestamp,
1312            } => {
1313                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1314                    if set.lamport_timestamp > lamport_timestamp {
1315                        return;
1316                    }
1317                }
1318
1319                self.remote_selections.insert(
1320                    lamport_timestamp.replica_id,
1321                    SelectionSet {
1322                        selections,
1323                        lamport_timestamp,
1324                    },
1325                );
1326                self.text.lamport_clock.observe(lamport_timestamp);
1327                self.selections_update_count += 1;
1328            }
1329            Operation::UpdateCompletionTriggers {
1330                triggers,
1331                lamport_timestamp,
1332            } => {
1333                self.completion_triggers = triggers;
1334                self.text.lamport_clock.observe(lamport_timestamp);
1335            }
1336        }
1337    }
1338
1339    fn apply_diagnostic_update(
1340        &mut self,
1341        diagnostics: DiagnosticSet,
1342        lamport_timestamp: clock::Lamport,
1343        cx: &mut ModelContext<Self>,
1344    ) {
1345        if lamport_timestamp > self.diagnostics_timestamp {
1346            self.diagnostics = diagnostics;
1347            self.diagnostics_timestamp = lamport_timestamp;
1348            self.diagnostics_update_count += 1;
1349            self.text.lamport_clock.observe(lamport_timestamp);
1350            cx.notify();
1351            cx.emit(Event::DiagnosticsUpdated);
1352        }
1353    }
1354
1355    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1356        cx.emit(Event::Operation(operation));
1357    }
1358
1359    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1360        self.remote_selections.remove(&replica_id);
1361        cx.notify();
1362    }
1363
1364    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1365        let was_dirty = self.is_dirty();
1366        let old_version = self.version.clone();
1367
1368        if let Some((transaction_id, operation)) = self.text.undo() {
1369            self.send_operation(Operation::Buffer(operation), cx);
1370            self.did_edit(&old_version, was_dirty, cx);
1371            Some(transaction_id)
1372        } else {
1373            None
1374        }
1375    }
1376
1377    pub fn undo_to_transaction(
1378        &mut self,
1379        transaction_id: TransactionId,
1380        cx: &mut ModelContext<Self>,
1381    ) -> bool {
1382        let was_dirty = self.is_dirty();
1383        let old_version = self.version.clone();
1384
1385        let operations = self.text.undo_to_transaction(transaction_id);
1386        let undone = !operations.is_empty();
1387        for operation in operations {
1388            self.send_operation(Operation::Buffer(operation), cx);
1389        }
1390        if undone {
1391            self.did_edit(&old_version, was_dirty, cx)
1392        }
1393        undone
1394    }
1395
1396    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1397        let was_dirty = self.is_dirty();
1398        let old_version = self.version.clone();
1399
1400        if let Some((transaction_id, operation)) = self.text.redo() {
1401            self.send_operation(Operation::Buffer(operation), cx);
1402            self.did_edit(&old_version, was_dirty, cx);
1403            Some(transaction_id)
1404        } else {
1405            None
1406        }
1407    }
1408
1409    pub fn redo_to_transaction(
1410        &mut self,
1411        transaction_id: TransactionId,
1412        cx: &mut ModelContext<Self>,
1413    ) -> bool {
1414        let was_dirty = self.is_dirty();
1415        let old_version = self.version.clone();
1416
1417        let operations = self.text.redo_to_transaction(transaction_id);
1418        let redone = !operations.is_empty();
1419        for operation in operations {
1420            self.send_operation(Operation::Buffer(operation), cx);
1421        }
1422        if redone {
1423            self.did_edit(&old_version, was_dirty, cx)
1424        }
1425        redone
1426    }
1427
1428    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1429        self.completion_triggers = triggers.clone();
1430        let lamport_timestamp = self.text.lamport_clock.tick();
1431        self.send_operation(
1432            Operation::UpdateCompletionTriggers {
1433                triggers,
1434                lamport_timestamp,
1435            },
1436            cx,
1437        );
1438        cx.notify();
1439    }
1440
1441    pub fn completion_triggers(&self) -> &[String] {
1442        &self.completion_triggers
1443    }
1444}
1445
1446#[cfg(any(test, feature = "test-support"))]
1447impl Buffer {
1448    pub fn set_group_interval(&mut self, group_interval: Duration) {
1449        self.text.set_group_interval(group_interval);
1450    }
1451
1452    pub fn randomly_edit<T>(
1453        &mut self,
1454        rng: &mut T,
1455        old_range_count: usize,
1456        cx: &mut ModelContext<Self>,
1457    ) where
1458        T: rand::Rng,
1459    {
1460        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1461        let mut last_end = None;
1462        for _ in 0..old_range_count {
1463            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1464                break;
1465            }
1466
1467            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1468            let range = self.random_byte_range(new_start, rng);
1469            last_end = Some(range.end);
1470
1471            let new_text_len = rng.gen_range(0..10);
1472            let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1473                .take(new_text_len)
1474                .collect();
1475
1476            edits.push((range, new_text));
1477        }
1478        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1479        self.edit_batched(edits, cx);
1480    }
1481
1482    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1483        let was_dirty = self.is_dirty();
1484        let old_version = self.version.clone();
1485
1486        let ops = self.text.randomly_undo_redo(rng);
1487        if !ops.is_empty() {
1488            for op in ops {
1489                self.send_operation(Operation::Buffer(op), cx);
1490                self.did_edit(&old_version, was_dirty, cx);
1491            }
1492        }
1493    }
1494}
1495
1496impl Entity for Buffer {
1497    type Event = Event;
1498}
1499
1500impl Deref for Buffer {
1501    type Target = TextBuffer;
1502
1503    fn deref(&self) -> &Self::Target {
1504        &self.text
1505    }
1506}
1507
1508impl BufferSnapshot {
1509    fn suggest_autoindents<'a>(
1510        &'a self,
1511        row_range: Range<u32>,
1512    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1513        let mut query_cursor = QueryCursorHandle::new();
1514        if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
1515            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1516
1517            // Get the "indentation ranges" that intersect this row range.
1518            let indent_capture_ix = grammar.indents_query.capture_index_for_name("indent");
1519            let end_capture_ix = grammar.indents_query.capture_index_for_name("end");
1520            query_cursor.set_point_range(
1521                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1522                    ..Point::new(row_range.end, 0).to_ts_point(),
1523            );
1524            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1525            for mat in query_cursor.matches(
1526                &grammar.indents_query,
1527                tree.root_node(),
1528                TextProvider(self.as_rope()),
1529            ) {
1530                let mut node_kind = "";
1531                let mut start: Option<Point> = None;
1532                let mut end: Option<Point> = None;
1533                for capture in mat.captures {
1534                    if Some(capture.index) == indent_capture_ix {
1535                        node_kind = capture.node.kind();
1536                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1537                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1538                    } else if Some(capture.index) == end_capture_ix {
1539                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
1540                    }
1541                }
1542
1543                if let Some((start, end)) = start.zip(end) {
1544                    if start.row == end.row {
1545                        continue;
1546                    }
1547
1548                    let range = start..end;
1549                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1550                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1551                        Ok(ix) => {
1552                            let prev_range = &mut indentation_ranges[ix];
1553                            prev_range.0.end = prev_range.0.end.max(range.end);
1554                        }
1555                    }
1556                }
1557            }
1558
1559            let mut prev_row = prev_non_blank_row.unwrap_or(0);
1560            Some(row_range.map(move |row| {
1561                let row_start = Point::new(row, self.indent_column_for_line(row));
1562
1563                let mut indent_from_prev_row = false;
1564                let mut outdent_to_row = u32::MAX;
1565                for (range, _node_kind) in &indentation_ranges {
1566                    if range.start.row >= row {
1567                        break;
1568                    }
1569
1570                    if range.start.row == prev_row && range.end > row_start {
1571                        indent_from_prev_row = true;
1572                    }
1573                    if range.end.row >= prev_row && range.end <= row_start {
1574                        outdent_to_row = outdent_to_row.min(range.start.row);
1575                    }
1576                }
1577
1578                let suggestion = if outdent_to_row == prev_row {
1579                    IndentSuggestion {
1580                        basis_row: prev_row,
1581                        indent: false,
1582                    }
1583                } else if indent_from_prev_row {
1584                    IndentSuggestion {
1585                        basis_row: prev_row,
1586                        indent: true,
1587                    }
1588                } else if outdent_to_row < prev_row {
1589                    IndentSuggestion {
1590                        basis_row: outdent_to_row,
1591                        indent: false,
1592                    }
1593                } else {
1594                    IndentSuggestion {
1595                        basis_row: prev_row,
1596                        indent: false,
1597                    }
1598                };
1599
1600                prev_row = row;
1601                suggestion
1602            }))
1603        } else {
1604            None
1605        }
1606    }
1607
1608    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1609        while row > 0 {
1610            row -= 1;
1611            if !self.is_line_blank(row) {
1612                return Some(row);
1613            }
1614        }
1615        None
1616    }
1617
1618    pub fn chunks<'a, T: ToOffset>(
1619        &'a self,
1620        range: Range<T>,
1621        language_aware: bool,
1622    ) -> BufferChunks<'a> {
1623        let range = range.start.to_offset(self)..range.end.to_offset(self);
1624
1625        let mut tree = None;
1626        let mut diagnostic_endpoints = Vec::new();
1627        if language_aware {
1628            tree = self.tree.as_ref();
1629            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1630                diagnostic_endpoints.push(DiagnosticEndpoint {
1631                    offset: entry.range.start,
1632                    is_start: true,
1633                    severity: entry.diagnostic.severity,
1634                    is_unnecessary: entry.diagnostic.is_unnecessary,
1635                });
1636                diagnostic_endpoints.push(DiagnosticEndpoint {
1637                    offset: entry.range.end,
1638                    is_start: false,
1639                    severity: entry.diagnostic.severity,
1640                    is_unnecessary: entry.diagnostic.is_unnecessary,
1641                });
1642            }
1643            diagnostic_endpoints
1644                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1645        }
1646
1647        BufferChunks::new(
1648            self.text.as_rope(),
1649            range,
1650            tree,
1651            self.grammar(),
1652            diagnostic_endpoints,
1653        )
1654    }
1655
1656    pub fn language(&self) -> Option<&Arc<Language>> {
1657        self.language.as_ref()
1658    }
1659
1660    fn grammar(&self) -> Option<&Arc<Grammar>> {
1661        self.language
1662            .as_ref()
1663            .and_then(|language| language.grammar.as_ref())
1664    }
1665
1666    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1667        let tree = self.tree.as_ref()?;
1668        let range = range.start.to_offset(self)..range.end.to_offset(self);
1669        let mut cursor = tree.root_node().walk();
1670
1671        // Descend to the first leaf that touches the start of the range,
1672        // and if the range is non-empty, extends beyond the start.
1673        while cursor.goto_first_child_for_byte(range.start).is_some() {
1674            if !range.is_empty() && cursor.node().end_byte() == range.start {
1675                cursor.goto_next_sibling();
1676            }
1677        }
1678
1679        // Ascend to the smallest ancestor that strictly contains the range.
1680        loop {
1681            let node_range = cursor.node().byte_range();
1682            if node_range.start <= range.start
1683                && node_range.end >= range.end
1684                && node_range.len() > range.len()
1685            {
1686                break;
1687            }
1688            if !cursor.goto_parent() {
1689                break;
1690            }
1691        }
1692
1693        let left_node = cursor.node();
1694
1695        // For an empty range, try to find another node immediately to the right of the range.
1696        if left_node.end_byte() == range.start {
1697            let mut right_node = None;
1698            while !cursor.goto_next_sibling() {
1699                if !cursor.goto_parent() {
1700                    break;
1701                }
1702            }
1703
1704            while cursor.node().start_byte() == range.start {
1705                right_node = Some(cursor.node());
1706                if !cursor.goto_first_child() {
1707                    break;
1708                }
1709            }
1710
1711            // If there is a candidate node on both sides of the (empty) range, then
1712            // decide between the two by favoring a named node over an anonymous token.
1713            // If both nodes are the same in that regard, favor the right one.
1714            if let Some(right_node) = right_node {
1715                if right_node.is_named() || !left_node.is_named() {
1716                    return Some(right_node.byte_range());
1717                }
1718            }
1719        }
1720
1721        Some(left_node.byte_range())
1722    }
1723
1724    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1725        self.outline_items_containing(0..self.len(), theme)
1726            .map(Outline::new)
1727    }
1728
1729    pub fn symbols_containing<T: ToOffset>(
1730        &self,
1731        position: T,
1732        theme: Option<&SyntaxTheme>,
1733    ) -> Option<Vec<OutlineItem<Anchor>>> {
1734        let position = position.to_offset(&self);
1735        let mut items =
1736            self.outline_items_containing(position.saturating_sub(1)..position + 1, theme)?;
1737        let mut prev_depth = None;
1738        items.retain(|item| {
1739            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
1740            prev_depth = Some(item.depth);
1741            result
1742        });
1743        Some(items)
1744    }
1745
1746    fn outline_items_containing(
1747        &self,
1748        range: Range<usize>,
1749        theme: Option<&SyntaxTheme>,
1750    ) -> Option<Vec<OutlineItem<Anchor>>> {
1751        let tree = self.tree.as_ref()?;
1752        let grammar = self
1753            .language
1754            .as_ref()
1755            .and_then(|language| language.grammar.as_ref())?;
1756
1757        let mut cursor = QueryCursorHandle::new();
1758        cursor.set_byte_range(range.clone());
1759        let matches = cursor.matches(
1760            &grammar.outline_query,
1761            tree.root_node(),
1762            TextProvider(self.as_rope()),
1763        );
1764
1765        let mut chunks = self.chunks(0..self.len(), true);
1766
1767        let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
1768        let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
1769        let context_capture_ix = grammar
1770            .outline_query
1771            .capture_index_for_name("context")
1772            .unwrap_or(u32::MAX);
1773
1774        let mut stack = Vec::<Range<usize>>::new();
1775        let items = matches
1776            .filter_map(|mat| {
1777                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1778                let item_range = item_node.start_byte()..item_node.end_byte();
1779                if item_range.end < range.start || item_range.start > range.end {
1780                    return None;
1781                }
1782                let mut text = String::new();
1783                let mut name_ranges = Vec::new();
1784                let mut highlight_ranges = Vec::new();
1785
1786                for capture in mat.captures {
1787                    let node_is_name;
1788                    if capture.index == name_capture_ix {
1789                        node_is_name = true;
1790                    } else if capture.index == context_capture_ix {
1791                        node_is_name = false;
1792                    } else {
1793                        continue;
1794                    }
1795
1796                    let range = capture.node.start_byte()..capture.node.end_byte();
1797                    if !text.is_empty() {
1798                        text.push(' ');
1799                    }
1800                    if node_is_name {
1801                        let mut start = text.len();
1802                        let end = start + range.len();
1803
1804                        // When multiple names are captured, then the matcheable text
1805                        // includes the whitespace in between the names.
1806                        if !name_ranges.is_empty() {
1807                            start -= 1;
1808                        }
1809
1810                        name_ranges.push(start..end);
1811                    }
1812
1813                    let mut offset = range.start;
1814                    chunks.seek(offset);
1815                    while let Some(mut chunk) = chunks.next() {
1816                        if chunk.text.len() > range.end - offset {
1817                            chunk.text = &chunk.text[0..(range.end - offset)];
1818                            offset = range.end;
1819                        } else {
1820                            offset += chunk.text.len();
1821                        }
1822                        let style = chunk
1823                            .syntax_highlight_id
1824                            .zip(theme)
1825                            .and_then(|(highlight, theme)| highlight.style(theme));
1826                        if let Some(style) = style {
1827                            let start = text.len();
1828                            let end = start + chunk.text.len();
1829                            highlight_ranges.push((start..end, style));
1830                        }
1831                        text.push_str(chunk.text);
1832                        if offset >= range.end {
1833                            break;
1834                        }
1835                    }
1836                }
1837
1838                while stack.last().map_or(false, |prev_range| {
1839                    !prev_range.contains(&item_range.start) || !prev_range.contains(&item_range.end)
1840                }) {
1841                    stack.pop();
1842                }
1843                stack.push(item_range.clone());
1844
1845                Some(OutlineItem {
1846                    depth: stack.len() - 1,
1847                    range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
1848                    text,
1849                    highlight_ranges,
1850                    name_ranges,
1851                })
1852            })
1853            .collect::<Vec<_>>();
1854        Some(items)
1855    }
1856
1857    pub fn enclosing_bracket_ranges<T: ToOffset>(
1858        &self,
1859        range: Range<T>,
1860    ) -> Option<(Range<usize>, Range<usize>)> {
1861        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1862        let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
1863        let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
1864
1865        // Find bracket pairs that *inclusively* contain the given range.
1866        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1867        let mut cursor = QueryCursorHandle::new();
1868        let matches = cursor.set_byte_range(range).matches(
1869            &grammar.brackets_query,
1870            tree.root_node(),
1871            TextProvider(self.as_rope()),
1872        );
1873
1874        // Get the ranges of the innermost pair of brackets.
1875        matches
1876            .filter_map(|mat| {
1877                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1878                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1879                Some((open.byte_range(), close.byte_range()))
1880            })
1881            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1882    }
1883
1884    pub fn remote_selections_in_range<'a>(
1885        &'a self,
1886        range: Range<Anchor>,
1887    ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
1888    {
1889        self.remote_selections
1890            .iter()
1891            .filter(|(replica_id, set)| {
1892                **replica_id != self.text.replica_id() && !set.selections.is_empty()
1893            })
1894            .map(move |(replica_id, set)| {
1895                let start_ix = match set.selections.binary_search_by(|probe| {
1896                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
1897                }) {
1898                    Ok(ix) | Err(ix) => ix,
1899                };
1900                let end_ix = match set.selections.binary_search_by(|probe| {
1901                    probe.start.cmp(&range.end, self).then(Ordering::Less)
1902                }) {
1903                    Ok(ix) | Err(ix) => ix,
1904                };
1905
1906                (*replica_id, set.selections[start_ix..end_ix].iter())
1907            })
1908    }
1909
1910    pub fn diagnostics_in_range<'a, T, O>(
1911        &'a self,
1912        search_range: Range<T>,
1913        reversed: bool,
1914    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1915    where
1916        T: 'a + Clone + ToOffset,
1917        O: 'a + FromAnchor,
1918    {
1919        self.diagnostics
1920            .range(search_range.clone(), self, true, reversed)
1921    }
1922
1923    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1924        let mut groups = Vec::new();
1925        self.diagnostics.groups(&mut groups, self);
1926        groups
1927    }
1928
1929    pub fn diagnostic_group<'a, O>(
1930        &'a self,
1931        group_id: usize,
1932    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1933    where
1934        O: 'a + FromAnchor,
1935    {
1936        self.diagnostics.group(group_id, self)
1937    }
1938
1939    pub fn diagnostics_update_count(&self) -> usize {
1940        self.diagnostics_update_count
1941    }
1942
1943    pub fn parse_count(&self) -> usize {
1944        self.parse_count
1945    }
1946
1947    pub fn selections_update_count(&self) -> usize {
1948        self.selections_update_count
1949    }
1950
1951    pub fn path(&self) -> Option<&Arc<Path>> {
1952        self.path.as_ref()
1953    }
1954
1955    pub fn file_update_count(&self) -> usize {
1956        self.file_update_count
1957    }
1958}
1959
1960impl Clone for BufferSnapshot {
1961    fn clone(&self) -> Self {
1962        Self {
1963            text: self.text.clone(),
1964            tree: self.tree.clone(),
1965            path: self.path.clone(),
1966            remote_selections: self.remote_selections.clone(),
1967            diagnostics: self.diagnostics.clone(),
1968            selections_update_count: self.selections_update_count,
1969            diagnostics_update_count: self.diagnostics_update_count,
1970            file_update_count: self.file_update_count,
1971            language: self.language.clone(),
1972            parse_count: self.parse_count,
1973        }
1974    }
1975}
1976
1977impl Deref for BufferSnapshot {
1978    type Target = text::BufferSnapshot;
1979
1980    fn deref(&self) -> &Self::Target {
1981        &self.text
1982    }
1983}
1984
1985impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1986    type I = ByteChunks<'a>;
1987
1988    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1989        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1990    }
1991}
1992
1993pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
1994
1995impl<'a> Iterator for ByteChunks<'a> {
1996    type Item = &'a [u8];
1997
1998    fn next(&mut self) -> Option<Self::Item> {
1999        self.0.next().map(str::as_bytes)
2000    }
2001}
2002
2003unsafe impl<'a> Send for BufferChunks<'a> {}
2004
2005impl<'a> BufferChunks<'a> {
2006    pub(crate) fn new(
2007        text: &'a Rope,
2008        range: Range<usize>,
2009        tree: Option<&'a Tree>,
2010        grammar: Option<&'a Arc<Grammar>>,
2011        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2012    ) -> Self {
2013        let mut highlights = None;
2014        if let Some((grammar, tree)) = grammar.zip(tree) {
2015            let mut query_cursor = QueryCursorHandle::new();
2016
2017            // TODO - add a Tree-sitter API to remove the need for this.
2018            let cursor = unsafe {
2019                std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
2020            };
2021            let captures = cursor.set_byte_range(range.clone()).captures(
2022                &grammar.highlights_query,
2023                tree.root_node(),
2024                TextProvider(text),
2025            );
2026            highlights = Some(BufferChunkHighlights {
2027                captures,
2028                next_capture: None,
2029                stack: Default::default(),
2030                highlight_map: grammar.highlight_map(),
2031                _query_cursor: query_cursor,
2032            })
2033        }
2034
2035        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2036        let chunks = text.chunks_in_range(range.clone());
2037
2038        BufferChunks {
2039            range,
2040            chunks,
2041            diagnostic_endpoints,
2042            error_depth: 0,
2043            warning_depth: 0,
2044            information_depth: 0,
2045            hint_depth: 0,
2046            unnecessary_depth: 0,
2047            highlights,
2048        }
2049    }
2050
2051    pub fn seek(&mut self, offset: usize) {
2052        self.range.start = offset;
2053        self.chunks.seek(self.range.start);
2054        if let Some(highlights) = self.highlights.as_mut() {
2055            highlights
2056                .stack
2057                .retain(|(end_offset, _)| *end_offset > offset);
2058            if let Some((mat, capture_ix)) = &highlights.next_capture {
2059                let capture = mat.captures[*capture_ix as usize];
2060                if offset >= capture.node.start_byte() {
2061                    let next_capture_end = capture.node.end_byte();
2062                    if offset < next_capture_end {
2063                        highlights.stack.push((
2064                            next_capture_end,
2065                            highlights.highlight_map.get(capture.index),
2066                        ));
2067                    }
2068                    highlights.next_capture.take();
2069                }
2070            }
2071            highlights.captures.set_byte_range(self.range.clone());
2072        }
2073    }
2074
2075    pub fn offset(&self) -> usize {
2076        self.range.start
2077    }
2078
2079    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2080        let depth = match endpoint.severity {
2081            DiagnosticSeverity::ERROR => &mut self.error_depth,
2082            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2083            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2084            DiagnosticSeverity::HINT => &mut self.hint_depth,
2085            _ => return,
2086        };
2087        if endpoint.is_start {
2088            *depth += 1;
2089        } else {
2090            *depth -= 1;
2091        }
2092
2093        if endpoint.is_unnecessary {
2094            if endpoint.is_start {
2095                self.unnecessary_depth += 1;
2096            } else {
2097                self.unnecessary_depth -= 1;
2098            }
2099        }
2100    }
2101
2102    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2103        if self.error_depth > 0 {
2104            Some(DiagnosticSeverity::ERROR)
2105        } else if self.warning_depth > 0 {
2106            Some(DiagnosticSeverity::WARNING)
2107        } else if self.information_depth > 0 {
2108            Some(DiagnosticSeverity::INFORMATION)
2109        } else if self.hint_depth > 0 {
2110            Some(DiagnosticSeverity::HINT)
2111        } else {
2112            None
2113        }
2114    }
2115
2116    fn current_code_is_unnecessary(&self) -> bool {
2117        self.unnecessary_depth > 0
2118    }
2119}
2120
2121impl<'a> Iterator for BufferChunks<'a> {
2122    type Item = Chunk<'a>;
2123
2124    fn next(&mut self) -> Option<Self::Item> {
2125        let mut next_capture_start = usize::MAX;
2126        let mut next_diagnostic_endpoint = usize::MAX;
2127
2128        if let Some(highlights) = self.highlights.as_mut() {
2129            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2130                if *parent_capture_end <= self.range.start {
2131                    highlights.stack.pop();
2132                } else {
2133                    break;
2134                }
2135            }
2136
2137            if highlights.next_capture.is_none() {
2138                highlights.next_capture = highlights.captures.next();
2139            }
2140
2141            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2142                let capture = mat.captures[*capture_ix as usize];
2143                if self.range.start < capture.node.start_byte() {
2144                    next_capture_start = capture.node.start_byte();
2145                    break;
2146                } else {
2147                    let highlight_id = highlights.highlight_map.get(capture.index);
2148                    highlights
2149                        .stack
2150                        .push((capture.node.end_byte(), highlight_id));
2151                    highlights.next_capture = highlights.captures.next();
2152                }
2153            }
2154        }
2155
2156        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2157            if endpoint.offset <= self.range.start {
2158                self.update_diagnostic_depths(endpoint);
2159                self.diagnostic_endpoints.next();
2160            } else {
2161                next_diagnostic_endpoint = endpoint.offset;
2162                break;
2163            }
2164        }
2165
2166        if let Some(chunk) = self.chunks.peek() {
2167            let chunk_start = self.range.start;
2168            let mut chunk_end = (self.chunks.offset() + chunk.len())
2169                .min(next_capture_start)
2170                .min(next_diagnostic_endpoint);
2171            let mut highlight_id = None;
2172            if let Some(highlights) = self.highlights.as_ref() {
2173                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2174                    chunk_end = chunk_end.min(*parent_capture_end);
2175                    highlight_id = Some(*parent_highlight_id);
2176                }
2177            }
2178
2179            let slice =
2180                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2181            self.range.start = chunk_end;
2182            if self.range.start == self.chunks.offset() + chunk.len() {
2183                self.chunks.next().unwrap();
2184            }
2185
2186            Some(Chunk {
2187                text: slice,
2188                syntax_highlight_id: highlight_id,
2189                highlight_style: None,
2190                diagnostic_severity: self.current_diagnostic_severity(),
2191                is_unnecessary: self.current_code_is_unnecessary(),
2192            })
2193        } else {
2194            None
2195        }
2196    }
2197}
2198
2199impl QueryCursorHandle {
2200    pub(crate) fn new() -> Self {
2201        QueryCursorHandle(Some(
2202            QUERY_CURSORS
2203                .lock()
2204                .pop()
2205                .unwrap_or_else(|| QueryCursor::new()),
2206        ))
2207    }
2208}
2209
2210impl Deref for QueryCursorHandle {
2211    type Target = QueryCursor;
2212
2213    fn deref(&self) -> &Self::Target {
2214        self.0.as_ref().unwrap()
2215    }
2216}
2217
2218impl DerefMut for QueryCursorHandle {
2219    fn deref_mut(&mut self) -> &mut Self::Target {
2220        self.0.as_mut().unwrap()
2221    }
2222}
2223
2224impl Drop for QueryCursorHandle {
2225    fn drop(&mut self) {
2226        let mut cursor = self.0.take().unwrap();
2227        cursor.set_byte_range(0..usize::MAX);
2228        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2229        QUERY_CURSORS.lock().push(cursor)
2230    }
2231}
2232
2233trait ToTreeSitterPoint {
2234    fn to_ts_point(self) -> tree_sitter::Point;
2235    fn from_ts_point(point: tree_sitter::Point) -> Self;
2236}
2237
2238impl ToTreeSitterPoint for Point {
2239    fn to_ts_point(self) -> tree_sitter::Point {
2240        tree_sitter::Point::new(self.row as usize, self.column as usize)
2241    }
2242
2243    fn from_ts_point(point: tree_sitter::Point) -> Self {
2244        Point::new(point.row as u32, point.column as u32)
2245    }
2246}
2247
2248impl operation_queue::Operation for Operation {
2249    fn lamport_timestamp(&self) -> clock::Lamport {
2250        match self {
2251            Operation::Buffer(_) => {
2252                unreachable!("buffer operations should never be deferred at this layer")
2253            }
2254            Operation::UpdateDiagnostics {
2255                lamport_timestamp, ..
2256            }
2257            | Operation::UpdateSelections {
2258                lamport_timestamp, ..
2259            }
2260            | Operation::UpdateCompletionTriggers {
2261                lamport_timestamp, ..
2262            } => *lamport_timestamp,
2263        }
2264    }
2265}
2266
2267impl Default for Diagnostic {
2268    fn default() -> Self {
2269        Self {
2270            code: Default::default(),
2271            severity: DiagnosticSeverity::ERROR,
2272            message: Default::default(),
2273            group_id: Default::default(),
2274            is_primary: Default::default(),
2275            is_valid: true,
2276            is_disk_based: false,
2277            is_unnecessary: false,
2278        }
2279    }
2280}
2281
2282impl Completion {
2283    pub fn sort_key(&self) -> (usize, &str) {
2284        let kind_key = match self.lsp_completion.kind {
2285            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2286            _ => 1,
2287        };
2288        (kind_key, &self.label.text[self.label.filter_range.clone()])
2289    }
2290
2291    pub fn is_snippet(&self) -> bool {
2292        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2293    }
2294}
2295
2296pub fn contiguous_ranges(
2297    values: impl Iterator<Item = u32>,
2298    max_len: usize,
2299) -> impl Iterator<Item = Range<u32>> {
2300    let mut values = values.into_iter();
2301    let mut current_range: Option<Range<u32>> = None;
2302    std::iter::from_fn(move || loop {
2303        if let Some(value) = values.next() {
2304            if let Some(range) = &mut current_range {
2305                if value == range.end && range.len() < max_len {
2306                    range.end += 1;
2307                    continue;
2308                }
2309            }
2310
2311            let prev_range = current_range.clone();
2312            current_range = Some(value..(value + 1));
2313            if prev_range.is_some() {
2314                return prev_range;
2315            }
2316        } else {
2317            return current_range.take();
2318        }
2319    })
2320}
2321
2322pub fn char_kind(c: char) -> CharKind {
2323    if c.is_whitespace() {
2324        CharKind::Whitespace
2325    } else if c.is_alphanumeric() || c == '_' {
2326        CharKind::Word
2327    } else {
2328        CharKind::Punctuation
2329    }
2330}