buffer.rs

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