lib.rs

   1mod highlight_map;
   2mod language;
   3#[cfg(test)]
   4mod tests;
   5
   6pub use self::{
   7    highlight_map::{HighlightId, HighlightMap},
   8    language::{BracketPair, Language, LanguageConfig, LanguageRegistry},
   9};
  10use anyhow::{anyhow, Result};
  11pub use buffer::{Buffer as TextBuffer, *};
  12use clock::ReplicaId;
  13use futures::FutureExt as _;
  14use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task};
  15use lazy_static::lazy_static;
  16use lsp::LanguageServer;
  17use parking_lot::Mutex;
  18use postage::{prelude::Stream, sink::Sink, watch};
  19use rpc::proto;
  20use similar::{ChangeTag, TextDiff};
  21use smol::future::yield_now;
  22use std::{
  23    any::Any,
  24    cell::RefCell,
  25    cmp,
  26    collections::{BTreeMap, HashMap, HashSet},
  27    ffi::OsString,
  28    future::Future,
  29    iter::{Iterator, Peekable},
  30    ops::{Deref, DerefMut, Range},
  31    path::{Path, PathBuf},
  32    str,
  33    sync::Arc,
  34    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
  35    vec,
  36};
  37use tree_sitter::{InputEdit, Parser, QueryCursor, Tree};
  38use util::{post_inc, TryFutureExt as _};
  39
  40pub use lsp::DiagnosticSeverity;
  41
  42thread_local! {
  43    static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
  44}
  45
  46lazy_static! {
  47    static ref QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Default::default();
  48}
  49
  50// TODO - Make this configurable
  51const INDENT_SIZE: u32 = 4;
  52
  53pub struct Buffer {
  54    text: TextBuffer,
  55    file: Option<Box<dyn File>>,
  56    saved_version: clock::Global,
  57    saved_mtime: SystemTime,
  58    language: Option<Arc<Language>>,
  59    autoindent_requests: Vec<Arc<AutoindentRequest>>,
  60    pending_autoindent: Option<Task<()>>,
  61    sync_parse_timeout: Duration,
  62    syntax_tree: Mutex<Option<SyntaxTree>>,
  63    parsing_in_background: bool,
  64    parse_count: usize,
  65    diagnostics: AnchorRangeMultimap<(DiagnosticSeverity, String)>,
  66    diagnostics_update_count: usize,
  67    language_server: Option<LanguageServerState>,
  68    #[cfg(test)]
  69    operations: Vec<Operation>,
  70}
  71
  72pub struct Snapshot {
  73    text: buffer::Snapshot,
  74    tree: Option<Tree>,
  75    diagnostics: AnchorRangeMultimap<(DiagnosticSeverity, String)>,
  76    is_parsing: bool,
  77    language: Option<Arc<Language>>,
  78    query_cursor: QueryCursorHandle,
  79}
  80
  81#[derive(Debug, PartialEq, Eq)]
  82pub struct Diagnostic {
  83    pub range: Range<Point>,
  84    pub severity: DiagnosticSeverity,
  85    pub message: String,
  86}
  87
  88struct LanguageServerState {
  89    server: Arc<LanguageServer>,
  90    latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>,
  91    pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>,
  92    next_version: usize,
  93    _maintain_server: Task<Option<()>>,
  94}
  95
  96#[derive(Clone)]
  97struct LanguageServerSnapshot {
  98    buffer_snapshot: buffer::Snapshot,
  99    version: usize,
 100    path: Arc<Path>,
 101}
 102
 103#[derive(Clone, Debug, Eq, PartialEq)]
 104pub enum Event {
 105    Edited,
 106    Dirtied,
 107    Saved,
 108    FileHandleChanged,
 109    Reloaded,
 110    Reparsed,
 111    Closed,
 112}
 113
 114pub trait File {
 115    fn worktree_id(&self) -> usize;
 116
 117    fn entry_id(&self) -> Option<usize>;
 118
 119    fn mtime(&self) -> SystemTime;
 120
 121    /// Returns the path of this file relative to the worktree's root directory.
 122    fn path(&self) -> &Arc<Path>;
 123
 124    /// Returns the absolute path of this file.
 125    fn abs_path(&self, cx: &AppContext) -> Option<PathBuf>;
 126
 127    /// Returns the path of this file relative to the worktree's parent directory (this means it
 128    /// includes the name of the worktree's root folder).
 129    fn full_path(&self, cx: &AppContext) -> PathBuf;
 130
 131    /// Returns the last component of this handle's absolute path. If this handle refers to the root
 132    /// of its worktree, then this method will return the name of the worktree itself.
 133    fn file_name<'a>(&'a self, cx: &'a AppContext) -> Option<OsString>;
 134
 135    fn is_deleted(&self) -> bool;
 136
 137    fn save(
 138        &self,
 139        buffer_id: u64,
 140        text: Rope,
 141        version: clock::Global,
 142        cx: &mut MutableAppContext,
 143    ) -> Task<Result<(clock::Global, SystemTime)>>;
 144
 145    fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>>;
 146
 147    fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext);
 148
 149    fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext);
 150
 151    fn boxed_clone(&self) -> Box<dyn File>;
 152
 153    fn as_any(&self) -> &dyn Any;
 154}
 155
 156struct QueryCursorHandle(Option<QueryCursor>);
 157
 158#[derive(Clone)]
 159struct SyntaxTree {
 160    tree: Tree,
 161    version: clock::Global,
 162}
 163
 164#[derive(Clone)]
 165struct AutoindentRequest {
 166    selection_set_ids: HashSet<SelectionSetId>,
 167    before_edit: Snapshot,
 168    edited: AnchorSet,
 169    inserted: Option<AnchorRangeSet>,
 170}
 171
 172#[derive(Debug)]
 173struct IndentSuggestion {
 174    basis_row: u32,
 175    indent: bool,
 176}
 177
 178struct TextProvider<'a>(&'a Rope);
 179
 180struct Highlights<'a> {
 181    captures: tree_sitter::QueryCaptures<'a, 'a, TextProvider<'a>>,
 182    next_capture: Option<(tree_sitter::QueryMatch<'a, 'a>, usize)>,
 183    stack: Vec<(usize, HighlightId)>,
 184    highlight_map: HighlightMap,
 185}
 186
 187pub struct HighlightedChunks<'a> {
 188    range: Range<usize>,
 189    chunks: Chunks<'a>,
 190    diagnostic_endpoints: Peekable<vec::IntoIter<DiagnosticEndpoint>>,
 191    error_depth: usize,
 192    warning_depth: usize,
 193    information_depth: usize,
 194    hint_depth: usize,
 195    highlights: Option<Highlights<'a>>,
 196}
 197
 198#[derive(Clone, Copy, Debug, Default)]
 199pub struct HighlightedChunk<'a> {
 200    pub text: &'a str,
 201    pub highlight_id: HighlightId,
 202    pub diagnostic: Option<DiagnosticSeverity>,
 203}
 204
 205struct Diff {
 206    base_version: clock::Global,
 207    new_text: Arc<str>,
 208    changes: Vec<(ChangeTag, usize)>,
 209}
 210
 211#[derive(Clone, Copy)]
 212struct DiagnosticEndpoint {
 213    offset: usize,
 214    is_start: bool,
 215    severity: DiagnosticSeverity,
 216}
 217
 218impl Buffer {
 219    pub fn new<T: Into<Arc<str>>>(
 220        replica_id: ReplicaId,
 221        base_text: T,
 222        cx: &mut ModelContext<Self>,
 223    ) -> Self {
 224        Self::build(
 225            TextBuffer::new(
 226                replica_id,
 227                cx.model_id() as u64,
 228                History::new(base_text.into()),
 229            ),
 230            None,
 231        )
 232    }
 233
 234    pub fn from_file<T: Into<Arc<str>>>(
 235        replica_id: ReplicaId,
 236        base_text: T,
 237        file: Box<dyn File>,
 238        cx: &mut ModelContext<Self>,
 239    ) -> Self {
 240        Self::build(
 241            TextBuffer::new(
 242                replica_id,
 243                cx.model_id() as u64,
 244                History::new(base_text.into()),
 245            ),
 246            Some(file),
 247        )
 248    }
 249
 250    pub fn from_proto(
 251        replica_id: ReplicaId,
 252        message: proto::Buffer,
 253        file: Option<Box<dyn File>>,
 254    ) -> Result<Self> {
 255        Ok(Self::build(
 256            TextBuffer::from_proto(replica_id, message)?,
 257            file,
 258        ))
 259    }
 260
 261    pub fn with_language(
 262        mut self,
 263        language: Option<Arc<Language>>,
 264        language_server: Option<Arc<LanguageServer>>,
 265        cx: &mut ModelContext<Self>,
 266    ) -> Self {
 267        self.set_language(language, language_server, cx);
 268        self
 269    }
 270
 271    fn build(buffer: TextBuffer, file: Option<Box<dyn File>>) -> Self {
 272        let saved_mtime;
 273        if let Some(file) = file.as_ref() {
 274            saved_mtime = file.mtime();
 275        } else {
 276            saved_mtime = UNIX_EPOCH;
 277        }
 278
 279        Self {
 280            text: buffer,
 281            saved_mtime,
 282            saved_version: clock::Global::new(),
 283            file,
 284            syntax_tree: Mutex::new(None),
 285            parsing_in_background: false,
 286            parse_count: 0,
 287            sync_parse_timeout: Duration::from_millis(1),
 288            autoindent_requests: Default::default(),
 289            pending_autoindent: Default::default(),
 290            language: None,
 291            diagnostics: Default::default(),
 292            diagnostics_update_count: 0,
 293            language_server: None,
 294            #[cfg(test)]
 295            operations: Default::default(),
 296        }
 297    }
 298
 299    pub fn snapshot(&self) -> Snapshot {
 300        Snapshot {
 301            text: self.text.snapshot(),
 302            tree: self.syntax_tree(),
 303            diagnostics: self.diagnostics.clone(),
 304            is_parsing: self.parsing_in_background,
 305            language: self.language.clone(),
 306            query_cursor: QueryCursorHandle::new(),
 307        }
 308    }
 309
 310    pub fn file(&self) -> Option<&dyn File> {
 311        self.file.as_deref()
 312    }
 313
 314    pub fn save(
 315        &mut self,
 316        cx: &mut ModelContext<Self>,
 317    ) -> Result<Task<Result<(clock::Global, SystemTime)>>> {
 318        let file = self
 319            .file
 320            .as_ref()
 321            .ok_or_else(|| anyhow!("buffer has no file"))?;
 322        let text = self.as_rope().clone();
 323        let version = self.version.clone();
 324        let save = file.save(self.remote_id(), text, version, cx.as_mut());
 325        Ok(cx.spawn(|this, mut cx| async move {
 326            let (version, mtime) = save.await?;
 327            this.update(&mut cx, |this, cx| {
 328                this.did_save(version.clone(), mtime, None, cx);
 329            });
 330            Ok((version, mtime))
 331        }))
 332    }
 333
 334    pub fn set_language(
 335        &mut self,
 336        language: Option<Arc<Language>>,
 337        language_server: Option<Arc<lsp::LanguageServer>>,
 338        cx: &mut ModelContext<Self>,
 339    ) {
 340        self.language = language;
 341        self.language_server = if let Some(server) = language_server {
 342            let (latest_snapshot_tx, mut latest_snapshot_rx) = watch::channel();
 343            Some(LanguageServerState {
 344                latest_snapshot: latest_snapshot_tx,
 345                pending_snapshots: Default::default(),
 346                next_version: 0,
 347                server: server.clone(),
 348                _maintain_server: cx.background().spawn(
 349                    async move {
 350                        let mut prev_snapshot: Option<LanguageServerSnapshot> = None;
 351                        while let Some(snapshot) = latest_snapshot_rx.recv().await {
 352                            if let Some(snapshot) = snapshot {
 353                                let uri = lsp::Url::from_file_path(&snapshot.path).unwrap();
 354                                if let Some(prev_snapshot) = prev_snapshot {
 355                                    let changes = lsp::DidChangeTextDocumentParams {
 356                                        text_document: lsp::VersionedTextDocumentIdentifier::new(
 357                                            uri,
 358                                            snapshot.version as i32,
 359                                        ),
 360                                        content_changes: snapshot
 361                                            .buffer_snapshot
 362                                            .edits_since::<(PointUtf16, usize)>(
 363                                                prev_snapshot.buffer_snapshot.version(),
 364                                            )
 365                                            .map(|edit| {
 366                                                let edit_start = edit.new.start.0;
 367                                                let edit_end = edit_start
 368                                                    + (edit.old.end.0 - edit.old.start.0);
 369                                                let new_text = snapshot
 370                                                    .buffer_snapshot
 371                                                    .text_for_range(
 372                                                        edit.new.start.1..edit.new.end.1,
 373                                                    )
 374                                                    .collect();
 375                                                lsp::TextDocumentContentChangeEvent {
 376                                                    range: Some(lsp::Range::new(
 377                                                        lsp::Position::new(
 378                                                            edit_start.row,
 379                                                            edit_start.column,
 380                                                        ),
 381                                                        lsp::Position::new(
 382                                                            edit_end.row,
 383                                                            edit_end.column,
 384                                                        ),
 385                                                    )),
 386                                                    range_length: None,
 387                                                    text: new_text,
 388                                                }
 389                                            })
 390                                            .collect(),
 391                                    };
 392                                    server
 393                                        .notify::<lsp::notification::DidChangeTextDocument>(changes)
 394                                        .await?;
 395                                } else {
 396                                    server
 397                                        .notify::<lsp::notification::DidOpenTextDocument>(
 398                                            lsp::DidOpenTextDocumentParams {
 399                                                text_document: lsp::TextDocumentItem::new(
 400                                                    uri,
 401                                                    Default::default(),
 402                                                    snapshot.version as i32,
 403                                                    snapshot.buffer_snapshot.text().into(),
 404                                                ),
 405                                            },
 406                                        )
 407                                        .await?;
 408                                }
 409
 410                                prev_snapshot = Some(snapshot);
 411                            }
 412                        }
 413                        Ok(())
 414                    }
 415                    .log_err(),
 416                ),
 417            })
 418        } else {
 419            None
 420        };
 421
 422        self.reparse(cx);
 423        self.update_language_server(cx);
 424    }
 425
 426    pub fn did_save(
 427        &mut self,
 428        version: clock::Global,
 429        mtime: SystemTime,
 430        new_file: Option<Box<dyn File>>,
 431        cx: &mut ModelContext<Self>,
 432    ) {
 433        self.saved_mtime = mtime;
 434        self.saved_version = version;
 435        if let Some(new_file) = new_file {
 436            self.file = Some(new_file);
 437        }
 438        if let Some(state) = &self.language_server {
 439            cx.background()
 440                .spawn(
 441                    state
 442                        .server
 443                        .notify::<lsp::notification::DidSaveTextDocument>(
 444                            lsp::DidSaveTextDocumentParams {
 445                                text_document: lsp::TextDocumentIdentifier {
 446                                    uri: lsp::Url::from_file_path(
 447                                        self.file.as_ref().unwrap().abs_path(cx).unwrap(),
 448                                    )
 449                                    .unwrap(),
 450                                },
 451                                text: None,
 452                            },
 453                        ),
 454                )
 455                .detach()
 456        }
 457        cx.emit(Event::Saved);
 458    }
 459
 460    pub fn file_updated(
 461        &mut self,
 462        new_file: Box<dyn File>,
 463        cx: &mut ModelContext<Self>,
 464    ) -> Option<Task<()>> {
 465        let old_file = self.file.as_ref()?;
 466        let mut file_changed = false;
 467        let mut task = None;
 468
 469        if new_file.path() != old_file.path() {
 470            file_changed = true;
 471        }
 472
 473        if new_file.is_deleted() {
 474            if !old_file.is_deleted() {
 475                file_changed = true;
 476                if !self.is_dirty() {
 477                    cx.emit(Event::Dirtied);
 478                }
 479            }
 480        } else {
 481            let new_mtime = new_file.mtime();
 482            if new_mtime != old_file.mtime() {
 483                file_changed = true;
 484
 485                if !self.is_dirty() {
 486                    task = Some(cx.spawn(|this, mut cx| {
 487                        async move {
 488                            let new_text = this.read_with(&cx, |this, cx| {
 489                                this.file.as_ref().and_then(|file| file.load_local(cx))
 490                            });
 491                            if let Some(new_text) = new_text {
 492                                let new_text = new_text.await?;
 493                                let diff = this
 494                                    .read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
 495                                    .await;
 496                                this.update(&mut cx, |this, cx| {
 497                                    if this.apply_diff(diff, cx) {
 498                                        this.saved_version = this.version.clone();
 499                                        this.saved_mtime = new_mtime;
 500                                        cx.emit(Event::Reloaded);
 501                                    }
 502                                });
 503                            }
 504                            Ok(())
 505                        }
 506                        .log_err()
 507                        .map(drop)
 508                    }));
 509                }
 510            }
 511        }
 512
 513        if file_changed {
 514            cx.emit(Event::FileHandleChanged);
 515        }
 516        self.file = Some(new_file);
 517        task
 518    }
 519
 520    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 521        cx.emit(Event::Closed);
 522    }
 523
 524    pub fn language(&self) -> Option<&Arc<Language>> {
 525        self.language.as_ref()
 526    }
 527
 528    pub fn parse_count(&self) -> usize {
 529        self.parse_count
 530    }
 531
 532    fn syntax_tree(&self) -> Option<Tree> {
 533        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
 534            self.interpolate_tree(syntax_tree);
 535            Some(syntax_tree.tree.clone())
 536        } else {
 537            None
 538        }
 539    }
 540
 541    #[cfg(any(test, feature = "test-support"))]
 542    pub fn is_parsing(&self) -> bool {
 543        self.parsing_in_background
 544    }
 545
 546    #[cfg(test)]
 547    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 548        self.sync_parse_timeout = timeout;
 549    }
 550
 551    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
 552        if self.parsing_in_background {
 553            return false;
 554        }
 555
 556        if let Some(language) = self.language.clone() {
 557            let old_tree = self.syntax_tree();
 558            let text = self.as_rope().clone();
 559            let parsed_version = self.version();
 560            let parse_task = cx.background().spawn({
 561                let language = language.clone();
 562                async move { Self::parse_text(&text, old_tree, &language) }
 563            });
 564
 565            match cx
 566                .background()
 567                .block_with_timeout(self.sync_parse_timeout, parse_task)
 568            {
 569                Ok(new_tree) => {
 570                    self.did_finish_parsing(new_tree, parsed_version, cx);
 571                    return true;
 572                }
 573                Err(parse_task) => {
 574                    self.parsing_in_background = true;
 575                    cx.spawn(move |this, mut cx| async move {
 576                        let new_tree = parse_task.await;
 577                        this.update(&mut cx, move |this, cx| {
 578                            let language_changed =
 579                                this.language.as_ref().map_or(true, |curr_language| {
 580                                    !Arc::ptr_eq(curr_language, &language)
 581                                });
 582                            let parse_again = this.version > parsed_version || language_changed;
 583                            this.parsing_in_background = false;
 584                            this.did_finish_parsing(new_tree, parsed_version, cx);
 585
 586                            if parse_again && this.reparse(cx) {
 587                                return;
 588                            }
 589                        });
 590                    })
 591                    .detach();
 592                }
 593            }
 594        }
 595        false
 596    }
 597
 598    fn parse_text(text: &Rope, old_tree: Option<Tree>, language: &Language) -> Tree {
 599        PARSER.with(|parser| {
 600            let mut parser = parser.borrow_mut();
 601            parser
 602                .set_language(language.grammar)
 603                .expect("incompatible grammar");
 604            let mut chunks = text.chunks_in_range(0..text.len());
 605            let tree = parser
 606                .parse_with(
 607                    &mut move |offset, _| {
 608                        chunks.seek(offset);
 609                        chunks.next().unwrap_or("").as_bytes()
 610                    },
 611                    old_tree.as_ref(),
 612                )
 613                .unwrap();
 614            tree
 615        })
 616    }
 617
 618    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
 619        for edit in self.edits_since::<(usize, Point)>(&tree.version) {
 620            let (bytes, lines) = edit.flatten();
 621            tree.tree.edit(&InputEdit {
 622                start_byte: bytes.new.start,
 623                old_end_byte: bytes.new.start + bytes.old.len(),
 624                new_end_byte: bytes.new.end,
 625                start_position: lines.new.start.to_ts_point(),
 626                old_end_position: (lines.new.start + (lines.old.end - lines.old.start))
 627                    .to_ts_point(),
 628                new_end_position: lines.new.end.to_ts_point(),
 629            });
 630        }
 631        tree.version = self.version();
 632    }
 633
 634    fn did_finish_parsing(
 635        &mut self,
 636        tree: Tree,
 637        version: clock::Global,
 638        cx: &mut ModelContext<Self>,
 639    ) {
 640        self.parse_count += 1;
 641        *self.syntax_tree.lock() = Some(SyntaxTree { tree, version });
 642        self.request_autoindent(cx);
 643        cx.emit(Event::Reparsed);
 644        cx.notify();
 645    }
 646
 647    pub fn update_diagnostics(
 648        &mut self,
 649        version: Option<i32>,
 650        mut diagnostics: Vec<lsp::Diagnostic>,
 651        cx: &mut ModelContext<Self>,
 652    ) -> Result<()> {
 653        let version = version.map(|version| version as usize);
 654        let content = if let Some(version) = version {
 655            let language_server = self.language_server.as_mut().unwrap();
 656            let snapshot = language_server
 657                .pending_snapshots
 658                .get(&version)
 659                .ok_or_else(|| anyhow!("missing snapshot"))?;
 660            snapshot.buffer_snapshot.content()
 661        } else {
 662            self.content()
 663        };
 664
 665        let empty_set = HashSet::new();
 666        let disk_based_sources = self
 667            .language
 668            .as_ref()
 669            .and_then(|language| language.disk_based_diagnostic_sources())
 670            .unwrap_or(&empty_set);
 671
 672        diagnostics.sort_unstable_by_key(|d| (d.range.start, d.range.end));
 673        self.diagnostics = {
 674            let mut edits_since_save = content
 675                .edits_since::<PointUtf16>(&self.saved_version)
 676                .peekable();
 677            let mut last_edit_old_end = PointUtf16::zero();
 678            let mut last_edit_new_end = PointUtf16::zero();
 679
 680            content.anchor_range_multimap(
 681                Bias::Left,
 682                Bias::Right,
 683                diagnostics.into_iter().filter_map(|diagnostic| {
 684                    let mut start = PointUtf16::new(
 685                        diagnostic.range.start.line,
 686                        diagnostic.range.start.character,
 687                    );
 688                    let mut end =
 689                        PointUtf16::new(diagnostic.range.end.line, diagnostic.range.end.character);
 690                    let severity = diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR);
 691
 692                    if diagnostic
 693                        .source
 694                        .as_ref()
 695                        .map_or(false, |source| disk_based_sources.contains(source))
 696                    {
 697                        while let Some(edit) = edits_since_save.peek() {
 698                            if edit.old.end <= start {
 699                                last_edit_old_end = edit.old.end;
 700                                last_edit_new_end = edit.new.end;
 701                                edits_since_save.next();
 702                            } else if edit.old.start <= end && edit.old.end >= start {
 703                                return None;
 704                            } else {
 705                                break;
 706                            }
 707                        }
 708
 709                        start = last_edit_new_end + (start - last_edit_old_end);
 710                        end = last_edit_new_end + (end - last_edit_old_end);
 711                    }
 712
 713                    let mut range = content.clip_point_utf16(start, Bias::Left)
 714                        ..content.clip_point_utf16(end, Bias::Right);
 715                    if range.start == range.end {
 716                        range.end.column += 1;
 717                        range.end = content.clip_point_utf16(range.end, Bias::Right);
 718                    }
 719                    Some((range, (severity, diagnostic.message)))
 720                }),
 721            )
 722        };
 723
 724        if let Some(version) = version {
 725            let language_server = self.language_server.as_mut().unwrap();
 726            let versions_to_delete = language_server
 727                .pending_snapshots
 728                .range(..version)
 729                .map(|(v, _)| *v)
 730                .collect::<Vec<_>>();
 731            for version in versions_to_delete {
 732                language_server.pending_snapshots.remove(&version);
 733            }
 734        }
 735
 736        self.diagnostics_update_count += 1;
 737        cx.notify();
 738        Ok(())
 739    }
 740
 741    pub fn diagnostics_in_range<'a, T: 'a + ToOffset>(
 742        &'a self,
 743        range: Range<T>,
 744    ) -> impl Iterator<Item = Diagnostic> + 'a {
 745        let content = self.content();
 746        self.diagnostics
 747            .intersecting_ranges(range, content, true)
 748            .map(move |(_, range, (severity, message))| Diagnostic {
 749                range,
 750                severity: *severity,
 751                message: message.clone(),
 752            })
 753    }
 754
 755    pub fn diagnostics_update_count(&self) -> usize {
 756        self.diagnostics_update_count
 757    }
 758
 759    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 760        if let Some(indent_columns) = self.compute_autoindents() {
 761            let indent_columns = cx.background().spawn(indent_columns);
 762            match cx
 763                .background()
 764                .block_with_timeout(Duration::from_micros(500), indent_columns)
 765            {
 766                Ok(indent_columns) => self.apply_autoindents(indent_columns, cx),
 767                Err(indent_columns) => {
 768                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 769                        let indent_columns = indent_columns.await;
 770                        this.update(&mut cx, |this, cx| {
 771                            this.apply_autoindents(indent_columns, cx);
 772                        });
 773                    }));
 774                }
 775            }
 776        }
 777    }
 778
 779    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, u32>>> {
 780        let max_rows_between_yields = 100;
 781        let snapshot = self.snapshot();
 782        if snapshot.language.is_none()
 783            || snapshot.tree.is_none()
 784            || self.autoindent_requests.is_empty()
 785        {
 786            return None;
 787        }
 788
 789        let autoindent_requests = self.autoindent_requests.clone();
 790        Some(async move {
 791            let mut indent_columns = BTreeMap::new();
 792            for request in autoindent_requests {
 793                let old_to_new_rows = request
 794                    .edited
 795                    .points(&request.before_edit)
 796                    .map(|point| point.row)
 797                    .zip(request.edited.points(&snapshot).map(|point| point.row))
 798                    .collect::<BTreeMap<u32, u32>>();
 799
 800                let mut old_suggestions = HashMap::<u32, u32>::default();
 801                let old_edited_ranges =
 802                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 803                for old_edited_range in old_edited_ranges {
 804                    let suggestions = request
 805                        .before_edit
 806                        .suggest_autoindents(old_edited_range.clone())
 807                        .into_iter()
 808                        .flatten();
 809                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 810                        let indentation_basis = old_to_new_rows
 811                            .get(&suggestion.basis_row)
 812                            .and_then(|from_row| old_suggestions.get(from_row).copied())
 813                            .unwrap_or_else(|| {
 814                                request
 815                                    .before_edit
 816                                    .indent_column_for_line(suggestion.basis_row)
 817                            });
 818                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 819                        old_suggestions.insert(
 820                            *old_to_new_rows.get(&old_row).unwrap(),
 821                            indentation_basis + delta,
 822                        );
 823                    }
 824                    yield_now().await;
 825                }
 826
 827                // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
 828                // buffer before the edit, but keyed by the row for these lines after the edits were applied.
 829                let new_edited_row_ranges =
 830                    contiguous_ranges(old_to_new_rows.values().copied(), max_rows_between_yields);
 831                for new_edited_row_range in new_edited_row_ranges {
 832                    let suggestions = snapshot
 833                        .suggest_autoindents(new_edited_row_range.clone())
 834                        .into_iter()
 835                        .flatten();
 836                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 837                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 838                        let new_indentation = indent_columns
 839                            .get(&suggestion.basis_row)
 840                            .copied()
 841                            .unwrap_or_else(|| {
 842                                snapshot.indent_column_for_line(suggestion.basis_row)
 843                            })
 844                            + delta;
 845                        if old_suggestions
 846                            .get(&new_row)
 847                            .map_or(true, |old_indentation| new_indentation != *old_indentation)
 848                        {
 849                            indent_columns.insert(new_row, new_indentation);
 850                        }
 851                    }
 852                    yield_now().await;
 853                }
 854
 855                if let Some(inserted) = request.inserted.as_ref() {
 856                    let inserted_row_ranges = contiguous_ranges(
 857                        inserted
 858                            .point_ranges(&snapshot)
 859                            .flat_map(|range| range.start.row..range.end.row + 1),
 860                        max_rows_between_yields,
 861                    );
 862                    for inserted_row_range in inserted_row_ranges {
 863                        let suggestions = snapshot
 864                            .suggest_autoindents(inserted_row_range.clone())
 865                            .into_iter()
 866                            .flatten();
 867                        for (row, suggestion) in inserted_row_range.zip(suggestions) {
 868                            let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 869                            let new_indentation = indent_columns
 870                                .get(&suggestion.basis_row)
 871                                .copied()
 872                                .unwrap_or_else(|| {
 873                                    snapshot.indent_column_for_line(suggestion.basis_row)
 874                                })
 875                                + delta;
 876                            indent_columns.insert(row, new_indentation);
 877                        }
 878                        yield_now().await;
 879                    }
 880                }
 881            }
 882            indent_columns
 883        })
 884    }
 885
 886    fn apply_autoindents(
 887        &mut self,
 888        indent_columns: BTreeMap<u32, u32>,
 889        cx: &mut ModelContext<Self>,
 890    ) {
 891        let selection_set_ids = self
 892            .autoindent_requests
 893            .drain(..)
 894            .flat_map(|req| req.selection_set_ids.clone())
 895            .collect::<HashSet<_>>();
 896
 897        self.start_transaction(selection_set_ids.iter().copied())
 898            .unwrap();
 899        for (row, indent_column) in &indent_columns {
 900            self.set_indent_column_for_line(*row, *indent_column, cx);
 901        }
 902
 903        for selection_set_id in &selection_set_ids {
 904            if let Ok(set) = self.selection_set(*selection_set_id) {
 905                let new_selections = set
 906                    .point_selections(&*self)
 907                    .map(|selection| {
 908                        if selection.start.column == 0 {
 909                            let delta = Point::new(
 910                                0,
 911                                indent_columns
 912                                    .get(&selection.start.row)
 913                                    .copied()
 914                                    .unwrap_or(0),
 915                            );
 916                            if delta.column > 0 {
 917                                return Selection {
 918                                    id: selection.id,
 919                                    goal: selection.goal,
 920                                    reversed: selection.reversed,
 921                                    start: selection.start + delta,
 922                                    end: selection.end + delta,
 923                                };
 924                            }
 925                        }
 926                        selection
 927                    })
 928                    .collect::<Vec<_>>();
 929                self.update_selection_set(*selection_set_id, &new_selections, cx)
 930                    .unwrap();
 931            }
 932        }
 933
 934        self.end_transaction(selection_set_ids.iter().copied(), cx)
 935            .unwrap();
 936    }
 937
 938    pub fn indent_column_for_line(&self, row: u32) -> u32 {
 939        self.content().indent_column_for_line(row)
 940    }
 941
 942    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
 943        let current_column = self.indent_column_for_line(row);
 944        if column > current_column {
 945            let offset = Point::new(row, 0).to_offset(&*self);
 946            self.edit(
 947                [offset..offset],
 948                " ".repeat((column - current_column) as usize),
 949                cx,
 950            );
 951        } else if column < current_column {
 952            self.edit(
 953                [Point::new(row, 0)..Point::new(row, current_column - column)],
 954                "",
 955                cx,
 956            );
 957        }
 958    }
 959
 960    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
 961        if let Some(tree) = self.syntax_tree() {
 962            let root = tree.root_node();
 963            let range = range.start.to_offset(self)..range.end.to_offset(self);
 964            let mut node = root.descendant_for_byte_range(range.start, range.end);
 965            while node.map_or(false, |n| n.byte_range() == range) {
 966                node = node.unwrap().parent();
 967            }
 968            node.map(|n| n.byte_range())
 969        } else {
 970            None
 971        }
 972    }
 973
 974    pub fn enclosing_bracket_ranges<T: ToOffset>(
 975        &self,
 976        range: Range<T>,
 977    ) -> Option<(Range<usize>, Range<usize>)> {
 978        let (lang, tree) = self.language.as_ref().zip(self.syntax_tree())?;
 979        let open_capture_ix = lang.brackets_query.capture_index_for_name("open")?;
 980        let close_capture_ix = lang.brackets_query.capture_index_for_name("close")?;
 981
 982        // Find bracket pairs that *inclusively* contain the given range.
 983        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
 984        let mut cursor = QueryCursorHandle::new();
 985        let matches = cursor.set_byte_range(range).matches(
 986            &lang.brackets_query,
 987            tree.root_node(),
 988            TextProvider(self.as_rope()),
 989        );
 990
 991        // Get the ranges of the innermost pair of brackets.
 992        matches
 993            .filter_map(|mat| {
 994                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
 995                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
 996                Some((open.byte_range(), close.byte_range()))
 997            })
 998            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
 999    }
1000
1001    fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
1002        // TODO: it would be nice to not allocate here.
1003        let old_text = self.text();
1004        let base_version = self.version();
1005        cx.background().spawn(async move {
1006            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
1007                .iter_all_changes()
1008                .map(|c| (c.tag(), c.value().len()))
1009                .collect::<Vec<_>>();
1010            Diff {
1011                base_version,
1012                new_text,
1013                changes,
1014            }
1015        })
1016    }
1017
1018    fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
1019        if self.version == diff.base_version {
1020            self.start_transaction(None).unwrap();
1021            let mut offset = 0;
1022            for (tag, len) in diff.changes {
1023                let range = offset..(offset + len);
1024                match tag {
1025                    ChangeTag::Equal => offset += len,
1026                    ChangeTag::Delete => self.edit(Some(range), "", cx),
1027                    ChangeTag::Insert => {
1028                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
1029                        offset += len;
1030                    }
1031                }
1032            }
1033            self.end_transaction(None, cx).unwrap();
1034            true
1035        } else {
1036            false
1037        }
1038    }
1039
1040    pub fn is_dirty(&self) -> bool {
1041        self.version > self.saved_version
1042            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1043    }
1044
1045    pub fn has_conflict(&self) -> bool {
1046        self.version > self.saved_version
1047            && self
1048                .file
1049                .as_ref()
1050                .map_or(false, |file| file.mtime() > self.saved_mtime)
1051    }
1052
1053    pub fn start_transaction(
1054        &mut self,
1055        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1056    ) -> Result<()> {
1057        self.start_transaction_at(selection_set_ids, Instant::now())
1058    }
1059
1060    fn start_transaction_at(
1061        &mut self,
1062        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1063        now: Instant,
1064    ) -> Result<()> {
1065        self.text.start_transaction_at(selection_set_ids, now)
1066    }
1067
1068    pub fn end_transaction(
1069        &mut self,
1070        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1071        cx: &mut ModelContext<Self>,
1072    ) -> Result<()> {
1073        self.end_transaction_at(selection_set_ids, Instant::now(), cx)
1074    }
1075
1076    fn end_transaction_at(
1077        &mut self,
1078        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1079        now: Instant,
1080        cx: &mut ModelContext<Self>,
1081    ) -> Result<()> {
1082        if let Some(start_version) = self.text.end_transaction_at(selection_set_ids, now) {
1083            let was_dirty = start_version != self.saved_version;
1084            self.did_edit(&start_version, was_dirty, cx);
1085        }
1086        Ok(())
1087    }
1088
1089    fn update_language_server(&mut self, cx: &AppContext) {
1090        let language_server = if let Some(language_server) = self.language_server.as_mut() {
1091            language_server
1092        } else {
1093            return;
1094        };
1095        let abs_path = self
1096            .file
1097            .as_ref()
1098            .map_or(Path::new("/").to_path_buf(), |file| {
1099                file.abs_path(cx).unwrap()
1100            });
1101
1102        let version = post_inc(&mut language_server.next_version);
1103        let snapshot = LanguageServerSnapshot {
1104            buffer_snapshot: self.text.snapshot(),
1105            version,
1106            path: Arc::from(abs_path),
1107        };
1108        language_server
1109            .pending_snapshots
1110            .insert(version, snapshot.clone());
1111        let _ = language_server
1112            .latest_snapshot
1113            .blocking_send(Some(snapshot));
1114    }
1115
1116    pub fn edit<I, S, T>(&mut self, ranges_iter: I, new_text: T, cx: &mut ModelContext<Self>)
1117    where
1118        I: IntoIterator<Item = Range<S>>,
1119        S: ToOffset,
1120        T: Into<String>,
1121    {
1122        self.edit_internal(ranges_iter, new_text, false, cx)
1123    }
1124
1125    pub fn edit_with_autoindent<I, S, T>(
1126        &mut self,
1127        ranges_iter: I,
1128        new_text: T,
1129        cx: &mut ModelContext<Self>,
1130    ) where
1131        I: IntoIterator<Item = Range<S>>,
1132        S: ToOffset,
1133        T: Into<String>,
1134    {
1135        self.edit_internal(ranges_iter, new_text, true, cx)
1136    }
1137
1138    pub fn edit_internal<I, S, T>(
1139        &mut self,
1140        ranges_iter: I,
1141        new_text: T,
1142        autoindent: bool,
1143        cx: &mut ModelContext<Self>,
1144    ) where
1145        I: IntoIterator<Item = Range<S>>,
1146        S: ToOffset,
1147        T: Into<String>,
1148    {
1149        let new_text = new_text.into();
1150
1151        // Skip invalid ranges and coalesce contiguous ones.
1152        let mut ranges: Vec<Range<usize>> = Vec::new();
1153        for range in ranges_iter {
1154            let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
1155            if !new_text.is_empty() || !range.is_empty() {
1156                if let Some(prev_range) = ranges.last_mut() {
1157                    if prev_range.end >= range.start {
1158                        prev_range.end = cmp::max(prev_range.end, range.end);
1159                    } else {
1160                        ranges.push(range);
1161                    }
1162                } else {
1163                    ranges.push(range);
1164                }
1165            }
1166        }
1167        if ranges.is_empty() {
1168            return;
1169        }
1170
1171        self.start_transaction(None).unwrap();
1172        self.pending_autoindent.take();
1173        let autoindent_request = if autoindent && self.language.is_some() {
1174            let before_edit = self.snapshot();
1175            let edited = self.content().anchor_set(ranges.iter().filter_map(|range| {
1176                let start = range.start.to_point(&*self);
1177                if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
1178                    None
1179                } else {
1180                    Some((range.start, Bias::Left))
1181                }
1182            }));
1183            Some((before_edit, edited))
1184        } else {
1185            None
1186        };
1187
1188        let first_newline_ix = new_text.find('\n');
1189        let new_text_len = new_text.len();
1190
1191        let edit = self.text.edit(ranges.iter().cloned(), new_text);
1192
1193        if let Some((before_edit, edited)) = autoindent_request {
1194            let mut inserted = None;
1195            if let Some(first_newline_ix) = first_newline_ix {
1196                let mut delta = 0isize;
1197                inserted = Some(self.content().anchor_range_set(ranges.iter().map(|range| {
1198                    let start = (delta + range.start as isize) as usize + first_newline_ix + 1;
1199                    let end = (delta + range.start as isize) as usize + new_text_len;
1200                    delta += (range.end as isize - range.start as isize) + new_text_len as isize;
1201                    (start, Bias::Left)..(end, Bias::Right)
1202                })));
1203            }
1204
1205            let selection_set_ids = self
1206                .text
1207                .peek_undo_stack()
1208                .unwrap()
1209                .starting_selection_set_ids()
1210                .collect();
1211            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1212                selection_set_ids,
1213                before_edit,
1214                edited,
1215                inserted,
1216            }));
1217        }
1218
1219        self.end_transaction(None, cx).unwrap();
1220        self.send_operation(Operation::Edit(edit), cx);
1221    }
1222
1223    fn did_edit(
1224        &mut self,
1225        old_version: &clock::Global,
1226        was_dirty: bool,
1227        cx: &mut ModelContext<Self>,
1228    ) {
1229        if self.edits_since::<usize>(old_version).next().is_none() {
1230            return;
1231        }
1232
1233        self.reparse(cx);
1234        self.update_language_server(cx);
1235
1236        cx.emit(Event::Edited);
1237        if !was_dirty {
1238            cx.emit(Event::Dirtied);
1239        }
1240        cx.notify();
1241    }
1242
1243    pub fn add_selection_set<T: ToOffset>(
1244        &mut self,
1245        selections: &[Selection<T>],
1246        cx: &mut ModelContext<Self>,
1247    ) -> SelectionSetId {
1248        let operation = self.text.add_selection_set(selections);
1249        if let Operation::UpdateSelections { set_id, .. } = &operation {
1250            let set_id = *set_id;
1251            cx.notify();
1252            self.send_operation(operation, cx);
1253            set_id
1254        } else {
1255            unreachable!()
1256        }
1257    }
1258
1259    pub fn update_selection_set<T: ToOffset>(
1260        &mut self,
1261        set_id: SelectionSetId,
1262        selections: &[Selection<T>],
1263        cx: &mut ModelContext<Self>,
1264    ) -> Result<()> {
1265        let operation = self.text.update_selection_set(set_id, selections)?;
1266        cx.notify();
1267        self.send_operation(operation, cx);
1268        Ok(())
1269    }
1270
1271    pub fn set_active_selection_set(
1272        &mut self,
1273        set_id: Option<SelectionSetId>,
1274        cx: &mut ModelContext<Self>,
1275    ) -> Result<()> {
1276        let operation = self.text.set_active_selection_set(set_id)?;
1277        self.send_operation(operation, cx);
1278        Ok(())
1279    }
1280
1281    pub fn remove_selection_set(
1282        &mut self,
1283        set_id: SelectionSetId,
1284        cx: &mut ModelContext<Self>,
1285    ) -> Result<()> {
1286        let operation = self.text.remove_selection_set(set_id)?;
1287        cx.notify();
1288        self.send_operation(operation, cx);
1289        Ok(())
1290    }
1291
1292    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1293        &mut self,
1294        ops: I,
1295        cx: &mut ModelContext<Self>,
1296    ) -> Result<()> {
1297        self.pending_autoindent.take();
1298        let was_dirty = self.is_dirty();
1299        let old_version = self.version.clone();
1300        self.text.apply_ops(ops)?;
1301        self.did_edit(&old_version, was_dirty, cx);
1302        // Notify independently of whether the buffer was edited as the operations could include a
1303        // selection update.
1304        cx.notify();
1305        Ok(())
1306    }
1307
1308    #[cfg(not(test))]
1309    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1310        if let Some(file) = &self.file {
1311            file.buffer_updated(self.remote_id(), operation, cx.as_mut());
1312        }
1313    }
1314
1315    #[cfg(test)]
1316    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
1317        self.operations.push(operation);
1318    }
1319
1320    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1321        self.text.remove_peer(replica_id);
1322        cx.notify();
1323    }
1324
1325    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
1326        let was_dirty = self.is_dirty();
1327        let old_version = self.version.clone();
1328
1329        for operation in self.text.undo() {
1330            self.send_operation(operation, cx);
1331        }
1332
1333        self.did_edit(&old_version, was_dirty, cx);
1334    }
1335
1336    pub fn redo(&mut self, cx: &mut ModelContext<Self>) {
1337        let was_dirty = self.is_dirty();
1338        let old_version = self.version.clone();
1339
1340        for operation in self.text.redo() {
1341            self.send_operation(operation, cx);
1342        }
1343
1344        self.did_edit(&old_version, was_dirty, cx);
1345    }
1346}
1347
1348#[cfg(any(test, feature = "test-support"))]
1349impl Buffer {
1350    pub fn randomly_edit<T>(&mut self, rng: &mut T, old_range_count: usize)
1351    where
1352        T: rand::Rng,
1353    {
1354        self.text.randomly_edit(rng, old_range_count);
1355    }
1356
1357    pub fn randomly_mutate<T>(&mut self, rng: &mut T)
1358    where
1359        T: rand::Rng,
1360    {
1361        self.text.randomly_mutate(rng);
1362    }
1363}
1364
1365impl Entity for Buffer {
1366    type Event = Event;
1367
1368    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
1369        if let Some(file) = self.file.as_ref() {
1370            file.buffer_removed(self.remote_id(), cx);
1371        }
1372    }
1373}
1374
1375// TODO: Do we need to clone a buffer?
1376impl Clone for Buffer {
1377    fn clone(&self) -> Self {
1378        Self {
1379            text: self.text.clone(),
1380            saved_version: self.saved_version.clone(),
1381            saved_mtime: self.saved_mtime,
1382            file: self.file.as_ref().map(|f| f.boxed_clone()),
1383            language: self.language.clone(),
1384            syntax_tree: Mutex::new(self.syntax_tree.lock().clone()),
1385            parsing_in_background: false,
1386            sync_parse_timeout: self.sync_parse_timeout,
1387            parse_count: self.parse_count,
1388            autoindent_requests: Default::default(),
1389            pending_autoindent: Default::default(),
1390            diagnostics: self.diagnostics.clone(),
1391            diagnostics_update_count: self.diagnostics_update_count,
1392            language_server: None,
1393            #[cfg(test)]
1394            operations: self.operations.clone(),
1395        }
1396    }
1397}
1398
1399impl Deref for Buffer {
1400    type Target = TextBuffer;
1401
1402    fn deref(&self) -> &Self::Target {
1403        &self.text
1404    }
1405}
1406
1407impl<'a> From<&'a Buffer> for Content<'a> {
1408    fn from(buffer: &'a Buffer) -> Self {
1409        Self::from(&buffer.text)
1410    }
1411}
1412
1413impl<'a> From<&'a mut Buffer> for Content<'a> {
1414    fn from(buffer: &'a mut Buffer) -> Self {
1415        Self::from(&buffer.text)
1416    }
1417}
1418
1419impl<'a> From<&'a Snapshot> for Content<'a> {
1420    fn from(snapshot: &'a Snapshot) -> Self {
1421        Self::from(&snapshot.text)
1422    }
1423}
1424
1425impl Snapshot {
1426    fn suggest_autoindents<'a>(
1427        &'a self,
1428        row_range: Range<u32>,
1429    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1430        let mut query_cursor = QueryCursorHandle::new();
1431        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
1432            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1433
1434            // Get the "indentation ranges" that intersect this row range.
1435            let indent_capture_ix = language.indents_query.capture_index_for_name("indent");
1436            let end_capture_ix = language.indents_query.capture_index_for_name("end");
1437            query_cursor.set_point_range(
1438                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1439                    ..Point::new(row_range.end, 0).to_ts_point(),
1440            );
1441            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1442            for mat in query_cursor.matches(
1443                &language.indents_query,
1444                tree.root_node(),
1445                TextProvider(self.as_rope()),
1446            ) {
1447                let mut node_kind = "";
1448                let mut start: Option<Point> = None;
1449                let mut end: Option<Point> = None;
1450                for capture in mat.captures {
1451                    if Some(capture.index) == indent_capture_ix {
1452                        node_kind = capture.node.kind();
1453                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1454                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1455                    } else if Some(capture.index) == end_capture_ix {
1456                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
1457                    }
1458                }
1459
1460                if let Some((start, end)) = start.zip(end) {
1461                    if start.row == end.row {
1462                        continue;
1463                    }
1464
1465                    let range = start..end;
1466                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1467                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1468                        Ok(ix) => {
1469                            let prev_range = &mut indentation_ranges[ix];
1470                            prev_range.0.end = prev_range.0.end.max(range.end);
1471                        }
1472                    }
1473                }
1474            }
1475
1476            let mut prev_row = prev_non_blank_row.unwrap_or(0);
1477            Some(row_range.map(move |row| {
1478                let row_start = Point::new(row, self.indent_column_for_line(row));
1479
1480                let mut indent_from_prev_row = false;
1481                let mut outdent_to_row = u32::MAX;
1482                for (range, _node_kind) in &indentation_ranges {
1483                    if range.start.row >= row {
1484                        break;
1485                    }
1486
1487                    if range.start.row == prev_row && range.end > row_start {
1488                        indent_from_prev_row = true;
1489                    }
1490                    if range.end.row >= prev_row && range.end <= row_start {
1491                        outdent_to_row = outdent_to_row.min(range.start.row);
1492                    }
1493                }
1494
1495                let suggestion = if outdent_to_row == prev_row {
1496                    IndentSuggestion {
1497                        basis_row: prev_row,
1498                        indent: false,
1499                    }
1500                } else if indent_from_prev_row {
1501                    IndentSuggestion {
1502                        basis_row: prev_row,
1503                        indent: true,
1504                    }
1505                } else if outdent_to_row < prev_row {
1506                    IndentSuggestion {
1507                        basis_row: outdent_to_row,
1508                        indent: false,
1509                    }
1510                } else {
1511                    IndentSuggestion {
1512                        basis_row: prev_row,
1513                        indent: false,
1514                    }
1515                };
1516
1517                prev_row = row;
1518                suggestion
1519            }))
1520        } else {
1521            None
1522        }
1523    }
1524
1525    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1526        while row > 0 {
1527            row -= 1;
1528            if !self.is_line_blank(row) {
1529                return Some(row);
1530            }
1531        }
1532        None
1533    }
1534
1535    fn is_line_blank(&self, row: u32) -> bool {
1536        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1537            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1538    }
1539
1540    pub fn highlighted_text_for_range<T: ToOffset>(
1541        &mut self,
1542        range: Range<T>,
1543    ) -> HighlightedChunks {
1544        let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
1545
1546        let mut diagnostic_endpoints = Vec::<DiagnosticEndpoint>::new();
1547        for (_, range, (severity, _)) in
1548            self.diagnostics
1549                .intersecting_ranges(range.clone(), self.content(), true)
1550        {
1551            diagnostic_endpoints.push(DiagnosticEndpoint {
1552                offset: range.start,
1553                is_start: true,
1554                severity: *severity,
1555            });
1556            diagnostic_endpoints.push(DiagnosticEndpoint {
1557                offset: range.end,
1558                is_start: false,
1559                severity: *severity,
1560            });
1561        }
1562        diagnostic_endpoints.sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1563        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1564
1565        let chunks = self.text.as_rope().chunks_in_range(range.clone());
1566        let highlights =
1567            if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
1568                let captures = self.query_cursor.set_byte_range(range.clone()).captures(
1569                    &language.highlights_query,
1570                    tree.root_node(),
1571                    TextProvider(self.text.as_rope()),
1572                );
1573
1574                Some(Highlights {
1575                    captures,
1576                    next_capture: None,
1577                    stack: Default::default(),
1578                    highlight_map: language.highlight_map(),
1579                })
1580            } else {
1581                None
1582            };
1583
1584        HighlightedChunks {
1585            range,
1586            chunks,
1587            diagnostic_endpoints,
1588            error_depth: 0,
1589            warning_depth: 0,
1590            information_depth: 0,
1591            hint_depth: 0,
1592            highlights,
1593        }
1594    }
1595}
1596
1597impl Clone for Snapshot {
1598    fn clone(&self) -> Self {
1599        Self {
1600            text: self.text.clone(),
1601            tree: self.tree.clone(),
1602            diagnostics: self.diagnostics.clone(),
1603            is_parsing: self.is_parsing,
1604            language: self.language.clone(),
1605            query_cursor: QueryCursorHandle::new(),
1606        }
1607    }
1608}
1609
1610impl Deref for Snapshot {
1611    type Target = buffer::Snapshot;
1612
1613    fn deref(&self) -> &Self::Target {
1614        &self.text
1615    }
1616}
1617
1618impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1619    type I = ByteChunks<'a>;
1620
1621    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1622        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1623    }
1624}
1625
1626struct ByteChunks<'a>(rope::Chunks<'a>);
1627
1628impl<'a> Iterator for ByteChunks<'a> {
1629    type Item = &'a [u8];
1630
1631    fn next(&mut self) -> Option<Self::Item> {
1632        self.0.next().map(str::as_bytes)
1633    }
1634}
1635
1636impl<'a> HighlightedChunks<'a> {
1637    pub fn seek(&mut self, offset: usize) {
1638        self.range.start = offset;
1639        self.chunks.seek(self.range.start);
1640        if let Some(highlights) = self.highlights.as_mut() {
1641            highlights
1642                .stack
1643                .retain(|(end_offset, _)| *end_offset > offset);
1644            if let Some((mat, capture_ix)) = &highlights.next_capture {
1645                let capture = mat.captures[*capture_ix as usize];
1646                if offset >= capture.node.start_byte() {
1647                    let next_capture_end = capture.node.end_byte();
1648                    if offset < next_capture_end {
1649                        highlights.stack.push((
1650                            next_capture_end,
1651                            highlights.highlight_map.get(capture.index),
1652                        ));
1653                    }
1654                    highlights.next_capture.take();
1655                }
1656            }
1657            highlights.captures.set_byte_range(self.range.clone());
1658        }
1659    }
1660
1661    pub fn offset(&self) -> usize {
1662        self.range.start
1663    }
1664
1665    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
1666        let depth = match endpoint.severity {
1667            DiagnosticSeverity::ERROR => &mut self.error_depth,
1668            DiagnosticSeverity::WARNING => &mut self.warning_depth,
1669            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
1670            DiagnosticSeverity::HINT => &mut self.hint_depth,
1671            _ => return,
1672        };
1673        if endpoint.is_start {
1674            *depth += 1;
1675        } else {
1676            *depth -= 1;
1677        }
1678    }
1679
1680    fn current_diagnostic_severity(&mut self) -> Option<DiagnosticSeverity> {
1681        if self.error_depth > 0 {
1682            Some(DiagnosticSeverity::ERROR)
1683        } else if self.warning_depth > 0 {
1684            Some(DiagnosticSeverity::WARNING)
1685        } else if self.information_depth > 0 {
1686            Some(DiagnosticSeverity::INFORMATION)
1687        } else if self.hint_depth > 0 {
1688            Some(DiagnosticSeverity::HINT)
1689        } else {
1690            None
1691        }
1692    }
1693}
1694
1695impl<'a> Iterator for HighlightedChunks<'a> {
1696    type Item = HighlightedChunk<'a>;
1697
1698    fn next(&mut self) -> Option<Self::Item> {
1699        let mut next_capture_start = usize::MAX;
1700        let mut next_diagnostic_endpoint = usize::MAX;
1701
1702        if let Some(highlights) = self.highlights.as_mut() {
1703            while let Some((parent_capture_end, _)) = highlights.stack.last() {
1704                if *parent_capture_end <= self.range.start {
1705                    highlights.stack.pop();
1706                } else {
1707                    break;
1708                }
1709            }
1710
1711            if highlights.next_capture.is_none() {
1712                highlights.next_capture = highlights.captures.next();
1713            }
1714
1715            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
1716                let capture = mat.captures[*capture_ix as usize];
1717                if self.range.start < capture.node.start_byte() {
1718                    next_capture_start = capture.node.start_byte();
1719                    break;
1720                } else {
1721                    let highlight_id = highlights.highlight_map.get(capture.index);
1722                    highlights
1723                        .stack
1724                        .push((capture.node.end_byte(), highlight_id));
1725                    highlights.next_capture = highlights.captures.next();
1726                }
1727            }
1728        }
1729
1730        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
1731            if endpoint.offset <= self.range.start {
1732                self.update_diagnostic_depths(endpoint);
1733                self.diagnostic_endpoints.next();
1734            } else {
1735                next_diagnostic_endpoint = endpoint.offset;
1736                break;
1737            }
1738        }
1739
1740        if let Some(chunk) = self.chunks.peek() {
1741            let chunk_start = self.range.start;
1742            let mut chunk_end = (self.chunks.offset() + chunk.len())
1743                .min(next_capture_start)
1744                .min(next_diagnostic_endpoint);
1745            let mut highlight_id = HighlightId::default();
1746            if let Some((parent_capture_end, parent_highlight_id)) =
1747                self.highlights.as_ref().and_then(|h| h.stack.last())
1748            {
1749                chunk_end = chunk_end.min(*parent_capture_end);
1750                highlight_id = *parent_highlight_id;
1751            }
1752
1753            let slice =
1754                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
1755            self.range.start = chunk_end;
1756            if self.range.start == self.chunks.offset() + chunk.len() {
1757                self.chunks.next().unwrap();
1758            }
1759
1760            Some(HighlightedChunk {
1761                text: slice,
1762                highlight_id,
1763                diagnostic: self.current_diagnostic_severity(),
1764            })
1765        } else {
1766            None
1767        }
1768    }
1769}
1770
1771impl QueryCursorHandle {
1772    fn new() -> Self {
1773        QueryCursorHandle(Some(
1774            QUERY_CURSORS
1775                .lock()
1776                .pop()
1777                .unwrap_or_else(|| QueryCursor::new()),
1778        ))
1779    }
1780}
1781
1782impl Deref for QueryCursorHandle {
1783    type Target = QueryCursor;
1784
1785    fn deref(&self) -> &Self::Target {
1786        self.0.as_ref().unwrap()
1787    }
1788}
1789
1790impl DerefMut for QueryCursorHandle {
1791    fn deref_mut(&mut self) -> &mut Self::Target {
1792        self.0.as_mut().unwrap()
1793    }
1794}
1795
1796impl Drop for QueryCursorHandle {
1797    fn drop(&mut self) {
1798        let mut cursor = self.0.take().unwrap();
1799        cursor.set_byte_range(0..usize::MAX);
1800        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
1801        QUERY_CURSORS.lock().push(cursor)
1802    }
1803}
1804
1805trait ToTreeSitterPoint {
1806    fn to_ts_point(self) -> tree_sitter::Point;
1807    fn from_ts_point(point: tree_sitter::Point) -> Self;
1808}
1809
1810impl ToTreeSitterPoint for Point {
1811    fn to_ts_point(self) -> tree_sitter::Point {
1812        tree_sitter::Point::new(self.row as usize, self.column as usize)
1813    }
1814
1815    fn from_ts_point(point: tree_sitter::Point) -> Self {
1816        Point::new(point.row as u32, point.column as u32)
1817    }
1818}
1819
1820fn contiguous_ranges(
1821    values: impl IntoIterator<Item = u32>,
1822    max_len: usize,
1823) -> impl Iterator<Item = Range<u32>> {
1824    let mut values = values.into_iter();
1825    let mut current_range: Option<Range<u32>> = None;
1826    std::iter::from_fn(move || loop {
1827        if let Some(value) = values.next() {
1828            if let Some(range) = &mut current_range {
1829                if value == range.end && range.len() < max_len {
1830                    range.end += 1;
1831                    continue;
1832                }
1833            }
1834
1835            let prev_range = current_range.clone();
1836            current_range = Some(value..(value + 1));
1837            if prev_range.is_some() {
1838                return prev_range;
1839            }
1840        } else {
1841            return current_range.take();
1842        }
1843    })
1844}