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