buffer.rs

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