buffer.rs

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