buffer.rs

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