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