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