buffer.rs

   1use crate::diagnostic_set::{DiagnosticEntry, DiagnosticGroup};
   2pub use crate::{
   3    diagnostic_set::DiagnosticSet,
   4    highlight_map::{HighlightId, HighlightMap},
   5    proto, BracketPair, Grammar, Language, LanguageConfig, LanguageRegistry, LanguageServerConfig,
   6    PLAIN_TEXT,
   7};
   8use anyhow::{anyhow, Result};
   9use clock::ReplicaId;
  10use futures::FutureExt as _;
  11use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, MutableAppContext, Task};
  12use lazy_static::lazy_static;
  13use lsp::LanguageServer;
  14use parking_lot::Mutex;
  15use postage::{prelude::Stream, sink::Sink, watch};
  16use similar::{ChangeTag, TextDiff};
  17use smol::future::yield_now;
  18use std::{
  19    any::Any,
  20    cell::RefCell,
  21    cmp::{self, Ordering},
  22    collections::{BTreeMap, HashMap},
  23    ffi::OsString,
  24    future::Future,
  25    iter::{Iterator, Peekable},
  26    ops::{Deref, DerefMut, Range, Sub},
  27    path::{Path, PathBuf},
  28    str,
  29    sync::Arc,
  30    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  31    vec,
  32};
  33use sum_tree::TreeMap;
  34use text::{operation_queue::OperationQueue, rope::TextDimension};
  35pub use text::{Buffer as TextBuffer, Operation as _, *};
  36use theme::SyntaxTheme;
  37use tree_sitter::{InputEdit, Parser, QueryCursor, Tree};
  38use util::{post_inc, TryFutureExt as _};
  39
  40#[cfg(any(test, feature = "test-support"))]
  41pub use tree_sitter_rust;
  42
  43pub use lsp::DiagnosticSeverity;
  44
  45thread_local! {
  46    static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
  47}
  48
  49lazy_static! {
  50    static ref QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Default::default();
  51}
  52
  53// TODO - Make this configurable
  54const INDENT_SIZE: u32 = 4;
  55
  56pub struct Buffer {
  57    text: TextBuffer,
  58    file: Option<Box<dyn File>>,
  59    saved_version: clock::Global,
  60    saved_mtime: SystemTime,
  61    language: Option<Arc<Language>>,
  62    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  63    pending_autoindent: Option<Task<()>>,
  64    sync_parse_timeout: Duration,
  65    syntax_tree: Mutex<Option<SyntaxTree>>,
  66    parsing_in_background: bool,
  67    parse_count: usize,
  68    remote_selections: TreeMap<ReplicaId, Arc<[Selection<Anchor>]>>,
  69    diagnostic_sets: Vec<DiagnosticSet>,
  70    diagnostics_update_count: usize,
  71    language_server: Option<LanguageServerState>,
  72    deferred_ops: OperationQueue<Operation>,
  73    #[cfg(test)]
  74    pub(crate) operations: Vec<Operation>,
  75}
  76
  77pub struct BufferSnapshot {
  78    text: text::BufferSnapshot,
  79    tree: Option<Tree>,
  80    diagnostic_sets: Vec<DiagnosticSet>,
  81    remote_selections: TreeMap<ReplicaId, Arc<[Selection<Anchor>]>>,
  82    diagnostics_update_count: usize,
  83    is_parsing: bool,
  84    language: Option<Arc<Language>>,
  85    parse_count: usize,
  86}
  87
  88#[derive(Clone, Debug, PartialEq, Eq)]
  89pub struct GroupId {
  90    source: Arc<str>,
  91    id: usize,
  92}
  93
  94#[derive(Clone, Debug, PartialEq, Eq)]
  95pub struct Diagnostic {
  96    pub code: Option<String>,
  97    pub severity: DiagnosticSeverity,
  98    pub message: String,
  99    pub group_id: usize,
 100    pub is_valid: bool,
 101    pub is_primary: bool,
 102    pub is_disk_based: bool,
 103}
 104
 105struct LanguageServerState {
 106    server: Arc<LanguageServer>,
 107    latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>,
 108    pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>,
 109    next_version: usize,
 110    _maintain_server: Task<Option<()>>,
 111}
 112
 113#[derive(Clone)]
 114struct LanguageServerSnapshot {
 115    buffer_snapshot: text::BufferSnapshot,
 116    version: usize,
 117    path: Arc<Path>,
 118}
 119
 120#[derive(Clone, Debug)]
 121pub enum Operation {
 122    Buffer(text::Operation),
 123    UpdateDiagnostics {
 124        provider_name: String,
 125        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 126        lamport_timestamp: clock::Lamport,
 127    },
 128    UpdateSelections {
 129        replica_id: ReplicaId,
 130        selections: Arc<[Selection<Anchor>]>,
 131        lamport_timestamp: clock::Lamport,
 132    },
 133    RemoveSelections {
 134        replica_id: ReplicaId,
 135        lamport_timestamp: clock::Lamport,
 136    },
 137}
 138
 139#[derive(Clone, Debug, Eq, PartialEq)]
 140pub enum Event {
 141    Edited,
 142    Dirtied,
 143    Saved,
 144    FileHandleChanged,
 145    Reloaded,
 146    Reparsed,
 147    DiagnosticsUpdated,
 148    Closed,
 149}
 150
 151pub trait File {
 152    fn worktree_id(&self) -> usize;
 153
 154    fn entry_id(&self) -> Option<usize>;
 155
 156    fn mtime(&self) -> SystemTime;
 157
 158    /// Returns the path of this file relative to the worktree's root directory.
 159    fn path(&self) -> &Arc<Path>;
 160
 161    /// Returns the absolute path of this file.
 162    fn abs_path(&self) -> Option<PathBuf>;
 163
 164    /// Returns the path of this file relative to the worktree's parent directory (this means it
 165    /// includes the name of the worktree's root folder).
 166    fn full_path(&self) -> PathBuf;
 167
 168    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 169    /// of its worktree, then this method will return the name of the worktree itself.
 170    fn file_name(&self) -> Option<OsString>;
 171
 172    fn is_deleted(&self) -> bool;
 173
 174    fn save(
 175        &self,
 176        buffer_id: u64,
 177        text: Rope,
 178        version: clock::Global,
 179        cx: &mut MutableAppContext,
 180    ) -> Task<Result<(clock::Global, SystemTime)>>;
 181
 182    fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>>;
 183
 184    fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext);
 185
 186    fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext);
 187
 188    fn boxed_clone(&self) -> Box<dyn File>;
 189
 190    fn as_any(&self) -> &dyn Any;
 191}
 192
 193struct QueryCursorHandle(Option<QueryCursor>);
 194
 195#[derive(Clone)]
 196struct SyntaxTree {
 197    tree: Tree,
 198    version: clock::Global,
 199}
 200
 201#[derive(Clone)]
 202struct AutoindentRequest {
 203    before_edit: BufferSnapshot,
 204    edited: Vec<Anchor>,
 205    inserted: Option<Vec<Range<Anchor>>>,
 206}
 207
 208#[derive(Debug)]
 209struct IndentSuggestion {
 210    basis_row: u32,
 211    indent: bool,
 212}
 213
 214struct TextProvider<'a>(&'a Rope);
 215
 216struct BufferChunkHighlights<'a> {
 217    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
 218    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
 219    stack: Vec<(usize, HighlightId)>,
 220    highlight_map: HighlightMap,
 221    theme: &'a SyntaxTheme,
 222    _query_cursor: QueryCursorHandle,
 223}
 224
 225pub struct BufferChunks<'a> {
 226    range: Range<usize>,
 227    chunks: rope::Chunks<'a>,
 228    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 229    error_depth: usize,
 230    warning_depth: usize,
 231    information_depth: usize,
 232    hint_depth: usize,
 233    highlights: Option<BufferChunkHighlights<'a>>,
 234}
 235
 236#[derive(Clone, Copy, Debug, Default)]
 237pub struct Chunk<'a> {
 238    pub text: &'a str,
 239    pub highlight_style: Option<HighlightStyle>,
 240    pub diagnostic: Option<DiagnosticSeverity>,
 241}
 242
 243pub(crate) struct Diff {
 244    base_version: clock::Global,
 245    new_text: Arc<str>,
 246    changes: Vec<(ChangeTag, usize)>,
 247}
 248
 249#[derive(Clone, Copy)]
 250struct DiagnosticEndpoint {
 251    offset: usize,
 252    is_start: bool,
 253    severity: DiagnosticSeverity,
 254}
 255
 256impl Buffer {
 257    pub fn new<T: Into<Arc<str>>>(
 258        replica_id: ReplicaId,
 259        base_text: T,
 260        cx: &mut ModelContext<Self>,
 261    ) -> Self {
 262        Self::build(
 263            TextBuffer::new(
 264                replica_id,
 265                cx.model_id() as u64,
 266                History::new(base_text.into()),
 267            ),
 268            None,
 269        )
 270    }
 271
 272    pub fn from_file<T: Into<Arc<str>>>(
 273        replica_id: ReplicaId,
 274        base_text: T,
 275        file: Box<dyn File>,
 276        cx: &mut ModelContext<Self>,
 277    ) -> Self {
 278        Self::build(
 279            TextBuffer::new(
 280                replica_id,
 281                cx.model_id() as u64,
 282                History::new(base_text.into()),
 283            ),
 284            Some(file),
 285        )
 286    }
 287
 288    pub fn from_proto(
 289        replica_id: ReplicaId,
 290        message: proto::Buffer,
 291        file: Option<Box<dyn File>>,
 292        cx: &mut ModelContext<Self>,
 293    ) -> Result<Self> {
 294        let mut buffer =
 295            text::Buffer::new(replica_id, message.id, History::new(message.content.into()));
 296        let ops = message
 297            .history
 298            .into_iter()
 299            .map(|op| text::Operation::Edit(proto::deserialize_edit_operation(op)));
 300        buffer.apply_ops(ops)?;
 301        let mut this = Self::build(buffer, file);
 302        for selection_set in message.selections {
 303            this.remote_selections.insert(
 304                selection_set.replica_id as ReplicaId,
 305                proto::deserialize_selections(selection_set.selections),
 306            );
 307        }
 308        let snapshot = this.snapshot();
 309        for diagnostic_set in message.diagnostic_sets {
 310            let (provider_name, entries) = proto::deserialize_diagnostic_set(diagnostic_set);
 311            this.apply_diagnostic_update(
 312                DiagnosticSet::from_sorted_entries(
 313                    provider_name,
 314                    entries.into_iter().cloned(),
 315                    &snapshot,
 316                ),
 317                cx,
 318            );
 319        }
 320
 321        Ok(this)
 322    }
 323
 324    pub fn to_proto(&self) -> proto::Buffer {
 325        proto::Buffer {
 326            id: self.remote_id(),
 327            content: self.text.base_text().to_string(),
 328            history: self
 329                .text
 330                .history()
 331                .map(proto::serialize_edit_operation)
 332                .collect(),
 333            selections: self
 334                .remote_selections
 335                .iter()
 336                .map(|(replica_id, selections)| proto::SelectionSet {
 337                    replica_id: *replica_id as u32,
 338                    selections: proto::serialize_selections(selections),
 339                })
 340                .collect(),
 341            diagnostic_sets: self
 342                .diagnostic_sets
 343                .iter()
 344                .map(|set| {
 345                    proto::serialize_diagnostic_set(set.provider_name().to_string(), set.iter())
 346                })
 347                .collect(),
 348        }
 349    }
 350
 351    pub fn with_language(
 352        mut self,
 353        language: Option<Arc<Language>>,
 354        language_server: Option<Arc<LanguageServer>>,
 355        cx: &mut ModelContext<Self>,
 356    ) -> Self {
 357        self.set_language(language, language_server, cx);
 358        self
 359    }
 360
 361    fn build(buffer: TextBuffer, file: Option<Box<dyn File>>) -> Self {
 362        let saved_mtime;
 363        if let Some(file) = file.as_ref() {
 364            saved_mtime = file.mtime();
 365        } else {
 366            saved_mtime = UNIX_EPOCH;
 367        }
 368
 369        Self {
 370            saved_mtime,
 371            saved_version: buffer.version(),
 372            text: buffer,
 373            file,
 374            syntax_tree: Mutex::new(None),
 375            parsing_in_background: false,
 376            parse_count: 0,
 377            sync_parse_timeout: Duration::from_millis(1),
 378            autoindent_requests: Default::default(),
 379            pending_autoindent: Default::default(),
 380            language: None,
 381            remote_selections: Default::default(),
 382            diagnostic_sets: Default::default(),
 383            diagnostics_update_count: 0,
 384            language_server: None,
 385            deferred_ops: OperationQueue::new(),
 386            #[cfg(test)]
 387            operations: Default::default(),
 388        }
 389    }
 390
 391    pub fn snapshot(&self) -> BufferSnapshot {
 392        BufferSnapshot {
 393            text: self.text.snapshot(),
 394            tree: self.syntax_tree(),
 395            remote_selections: self.remote_selections.clone(),
 396            diagnostic_sets: self.diagnostic_sets.clone(),
 397            diagnostics_update_count: self.diagnostics_update_count,
 398            is_parsing: self.parsing_in_background,
 399            language: self.language.clone(),
 400            parse_count: self.parse_count,
 401        }
 402    }
 403
 404    pub fn file(&self) -> Option<&dyn File> {
 405        self.file.as_deref()
 406    }
 407
 408    pub fn save(
 409        &mut self,
 410        cx: &mut ModelContext<Self>,
 411    ) -> Result<Task<Result<(clock::Global, SystemTime)>>> {
 412        let file = self
 413            .file
 414            .as_ref()
 415            .ok_or_else(|| anyhow!("buffer has no file"))?;
 416        let text = self.as_rope().clone();
 417        let version = self.version();
 418        let save = file.save(self.remote_id(), text, version, cx.as_mut());
 419        Ok(cx.spawn(|this, mut cx| async move {
 420            let (version, mtime) = save.await?;
 421            this.update(&mut cx, |this, cx| {
 422                this.did_save(version.clone(), mtime, None, cx);
 423            });
 424            Ok((version, mtime))
 425        }))
 426    }
 427
 428    pub fn set_language(
 429        &mut self,
 430        language: Option<Arc<Language>>,
 431        language_server: Option<Arc<lsp::LanguageServer>>,
 432        cx: &mut ModelContext<Self>,
 433    ) {
 434        self.language = language;
 435        self.language_server = if let Some(server) = language_server {
 436            let (latest_snapshot_tx, mut latest_snapshot_rx) = watch::channel();
 437            Some(LanguageServerState {
 438                latest_snapshot: latest_snapshot_tx,
 439                pending_snapshots: Default::default(),
 440                next_version: 0,
 441                server: server.clone(),
 442                _maintain_server: cx.background().spawn(
 443                    async move {
 444                        let mut prev_snapshot: Option<LanguageServerSnapshot> = None;
 445                        while let Some(snapshot) = latest_snapshot_rx.recv().await {
 446                            if let Some(snapshot) = snapshot {
 447                                let uri = lsp::Url::from_file_path(&snapshot.path).unwrap();
 448                                if let Some(prev_snapshot) = prev_snapshot {
 449                                    let changes = lsp::DidChangeTextDocumentParams {
 450                                        text_document: lsp::VersionedTextDocumentIdentifier::new(
 451                                            uri,
 452                                            snapshot.version as i32,
 453                                        ),
 454                                        content_changes: snapshot
 455                                            .buffer_snapshot
 456                                            .edits_since::<(PointUtf16, usize)>(
 457                                                prev_snapshot.buffer_snapshot.version(),
 458                                            )
 459                                            .map(|edit| {
 460                                                let edit_start = edit.new.start.0;
 461                                                let edit_end = edit_start
 462                                                    + (edit.old.end.0 - edit.old.start.0);
 463                                                let new_text = snapshot
 464                                                    .buffer_snapshot
 465                                                    .text_for_range(
 466                                                        edit.new.start.1..edit.new.end.1,
 467                                                    )
 468                                                    .collect();
 469                                                lsp::TextDocumentContentChangeEvent {
 470                                                    range: Some(lsp::Range::new(
 471                                                        lsp::Position::new(
 472                                                            edit_start.row,
 473                                                            edit_start.column,
 474                                                        ),
 475                                                        lsp::Position::new(
 476                                                            edit_end.row,
 477                                                            edit_end.column,
 478                                                        ),
 479                                                    )),
 480                                                    range_length: None,
 481                                                    text: new_text,
 482                                                }
 483                                            })
 484                                            .collect(),
 485                                    };
 486                                    server
 487                                        .notify::<lsp::notification::DidChangeTextDocument>(changes)
 488                                        .await?;
 489                                } else {
 490                                    server
 491                                        .notify::<lsp::notification::DidOpenTextDocument>(
 492                                            lsp::DidOpenTextDocumentParams {
 493                                                text_document: lsp::TextDocumentItem::new(
 494                                                    uri,
 495                                                    Default::default(),
 496                                                    snapshot.version as i32,
 497                                                    snapshot.buffer_snapshot.text().to_string(),
 498                                                ),
 499                                            },
 500                                        )
 501                                        .await?;
 502                                }
 503
 504                                prev_snapshot = Some(snapshot);
 505                            }
 506                        }
 507                        Ok(())
 508                    }
 509                    .log_err(),
 510                ),
 511            })
 512        } else {
 513            None
 514        };
 515
 516        self.reparse(cx);
 517        self.update_language_server();
 518    }
 519
 520    pub fn did_save(
 521        &mut self,
 522        version: clock::Global,
 523        mtime: SystemTime,
 524        new_file: Option<Box<dyn File>>,
 525        cx: &mut ModelContext<Self>,
 526    ) {
 527        self.saved_mtime = mtime;
 528        self.saved_version = version;
 529        if let Some(new_file) = new_file {
 530            self.file = Some(new_file);
 531        }
 532        if let Some(state) = &self.language_server {
 533            cx.background()
 534                .spawn(
 535                    state
 536                        .server
 537                        .notify::<lsp::notification::DidSaveTextDocument>(
 538                            lsp::DidSaveTextDocumentParams {
 539                                text_document: lsp::TextDocumentIdentifier {
 540                                    uri: lsp::Url::from_file_path(
 541                                        self.file.as_ref().unwrap().abs_path().unwrap(),
 542                                    )
 543                                    .unwrap(),
 544                                },
 545                                text: None,
 546                            },
 547                        ),
 548                )
 549                .detach()
 550        }
 551        cx.emit(Event::Saved);
 552    }
 553
 554    pub fn file_updated(
 555        &mut self,
 556        new_file: Box<dyn File>,
 557        cx: &mut ModelContext<Self>,
 558    ) -> Option<Task<()>> {
 559        let old_file = self.file.as_ref()?;
 560        let mut file_changed = false;
 561        let mut task = None;
 562
 563        if new_file.path() != old_file.path() {
 564            file_changed = true;
 565        }
 566
 567        if new_file.is_deleted() {
 568            if !old_file.is_deleted() {
 569                file_changed = true;
 570                if !self.is_dirty() {
 571                    cx.emit(Event::Dirtied);
 572                }
 573            }
 574        } else {
 575            let new_mtime = new_file.mtime();
 576            if new_mtime != old_file.mtime() {
 577                file_changed = true;
 578
 579                if !self.is_dirty() {
 580                    task = Some(cx.spawn(|this, mut cx| {
 581                        async move {
 582                            let new_text = this.read_with(&cx, |this, cx| {
 583                                this.file.as_ref().and_then(|file| file.load_local(cx))
 584                            });
 585                            if let Some(new_text) = new_text {
 586                                let new_text = new_text.await?;
 587                                let diff = this
 588                                    .read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
 589                                    .await;
 590                                this.update(&mut cx, |this, cx| {
 591                                    if this.apply_diff(diff, cx) {
 592                                        this.saved_version = this.version();
 593                                        this.saved_mtime = new_mtime;
 594                                        cx.emit(Event::Reloaded);
 595                                    }
 596                                });
 597                            }
 598                            Ok(())
 599                        }
 600                        .log_err()
 601                        .map(drop)
 602                    }));
 603                }
 604            }
 605        }
 606
 607        if file_changed {
 608            cx.emit(Event::FileHandleChanged);
 609        }
 610        self.file = Some(new_file);
 611        task
 612    }
 613
 614    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 615        cx.emit(Event::Closed);
 616    }
 617
 618    pub fn language(&self) -> Option<&Arc<Language>> {
 619        self.language.as_ref()
 620    }
 621
 622    pub fn parse_count(&self) -> usize {
 623        self.parse_count
 624    }
 625
 626    pub fn diagnostics_update_count(&self) -> usize {
 627        self.diagnostics_update_count
 628    }
 629
 630    pub(crate) fn syntax_tree(&self) -> Option<Tree> {
 631        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
 632            self.interpolate_tree(syntax_tree);
 633            Some(syntax_tree.tree.clone())
 634        } else {
 635            None
 636        }
 637    }
 638
 639    #[cfg(any(test, feature = "test-support"))]
 640    pub fn is_parsing(&self) -> bool {
 641        self.parsing_in_background
 642    }
 643
 644    #[cfg(test)]
 645    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 646        self.sync_parse_timeout = timeout;
 647    }
 648
 649    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
 650        if self.parsing_in_background {
 651            return false;
 652        }
 653
 654        if let Some(grammar) = self.grammar().cloned() {
 655            let old_tree = self.syntax_tree();
 656            let text = self.as_rope().clone();
 657            let parsed_version = self.version();
 658            let parse_task = cx.background().spawn({
 659                let grammar = grammar.clone();
 660                async move { Self::parse_text(&text, old_tree, &grammar) }
 661            });
 662
 663            match cx
 664                .background()
 665                .block_with_timeout(self.sync_parse_timeout, parse_task)
 666            {
 667                Ok(new_tree) => {
 668                    self.did_finish_parsing(new_tree, parsed_version, cx);
 669                    return true;
 670                }
 671                Err(parse_task) => {
 672                    self.parsing_in_background = true;
 673                    cx.spawn(move |this, mut cx| async move {
 674                        let new_tree = parse_task.await;
 675                        this.update(&mut cx, move |this, cx| {
 676                            let grammar_changed = this
 677                                .grammar()
 678                                .map_or(true, |curr_grammar| !Arc::ptr_eq(&grammar, curr_grammar));
 679                            let parse_again = this.version.gt(&parsed_version) || grammar_changed;
 680                            this.parsing_in_background = false;
 681                            this.did_finish_parsing(new_tree, parsed_version, cx);
 682
 683                            if parse_again && this.reparse(cx) {
 684                                return;
 685                            }
 686                        });
 687                    })
 688                    .detach();
 689                }
 690            }
 691        }
 692        false
 693    }
 694
 695    fn parse_text(text: &Rope, old_tree: Option<Tree>, grammar: &Grammar) -> Tree {
 696        PARSER.with(|parser| {
 697            let mut parser = parser.borrow_mut();
 698            parser
 699                .set_language(grammar.ts_language)
 700                .expect("incompatible grammar");
 701            let mut chunks = text.chunks_in_range(0..text.len());
 702            let tree = parser
 703                .parse_with(
 704                    &mut move |offset, _| {
 705                        chunks.seek(offset);
 706                        chunks.next().unwrap_or("").as_bytes()
 707                    },
 708                    old_tree.as_ref(),
 709                )
 710                .unwrap();
 711            tree
 712        })
 713    }
 714
 715    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
 716        for edit in self.edits_since::<(usize, Point)>(&tree.version) {
 717            let (bytes, lines) = edit.flatten();
 718            tree.tree.edit(&InputEdit {
 719                start_byte: bytes.new.start,
 720                old_end_byte: bytes.new.start + bytes.old.len(),
 721                new_end_byte: bytes.new.end,
 722                start_position: lines.new.start.to_ts_point(),
 723                old_end_position: (lines.new.start + (lines.old.end - lines.old.start))
 724                    .to_ts_point(),
 725                new_end_position: lines.new.end.to_ts_point(),
 726            });
 727        }
 728        tree.version = self.version();
 729    }
 730
 731    fn did_finish_parsing(
 732        &mut self,
 733        tree: Tree,
 734        version: clock::Global,
 735        cx: &mut ModelContext<Self>,
 736    ) {
 737        self.parse_count += 1;
 738        *self.syntax_tree.lock() = Some(SyntaxTree { tree, version });
 739        self.request_autoindent(cx);
 740        cx.emit(Event::Reparsed);
 741        cx.notify();
 742    }
 743
 744    pub fn update_diagnostics<T>(
 745        &mut self,
 746        provider_name: Arc<str>,
 747        version: Option<i32>,
 748        mut diagnostics: Vec<DiagnosticEntry<T>>,
 749        cx: &mut ModelContext<Self>,
 750    ) -> Result<Operation>
 751    where
 752        T: Copy + Ord + TextDimension + Sub<Output = T> + Clip + ToPoint,
 753    {
 754        fn compare_diagnostics(a: &Diagnostic, b: &Diagnostic) -> Ordering {
 755            Ordering::Equal
 756                .then_with(|| b.is_primary.cmp(&a.is_primary))
 757                .then_with(|| a.is_disk_based.cmp(&b.is_disk_based))
 758                .then_with(|| a.severity.cmp(&b.severity))
 759                .then_with(|| a.message.cmp(&b.message))
 760        }
 761
 762        let version = version.map(|version| version as usize);
 763        let content = if let Some(version) = version {
 764            let language_server = self.language_server.as_mut().unwrap();
 765            language_server
 766                .pending_snapshots
 767                .retain(|&v, _| v >= version);
 768            let snapshot = language_server
 769                .pending_snapshots
 770                .get(&version)
 771                .ok_or_else(|| anyhow!("missing snapshot"))?;
 772            &snapshot.buffer_snapshot
 773        } else {
 774            self.deref()
 775        };
 776
 777        diagnostics.sort_unstable_by(|a, b| {
 778            Ordering::Equal
 779                .then_with(|| a.range.start.cmp(&b.range.start))
 780                .then_with(|| b.range.end.cmp(&a.range.end))
 781                .then_with(|| compare_diagnostics(&a.diagnostic, &b.diagnostic))
 782        });
 783
 784        let mut sanitized_diagnostics = Vec::new();
 785        let mut edits_since_save = content.edits_since::<T>(&self.saved_version).peekable();
 786        let mut last_edit_old_end = T::default();
 787        let mut last_edit_new_end = T::default();
 788        'outer: for entry in diagnostics {
 789            let mut start = entry.range.start;
 790            let mut end = entry.range.end;
 791
 792            // Some diagnostics are based on files on disk instead of buffers'
 793            // current contents. Adjust these diagnostics' ranges to reflect
 794            // any unsaved edits.
 795            if entry.diagnostic.is_disk_based {
 796                while let Some(edit) = edits_since_save.peek() {
 797                    if edit.old.end <= start {
 798                        last_edit_old_end = edit.old.end;
 799                        last_edit_new_end = edit.new.end;
 800                        edits_since_save.next();
 801                    } else if edit.old.start <= end && edit.old.end >= start {
 802                        continue 'outer;
 803                    } else {
 804                        break;
 805                    }
 806                }
 807
 808                let start_overshoot = start - last_edit_old_end;
 809                start = last_edit_new_end;
 810                start.add_assign(&start_overshoot);
 811
 812                let end_overshoot = end - last_edit_old_end;
 813                end = last_edit_new_end;
 814                end.add_assign(&end_overshoot);
 815            }
 816
 817            let range = start.clip(Bias::Left, content)..end.clip(Bias::Right, content);
 818            let mut range = range.start.to_point(content)..range.end.to_point(content);
 819            // Expand empty ranges by one character
 820            if range.start == range.end {
 821                range.end.column += 1;
 822                range.end = content.clip_point(range.end, Bias::Right);
 823                if range.start == range.end && range.end.column > 0 {
 824                    range.start.column -= 1;
 825                    range.start = content.clip_point(range.start, Bias::Left);
 826                }
 827            }
 828
 829            sanitized_diagnostics.push(DiagnosticEntry {
 830                range,
 831                diagnostic: entry.diagnostic,
 832            });
 833        }
 834        drop(edits_since_save);
 835
 836        let set = DiagnosticSet::new(provider_name, sanitized_diagnostics, content);
 837        self.apply_diagnostic_update(set.clone(), cx);
 838        Ok(Operation::UpdateDiagnostics {
 839            provider_name: set.provider_name().to_string(),
 840            diagnostics: set.iter().cloned().collect(),
 841            lamport_timestamp: self.text.lamport_clock.tick(),
 842        })
 843    }
 844
 845    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 846        if let Some(indent_columns) = self.compute_autoindents() {
 847            let indent_columns = cx.background().spawn(indent_columns);
 848            match cx
 849                .background()
 850                .block_with_timeout(Duration::from_micros(500), indent_columns)
 851            {
 852                Ok(indent_columns) => self.apply_autoindents(indent_columns, cx),
 853                Err(indent_columns) => {
 854                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 855                        let indent_columns = indent_columns.await;
 856                        this.update(&mut cx, |this, cx| {
 857                            this.apply_autoindents(indent_columns, cx);
 858                        });
 859                    }));
 860                }
 861            }
 862        }
 863    }
 864
 865    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, u32>>> {
 866        let max_rows_between_yields = 100;
 867        let snapshot = self.snapshot();
 868        if snapshot.language.is_none()
 869            || snapshot.tree.is_none()
 870            || self.autoindent_requests.is_empty()
 871        {
 872            return None;
 873        }
 874
 875        let autoindent_requests = self.autoindent_requests.clone();
 876        Some(async move {
 877            let mut indent_columns = BTreeMap::new();
 878            for request in autoindent_requests {
 879                let old_to_new_rows = request
 880                    .edited
 881                    .iter()
 882                    .map(|anchor| anchor.summary::<Point>(&request.before_edit).row)
 883                    .zip(
 884                        request
 885                            .edited
 886                            .iter()
 887                            .map(|anchor| anchor.summary::<Point>(&snapshot).row),
 888                    )
 889                    .collect::<BTreeMap<u32, u32>>();
 890
 891                let mut old_suggestions = HashMap::<u32, u32>::default();
 892                let old_edited_ranges =
 893                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 894                for old_edited_range in old_edited_ranges {
 895                    let suggestions = request
 896                        .before_edit
 897                        .suggest_autoindents(old_edited_range.clone())
 898                        .into_iter()
 899                        .flatten();
 900                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 901                        let indentation_basis = old_to_new_rows
 902                            .get(&suggestion.basis_row)
 903                            .and_then(|from_row| old_suggestions.get(from_row).copied())
 904                            .unwrap_or_else(|| {
 905                                request
 906                                    .before_edit
 907                                    .indent_column_for_line(suggestion.basis_row)
 908                            });
 909                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 910                        old_suggestions.insert(
 911                            *old_to_new_rows.get(&old_row).unwrap(),
 912                            indentation_basis + delta,
 913                        );
 914                    }
 915                    yield_now().await;
 916                }
 917
 918                // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
 919                // buffer before the edit, but keyed by the row for these lines after the edits were applied.
 920                let new_edited_row_ranges =
 921                    contiguous_ranges(old_to_new_rows.values().copied(), max_rows_between_yields);
 922                for new_edited_row_range in new_edited_row_ranges {
 923                    let suggestions = snapshot
 924                        .suggest_autoindents(new_edited_row_range.clone())
 925                        .into_iter()
 926                        .flatten();
 927                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 928                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 929                        let new_indentation = indent_columns
 930                            .get(&suggestion.basis_row)
 931                            .copied()
 932                            .unwrap_or_else(|| {
 933                                snapshot.indent_column_for_line(suggestion.basis_row)
 934                            })
 935                            + delta;
 936                        if old_suggestions
 937                            .get(&new_row)
 938                            .map_or(true, |old_indentation| new_indentation != *old_indentation)
 939                        {
 940                            indent_columns.insert(new_row, new_indentation);
 941                        }
 942                    }
 943                    yield_now().await;
 944                }
 945
 946                if let Some(inserted) = request.inserted.as_ref() {
 947                    let inserted_row_ranges = contiguous_ranges(
 948                        inserted
 949                            .iter()
 950                            .map(|range| range.to_point(&snapshot))
 951                            .flat_map(|range| range.start.row..range.end.row + 1),
 952                        max_rows_between_yields,
 953                    );
 954                    for inserted_row_range in inserted_row_ranges {
 955                        let suggestions = snapshot
 956                            .suggest_autoindents(inserted_row_range.clone())
 957                            .into_iter()
 958                            .flatten();
 959                        for (row, suggestion) in inserted_row_range.zip(suggestions) {
 960                            let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 961                            let new_indentation = indent_columns
 962                                .get(&suggestion.basis_row)
 963                                .copied()
 964                                .unwrap_or_else(|| {
 965                                    snapshot.indent_column_for_line(suggestion.basis_row)
 966                                })
 967                                + delta;
 968                            indent_columns.insert(row, new_indentation);
 969                        }
 970                        yield_now().await;
 971                    }
 972                }
 973            }
 974            indent_columns
 975        })
 976    }
 977
 978    fn apply_autoindents(
 979        &mut self,
 980        indent_columns: BTreeMap<u32, u32>,
 981        cx: &mut ModelContext<Self>,
 982    ) {
 983        self.start_transaction();
 984        for (row, indent_column) in &indent_columns {
 985            self.set_indent_column_for_line(*row, *indent_column, cx);
 986        }
 987        self.end_transaction(cx);
 988    }
 989
 990    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
 991        let current_column = self.indent_column_for_line(row);
 992        if column > current_column {
 993            let offset = Point::new(row, 0).to_offset(&*self);
 994            self.edit(
 995                [offset..offset],
 996                " ".repeat((column - current_column) as usize),
 997                cx,
 998            );
 999        } else if column < current_column {
1000            self.edit(
1001                [Point::new(row, 0)..Point::new(row, current_column - column)],
1002                "",
1003                cx,
1004            );
1005        }
1006    }
1007
1008    pub(crate) fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
1009        // TODO: it would be nice to not allocate here.
1010        let old_text = self.text();
1011        let base_version = self.version();
1012        cx.background().spawn(async move {
1013            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
1014                .iter_all_changes()
1015                .map(|c| (c.tag(), c.value().len()))
1016                .collect::<Vec<_>>();
1017            Diff {
1018                base_version,
1019                new_text,
1020                changes,
1021            }
1022        })
1023    }
1024
1025    pub(crate) fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
1026        if self.version == diff.base_version {
1027            self.start_transaction();
1028            let mut offset = 0;
1029            for (tag, len) in diff.changes {
1030                let range = offset..(offset + len);
1031                match tag {
1032                    ChangeTag::Equal => offset += len,
1033                    ChangeTag::Delete => self.edit(Some(range), "", cx),
1034                    ChangeTag::Insert => {
1035                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
1036                        offset += len;
1037                    }
1038                }
1039            }
1040            self.end_transaction(cx);
1041            true
1042        } else {
1043            false
1044        }
1045    }
1046
1047    pub fn is_dirty(&self) -> bool {
1048        !self.saved_version.ge(&self.version)
1049            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1050    }
1051
1052    pub fn has_conflict(&self) -> bool {
1053        !self.saved_version.ge(&self.version)
1054            && self
1055                .file
1056                .as_ref()
1057                .map_or(false, |file| file.mtime() > self.saved_mtime)
1058    }
1059
1060    pub fn subscribe(&mut self) -> Subscription {
1061        self.text.subscribe()
1062    }
1063
1064    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1065        self.start_transaction_at(Instant::now())
1066    }
1067
1068    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1069        self.text.start_transaction_at(now)
1070    }
1071
1072    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1073        self.end_transaction_at(Instant::now(), cx)
1074    }
1075
1076    pub fn end_transaction_at(
1077        &mut self,
1078        now: Instant,
1079        cx: &mut ModelContext<Self>,
1080    ) -> Option<TransactionId> {
1081        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1082            let was_dirty = start_version != self.saved_version;
1083            self.did_edit(&start_version, was_dirty, cx);
1084            Some(transaction_id)
1085        } else {
1086            None
1087        }
1088    }
1089
1090    pub fn set_active_selections(
1091        &mut self,
1092        selections: Arc<[Selection<Anchor>]>,
1093        cx: &mut ModelContext<Self>,
1094    ) {
1095        let lamport_timestamp = self.text.lamport_clock.tick();
1096        self.remote_selections
1097            .insert(self.text.replica_id(), selections.clone());
1098        self.send_operation(
1099            Operation::UpdateSelections {
1100                replica_id: self.text.replica_id(),
1101                selections,
1102                lamport_timestamp,
1103            },
1104            cx,
1105        );
1106    }
1107
1108    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1109        let lamport_timestamp = self.text.lamport_clock.tick();
1110        self.send_operation(
1111            Operation::RemoveSelections {
1112                replica_id: self.text.replica_id(),
1113                lamport_timestamp,
1114            },
1115            cx,
1116        );
1117    }
1118
1119    fn update_language_server(&mut self) {
1120        let language_server = if let Some(language_server) = self.language_server.as_mut() {
1121            language_server
1122        } else {
1123            return;
1124        };
1125        let abs_path = self
1126            .file
1127            .as_ref()
1128            .map_or(Path::new("/").to_path_buf(), |file| {
1129                file.abs_path().unwrap()
1130            });
1131
1132        let version = post_inc(&mut language_server.next_version);
1133        let snapshot = LanguageServerSnapshot {
1134            buffer_snapshot: self.text.snapshot(),
1135            version,
1136            path: Arc::from(abs_path),
1137        };
1138        language_server
1139            .pending_snapshots
1140            .insert(version, snapshot.clone());
1141        let _ = language_server
1142            .latest_snapshot
1143            .blocking_send(Some(snapshot));
1144    }
1145
1146    pub fn edit<I, S, T>(&mut self, ranges_iter: I, new_text: T, cx: &mut ModelContext<Self>)
1147    where
1148        I: IntoIterator<Item = Range<S>>,
1149        S: ToOffset,
1150        T: Into<String>,
1151    {
1152        self.edit_internal(ranges_iter, new_text, false, cx)
1153    }
1154
1155    pub fn edit_with_autoindent<I, S, T>(
1156        &mut self,
1157        ranges_iter: I,
1158        new_text: T,
1159        cx: &mut ModelContext<Self>,
1160    ) where
1161        I: IntoIterator<Item = Range<S>>,
1162        S: ToOffset,
1163        T: Into<String>,
1164    {
1165        self.edit_internal(ranges_iter, new_text, true, cx)
1166    }
1167
1168    pub fn edit_internal<I, S, T>(
1169        &mut self,
1170        ranges_iter: I,
1171        new_text: T,
1172        autoindent: bool,
1173        cx: &mut ModelContext<Self>,
1174    ) where
1175        I: IntoIterator<Item = Range<S>>,
1176        S: ToOffset,
1177        T: Into<String>,
1178    {
1179        let new_text = new_text.into();
1180
1181        // Skip invalid ranges and coalesce contiguous ones.
1182        let mut ranges: Vec<Range<usize>> = Vec::new();
1183        for range in ranges_iter {
1184            let range = range.start.to_offset(self)..range.end.to_offset(self);
1185            if !new_text.is_empty() || !range.is_empty() {
1186                if let Some(prev_range) = ranges.last_mut() {
1187                    if prev_range.end >= range.start {
1188                        prev_range.end = cmp::max(prev_range.end, range.end);
1189                    } else {
1190                        ranges.push(range);
1191                    }
1192                } else {
1193                    ranges.push(range);
1194                }
1195            }
1196        }
1197        if ranges.is_empty() {
1198            return;
1199        }
1200
1201        self.start_transaction();
1202        self.pending_autoindent.take();
1203        let autoindent_request = if autoindent && self.language.is_some() {
1204            let before_edit = self.snapshot();
1205            let edited = ranges
1206                .iter()
1207                .filter_map(|range| {
1208                    let start = range.start.to_point(self);
1209                    if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
1210                        None
1211                    } else {
1212                        Some(self.anchor_before(range.start))
1213                    }
1214                })
1215                .collect();
1216            Some((before_edit, edited))
1217        } else {
1218            None
1219        };
1220
1221        let first_newline_ix = new_text.find('\n');
1222        let new_text_len = new_text.len();
1223
1224        let edit = self.text.edit(ranges.iter().cloned(), new_text);
1225
1226        if let Some((before_edit, edited)) = autoindent_request {
1227            let mut inserted = None;
1228            if let Some(first_newline_ix) = first_newline_ix {
1229                let mut delta = 0isize;
1230                inserted = Some(
1231                    ranges
1232                        .iter()
1233                        .map(|range| {
1234                            let start =
1235                                (delta + range.start as isize) as usize + first_newline_ix + 1;
1236                            let end = (delta + range.start as isize) as usize + new_text_len;
1237                            delta +=
1238                                (range.end as isize - range.start as isize) + new_text_len as isize;
1239                            self.anchor_before(start)..self.anchor_after(end)
1240                        })
1241                        .collect(),
1242                );
1243            }
1244
1245            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1246                before_edit,
1247                edited,
1248                inserted,
1249            }));
1250        }
1251
1252        self.end_transaction(cx);
1253        self.send_operation(Operation::Buffer(text::Operation::Edit(edit)), cx);
1254    }
1255
1256    fn did_edit(
1257        &mut self,
1258        old_version: &clock::Global,
1259        was_dirty: bool,
1260        cx: &mut ModelContext<Self>,
1261    ) {
1262        if self.edits_since::<usize>(old_version).next().is_none() {
1263            return;
1264        }
1265
1266        self.reparse(cx);
1267        self.update_language_server();
1268
1269        cx.emit(Event::Edited);
1270        if !was_dirty {
1271            cx.emit(Event::Dirtied);
1272        }
1273        cx.notify();
1274    }
1275
1276    fn grammar(&self) -> Option<&Arc<Grammar>> {
1277        self.language.as_ref().and_then(|l| l.grammar.as_ref())
1278    }
1279
1280    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1281        &mut self,
1282        ops: I,
1283        cx: &mut ModelContext<Self>,
1284    ) -> Result<()> {
1285        self.pending_autoindent.take();
1286        let was_dirty = self.is_dirty();
1287        let old_version = self.version.clone();
1288        let mut deferred_ops = Vec::new();
1289        let buffer_ops = ops
1290            .into_iter()
1291            .filter_map(|op| match op {
1292                Operation::Buffer(op) => Some(op),
1293                _ => {
1294                    if self.can_apply_op(&op) {
1295                        self.apply_op(op, cx);
1296                    } else {
1297                        deferred_ops.push(op);
1298                    }
1299                    None
1300                }
1301            })
1302            .collect::<Vec<_>>();
1303        self.text.apply_ops(buffer_ops)?;
1304        self.flush_deferred_ops(cx);
1305        self.did_edit(&old_version, was_dirty, cx);
1306        // Notify independently of whether the buffer was edited as the operations could include a
1307        // selection update.
1308        cx.notify();
1309        Ok(())
1310    }
1311
1312    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1313        let mut deferred_ops = Vec::new();
1314        for op in self.deferred_ops.drain().iter().cloned() {
1315            if self.can_apply_op(&op) {
1316                self.apply_op(op, cx);
1317            } else {
1318                deferred_ops.push(op);
1319            }
1320        }
1321        self.deferred_ops.insert(deferred_ops);
1322    }
1323
1324    fn can_apply_op(&self, operation: &Operation) -> bool {
1325        match operation {
1326            Operation::Buffer(_) => {
1327                unreachable!("buffer operations should never be applied at this layer")
1328            }
1329            Operation::UpdateDiagnostics {
1330                diagnostics: diagnostic_set,
1331                ..
1332            } => diagnostic_set.iter().all(|diagnostic| {
1333                self.text.can_resolve(&diagnostic.range.start)
1334                    && self.text.can_resolve(&diagnostic.range.end)
1335            }),
1336            Operation::UpdateSelections { selections, .. } => selections
1337                .iter()
1338                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1339            Operation::RemoveSelections { .. } => true,
1340        }
1341    }
1342
1343    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1344        match operation {
1345            Operation::Buffer(_) => {
1346                unreachable!("buffer operations should never be applied at this layer")
1347            }
1348            Operation::UpdateDiagnostics {
1349                provider_name,
1350                diagnostics: diagnostic_set,
1351                ..
1352            } => {
1353                let snapshot = self.snapshot();
1354                self.apply_diagnostic_update(
1355                    DiagnosticSet::from_sorted_entries(
1356                        provider_name,
1357                        diagnostic_set.iter().cloned(),
1358                        &snapshot,
1359                    ),
1360                    cx,
1361                );
1362            }
1363            Operation::UpdateSelections {
1364                replica_id,
1365                selections,
1366                lamport_timestamp,
1367            } => {
1368                self.remote_selections.insert(replica_id, selections);
1369                self.text.lamport_clock.observe(lamport_timestamp);
1370            }
1371            Operation::RemoveSelections {
1372                replica_id,
1373                lamport_timestamp,
1374            } => {
1375                self.remote_selections.remove(&replica_id);
1376                self.text.lamport_clock.observe(lamport_timestamp);
1377            }
1378        }
1379    }
1380
1381    fn apply_diagnostic_update(&mut self, set: DiagnosticSet, cx: &mut ModelContext<Self>) {
1382        match self
1383            .diagnostic_sets
1384            .binary_search_by_key(&set.provider_name(), |set| set.provider_name())
1385        {
1386            Ok(ix) => self.diagnostic_sets[ix] = set.clone(),
1387            Err(ix) => self.diagnostic_sets.insert(ix, set.clone()),
1388        }
1389
1390        self.diagnostics_update_count += 1;
1391        cx.notify();
1392        cx.emit(Event::DiagnosticsUpdated);
1393    }
1394
1395    #[cfg(not(test))]
1396    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1397        if let Some(file) = &self.file {
1398            file.buffer_updated(self.remote_id(), operation, cx.as_mut());
1399        }
1400    }
1401
1402    #[cfg(test)]
1403    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
1404        self.operations.push(operation);
1405    }
1406
1407    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1408        self.remote_selections.remove(&replica_id);
1409        cx.notify();
1410    }
1411
1412    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1413        let was_dirty = self.is_dirty();
1414        let old_version = self.version.clone();
1415
1416        if let Some((transaction_id, operation)) = self.text.undo() {
1417            self.send_operation(Operation::Buffer(operation), cx);
1418            self.did_edit(&old_version, was_dirty, cx);
1419            Some(transaction_id)
1420        } else {
1421            None
1422        }
1423    }
1424
1425    pub fn undo_transaction(
1426        &mut self,
1427        transaction_id: TransactionId,
1428        cx: &mut ModelContext<Self>,
1429    ) -> bool {
1430        let was_dirty = self.is_dirty();
1431        let old_version = self.version.clone();
1432
1433        if let Some(operation) = self.text.undo_transaction(transaction_id) {
1434            self.send_operation(Operation::Buffer(operation), cx);
1435            self.did_edit(&old_version, was_dirty, cx);
1436            true
1437        } else {
1438            false
1439        }
1440    }
1441
1442    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1443        let was_dirty = self.is_dirty();
1444        let old_version = self.version.clone();
1445
1446        if let Some((transaction_id, operation)) = self.text.redo() {
1447            self.send_operation(Operation::Buffer(operation), cx);
1448            self.did_edit(&old_version, was_dirty, cx);
1449            Some(transaction_id)
1450        } else {
1451            None
1452        }
1453    }
1454
1455    pub fn redo_transaction(
1456        &mut self,
1457        transaction_id: TransactionId,
1458        cx: &mut ModelContext<Self>,
1459    ) -> bool {
1460        let was_dirty = self.is_dirty();
1461        let old_version = self.version.clone();
1462
1463        if let Some(operation) = self.text.redo_transaction(transaction_id) {
1464            self.send_operation(Operation::Buffer(operation), cx);
1465            self.did_edit(&old_version, was_dirty, cx);
1466            true
1467        } else {
1468            false
1469        }
1470    }
1471}
1472
1473#[cfg(any(test, feature = "test-support"))]
1474impl Buffer {
1475    pub fn randomly_edit<T>(
1476        &mut self,
1477        rng: &mut T,
1478        old_range_count: usize,
1479        cx: &mut ModelContext<Self>,
1480    ) where
1481        T: rand::Rng,
1482    {
1483        self.start_transaction();
1484        self.text.randomly_edit(rng, old_range_count);
1485        self.end_transaction(cx);
1486    }
1487}
1488
1489impl Entity for Buffer {
1490    type Event = Event;
1491
1492    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
1493        if let Some(file) = self.file.as_ref() {
1494            file.buffer_removed(self.remote_id(), cx);
1495        }
1496    }
1497}
1498
1499impl Deref for Buffer {
1500    type Target = TextBuffer;
1501
1502    fn deref(&self) -> &Self::Target {
1503        &self.text
1504    }
1505}
1506
1507impl BufferSnapshot {
1508    fn suggest_autoindents<'a>(
1509        &'a self,
1510        row_range: Range<u32>,
1511    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1512        let mut query_cursor = QueryCursorHandle::new();
1513        if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
1514            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1515
1516            // Get the "indentation ranges" that intersect this row range.
1517            let indent_capture_ix = grammar.indents_query.capture_index_for_name("indent");
1518            let end_capture_ix = grammar.indents_query.capture_index_for_name("end");
1519            query_cursor.set_point_range(
1520                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1521                    ..Point::new(row_range.end, 0).to_ts_point(),
1522            );
1523            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1524            for mat in query_cursor.matches(
1525                &grammar.indents_query,
1526                tree.root_node(),
1527                TextProvider(self.as_rope()),
1528            ) {
1529                let mut node_kind = "";
1530                let mut start: Option<Point> = None;
1531                let mut end: Option<Point> = None;
1532                for capture in mat.captures {
1533                    if Some(capture.index) == indent_capture_ix {
1534                        node_kind = capture.node.kind();
1535                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1536                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1537                    } else if Some(capture.index) == end_capture_ix {
1538                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
1539                    }
1540                }
1541
1542                if let Some((start, end)) = start.zip(end) {
1543                    if start.row == end.row {
1544                        continue;
1545                    }
1546
1547                    let range = start..end;
1548                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1549                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1550                        Ok(ix) => {
1551                            let prev_range = &mut indentation_ranges[ix];
1552                            prev_range.0.end = prev_range.0.end.max(range.end);
1553                        }
1554                    }
1555                }
1556            }
1557
1558            let mut prev_row = prev_non_blank_row.unwrap_or(0);
1559            Some(row_range.map(move |row| {
1560                let row_start = Point::new(row, self.indent_column_for_line(row));
1561
1562                let mut indent_from_prev_row = false;
1563                let mut outdent_to_row = u32::MAX;
1564                for (range, _node_kind) in &indentation_ranges {
1565                    if range.start.row >= row {
1566                        break;
1567                    }
1568
1569                    if range.start.row == prev_row && range.end > row_start {
1570                        indent_from_prev_row = true;
1571                    }
1572                    if range.end.row >= prev_row && range.end <= row_start {
1573                        outdent_to_row = outdent_to_row.min(range.start.row);
1574                    }
1575                }
1576
1577                let suggestion = if outdent_to_row == prev_row {
1578                    IndentSuggestion {
1579                        basis_row: prev_row,
1580                        indent: false,
1581                    }
1582                } else if indent_from_prev_row {
1583                    IndentSuggestion {
1584                        basis_row: prev_row,
1585                        indent: true,
1586                    }
1587                } else if outdent_to_row < prev_row {
1588                    IndentSuggestion {
1589                        basis_row: outdent_to_row,
1590                        indent: false,
1591                    }
1592                } else {
1593                    IndentSuggestion {
1594                        basis_row: prev_row,
1595                        indent: false,
1596                    }
1597                };
1598
1599                prev_row = row;
1600                suggestion
1601            }))
1602        } else {
1603            None
1604        }
1605    }
1606
1607    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1608        while row > 0 {
1609            row -= 1;
1610            if !self.is_line_blank(row) {
1611                return Some(row);
1612            }
1613        }
1614        None
1615    }
1616
1617    pub fn chunks<'a, T: ToOffset>(
1618        &'a self,
1619        range: Range<T>,
1620        theme: Option<&'a SyntaxTheme>,
1621    ) -> BufferChunks<'a> {
1622        let range = range.start.to_offset(self)..range.end.to_offset(self);
1623
1624        let mut highlights = None;
1625        let mut diagnostic_endpoints = Vec::<DiagnosticEndpoint>::new();
1626        if let Some(theme) = theme {
1627            for (_, entry) in self.diagnostics_in_range::<_, usize>(range.clone()) {
1628                diagnostic_endpoints.push(DiagnosticEndpoint {
1629                    offset: entry.range.start,
1630                    is_start: true,
1631                    severity: entry.diagnostic.severity,
1632                });
1633                diagnostic_endpoints.push(DiagnosticEndpoint {
1634                    offset: entry.range.end,
1635                    is_start: false,
1636                    severity: entry.diagnostic.severity,
1637                });
1638            }
1639            diagnostic_endpoints
1640                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1641
1642            if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
1643                let mut query_cursor = QueryCursorHandle::new();
1644
1645                // TODO - add a Tree-sitter API to remove the need for this.
1646                let cursor = unsafe {
1647                    std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
1648                };
1649                let captures = cursor.set_byte_range(range.clone()).captures(
1650                    &grammar.highlights_query,
1651                    tree.root_node(),
1652                    TextProvider(self.text.as_rope()),
1653                );
1654                highlights = Some(BufferChunkHighlights {
1655                    captures,
1656                    next_capture: None,
1657                    stack: Default::default(),
1658                    highlight_map: grammar.highlight_map(),
1659                    _query_cursor: query_cursor,
1660                    theme,
1661                })
1662            }
1663        }
1664
1665        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1666        let chunks = self.text.as_rope().chunks_in_range(range.clone());
1667
1668        BufferChunks {
1669            range,
1670            chunks,
1671            diagnostic_endpoints,
1672            error_depth: 0,
1673            warning_depth: 0,
1674            information_depth: 0,
1675            hint_depth: 0,
1676            highlights,
1677        }
1678    }
1679
1680    pub fn language(&self) -> Option<&Arc<Language>> {
1681        self.language.as_ref()
1682    }
1683
1684    fn grammar(&self) -> Option<&Arc<Grammar>> {
1685        self.language
1686            .as_ref()
1687            .and_then(|language| language.grammar.as_ref())
1688    }
1689
1690    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1691        if let Some(tree) = self.tree.as_ref() {
1692            let root = tree.root_node();
1693            let range = range.start.to_offset(self)..range.end.to_offset(self);
1694            let mut node = root.descendant_for_byte_range(range.start, range.end);
1695            while node.map_or(false, |n| n.byte_range() == range) {
1696                node = node.unwrap().parent();
1697            }
1698            node.map(|n| n.byte_range())
1699        } else {
1700            None
1701        }
1702    }
1703
1704    pub fn enclosing_bracket_ranges<T: ToOffset>(
1705        &self,
1706        range: Range<T>,
1707    ) -> Option<(Range<usize>, Range<usize>)> {
1708        let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1709        let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
1710        let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
1711
1712        // Find bracket pairs that *inclusively* contain the given range.
1713        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1714        let mut cursor = QueryCursorHandle::new();
1715        let matches = cursor.set_byte_range(range).matches(
1716            &grammar.brackets_query,
1717            tree.root_node(),
1718            TextProvider(self.as_rope()),
1719        );
1720
1721        // Get the ranges of the innermost pair of brackets.
1722        matches
1723            .filter_map(|mat| {
1724                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1725                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1726                Some((open.byte_range(), close.byte_range()))
1727            })
1728            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1729    }
1730
1731    pub fn remote_selections_in_range<'a>(
1732        &'a self,
1733        range: Range<Anchor>,
1734    ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
1735    {
1736        self.remote_selections
1737            .iter()
1738            .filter(|(replica_id, _)| **replica_id != self.text.replica_id())
1739            .map(move |(replica_id, selections)| {
1740                let start_ix = match selections
1741                    .binary_search_by(|probe| probe.end.cmp(&range.start, self).unwrap())
1742                {
1743                    Ok(ix) | Err(ix) => ix,
1744                };
1745                let end_ix = match selections
1746                    .binary_search_by(|probe| probe.start.cmp(&range.end, self).unwrap())
1747                {
1748                    Ok(ix) | Err(ix) => ix,
1749                };
1750
1751                (*replica_id, selections[start_ix..end_ix].iter())
1752            })
1753    }
1754
1755    pub fn diagnostics_in_range<'a, T, O>(
1756        &'a self,
1757        search_range: Range<T>,
1758    ) -> impl 'a + Iterator<Item = (&'a str, DiagnosticEntry<O>)>
1759    where
1760        T: 'a + Clone + ToOffset,
1761        O: 'a + FromAnchor,
1762    {
1763        self.diagnostic_sets.iter().flat_map(move |set| {
1764            set.range(search_range.clone(), self, true)
1765                .map(|e| (set.provider_name(), e))
1766        })
1767    }
1768
1769    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1770        let mut groups = Vec::new();
1771        for set in &self.diagnostic_sets {
1772            set.groups(&mut groups, self);
1773        }
1774        groups
1775    }
1776
1777    pub fn diagnostic_group<'a, O>(
1778        &'a self,
1779        provider_name: &str,
1780        group_id: usize,
1781    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1782    where
1783        O: 'a + FromAnchor,
1784    {
1785        self.diagnostic_sets
1786            .iter()
1787            .find(|s| s.provider_name() == provider_name)
1788            .into_iter()
1789            .flat_map(move |s| s.group(group_id, self))
1790    }
1791
1792    pub fn diagnostics_update_count(&self) -> usize {
1793        self.diagnostics_update_count
1794    }
1795
1796    pub fn parse_count(&self) -> usize {
1797        self.parse_count
1798    }
1799}
1800
1801impl Clone for BufferSnapshot {
1802    fn clone(&self) -> Self {
1803        Self {
1804            text: self.text.clone(),
1805            tree: self.tree.clone(),
1806            remote_selections: self.remote_selections.clone(),
1807            diagnostic_sets: self.diagnostic_sets.clone(),
1808            diagnostics_update_count: self.diagnostics_update_count,
1809            is_parsing: self.is_parsing,
1810            language: self.language.clone(),
1811            parse_count: self.parse_count,
1812        }
1813    }
1814}
1815
1816impl Deref for BufferSnapshot {
1817    type Target = text::BufferSnapshot;
1818
1819    fn deref(&self) -> &Self::Target {
1820        &self.text
1821    }
1822}
1823
1824impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1825    type I = ByteChunks<'a>;
1826
1827    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1828        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1829    }
1830}
1831
1832struct ByteChunks<'a>(rope::Chunks<'a>);
1833
1834impl<'a> Iterator for ByteChunks<'a> {
1835    type Item = &'a [u8];
1836
1837    fn next(&mut self) -> Option<Self::Item> {
1838        self.0.next().map(str::as_bytes)
1839    }
1840}
1841
1842unsafe impl<'a> Send for BufferChunks<'a> {}
1843
1844impl<'a> BufferChunks<'a> {
1845    pub fn seek(&mut self, offset: usize) {
1846        self.range.start = offset;
1847        self.chunks.seek(self.range.start);
1848        if let Some(highlights) = self.highlights.as_mut() {
1849            highlights
1850                .stack
1851                .retain(|(end_offset, _)| *end_offset > offset);
1852            if let Some((mat, capture_ix)) = &highlights.next_capture {
1853                let capture = mat.captures[*capture_ix as usize];
1854                if offset >= capture.node.start_byte() {
1855                    let next_capture_end = capture.node.end_byte();
1856                    if offset < next_capture_end {
1857                        highlights.stack.push((
1858                            next_capture_end,
1859                            highlights.highlight_map.get(capture.index),
1860                        ));
1861                    }
1862                    highlights.next_capture.take();
1863                }
1864            }
1865            highlights.captures.set_byte_range(self.range.clone());
1866        }
1867    }
1868
1869    pub fn offset(&self) -> usize {
1870        self.range.start
1871    }
1872
1873    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
1874        let depth = match endpoint.severity {
1875            DiagnosticSeverity::ERROR => &mut self.error_depth,
1876            DiagnosticSeverity::WARNING => &mut self.warning_depth,
1877            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
1878            DiagnosticSeverity::HINT => &mut self.hint_depth,
1879            _ => return,
1880        };
1881        if endpoint.is_start {
1882            *depth += 1;
1883        } else {
1884            *depth -= 1;
1885        }
1886    }
1887
1888    fn current_diagnostic_severity(&mut self) -> Option<DiagnosticSeverity> {
1889        if self.error_depth > 0 {
1890            Some(DiagnosticSeverity::ERROR)
1891        } else if self.warning_depth > 0 {
1892            Some(DiagnosticSeverity::WARNING)
1893        } else if self.information_depth > 0 {
1894            Some(DiagnosticSeverity::INFORMATION)
1895        } else if self.hint_depth > 0 {
1896            Some(DiagnosticSeverity::HINT)
1897        } else {
1898            None
1899        }
1900    }
1901}
1902
1903impl<'a> Iterator for BufferChunks<'a> {
1904    type Item = Chunk<'a>;
1905
1906    fn next(&mut self) -> Option<Self::Item> {
1907        let mut next_capture_start = usize::MAX;
1908        let mut next_diagnostic_endpoint = usize::MAX;
1909
1910        if let Some(highlights) = self.highlights.as_mut() {
1911            while let Some((parent_capture_end, _)) = highlights.stack.last() {
1912                if *parent_capture_end <= self.range.start {
1913                    highlights.stack.pop();
1914                } else {
1915                    break;
1916                }
1917            }
1918
1919            if highlights.next_capture.is_none() {
1920                highlights.next_capture = highlights.captures.next();
1921            }
1922
1923            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
1924                let capture = mat.captures[*capture_ix as usize];
1925                if self.range.start < capture.node.start_byte() {
1926                    next_capture_start = capture.node.start_byte();
1927                    break;
1928                } else {
1929                    let highlight_id = highlights.highlight_map.get(capture.index);
1930                    highlights
1931                        .stack
1932                        .push((capture.node.end_byte(), highlight_id));
1933                    highlights.next_capture = highlights.captures.next();
1934                }
1935            }
1936        }
1937
1938        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
1939            if endpoint.offset <= self.range.start {
1940                self.update_diagnostic_depths(endpoint);
1941                self.diagnostic_endpoints.next();
1942            } else {
1943                next_diagnostic_endpoint = endpoint.offset;
1944                break;
1945            }
1946        }
1947
1948        if let Some(chunk) = self.chunks.peek() {
1949            let chunk_start = self.range.start;
1950            let mut chunk_end = (self.chunks.offset() + chunk.len())
1951                .min(next_capture_start)
1952                .min(next_diagnostic_endpoint);
1953            let mut highlight_style = None;
1954            if let Some(highlights) = self.highlights.as_ref() {
1955                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
1956                    chunk_end = chunk_end.min(*parent_capture_end);
1957                    highlight_style = parent_highlight_id.style(highlights.theme);
1958                }
1959            }
1960
1961            let slice =
1962                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
1963            self.range.start = chunk_end;
1964            if self.range.start == self.chunks.offset() + chunk.len() {
1965                self.chunks.next().unwrap();
1966            }
1967
1968            Some(Chunk {
1969                text: slice,
1970                highlight_style,
1971                diagnostic: self.current_diagnostic_severity(),
1972            })
1973        } else {
1974            None
1975        }
1976    }
1977}
1978
1979impl QueryCursorHandle {
1980    fn new() -> Self {
1981        QueryCursorHandle(Some(
1982            QUERY_CURSORS
1983                .lock()
1984                .pop()
1985                .unwrap_or_else(|| QueryCursor::new()),
1986        ))
1987    }
1988}
1989
1990impl Deref for QueryCursorHandle {
1991    type Target = QueryCursor;
1992
1993    fn deref(&self) -> &Self::Target {
1994        self.0.as_ref().unwrap()
1995    }
1996}
1997
1998impl DerefMut for QueryCursorHandle {
1999    fn deref_mut(&mut self) -> &mut Self::Target {
2000        self.0.as_mut().unwrap()
2001    }
2002}
2003
2004impl Drop for QueryCursorHandle {
2005    fn drop(&mut self) {
2006        let mut cursor = self.0.take().unwrap();
2007        cursor.set_byte_range(0..usize::MAX);
2008        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2009        QUERY_CURSORS.lock().push(cursor)
2010    }
2011}
2012
2013trait ToTreeSitterPoint {
2014    fn to_ts_point(self) -> tree_sitter::Point;
2015    fn from_ts_point(point: tree_sitter::Point) -> Self;
2016}
2017
2018impl ToTreeSitterPoint for Point {
2019    fn to_ts_point(self) -> tree_sitter::Point {
2020        tree_sitter::Point::new(self.row as usize, self.column as usize)
2021    }
2022
2023    fn from_ts_point(point: tree_sitter::Point) -> Self {
2024        Point::new(point.row as u32, point.column as u32)
2025    }
2026}
2027
2028impl operation_queue::Operation for Operation {
2029    fn lamport_timestamp(&self) -> clock::Lamport {
2030        match self {
2031            Operation::Buffer(_) => {
2032                unreachable!("buffer operations should never be deferred at this layer")
2033            }
2034            Operation::UpdateDiagnostics {
2035                lamport_timestamp, ..
2036            }
2037            | Operation::UpdateSelections {
2038                lamport_timestamp, ..
2039            }
2040            | Operation::RemoveSelections {
2041                lamport_timestamp, ..
2042            } => *lamport_timestamp,
2043        }
2044    }
2045}
2046
2047impl Default for Diagnostic {
2048    fn default() -> Self {
2049        Self {
2050            code: Default::default(),
2051            severity: DiagnosticSeverity::ERROR,
2052            message: Default::default(),
2053            group_id: Default::default(),
2054            is_primary: Default::default(),
2055            is_valid: true,
2056            is_disk_based: false,
2057        }
2058    }
2059}
2060
2061pub fn contiguous_ranges(
2062    values: impl Iterator<Item = u32>,
2063    max_len: usize,
2064) -> impl Iterator<Item = Range<u32>> {
2065    let mut values = values.into_iter();
2066    let mut current_range: Option<Range<u32>> = None;
2067    std::iter::from_fn(move || loop {
2068        if let Some(value) = values.next() {
2069            if let Some(range) = &mut current_range {
2070                if value == range.end && range.len() < max_len {
2071                    range.end += 1;
2072                    continue;
2073                }
2074            }
2075
2076            let prev_range = current_range.clone();
2077            current_range = Some(value..(value + 1));
2078            if prev_range.is_some() {
2079                return prev_range;
2080            }
2081        } else {
2082            return current_range.take();
2083        }
2084    })
2085}