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