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