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    #[cfg(test)]
 801    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 802        self.sync_parse_timeout = timeout;
 803    }
 804
 805    fn reparse(&mut self, cx: &mut ModelContext<Self>) {
 806        if self.parsing_in_background {
 807            return;
 808        }
 809        let language = if let Some(language) = self.language.clone() {
 810            language
 811        } else {
 812            return;
 813        };
 814
 815        let text = self.text_snapshot();
 816        let parsed_version = self.version();
 817
 818        let mut syntax_map = self.syntax_map.lock();
 819        syntax_map.interpolate(&text);
 820        let language_registry = syntax_map.language_registry();
 821        let mut syntax_snapshot = syntax_map.snapshot();
 822        let syntax_map_version = syntax_map.parsed_version();
 823        drop(syntax_map);
 824
 825        let parse_task = cx.background().spawn({
 826            let language = language.clone();
 827            async move {
 828                syntax_snapshot.reparse(&syntax_map_version, &text, language_registry, language);
 829                syntax_snapshot
 830            }
 831        });
 832
 833        match cx
 834            .background()
 835            .block_with_timeout(self.sync_parse_timeout, parse_task)
 836        {
 837            Ok(new_syntax_snapshot) => {
 838                self.did_finish_parsing(new_syntax_snapshot, parsed_version, cx);
 839                return;
 840            }
 841            Err(parse_task) => {
 842                self.parsing_in_background = true;
 843                cx.spawn(move |this, mut cx| async move {
 844                    let new_syntax_map = parse_task.await;
 845                    this.update(&mut cx, move |this, cx| {
 846                        let grammar_changed =
 847                            this.language.as_ref().map_or(true, |current_language| {
 848                                !Arc::ptr_eq(&language, current_language)
 849                            });
 850                        let parse_again =
 851                            this.version.changed_since(&parsed_version) || grammar_changed;
 852                        this.did_finish_parsing(new_syntax_map, parsed_version, cx);
 853                        this.parsing_in_background = false;
 854                        if parse_again {
 855                            this.reparse(cx);
 856                        }
 857                    });
 858                })
 859                .detach();
 860            }
 861        }
 862    }
 863
 864    fn did_finish_parsing(
 865        &mut self,
 866        syntax_snapshot: SyntaxSnapshot,
 867        version: clock::Global,
 868        cx: &mut ModelContext<Self>,
 869    ) {
 870        self.parse_count += 1;
 871        self.syntax_map.lock().did_parse(syntax_snapshot, version);
 872        self.request_autoindent(cx);
 873        cx.emit(Event::Reparsed);
 874        cx.notify();
 875    }
 876
 877    pub fn update_diagnostics(&mut self, diagnostics: DiagnosticSet, cx: &mut ModelContext<Self>) {
 878        let lamport_timestamp = self.text.lamport_clock.tick();
 879        let op = Operation::UpdateDiagnostics {
 880            diagnostics: diagnostics.iter().cloned().collect(),
 881            lamport_timestamp,
 882        };
 883        self.apply_diagnostic_update(diagnostics, lamport_timestamp, cx);
 884        self.send_operation(op, cx);
 885    }
 886
 887    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 888        if let Some(indent_sizes) = self.compute_autoindents() {
 889            let indent_sizes = cx.background().spawn(indent_sizes);
 890            match cx
 891                .background()
 892                .block_with_timeout(Duration::from_micros(500), indent_sizes)
 893            {
 894                Ok(indent_sizes) => self.apply_autoindents(indent_sizes, cx),
 895                Err(indent_sizes) => {
 896                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 897                        let indent_sizes = indent_sizes.await;
 898                        this.update(&mut cx, |this, cx| {
 899                            this.apply_autoindents(indent_sizes, cx);
 900                        });
 901                    }));
 902                }
 903            }
 904        } else {
 905            self.autoindent_requests.clear();
 906        }
 907    }
 908
 909    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, IndentSize>>> {
 910        let max_rows_between_yields = 100;
 911        let snapshot = self.snapshot();
 912        if snapshot.syntax.is_empty() || self.autoindent_requests.is_empty() {
 913            return None;
 914        }
 915
 916        let autoindent_requests = self.autoindent_requests.clone();
 917        Some(async move {
 918            let mut indent_sizes = BTreeMap::new();
 919            for request in autoindent_requests {
 920                // Resolve each edited range to its row in the current buffer and in the
 921                // buffer before this batch of edits.
 922                let mut row_ranges = Vec::new();
 923                let mut old_to_new_rows = BTreeMap::new();
 924                let mut language_indent_sizes_by_new_row = Vec::new();
 925                for entry in &request.entries {
 926                    let position = entry.range.start;
 927                    let new_row = position.to_point(&snapshot).row;
 928                    let new_end_row = entry.range.end.to_point(&snapshot).row + 1;
 929                    language_indent_sizes_by_new_row.push((new_row, entry.indent_size));
 930
 931                    if !entry.first_line_is_new {
 932                        let old_row = position.to_point(&request.before_edit).row;
 933                        old_to_new_rows.insert(old_row, new_row);
 934                    }
 935                    row_ranges.push((new_row..new_end_row, entry.original_indent_column));
 936                }
 937
 938                // Build a map containing the suggested indentation for each of the edited lines
 939                // with respect to the state of the buffer before these edits. This map is keyed
 940                // by the rows for these lines in the current state of the buffer.
 941                let mut old_suggestions = BTreeMap::<u32, (IndentSize, bool)>::default();
 942                let old_edited_ranges =
 943                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 944                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 945                let mut language_indent_size = IndentSize::default();
 946                for old_edited_range in old_edited_ranges {
 947                    let suggestions = request
 948                        .before_edit
 949                        .suggest_autoindents(old_edited_range.clone())
 950                        .into_iter()
 951                        .flatten();
 952                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 953                        if let Some(suggestion) = suggestion {
 954                            let new_row = *old_to_new_rows.get(&old_row).unwrap();
 955
 956                            // Find the indent size based on the language for this row.
 957                            while let Some((row, size)) = language_indent_sizes.peek() {
 958                                if *row > new_row {
 959                                    break;
 960                                }
 961                                language_indent_size = *size;
 962                                language_indent_sizes.next();
 963                            }
 964
 965                            let suggested_indent = old_to_new_rows
 966                                .get(&suggestion.basis_row)
 967                                .and_then(|from_row| {
 968                                    Some(old_suggestions.get(from_row).copied()?.0)
 969                                })
 970                                .unwrap_or_else(|| {
 971                                    request
 972                                        .before_edit
 973                                        .indent_size_for_line(suggestion.basis_row)
 974                                })
 975                                .with_delta(suggestion.delta, language_indent_size);
 976                            old_suggestions
 977                                .insert(new_row, (suggested_indent, suggestion.within_error));
 978                        }
 979                    }
 980                    yield_now().await;
 981                }
 982
 983                // In block mode, only compute indentation suggestions for the first line
 984                // of each insertion. Otherwise, compute suggestions for every inserted line.
 985                let new_edited_row_ranges = contiguous_ranges(
 986                    row_ranges.iter().flat_map(|(range, _)| {
 987                        if request.is_block_mode {
 988                            range.start..range.start + 1
 989                        } else {
 990                            range.clone()
 991                        }
 992                    }),
 993                    max_rows_between_yields,
 994                );
 995
 996                // Compute new suggestions for each line, but only include them in the result
 997                // if they differ from the old suggestion for that line.
 998                let mut language_indent_sizes = language_indent_sizes_by_new_row.iter().peekable();
 999                let mut language_indent_size = IndentSize::default();
1000                for new_edited_row_range in new_edited_row_ranges {
1001                    let suggestions = snapshot
1002                        .suggest_autoindents(new_edited_row_range.clone())
1003                        .into_iter()
1004                        .flatten();
1005                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
1006                        if let Some(suggestion) = suggestion {
1007                            // Find the indent size based on the language for this row.
1008                            while let Some((row, size)) = language_indent_sizes.peek() {
1009                                if *row > new_row {
1010                                    break;
1011                                }
1012                                language_indent_size = *size;
1013                                language_indent_sizes.next();
1014                            }
1015
1016                            let suggested_indent = indent_sizes
1017                                .get(&suggestion.basis_row)
1018                                .copied()
1019                                .unwrap_or_else(|| {
1020                                    snapshot.indent_size_for_line(suggestion.basis_row)
1021                                })
1022                                .with_delta(suggestion.delta, language_indent_size);
1023                            if old_suggestions.get(&new_row).map_or(
1024                                true,
1025                                |(old_indentation, was_within_error)| {
1026                                    suggested_indent != *old_indentation
1027                                        && (!suggestion.within_error || *was_within_error)
1028                                },
1029                            ) {
1030                                indent_sizes.insert(new_row, suggested_indent);
1031                            }
1032                        }
1033                    }
1034                    yield_now().await;
1035                }
1036
1037                // For each block of inserted text, adjust the indentation of the remaining
1038                // lines of the block by the same amount as the first line was adjusted.
1039                if request.is_block_mode {
1040                    for (row_range, original_indent_column) in
1041                        row_ranges
1042                            .into_iter()
1043                            .filter_map(|(range, original_indent_column)| {
1044                                if range.len() > 1 {
1045                                    Some((range, original_indent_column?))
1046                                } else {
1047                                    None
1048                                }
1049                            })
1050                    {
1051                        let new_indent = indent_sizes
1052                            .get(&row_range.start)
1053                            .copied()
1054                            .unwrap_or_else(|| snapshot.indent_size_for_line(row_range.start));
1055                        let delta = new_indent.len as i64 - original_indent_column as i64;
1056                        if delta != 0 {
1057                            for row in row_range.skip(1) {
1058                                indent_sizes.entry(row).or_insert_with(|| {
1059                                    let mut size = snapshot.indent_size_for_line(row);
1060                                    if size.kind == new_indent.kind {
1061                                        match delta.cmp(&0) {
1062                                            Ordering::Greater => size.len += delta as u32,
1063                                            Ordering::Less => {
1064                                                size.len = size.len.saturating_sub(-delta as u32)
1065                                            }
1066                                            Ordering::Equal => {}
1067                                        }
1068                                    }
1069                                    size
1070                                });
1071                            }
1072                        }
1073                    }
1074                }
1075            }
1076
1077            indent_sizes
1078        })
1079    }
1080
1081    fn apply_autoindents(
1082        &mut self,
1083        indent_sizes: BTreeMap<u32, IndentSize>,
1084        cx: &mut ModelContext<Self>,
1085    ) {
1086        self.autoindent_requests.clear();
1087
1088        let edits: Vec<_> = indent_sizes
1089            .into_iter()
1090            .filter_map(|(row, indent_size)| {
1091                let current_size = indent_size_for_line(self, row);
1092                Self::edit_for_indent_size_adjustment(row, current_size, indent_size)
1093            })
1094            .collect();
1095
1096        self.edit(edits, None, cx);
1097    }
1098
1099    // Create a minimal edit that will cause the the given row to be indented
1100    // with the given size. After applying this edit, the length of the line
1101    // will always be at least `new_size.len`.
1102    pub fn edit_for_indent_size_adjustment(
1103        row: u32,
1104        current_size: IndentSize,
1105        new_size: IndentSize,
1106    ) -> Option<(Range<Point>, String)> {
1107        if new_size.kind != current_size.kind {
1108            Some((
1109                Point::new(row, 0)..Point::new(row, current_size.len),
1110                iter::repeat(new_size.char())
1111                    .take(new_size.len as usize)
1112                    .collect::<String>(),
1113            ))
1114        } else {
1115            match new_size.len.cmp(&current_size.len) {
1116                Ordering::Greater => {
1117                    let point = Point::new(row, 0);
1118                    Some((
1119                        point..point,
1120                        iter::repeat(new_size.char())
1121                            .take((new_size.len - current_size.len) as usize)
1122                            .collect::<String>(),
1123                    ))
1124                }
1125
1126                Ordering::Less => Some((
1127                    Point::new(row, 0)..Point::new(row, current_size.len - new_size.len),
1128                    String::new(),
1129                )),
1130
1131                Ordering::Equal => None,
1132            }
1133        }
1134    }
1135
1136    pub fn diff(&self, mut new_text: String, cx: &AppContext) -> Task<Diff> {
1137        let old_text = self.as_rope().clone();
1138        let base_version = self.version();
1139        cx.background().spawn(async move {
1140            let old_text = old_text.to_string();
1141            let line_ending = LineEnding::detect(&new_text);
1142            LineEnding::normalize(&mut new_text);
1143            let diff = TextDiff::from_chars(old_text.as_str(), new_text.as_str());
1144            let mut edits = Vec::new();
1145            let mut offset = 0;
1146            let empty: Arc<str> = "".into();
1147            for change in diff.iter_all_changes() {
1148                let value = change.value();
1149                let end_offset = offset + value.len();
1150                match change.tag() {
1151                    ChangeTag::Equal => {
1152                        offset = end_offset;
1153                    }
1154                    ChangeTag::Delete => {
1155                        edits.push((offset..end_offset, empty.clone()));
1156                        offset = end_offset;
1157                    }
1158                    ChangeTag::Insert => {
1159                        edits.push((offset..offset, value.into()));
1160                    }
1161                }
1162            }
1163            Diff {
1164                base_version,
1165                line_ending,
1166                edits,
1167            }
1168        })
1169    }
1170
1171    pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<&Transaction> {
1172        if self.version == diff.base_version {
1173            self.finalize_last_transaction();
1174            self.start_transaction();
1175            self.text.set_line_ending(diff.line_ending);
1176            self.edit(diff.edits, None, cx);
1177            if self.end_transaction(cx).is_some() {
1178                self.finalize_last_transaction()
1179            } else {
1180                None
1181            }
1182        } else {
1183            None
1184        }
1185    }
1186
1187    pub fn is_dirty(&self) -> bool {
1188        self.saved_version_fingerprint != self.as_rope().fingerprint()
1189            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1190    }
1191
1192    pub fn has_conflict(&self) -> bool {
1193        self.saved_version_fingerprint != self.as_rope().fingerprint()
1194            && self
1195                .file
1196                .as_ref()
1197                .map_or(false, |file| file.mtime() > self.saved_mtime)
1198    }
1199
1200    pub fn subscribe(&mut self) -> Subscription {
1201        self.text.subscribe()
1202    }
1203
1204    pub fn start_transaction(&mut self) -> Option<TransactionId> {
1205        self.start_transaction_at(Instant::now())
1206    }
1207
1208    pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1209        self.transaction_depth += 1;
1210        if self.was_dirty_before_starting_transaction.is_none() {
1211            self.was_dirty_before_starting_transaction = Some(self.is_dirty());
1212        }
1213        self.text.start_transaction_at(now)
1214    }
1215
1216    pub fn end_transaction(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1217        self.end_transaction_at(Instant::now(), cx)
1218    }
1219
1220    pub fn end_transaction_at(
1221        &mut self,
1222        now: Instant,
1223        cx: &mut ModelContext<Self>,
1224    ) -> Option<TransactionId> {
1225        assert!(self.transaction_depth > 0);
1226        self.transaction_depth -= 1;
1227        let was_dirty = if self.transaction_depth == 0 {
1228            self.was_dirty_before_starting_transaction.take().unwrap()
1229        } else {
1230            false
1231        };
1232        if let Some((transaction_id, start_version)) = self.text.end_transaction_at(now) {
1233            self.did_edit(&start_version, was_dirty, cx);
1234            Some(transaction_id)
1235        } else {
1236            None
1237        }
1238    }
1239
1240    pub fn push_transaction(&mut self, transaction: Transaction, now: Instant) {
1241        self.text.push_transaction(transaction, now);
1242    }
1243
1244    pub fn finalize_last_transaction(&mut self) -> Option<&Transaction> {
1245        self.text.finalize_last_transaction()
1246    }
1247
1248    pub fn group_until_transaction(&mut self, transaction_id: TransactionId) {
1249        self.text.group_until_transaction(transaction_id);
1250    }
1251
1252    pub fn forget_transaction(&mut self, transaction_id: TransactionId) {
1253        self.text.forget_transaction(transaction_id);
1254    }
1255
1256    pub fn wait_for_edits(
1257        &mut self,
1258        edit_ids: impl IntoIterator<Item = clock::Local>,
1259    ) -> impl Future<Output = ()> {
1260        self.text.wait_for_edits(edit_ids)
1261    }
1262
1263    pub fn wait_for_anchors<'a>(
1264        &mut self,
1265        anchors: impl IntoIterator<Item = &'a Anchor>,
1266    ) -> impl Future<Output = ()> {
1267        self.text.wait_for_anchors(anchors)
1268    }
1269
1270    pub fn wait_for_version(&mut self, version: clock::Global) -> impl Future<Output = ()> {
1271        self.text.wait_for_version(version)
1272    }
1273
1274    pub fn set_active_selections(
1275        &mut self,
1276        selections: Arc<[Selection<Anchor>]>,
1277        line_mode: bool,
1278        cursor_shape: CursorShape,
1279        cx: &mut ModelContext<Self>,
1280    ) {
1281        let lamport_timestamp = self.text.lamport_clock.tick();
1282        self.remote_selections.insert(
1283            self.text.replica_id(),
1284            SelectionSet {
1285                selections: selections.clone(),
1286                lamport_timestamp,
1287                line_mode,
1288                cursor_shape,
1289            },
1290        );
1291        self.send_operation(
1292            Operation::UpdateSelections {
1293                selections,
1294                line_mode,
1295                lamport_timestamp,
1296                cursor_shape,
1297            },
1298            cx,
1299        );
1300    }
1301
1302    pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
1303        self.set_active_selections(Arc::from([]), false, Default::default(), cx);
1304    }
1305
1306    pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
1307    where
1308        T: Into<Arc<str>>,
1309    {
1310        self.edit([(0..self.len(), text)], None, cx)
1311    }
1312
1313    pub fn edit<I, S, T>(
1314        &mut self,
1315        edits_iter: I,
1316        autoindent_mode: Option<AutoindentMode>,
1317        cx: &mut ModelContext<Self>,
1318    ) -> Option<clock::Local>
1319    where
1320        I: IntoIterator<Item = (Range<S>, T)>,
1321        S: ToOffset,
1322        T: Into<Arc<str>>,
1323    {
1324        // Skip invalid edits and coalesce contiguous ones.
1325        let mut edits: Vec<(Range<usize>, Arc<str>)> = Vec::new();
1326        for (range, new_text) in edits_iter {
1327            let mut range = range.start.to_offset(self)..range.end.to_offset(self);
1328            if range.start > range.end {
1329                mem::swap(&mut range.start, &mut range.end);
1330            }
1331            let new_text = new_text.into();
1332            if !new_text.is_empty() || !range.is_empty() {
1333                if let Some((prev_range, prev_text)) = edits.last_mut() {
1334                    if prev_range.end >= range.start {
1335                        prev_range.end = cmp::max(prev_range.end, range.end);
1336                        *prev_text = format!("{prev_text}{new_text}").into();
1337                    } else {
1338                        edits.push((range, new_text));
1339                    }
1340                } else {
1341                    edits.push((range, new_text));
1342                }
1343            }
1344        }
1345        if edits.is_empty() {
1346            return None;
1347        }
1348
1349        self.start_transaction();
1350        self.pending_autoindent.take();
1351        let autoindent_request = autoindent_mode
1352            .and_then(|mode| self.language.as_ref().map(|_| (self.snapshot(), mode)));
1353
1354        let edit_operation = self.text.edit(edits.iter().cloned());
1355        let edit_id = edit_operation.local_timestamp();
1356
1357        if let Some((before_edit, mode)) = autoindent_request {
1358            let mut delta = 0isize;
1359            let entries = edits
1360                .into_iter()
1361                .enumerate()
1362                .zip(&edit_operation.as_edit().unwrap().new_text)
1363                .map(|((ix, (range, _)), new_text)| {
1364                    let new_text_len = new_text.len();
1365                    let old_start = range.start.to_point(&before_edit);
1366                    let new_start = (delta + range.start as isize) as usize;
1367                    delta += new_text_len as isize - (range.end as isize - range.start as isize);
1368
1369                    let mut range_of_insertion_to_indent = 0..new_text_len;
1370                    let mut first_line_is_new = false;
1371                    let mut original_indent_column = None;
1372
1373                    // When inserting an entire line at the beginning of an existing line,
1374                    // treat the insertion as new.
1375                    if new_text.contains('\n')
1376                        && old_start.column <= before_edit.indent_size_for_line(old_start.row).len
1377                    {
1378                        first_line_is_new = true;
1379                    }
1380
1381                    // When inserting text starting with a newline, avoid auto-indenting the
1382                    // previous line.
1383                    if new_text.starts_with('\n') {
1384                        range_of_insertion_to_indent.start += 1;
1385                        first_line_is_new = true;
1386                    }
1387
1388                    // Avoid auto-indenting after the insertion.
1389                    if let AutoindentMode::Block {
1390                        original_indent_columns,
1391                    } = &mode
1392                    {
1393                        original_indent_column =
1394                            Some(original_indent_columns.get(ix).copied().unwrap_or_else(|| {
1395                                indent_size_for_text(
1396                                    new_text[range_of_insertion_to_indent.clone()].chars(),
1397                                )
1398                                .len
1399                            }));
1400                        if new_text[range_of_insertion_to_indent.clone()].ends_with('\n') {
1401                            range_of_insertion_to_indent.end -= 1;
1402                        }
1403                    }
1404
1405                    AutoindentRequestEntry {
1406                        first_line_is_new,
1407                        original_indent_column,
1408                        indent_size: before_edit.language_indent_size_at(range.start, cx),
1409                        range: self.anchor_before(new_start + range_of_insertion_to_indent.start)
1410                            ..self.anchor_after(new_start + range_of_insertion_to_indent.end),
1411                    }
1412                })
1413                .collect();
1414
1415            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1416                before_edit,
1417                entries,
1418                is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
1419            }));
1420        }
1421
1422        self.end_transaction(cx);
1423        self.send_operation(Operation::Buffer(edit_operation), cx);
1424        Some(edit_id)
1425    }
1426
1427    fn did_edit(
1428        &mut self,
1429        old_version: &clock::Global,
1430        was_dirty: bool,
1431        cx: &mut ModelContext<Self>,
1432    ) {
1433        if self.edits_since::<usize>(old_version).next().is_none() {
1434            return;
1435        }
1436
1437        self.reparse(cx);
1438
1439        cx.emit(Event::Edited);
1440        if was_dirty != self.is_dirty() {
1441            cx.emit(Event::DirtyChanged);
1442        }
1443        cx.notify();
1444    }
1445
1446    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1447        &mut self,
1448        ops: I,
1449        cx: &mut ModelContext<Self>,
1450    ) -> Result<()> {
1451        self.pending_autoindent.take();
1452        let was_dirty = self.is_dirty();
1453        let old_version = self.version.clone();
1454        let mut deferred_ops = Vec::new();
1455        let buffer_ops = ops
1456            .into_iter()
1457            .filter_map(|op| match op {
1458                Operation::Buffer(op) => Some(op),
1459                _ => {
1460                    if self.can_apply_op(&op) {
1461                        self.apply_op(op, cx);
1462                    } else {
1463                        deferred_ops.push(op);
1464                    }
1465                    None
1466                }
1467            })
1468            .collect::<Vec<_>>();
1469        self.text.apply_ops(buffer_ops)?;
1470        self.deferred_ops.insert(deferred_ops);
1471        self.flush_deferred_ops(cx);
1472        self.did_edit(&old_version, was_dirty, cx);
1473        // Notify independently of whether the buffer was edited as the operations could include a
1474        // selection update.
1475        cx.notify();
1476        Ok(())
1477    }
1478
1479    fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1480        let mut deferred_ops = Vec::new();
1481        for op in self.deferred_ops.drain().iter().cloned() {
1482            if self.can_apply_op(&op) {
1483                self.apply_op(op, cx);
1484            } else {
1485                deferred_ops.push(op);
1486            }
1487        }
1488        self.deferred_ops.insert(deferred_ops);
1489    }
1490
1491    fn can_apply_op(&self, operation: &Operation) -> bool {
1492        match operation {
1493            Operation::Buffer(_) => {
1494                unreachable!("buffer operations should never be applied at this layer")
1495            }
1496            Operation::UpdateDiagnostics {
1497                diagnostics: diagnostic_set,
1498                ..
1499            } => diagnostic_set.iter().all(|diagnostic| {
1500                self.text.can_resolve(&diagnostic.range.start)
1501                    && self.text.can_resolve(&diagnostic.range.end)
1502            }),
1503            Operation::UpdateSelections { selections, .. } => selections
1504                .iter()
1505                .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1506            Operation::UpdateCompletionTriggers { .. } => true,
1507        }
1508    }
1509
1510    fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1511        match operation {
1512            Operation::Buffer(_) => {
1513                unreachable!("buffer operations should never be applied at this layer")
1514            }
1515            Operation::UpdateDiagnostics {
1516                diagnostics: diagnostic_set,
1517                lamport_timestamp,
1518            } => {
1519                let snapshot = self.snapshot();
1520                self.apply_diagnostic_update(
1521                    DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1522                    lamport_timestamp,
1523                    cx,
1524                );
1525            }
1526            Operation::UpdateSelections {
1527                selections,
1528                lamport_timestamp,
1529                line_mode,
1530                cursor_shape,
1531            } => {
1532                if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1533                    if set.lamport_timestamp > lamport_timestamp {
1534                        return;
1535                    }
1536                }
1537
1538                self.remote_selections.insert(
1539                    lamport_timestamp.replica_id,
1540                    SelectionSet {
1541                        selections,
1542                        lamport_timestamp,
1543                        line_mode,
1544                        cursor_shape,
1545                    },
1546                );
1547                self.text.lamport_clock.observe(lamport_timestamp);
1548                self.selections_update_count += 1;
1549            }
1550            Operation::UpdateCompletionTriggers {
1551                triggers,
1552                lamport_timestamp,
1553            } => {
1554                self.completion_triggers = triggers;
1555                self.text.lamport_clock.observe(lamport_timestamp);
1556            }
1557        }
1558    }
1559
1560    fn apply_diagnostic_update(
1561        &mut self,
1562        diagnostics: DiagnosticSet,
1563        lamport_timestamp: clock::Lamport,
1564        cx: &mut ModelContext<Self>,
1565    ) {
1566        if lamport_timestamp > self.diagnostics_timestamp {
1567            self.diagnostics = diagnostics;
1568            self.diagnostics_timestamp = lamport_timestamp;
1569            self.diagnostics_update_count += 1;
1570            self.text.lamport_clock.observe(lamport_timestamp);
1571            cx.notify();
1572            cx.emit(Event::DiagnosticsUpdated);
1573        }
1574    }
1575
1576    fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1577        cx.emit(Event::Operation(operation));
1578    }
1579
1580    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1581        self.remote_selections.remove(&replica_id);
1582        cx.notify();
1583    }
1584
1585    pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1586        let was_dirty = self.is_dirty();
1587        let old_version = self.version.clone();
1588
1589        if let Some((transaction_id, operation)) = self.text.undo() {
1590            self.send_operation(Operation::Buffer(operation), cx);
1591            self.did_edit(&old_version, was_dirty, cx);
1592            Some(transaction_id)
1593        } else {
1594            None
1595        }
1596    }
1597
1598    pub fn undo_to_transaction(
1599        &mut self,
1600        transaction_id: TransactionId,
1601        cx: &mut ModelContext<Self>,
1602    ) -> bool {
1603        let was_dirty = self.is_dirty();
1604        let old_version = self.version.clone();
1605
1606        let operations = self.text.undo_to_transaction(transaction_id);
1607        let undone = !operations.is_empty();
1608        for operation in operations {
1609            self.send_operation(Operation::Buffer(operation), cx);
1610        }
1611        if undone {
1612            self.did_edit(&old_version, was_dirty, cx)
1613        }
1614        undone
1615    }
1616
1617    pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1618        let was_dirty = self.is_dirty();
1619        let old_version = self.version.clone();
1620
1621        if let Some((transaction_id, operation)) = self.text.redo() {
1622            self.send_operation(Operation::Buffer(operation), cx);
1623            self.did_edit(&old_version, was_dirty, cx);
1624            Some(transaction_id)
1625        } else {
1626            None
1627        }
1628    }
1629
1630    pub fn redo_to_transaction(
1631        &mut self,
1632        transaction_id: TransactionId,
1633        cx: &mut ModelContext<Self>,
1634    ) -> bool {
1635        let was_dirty = self.is_dirty();
1636        let old_version = self.version.clone();
1637
1638        let operations = self.text.redo_to_transaction(transaction_id);
1639        let redone = !operations.is_empty();
1640        for operation in operations {
1641            self.send_operation(Operation::Buffer(operation), cx);
1642        }
1643        if redone {
1644            self.did_edit(&old_version, was_dirty, cx)
1645        }
1646        redone
1647    }
1648
1649    pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1650        self.completion_triggers = triggers.clone();
1651        self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1652        self.send_operation(
1653            Operation::UpdateCompletionTriggers {
1654                triggers,
1655                lamport_timestamp: self.completion_triggers_timestamp,
1656            },
1657            cx,
1658        );
1659        cx.notify();
1660    }
1661
1662    pub fn completion_triggers(&self) -> &[String] {
1663        &self.completion_triggers
1664    }
1665}
1666
1667#[cfg(any(test, feature = "test-support"))]
1668impl Buffer {
1669    pub fn set_group_interval(&mut self, group_interval: Duration) {
1670        self.text.set_group_interval(group_interval);
1671    }
1672
1673    pub fn randomly_edit<T>(
1674        &mut self,
1675        rng: &mut T,
1676        old_range_count: usize,
1677        cx: &mut ModelContext<Self>,
1678    ) where
1679        T: rand::Rng,
1680    {
1681        let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1682        let mut last_end = None;
1683        for _ in 0..old_range_count {
1684            if last_end.map_or(false, |last_end| last_end >= self.len()) {
1685                break;
1686            }
1687
1688            let new_start = last_end.map_or(0, |last_end| last_end + 1);
1689            let mut range = self.random_byte_range(new_start, rng);
1690            if rng.gen_bool(0.2) {
1691                mem::swap(&mut range.start, &mut range.end);
1692            }
1693            last_end = Some(range.end);
1694
1695            let new_text_len = rng.gen_range(0..10);
1696            let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1697
1698            edits.push((range, new_text));
1699        }
1700        log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1701        self.edit(edits, None, cx);
1702    }
1703
1704    pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1705        let was_dirty = self.is_dirty();
1706        let old_version = self.version.clone();
1707
1708        let ops = self.text.randomly_undo_redo(rng);
1709        if !ops.is_empty() {
1710            for op in ops {
1711                self.send_operation(Operation::Buffer(op), cx);
1712                self.did_edit(&old_version, was_dirty, cx);
1713            }
1714        }
1715    }
1716}
1717
1718impl Entity for Buffer {
1719    type Event = Event;
1720}
1721
1722impl Deref for Buffer {
1723    type Target = TextBuffer;
1724
1725    fn deref(&self) -> &Self::Target {
1726        &self.text
1727    }
1728}
1729
1730impl BufferSnapshot {
1731    pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1732        indent_size_for_line(self, row)
1733    }
1734
1735    pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1736        let language_name = self.language_at(position).map(|language| language.name());
1737        let settings = cx.global::<Settings>();
1738        if settings.hard_tabs(language_name.as_deref()) {
1739            IndentSize::tab()
1740        } else {
1741            IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
1742        }
1743    }
1744
1745    pub fn suggested_indents(
1746        &self,
1747        rows: impl Iterator<Item = u32>,
1748        single_indent_size: IndentSize,
1749    ) -> BTreeMap<u32, IndentSize> {
1750        let mut result = BTreeMap::new();
1751
1752        for row_range in contiguous_ranges(rows, 10) {
1753            let suggestions = match self.suggest_autoindents(row_range.clone()) {
1754                Some(suggestions) => suggestions,
1755                _ => break,
1756            };
1757
1758            for (row, suggestion) in row_range.zip(suggestions) {
1759                let indent_size = if let Some(suggestion) = suggestion {
1760                    result
1761                        .get(&suggestion.basis_row)
1762                        .copied()
1763                        .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1764                        .with_delta(suggestion.delta, single_indent_size)
1765                } else {
1766                    self.indent_size_for_line(row)
1767                };
1768
1769                result.insert(row, indent_size);
1770            }
1771        }
1772
1773        result
1774    }
1775
1776    fn suggest_autoindents(
1777        &self,
1778        row_range: Range<u32>,
1779    ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1780        let config = &self.language.as_ref()?.config;
1781        let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1782
1783        // Find the suggested indentation ranges based on the syntax tree.
1784        let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1785        let end = Point::new(row_range.end, 0);
1786        let range = (start..end).to_offset(&self.text);
1787        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1788            Some(&grammar.indents_config.as_ref()?.query)
1789        });
1790        let indent_configs = matches
1791            .grammars()
1792            .iter()
1793            .map(|grammar| grammar.indents_config.as_ref().unwrap())
1794            .collect::<Vec<_>>();
1795
1796        let mut indent_ranges = Vec::<Range<Point>>::new();
1797        let mut outdent_positions = Vec::<Point>::new();
1798        while let Some(mat) = matches.peek() {
1799            let mut start: Option<Point> = None;
1800            let mut end: Option<Point> = None;
1801
1802            let config = &indent_configs[mat.grammar_index];
1803            for capture in mat.captures {
1804                if capture.index == config.indent_capture_ix {
1805                    start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1806                    end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1807                } else if Some(capture.index) == config.start_capture_ix {
1808                    start = Some(Point::from_ts_point(capture.node.end_position()));
1809                } else if Some(capture.index) == config.end_capture_ix {
1810                    end = Some(Point::from_ts_point(capture.node.start_position()));
1811                } else if Some(capture.index) == config.outdent_capture_ix {
1812                    outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1813                }
1814            }
1815
1816            matches.advance();
1817            if let Some((start, end)) = start.zip(end) {
1818                if start.row == end.row {
1819                    continue;
1820                }
1821
1822                let range = start..end;
1823                match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1824                    Err(ix) => indent_ranges.insert(ix, range),
1825                    Ok(ix) => {
1826                        let prev_range = &mut indent_ranges[ix];
1827                        prev_range.end = prev_range.end.max(range.end);
1828                    }
1829                }
1830            }
1831        }
1832
1833        let mut error_ranges = Vec::<Range<Point>>::new();
1834        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1835            Some(&grammar.error_query)
1836        });
1837        while let Some(mat) = matches.peek() {
1838            let node = mat.captures[0].node;
1839            let start = Point::from_ts_point(node.start_position());
1840            let end = Point::from_ts_point(node.end_position());
1841            let range = start..end;
1842            let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
1843                Ok(ix) | Err(ix) => ix,
1844            };
1845            let mut end_ix = ix;
1846            while let Some(existing_range) = error_ranges.get(end_ix) {
1847                if existing_range.end < end {
1848                    end_ix += 1;
1849                } else {
1850                    break;
1851                }
1852            }
1853            error_ranges.splice(ix..end_ix, [range]);
1854            matches.advance();
1855        }
1856
1857        outdent_positions.sort();
1858        for outdent_position in outdent_positions {
1859            // find the innermost indent range containing this outdent_position
1860            // set its end to the outdent position
1861            if let Some(range_to_truncate) = indent_ranges
1862                .iter_mut()
1863                .filter(|indent_range| indent_range.contains(&outdent_position))
1864                .last()
1865            {
1866                range_to_truncate.end = outdent_position;
1867            }
1868        }
1869
1870        // Find the suggested indentation increases and decreased based on regexes.
1871        let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1872        self.for_each_line(
1873            Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1874                ..Point::new(row_range.end, 0),
1875            |row, line| {
1876                if config
1877                    .decrease_indent_pattern
1878                    .as_ref()
1879                    .map_or(false, |regex| regex.is_match(line))
1880                {
1881                    indent_change_rows.push((row, Ordering::Less));
1882                }
1883                if config
1884                    .increase_indent_pattern
1885                    .as_ref()
1886                    .map_or(false, |regex| regex.is_match(line))
1887                {
1888                    indent_change_rows.push((row + 1, Ordering::Greater));
1889                }
1890            },
1891        );
1892
1893        let mut indent_changes = indent_change_rows.into_iter().peekable();
1894        let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1895            prev_non_blank_row.unwrap_or(0)
1896        } else {
1897            row_range.start.saturating_sub(1)
1898        };
1899        let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1900        Some(row_range.map(move |row| {
1901            let row_start = Point::new(row, self.indent_size_for_line(row).len);
1902
1903            let mut indent_from_prev_row = false;
1904            let mut outdent_from_prev_row = false;
1905            let mut outdent_to_row = u32::MAX;
1906
1907            while let Some((indent_row, delta)) = indent_changes.peek() {
1908                match indent_row.cmp(&row) {
1909                    Ordering::Equal => match delta {
1910                        Ordering::Less => outdent_from_prev_row = true,
1911                        Ordering::Greater => indent_from_prev_row = true,
1912                        _ => {}
1913                    },
1914
1915                    Ordering::Greater => break,
1916                    Ordering::Less => {}
1917                }
1918
1919                indent_changes.next();
1920            }
1921
1922            for range in &indent_ranges {
1923                if range.start.row >= row {
1924                    break;
1925                }
1926                if range.start.row == prev_row && range.end > row_start {
1927                    indent_from_prev_row = true;
1928                }
1929                if range.end > prev_row_start && range.end <= row_start {
1930                    outdent_to_row = outdent_to_row.min(range.start.row);
1931                }
1932            }
1933
1934            let within_error = error_ranges
1935                .iter()
1936                .any(|e| e.start.row < row && e.end > row_start);
1937
1938            let suggestion = if outdent_to_row == prev_row
1939                || (outdent_from_prev_row && indent_from_prev_row)
1940            {
1941                Some(IndentSuggestion {
1942                    basis_row: prev_row,
1943                    delta: Ordering::Equal,
1944                    within_error,
1945                })
1946            } else if indent_from_prev_row {
1947                Some(IndentSuggestion {
1948                    basis_row: prev_row,
1949                    delta: Ordering::Greater,
1950                    within_error,
1951                })
1952            } else if outdent_to_row < prev_row {
1953                Some(IndentSuggestion {
1954                    basis_row: outdent_to_row,
1955                    delta: Ordering::Equal,
1956                    within_error,
1957                })
1958            } else if outdent_from_prev_row {
1959                Some(IndentSuggestion {
1960                    basis_row: prev_row,
1961                    delta: Ordering::Less,
1962                    within_error,
1963                })
1964            } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
1965            {
1966                Some(IndentSuggestion {
1967                    basis_row: prev_row,
1968                    delta: Ordering::Equal,
1969                    within_error,
1970                })
1971            } else {
1972                None
1973            };
1974
1975            prev_row = row;
1976            prev_row_start = row_start;
1977            suggestion
1978        }))
1979    }
1980
1981    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1982        while row > 0 {
1983            row -= 1;
1984            if !self.is_line_blank(row) {
1985                return Some(row);
1986            }
1987        }
1988        None
1989    }
1990
1991    pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
1992        let range = range.start.to_offset(self)..range.end.to_offset(self);
1993
1994        let mut syntax = None;
1995        let mut diagnostic_endpoints = Vec::new();
1996        if language_aware {
1997            let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
1998                grammar.highlights_query.as_ref()
1999            });
2000            let highlight_maps = captures
2001                .grammars()
2002                .into_iter()
2003                .map(|grammar| grammar.highlight_map())
2004                .collect();
2005            syntax = Some((captures, highlight_maps));
2006            for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
2007                diagnostic_endpoints.push(DiagnosticEndpoint {
2008                    offset: entry.range.start,
2009                    is_start: true,
2010                    severity: entry.diagnostic.severity,
2011                    is_unnecessary: entry.diagnostic.is_unnecessary,
2012                });
2013                diagnostic_endpoints.push(DiagnosticEndpoint {
2014                    offset: entry.range.end,
2015                    is_start: false,
2016                    severity: entry.diagnostic.severity,
2017                    is_unnecessary: entry.diagnostic.is_unnecessary,
2018                });
2019            }
2020            diagnostic_endpoints
2021                .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2022        }
2023
2024        BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
2025    }
2026
2027    pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2028        let mut line = String::new();
2029        let mut row = range.start.row;
2030        for chunk in self
2031            .as_rope()
2032            .chunks_in_range(range.to_offset(self))
2033            .chain(["\n"])
2034        {
2035            for (newline_ix, text) in chunk.split('\n').enumerate() {
2036                if newline_ix > 0 {
2037                    callback(row, &line);
2038                    row += 1;
2039                    line.clear();
2040                }
2041                line.push_str(text);
2042            }
2043        }
2044    }
2045
2046    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2047        let offset = position.to_offset(self);
2048        self.syntax
2049            .layers_for_range(offset..offset, &self.text)
2050            .filter(|l| l.node.end_byte() > offset)
2051            .last()
2052            .map(|info| info.language)
2053            .or(self.language.as_ref())
2054    }
2055
2056    pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2057        let offset = position.to_offset(self);
2058
2059        if let Some(layer_info) = self
2060            .syntax
2061            .layers_for_range(offset..offset, &self.text)
2062            .filter(|l| l.node.end_byte() > offset)
2063            .last()
2064        {
2065            Some(LanguageScope {
2066                language: layer_info.language.clone(),
2067                override_id: layer_info.override_id(offset, &self.text),
2068            })
2069        } else {
2070            self.language.clone().map(|language| LanguageScope {
2071                language,
2072                override_id: None,
2073            })
2074        }
2075    }
2076
2077    pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2078        let mut start = start.to_offset(self);
2079        let mut end = start;
2080        let mut next_chars = self.chars_at(start).peekable();
2081        let mut prev_chars = self.reversed_chars_at(start).peekable();
2082        let word_kind = cmp::max(
2083            prev_chars.peek().copied().map(char_kind),
2084            next_chars.peek().copied().map(char_kind),
2085        );
2086
2087        for ch in prev_chars {
2088            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2089                start -= ch.len_utf8();
2090            } else {
2091                break;
2092            }
2093        }
2094
2095        for ch in next_chars {
2096            if Some(char_kind(ch)) == word_kind && ch != '\n' {
2097                end += ch.len_utf8();
2098            } else {
2099                break;
2100            }
2101        }
2102
2103        (start..end, word_kind)
2104    }
2105
2106    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2107        let range = range.start.to_offset(self)..range.end.to_offset(self);
2108        let mut result: Option<Range<usize>> = None;
2109        'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2110            let mut cursor = layer.node.walk();
2111
2112            // Descend to the first leaf that touches the start of the range,
2113            // and if the range is non-empty, extends beyond the start.
2114            while cursor.goto_first_child_for_byte(range.start).is_some() {
2115                if !range.is_empty() && cursor.node().end_byte() == range.start {
2116                    cursor.goto_next_sibling();
2117                }
2118            }
2119
2120            // Ascend to the smallest ancestor that strictly contains the range.
2121            loop {
2122                let node_range = cursor.node().byte_range();
2123                if node_range.start <= range.start
2124                    && node_range.end >= range.end
2125                    && node_range.len() > range.len()
2126                {
2127                    break;
2128                }
2129                if !cursor.goto_parent() {
2130                    continue 'outer;
2131                }
2132            }
2133
2134            let left_node = cursor.node();
2135            let mut layer_result = left_node.byte_range();
2136
2137            // For an empty range, try to find another node immediately to the right of the range.
2138            if left_node.end_byte() == range.start {
2139                let mut right_node = None;
2140                while !cursor.goto_next_sibling() {
2141                    if !cursor.goto_parent() {
2142                        break;
2143                    }
2144                }
2145
2146                while cursor.node().start_byte() == range.start {
2147                    right_node = Some(cursor.node());
2148                    if !cursor.goto_first_child() {
2149                        break;
2150                    }
2151                }
2152
2153                // If there is a candidate node on both sides of the (empty) range, then
2154                // decide between the two by favoring a named node over an anonymous token.
2155                // If both nodes are the same in that regard, favor the right one.
2156                if let Some(right_node) = right_node {
2157                    if right_node.is_named() || !left_node.is_named() {
2158                        layer_result = right_node.byte_range();
2159                    }
2160                }
2161            }
2162
2163            if let Some(previous_result) = &result {
2164                if previous_result.len() < layer_result.len() {
2165                    continue;
2166                }
2167            }
2168            result = Some(layer_result);
2169        }
2170
2171        result
2172    }
2173
2174    pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2175        self.outline_items_containing(0..self.len(), theme)
2176            .map(Outline::new)
2177    }
2178
2179    pub fn symbols_containing<T: ToOffset>(
2180        &self,
2181        position: T,
2182        theme: Option<&SyntaxTheme>,
2183    ) -> Option<Vec<OutlineItem<Anchor>>> {
2184        let position = position.to_offset(self);
2185        let mut items = self.outline_items_containing(
2186            position.saturating_sub(1)..self.len().min(position + 1),
2187            theme,
2188        )?;
2189        let mut prev_depth = None;
2190        items.retain(|item| {
2191            let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2192            prev_depth = Some(item.depth);
2193            result
2194        });
2195        Some(items)
2196    }
2197
2198    fn outline_items_containing(
2199        &self,
2200        range: Range<usize>,
2201        theme: Option<&SyntaxTheme>,
2202    ) -> Option<Vec<OutlineItem<Anchor>>> {
2203        let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2204            grammar.outline_config.as_ref().map(|c| &c.query)
2205        });
2206        let configs = matches
2207            .grammars()
2208            .iter()
2209            .map(|g| g.outline_config.as_ref().unwrap())
2210            .collect::<Vec<_>>();
2211
2212        let mut chunks = self.chunks(0..self.len(), true);
2213        let mut stack = Vec::<Range<usize>>::new();
2214        let mut items = Vec::new();
2215        while let Some(mat) = matches.peek() {
2216            let config = &configs[mat.grammar_index];
2217            let item_node = mat.captures.iter().find_map(|cap| {
2218                if cap.index == config.item_capture_ix {
2219                    Some(cap.node)
2220                } else {
2221                    None
2222                }
2223            })?;
2224
2225            let item_range = item_node.byte_range();
2226            if item_range.end < range.start || item_range.start > range.end {
2227                matches.advance();
2228                continue;
2229            }
2230
2231            let mut text = String::new();
2232            let mut name_ranges = Vec::new();
2233            let mut highlight_ranges = Vec::new();
2234            for capture in mat.captures {
2235                let node_is_name;
2236                if capture.index == config.name_capture_ix {
2237                    node_is_name = true;
2238                } else if Some(capture.index) == config.context_capture_ix {
2239                    node_is_name = false;
2240                } else {
2241                    continue;
2242                }
2243
2244                let mut range = capture.node.start_byte()..capture.node.end_byte();
2245                let start = capture.node.start_position();
2246                if capture.node.end_position().row > start.row {
2247                    range.end =
2248                        range.start + self.line_len(start.row as u32) as usize - start.column;
2249                }
2250
2251                if !text.is_empty() {
2252                    text.push(' ');
2253                }
2254                if node_is_name {
2255                    let mut start = text.len();
2256                    let end = start + range.len();
2257
2258                    // When multiple names are captured, then the matcheable text
2259                    // includes the whitespace in between the names.
2260                    if !name_ranges.is_empty() {
2261                        start -= 1;
2262                    }
2263
2264                    name_ranges.push(start..end);
2265                }
2266
2267                let mut offset = range.start;
2268                chunks.seek(offset);
2269                for mut chunk in chunks.by_ref() {
2270                    if chunk.text.len() > range.end - offset {
2271                        chunk.text = &chunk.text[0..(range.end - offset)];
2272                        offset = range.end;
2273                    } else {
2274                        offset += chunk.text.len();
2275                    }
2276                    let style = chunk
2277                        .syntax_highlight_id
2278                        .zip(theme)
2279                        .and_then(|(highlight, theme)| highlight.style(theme));
2280                    if let Some(style) = style {
2281                        let start = text.len();
2282                        let end = start + chunk.text.len();
2283                        highlight_ranges.push((start..end, style));
2284                    }
2285                    text.push_str(chunk.text);
2286                    if offset >= range.end {
2287                        break;
2288                    }
2289                }
2290            }
2291
2292            matches.advance();
2293            while stack.last().map_or(false, |prev_range| {
2294                prev_range.start > item_range.start || prev_range.end < item_range.end
2295            }) {
2296                stack.pop();
2297            }
2298            stack.push(item_range.clone());
2299
2300            items.push(OutlineItem {
2301                depth: stack.len() - 1,
2302                range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2303                text,
2304                highlight_ranges,
2305                name_ranges,
2306            })
2307        }
2308        Some(items)
2309    }
2310
2311    pub fn enclosing_bracket_ranges<T: ToOffset>(
2312        &self,
2313        range: Range<T>,
2314    ) -> Option<(Range<usize>, Range<usize>)> {
2315        // Find bracket pairs that *inclusively* contain the given range.
2316        let range = range.start.to_offset(self)..range.end.to_offset(self);
2317        let mut matches = self.syntax.matches(
2318            range.start.saturating_sub(1)..self.len().min(range.end + 1),
2319            &self.text,
2320            |grammar| grammar.brackets_config.as_ref().map(|c| &c.query),
2321        );
2322        let configs = matches
2323            .grammars()
2324            .iter()
2325            .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2326            .collect::<Vec<_>>();
2327
2328        // Get the ranges of the innermost pair of brackets.
2329        let mut result: Option<(Range<usize>, Range<usize>)> = None;
2330        while let Some(mat) = matches.peek() {
2331            let mut open = None;
2332            let mut close = None;
2333            let config = &configs[mat.grammar_index];
2334            for capture in mat.captures {
2335                if capture.index == config.open_capture_ix {
2336                    open = Some(capture.node.byte_range());
2337                } else if capture.index == config.close_capture_ix {
2338                    close = Some(capture.node.byte_range());
2339                }
2340            }
2341
2342            matches.advance();
2343
2344            let Some((open, close)) = open.zip(close) else { continue };
2345            if open.start > range.start || close.end < range.end {
2346                continue;
2347            }
2348            let len = close.end - open.start;
2349
2350            if let Some((existing_open, existing_close)) = &result {
2351                let existing_len = existing_close.end - existing_open.start;
2352                if len > existing_len {
2353                    continue;
2354                }
2355            }
2356
2357            result = Some((open, close));
2358        }
2359
2360        result
2361    }
2362
2363    #[allow(clippy::type_complexity)]
2364    pub fn remote_selections_in_range(
2365        &self,
2366        range: Range<Anchor>,
2367    ) -> impl Iterator<
2368        Item = (
2369            ReplicaId,
2370            bool,
2371            CursorShape,
2372            impl Iterator<Item = &Selection<Anchor>> + '_,
2373        ),
2374    > + '_ {
2375        self.remote_selections
2376            .iter()
2377            .filter(|(replica_id, set)| {
2378                **replica_id != self.text.replica_id() && !set.selections.is_empty()
2379            })
2380            .map(move |(replica_id, set)| {
2381                let start_ix = match set.selections.binary_search_by(|probe| {
2382                    probe.end.cmp(&range.start, self).then(Ordering::Greater)
2383                }) {
2384                    Ok(ix) | Err(ix) => ix,
2385                };
2386                let end_ix = match set.selections.binary_search_by(|probe| {
2387                    probe.start.cmp(&range.end, self).then(Ordering::Less)
2388                }) {
2389                    Ok(ix) | Err(ix) => ix,
2390                };
2391
2392                (
2393                    *replica_id,
2394                    set.line_mode,
2395                    set.cursor_shape,
2396                    set.selections[start_ix..end_ix].iter(),
2397                )
2398            })
2399    }
2400
2401    pub fn git_diff_hunks_in_row_range<'a>(
2402        &'a self,
2403        range: Range<u32>,
2404        reversed: bool,
2405    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2406        self.git_diff.hunks_in_row_range(range, self, reversed)
2407    }
2408
2409    pub fn git_diff_hunks_intersecting_range<'a>(
2410        &'a self,
2411        range: Range<Anchor>,
2412        reversed: bool,
2413    ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2414        self.git_diff
2415            .hunks_intersecting_range(range, self, reversed)
2416    }
2417
2418    pub fn diagnostics_in_range<'a, T, O>(
2419        &'a self,
2420        search_range: Range<T>,
2421        reversed: bool,
2422    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2423    where
2424        T: 'a + Clone + ToOffset,
2425        O: 'a + FromAnchor,
2426    {
2427        self.diagnostics.range(search_range, self, true, reversed)
2428    }
2429
2430    pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
2431        let mut groups = Vec::new();
2432        self.diagnostics.groups(&mut groups, self);
2433        groups
2434    }
2435
2436    pub fn diagnostic_group<'a, O>(
2437        &'a self,
2438        group_id: usize,
2439    ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2440    where
2441        O: 'a + FromAnchor,
2442    {
2443        self.diagnostics.group(group_id, self)
2444    }
2445
2446    pub fn diagnostics_update_count(&self) -> usize {
2447        self.diagnostics_update_count
2448    }
2449
2450    pub fn parse_count(&self) -> usize {
2451        self.parse_count
2452    }
2453
2454    pub fn selections_update_count(&self) -> usize {
2455        self.selections_update_count
2456    }
2457
2458    pub fn file(&self) -> Option<&Arc<dyn File>> {
2459        self.file.as_ref()
2460    }
2461
2462    pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2463        if let Some(file) = self.file() {
2464            if file.path().file_name().is_none() || include_root {
2465                Some(file.full_path(cx))
2466            } else {
2467                Some(file.path().to_path_buf())
2468            }
2469        } else {
2470            None
2471        }
2472    }
2473
2474    pub fn file_update_count(&self) -> usize {
2475        self.file_update_count
2476    }
2477
2478    pub fn git_diff_update_count(&self) -> usize {
2479        self.git_diff_update_count
2480    }
2481}
2482
2483fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2484    indent_size_for_text(text.chars_at(Point::new(row, 0)))
2485}
2486
2487pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2488    let mut result = IndentSize::spaces(0);
2489    for c in text {
2490        let kind = match c {
2491            ' ' => IndentKind::Space,
2492            '\t' => IndentKind::Tab,
2493            _ => break,
2494        };
2495        if result.len == 0 {
2496            result.kind = kind;
2497        }
2498        result.len += 1;
2499    }
2500    result
2501}
2502
2503impl Clone for BufferSnapshot {
2504    fn clone(&self) -> Self {
2505        Self {
2506            text: self.text.clone(),
2507            git_diff: self.git_diff.clone(),
2508            syntax: self.syntax.clone(),
2509            file: self.file.clone(),
2510            remote_selections: self.remote_selections.clone(),
2511            diagnostics: self.diagnostics.clone(),
2512            selections_update_count: self.selections_update_count,
2513            diagnostics_update_count: self.diagnostics_update_count,
2514            file_update_count: self.file_update_count,
2515            git_diff_update_count: self.git_diff_update_count,
2516            language: self.language.clone(),
2517            parse_count: self.parse_count,
2518        }
2519    }
2520}
2521
2522impl Deref for BufferSnapshot {
2523    type Target = text::BufferSnapshot;
2524
2525    fn deref(&self) -> &Self::Target {
2526        &self.text
2527    }
2528}
2529
2530unsafe impl<'a> Send for BufferChunks<'a> {}
2531
2532impl<'a> BufferChunks<'a> {
2533    pub(crate) fn new(
2534        text: &'a Rope,
2535        range: Range<usize>,
2536        syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2537        diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2538    ) -> Self {
2539        let mut highlights = None;
2540        if let Some((captures, highlight_maps)) = syntax {
2541            highlights = Some(BufferChunkHighlights {
2542                captures,
2543                next_capture: None,
2544                stack: Default::default(),
2545                highlight_maps,
2546            })
2547        }
2548
2549        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2550        let chunks = text.chunks_in_range(range.clone());
2551
2552        BufferChunks {
2553            range,
2554            chunks,
2555            diagnostic_endpoints,
2556            error_depth: 0,
2557            warning_depth: 0,
2558            information_depth: 0,
2559            hint_depth: 0,
2560            unnecessary_depth: 0,
2561            highlights,
2562        }
2563    }
2564
2565    pub fn seek(&mut self, offset: usize) {
2566        self.range.start = offset;
2567        self.chunks.seek(self.range.start);
2568        if let Some(highlights) = self.highlights.as_mut() {
2569            highlights
2570                .stack
2571                .retain(|(end_offset, _)| *end_offset > offset);
2572            if let Some(capture) = &highlights.next_capture {
2573                if offset >= capture.node.start_byte() {
2574                    let next_capture_end = capture.node.end_byte();
2575                    if offset < next_capture_end {
2576                        highlights.stack.push((
2577                            next_capture_end,
2578                            highlights.highlight_maps[capture.grammar_index].get(capture.index),
2579                        ));
2580                    }
2581                    highlights.next_capture.take();
2582                }
2583            }
2584            highlights.captures.set_byte_range(self.range.clone());
2585        }
2586    }
2587
2588    pub fn offset(&self) -> usize {
2589        self.range.start
2590    }
2591
2592    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2593        let depth = match endpoint.severity {
2594            DiagnosticSeverity::ERROR => &mut self.error_depth,
2595            DiagnosticSeverity::WARNING => &mut self.warning_depth,
2596            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2597            DiagnosticSeverity::HINT => &mut self.hint_depth,
2598            _ => return,
2599        };
2600        if endpoint.is_start {
2601            *depth += 1;
2602        } else {
2603            *depth -= 1;
2604        }
2605
2606        if endpoint.is_unnecessary {
2607            if endpoint.is_start {
2608                self.unnecessary_depth += 1;
2609            } else {
2610                self.unnecessary_depth -= 1;
2611            }
2612        }
2613    }
2614
2615    fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2616        if self.error_depth > 0 {
2617            Some(DiagnosticSeverity::ERROR)
2618        } else if self.warning_depth > 0 {
2619            Some(DiagnosticSeverity::WARNING)
2620        } else if self.information_depth > 0 {
2621            Some(DiagnosticSeverity::INFORMATION)
2622        } else if self.hint_depth > 0 {
2623            Some(DiagnosticSeverity::HINT)
2624        } else {
2625            None
2626        }
2627    }
2628
2629    fn current_code_is_unnecessary(&self) -> bool {
2630        self.unnecessary_depth > 0
2631    }
2632}
2633
2634impl<'a> Iterator for BufferChunks<'a> {
2635    type Item = Chunk<'a>;
2636
2637    fn next(&mut self) -> Option<Self::Item> {
2638        let mut next_capture_start = usize::MAX;
2639        let mut next_diagnostic_endpoint = usize::MAX;
2640
2641        if let Some(highlights) = self.highlights.as_mut() {
2642            while let Some((parent_capture_end, _)) = highlights.stack.last() {
2643                if *parent_capture_end <= self.range.start {
2644                    highlights.stack.pop();
2645                } else {
2646                    break;
2647                }
2648            }
2649
2650            if highlights.next_capture.is_none() {
2651                highlights.next_capture = highlights.captures.next();
2652            }
2653
2654            while let Some(capture) = highlights.next_capture.as_ref() {
2655                if self.range.start < capture.node.start_byte() {
2656                    next_capture_start = capture.node.start_byte();
2657                    break;
2658                } else {
2659                    let highlight_id =
2660                        highlights.highlight_maps[capture.grammar_index].get(capture.index);
2661                    highlights
2662                        .stack
2663                        .push((capture.node.end_byte(), highlight_id));
2664                    highlights.next_capture = highlights.captures.next();
2665                }
2666            }
2667        }
2668
2669        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2670            if endpoint.offset <= self.range.start {
2671                self.update_diagnostic_depths(endpoint);
2672                self.diagnostic_endpoints.next();
2673            } else {
2674                next_diagnostic_endpoint = endpoint.offset;
2675                break;
2676            }
2677        }
2678
2679        if let Some(chunk) = self.chunks.peek() {
2680            let chunk_start = self.range.start;
2681            let mut chunk_end = (self.chunks.offset() + chunk.len())
2682                .min(next_capture_start)
2683                .min(next_diagnostic_endpoint);
2684            let mut highlight_id = None;
2685            if let Some(highlights) = self.highlights.as_ref() {
2686                if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2687                    chunk_end = chunk_end.min(*parent_capture_end);
2688                    highlight_id = Some(*parent_highlight_id);
2689                }
2690            }
2691
2692            let slice =
2693                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2694            self.range.start = chunk_end;
2695            if self.range.start == self.chunks.offset() + chunk.len() {
2696                self.chunks.next().unwrap();
2697            }
2698
2699            Some(Chunk {
2700                text: slice,
2701                syntax_highlight_id: highlight_id,
2702                highlight_style: None,
2703                diagnostic_severity: self.current_diagnostic_severity(),
2704                is_unnecessary: self.current_code_is_unnecessary(),
2705            })
2706        } else {
2707            None
2708        }
2709    }
2710}
2711
2712impl operation_queue::Operation for Operation {
2713    fn lamport_timestamp(&self) -> clock::Lamport {
2714        match self {
2715            Operation::Buffer(_) => {
2716                unreachable!("buffer operations should never be deferred at this layer")
2717            }
2718            Operation::UpdateDiagnostics {
2719                lamport_timestamp, ..
2720            }
2721            | Operation::UpdateSelections {
2722                lamport_timestamp, ..
2723            }
2724            | Operation::UpdateCompletionTriggers {
2725                lamport_timestamp, ..
2726            } => *lamport_timestamp,
2727        }
2728    }
2729}
2730
2731impl Default for Diagnostic {
2732    fn default() -> Self {
2733        Self {
2734            code: None,
2735            severity: DiagnosticSeverity::ERROR,
2736            message: Default::default(),
2737            group_id: 0,
2738            is_primary: false,
2739            is_valid: true,
2740            is_disk_based: false,
2741            is_unnecessary: false,
2742        }
2743    }
2744}
2745
2746impl IndentSize {
2747    pub fn spaces(len: u32) -> Self {
2748        Self {
2749            len,
2750            kind: IndentKind::Space,
2751        }
2752    }
2753
2754    pub fn tab() -> Self {
2755        Self {
2756            len: 1,
2757            kind: IndentKind::Tab,
2758        }
2759    }
2760
2761    pub fn chars(&self) -> impl Iterator<Item = char> {
2762        iter::repeat(self.char()).take(self.len as usize)
2763    }
2764
2765    pub fn char(&self) -> char {
2766        match self.kind {
2767            IndentKind::Space => ' ',
2768            IndentKind::Tab => '\t',
2769        }
2770    }
2771
2772    pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2773        match direction {
2774            Ordering::Less => {
2775                if self.kind == size.kind && self.len >= size.len {
2776                    self.len -= size.len;
2777                }
2778            }
2779            Ordering::Equal => {}
2780            Ordering::Greater => {
2781                if self.len == 0 {
2782                    self = size;
2783                } else if self.kind == size.kind {
2784                    self.len += size.len;
2785                }
2786            }
2787        }
2788        self
2789    }
2790}
2791
2792impl Completion {
2793    pub fn sort_key(&self) -> (usize, &str) {
2794        let kind_key = match self.lsp_completion.kind {
2795            Some(lsp::CompletionItemKind::VARIABLE) => 0,
2796            _ => 1,
2797        };
2798        (kind_key, &self.label.text[self.label.filter_range.clone()])
2799    }
2800
2801    pub fn is_snippet(&self) -> bool {
2802        self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2803    }
2804}
2805
2806pub fn contiguous_ranges(
2807    values: impl Iterator<Item = u32>,
2808    max_len: usize,
2809) -> impl Iterator<Item = Range<u32>> {
2810    let mut values = values;
2811    let mut current_range: Option<Range<u32>> = None;
2812    std::iter::from_fn(move || loop {
2813        if let Some(value) = values.next() {
2814            if let Some(range) = &mut current_range {
2815                if value == range.end && range.len() < max_len {
2816                    range.end += 1;
2817                    continue;
2818                }
2819            }
2820
2821            let prev_range = current_range.clone();
2822            current_range = Some(value..(value + 1));
2823            if prev_range.is_some() {
2824                return prev_range;
2825            }
2826        } else {
2827            return current_range.take();
2828        }
2829    })
2830}
2831
2832pub fn char_kind(c: char) -> CharKind {
2833    if c.is_whitespace() {
2834        CharKind::Whitespace
2835    } else if c.is_alphanumeric() || c == '_' {
2836        CharKind::Word
2837    } else {
2838        CharKind::Punctuation
2839    }
2840}