buffer.rs

   1pub use crate::{
   2    diagnostic_set::DiagnosticSet,
   3    highlight_map::{HighlightId, HighlightMap},
   4    proto, BracketPair, Grammar, Language, LanguageConfig, LanguageRegistry, PLAIN_TEXT,
   5};
   6use crate::{
   7    diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
   8    outline::OutlineItem,
   9    syntax_map::{
  10        SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxSnapshot, ToTreeSitterPoint,
  11    },
  12    CodeLabel, Outline,
  13};
  14use anyhow::{anyhow, Result};
  15use clock::ReplicaId;
  16use futures::FutureExt as _;
  17use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, MutableAppContext, Task};
  18use parking_lot::Mutex;
  19use settings::Settings;
  20use similar::{ChangeTag, TextDiff};
  21use smol::future::yield_now;
  22use std::{
  23    any::Any,
  24    cmp::{self, Ordering},
  25    collections::BTreeMap,
  26    ffi::OsStr,
  27    future::Future,
  28    iter::{self, Iterator, Peekable},
  29    mem,
  30    ops::{Deref, Range},
  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;
  39pub use text::{Buffer as TextBuffer, BufferSnapshot as TextBufferSnapshot, Operation as _, *};
  40use theme::SyntaxTheme;
  41use util::TryFutureExt as _;
  42
  43#[cfg(any(test, feature = "test-support"))]
  44pub use {tree_sitter_rust, tree_sitter_typescript};
  45
  46pub use lsp::DiagnosticSeverity;
  47
  48pub struct Buffer {
  49    text: TextBuffer,
  50    file: Option<Arc<dyn File>>,
  51    saved_version: clock::Global,
  52    saved_version_fingerprint: String,
  53    saved_mtime: SystemTime,
  54    transaction_depth: usize,
  55    was_dirty_before_starting_transaction: Option<bool>,
  56    language: Option<Arc<Language>>,
  57    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  58    pending_autoindent: Option<Task<()>>,
  59    sync_parse_timeout: Duration,
  60    syntax_map: Mutex<SyntaxMap>,
  61    parsing_in_background: bool,
  62    parse_count: usize,
  63    diagnostics: DiagnosticSet,
  64    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  65    selections_update_count: usize,
  66    diagnostics_update_count: usize,
  67    diagnostics_timestamp: clock::Lamport,
  68    file_update_count: usize,
  69    completion_triggers: Vec<String>,
  70    deferred_ops: OperationQueue<Operation>,
  71}
  72
  73pub struct BufferSnapshot {
  74    text: text::BufferSnapshot,
  75    pub(crate) syntax: SyntaxSnapshot,
  76    file: Option<Arc<dyn File>>,
  77    diagnostics: DiagnosticSet,
  78    diagnostics_update_count: usize,
  79    file_update_count: usize,
  80    remote_selections: TreeMap<ReplicaId, SelectionSet>,
  81    selections_update_count: usize,
  82    language: Option<Arc<Language>>,
  83    parse_count: usize,
  84}
  85
  86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  87pub struct IndentSize {
  88    pub len: u32,
  89    pub kind: IndentKind,
  90}
  91
  92#[derive(Clone, Copy, Debug, PartialEq, Eq)]
  93pub enum IndentKind {
  94    Space,
  95    Tab,
  96}
  97
  98#[derive(Clone, Debug)]
  99struct SelectionSet {
 100    line_mode: bool,
 101    selections: Arc<[Selection<Anchor>]>,
 102    lamport_timestamp: clock::Lamport,
 103}
 104
 105#[derive(Clone, Debug, PartialEq, Eq)]
 106pub struct GroupId {
 107    source: Arc<str>,
 108    id: usize,
 109}
 110
 111#[derive(Clone, Debug, PartialEq, Eq)]
 112pub struct Diagnostic {
 113    pub code: Option<String>,
 114    pub severity: DiagnosticSeverity,
 115    pub message: String,
 116    pub group_id: usize,
 117    pub is_valid: bool,
 118    pub is_primary: bool,
 119    pub is_disk_based: bool,
 120    pub is_unnecessary: bool,
 121}
 122
 123#[derive(Clone, Debug)]
 124pub struct Completion {
 125    pub old_range: Range<Anchor>,
 126    pub new_text: String,
 127    pub label: CodeLabel,
 128    pub lsp_completion: lsp::CompletionItem,
 129}
 130
 131#[derive(Clone, Debug)]
 132pub struct CodeAction {
 133    pub range: Range<Anchor>,
 134    pub lsp_action: lsp::CodeAction,
 135}
 136
 137#[derive(Clone, Debug, PartialEq, Eq)]
 138pub enum Operation {
 139    Buffer(text::Operation),
 140    UpdateDiagnostics {
 141        diagnostics: Arc<[DiagnosticEntry<Anchor>]>,
 142        lamport_timestamp: clock::Lamport,
 143    },
 144    UpdateSelections {
 145        selections: Arc<[Selection<Anchor>]>,
 146        lamport_timestamp: clock::Lamport,
 147        line_mode: bool,
 148    },
 149    UpdateCompletionTriggers {
 150        triggers: Vec<String>,
 151        lamport_timestamp: clock::Lamport,
 152    },
 153}
 154
 155#[derive(Clone, Debug, PartialEq, Eq)]
 156pub enum Event {
 157    Operation(Operation),
 158    Edited,
 159    DirtyChanged,
 160    Saved,
 161    FileHandleChanged,
 162    Reloaded,
 163    Reparsed,
 164    DiagnosticsUpdated,
 165    Closed,
 166}
 167
 168pub trait File: Send + Sync {
 169    fn as_local(&self) -> Option<&dyn LocalFile>;
 170
 171    fn is_local(&self) -> bool {
 172        self.as_local().is_some()
 173    }
 174
 175    fn mtime(&self) -> SystemTime;
 176
 177    /// Returns the path of this file relative to the worktree's root directory.
 178    fn path(&self) -> &Arc<Path>;
 179
 180    /// Returns the path of this file relative to the worktree's parent directory (this means it
 181    /// includes the name of the worktree's root folder).
 182    fn full_path(&self, cx: &AppContext) -> PathBuf;
 183
 184    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 185    /// of its worktree, then this method will return the name of the worktree itself.
 186    fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
 187
 188    fn is_deleted(&self) -> bool;
 189
 190    fn save(
 191        &self,
 192        buffer_id: u64,
 193        text: Rope,
 194        version: clock::Global,
 195        line_ending: LineEnding,
 196        cx: &mut MutableAppContext,
 197    ) -> Task<Result<(clock::Global, String, SystemTime)>>;
 198
 199    fn as_any(&self) -> &dyn Any;
 200
 201    fn to_proto(&self) -> rpc::proto::File;
 202}
 203
 204pub trait LocalFile: File {
 205    /// Returns the absolute path of this file.
 206    fn abs_path(&self, cx: &AppContext) -> PathBuf;
 207
 208    fn load(&self, cx: &AppContext) -> Task<Result<String>>;
 209
 210    fn buffer_reloaded(
 211        &self,
 212        buffer_id: u64,
 213        version: &clock::Global,
 214        fingerprint: String,
 215        line_ending: LineEnding,
 216        mtime: SystemTime,
 217        cx: &mut MutableAppContext,
 218    );
 219}
 220
 221#[derive(Clone, Debug)]
 222pub enum AutoindentMode {
 223    /// Indent each line of inserted text.
 224    EachLine,
 225    /// Apply the same indentation adjustment to all of the lines
 226    /// in a given insertion.
 227    Block {
 228        /// The original indentation level of the first line of each
 229        /// insertion, if it has been copied.
 230        original_indent_columns: Vec<u32>,
 231    },
 232}
 233
 234#[derive(Clone)]
 235struct AutoindentRequest {
 236    before_edit: BufferSnapshot,
 237    entries: Vec<AutoindentRequestEntry>,
 238    indent_size: IndentSize,
 239    is_block_mode: bool,
 240}
 241
 242#[derive(Clone)]
 243struct AutoindentRequestEntry {
 244    /// A range of the buffer whose indentation should be adjusted.
 245    range: Range<Anchor>,
 246    /// Whether or not these lines should be considered brand new, for the
 247    /// purpose of auto-indent. When text is not new, its indentation will
 248    /// only be adjusted if the suggested indentation level has *changed*
 249    /// since the edit was made.
 250    first_line_is_new: bool,
 251    original_indent_column: Option<u32>,
 252}
 253
 254#[derive(Debug)]
 255struct IndentSuggestion {
 256    basis_row: u32,
 257    delta: Ordering,
 258}
 259
 260struct BufferChunkHighlights<'a> {
 261    captures: SyntaxMapCaptures<'a>,
 262    next_capture: Option<SyntaxMapCapture<'a>>,
 263    stack: Vec<(usize, HighlightId)>,
 264    highlight_maps: Vec<HighlightMap>,
 265}
 266
 267pub struct BufferChunks<'a> {
 268    range: Range<usize>,
 269    chunks: rope::Chunks<'a>,
 270    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 271    error_depth: usize,
 272    warning_depth: usize,
 273    information_depth: usize,
 274    hint_depth: usize,
 275    unnecessary_depth: usize,
 276    highlights: Option<BufferChunkHighlights<'a>>,
 277}
 278
 279#[derive(Clone, Copy, Debug, Default)]
 280pub struct Chunk<'a> {
 281    pub text: &'a str,
 282    pub syntax_highlight_id: Option<HighlightId>,
 283    pub highlight_style: Option<HighlightStyle>,
 284    pub diagnostic_severity: Option<DiagnosticSeverity>,
 285    pub is_unnecessary: bool,
 286}
 287
 288pub struct Diff {
 289    base_version: clock::Global,
 290    new_text: Arc<str>,
 291    changes: Vec<(ChangeTag, usize)>,
 292    line_ending: LineEnding,
 293    start_offset: usize,
 294}
 295
 296#[derive(Clone, Copy)]
 297pub(crate) struct DiagnosticEndpoint {
 298    offset: usize,
 299    is_start: bool,
 300    severity: DiagnosticSeverity,
 301    is_unnecessary: bool,
 302}
 303
 304#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug)]
 305pub enum CharKind {
 306    Punctuation,
 307    Whitespace,
 308    Word,
 309}
 310
 311impl CharKind {
 312    pub fn coerce_punctuation(self, treat_punctuation_as_word: bool) -> Self {
 313        if treat_punctuation_as_word && self == CharKind::Punctuation {
 314            CharKind::Word
 315        } else {
 316            self
 317        }
 318    }
 319}
 320
 321impl Buffer {
 322    pub fn new<T: Into<String>>(
 323        replica_id: ReplicaId,
 324        base_text: T,
 325        cx: &mut ModelContext<Self>,
 326    ) -> Self {
 327        Self::build(
 328            TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
 329            None,
 330        )
 331    }
 332
 333    pub fn from_file<T: Into<String>>(
 334        replica_id: ReplicaId,
 335        base_text: T,
 336        file: Arc<dyn File>,
 337        cx: &mut ModelContext<Self>,
 338    ) -> Self {
 339        Self::build(
 340            TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
 341            Some(file),
 342        )
 343    }
 344
 345    pub fn from_proto(
 346        replica_id: ReplicaId,
 347        message: proto::Buffer,
 348        file: Option<Arc<dyn File>>,
 349        cx: &mut ModelContext<Self>,
 350    ) -> Result<Self> {
 351        let buffer = TextBuffer::new(replica_id, message.id, message.base_text);
 352        let mut this = Self::build(buffer, file);
 353        this.text.set_line_ending(proto::deserialize_line_ending(
 354            proto::LineEnding::from_i32(message.line_ending)
 355                .ok_or_else(|| anyhow!("missing line_ending"))?,
 356        ));
 357        let ops = message
 358            .operations
 359            .into_iter()
 360            .map(proto::deserialize_operation)
 361            .collect::<Result<Vec<_>>>()?;
 362        this.apply_ops(ops, cx)?;
 363
 364        for selection_set in message.selections {
 365            let lamport_timestamp = clock::Lamport {
 366                replica_id: selection_set.replica_id as ReplicaId,
 367                value: selection_set.lamport_timestamp,
 368            };
 369            this.remote_selections.insert(
 370                selection_set.replica_id as ReplicaId,
 371                SelectionSet {
 372                    line_mode: selection_set.line_mode,
 373                    selections: proto::deserialize_selections(selection_set.selections),
 374                    lamport_timestamp,
 375                },
 376            );
 377            this.text.lamport_clock.observe(lamport_timestamp);
 378        }
 379        let snapshot = this.snapshot();
 380        let entries = proto::deserialize_diagnostics(message.diagnostics);
 381        this.apply_diagnostic_update(
 382            DiagnosticSet::from_sorted_entries(entries.iter().cloned(), &snapshot),
 383            clock::Lamport {
 384                replica_id: 0,
 385                value: message.diagnostics_timestamp,
 386            },
 387            cx,
 388        );
 389
 390        this.completion_triggers = message.completion_triggers;
 391
 392        Ok(this)
 393    }
 394
 395    pub fn to_proto(&self) -> proto::Buffer {
 396        let mut operations = self
 397            .text
 398            .history()
 399            .map(|op| proto::serialize_operation(&Operation::Buffer(op.clone())))
 400            .chain(self.deferred_ops.iter().map(proto::serialize_operation))
 401            .collect::<Vec<_>>();
 402        operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
 403        proto::Buffer {
 404            id: self.remote_id(),
 405            file: self.file.as_ref().map(|f| f.to_proto()),
 406            base_text: self.base_text().to_string(),
 407            operations,
 408            selections: self
 409                .remote_selections
 410                .iter()
 411                .map(|(replica_id, set)| proto::SelectionSet {
 412                    replica_id: *replica_id as u32,
 413                    selections: proto::serialize_selections(&set.selections),
 414                    lamport_timestamp: set.lamport_timestamp.value,
 415                    line_mode: set.line_mode,
 416                })
 417                .collect(),
 418            diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
 419            diagnostics_timestamp: self.diagnostics_timestamp.value,
 420            completion_triggers: self.completion_triggers.clone(),
 421            line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
 422        }
 423    }
 424
 425    pub fn with_language(mut self, language: Arc<Language>, cx: &mut ModelContext<Self>) -> Self {
 426        self.set_language(Some(language), cx);
 427        self
 428    }
 429
 430    fn build(buffer: TextBuffer, file: Option<Arc<dyn File>>) -> Self {
 431        let saved_mtime = if let Some(file) = file.as_ref() {
 432            file.mtime()
 433        } else {
 434            UNIX_EPOCH
 435        };
 436
 437        Self {
 438            saved_mtime,
 439            saved_version: buffer.version(),
 440            saved_version_fingerprint: buffer.as_rope().fingerprint(),
 441            transaction_depth: 0,
 442            was_dirty_before_starting_transaction: None,
 443            text: buffer,
 444            file,
 445            syntax_map: Mutex::new(SyntaxMap::new()),
 446            parsing_in_background: false,
 447            parse_count: 0,
 448            sync_parse_timeout: Duration::from_millis(1),
 449            autoindent_requests: Default::default(),
 450            pending_autoindent: Default::default(),
 451            language: None,
 452            remote_selections: Default::default(),
 453            selections_update_count: 0,
 454            diagnostics: Default::default(),
 455            diagnostics_update_count: 0,
 456            diagnostics_timestamp: Default::default(),
 457            file_update_count: 0,
 458            completion_triggers: Default::default(),
 459            deferred_ops: OperationQueue::new(),
 460        }
 461    }
 462
 463    pub fn snapshot(&self) -> BufferSnapshot {
 464        let text = self.text.snapshot();
 465        let mut syntax_map = self.syntax_map.lock();
 466        syntax_map.interpolate(&text);
 467        let syntax = syntax_map.snapshot();
 468
 469        BufferSnapshot {
 470            text,
 471            syntax,
 472            file: self.file.clone(),
 473            remote_selections: self.remote_selections.clone(),
 474            diagnostics: self.diagnostics.clone(),
 475            diagnostics_update_count: self.diagnostics_update_count,
 476            file_update_count: self.file_update_count,
 477            language: self.language.clone(),
 478            parse_count: self.parse_count,
 479            selections_update_count: self.selections_update_count,
 480        }
 481    }
 482
 483    pub fn as_text_snapshot(&self) -> &text::BufferSnapshot {
 484        &self.text
 485    }
 486
 487    pub fn text_snapshot(&self) -> text::BufferSnapshot {
 488        self.text.snapshot()
 489    }
 490
 491    pub fn file(&self) -> Option<&dyn File> {
 492        self.file.as_deref()
 493    }
 494
 495    pub fn save(
 496        &mut self,
 497        cx: &mut ModelContext<Self>,
 498    ) -> Task<Result<(clock::Global, String, SystemTime)>> {
 499        let file = if let Some(file) = self.file.as_ref() {
 500            file
 501        } else {
 502            return Task::ready(Err(anyhow!("buffer has no file")));
 503        };
 504        let text = self.as_rope().clone();
 505        let version = self.version();
 506        let save = file.save(
 507            self.remote_id(),
 508            text,
 509            version,
 510            self.line_ending(),
 511            cx.as_mut(),
 512        );
 513        cx.spawn(|this, mut cx| async move {
 514            let (version, fingerprint, mtime) = save.await?;
 515            this.update(&mut cx, |this, cx| {
 516                this.did_save(version.clone(), fingerprint.clone(), mtime, None, cx);
 517            });
 518            Ok((version, fingerprint, mtime))
 519        })
 520    }
 521
 522    pub fn saved_version(&self) -> &clock::Global {
 523        &self.saved_version
 524    }
 525
 526    pub fn set_language(&mut self, language: Option<Arc<Language>>, cx: &mut ModelContext<Self>) {
 527        self.syntax_map.lock().clear();
 528        self.language = language;
 529        self.reparse(cx);
 530    }
 531
 532    pub fn set_language_registry(&mut self, language_registry: Arc<LanguageRegistry>) {
 533        self.syntax_map
 534            .lock()
 535            .set_language_registry(language_registry);
 536    }
 537
 538    pub fn did_save(
 539        &mut self,
 540        version: clock::Global,
 541        fingerprint: String,
 542        mtime: SystemTime,
 543        new_file: Option<Arc<dyn File>>,
 544        cx: &mut ModelContext<Self>,
 545    ) {
 546        self.saved_version = version;
 547        self.saved_version_fingerprint = fingerprint;
 548        self.saved_mtime = mtime;
 549        if let Some(new_file) = new_file {
 550            self.file = Some(new_file);
 551            self.file_update_count += 1;
 552        }
 553        cx.emit(Event::Saved);
 554        cx.notify();
 555    }
 556
 557    pub fn reload(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<Option<Transaction>>> {
 558        cx.spawn(|this, mut cx| async move {
 559            if let Some((new_mtime, new_text)) = this.read_with(&cx, |this, cx| {
 560                let file = this.file.as_ref()?.as_local()?;
 561                Some((file.mtime(), file.load(cx)))
 562            }) {
 563                let new_text = new_text.await?;
 564                let diff = this
 565                    .read_with(&cx, |this, cx| this.diff(new_text, cx))
 566                    .await;
 567                this.update(&mut cx, |this, cx| {
 568                    if let Some(transaction) = this.apply_diff(diff, cx).cloned() {
 569                        this.did_reload(
 570                            this.version(),
 571                            this.as_rope().fingerprint(),
 572                            this.line_ending(),
 573                            new_mtime,
 574                            cx,
 575                        );
 576                        Ok(Some(transaction))
 577                    } else {
 578                        Ok(None)
 579                    }
 580                })
 581            } else {
 582                Ok(None)
 583            }
 584        })
 585    }
 586
 587    pub fn did_reload(
 588        &mut self,
 589        version: clock::Global,
 590        fingerprint: String,
 591        line_ending: LineEnding,
 592        mtime: SystemTime,
 593        cx: &mut ModelContext<Self>,
 594    ) {
 595        self.saved_version = version;
 596        self.saved_version_fingerprint = fingerprint;
 597        self.text.set_line_ending(line_ending);
 598        self.saved_mtime = mtime;
 599        if let Some(file) = self.file.as_ref().and_then(|f| f.as_local()) {
 600            file.buffer_reloaded(
 601                self.remote_id(),
 602                &self.saved_version,
 603                self.saved_version_fingerprint.clone(),
 604                self.line_ending(),
 605                self.saved_mtime,
 606                cx,
 607            );
 608        }
 609        cx.emit(Event::Reloaded);
 610        cx.notify();
 611    }
 612
 613    pub fn file_updated(
 614        &mut self,
 615        new_file: Arc<dyn File>,
 616        cx: &mut ModelContext<Self>,
 617    ) -> Task<()> {
 618        let old_file = if let Some(file) = self.file.as_ref() {
 619            file
 620        } else {
 621            return Task::ready(());
 622        };
 623        let mut file_changed = false;
 624        let mut task = Task::ready(());
 625
 626        if new_file.path() != old_file.path() {
 627            file_changed = true;
 628        }
 629
 630        if new_file.is_deleted() {
 631            if !old_file.is_deleted() {
 632                file_changed = true;
 633                if !self.is_dirty() {
 634                    cx.emit(Event::DirtyChanged);
 635                }
 636            }
 637        } else {
 638            let new_mtime = new_file.mtime();
 639            if new_mtime != old_file.mtime() {
 640                file_changed = true;
 641
 642                if !self.is_dirty() {
 643                    let reload = self.reload(cx).log_err().map(drop);
 644                    task = cx.foreground().spawn(reload);
 645                }
 646            }
 647        }
 648
 649        if file_changed {
 650            self.file_update_count += 1;
 651            cx.emit(Event::FileHandleChanged);
 652            cx.notify();
 653        }
 654        self.file = Some(new_file);
 655        task
 656    }
 657
 658    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 659        cx.emit(Event::Closed);
 660    }
 661
 662    pub fn language(&self) -> Option<&Arc<Language>> {
 663        self.language.as_ref()
 664    }
 665
 666    pub fn parse_count(&self) -> usize {
 667        self.parse_count
 668    }
 669
 670    pub fn selections_update_count(&self) -> usize {
 671        self.selections_update_count
 672    }
 673
 674    pub fn diagnostics_update_count(&self) -> usize {
 675        self.diagnostics_update_count
 676    }
 677
 678    pub fn file_update_count(&self) -> usize {
 679        self.file_update_count
 680    }
 681
 682    #[cfg(any(test, feature = "test-support"))]
 683    pub fn is_parsing(&self) -> bool {
 684        self.parsing_in_background
 685    }
 686
 687    #[cfg(test)]
 688    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 689        self.sync_parse_timeout = timeout;
 690    }
 691
 692    fn reparse(&mut self, cx: &mut ModelContext<Self>) {
 693        if self.parsing_in_background {
 694            return;
 695        }
 696        let language = if let Some(language) = self.language.clone() {
 697            language
 698        } else {
 699            return;
 700        };
 701
 702        let text = self.text_snapshot();
 703        let parsed_version = self.version();
 704
 705        let mut syntax_map = self.syntax_map.lock();
 706        syntax_map.interpolate(&text);
 707        let language_registry = syntax_map.language_registry();
 708        let mut syntax_snapshot = syntax_map.snapshot();
 709        let syntax_map_version = syntax_map.parsed_version();
 710        drop(syntax_map);
 711
 712        let parse_task = cx.background().spawn({
 713            let language = language.clone();
 714            async move {
 715                syntax_snapshot.reparse(&syntax_map_version, &text, language_registry, language);
 716                syntax_snapshot
 717            }
 718        });
 719
 720        match cx
 721            .background()
 722            .block_with_timeout(self.sync_parse_timeout, parse_task)
 723        {
 724            Ok(new_syntax_snapshot) => {
 725                self.did_finish_parsing(new_syntax_snapshot, parsed_version, cx);
 726                return;
 727            }
 728            Err(parse_task) => {
 729                self.parsing_in_background = true;
 730                cx.spawn(move |this, mut cx| async move {
 731                    let new_syntax_map = parse_task.await;
 732                    this.update(&mut cx, move |this, cx| {
 733                        let grammar_changed =
 734                            this.language.as_ref().map_or(true, |current_language| {
 735                                !Arc::ptr_eq(&language, current_language)
 736                            });
 737                        let parse_again =
 738                            this.version.changed_since(&parsed_version) || grammar_changed;
 739                        this.did_finish_parsing(new_syntax_map, parsed_version, cx);
 740                        this.parsing_in_background = false;
 741                        if parse_again {
 742                            this.reparse(cx);
 743                        }
 744                    });
 745                })
 746                .detach();
 747            }
 748        }
 749    }
 750
 751    fn did_finish_parsing(
 752        &mut self,
 753        syntax_snapshot: SyntaxSnapshot,
 754        version: clock::Global,
 755        cx: &mut ModelContext<Self>,
 756    ) {
 757        self.parse_count += 1;
 758        self.syntax_map.lock().did_parse(syntax_snapshot, version);
 759        self.request_autoindent(cx);
 760        cx.emit(Event::Reparsed);
 761        cx.notify();
 762    }
 763
 764    pub fn update_diagnostics(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
 765        let lamport_timestamp = self.text.lamport_clock.tick();
 766        let op = Operation::UpdateDiagnostics {
 767            diagnostics: diagnostics.iter().cloned().collect(),
 768            lamport_timestamp,
 769        };
 770        self.apply_diagnostic_update(diagnostics, lamport_timestamp, cx);
 771        self.send_operation(op, cx);
 772    }
 773
 774    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 775        if let Some(indent_sizes) = self.compute_autoindents() {
 776            let indent_sizes = cx.background().spawn(indent_sizes);
 777            match cx
 778                .background()
 779                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 780            {
 781                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
 782                Err(indent_sizes) => {
 783                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 784                        let indent_sizes = indent_sizes.await;
 785                        this.update(&mut cx, |this, cx| {
 786                            this.apply_autoindents(indent_sizes, cx);
 787                        });
 788                    }));
 789                }
 790            }
 791        }
 792    }
 793
 794    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
 795        let max_rows_between_yields = 100;
 796        let snapshot = self.snapshot();
 797        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
 798            return None;
 799        }
 800
 801        let autoindent_requests = self.autoindent_requests.clone();
 802        Some(async move {
 803            let mut indent_sizes = BTreeMap::new();
 804            for request in autoindent_requests {
 805                // Resolve each edited range to its row in the current buffer and in the
 806                // buffer before this batch of edits.
 807                let mut row_ranges = Vec::new();
 808                let mut old_to_new_rows = BTreeMap::new();
 809                for entry in &request.entries {
 810                    let position = entry.range.start;
 811                    let new_row = position.to_point(&snapshot).row;
 812                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
 813                    if !entry.first_line_is_new {
 814                        let old_row = position.to_point(&request.before_edit).row;
 815                        old_to_new_rows.insert(old_row, new_row);
 816                    }
 817                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
 818                }
 819
 820                // Build a map containing the suggested indentation for each of the edited lines
 821                // with respect to the state of the buffer before these edits. This map is keyed
 822                // by the rows for these lines in the current state of the buffer.
 823                let mut old_suggestions = BTreeMap::<u32, IndentSize>::default();
 824                let old_edited_ranges =
 825                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 826                for old_edited_range in old_edited_ranges {
 827                    let suggestions = request
 828                        .before_edit
 829                        .suggest_autoindents(old_edited_range.clone())
 830                        .into_iter()
 831                        .flatten();
 832                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 833                        if let Some(suggestion) = suggestion {
 834                            let suggested_indent = old_to_new_rows
 835                                .get(&suggestion.basis_row)
 836                                .and_then(|from_row| old_suggestions.get(from_row).copied())
 837                                .unwrap_or_else(|| {
 838                                    request
 839                                        .before_edit
 840                                        .indent_size_for_line(suggestion.basis_row)
 841                                })
 842                                .with_delta(suggestion.delta, request.indent_size);
 843                            old_suggestions
 844                                .insert(*old_to_new_rows.get(&old_row).unwrap(), suggested_indent);
 845                        }
 846                    }
 847                    yield_now().await;
 848                }
 849
 850                // In block mode, only compute indentation suggestions for the first line
 851                // of each insertion. Otherwise, compute suggestions for every inserted line.
 852                let new_edited_row_ranges = contiguous_ranges(
 853                    row_ranges.iter().flat_map(|(range, _)| {
 854                        if request.is_block_mode {
 855                            range.start..range.start + 1
 856                        } else {
 857                            range.clone()
 858                        }
 859                    }),
 860                    max_rows_between_yields,
 861                );
 862
 863                // Compute new suggestions for each line, but only include them in the result
 864                // if they differ from the old suggestion for that line.
 865                for new_edited_row_range in new_edited_row_ranges {
 866                    let suggestions = snapshot
 867                        .suggest_autoindents(new_edited_row_range.clone())
 868                        .into_iter()
 869                        .flatten();
 870                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 871                        if let Some(suggestion) = suggestion {
 872                            let suggested_indent = indent_sizes
 873                                .get(&suggestion.basis_row)
 874                                .copied()
 875                                .unwrap_or_else(|| {
 876                                    snapshot.indent_size_for_line(suggestion.basis_row)
 877                                })
 878                                .with_delta(suggestion.delta, request.indent_size);
 879                            if old_suggestions
 880                                .get(&new_row)
 881                                .map_or(true, |old_indentation| {
 882                                    suggested_indent != *old_indentation
 883                                })
 884                            {
 885                                indent_sizes.insert(new_row, suggested_indent);
 886                            }
 887                        }
 888                    }
 889                    yield_now().await;
 890                }
 891
 892                // For each block of inserted text, adjust the indentation of the remaining
 893                // lines of the block by the same amount as the first line was adjusted.
 894                if request.is_block_mode {
 895                    for (row_range, original_indent_column) in
 896                        row_ranges
 897                            .into_iter()
 898                            .filter_map(|(range, original_indent_column)| {
 899                                if range.len() > 1 {
 900                                    Some((range, original_indent_column?))
 901                                } else {
 902                                    None
 903                                }
 904                            })
 905                    {
 906                        let new_indent = indent_sizes
 907                            .get(&row_range.start)
 908                            .copied()
 909                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
 910                        let delta = new_indent.len as i64 - original_indent_column as i64;
 911                        if delta != 0 {
 912                            for row in row_range.skip(1) {
 913                                indent_sizes.entry(row).or_insert_with(|| {
 914                                    let mut size = snapshot.indent_size_for_line(row);
 915                                    if size.kind == new_indent.kind {
 916                                        match delta.cmp(&0) {
 917                                            Ordering::Greater => size.len += delta as u32,
 918                                            Ordering::Less => {
 919                                                size.len = size.len.saturating_sub(-delta as u32)
 920                                            }
 921                                            Ordering::Equal => {}
 922                                        }
 923                                    }
 924                                    size
 925                                });
 926                            }
 927                        }
 928                    }
 929                }
 930            }
 931
 932            indent_sizes
 933        })
 934    }
 935
 936    fn apply_autoindents(
 937        &mut self,
 938        indent_sizes: BTreeMap<u32, IndentSize>,
 939        cx: &mut ModelContext<Self>,
 940    ) {
 941        self.autoindent_requests.clear();
 942
 943        let edits: Vec<_> = indent_sizes
 944            .into_iter()
 945            .filter_map(|(row, indent_size)| {
 946                let current_size = indent_size_for_line(self, row);
 947                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
 948            })
 949            .collect();
 950
 951        self.edit(edits, None, cx);
 952    }
 953
 954    pub fn edit_for_indent_size_adjustment(
 955        row: u32,
 956        current_size: IndentSize,
 957        new_size: IndentSize,
 958    ) -> Option<(Range<Point>, String)> {
 959        if new_size.kind != current_size.kind && current_size.len > 0 {
 960            return None;
 961        }
 962
 963        match new_size.len.cmp(&current_size.len) {
 964            Ordering::Greater => {
 965                let point = Point::new(row, 0);
 966                Some((
 967                    point..point,
 968                    iter::repeat(new_size.char())
 969                        .take((new_size.len - current_size.len) as usize)
 970                        .collect::<String>(),
 971                ))
 972            }
 973
 974            Ordering::Less => Some((
 975                Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
 976                String::new(),
 977            )),
 978
 979            Ordering::Equal => None,
 980        }
 981    }
 982
 983    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
 984        let old_text = self.as_rope().clone();
 985        let base_version = self.version();
 986        cx.background().spawn(async move {
 987            let old_text = old_text.to_string();
 988            let line_ending = LineEnding::detect(&new_text);
 989            LineEnding::normalize(&mut new_text);
 990            let changes = TextDiff::from_chars(old_text.as_str(), new_text.as_str())
 991                .iter_all_changes()
 992                .map(|c| (c.tag(), c.value().len()))
 993                .collect::<Vec<_>>();
 994            Diff {
 995                base_version,
 996                new_text: new_text.into(),
 997                changes,
 998                line_ending,
 999                start_offset: 0,
1000            }
1001        })
1002    }
1003
1004    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<&Transaction> {
1005        if self.version == diff.base_version {
1006            self.finalize_last_transaction();
1007            self.start_transaction();
1008            self.text.set_line_ending(diff.line_ending);
1009            let mut offset = diff.start_offset;
1010            for (tag, len) in diff.changes {
1011                let range = offset..(offset + len);
1012                match tag {
1013                    ChangeTag::Equal => offset += len,
1014                    ChangeTag::Delete => {
1015                        self.edit([(range, "")], None, cx);
1016                    }
1017                    ChangeTag::Insert => {
1018                        self.edit(
1019                            [(
1020                                offset..offset,
1021                                &diff.new_text[range.start - diff.start_offset
1022                                    ..range.end - diff.start_offset],
1023                            )],
1024                            None,
1025                            cx,
1026                        );
1027                        offset += len;
1028                    }
1029                }
1030            }
1031            if self.end_transaction(cx).is_some() {
1032                self.finalize_last_transaction()
1033            } else {
1034                None
1035            }
1036        } else {
1037            None
1038        }
1039    }
1040
1041    pub fn is_dirty(&self) -> bool {
1042        self.saved_version_fingerprint != self.as_rope().fingerprint()
1043            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1044    }
1045
1046    pub fn has_conflict(&self) -> bool {
1047        self.saved_version_fingerprint != self.as_rope().fingerprint()
1048            && self
1049                .file
1050                .as_ref()
1051                .map_or(false, |file| file.mtime() > self.saved_mtime)
1052    }
1053
1054    pub fn subscribe(&mut self) -> Subscription {
1055        self.text.subscribe()
1056    }
1057
1058    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1059        self.start_transaction_at(Instant::now())
1060    }
1061
1062    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1063        self.transaction_depth += 1;
1064        if self.was_dirty_before_starting_transaction.is_none() {
1065            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1066        }
1067        self.text.start_transaction_at(now)
1068    }
1069
1070    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1071        self.end_transaction_at(Instant::now(), cx)
1072    }
1073
1074    pub fn end_transaction_at(
1075        &mut self,
1076        now: Instant,
1077        cx: &mut ModelContext<Self>,
1078    ) -> Option<TransactionId> {
1079        assert!(self.transaction_depth > 0);
1080        self.transaction_depth -= 1;
1081        let was_dirty = if self.transaction_depth == 0 {
1082            self.was_dirty_before_starting_transaction.take().unwrap()
1083        } else {
1084            false
1085        };
1086        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1087            self.did_edit(&start_version, was_dirty, cx);
1088            Some(transaction_id)
1089        } else {
1090            None
1091        }
1092    }
1093
1094    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1095        self.text.push_transaction(transaction, now);
1096    }
1097
1098    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1099        self.text.finalize_last_transaction()
1100    }
1101
1102    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1103        self.text.group_until_transaction(transaction_id);
1104    }
1105
1106    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1107        self.text.forget_transaction(transaction_id);
1108    }
1109
1110    pub fn wait_for_edits(
1111        &mut self,
1112        edit_ids: impl IntoIterator<Item = clock::Local>,
1113    ) -> impl Future<Output = ()> {
1114        self.text.wait_for_edits(edit_ids)
1115    }
1116
1117    pub fn wait_for_anchors<'a>(
1118        &mut self,
1119        anchors: impl IntoIterator<Item = &'a Anchor>,
1120    ) -> impl Future<Output = ()> {
1121        self.text.wait_for_anchors(anchors)
1122    }
1123
1124    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1125        self.text.wait_for_version(version)
1126    }
1127
1128    pub fn set_active_selections(
1129        &mut self,
1130        selections: Arc<[Selection<Anchor>]>,
1131        line_mode: bool,
1132        cx: &mut ModelContext<Self>,
1133    ) {
1134        let lamport_timestamp = self.text.lamport_clock.tick();
1135        self.remote_selections.insert(
1136            self.text.replica_id(),
1137            SelectionSet {
1138                selections: selections.clone(),
1139                lamport_timestamp,
1140                line_mode,
1141            },
1142        );
1143        self.send_operation(
1144            Operation::UpdateSelections {
1145                selections,
1146                line_mode,
1147                lamport_timestamp,
1148            },
1149            cx,
1150        );
1151    }
1152
1153    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1154        self.set_active_selections(Arc::from([]), false, cx);
1155    }
1156
1157    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
1158    where
1159        T: Into<Arc<str>>,
1160    {
1161        self.edit([(0..self.len(), text)], None, cx)
1162    }
1163
1164    pub fn edit<I, S, T>(
1165        &mut self,
1166        edits_iter: I,
1167        autoindent_mode: Option<AutoindentMode>,
1168        cx: &mut ModelContext<Self>,
1169    ) -> Option<clock::Local>
1170    where
1171        I: IntoIterator<Item = (Range<S>, T)>,
1172        S: ToOffset,
1173        T: Into<Arc<str>>,
1174    {
1175        // Skip invalid edits and coalesce contiguous ones.
1176        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1177        for (range, new_text) in edits_iter {
1178            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1179            if range.start > range.end {
1180                mem::swap(&mut range.start, &mut range.end);
1181            }
1182            let new_text = new_text.into();
1183            if !new_text.is_empty() || !range.is_empty() {
1184                if let Some((prev_range, prev_text)) = edits.last_mut() {
1185                    if prev_range.end >= range.start {
1186                        prev_range.end = cmp::max(prev_range.end, range.end);
1187                        *prev_text = format!("{prev_text}{new_text}").into();
1188                    } else {
1189                        edits.push((range, new_text));
1190                    }
1191                } else {
1192                    edits.push((range, new_text));
1193                }
1194            }
1195        }
1196        if edits.is_empty() {
1197            return None;
1198        }
1199
1200        self.start_transaction();
1201        self.pending_autoindent.take();
1202        let autoindent_request = autoindent_mode
1203            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1204
1205        let edit_operation = self.text.edit(edits.iter().cloned());
1206        let edit_id = edit_operation.local_timestamp();
1207
1208        if let Some((before_edit, mode)) = autoindent_request {
1209            let indent_size = before_edit.single_indent_size(cx);
1210            let (start_columns, is_block_mode) = match mode {
1211                AutoindentMode::Block {
1212                    original_indent_columns: start_columns,
1213                } => (start_columns, true),
1214                AutoindentMode::EachLine => (Default::default(), false),
1215            };
1216
1217            let mut delta = 0isize;
1218            let entries = edits
1219                .into_iter()
1220                .enumerate()
1221                .zip(&edit_operation.as_edit().unwrap().new_text)
1222                .map(|((ix, (range, _)), new_text)| {
1223                    let new_text_len = new_text.len();
1224                    let old_start = range.start.to_point(&before_edit);
1225                    let new_start = (delta + range.start as isize) as usize;
1226                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1227
1228                    let mut range_of_insertion_to_indent = 0..new_text_len;
1229                    let mut first_line_is_new = false;
1230                    let mut start_column = None;
1231
1232                    // When inserting an entire line at the beginning of an existing line,
1233                    // treat the insertion as new.
1234                    if new_text.contains('\n')
1235                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1236                    {
1237                        first_line_is_new = true;
1238                    }
1239
1240                    // When inserting text starting with a newline, avoid auto-indenting the
1241                    // previous line.
1242                    if new_text[range_of_insertion_to_indent.clone()].starts_with('\n') {
1243                        range_of_insertion_to_indent.start += 1;
1244                        first_line_is_new = true;
1245                    }
1246
1247                    // Avoid auto-indenting after the insertion.
1248                    if is_block_mode {
1249                        start_column = start_columns.get(ix).copied();
1250                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1251                            range_of_insertion_to_indent.end -= 1;
1252                        }
1253                    }
1254
1255                    AutoindentRequestEntry {
1256                        first_line_is_new,
1257                        original_indent_column: start_column,
1258                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1259                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1260                    }
1261                })
1262                .collect();
1263
1264            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1265                before_edit,
1266                entries,
1267                indent_size,
1268                is_block_mode,
1269            }));
1270        }
1271
1272        self.end_transaction(cx);
1273        self.send_operation(Operation::Buffer(edit_operation), cx);
1274        Some(edit_id)
1275    }
1276
1277    fn did_edit(
1278        &mut self,
1279        old_version: &clock::Global,
1280        was_dirty: bool,
1281        cx: &mut ModelContext<Self>,
1282    ) {
1283        if self.edits_since::<usize>(old_version).next().is_none() {
1284            return;
1285        }
1286
1287        self.reparse(cx);
1288
1289        cx.emit(Event::Edited);
1290        if was_dirty != self.is_dirty() {
1291            cx.emit(Event::DirtyChanged);
1292        }
1293        cx.notify();
1294    }
1295
1296    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1297        &mut self,
1298        ops: I,
1299        cx: &mut ModelContext<Self>,
1300    ) -> Result<()> {
1301        self.pending_autoindent.take();
1302        let was_dirty = self.is_dirty();
1303        let old_version = self.version.clone();
1304        let mut deferred_ops = Vec::new();
1305        let buffer_ops = ops
1306            .into_iter()
1307            .filter_map(|op| match op {
1308                Operation::Buffer(op) => Some(op),
1309                _ => {
1310                    if self.can_apply_op(&op) {
1311                        self.apply_op(op, cx);
1312                    } else {
1313                        deferred_ops.push(op);
1314                    }
1315                    None
1316                }
1317            })
1318            .collect::<Vec<_>>();
1319        self.text.apply_ops(buffer_ops)?;
1320        self.deferred_ops.insert(deferred_ops);
1321        self.flush_deferred_ops(cx);
1322        self.did_edit(&old_version, was_dirty, cx);
1323        // Notify independently of whether the buffer was edited as the operations could include a
1324        // selection update.
1325        cx.notify();
1326        Ok(())
1327    }
1328
1329    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1330        let mut deferred_ops = Vec::new();
1331        for op in self.deferred_ops.drain().iter().cloned() {
1332            if self.can_apply_op(&op) {
1333                self.apply_op(op, cx);
1334            } else {
1335                deferred_ops.push(op);
1336            }
1337        }
1338        self.deferred_ops.insert(deferred_ops);
1339    }
1340
1341    fn can_apply_op(&self, operation: &Operation) -> bool {
1342        match operation {
1343            Operation::Buffer(_) => {
1344                unreachable!("buffer operations should never be applied at this layer")
1345            }
1346            Operation::UpdateDiagnostics {
1347                diagnostics: diagnostic_set,
1348                ..
1349            } => diagnostic_set.iter().all(|diagnostic| {
1350                self.text.can_resolve(&diagnostic.range.start)
1351                    && self.text.can_resolve(&diagnostic.range.end)
1352            }),
1353            Operation::UpdateSelections { selections, .. } => selections
1354                .iter()
1355                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1356            Operation::UpdateCompletionTriggers { .. } => true,
1357        }
1358    }
1359
1360    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1361        match operation {
1362            Operation::Buffer(_) => {
1363                unreachable!("buffer operations should never be applied at this layer")
1364            }
1365            Operation::UpdateDiagnostics {
1366                diagnostics: diagnostic_set,
1367                lamport_timestamp,
1368            } => {
1369                let snapshot = self.snapshot();
1370                self.apply_diagnostic_update(
1371                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1372                    lamport_timestamp,
1373                    cx,
1374                );
1375            }
1376            Operation::UpdateSelections {
1377                selections,
1378                lamport_timestamp,
1379                line_mode,
1380            } => {
1381                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1382                    if set.lamport_timestamp > lamport_timestamp {
1383                        return;
1384                    }
1385                }
1386
1387                self.remote_selections.insert(
1388                    lamport_timestamp.replica_id,
1389                    SelectionSet {
1390                        selections,
1391                        lamport_timestamp,
1392                        line_mode,
1393                    },
1394                );
1395                self.text.lamport_clock.observe(lamport_timestamp);
1396                self.selections_update_count += 1;
1397            }
1398            Operation::UpdateCompletionTriggers {
1399                triggers,
1400                lamport_timestamp,
1401            } => {
1402                self.completion_triggers = triggers;
1403                self.text.lamport_clock.observe(lamport_timestamp);
1404            }
1405        }
1406    }
1407
1408    fn apply_diagnostic_update(
1409        &mut self,
1410        diagnostics: DiagnosticSet,
1411        lamport_timestamp: clock::Lamport,
1412        cx: &mut ModelContext<Self>,
1413    ) {
1414        if lamport_timestamp > self.diagnostics_timestamp {
1415            self.diagnostics = diagnostics;
1416            self.diagnostics_timestamp = lamport_timestamp;
1417            self.diagnostics_update_count += 1;
1418            self.text.lamport_clock.observe(lamport_timestamp);
1419            cx.notify();
1420            cx.emit(Event::DiagnosticsUpdated);
1421        }
1422    }
1423
1424    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1425        cx.emit(Event::Operation(operation));
1426    }
1427
1428    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1429        self.remote_selections.remove(&replica_id);
1430        cx.notify();
1431    }
1432
1433    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1434        let was_dirty = self.is_dirty();
1435        let old_version = self.version.clone();
1436
1437        if let Some((transaction_id, operation)) = self.text.undo() {
1438            self.send_operation(Operation::Buffer(operation), cx);
1439            self.did_edit(&old_version, was_dirty, cx);
1440            Some(transaction_id)
1441        } else {
1442            None
1443        }
1444    }
1445
1446    pub fn undo_to_transaction(
1447        &mut self,
1448        transaction_id: TransactionId,
1449        cx: &mut ModelContext<Self>,
1450    ) -> bool {
1451        let was_dirty = self.is_dirty();
1452        let old_version = self.version.clone();
1453
1454        let operations = self.text.undo_to_transaction(transaction_id);
1455        let undone = !operations.is_empty();
1456        for operation in operations {
1457            self.send_operation(Operation::Buffer(operation), cx);
1458        }
1459        if undone {
1460            self.did_edit(&old_version, was_dirty, cx)
1461        }
1462        undone
1463    }
1464
1465    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1466        let was_dirty = self.is_dirty();
1467        let old_version = self.version.clone();
1468
1469        if let Some((transaction_id, operation)) = self.text.redo() {
1470            self.send_operation(Operation::Buffer(operation), cx);
1471            self.did_edit(&old_version, was_dirty, cx);
1472            Some(transaction_id)
1473        } else {
1474            None
1475        }
1476    }
1477
1478    pub fn redo_to_transaction(
1479        &mut self,
1480        transaction_id: TransactionId,
1481        cx: &mut ModelContext<Self>,
1482    ) -> bool {
1483        let was_dirty = self.is_dirty();
1484        let old_version = self.version.clone();
1485
1486        let operations = self.text.redo_to_transaction(transaction_id);
1487        let redone = !operations.is_empty();
1488        for operation in operations {
1489            self.send_operation(Operation::Buffer(operation), cx);
1490        }
1491        if redone {
1492            self.did_edit(&old_version, was_dirty, cx)
1493        }
1494        redone
1495    }
1496
1497    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1498        self.completion_triggers = triggers.clone();
1499        let lamport_timestamp = self.text.lamport_clock.tick();
1500        self.send_operation(
1501            Operation::UpdateCompletionTriggers {
1502                triggers,
1503                lamport_timestamp,
1504            },
1505            cx,
1506        );
1507        cx.notify();
1508    }
1509
1510    pub fn completion_triggers(&self) -> &[String] {
1511        &self.completion_triggers
1512    }
1513}
1514
1515#[cfg(any(test, feature = "test-support"))]
1516impl Buffer {
1517    pub fn set_group_interval(&mut self, group_interval: Duration) {
1518        self.text.set_group_interval(group_interval);
1519    }
1520
1521    pub fn randomly_edit<T>(
1522        &mut self,
1523        rng: &mut T,
1524        old_range_count: usize,
1525        cx: &mut ModelContext<Self>,
1526    ) where
1527        T: rand::Rng,
1528    {
1529        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1530        let mut last_end = None;
1531        for _ in 0..old_range_count {
1532            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1533                break;
1534            }
1535
1536            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1537            let mut range = self.random_byte_range(new_start, rng);
1538            if rng.gen_bool(0.2) {
1539                mem::swap(&mut range.start, &mut range.end);
1540            }
1541            last_end = Some(range.end);
1542
1543            let new_text_len = rng.gen_range(0..10);
1544            let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1545                .take(new_text_len)
1546                .collect();
1547
1548            edits.push((range, new_text));
1549        }
1550        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1551        self.edit(edits, None, cx);
1552    }
1553
1554    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1555        let was_dirty = self.is_dirty();
1556        let old_version = self.version.clone();
1557
1558        let ops = self.text.randomly_undo_redo(rng);
1559        if !ops.is_empty() {
1560            for op in ops {
1561                self.send_operation(Operation::Buffer(op), cx);
1562                self.did_edit(&old_version, was_dirty, cx);
1563            }
1564        }
1565    }
1566}
1567
1568impl Entity for Buffer {
1569    type Event = Event;
1570}
1571
1572impl Deref for Buffer {
1573    type Target = TextBuffer;
1574
1575    fn deref(&self) -> &Self::Target {
1576        &self.text
1577    }
1578}
1579
1580impl BufferSnapshot {
1581    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1582        indent_size_for_line(self, row)
1583    }
1584
1585    pub fn single_indent_size(&self, cx: &AppContext) -> IndentSize {
1586        let language_name = self.language().map(|language| language.name());
1587        let settings = cx.global::<Settings>();
1588        if settings.hard_tabs(language_name.as_deref()) {
1589            IndentSize::tab()
1590        } else {
1591            IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
1592        }
1593    }
1594
1595    pub fn suggested_indents(
1596        &self,
1597        rows: impl Iterator<Item = u32>,
1598        single_indent_size: IndentSize,
1599    ) -> BTreeMap<u32, IndentSize> {
1600        let mut result = BTreeMap::new();
1601
1602        for row_range in contiguous_ranges(rows, 10) {
1603            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1604                Some(suggestions) => suggestions,
1605                _ => break,
1606            };
1607
1608            for (row, suggestion) in row_range.zip(suggestions) {
1609                let indent_size = if let Some(suggestion) = suggestion {
1610                    result
1611                        .get(&suggestion.basis_row)
1612                        .copied()
1613                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1614                        .with_delta(suggestion.delta, single_indent_size)
1615                } else {
1616                    self.indent_size_for_line(row)
1617                };
1618
1619                result.insert(row, indent_size);
1620            }
1621        }
1622
1623        result
1624    }
1625
1626    fn suggest_autoindents(
1627        &self,
1628        row_range: Range<u32>,
1629    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1630        let language = self.language.as_ref()?;
1631        let grammar = language.grammar.as_ref()?;
1632        let config = &language.config;
1633        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1634
1635        // Find the suggested indentation ranges based on the syntax tree.
1636        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1637        let end = Point::new(row_range.end, 0);
1638        let range = (start..end).to_offset(&self.text);
1639        let mut matches = self.syntax.matches(range, &self.text, |grammar| {
1640            Some(&grammar.indents_config.as_ref()?.query)
1641        });
1642
1643        let mut indent_ranges = Vec::<Range<Point>>::new();
1644        while let Some(mat) = matches.peek() {
1645            let mut start: Option<Point> = None;
1646            let mut end: Option<Point> = None;
1647
1648            if let Some(config) = &grammar.indents_config {
1649                for capture in mat.captures {
1650                    if capture.index == config.indent_capture_ix {
1651                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1652                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1653                    } else if Some(capture.index) == config.end_capture_ix {
1654                        end = Some(Point::from_ts_point(capture.node.start_position()));
1655                    }
1656                }
1657            }
1658
1659            matches.advance();
1660            if let Some((start, end)) = start.zip(end) {
1661                if start.row == end.row {
1662                    continue;
1663                }
1664
1665                let range = start..end;
1666                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1667                    Err(ix) => indent_ranges.insert(ix, range),
1668                    Ok(ix) => {
1669                        let prev_range = &mut indent_ranges[ix];
1670                        prev_range.end = prev_range.end.max(range.end);
1671                    }
1672                }
1673            }
1674        }
1675
1676        // Find the suggested indentation increases and decreased based on regexes.
1677        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1678        self.for_each_line(
1679            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1680                ..Point::new(row_range.end, 0),
1681            |row, line| {
1682                if config
1683                    .decrease_indent_pattern
1684                    .as_ref()
1685                    .map_or(false, |regex| regex.is_match(line))
1686                {
1687                    indent_change_rows.push((row, Ordering::Less));
1688                }
1689                if config
1690                    .increase_indent_pattern
1691                    .as_ref()
1692                    .map_or(false, |regex| regex.is_match(line))
1693                {
1694                    indent_change_rows.push((row + 1, Ordering::Greater));
1695                }
1696            },
1697        );
1698
1699        let mut indent_changes = indent_change_rows.into_iter().peekable();
1700        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1701            prev_non_blank_row.unwrap_or(0)
1702        } else {
1703            row_range.start.saturating_sub(1)
1704        };
1705        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1706        Some(row_range.map(move |row| {
1707            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1708
1709            let mut indent_from_prev_row = false;
1710            let mut outdent_from_prev_row = false;
1711            let mut outdent_to_row = u32::MAX;
1712
1713            while let Some((indent_row, delta)) = indent_changes.peek() {
1714                match indent_row.cmp(&row) {
1715                    Ordering::Equal => match delta {
1716                        Ordering::Less => outdent_from_prev_row = true,
1717                        Ordering::Greater => indent_from_prev_row = true,
1718                        _ => {}
1719                    },
1720
1721                    Ordering::Greater => break,
1722                    Ordering::Less => {}
1723                }
1724
1725                indent_changes.next();
1726            }
1727
1728            for range in &indent_ranges {
1729                if range.start.row >= row {
1730                    break;
1731                }
1732                if range.start.row == prev_row && range.end > row_start {
1733                    indent_from_prev_row = true;
1734                }
1735                if range.end > prev_row_start && range.end <= row_start {
1736                    outdent_to_row = outdent_to_row.min(range.start.row);
1737                }
1738            }
1739
1740            let suggestion = if outdent_to_row == prev_row
1741                || (outdent_from_prev_row && indent_from_prev_row)
1742            {
1743                Some(IndentSuggestion {
1744                    basis_row: prev_row,
1745                    delta: Ordering::Equal,
1746                })
1747            } else if indent_from_prev_row {
1748                Some(IndentSuggestion {
1749                    basis_row: prev_row,
1750                    delta: Ordering::Greater,
1751                })
1752            } else if outdent_to_row < prev_row {
1753                Some(IndentSuggestion {
1754                    basis_row: outdent_to_row,
1755                    delta: Ordering::Equal,
1756                })
1757            } else if outdent_from_prev_row {
1758                Some(IndentSuggestion {
1759                    basis_row: prev_row,
1760                    delta: Ordering::Less,
1761                })
1762            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
1763            {
1764                Some(IndentSuggestion {
1765                    basis_row: prev_row,
1766                    delta: Ordering::Equal,
1767                })
1768            } else {
1769                None
1770            };
1771
1772            prev_row = row;
1773            prev_row_start = row_start;
1774            suggestion
1775        }))
1776    }
1777
1778    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1779        while row > 0 {
1780            row -= 1;
1781            if !self.is_line_blank(row) {
1782                return Some(row);
1783            }
1784        }
1785        None
1786    }
1787
1788    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
1789        let range = range.start.to_offset(self)..range.end.to_offset(self);
1790
1791        let mut syntax = None;
1792        let mut diagnostic_endpoints = Vec::new();
1793        if language_aware {
1794            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
1795                grammar.highlights_query.as_ref()
1796            });
1797            let highlight_maps = captures
1798                .grammars()
1799                .into_iter()
1800                .map(|grammar| grammar.highlight_map())
1801                .collect();
1802            syntax = Some((captures, highlight_maps));
1803            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1804                diagnostic_endpoints.push(DiagnosticEndpoint {
1805                    offset: entry.range.start,
1806                    is_start: true,
1807                    severity: entry.diagnostic.severity,
1808                    is_unnecessary: entry.diagnostic.is_unnecessary,
1809                });
1810                diagnostic_endpoints.push(DiagnosticEndpoint {
1811                    offset: entry.range.end,
1812                    is_start: false,
1813                    severity: entry.diagnostic.severity,
1814                    is_unnecessary: entry.diagnostic.is_unnecessary,
1815                });
1816            }
1817            diagnostic_endpoints
1818                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1819        }
1820
1821        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
1822    }
1823
1824    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
1825        let mut line = String::new();
1826        let mut row = range.start.row;
1827        for chunk in self
1828            .as_rope()
1829            .chunks_in_range(range.to_offset(self))
1830            .chain(["\n"])
1831        {
1832            for (newline_ix, text) in chunk.split('\n').enumerate() {
1833                if newline_ix > 0 {
1834                    callback(row, &line);
1835                    row += 1;
1836                    line.clear();
1837                }
1838                line.push_str(text);
1839            }
1840        }
1841    }
1842
1843    pub fn language(&self) -> Option<&Arc<Language>> {
1844        self.language.as_ref()
1845    }
1846
1847    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
1848        let mut start = start.to_offset(self);
1849        let mut end = start;
1850        let mut next_chars = self.chars_at(start).peekable();
1851        let mut prev_chars = self.reversed_chars_at(start).peekable();
1852        let word_kind = cmp::max(
1853            prev_chars.peek().copied().map(char_kind),
1854            next_chars.peek().copied().map(char_kind),
1855        );
1856
1857        for ch in prev_chars {
1858            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1859                start -= ch.len_utf8();
1860            } else {
1861                break;
1862            }
1863        }
1864
1865        for ch in next_chars {
1866            if Some(char_kind(ch)) == word_kind && ch != '\n' {
1867                end += ch.len_utf8();
1868            } else {
1869                break;
1870            }
1871        }
1872
1873        (start..end, word_kind)
1874    }
1875
1876    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1877        let range = range.start.to_offset(self)..range.end.to_offset(self);
1878        let mut result: Option<Range<usize>> = None;
1879        'outer: for (_, _, node) in self.syntax.layers_for_range(range.clone(), &self.text) {
1880            let mut cursor = node.walk();
1881
1882            // Descend to the first leaf that touches the start of the range,
1883            // and if the range is non-empty, extends beyond the start.
1884            while cursor.goto_first_child_for_byte(range.start).is_some() {
1885                if !range.is_empty() && cursor.node().end_byte() == range.start {
1886                    cursor.goto_next_sibling();
1887                }
1888            }
1889
1890            // Ascend to the smallest ancestor that strictly contains the range.
1891            loop {
1892                let node_range = cursor.node().byte_range();
1893                if node_range.start <= range.start
1894                    && node_range.end >= range.end
1895                    && node_range.len() > range.len()
1896                {
1897                    break;
1898                }
1899                if !cursor.goto_parent() {
1900                    continue 'outer;
1901                }
1902            }
1903
1904            let left_node = cursor.node();
1905            let mut layer_result = left_node.byte_range();
1906
1907            // For an empty range, try to find another node immediately to the right of the range.
1908            if left_node.end_byte() == range.start {
1909                let mut right_node = None;
1910                while !cursor.goto_next_sibling() {
1911                    if !cursor.goto_parent() {
1912                        break;
1913                    }
1914                }
1915
1916                while cursor.node().start_byte() == range.start {
1917                    right_node = Some(cursor.node());
1918                    if !cursor.goto_first_child() {
1919                        break;
1920                    }
1921                }
1922
1923                // If there is a candidate node on both sides of the (empty) range, then
1924                // decide between the two by favoring a named node over an anonymous token.
1925                // If both nodes are the same in that regard, favor the right one.
1926                if let Some(right_node) = right_node {
1927                    if right_node.is_named() || !left_node.is_named() {
1928                        layer_result = right_node.byte_range();
1929                    }
1930                }
1931            }
1932
1933            if let Some(previous_result) = &result {
1934                if previous_result.len() < layer_result.len() {
1935                    continue;
1936                }
1937            }
1938            result = Some(layer_result);
1939        }
1940
1941        result
1942    }
1943
1944    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1945        self.outline_items_containing(0..self.len(), theme)
1946            .map(Outline::new)
1947    }
1948
1949    pub fn symbols_containing<T: ToOffset>(
1950        &self,
1951        position: T,
1952        theme: Option<&SyntaxTheme>,
1953    ) -> Option<Vec<OutlineItem<Anchor>>> {
1954        let position = position.to_offset(self);
1955        let mut items = self.outline_items_containing(
1956            position.saturating_sub(1)..self.len().min(position + 1),
1957            theme,
1958        )?;
1959        let mut prev_depth = None;
1960        items.retain(|item| {
1961            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
1962            prev_depth = Some(item.depth);
1963            result
1964        });
1965        Some(items)
1966    }
1967
1968    fn outline_items_containing(
1969        &self,
1970        range: Range<usize>,
1971        theme: Option<&SyntaxTheme>,
1972    ) -> Option<Vec<OutlineItem<Anchor>>> {
1973        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1974            grammar.outline_config.as_ref().map(|c| &c.query)
1975        });
1976        let configs = matches
1977            .grammars()
1978            .iter()
1979            .map(|g| g.outline_config.as_ref().unwrap())
1980            .collect::<Vec<_>>();
1981
1982        let mut chunks = self.chunks(0..self.len(), true);
1983        let mut stack = Vec::<Range<usize>>::new();
1984        let mut items = Vec::new();
1985        while let Some(mat) = matches.peek() {
1986            let config = &configs[mat.grammar_index];
1987            let item_node = mat.captures.iter().find_map(|cap| {
1988                if cap.index == config.item_capture_ix {
1989                    Some(cap.node)
1990                } else {
1991                    None
1992                }
1993            })?;
1994
1995            let item_range = item_node.byte_range();
1996            if item_range.end < range.start || item_range.start > range.end {
1997                matches.advance();
1998                continue;
1999            }
2000
2001            // TODO - move later, after processing captures
2002
2003            let mut text = String::new();
2004            let mut name_ranges = Vec::new();
2005            let mut highlight_ranges = Vec::new();
2006            for capture in mat.captures {
2007                let node_is_name;
2008                if capture.index == config.name_capture_ix {
2009                    node_is_name = true;
2010                } else if Some(capture.index) == config.context_capture_ix {
2011                    node_is_name = false;
2012                } else {
2013                    continue;
2014                }
2015
2016                let range = capture.node.start_byte()..capture.node.end_byte();
2017                if !text.is_empty() {
2018                    text.push(' ');
2019                }
2020                if node_is_name {
2021                    let mut start = text.len();
2022                    let end = start + range.len();
2023
2024                    // When multiple names are captured, then the matcheable text
2025                    // includes the whitespace in between the names.
2026                    if !name_ranges.is_empty() {
2027                        start -= 1;
2028                    }
2029
2030                    name_ranges.push(start..end);
2031                }
2032
2033                let mut offset = range.start;
2034                chunks.seek(offset);
2035                for mut chunk in chunks.by_ref() {
2036                    if chunk.text.len() > range.end - offset {
2037                        chunk.text = &chunk.text[0..(range.end - offset)];
2038                        offset = range.end;
2039                    } else {
2040                        offset += chunk.text.len();
2041                    }
2042                    let style = chunk
2043                        .syntax_highlight_id
2044                        .zip(theme)
2045                        .and_then(|(highlight, theme)| highlight.style(theme));
2046                    if let Some(style) = style {
2047                        let start = text.len();
2048                        let end = start + chunk.text.len();
2049                        highlight_ranges.push((start..end, style));
2050                    }
2051                    text.push_str(chunk.text);
2052                    if offset >= range.end {
2053                        break;
2054                    }
2055                }
2056            }
2057
2058            matches.advance();
2059            while stack.last().map_or(false, |prev_range| {
2060                prev_range.start > item_range.start || prev_range.end < item_range.end
2061            }) {
2062                stack.pop();
2063            }
2064            stack.push(item_range.clone());
2065
2066            items.push(OutlineItem {
2067                depth: stack.len() - 1,
2068                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2069                text,
2070                highlight_ranges,
2071                name_ranges,
2072            })
2073        }
2074        Some(items)
2075    }
2076
2077    pub fn enclosing_bracket_ranges<T: ToOffset>(
2078        &self,
2079        range: Range<T>,
2080    ) -> Option<(Range<usize>, Range<usize>)> {
2081        // Find bracket pairs that *inclusively* contain the given range.
2082        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
2083        let mut matches = self.syntax.matches(range, &self.text, |grammar| {
2084            grammar.brackets_config.as_ref().map(|c| &c.query)
2085        });
2086        let configs = matches
2087            .grammars()
2088            .iter()
2089            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2090            .collect::<Vec<_>>();
2091
2092        // Get the ranges of the innermost pair of brackets.
2093        let mut result: Option<(Range<usize>, Range<usize>)> = None;
2094        while let Some(mat) = matches.peek() {
2095            let mut open = None;
2096            let mut close = None;
2097            let config = &configs[mat.grammar_index];
2098            for capture in mat.captures {
2099                if capture.index == config.open_capture_ix {
2100                    open = Some(capture.node.byte_range());
2101                } else if capture.index == config.close_capture_ix {
2102                    close = Some(capture.node.byte_range());
2103                }
2104            }
2105
2106            matches.advance();
2107
2108            if let Some((open, close)) = open.zip(close) {
2109                let len = close.end - open.start;
2110
2111                if let Some((existing_open, existing_close)) = &result {
2112                    let existing_len = existing_close.end - existing_open.start;
2113                    if len > existing_len {
2114                        continue;
2115                    }
2116                }
2117
2118                result = Some((open, close));
2119            }
2120        }
2121
2122        result
2123    }
2124
2125    #[allow(clippy::type_complexity)]
2126    pub fn remote_selections_in_range(
2127        &self,
2128        range: Range<Anchor>,
2129    ) -> impl Iterator<
2130        Item = (
2131            ReplicaId,
2132            bool,
2133            impl Iterator<Item = &Selection<Anchor>> + '_,
2134        ),
2135    > + '_ {
2136        self.remote_selections
2137            .iter()
2138            .filter(|(replica_id, set)| {
2139                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2140            })
2141            .map(move |(replica_id, set)| {
2142                let start_ix = match set.selections.binary_search_by(|probe| {
2143                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2144                }) {
2145                    Ok(ix) | Err(ix) => ix,
2146                };
2147                let end_ix = match set.selections.binary_search_by(|probe| {
2148                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2149                }) {
2150                    Ok(ix) | Err(ix) => ix,
2151                };
2152
2153                (
2154                    *replica_id,
2155                    set.line_mode,
2156                    set.selections[start_ix..end_ix].iter(),
2157                )
2158            })
2159    }
2160
2161    pub fn diagnostics_in_range<'a, T, O>(
2162        &'a self,
2163        search_range: Range<T>,
2164        reversed: bool,
2165    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2166    where
2167        T: 'a + Clone + ToOffset,
2168        O: 'a + FromAnchor,
2169    {
2170        self.diagnostics.range(search_range, self, true, reversed)
2171    }
2172
2173    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2174        let mut groups = Vec::new();
2175        self.diagnostics.groups(&mut groups, self);
2176        groups
2177    }
2178
2179    pub fn diagnostic_group<'a, O>(
2180        &'a self,
2181        group_id: usize,
2182    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2183    where
2184        O: 'a + FromAnchor,
2185    {
2186        self.diagnostics.group(group_id, self)
2187    }
2188
2189    pub fn diagnostics_update_count(&self) -> usize {
2190        self.diagnostics_update_count
2191    }
2192
2193    pub fn parse_count(&self) -> usize {
2194        self.parse_count
2195    }
2196
2197    pub fn selections_update_count(&self) -> usize {
2198        self.selections_update_count
2199    }
2200
2201    pub fn file(&self) -> Option<&dyn File> {
2202        self.file.as_deref()
2203    }
2204
2205    pub fn file_update_count(&self) -> usize {
2206        self.file_update_count
2207    }
2208}
2209
2210pub fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2211    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2212}
2213
2214pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2215    let mut result = IndentSize::spaces(0);
2216    for c in text {
2217        let kind = match c {
2218            ' ' => IndentKind::Space,
2219            '\t' => IndentKind::Tab,
2220            _ => break,
2221        };
2222        if result.len == 0 {
2223            result.kind = kind;
2224        }
2225        result.len += 1;
2226    }
2227    result
2228}
2229
2230impl Clone for BufferSnapshot {
2231    fn clone(&self) -> Self {
2232        Self {
2233            text: self.text.clone(),
2234            syntax: self.syntax.clone(),
2235            file: self.file.clone(),
2236            remote_selections: self.remote_selections.clone(),
2237            diagnostics: self.diagnostics.clone(),
2238            selections_update_count: self.selections_update_count,
2239            diagnostics_update_count: self.diagnostics_update_count,
2240            file_update_count: self.file_update_count,
2241            language: self.language.clone(),
2242            parse_count: self.parse_count,
2243        }
2244    }
2245}
2246
2247impl Deref for BufferSnapshot {
2248    type Target = text::BufferSnapshot;
2249
2250    fn deref(&self) -> &Self::Target {
2251        &self.text
2252    }
2253}
2254
2255unsafe impl<'a> Send for BufferChunks<'a> {}
2256
2257impl<'a> BufferChunks<'a> {
2258    pub(crate) fn new(
2259        text: &'a Rope,
2260        range: Range<usize>,
2261        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2262        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2263    ) -> Self {
2264        let mut highlights = None;
2265        if let Some((captures, highlight_maps)) = syntax {
2266            highlights = Some(BufferChunkHighlights {
2267                captures,
2268                next_capture: None,
2269                stack: Default::default(),
2270                highlight_maps,
2271            })
2272        }
2273
2274        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2275        let chunks = text.chunks_in_range(range.clone());
2276
2277        BufferChunks {
2278            range,
2279            chunks,
2280            diagnostic_endpoints,
2281            error_depth: 0,
2282            warning_depth: 0,
2283            information_depth: 0,
2284            hint_depth: 0,
2285            unnecessary_depth: 0,
2286            highlights,
2287        }
2288    }
2289
2290    pub fn seek(&mut self, offset: usize) {
2291        self.range.start = offset;
2292        self.chunks.seek(self.range.start);
2293        if let Some(highlights) = self.highlights.as_mut() {
2294            highlights
2295                .stack
2296                .retain(|(end_offset, _)| *end_offset > offset);
2297            if let Some(capture) = &highlights.next_capture {
2298                if offset >= capture.node.start_byte() {
2299                    let next_capture_end = capture.node.end_byte();
2300                    if offset < next_capture_end {
2301                        highlights.stack.push((
2302                            next_capture_end,
2303                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2304                        ));
2305                    }
2306                    highlights.next_capture.take();
2307                }
2308            }
2309            highlights.captures.set_byte_range(self.range.clone());
2310        }
2311    }
2312
2313    pub fn offset(&self) -> usize {
2314        self.range.start
2315    }
2316
2317    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2318        let depth = match endpoint.severity {
2319            DiagnosticSeverity::ERROR => &mut self.error_depth,
2320            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2321            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2322            DiagnosticSeverity::HINT => &mut self.hint_depth,
2323            _ => return,
2324        };
2325        if endpoint.is_start {
2326            *depth += 1;
2327        } else {
2328            *depth -= 1;
2329        }
2330
2331        if endpoint.is_unnecessary {
2332            if endpoint.is_start {
2333                self.unnecessary_depth += 1;
2334            } else {
2335                self.unnecessary_depth -= 1;
2336            }
2337        }
2338    }
2339
2340    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2341        if self.error_depth > 0 {
2342            Some(DiagnosticSeverity::ERROR)
2343        } else if self.warning_depth > 0 {
2344            Some(DiagnosticSeverity::WARNING)
2345        } else if self.information_depth > 0 {
2346            Some(DiagnosticSeverity::INFORMATION)
2347        } else if self.hint_depth > 0 {
2348            Some(DiagnosticSeverity::HINT)
2349        } else {
2350            None
2351        }
2352    }
2353
2354    fn current_code_is_unnecessary(&self) -> bool {
2355        self.unnecessary_depth > 0
2356    }
2357}
2358
2359impl<'a> Iterator for BufferChunks<'a> {
2360    type Item = Chunk<'a>;
2361
2362    fn next(&mut self) -> Option<Self::Item> {
2363        let mut next_capture_start = usize::MAX;
2364        let mut next_diagnostic_endpoint = usize::MAX;
2365
2366        if let Some(highlights) = self.highlights.as_mut() {
2367            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2368                if *parent_capture_end <= self.range.start {
2369                    highlights.stack.pop();
2370                } else {
2371                    break;
2372                }
2373            }
2374
2375            if highlights.next_capture.is_none() {
2376                highlights.next_capture = highlights.captures.next();
2377            }
2378
2379            while let Some(capture) = highlights.next_capture.as_ref() {
2380                if self.range.start < capture.node.start_byte() {
2381                    next_capture_start = capture.node.start_byte();
2382                    break;
2383                } else {
2384                    let highlight_id =
2385                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2386                    highlights
2387                        .stack
2388                        .push((capture.node.end_byte(), highlight_id));
2389                    highlights.next_capture = highlights.captures.next();
2390                }
2391            }
2392        }
2393
2394        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2395            if endpoint.offset <= self.range.start {
2396                self.update_diagnostic_depths(endpoint);
2397                self.diagnostic_endpoints.next();
2398            } else {
2399                next_diagnostic_endpoint = endpoint.offset;
2400                break;
2401            }
2402        }
2403
2404        if let Some(chunk) = self.chunks.peek() {
2405            let chunk_start = self.range.start;
2406            let mut chunk_end = (self.chunks.offset() + chunk.len())
2407                .min(next_capture_start)
2408                .min(next_diagnostic_endpoint);
2409            let mut highlight_id = None;
2410            if let Some(highlights) = self.highlights.as_ref() {
2411                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2412                    chunk_end = chunk_end.min(*parent_capture_end);
2413                    highlight_id = Some(*parent_highlight_id);
2414                }
2415            }
2416
2417            let slice =
2418                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2419            self.range.start = chunk_end;
2420            if self.range.start == self.chunks.offset() + chunk.len() {
2421                self.chunks.next().unwrap();
2422            }
2423
2424            Some(Chunk {
2425                text: slice,
2426                syntax_highlight_id: highlight_id,
2427                highlight_style: None,
2428                diagnostic_severity: self.current_diagnostic_severity(),
2429                is_unnecessary: self.current_code_is_unnecessary(),
2430            })
2431        } else {
2432            None
2433        }
2434    }
2435}
2436
2437impl operation_queue::Operation for Operation {
2438    fn lamport_timestamp(&self) -> clock::Lamport {
2439        match self {
2440            Operation::Buffer(_) => {
2441                unreachable!("buffer operations should never be deferred at this layer")
2442            }
2443            Operation::UpdateDiagnostics {
2444                lamport_timestamp, ..
2445            }
2446            | Operation::UpdateSelections {
2447                lamport_timestamp, ..
2448            }
2449            | Operation::UpdateCompletionTriggers {
2450                lamport_timestamp, ..
2451            } => *lamport_timestamp,
2452        }
2453    }
2454}
2455
2456impl Default for Diagnostic {
2457    fn default() -> Self {
2458        Self {
2459            code: None,
2460            severity: DiagnosticSeverity::ERROR,
2461            message: Default::default(),
2462            group_id: 0,
2463            is_primary: false,
2464            is_valid: true,
2465            is_disk_based: false,
2466            is_unnecessary: false,
2467        }
2468    }
2469}
2470
2471impl IndentSize {
2472    pub fn spaces(len: u32) -> Self {
2473        Self {
2474            len,
2475            kind: IndentKind::Space,
2476        }
2477    }
2478
2479    pub fn tab() -> Self {
2480        Self {
2481            len: 1,
2482            kind: IndentKind::Tab,
2483        }
2484    }
2485
2486    pub fn chars(&self) -> impl Iterator<Item = char> {
2487        iter::repeat(self.char()).take(self.len as usize)
2488    }
2489
2490    pub fn char(&self) -> char {
2491        match self.kind {
2492            IndentKind::Space => ' ',
2493            IndentKind::Tab => '\t',
2494        }
2495    }
2496
2497    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2498        match direction {
2499            Ordering::Less => {
2500                if self.kind == size.kind && self.len >= size.len {
2501                    self.len -= size.len;
2502                }
2503            }
2504            Ordering::Equal => {}
2505            Ordering::Greater => {
2506                if self.len == 0 {
2507                    self = size;
2508                } else if self.kind == size.kind {
2509                    self.len += size.len;
2510                }
2511            }
2512        }
2513        self
2514    }
2515}
2516
2517impl Completion {
2518    pub fn sort_key(&self) -> (usize, &str) {
2519        let kind_key = match self.lsp_completion.kind {
2520            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2521            _ => 1,
2522        };
2523        (kind_key, &self.label.text[self.label.filter_range.clone()])
2524    }
2525
2526    pub fn is_snippet(&self) -> bool {
2527        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2528    }
2529}
2530
2531pub fn contiguous_ranges(
2532    values: impl Iterator<Item = u32>,
2533    max_len: usize,
2534) -> impl Iterator<Item = Range<u32>> {
2535    let mut values = values;
2536    let mut current_range: Option<Range<u32>> = None;
2537    std::iter::from_fn(move || loop {
2538        if let Some(value) = values.next() {
2539            if let Some(range) = &mut current_range {
2540                if value == range.end && range.len() < max_len {
2541                    range.end += 1;
2542                    continue;
2543                }
2544            }
2545
2546            let prev_range = current_range.clone();
2547            current_range = Some(value..(value + 1));
2548            if prev_range.is_some() {
2549                return prev_range;
2550            }
2551        } else {
2552            return current_range.take();
2553        }
2554    })
2555}
2556
2557pub fn char_kind(c: char) -> CharKind {
2558    if c.is_whitespace() {
2559        CharKind::Whitespace
2560    } else if c.is_alphanumeric() || c == '_' {
2561        CharKind::Word
2562    } else {
2563        CharKind::Punctuation
2564    }
2565}