buffer.rs

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