buffer.rs

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