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.snapshot_for_version(version)?
1029            } else {
1030                self.deref()
1031            };
1032
1033        diagnostics.sort_unstable_by(|a, b| {
1034            Ordering::Equal
1035                .then_with(|| a.range.start.cmp(&b.range.start))
1036                .then_with(|| b.range.end.cmp(&a.range.end))
1037                .then_with(|| compare_diagnostics(&a.diagnostic, &b.diagnostic))
1038        });
1039
1040        let mut sanitized_diagnostics = Vec::new();
1041        let mut edits_since_save = content.edits_since::<T>(&self.saved_version).peekable();
1042        let mut last_edit_old_end = T::default();
1043        let mut last_edit_new_end = T::default();
1044        'outer: for entry in diagnostics {
1045            let mut start = entry.range.start;
1046            let mut end = entry.range.end;
1047
1048            // Some diagnostics are based on files on disk instead of buffers'
1049            // current contents. Adjust these diagnostics' ranges to reflect
1050            // any unsaved edits.
1051            if entry.diagnostic.is_disk_based {
1052                while let Some(edit) = edits_since_save.peek() {
1053                    if edit.old.end <= start {
1054                        last_edit_old_end = edit.old.end;
1055                        last_edit_new_end = edit.new.end;
1056                        edits_since_save.next();
1057                    } else if edit.old.start <= end && edit.old.end >= start {
1058                        continue 'outer;
1059                    } else {
1060                        break;
1061                    }
1062                }
1063
1064                let start_overshoot = start - last_edit_old_end;
1065                start = last_edit_new_end;
1066                start.add_assign(&start_overshoot);
1067
1068                let end_overshoot = end - last_edit_old_end;
1069                end = last_edit_new_end;
1070                end.add_assign(&end_overshoot);
1071            }
1072
1073            let range = start.clip(Bias::Left, content)..end.clip(Bias::Right, content);
1074            let mut range = range.start.to_point(content)..range.end.to_point(content);
1075            // Expand empty ranges by one character
1076            if range.start == range.end {
1077                range.end.column += 1;
1078                range.end = content.clip_point(range.end, Bias::Right);
1079                if range.start == range.end && range.end.column > 0 {
1080                    range.start.column -= 1;
1081                    range.start = content.clip_point(range.start, Bias::Left);
1082                }
1083            }
1084
1085            sanitized_diagnostics.push(DiagnosticEntry {
1086                range,
1087                diagnostic: entry.diagnostic,
1088            });
1089        }
1090        drop(edits_since_save);
1091
1092        let set = DiagnosticSet::new(sanitized_diagnostics, content);
1093        self.apply_diagnostic_update(set.clone(), cx);
1094
1095        let op = Operation::UpdateDiagnostics {
1096            diagnostics: set.iter().cloned().collect(),
1097            lamport_timestamp: self.text.lamport_clock.tick(),
1098        };
1099        self.send_operation(op, cx);
1100        Ok(())
1101    }
1102
1103    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
1104        if let Some(indent_columns) = self.compute_autoindents() {
1105            let indent_columns = cx.background().spawn(indent_columns);
1106            match cx
1107                .background()
1108                .block_with_timeout(Duration::from_micros(500), indent_columns)
1109            {
1110                Ok(indent_columns) => self.apply_autoindents(indent_columns, cx),
1111                Err(indent_columns) => {
1112                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
1113                        let indent_columns = indent_columns.await;
1114                        this.update(&mut cx, |this, cx| {
1115                            this.apply_autoindents(indent_columns, cx);
1116                        });
1117                    }));
1118                }
1119            }
1120        }
1121    }
1122
1123    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, u32>>> {
1124        let max_rows_between_yields = 100;
1125        let snapshot = self.snapshot();
1126        if snapshot.language.is_none()
1127            || snapshot.tree.is_none()
1128            || self.autoindent_requests.is_empty()
1129        {
1130            return None;
1131        }
1132
1133        let autoindent_requests = self.autoindent_requests.clone();
1134        Some(async move {
1135            let mut indent_columns = BTreeMap::new();
1136            for request in autoindent_requests {
1137                let old_to_new_rows = request
1138                    .edited
1139                    .iter()
1140                    .map(|anchor| anchor.summary::<Point>(&request.before_edit).row)
1141                    .zip(
1142                        request
1143                            .edited
1144                            .iter()
1145                            .map(|anchor| anchor.summary::<Point>(&snapshot).row),
1146                    )
1147                    .collect::<BTreeMap<u32, u32>>();
1148
1149                let mut old_suggestions = HashMap::<u32, u32>::default();
1150                let old_edited_ranges =
1151                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
1152                for old_edited_range in old_edited_ranges {
1153                    let suggestions = request
1154                        .before_edit
1155                        .suggest_autoindents(old_edited_range.clone())
1156                        .into_iter()
1157                        .flatten();
1158                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
1159                        let indentation_basis = old_to_new_rows
1160                            .get(&suggestion.basis_row)
1161                            .and_then(|from_row| old_suggestions.get(from_row).copied())
1162                            .unwrap_or_else(|| {
1163                                request
1164                                    .before_edit
1165                                    .indent_column_for_line(suggestion.basis_row)
1166                            });
1167                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1168                        old_suggestions.insert(
1169                            *old_to_new_rows.get(&old_row).unwrap(),
1170                            indentation_basis + delta,
1171                        );
1172                    }
1173                    yield_now().await;
1174                }
1175
1176                // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
1177                // buffer before the edit, but keyed by the row for these lines after the edits were applied.
1178                let new_edited_row_ranges =
1179                    contiguous_ranges(old_to_new_rows.values().copied(), max_rows_between_yields);
1180                for new_edited_row_range in new_edited_row_ranges {
1181                    let suggestions = snapshot
1182                        .suggest_autoindents(new_edited_row_range.clone())
1183                        .into_iter()
1184                        .flatten();
1185                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1186                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1187                        let new_indentation = indent_columns
1188                            .get(&suggestion.basis_row)
1189                            .copied()
1190                            .unwrap_or_else(|| {
1191                                snapshot.indent_column_for_line(suggestion.basis_row)
1192                            })
1193                            + delta;
1194                        if old_suggestions
1195                            .get(&new_row)
1196                            .map_or(true, |old_indentation| new_indentation != *old_indentation)
1197                        {
1198                            indent_columns.insert(new_row, new_indentation);
1199                        }
1200                    }
1201                    yield_now().await;
1202                }
1203
1204                if let Some(inserted) = request.inserted.as_ref() {
1205                    let inserted_row_ranges = contiguous_ranges(
1206                        inserted
1207                            .iter()
1208                            .map(|range| range.to_point(&snapshot))
1209                            .flat_map(|range| range.start.row..range.end.row + 1),
1210                        max_rows_between_yields,
1211                    );
1212                    for inserted_row_range in inserted_row_ranges {
1213                        let suggestions = snapshot
1214                            .suggest_autoindents(inserted_row_range.clone())
1215                            .into_iter()
1216                            .flatten();
1217                        for (row, suggestion) in inserted_row_range.zip(suggestions) {
1218                            let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
1219                            let new_indentation = indent_columns
1220                                .get(&suggestion.basis_row)
1221                                .copied()
1222                                .unwrap_or_else(|| {
1223                                    snapshot.indent_column_for_line(suggestion.basis_row)
1224                                })
1225                                + delta;
1226                            indent_columns.insert(row, new_indentation);
1227                        }
1228                        yield_now().await;
1229                    }
1230                }
1231            }
1232            indent_columns
1233        })
1234    }
1235
1236    fn apply_autoindents(
1237        &mut self,
1238        indent_columns: BTreeMap<u32, u32>,
1239        cx: &mut ModelContext<Self>,
1240    ) {
1241        self.autoindent_requests.clear();
1242        self.start_transaction();
1243        for (row, indent_column) in &indent_columns {
1244            self.set_indent_column_for_line(*row, *indent_column, cx);
1245        }
1246        self.end_transaction(cx);
1247    }
1248
1249    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
1250        let current_column = self.indent_column_for_line(row);
1251        if column > current_column {
1252            let offset = Point::new(row, 0).to_offset(&*self);
1253            self.edit(
1254                [offset..offset],
1255                " ".repeat((column - current_column) as usize),
1256                cx,
1257            );
1258        } else if column < current_column {
1259            self.edit(
1260                [Point::new(row, 0)..Point::new(row, current_column - column)],
1261                "",
1262                cx,
1263            );
1264        }
1265    }
1266
1267    pub(crate) fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
1268        // TODO: it would be nice to not allocate here.
1269        let old_text = self.text();
1270        let base_version = self.version();
1271        cx.background().spawn(async move {
1272            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
1273                .iter_all_changes()
1274                .map(|c| (c.tag(), c.value().len()))
1275                .collect::<Vec<_>>();
1276            Diff {
1277                base_version,
1278                new_text,
1279                changes,
1280            }
1281        })
1282    }
1283
1284    pub(crate) fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
1285        if self.version == diff.base_version {
1286            self.start_transaction();
1287            let mut offset = 0;
1288            for (tag, len) in diff.changes {
1289                let range = offset..(offset + len);
1290                match tag {
1291                    ChangeTag::Equal => offset += len,
1292                    ChangeTag::Delete => {
1293                        self.edit(Some(range), "", cx);
1294                    }
1295                    ChangeTag::Insert => {
1296                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
1297                        offset += len;
1298                    }
1299                }
1300            }
1301            self.end_transaction(cx);
1302            true
1303        } else {
1304            false
1305        }
1306    }
1307
1308    pub fn is_dirty(&self) -> bool {
1309        !self.saved_version.observed_all(&self.version)
1310            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1311    }
1312
1313    pub fn has_conflict(&self) -> bool {
1314        !self.saved_version.observed_all(&self.version)
1315            && self
1316                .file
1317                .as_ref()
1318                .map_or(false, |file| file.mtime() > self.saved_mtime)
1319    }
1320
1321    pub fn subscribe(&mut self) -> Subscription {
1322        self.text.subscribe()
1323    }
1324
1325    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1326        self.start_transaction_at(Instant::now())
1327    }
1328
1329    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1330        self.text.start_transaction_at(now)
1331    }
1332
1333    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1334        self.end_transaction_at(Instant::now(), cx)
1335    }
1336
1337    pub fn end_transaction_at(
1338        &mut self,
1339        now: Instant,
1340        cx: &mut ModelContext<Self>,
1341    ) -> Option<TransactionId> {
1342        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1343            let was_dirty = start_version != self.saved_version;
1344            self.did_edit(&start_version, was_dirty, cx);
1345            Some(transaction_id)
1346        } else {
1347            None
1348        }
1349    }
1350
1351    pub fn avoid_grouping_next_transaction(&mut self) {
1352        self.text.avoid_grouping_next_transaction();
1353    }
1354
1355    pub fn set_active_selections(
1356        &mut self,
1357        selections: Arc<[Selection<Anchor>]>,
1358        cx: &mut ModelContext<Self>,
1359    ) {
1360        let lamport_timestamp = self.text.lamport_clock.tick();
1361        self.remote_selections.insert(
1362            self.text.replica_id(),
1363            SelectionSet {
1364                selections: selections.clone(),
1365                lamport_timestamp,
1366            },
1367        );
1368        self.send_operation(
1369            Operation::UpdateSelections {
1370                replica_id: self.text.replica_id(),
1371                selections,
1372                lamport_timestamp,
1373            },
1374            cx,
1375        );
1376    }
1377
1378    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1379        self.set_active_selections(Arc::from([]), cx);
1380    }
1381
1382    fn update_language_server(&mut self, cx: &AppContext) {
1383        let language_server = if let Some(language_server) = self.language_server.as_mut() {
1384            language_server
1385        } else {
1386            return;
1387        };
1388        let abs_path = self
1389            .file
1390            .as_ref()
1391            .and_then(|f| f.as_local())
1392            .map_or(Path::new("/").to_path_buf(), |file| file.abs_path(cx));
1393
1394        let version = post_inc(&mut language_server.next_version);
1395        let snapshot = LanguageServerSnapshot {
1396            buffer_snapshot: self.text.snapshot(),
1397            version,
1398            path: Arc::from(abs_path),
1399        };
1400        language_server
1401            .pending_snapshots
1402            .insert(version, snapshot.clone());
1403        let _ = language_server
1404            .latest_snapshot
1405            .blocking_send(Some(snapshot));
1406    }
1407
1408    pub fn edit<I, S, T>(
1409        &mut self,
1410        ranges_iter: I,
1411        new_text: T,
1412        cx: &mut ModelContext<Self>,
1413    ) -> Option<clock::Local>
1414    where
1415        I: IntoIterator<Item = Range<S>>,
1416        S: ToOffset,
1417        T: Into<String>,
1418    {
1419        self.edit_internal(ranges_iter, new_text, false, cx)
1420    }
1421
1422    pub fn edit_with_autoindent<I, S, T>(
1423        &mut self,
1424        ranges_iter: I,
1425        new_text: T,
1426        cx: &mut ModelContext<Self>,
1427    ) -> Option<clock::Local>
1428    where
1429        I: IntoIterator<Item = Range<S>>,
1430        S: ToOffset,
1431        T: Into<String>,
1432    {
1433        self.edit_internal(ranges_iter, new_text, true, cx)
1434    }
1435
1436    pub fn edit_internal<I, S, T>(
1437        &mut self,
1438        ranges_iter: I,
1439        new_text: T,
1440        autoindent: bool,
1441        cx: &mut ModelContext<Self>,
1442    ) -> Option<clock::Local>
1443    where
1444        I: IntoIterator<Item = Range<S>>,
1445        S: ToOffset,
1446        T: Into<String>,
1447    {
1448        let new_text = new_text.into();
1449
1450        // Skip invalid ranges and coalesce contiguous ones.
1451        let mut ranges: Vec<Range<usize>> = Vec::new();
1452        for range in ranges_iter {
1453            let range = range.start.to_offset(self)..range.end.to_offset(self);
1454            if !new_text.is_empty() || !range.is_empty() {
1455                if let Some(prev_range) = ranges.last_mut() {
1456                    if prev_range.end >= range.start {
1457                        prev_range.end = cmp::max(prev_range.end, range.end);
1458                    } else {
1459                        ranges.push(range);
1460                    }
1461                } else {
1462                    ranges.push(range);
1463                }
1464            }
1465        }
1466        if ranges.is_empty() {
1467            return None;
1468        }
1469
1470        self.start_transaction();
1471        self.pending_autoindent.take();
1472        let autoindent_request = if autoindent && self.language.is_some() {
1473            let before_edit = self.snapshot();
1474            let edited = ranges
1475                .iter()
1476                .filter_map(|range| {
1477                    let start = range.start.to_point(self);
1478                    if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
1479                        None
1480                    } else {
1481                        Some(self.anchor_before(range.start))
1482                    }
1483                })
1484                .collect();
1485            Some((before_edit, edited))
1486        } else {
1487            None
1488        };
1489
1490        let first_newline_ix = new_text.find('\n');
1491        let new_text_len = new_text.len();
1492
1493        let edit = self.text.edit(ranges.iter().cloned(), new_text);
1494        let edit_id = edit.timestamp.local();
1495
1496        if let Some((before_edit, edited)) = autoindent_request {
1497            let mut inserted = None;
1498            if let Some(first_newline_ix) = first_newline_ix {
1499                let mut delta = 0isize;
1500                inserted = Some(
1501                    ranges
1502                        .iter()
1503                        .map(|range| {
1504                            let start =
1505                                (delta + range.start as isize) as usize + first_newline_ix + 1;
1506                            let end = (delta + range.start as isize) as usize + new_text_len;
1507                            delta +=
1508                                (range.end as isize - range.start as isize) + new_text_len as isize;
1509                            self.anchor_before(start)..self.anchor_after(end)
1510                        })
1511                        .collect(),
1512                );
1513            }
1514
1515            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1516                before_edit,
1517                edited,
1518                inserted,
1519            }));
1520        }
1521
1522        self.end_transaction(cx);
1523        self.send_operation(Operation::Buffer(text::Operation::Edit(edit)), cx);
1524        Some(edit_id)
1525    }
1526
1527    fn apply_lsp_edits(
1528        &mut self,
1529        edits: Vec<lsp::TextEdit>,
1530        cx: &mut ModelContext<Self>,
1531    ) -> Result<Vec<clock::Local>> {
1532        for edit in &edits {
1533            let range = range_from_lsp(edit.range);
1534            if self.clip_point_utf16(range.start, Bias::Left) != range.start
1535                || self.clip_point_utf16(range.end, Bias::Left) != range.end
1536            {
1537                return Err(anyhow!(
1538                    "invalid formatting edits received from language server"
1539                ));
1540            }
1541        }
1542
1543        self.start_transaction();
1544        let edit_ids = edits
1545            .into_iter()
1546            .rev()
1547            .filter_map(|edit| self.edit([range_from_lsp(edit.range)], edit.new_text, cx))
1548            .collect();
1549        self.end_transaction(cx);
1550        Ok(edit_ids)
1551    }
1552
1553    fn did_edit(
1554        &mut self,
1555        old_version: &clock::Global,
1556        was_dirty: bool,
1557        cx: &mut ModelContext<Self>,
1558    ) {
1559        if self.edits_since::<usize>(old_version).next().is_none() {
1560            return;
1561        }
1562
1563        self.reparse(cx);
1564        self.update_language_server(cx);
1565
1566        cx.emit(Event::Edited);
1567        if !was_dirty {
1568            cx.emit(Event::Dirtied);
1569        }
1570        cx.notify();
1571    }
1572
1573    fn grammar(&self) -> Option<&Arc<Grammar>> {
1574        self.language.as_ref().and_then(|l| l.grammar.as_ref())
1575    }
1576
1577    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1578        &mut self,
1579        ops: I,
1580        cx: &mut ModelContext<Self>,
1581    ) -> Result<()> {
1582        self.pending_autoindent.take();
1583        let was_dirty = self.is_dirty();
1584        let old_version = self.version.clone();
1585        let mut deferred_ops = Vec::new();
1586        let buffer_ops = ops
1587            .into_iter()
1588            .filter_map(|op| match op {
1589                Operation::Buffer(op) => Some(op),
1590                _ => {
1591                    if self.can_apply_op(&op) {
1592                        self.apply_op(op, cx);
1593                    } else {
1594                        deferred_ops.push(op);
1595                    }
1596                    None
1597                }
1598            })
1599            .collect::<Vec<_>>();
1600        self.text.apply_ops(buffer_ops)?;
1601        self.deferred_ops.insert(deferred_ops);
1602        self.flush_deferred_ops(cx);
1603        self.did_edit(&old_version, was_dirty, cx);
1604        // Notify independently of whether the buffer was edited as the operations could include a
1605        // selection update.
1606        cx.notify();
1607        Ok(())
1608    }
1609
1610    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1611        let mut deferred_ops = Vec::new();
1612        for op in self.deferred_ops.drain().iter().cloned() {
1613            if self.can_apply_op(&op) {
1614                self.apply_op(op, cx);
1615            } else {
1616                deferred_ops.push(op);
1617            }
1618        }
1619        self.deferred_ops.insert(deferred_ops);
1620    }
1621
1622    fn can_apply_op(&self, operation: &Operation) -> bool {
1623        match operation {
1624            Operation::Buffer(_) => {
1625                unreachable!("buffer operations should never be applied at this layer")
1626            }
1627            Operation::UpdateDiagnostics {
1628                diagnostics: diagnostic_set,
1629                ..
1630            } => diagnostic_set.iter().all(|diagnostic| {
1631                self.text.can_resolve(&diagnostic.range.start)
1632                    && self.text.can_resolve(&diagnostic.range.end)
1633            }),
1634            Operation::UpdateSelections { selections, .. } => selections
1635                .iter()
1636                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1637            Operation::UpdateCompletionTriggers { .. } => true,
1638        }
1639    }
1640
1641    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1642        match operation {
1643            Operation::Buffer(_) => {
1644                unreachable!("buffer operations should never be applied at this layer")
1645            }
1646            Operation::UpdateDiagnostics {
1647                diagnostics: diagnostic_set,
1648                ..
1649            } => {
1650                let snapshot = self.snapshot();
1651                self.apply_diagnostic_update(
1652                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1653                    cx,
1654                );
1655            }
1656            Operation::UpdateSelections {
1657                replica_id,
1658                selections,
1659                lamport_timestamp,
1660            } => {
1661                if let Some(set) = self.remote_selections.get(&replica_id) {
1662                    if set.lamport_timestamp > lamport_timestamp {
1663                        return;
1664                    }
1665                }
1666
1667                self.remote_selections.insert(
1668                    replica_id,
1669                    SelectionSet {
1670                        selections,
1671                        lamport_timestamp,
1672                    },
1673                );
1674                self.text.lamport_clock.observe(lamport_timestamp);
1675                self.selections_update_count += 1;
1676            }
1677            Operation::UpdateCompletionTriggers { triggers } => {
1678                self.completion_triggers = triggers;
1679            }
1680        }
1681    }
1682
1683    fn apply_diagnostic_update(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
1684        self.diagnostics = diagnostics;
1685        self.diagnostics_update_count += 1;
1686        cx.notify();
1687        cx.emit(Event::DiagnosticsUpdated);
1688    }
1689
1690    #[cfg(not(test))]
1691    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1692        if let Some(file) = &self.file {
1693            file.buffer_updated(self.remote_id(), operation, cx.as_mut());
1694        }
1695    }
1696
1697    #[cfg(test)]
1698    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
1699        self.operations.push(operation);
1700    }
1701
1702    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1703        self.remote_selections.remove(&replica_id);
1704        cx.notify();
1705    }
1706
1707    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1708        let was_dirty = self.is_dirty();
1709        let old_version = self.version.clone();
1710
1711        if let Some((transaction_id, operation)) = self.text.undo() {
1712            self.send_operation(Operation::Buffer(operation), cx);
1713            self.did_edit(&old_version, was_dirty, cx);
1714            Some(transaction_id)
1715        } else {
1716            None
1717        }
1718    }
1719
1720    pub fn undo_transaction(
1721        &mut self,
1722        transaction_id: TransactionId,
1723        cx: &mut ModelContext<Self>,
1724    ) -> bool {
1725        let was_dirty = self.is_dirty();
1726        let old_version = self.version.clone();
1727
1728        if let Some(operation) = self.text.undo_transaction(transaction_id) {
1729            self.send_operation(Operation::Buffer(operation), cx);
1730            self.did_edit(&old_version, was_dirty, cx);
1731            true
1732        } else {
1733            false
1734        }
1735    }
1736
1737    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1738        let was_dirty = self.is_dirty();
1739        let old_version = self.version.clone();
1740
1741        if let Some((transaction_id, operation)) = self.text.redo() {
1742            self.send_operation(Operation::Buffer(operation), cx);
1743            self.did_edit(&old_version, was_dirty, cx);
1744            Some(transaction_id)
1745        } else {
1746            None
1747        }
1748    }
1749
1750    pub fn redo_transaction(
1751        &mut self,
1752        transaction_id: TransactionId,
1753        cx: &mut ModelContext<Self>,
1754    ) -> bool {
1755        let was_dirty = self.is_dirty();
1756        let old_version = self.version.clone();
1757
1758        if let Some(operation) = self.text.redo_transaction(transaction_id) {
1759            self.send_operation(Operation::Buffer(operation), cx);
1760            self.did_edit(&old_version, was_dirty, cx);
1761            true
1762        } else {
1763            false
1764        }
1765    }
1766
1767    pub fn completions<T>(
1768        &self,
1769        position: T,
1770        cx: &mut ModelContext<Self>,
1771    ) -> Task<Result<Vec<Completion<Anchor>>>>
1772    where
1773        T: ToOffset,
1774    {
1775        let file = if let Some(file) = self.file.as_ref() {
1776            file
1777        } else {
1778            return Task::ready(Ok(Default::default()));
1779        };
1780        let language = self.language.clone();
1781
1782        if let Some(file) = file.as_local() {
1783            let server = if let Some(language_server) = self.language_server.as_ref() {
1784                language_server.server.clone()
1785            } else {
1786                return Task::ready(Ok(Default::default()));
1787            };
1788            let abs_path = file.abs_path(cx);
1789            let position = self.offset_to_point_utf16(position.to_offset(self));
1790
1791            cx.spawn(|this, cx| async move {
1792                let completions = server
1793                    .request::<lsp::request::Completion>(lsp::CompletionParams {
1794                        text_document_position: lsp::TextDocumentPositionParams::new(
1795                            lsp::TextDocumentIdentifier::new(
1796                                lsp::Url::from_file_path(abs_path).unwrap(),
1797                            ),
1798                            position.to_lsp_position(),
1799                        ),
1800                        context: Default::default(),
1801                        work_done_progress_params: Default::default(),
1802                        partial_result_params: Default::default(),
1803                    })
1804                    .await?;
1805
1806                let completions = if let Some(completions) = completions {
1807                    match completions {
1808                        lsp::CompletionResponse::Array(completions) => completions,
1809                        lsp::CompletionResponse::List(list) => list.items,
1810                    }
1811                } else {
1812                    Default::default()
1813                };
1814
1815                this.read_with(&cx, |this, _| {
1816                    Ok(completions.into_iter().filter_map(|lsp_completion| {
1817                        let (old_range, new_text) = match lsp_completion.text_edit.as_ref()? {
1818                            lsp::CompletionTextEdit::Edit(edit) => (range_from_lsp(edit.range), edit.new_text.clone()),
1819                            lsp::CompletionTextEdit::InsertAndReplace(_) => {
1820                                log::info!("received an insert and replace completion but we don't yet support that");
1821                                return None
1822                            },
1823                        };
1824
1825                        let clipped_start = this.clip_point_utf16(old_range.start, Bias::Left);
1826                        let clipped_end = this.clip_point_utf16(old_range.end, Bias::Left) ;
1827                        if clipped_start == old_range.start && clipped_end == old_range.end {
1828                            Some(Completion {
1829                                old_range: this.anchor_before(old_range.start)..this.anchor_after(old_range.end),
1830                                new_text,
1831                                label: language.as_ref().and_then(|l| l.label_for_completion(&lsp_completion)).unwrap_or_else(|| CompletionLabel::plain(&lsp_completion)),
1832                                lsp_completion,
1833                            })
1834                        } else {
1835                            None
1836                        }
1837                    }).collect())
1838                })
1839            })
1840        } else {
1841            file.completions(
1842                self.remote_id(),
1843                self.anchor_before(position),
1844                language,
1845                cx.as_mut(),
1846            )
1847        }
1848    }
1849
1850    pub fn code_actions<T>(
1851        &self,
1852        position: T,
1853        cx: &mut ModelContext<Self>,
1854    ) -> Task<Result<Vec<CodeAction<Anchor>>>>
1855    where
1856        T: ToPointUtf16,
1857    {
1858        let file = if let Some(file) = self.file.as_ref() {
1859            file
1860        } else {
1861            return Task::ready(Ok(Default::default()));
1862        };
1863
1864        if let Some(file) = file.as_local() {
1865            let server = if let Some(language_server) = self.language_server.as_ref() {
1866                language_server.server.clone()
1867            } else {
1868                return Task::ready(Ok(Default::default()));
1869            };
1870            let abs_path = file.abs_path(cx);
1871            let position = position.to_point_utf16(self);
1872            let anchor = self.anchor_after(position);
1873
1874            cx.foreground().spawn(async move {
1875                let actions = server
1876                    .request::<lsp::request::CodeActionRequest>(lsp::CodeActionParams {
1877                        text_document: lsp::TextDocumentIdentifier::new(
1878                            lsp::Url::from_file_path(abs_path).unwrap(),
1879                        ),
1880                        range: lsp::Range::new(
1881                            position.to_lsp_position(),
1882                            position.to_lsp_position(),
1883                        ),
1884                        work_done_progress_params: Default::default(),
1885                        partial_result_params: Default::default(),
1886                        context: lsp::CodeActionContext {
1887                            diagnostics: Default::default(),
1888                            only: Some(vec![
1889                                lsp::CodeActionKind::QUICKFIX,
1890                                lsp::CodeActionKind::REFACTOR,
1891                                lsp::CodeActionKind::REFACTOR_EXTRACT,
1892                            ]),
1893                        },
1894                    })
1895                    .await?
1896                    .unwrap_or_default()
1897                    .into_iter()
1898                    .filter_map(|entry| {
1899                        if let lsp::CodeActionOrCommand::CodeAction(lsp_action) = entry {
1900                            if lsp_action.data.is_none() {
1901                                log::warn!("skipping code action without data {lsp_action:?}");
1902                                None
1903                            } else {
1904                                Some(CodeAction {
1905                                    position: anchor.clone(),
1906                                    lsp_action,
1907                                })
1908                            }
1909                        } else {
1910                            None
1911                        }
1912                    })
1913                    .collect();
1914                Ok(actions)
1915            })
1916        } else {
1917            log::info!("code actions are not implemented for guests");
1918            Task::ready(Ok(Default::default()))
1919        }
1920    }
1921
1922    pub fn apply_additional_edits_for_completion(
1923        &mut self,
1924        completion: Completion<Anchor>,
1925        push_to_history: bool,
1926        cx: &mut ModelContext<Self>,
1927    ) -> Task<Result<Vec<clock::Local>>> {
1928        let file = if let Some(file) = self.file.as_ref() {
1929            file
1930        } else {
1931            return Task::ready(Ok(Default::default()));
1932        };
1933
1934        if file.is_local() {
1935            let server = if let Some(lang) = self.language_server.as_ref() {
1936                lang.server.clone()
1937            } else {
1938                return Task::ready(Ok(Default::default()));
1939            };
1940
1941            cx.spawn(|this, mut cx| async move {
1942                let resolved_completion = server
1943                    .request::<lsp::request::ResolveCompletionItem>(completion.lsp_completion)
1944                    .await?;
1945                if let Some(additional_edits) = resolved_completion.additional_text_edits {
1946                    this.update(&mut cx, |this, cx| {
1947                        if !push_to_history {
1948                            this.avoid_grouping_next_transaction();
1949                        }
1950                        this.start_transaction();
1951                        let edit_ids = this.apply_lsp_edits(additional_edits, cx);
1952                        if let Some(transaction_id) = this.end_transaction(cx) {
1953                            if !push_to_history {
1954                                this.text.forget_transaction(transaction_id);
1955                            }
1956                        }
1957                        edit_ids
1958                    })
1959                } else {
1960                    Ok(Default::default())
1961                }
1962            })
1963        } else {
1964            let apply_edits = file.apply_additional_edits_for_completion(
1965                self.remote_id(),
1966                completion,
1967                cx.as_mut(),
1968            );
1969            cx.spawn(|this, mut cx| async move {
1970                let edit_ids = apply_edits.await?;
1971                this.update(&mut cx, |this, _| this.text.wait_for_edits(&edit_ids))
1972                    .await;
1973                if push_to_history {
1974                    this.update(&mut cx, |this, _| {
1975                        this.text
1976                            .push_transaction(edit_ids.iter().copied(), Instant::now());
1977                    });
1978                }
1979                Ok(edit_ids)
1980            })
1981        }
1982    }
1983
1984    pub fn completion_triggers(&self) -> &[String] {
1985        &self.completion_triggers
1986    }
1987}
1988
1989#[cfg(any(test, feature = "test-support"))]
1990impl Buffer {
1991    pub fn set_group_interval(&mut self, group_interval: Duration) {
1992        self.text.set_group_interval(group_interval);
1993    }
1994
1995    pub fn randomly_edit<T>(
1996        &mut self,
1997        rng: &mut T,
1998        old_range_count: usize,
1999        cx: &mut ModelContext<Self>,
2000    ) where
2001        T: rand::Rng,
2002    {
2003        let mut old_ranges: Vec<Range<usize>> = Vec::new();
2004        for _ in 0..old_range_count {
2005            let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
2006            if last_end > self.len() {
2007                break;
2008            }
2009            old_ranges.push(self.text.random_byte_range(last_end, rng));
2010        }
2011        let new_text_len = rng.gen_range(0..10);
2012        let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
2013            .take(new_text_len)
2014            .collect();
2015        log::info!(
2016            "mutating buffer {} at {:?}: {:?}",
2017            self.replica_id(),
2018            old_ranges,
2019            new_text
2020        );
2021        self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
2022    }
2023
2024    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
2025        let was_dirty = self.is_dirty();
2026        let old_version = self.version.clone();
2027
2028        let ops = self.text.randomly_undo_redo(rng);
2029        if !ops.is_empty() {
2030            for op in ops {
2031                self.send_operation(Operation::Buffer(op), cx);
2032                self.did_edit(&old_version, was_dirty, cx);
2033            }
2034        }
2035    }
2036}
2037
2038impl Entity for Buffer {
2039    type Event = Event;
2040
2041    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
2042        if let Some(file) = self.file.as_ref() {
2043            file.buffer_removed(self.remote_id(), cx);
2044        }
2045    }
2046}
2047
2048impl Deref for Buffer {
2049    type Target = TextBuffer;
2050
2051    fn deref(&self) -> &Self::Target {
2052        &self.text
2053    }
2054}
2055
2056impl BufferSnapshot {
2057    fn suggest_autoindents<'a>(
2058        &'a self,
2059        row_range: Range<u32>,
2060    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
2061        let mut query_cursor = QueryCursorHandle::new();
2062        if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
2063            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
2064
2065            // Get the "indentation ranges" that intersect this row range.
2066            let indent_capture_ix = grammar.indents_query.capture_index_for_name("indent");
2067            let end_capture_ix = grammar.indents_query.capture_index_for_name("end");
2068            query_cursor.set_point_range(
2069                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
2070                    ..Point::new(row_range.end, 0).to_ts_point(),
2071            );
2072            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
2073            for mat in query_cursor.matches(
2074                &grammar.indents_query,
2075                tree.root_node(),
2076                TextProvider(self.as_rope()),
2077            ) {
2078                let mut node_kind = "";
2079                let mut start: Option<Point> = None;
2080                let mut end: Option<Point> = None;
2081                for capture in mat.captures {
2082                    if Some(capture.index) == indent_capture_ix {
2083                        node_kind = capture.node.kind();
2084                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
2085                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
2086                    } else if Some(capture.index) == end_capture_ix {
2087                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
2088                    }
2089                }
2090
2091                if let Some((start, end)) = start.zip(end) {
2092                    if start.row == end.row {
2093                        continue;
2094                    }
2095
2096                    let range = start..end;
2097                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
2098                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
2099                        Ok(ix) => {
2100                            let prev_range = &mut indentation_ranges[ix];
2101                            prev_range.0.end = prev_range.0.end.max(range.end);
2102                        }
2103                    }
2104                }
2105            }
2106
2107            let mut prev_row = prev_non_blank_row.unwrap_or(0);
2108            Some(row_range.map(move |row| {
2109                let row_start = Point::new(row, self.indent_column_for_line(row));
2110
2111                let mut indent_from_prev_row = false;
2112                let mut outdent_to_row = u32::MAX;
2113                for (range, _node_kind) in &indentation_ranges {
2114                    if range.start.row >= row {
2115                        break;
2116                    }
2117
2118                    if range.start.row == prev_row && range.end > row_start {
2119                        indent_from_prev_row = true;
2120                    }
2121                    if range.end.row >= prev_row && range.end <= row_start {
2122                        outdent_to_row = outdent_to_row.min(range.start.row);
2123                    }
2124                }
2125
2126                let suggestion = if outdent_to_row == prev_row {
2127                    IndentSuggestion {
2128                        basis_row: prev_row,
2129                        indent: false,
2130                    }
2131                } else if indent_from_prev_row {
2132                    IndentSuggestion {
2133                        basis_row: prev_row,
2134                        indent: true,
2135                    }
2136                } else if outdent_to_row < prev_row {
2137                    IndentSuggestion {
2138                        basis_row: outdent_to_row,
2139                        indent: false,
2140                    }
2141                } else {
2142                    IndentSuggestion {
2143                        basis_row: prev_row,
2144                        indent: false,
2145                    }
2146                };
2147
2148                prev_row = row;
2149                suggestion
2150            }))
2151        } else {
2152            None
2153        }
2154    }
2155
2156    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2157        while row > 0 {
2158            row -= 1;
2159            if !self.is_line_blank(row) {
2160                return Some(row);
2161            }
2162        }
2163        None
2164    }
2165
2166    pub fn chunks<'a, T: ToOffset>(
2167        &'a self,
2168        range: Range<T>,
2169        language_aware: bool,
2170    ) -> BufferChunks<'a> {
2171        let range = range.start.to_offset(self)..range.end.to_offset(self);
2172
2173        let mut tree = None;
2174        let mut diagnostic_endpoints = Vec::new();
2175        if language_aware {
2176            tree = self.tree.as_ref();
2177            for entry in self.diagnostics_in_range::<_, usize>(range.clone()) {
2178                diagnostic_endpoints.push(DiagnosticEndpoint {
2179                    offset: entry.range.start,
2180                    is_start: true,
2181                    severity: entry.diagnostic.severity,
2182                });
2183                diagnostic_endpoints.push(DiagnosticEndpoint {
2184                    offset: entry.range.end,
2185                    is_start: false,
2186                    severity: entry.diagnostic.severity,
2187                });
2188            }
2189            diagnostic_endpoints
2190                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2191        }
2192
2193        BufferChunks::new(
2194            self.text.as_rope(),
2195            range,
2196            tree,
2197            self.grammar(),
2198            diagnostic_endpoints,
2199        )
2200    }
2201
2202    pub fn language(&self) -> Option<&Arc<Language>> {
2203        self.language.as_ref()
2204    }
2205
2206    fn grammar(&self) -> Option<&Arc<Grammar>> {
2207        self.language
2208            .as_ref()
2209            .and_then(|language| language.grammar.as_ref())
2210    }
2211
2212    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2213        let tree = self.tree.as_ref()?;
2214        let range = range.start.to_offset(self)..range.end.to_offset(self);
2215        let mut cursor = tree.root_node().walk();
2216
2217        // Descend to smallest leaf that touches or exceeds the start of the range.
2218        while cursor.goto_first_child_for_byte(range.start).is_some() {}
2219
2220        // Ascend to the smallest ancestor that strictly contains the range.
2221        loop {
2222            let node_range = cursor.node().byte_range();
2223            if node_range.start <= range.start
2224                && node_range.end >= range.end
2225                && node_range.len() > range.len()
2226            {
2227                break;
2228            }
2229            if !cursor.goto_parent() {
2230                break;
2231            }
2232        }
2233
2234        let left_node = cursor.node();
2235
2236        // For an empty range, try to find another node immediately to the right of the range.
2237        if left_node.end_byte() == range.start {
2238            let mut right_node = None;
2239            while !cursor.goto_next_sibling() {
2240                if !cursor.goto_parent() {
2241                    break;
2242                }
2243            }
2244
2245            while cursor.node().start_byte() == range.start {
2246                right_node = Some(cursor.node());
2247                if !cursor.goto_first_child() {
2248                    break;
2249                }
2250            }
2251
2252            if let Some(right_node) = right_node {
2253                if right_node.is_named() || !left_node.is_named() {
2254                    return Some(right_node.byte_range());
2255                }
2256            }
2257        }
2258
2259        Some(left_node.byte_range())
2260    }
2261
2262    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2263        let tree = self.tree.as_ref()?;
2264        let grammar = self
2265            .language
2266            .as_ref()
2267            .and_then(|language| language.grammar.as_ref())?;
2268
2269        let mut cursor = QueryCursorHandle::new();
2270        let matches = cursor.matches(
2271            &grammar.outline_query,
2272            tree.root_node(),
2273            TextProvider(self.as_rope()),
2274        );
2275
2276        let mut chunks = self.chunks(0..self.len(), true);
2277
2278        let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
2279        let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
2280        let context_capture_ix = grammar
2281            .outline_query
2282            .capture_index_for_name("context")
2283            .unwrap_or(u32::MAX);
2284
2285        let mut stack = Vec::<Range<usize>>::new();
2286        let items = matches
2287            .filter_map(|mat| {
2288                let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
2289                let range = item_node.start_byte()..item_node.end_byte();
2290                let mut text = String::new();
2291                let mut name_ranges = Vec::new();
2292                let mut highlight_ranges = Vec::new();
2293
2294                for capture in mat.captures {
2295                    let node_is_name;
2296                    if capture.index == name_capture_ix {
2297                        node_is_name = true;
2298                    } else if capture.index == context_capture_ix {
2299                        node_is_name = false;
2300                    } else {
2301                        continue;
2302                    }
2303
2304                    let range = capture.node.start_byte()..capture.node.end_byte();
2305                    if !text.is_empty() {
2306                        text.push(' ');
2307                    }
2308                    if node_is_name {
2309                        let mut start = text.len();
2310                        let end = start + range.len();
2311
2312                        // When multiple names are captured, then the matcheable text
2313                        // includes the whitespace in between the names.
2314                        if !name_ranges.is_empty() {
2315                            start -= 1;
2316                        }
2317
2318                        name_ranges.push(start..end);
2319                    }
2320
2321                    let mut offset = range.start;
2322                    chunks.seek(offset);
2323                    while let Some(mut chunk) = chunks.next() {
2324                        if chunk.text.len() > range.end - offset {
2325                            chunk.text = &chunk.text[0..(range.end - offset)];
2326                            offset = range.end;
2327                        } else {
2328                            offset += chunk.text.len();
2329                        }
2330                        let style = chunk
2331                            .highlight_id
2332                            .zip(theme)
2333                            .and_then(|(highlight, theme)| highlight.style(theme));
2334                        if let Some(style) = style {
2335                            let start = text.len();
2336                            let end = start + chunk.text.len();
2337                            highlight_ranges.push((start..end, style));
2338                        }
2339                        text.push_str(chunk.text);
2340                        if offset >= range.end {
2341                            break;
2342                        }
2343                    }
2344                }
2345
2346                while stack.last().map_or(false, |prev_range| {
2347                    !prev_range.contains(&range.start) || !prev_range.contains(&range.end)
2348                }) {
2349                    stack.pop();
2350                }
2351                stack.push(range.clone());
2352
2353                Some(OutlineItem {
2354                    depth: stack.len() - 1,
2355                    range: self.anchor_after(range.start)..self.anchor_before(range.end),
2356                    text,
2357                    highlight_ranges,
2358                    name_ranges,
2359                })
2360            })
2361            .collect::<Vec<_>>();
2362
2363        if items.is_empty() {
2364            None
2365        } else {
2366            Some(Outline::new(items))
2367        }
2368    }
2369
2370    pub fn enclosing_bracket_ranges<T: ToOffset>(
2371        &self,
2372        range: Range<T>,
2373    ) -> Option<(Range<usize>, Range<usize>)> {
2374        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
2375        let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
2376        let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
2377
2378        // Find bracket pairs that *inclusively* contain the given range.
2379        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
2380        let mut cursor = QueryCursorHandle::new();
2381        let matches = cursor.set_byte_range(range).matches(
2382            &grammar.brackets_query,
2383            tree.root_node(),
2384            TextProvider(self.as_rope()),
2385        );
2386
2387        // Get the ranges of the innermost pair of brackets.
2388        matches
2389            .filter_map(|mat| {
2390                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
2391                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
2392                Some((open.byte_range(), close.byte_range()))
2393            })
2394            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
2395    }
2396
2397    /*
2398    impl BufferSnapshot
2399      pub fn remote_selections_in_range(&self, Range<Anchor>) -> impl Iterator<Item = (ReplicaId, impl Iterator<Item = &Selection<Anchor>>)>
2400      pub fn remote_selections_in_range(&self, Range<Anchor>) -> impl Iterator<Item = (ReplicaId, i
2401    */
2402
2403    pub fn remote_selections_in_range<'a>(
2404        &'a self,
2405        range: Range<Anchor>,
2406    ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
2407    {
2408        self.remote_selections
2409            .iter()
2410            .filter(|(replica_id, set)| {
2411                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2412            })
2413            .map(move |(replica_id, set)| {
2414                let start_ix = match set.selections.binary_search_by(|probe| {
2415                    probe
2416                        .end
2417                        .cmp(&range.start, self)
2418                        .unwrap()
2419                        .then(Ordering::Greater)
2420                }) {
2421                    Ok(ix) | Err(ix) => ix,
2422                };
2423                let end_ix = match set.selections.binary_search_by(|probe| {
2424                    probe
2425                        .start
2426                        .cmp(&range.end, self)
2427                        .unwrap()
2428                        .then(Ordering::Less)
2429                }) {
2430                    Ok(ix) | Err(ix) => ix,
2431                };
2432
2433                (*replica_id, set.selections[start_ix..end_ix].iter())
2434            })
2435    }
2436
2437    pub fn diagnostics_in_range<'a, T, O>(
2438        &'a self,
2439        search_range: Range<T>,
2440    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2441    where
2442        T: 'a + Clone + ToOffset,
2443        O: 'a + FromAnchor,
2444    {
2445        self.diagnostics.range(search_range.clone(), self, true)
2446    }
2447
2448    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2449        let mut groups = Vec::new();
2450        self.diagnostics.groups(&mut groups, self);
2451        groups
2452    }
2453
2454    pub fn diagnostic_group<'a, O>(
2455        &'a self,
2456        group_id: usize,
2457    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2458    where
2459        O: 'a + FromAnchor,
2460    {
2461        self.diagnostics.group(group_id, self)
2462    }
2463
2464    pub fn diagnostics_update_count(&self) -> usize {
2465        self.diagnostics_update_count
2466    }
2467
2468    pub fn parse_count(&self) -> usize {
2469        self.parse_count
2470    }
2471
2472    pub fn selections_update_count(&self) -> usize {
2473        self.selections_update_count
2474    }
2475}
2476
2477impl Clone for BufferSnapshot {
2478    fn clone(&self) -> Self {
2479        Self {
2480            text: self.text.clone(),
2481            tree: self.tree.clone(),
2482            remote_selections: self.remote_selections.clone(),
2483            diagnostics: self.diagnostics.clone(),
2484            selections_update_count: self.selections_update_count,
2485            diagnostics_update_count: self.diagnostics_update_count,
2486            is_parsing: self.is_parsing,
2487            language: self.language.clone(),
2488            parse_count: self.parse_count,
2489        }
2490    }
2491}
2492
2493impl Deref for BufferSnapshot {
2494    type Target = text::BufferSnapshot;
2495
2496    fn deref(&self) -> &Self::Target {
2497        &self.text
2498    }
2499}
2500
2501impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
2502    type I = ByteChunks<'a>;
2503
2504    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
2505        ByteChunks(self.0.chunks_in_range(node.byte_range()))
2506    }
2507}
2508
2509pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
2510
2511impl<'a> Iterator for ByteChunks<'a> {
2512    type Item = &'a [u8];
2513
2514    fn next(&mut self) -> Option<Self::Item> {
2515        self.0.next().map(str::as_bytes)
2516    }
2517}
2518
2519unsafe impl<'a> Send for BufferChunks<'a> {}
2520
2521impl<'a> BufferChunks<'a> {
2522    pub(crate) fn new(
2523        text: &'a Rope,
2524        range: Range<usize>,
2525        tree: Option<&'a Tree>,
2526        grammar: Option<&'a Arc<Grammar>>,
2527        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2528    ) -> Self {
2529        let mut highlights = None;
2530        if let Some((grammar, tree)) = grammar.zip(tree) {
2531            let mut query_cursor = QueryCursorHandle::new();
2532
2533            // TODO - add a Tree-sitter API to remove the need for this.
2534            let cursor = unsafe {
2535                std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
2536            };
2537            let captures = cursor.set_byte_range(range.clone()).captures(
2538                &grammar.highlights_query,
2539                tree.root_node(),
2540                TextProvider(text),
2541            );
2542            highlights = Some(BufferChunkHighlights {
2543                captures,
2544                next_capture: None,
2545                stack: Default::default(),
2546                highlight_map: grammar.highlight_map(),
2547                _query_cursor: query_cursor,
2548            })
2549        }
2550
2551        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2552        let chunks = text.chunks_in_range(range.clone());
2553
2554        BufferChunks {
2555            range,
2556            chunks,
2557            diagnostic_endpoints,
2558            error_depth: 0,
2559            warning_depth: 0,
2560            information_depth: 0,
2561            hint_depth: 0,
2562            highlights,
2563        }
2564    }
2565
2566    pub fn seek(&mut self, offset: usize) {
2567        self.range.start = offset;
2568        self.chunks.seek(self.range.start);
2569        if let Some(highlights) = self.highlights.as_mut() {
2570            highlights
2571                .stack
2572                .retain(|(end_offset, _)| *end_offset > offset);
2573            if let Some((mat, capture_ix)) = &highlights.next_capture {
2574                let capture = mat.captures[*capture_ix as usize];
2575                if offset >= capture.node.start_byte() {
2576                    let next_capture_end = capture.node.end_byte();
2577                    if offset < next_capture_end {
2578                        highlights.stack.push((
2579                            next_capture_end,
2580                            highlights.highlight_map.get(capture.index),
2581                        ));
2582                    }
2583                    highlights.next_capture.take();
2584                }
2585            }
2586            highlights.captures.set_byte_range(self.range.clone());
2587        }
2588    }
2589
2590    pub fn offset(&self) -> usize {
2591        self.range.start
2592    }
2593
2594    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2595        let depth = match endpoint.severity {
2596            DiagnosticSeverity::ERROR => &mut self.error_depth,
2597            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2598            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2599            DiagnosticSeverity::HINT => &mut self.hint_depth,
2600            _ => return,
2601        };
2602        if endpoint.is_start {
2603            *depth += 1;
2604        } else {
2605            *depth -= 1;
2606        }
2607    }
2608
2609    fn current_diagnostic_severity(&mut self) -> Option<DiagnosticSeverity> {
2610        if self.error_depth > 0 {
2611            Some(DiagnosticSeverity::ERROR)
2612        } else if self.warning_depth > 0 {
2613            Some(DiagnosticSeverity::WARNING)
2614        } else if self.information_depth > 0 {
2615            Some(DiagnosticSeverity::INFORMATION)
2616        } else if self.hint_depth > 0 {
2617            Some(DiagnosticSeverity::HINT)
2618        } else {
2619            None
2620        }
2621    }
2622}
2623
2624impl<'a> Iterator for BufferChunks<'a> {
2625    type Item = Chunk<'a>;
2626
2627    fn next(&mut self) -> Option<Self::Item> {
2628        let mut next_capture_start = usize::MAX;
2629        let mut next_diagnostic_endpoint = usize::MAX;
2630
2631        if let Some(highlights) = self.highlights.as_mut() {
2632            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2633                if *parent_capture_end <= self.range.start {
2634                    highlights.stack.pop();
2635                } else {
2636                    break;
2637                }
2638            }
2639
2640            if highlights.next_capture.is_none() {
2641                highlights.next_capture = highlights.captures.next();
2642            }
2643
2644            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2645                let capture = mat.captures[*capture_ix as usize];
2646                if self.range.start < capture.node.start_byte() {
2647                    next_capture_start = capture.node.start_byte();
2648                    break;
2649                } else {
2650                    let highlight_id = highlights.highlight_map.get(capture.index);
2651                    highlights
2652                        .stack
2653                        .push((capture.node.end_byte(), highlight_id));
2654                    highlights.next_capture = highlights.captures.next();
2655                }
2656            }
2657        }
2658
2659        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2660            if endpoint.offset <= self.range.start {
2661                self.update_diagnostic_depths(endpoint);
2662                self.diagnostic_endpoints.next();
2663            } else {
2664                next_diagnostic_endpoint = endpoint.offset;
2665                break;
2666            }
2667        }
2668
2669        if let Some(chunk) = self.chunks.peek() {
2670            let chunk_start = self.range.start;
2671            let mut chunk_end = (self.chunks.offset() + chunk.len())
2672                .min(next_capture_start)
2673                .min(next_diagnostic_endpoint);
2674            let mut highlight_id = None;
2675            if let Some(highlights) = self.highlights.as_ref() {
2676                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2677                    chunk_end = chunk_end.min(*parent_capture_end);
2678                    highlight_id = Some(*parent_highlight_id);
2679                }
2680            }
2681
2682            let slice =
2683                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2684            self.range.start = chunk_end;
2685            if self.range.start == self.chunks.offset() + chunk.len() {
2686                self.chunks.next().unwrap();
2687            }
2688
2689            Some(Chunk {
2690                text: slice,
2691                highlight_id,
2692                diagnostic: self.current_diagnostic_severity(),
2693            })
2694        } else {
2695            None
2696        }
2697    }
2698}
2699
2700impl QueryCursorHandle {
2701    pub(crate) fn new() -> Self {
2702        QueryCursorHandle(Some(
2703            QUERY_CURSORS
2704                .lock()
2705                .pop()
2706                .unwrap_or_else(|| QueryCursor::new()),
2707        ))
2708    }
2709}
2710
2711impl Deref for QueryCursorHandle {
2712    type Target = QueryCursor;
2713
2714    fn deref(&self) -> &Self::Target {
2715        self.0.as_ref().unwrap()
2716    }
2717}
2718
2719impl DerefMut for QueryCursorHandle {
2720    fn deref_mut(&mut self) -> &mut Self::Target {
2721        self.0.as_mut().unwrap()
2722    }
2723}
2724
2725impl Drop for QueryCursorHandle {
2726    fn drop(&mut self) {
2727        let mut cursor = self.0.take().unwrap();
2728        cursor.set_byte_range(0..usize::MAX);
2729        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2730        QUERY_CURSORS.lock().push(cursor)
2731    }
2732}
2733
2734trait ToTreeSitterPoint {
2735    fn to_ts_point(self) -> tree_sitter::Point;
2736    fn from_ts_point(point: tree_sitter::Point) -> Self;
2737}
2738
2739impl ToTreeSitterPoint for Point {
2740    fn to_ts_point(self) -> tree_sitter::Point {
2741        tree_sitter::Point::new(self.row as usize, self.column as usize)
2742    }
2743
2744    fn from_ts_point(point: tree_sitter::Point) -> Self {
2745        Point::new(point.row as u32, point.column as u32)
2746    }
2747}
2748
2749impl operation_queue::Operation for Operation {
2750    fn lamport_timestamp(&self) -> clock::Lamport {
2751        match self {
2752            Operation::Buffer(_) => {
2753                unreachable!("buffer operations should never be deferred at this layer")
2754            }
2755            Operation::UpdateDiagnostics {
2756                lamport_timestamp, ..
2757            }
2758            | Operation::UpdateSelections {
2759                lamport_timestamp, ..
2760            } => *lamport_timestamp,
2761            Operation::UpdateCompletionTriggers { .. } => {
2762                unreachable!("updating completion triggers should never be deferred")
2763            }
2764        }
2765    }
2766}
2767
2768impl LanguageServerState {
2769    fn snapshot_for_version(&mut self, version: usize) -> Result<&text::BufferSnapshot> {
2770        const OLD_VERSIONS_TO_RETAIN: usize = 10;
2771
2772        self.pending_snapshots
2773            .retain(|&v, _| v + OLD_VERSIONS_TO_RETAIN >= version);
2774        let snapshot = self
2775            .pending_snapshots
2776            .get(&version)
2777            .ok_or_else(|| anyhow!("missing snapshot"))?;
2778        Ok(&snapshot.buffer_snapshot)
2779    }
2780}
2781
2782impl Default for Diagnostic {
2783    fn default() -> Self {
2784        Self {
2785            code: Default::default(),
2786            severity: DiagnosticSeverity::ERROR,
2787            message: Default::default(),
2788            group_id: Default::default(),
2789            is_primary: Default::default(),
2790            is_valid: true,
2791            is_disk_based: false,
2792        }
2793    }
2794}
2795
2796impl<T> Completion<T> {
2797    pub fn sort_key(&self) -> (usize, &str) {
2798        let kind_key = match self.lsp_completion.kind {
2799            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2800            _ => 1,
2801        };
2802        (kind_key, &self.label.text[self.label.filter_range.clone()])
2803    }
2804
2805    pub fn is_snippet(&self) -> bool {
2806        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2807    }
2808}
2809
2810pub fn contiguous_ranges(
2811    values: impl Iterator<Item = u32>,
2812    max_len: usize,
2813) -> impl Iterator<Item = Range<u32>> {
2814    let mut values = values.into_iter();
2815    let mut current_range: Option<Range<u32>> = None;
2816    std::iter::from_fn(move || loop {
2817        if let Some(value) = values.next() {
2818            if let Some(range) = &mut current_range {
2819                if value == range.end && range.len() < max_len {
2820                    range.end += 1;
2821                    continue;
2822                }
2823            }
2824
2825            let prev_range = current_range.clone();
2826            current_range = Some(value..(value + 1));
2827            if prev_range.is_some() {
2828                return prev_range;
2829            }
2830        } else {
2831            return current_range.take();
2832        }
2833    })
2834}