buffer.rs

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