buffer.rs

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