buffer.rs

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