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