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(
 363                                                prev_snapshot.buffer_snapshot.version().clone(),
 364                                            )
 365                                            .map(|edit| {
 366                                                lsp::TextDocumentContentChangeEvent {
 367                                                    // TODO: Use UTF-16 positions.
 368                                                    range: Some(lsp::Range::new(
 369                                                        lsp::Position::new(
 370                                                            edit.old_lines.start.row,
 371                                                            edit.old_lines.start.column,
 372                                                        ),
 373                                                        lsp::Position::new(
 374                                                            edit.old_lines.end.row,
 375                                                            edit.old_lines.end.column,
 376                                                        ),
 377                                                    )),
 378                                                    range_length: None,
 379                                                    text: snapshot
 380                                                        .buffer_snapshot
 381                                                        .text_for_range(edit.new_bytes)
 382                                                        .collect(),
 383                                                }
 384                                            })
 385                                            .collect(),
 386                                    };
 387                                    server
 388                                        .notify::<lsp::notification::DidChangeTextDocument>(changes)
 389                                        .await?;
 390                                } else {
 391                                    server
 392                                        .notify::<lsp::notification::DidOpenTextDocument>(
 393                                            lsp::DidOpenTextDocumentParams {
 394                                                text_document: lsp::TextDocumentItem::new(
 395                                                    uri,
 396                                                    Default::default(),
 397                                                    snapshot.version as i32,
 398                                                    snapshot.buffer_snapshot.text().into(),
 399                                                ),
 400                                            },
 401                                        )
 402                                        .await?;
 403                                }
 404
 405                                prev_snapshot = Some(snapshot);
 406                            }
 407                        }
 408                        Ok(())
 409                    }
 410                    .log_err(),
 411                ),
 412            })
 413        } else {
 414            None
 415        };
 416
 417        self.reparse(cx);
 418        self.update_language_server(cx);
 419    }
 420
 421    pub fn did_save(
 422        &mut self,
 423        version: clock::Global,
 424        mtime: SystemTime,
 425        new_file: Option<Box<dyn File>>,
 426        cx: &mut ModelContext<Self>,
 427    ) {
 428        self.saved_mtime = mtime;
 429        self.saved_version = version;
 430        if let Some(new_file) = new_file {
 431            self.file = Some(new_file);
 432        }
 433        if let Some(state) = &self.language_server {
 434            cx.background()
 435                .spawn(
 436                    state
 437                        .server
 438                        .notify::<lsp::notification::DidSaveTextDocument>(
 439                            lsp::DidSaveTextDocumentParams {
 440                                text_document: lsp::TextDocumentIdentifier {
 441                                    uri: lsp::Url::from_file_path(
 442                                        self.file.as_ref().unwrap().abs_path(cx).unwrap(),
 443                                    )
 444                                    .unwrap(),
 445                                },
 446                                text: None,
 447                            },
 448                        ),
 449                )
 450                .detach()
 451        }
 452        cx.emit(Event::Saved);
 453    }
 454
 455    pub fn file_updated(
 456        &mut self,
 457        new_file: Box<dyn File>,
 458        cx: &mut ModelContext<Self>,
 459    ) -> Option<Task<()>> {
 460        let old_file = self.file.as_ref()?;
 461        let mut file_changed = false;
 462        let mut task = None;
 463
 464        if new_file.path() != old_file.path() {
 465            file_changed = true;
 466        }
 467
 468        if new_file.is_deleted() {
 469            if !old_file.is_deleted() {
 470                file_changed = true;
 471                if !self.is_dirty() {
 472                    cx.emit(Event::Dirtied);
 473                }
 474            }
 475        } else {
 476            let new_mtime = new_file.mtime();
 477            if new_mtime != old_file.mtime() {
 478                file_changed = true;
 479
 480                if !self.is_dirty() {
 481                    task = Some(cx.spawn(|this, mut cx| {
 482                        async move {
 483                            let new_text = this.read_with(&cx, |this, cx| {
 484                                this.file.as_ref().and_then(|file| file.load_local(cx))
 485                            });
 486                            if let Some(new_text) = new_text {
 487                                let new_text = new_text.await?;
 488                                let diff = this
 489                                    .read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
 490                                    .await;
 491                                this.update(&mut cx, |this, cx| {
 492                                    if this.apply_diff(diff, cx) {
 493                                        this.saved_version = this.version.clone();
 494                                        this.saved_mtime = new_mtime;
 495                                        cx.emit(Event::Reloaded);
 496                                    }
 497                                });
 498                            }
 499                            Ok(())
 500                        }
 501                        .log_err()
 502                        .map(drop)
 503                    }));
 504                }
 505            }
 506        }
 507
 508        if file_changed {
 509            cx.emit(Event::FileHandleChanged);
 510        }
 511        self.file = Some(new_file);
 512        task
 513    }
 514
 515    pub fn close(&mut self, cx: &mut ModelContext<Self>) {
 516        cx.emit(Event::Closed);
 517    }
 518
 519    pub fn language(&self) -> Option<&Arc<Language>> {
 520        self.language.as_ref()
 521    }
 522
 523    pub fn parse_count(&self) -> usize {
 524        self.parse_count
 525    }
 526
 527    fn syntax_tree(&self) -> Option<Tree> {
 528        if let Some(syntax_tree) = self.syntax_tree.lock().as_mut() {
 529            self.interpolate_tree(syntax_tree);
 530            Some(syntax_tree.tree.clone())
 531        } else {
 532            None
 533        }
 534    }
 535
 536    #[cfg(any(test, feature = "test-support"))]
 537    pub fn is_parsing(&self) -> bool {
 538        self.parsing_in_background
 539    }
 540
 541    #[cfg(test)]
 542    pub fn set_sync_parse_timeout(&mut self, timeout: Duration) {
 543        self.sync_parse_timeout = timeout;
 544    }
 545
 546    fn reparse(&mut self, cx: &mut ModelContext<Self>) -> bool {
 547        if self.parsing_in_background {
 548            return false;
 549        }
 550
 551        if let Some(language) = self.language.clone() {
 552            let old_tree = self.syntax_tree();
 553            let text = self.as_rope().clone();
 554            let parsed_version = self.version();
 555            let parse_task = cx.background().spawn({
 556                let language = language.clone();
 557                async move { Self::parse_text(&text, old_tree, &language) }
 558            });
 559
 560            match cx
 561                .background()
 562                .block_with_timeout(self.sync_parse_timeout, parse_task)
 563            {
 564                Ok(new_tree) => {
 565                    self.did_finish_parsing(new_tree, parsed_version, cx);
 566                    return true;
 567                }
 568                Err(parse_task) => {
 569                    self.parsing_in_background = true;
 570                    cx.spawn(move |this, mut cx| async move {
 571                        let new_tree = parse_task.await;
 572                        this.update(&mut cx, move |this, cx| {
 573                            let language_changed =
 574                                this.language.as_ref().map_or(true, |curr_language| {
 575                                    !Arc::ptr_eq(curr_language, &language)
 576                                });
 577                            let parse_again = this.version > parsed_version || language_changed;
 578                            this.parsing_in_background = false;
 579                            this.did_finish_parsing(new_tree, parsed_version, cx);
 580
 581                            if parse_again && this.reparse(cx) {
 582                                return;
 583                            }
 584                        });
 585                    })
 586                    .detach();
 587                }
 588            }
 589        }
 590        false
 591    }
 592
 593    fn parse_text(text: &Rope, old_tree: Option<Tree>, language: &Language) -> Tree {
 594        PARSER.with(|parser| {
 595            let mut parser = parser.borrow_mut();
 596            parser
 597                .set_language(language.grammar)
 598                .expect("incompatible grammar");
 599            let mut chunks = text.chunks_in_range(0..text.len());
 600            let tree = parser
 601                .parse_with(
 602                    &mut move |offset, _| {
 603                        chunks.seek(offset);
 604                        chunks.next().unwrap_or("").as_bytes()
 605                    },
 606                    old_tree.as_ref(),
 607                )
 608                .unwrap();
 609            tree
 610        })
 611    }
 612
 613    fn interpolate_tree(&self, tree: &mut SyntaxTree) {
 614        let mut delta = 0_isize;
 615        for edit in self.edits_since(tree.version.clone()) {
 616            let start_offset = (edit.old_bytes.start as isize + delta) as usize;
 617            let start_point = self.as_rope().to_point(start_offset);
 618            tree.tree.edit(&InputEdit {
 619                start_byte: start_offset,
 620                old_end_byte: start_offset + edit.deleted_bytes(),
 621                new_end_byte: start_offset + edit.inserted_bytes(),
 622                start_position: start_point.to_ts_point(),
 623                old_end_position: (start_point + edit.deleted_lines()).to_ts_point(),
 624                new_end_position: self
 625                    .as_rope()
 626                    .to_point(start_offset + edit.inserted_bytes())
 627                    .to_ts_point(),
 628            });
 629            delta += edit.inserted_bytes() as isize - edit.deleted_bytes() as isize;
 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        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        self.diagnostics = content.anchor_range_multimap(
 665            Bias::Left,
 666            Bias::Right,
 667            diagnostics.into_iter().map(|diagnostic| {
 668                // TODO: Use UTF-16 positions.
 669                let start = Point::new(
 670                    diagnostic.range.start.line,
 671                    diagnostic.range.start.character,
 672                );
 673                let end = Point::new(diagnostic.range.end.line, diagnostic.range.end.character);
 674                let severity = diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR);
 675                (start..end, (severity, diagnostic.message))
 676            }),
 677        );
 678
 679        if let Some(version) = version {
 680            let language_server = self.language_server.as_mut().unwrap();
 681            let versions_to_delete = language_server
 682                .pending_snapshots
 683                .range(..version)
 684                .map(|(v, _)| *v)
 685                .collect::<Vec<_>>();
 686            for version in versions_to_delete {
 687                language_server.pending_snapshots.remove(&version);
 688            }
 689        }
 690
 691        self.diagnostics_update_count += 1;
 692        cx.notify();
 693        Ok(())
 694    }
 695
 696    pub fn diagnostics_in_range<'a, T: ToOffset>(
 697        &'a self,
 698        range: Range<T>,
 699    ) -> impl Iterator<Item = Diagnostic> + 'a {
 700        let content = self.content();
 701        let range = range.start.to_offset(&content)..range.end.to_offset(&content);
 702        self.diagnostics
 703            .intersecting_ranges(range, content, true)
 704            .map(move |(_, range, (severity, message))| Diagnostic {
 705                range,
 706                severity: *severity,
 707                message: message.clone(),
 708            })
 709    }
 710
 711    pub fn diagnostics_update_count(&self) -> usize {
 712        self.diagnostics_update_count
 713    }
 714
 715    fn request_autoindent(&mut self, cx: &mut ModelContext<Self>) {
 716        if let Some(indent_columns) = self.compute_autoindents() {
 717            let indent_columns = cx.background().spawn(indent_columns);
 718            match cx
 719                .background()
 720                .block_with_timeout(Duration::from_micros(500), indent_columns)
 721            {
 722                Ok(indent_columns) => self.apply_autoindents(indent_columns, cx),
 723                Err(indent_columns) => {
 724                    self.pending_autoindent = Some(cx.spawn(|this, mut cx| async move {
 725                        let indent_columns = indent_columns.await;
 726                        this.update(&mut cx, |this, cx| {
 727                            this.apply_autoindents(indent_columns, cx);
 728                        });
 729                    }));
 730                }
 731            }
 732        }
 733    }
 734
 735    fn compute_autoindents(&self) -> Option<impl Future<Output = BTreeMap<u32, u32>>> {
 736        let max_rows_between_yields = 100;
 737        let snapshot = self.snapshot();
 738        if snapshot.language.is_none()
 739            || snapshot.tree.is_none()
 740            || self.autoindent_requests.is_empty()
 741        {
 742            return None;
 743        }
 744
 745        let autoindent_requests = self.autoindent_requests.clone();
 746        Some(async move {
 747            let mut indent_columns = BTreeMap::new();
 748            for request in autoindent_requests {
 749                let old_to_new_rows = request
 750                    .edited
 751                    .to_points(&request.before_edit)
 752                    .map(|point| point.row)
 753                    .zip(request.edited.to_points(&snapshot).map(|point| point.row))
 754                    .collect::<BTreeMap<u32, u32>>();
 755
 756                let mut old_suggestions = HashMap::<u32, u32>::default();
 757                let old_edited_ranges =
 758                    contiguous_ranges(old_to_new_rows.keys().copied(), max_rows_between_yields);
 759                for old_edited_range in old_edited_ranges {
 760                    let suggestions = request
 761                        .before_edit
 762                        .suggest_autoindents(old_edited_range.clone())
 763                        .into_iter()
 764                        .flatten();
 765                    for (old_row, suggestion) in old_edited_range.zip(suggestions) {
 766                        let indentation_basis = old_to_new_rows
 767                            .get(&suggestion.basis_row)
 768                            .and_then(|from_row| old_suggestions.get(from_row).copied())
 769                            .unwrap_or_else(|| {
 770                                request
 771                                    .before_edit
 772                                    .indent_column_for_line(suggestion.basis_row)
 773                            });
 774                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 775                        old_suggestions.insert(
 776                            *old_to_new_rows.get(&old_row).unwrap(),
 777                            indentation_basis + delta,
 778                        );
 779                    }
 780                    yield_now().await;
 781                }
 782
 783                // At this point, old_suggestions contains the suggested indentation for all edited lines with respect to the state of the
 784                // buffer before the edit, but keyed by the row for these lines after the edits were applied.
 785                let new_edited_row_ranges =
 786                    contiguous_ranges(old_to_new_rows.values().copied(), max_rows_between_yields);
 787                for new_edited_row_range in new_edited_row_ranges {
 788                    let suggestions = snapshot
 789                        .suggest_autoindents(new_edited_row_range.clone())
 790                        .into_iter()
 791                        .flatten();
 792                    for (new_row, suggestion) in new_edited_row_range.zip(suggestions) {
 793                        let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 794                        let new_indentation = indent_columns
 795                            .get(&suggestion.basis_row)
 796                            .copied()
 797                            .unwrap_or_else(|| {
 798                                snapshot.indent_column_for_line(suggestion.basis_row)
 799                            })
 800                            + delta;
 801                        if old_suggestions
 802                            .get(&new_row)
 803                            .map_or(true, |old_indentation| new_indentation != *old_indentation)
 804                        {
 805                            indent_columns.insert(new_row, new_indentation);
 806                        }
 807                    }
 808                    yield_now().await;
 809                }
 810
 811                if let Some(inserted) = request.inserted.as_ref() {
 812                    let inserted_row_ranges = contiguous_ranges(
 813                        inserted
 814                            .to_point_ranges(&snapshot)
 815                            .flat_map(|range| range.start.row..range.end.row + 1),
 816                        max_rows_between_yields,
 817                    );
 818                    for inserted_row_range in inserted_row_ranges {
 819                        let suggestions = snapshot
 820                            .suggest_autoindents(inserted_row_range.clone())
 821                            .into_iter()
 822                            .flatten();
 823                        for (row, suggestion) in inserted_row_range.zip(suggestions) {
 824                            let delta = if suggestion.indent { INDENT_SIZE } else { 0 };
 825                            let new_indentation = indent_columns
 826                                .get(&suggestion.basis_row)
 827                                .copied()
 828                                .unwrap_or_else(|| {
 829                                    snapshot.indent_column_for_line(suggestion.basis_row)
 830                                })
 831                                + delta;
 832                            indent_columns.insert(row, new_indentation);
 833                        }
 834                        yield_now().await;
 835                    }
 836                }
 837            }
 838            indent_columns
 839        })
 840    }
 841
 842    fn apply_autoindents(
 843        &mut self,
 844        indent_columns: BTreeMap<u32, u32>,
 845        cx: &mut ModelContext<Self>,
 846    ) {
 847        let selection_set_ids = self
 848            .autoindent_requests
 849            .drain(..)
 850            .flat_map(|req| req.selection_set_ids.clone())
 851            .collect::<HashSet<_>>();
 852
 853        self.start_transaction(selection_set_ids.iter().copied())
 854            .unwrap();
 855        for (row, indent_column) in &indent_columns {
 856            self.set_indent_column_for_line(*row, *indent_column, cx);
 857        }
 858
 859        for selection_set_id in &selection_set_ids {
 860            if let Ok(set) = self.selection_set(*selection_set_id) {
 861                let new_selections = set
 862                    .selections
 863                    .iter()
 864                    .map(|selection| {
 865                        let start_point = selection.start.to_point(&self.text);
 866                        if start_point.column == 0 {
 867                            let end_point = selection.end.to_point(&self.text);
 868                            let delta = Point::new(
 869                                0,
 870                                indent_columns.get(&start_point.row).copied().unwrap_or(0),
 871                            );
 872                            if delta.column > 0 {
 873                                return Selection {
 874                                    id: selection.id,
 875                                    goal: selection.goal,
 876                                    reversed: selection.reversed,
 877                                    start: self
 878                                        .anchor_at(start_point + delta, selection.start.bias),
 879                                    end: self.anchor_at(end_point + delta, selection.end.bias),
 880                                };
 881                            }
 882                        }
 883                        selection.clone()
 884                    })
 885                    .collect::<Arc<[_]>>();
 886                self.update_selection_set(*selection_set_id, new_selections, cx)
 887                    .unwrap();
 888            }
 889        }
 890
 891        self.end_transaction(selection_set_ids.iter().copied(), cx)
 892            .unwrap();
 893    }
 894
 895    pub fn indent_column_for_line(&self, row: u32) -> u32 {
 896        self.content().indent_column_for_line(row)
 897    }
 898
 899    fn set_indent_column_for_line(&mut self, row: u32, column: u32, cx: &mut ModelContext<Self>) {
 900        let current_column = self.indent_column_for_line(row);
 901        if column > current_column {
 902            let offset = Point::new(row, 0).to_offset(&*self);
 903            self.edit(
 904                [offset..offset],
 905                " ".repeat((column - current_column) as usize),
 906                cx,
 907            );
 908        } else if column < current_column {
 909            self.edit(
 910                [Point::new(row, 0)..Point::new(row, current_column - column)],
 911                "",
 912                cx,
 913            );
 914        }
 915    }
 916
 917    pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
 918        if let Some(tree) = self.syntax_tree() {
 919            let root = tree.root_node();
 920            let range = range.start.to_offset(self)..range.end.to_offset(self);
 921            let mut node = root.descendant_for_byte_range(range.start, range.end);
 922            while node.map_or(false, |n| n.byte_range() == range) {
 923                node = node.unwrap().parent();
 924            }
 925            node.map(|n| n.byte_range())
 926        } else {
 927            None
 928        }
 929    }
 930
 931    pub fn enclosing_bracket_ranges<T: ToOffset>(
 932        &self,
 933        range: Range<T>,
 934    ) -> Option<(Range<usize>, Range<usize>)> {
 935        let (lang, tree) = self.language.as_ref().zip(self.syntax_tree())?;
 936        let open_capture_ix = lang.brackets_query.capture_index_for_name("open")?;
 937        let close_capture_ix = lang.brackets_query.capture_index_for_name("close")?;
 938
 939        // Find bracket pairs that *inclusively* contain the given range.
 940        let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
 941        let mut cursor = QueryCursorHandle::new();
 942        let matches = cursor.set_byte_range(range).matches(
 943            &lang.brackets_query,
 944            tree.root_node(),
 945            TextProvider(self.as_rope()),
 946        );
 947
 948        // Get the ranges of the innermost pair of brackets.
 949        matches
 950            .filter_map(|mat| {
 951                let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
 952                let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
 953                Some((open.byte_range(), close.byte_range()))
 954            })
 955            .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
 956    }
 957
 958    fn diff(&self, new_text: Arc<str>, cx: &AppContext) -> Task<Diff> {
 959        // TODO: it would be nice to not allocate here.
 960        let old_text = self.text();
 961        let base_version = self.version();
 962        cx.background().spawn(async move {
 963            let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_ref())
 964                .iter_all_changes()
 965                .map(|c| (c.tag(), c.value().len()))
 966                .collect::<Vec<_>>();
 967            Diff {
 968                base_version,
 969                new_text,
 970                changes,
 971            }
 972        })
 973    }
 974
 975    fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
 976        if self.version == diff.base_version {
 977            self.start_transaction(None).unwrap();
 978            let mut offset = 0;
 979            for (tag, len) in diff.changes {
 980                let range = offset..(offset + len);
 981                match tag {
 982                    ChangeTag::Equal => offset += len,
 983                    ChangeTag::Delete => self.edit(Some(range), "", cx),
 984                    ChangeTag::Insert => {
 985                        self.edit(Some(offset..offset), &diff.new_text[range], cx);
 986                        offset += len;
 987                    }
 988                }
 989            }
 990            self.end_transaction(None, cx).unwrap();
 991            true
 992        } else {
 993            false
 994        }
 995    }
 996
 997    pub fn is_dirty(&self) -> bool {
 998        self.version > self.saved_version
 999            || self.file.as_ref().map_or(false, |file| file.is_deleted())
1000    }
1001
1002    pub fn has_conflict(&self) -> bool {
1003        self.version > self.saved_version
1004            && self
1005                .file
1006                .as_ref()
1007                .map_or(false, |file| file.mtime() > self.saved_mtime)
1008    }
1009
1010    pub fn start_transaction(
1011        &mut self,
1012        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1013    ) -> Result<()> {
1014        self.start_transaction_at(selection_set_ids, Instant::now())
1015    }
1016
1017    fn start_transaction_at(
1018        &mut self,
1019        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1020        now: Instant,
1021    ) -> Result<()> {
1022        self.text.start_transaction_at(selection_set_ids, now)
1023    }
1024
1025    pub fn end_transaction(
1026        &mut self,
1027        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1028        cx: &mut ModelContext<Self>,
1029    ) -> Result<()> {
1030        self.end_transaction_at(selection_set_ids, Instant::now(), cx)
1031    }
1032
1033    fn end_transaction_at(
1034        &mut self,
1035        selection_set_ids: impl IntoIterator<Item = SelectionSetId>,
1036        now: Instant,
1037        cx: &mut ModelContext<Self>,
1038    ) -> Result<()> {
1039        if let Some(start_version) = self.text.end_transaction_at(selection_set_ids, now) {
1040            let was_dirty = start_version != self.saved_version;
1041            self.did_edit(start_version, was_dirty, cx);
1042        }
1043        Ok(())
1044    }
1045
1046    fn update_language_server(&mut self, cx: &AppContext) {
1047        let language_server = if let Some(language_server) = self.language_server.as_mut() {
1048            language_server
1049        } else {
1050            return;
1051        };
1052        let abs_path = self
1053            .file
1054            .as_ref()
1055            .map_or(Path::new("/").to_path_buf(), |file| {
1056                file.abs_path(cx).unwrap()
1057            });
1058
1059        let version = post_inc(&mut language_server.next_version);
1060        let snapshot = LanguageServerSnapshot {
1061            buffer_snapshot: self.text.snapshot(),
1062            version,
1063            path: Arc::from(abs_path),
1064        };
1065        language_server
1066            .pending_snapshots
1067            .insert(version, snapshot.clone());
1068        let _ = language_server
1069            .latest_snapshot
1070            .blocking_send(Some(snapshot));
1071    }
1072
1073    pub fn edit<I, S, T>(&mut self, ranges_iter: I, new_text: T, cx: &mut ModelContext<Self>)
1074    where
1075        I: IntoIterator<Item = Range<S>>,
1076        S: ToOffset,
1077        T: Into<String>,
1078    {
1079        self.edit_internal(ranges_iter, new_text, false, cx)
1080    }
1081
1082    pub fn edit_with_autoindent<I, S, T>(
1083        &mut self,
1084        ranges_iter: I,
1085        new_text: T,
1086        cx: &mut ModelContext<Self>,
1087    ) where
1088        I: IntoIterator<Item = Range<S>>,
1089        S: ToOffset,
1090        T: Into<String>,
1091    {
1092        self.edit_internal(ranges_iter, new_text, true, cx)
1093    }
1094
1095    pub fn edit_internal<I, S, T>(
1096        &mut self,
1097        ranges_iter: I,
1098        new_text: T,
1099        autoindent: bool,
1100        cx: &mut ModelContext<Self>,
1101    ) where
1102        I: IntoIterator<Item = Range<S>>,
1103        S: ToOffset,
1104        T: Into<String>,
1105    {
1106        let new_text = new_text.into();
1107
1108        // Skip invalid ranges and coalesce contiguous ones.
1109        let mut ranges: Vec<Range<usize>> = Vec::new();
1110        for range in ranges_iter {
1111            let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
1112            if !new_text.is_empty() || !range.is_empty() {
1113                if let Some(prev_range) = ranges.last_mut() {
1114                    if prev_range.end >= range.start {
1115                        prev_range.end = cmp::max(prev_range.end, range.end);
1116                    } else {
1117                        ranges.push(range);
1118                    }
1119                } else {
1120                    ranges.push(range);
1121                }
1122            }
1123        }
1124        if ranges.is_empty() {
1125            return;
1126        }
1127
1128        self.start_transaction(None).unwrap();
1129        self.pending_autoindent.take();
1130        let autoindent_request = if autoindent && self.language.is_some() {
1131            let before_edit = self.snapshot();
1132            let edited = self.content().anchor_set(ranges.iter().filter_map(|range| {
1133                let start = range.start.to_point(&*self);
1134                if new_text.starts_with('\n') && start.column == self.line_len(start.row) {
1135                    None
1136                } else {
1137                    Some((range.start, Bias::Left))
1138                }
1139            }));
1140            Some((before_edit, edited))
1141        } else {
1142            None
1143        };
1144
1145        let first_newline_ix = new_text.find('\n');
1146        let new_text_len = new_text.len();
1147
1148        let edit = self.text.edit(ranges.iter().cloned(), new_text);
1149
1150        if let Some((before_edit, edited)) = autoindent_request {
1151            let mut inserted = None;
1152            if let Some(first_newline_ix) = first_newline_ix {
1153                let mut delta = 0isize;
1154                inserted = Some(self.content().anchor_range_set(ranges.iter().map(|range| {
1155                    let start = (delta + range.start as isize) as usize + first_newline_ix + 1;
1156                    let end = (delta + range.start as isize) as usize + new_text_len;
1157                    delta += (range.end as isize - range.start as isize) + new_text_len as isize;
1158                    (start, Bias::Left)..(end, Bias::Right)
1159                })));
1160            }
1161
1162            let selection_set_ids = self
1163                .text
1164                .peek_undo_stack()
1165                .unwrap()
1166                .starting_selection_set_ids()
1167                .collect();
1168            self.autoindent_requests.push(Arc::new(AutoindentRequest {
1169                selection_set_ids,
1170                before_edit,
1171                edited,
1172                inserted,
1173            }));
1174        }
1175
1176        self.end_transaction(None, cx).unwrap();
1177        self.send_operation(Operation::Edit(edit), cx);
1178    }
1179
1180    fn did_edit(
1181        &mut self,
1182        old_version: clock::Global,
1183        was_dirty: bool,
1184        cx: &mut ModelContext<Self>,
1185    ) {
1186        if self.edits_since(old_version).next().is_none() {
1187            return;
1188        }
1189
1190        self.reparse(cx);
1191        self.update_language_server(cx);
1192
1193        cx.emit(Event::Edited);
1194        if !was_dirty {
1195            cx.emit(Event::Dirtied);
1196        }
1197        cx.notify();
1198    }
1199
1200    pub fn add_selection_set(
1201        &mut self,
1202        selections: impl Into<Arc<[Selection]>>,
1203        cx: &mut ModelContext<Self>,
1204    ) -> SelectionSetId {
1205        let operation = self.text.add_selection_set(selections);
1206        if let Operation::UpdateSelections { set_id, .. } = &operation {
1207            let set_id = *set_id;
1208            cx.notify();
1209            self.send_operation(operation, cx);
1210            set_id
1211        } else {
1212            unreachable!()
1213        }
1214    }
1215
1216    pub fn update_selection_set(
1217        &mut self,
1218        set_id: SelectionSetId,
1219        selections: impl Into<Arc<[Selection]>>,
1220        cx: &mut ModelContext<Self>,
1221    ) -> Result<()> {
1222        let operation = self.text.update_selection_set(set_id, selections)?;
1223        cx.notify();
1224        self.send_operation(operation, cx);
1225        Ok(())
1226    }
1227
1228    pub fn set_active_selection_set(
1229        &mut self,
1230        set_id: Option<SelectionSetId>,
1231        cx: &mut ModelContext<Self>,
1232    ) -> Result<()> {
1233        let operation = self.text.set_active_selection_set(set_id)?;
1234        self.send_operation(operation, cx);
1235        Ok(())
1236    }
1237
1238    pub fn remove_selection_set(
1239        &mut self,
1240        set_id: SelectionSetId,
1241        cx: &mut ModelContext<Self>,
1242    ) -> Result<()> {
1243        let operation = self.text.remove_selection_set(set_id)?;
1244        cx.notify();
1245        self.send_operation(operation, cx);
1246        Ok(())
1247    }
1248
1249    pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1250        &mut self,
1251        ops: I,
1252        cx: &mut ModelContext<Self>,
1253    ) -> Result<()> {
1254        self.pending_autoindent.take();
1255        let was_dirty = self.is_dirty();
1256        let old_version = self.version.clone();
1257        self.text.apply_ops(ops)?;
1258        self.did_edit(old_version, was_dirty, cx);
1259        Ok(())
1260    }
1261
1262    #[cfg(not(test))]
1263    pub fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1264        if let Some(file) = &self.file {
1265            file.buffer_updated(self.remote_id(), operation, cx.as_mut());
1266        }
1267    }
1268
1269    #[cfg(test)]
1270    pub fn send_operation(&mut self, operation: Operation, _: &mut ModelContext<Self>) {
1271        self.operations.push(operation);
1272    }
1273
1274    pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1275        self.text.remove_peer(replica_id);
1276        cx.notify();
1277    }
1278
1279    pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
1280        let was_dirty = self.is_dirty();
1281        let old_version = self.version.clone();
1282
1283        for operation in self.text.undo() {
1284            self.send_operation(operation, cx);
1285        }
1286
1287        self.did_edit(old_version, was_dirty, cx);
1288    }
1289
1290    pub fn redo(&mut self, cx: &mut ModelContext<Self>) {
1291        let was_dirty = self.is_dirty();
1292        let old_version = self.version.clone();
1293
1294        for operation in self.text.redo() {
1295            self.send_operation(operation, cx);
1296        }
1297
1298        self.did_edit(old_version, was_dirty, cx);
1299    }
1300}
1301
1302#[cfg(any(test, feature = "test-support"))]
1303impl Buffer {
1304    pub fn randomly_edit<T>(&mut self, rng: &mut T, old_range_count: usize)
1305    where
1306        T: rand::Rng,
1307    {
1308        self.text.randomly_edit(rng, old_range_count);
1309    }
1310
1311    pub fn randomly_mutate<T>(&mut self, rng: &mut T)
1312    where
1313        T: rand::Rng,
1314    {
1315        self.text.randomly_mutate(rng);
1316    }
1317}
1318
1319impl Entity for Buffer {
1320    type Event = Event;
1321
1322    fn release(&mut self, cx: &mut gpui::MutableAppContext) {
1323        if let Some(file) = self.file.as_ref() {
1324            file.buffer_removed(self.remote_id(), cx);
1325        }
1326    }
1327}
1328
1329// TODO: Do we need to clone a buffer?
1330impl Clone for Buffer {
1331    fn clone(&self) -> Self {
1332        Self {
1333            text: self.text.clone(),
1334            saved_version: self.saved_version.clone(),
1335            saved_mtime: self.saved_mtime,
1336            file: self.file.as_ref().map(|f| f.boxed_clone()),
1337            language: self.language.clone(),
1338            syntax_tree: Mutex::new(self.syntax_tree.lock().clone()),
1339            parsing_in_background: false,
1340            sync_parse_timeout: self.sync_parse_timeout,
1341            parse_count: self.parse_count,
1342            autoindent_requests: Default::default(),
1343            pending_autoindent: Default::default(),
1344            diagnostics: self.diagnostics.clone(),
1345            diagnostics_update_count: self.diagnostics_update_count,
1346            language_server: None,
1347            #[cfg(test)]
1348            operations: self.operations.clone(),
1349        }
1350    }
1351}
1352
1353impl Deref for Buffer {
1354    type Target = TextBuffer;
1355
1356    fn deref(&self) -> &Self::Target {
1357        &self.text
1358    }
1359}
1360
1361impl<'a> From<&'a Buffer> for Content<'a> {
1362    fn from(buffer: &'a Buffer) -> Self {
1363        Self::from(&buffer.text)
1364    }
1365}
1366
1367impl<'a> From<&'a mut Buffer> for Content<'a> {
1368    fn from(buffer: &'a mut Buffer) -> Self {
1369        Self::from(&buffer.text)
1370    }
1371}
1372
1373impl<'a> From<&'a Snapshot> for Content<'a> {
1374    fn from(snapshot: &'a Snapshot) -> Self {
1375        Self::from(&snapshot.text)
1376    }
1377}
1378
1379impl Snapshot {
1380    fn suggest_autoindents<'a>(
1381        &'a self,
1382        row_range: Range<u32>,
1383    ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1384        let mut query_cursor = QueryCursorHandle::new();
1385        if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
1386            let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1387
1388            // Get the "indentation ranges" that intersect this row range.
1389            let indent_capture_ix = language.indents_query.capture_index_for_name("indent");
1390            let end_capture_ix = language.indents_query.capture_index_for_name("end");
1391            query_cursor.set_point_range(
1392                Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1393                    ..Point::new(row_range.end, 0).to_ts_point(),
1394            );
1395            let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1396            for mat in query_cursor.matches(
1397                &language.indents_query,
1398                tree.root_node(),
1399                TextProvider(self.as_rope()),
1400            ) {
1401                let mut node_kind = "";
1402                let mut start: Option<Point> = None;
1403                let mut end: Option<Point> = None;
1404                for capture in mat.captures {
1405                    if Some(capture.index) == indent_capture_ix {
1406                        node_kind = capture.node.kind();
1407                        start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1408                        end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1409                    } else if Some(capture.index) == end_capture_ix {
1410                        end = Some(Point::from_ts_point(capture.node.start_position().into()));
1411                    }
1412                }
1413
1414                if let Some((start, end)) = start.zip(end) {
1415                    if start.row == end.row {
1416                        continue;
1417                    }
1418
1419                    let range = start..end;
1420                    match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1421                        Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1422                        Ok(ix) => {
1423                            let prev_range = &mut indentation_ranges[ix];
1424                            prev_range.0.end = prev_range.0.end.max(range.end);
1425                        }
1426                    }
1427                }
1428            }
1429
1430            let mut prev_row = prev_non_blank_row.unwrap_or(0);
1431            Some(row_range.map(move |row| {
1432                let row_start = Point::new(row, self.indent_column_for_line(row));
1433
1434                let mut indent_from_prev_row = false;
1435                let mut outdent_to_row = u32::MAX;
1436                for (range, _node_kind) in &indentation_ranges {
1437                    if range.start.row >= row {
1438                        break;
1439                    }
1440
1441                    if range.start.row == prev_row && range.end > row_start {
1442                        indent_from_prev_row = true;
1443                    }
1444                    if range.end.row >= prev_row && range.end <= row_start {
1445                        outdent_to_row = outdent_to_row.min(range.start.row);
1446                    }
1447                }
1448
1449                let suggestion = if outdent_to_row == prev_row {
1450                    IndentSuggestion {
1451                        basis_row: prev_row,
1452                        indent: false,
1453                    }
1454                } else if indent_from_prev_row {
1455                    IndentSuggestion {
1456                        basis_row: prev_row,
1457                        indent: true,
1458                    }
1459                } else if outdent_to_row < prev_row {
1460                    IndentSuggestion {
1461                        basis_row: outdent_to_row,
1462                        indent: false,
1463                    }
1464                } else {
1465                    IndentSuggestion {
1466                        basis_row: prev_row,
1467                        indent: false,
1468                    }
1469                };
1470
1471                prev_row = row;
1472                suggestion
1473            }))
1474        } else {
1475            None
1476        }
1477    }
1478
1479    fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1480        while row > 0 {
1481            row -= 1;
1482            if !self.is_line_blank(row) {
1483                return Some(row);
1484            }
1485        }
1486        None
1487    }
1488
1489    fn is_line_blank(&self, row: u32) -> bool {
1490        self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1491            .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1492    }
1493
1494    pub fn highlighted_text_for_range<T: ToOffset>(
1495        &mut self,
1496        range: Range<T>,
1497    ) -> HighlightedChunks {
1498        let range = range.start.to_offset(&*self)..range.end.to_offset(&*self);
1499
1500        let mut diagnostic_endpoints = Vec::<DiagnosticEndpoint>::new();
1501        for (_, range, (severity, _)) in
1502            self.diagnostics
1503                .intersecting_ranges(range.clone(), self.content(), true)
1504        {
1505            diagnostic_endpoints.push(DiagnosticEndpoint {
1506                offset: range.start,
1507                is_start: true,
1508                severity: *severity,
1509            });
1510            diagnostic_endpoints.push(DiagnosticEndpoint {
1511                offset: range.end,
1512                is_start: false,
1513                severity: *severity,
1514            });
1515        }
1516        diagnostic_endpoints.sort_unstable_by_key(|endpoint| endpoint.offset);
1517        let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1518
1519        let chunks = self.text.as_rope().chunks_in_range(range.clone());
1520        let highlights =
1521            if let Some((language, tree)) = self.language.as_ref().zip(self.tree.as_ref()) {
1522                let captures = self.query_cursor.set_byte_range(range.clone()).captures(
1523                    &language.highlights_query,
1524                    tree.root_node(),
1525                    TextProvider(self.text.as_rope()),
1526                );
1527
1528                Some(Highlights {
1529                    captures,
1530                    next_capture: None,
1531                    stack: Default::default(),
1532                    highlight_map: language.highlight_map(),
1533                })
1534            } else {
1535                None
1536            };
1537
1538        HighlightedChunks {
1539            range,
1540            chunks,
1541            diagnostic_endpoints,
1542            error_depth: 0,
1543            warning_depth: 0,
1544            information_depth: 0,
1545            hint_depth: 0,
1546            highlights,
1547        }
1548    }
1549}
1550
1551impl Clone for Snapshot {
1552    fn clone(&self) -> Self {
1553        Self {
1554            text: self.text.clone(),
1555            tree: self.tree.clone(),
1556            diagnostics: self.diagnostics.clone(),
1557            is_parsing: self.is_parsing,
1558            language: self.language.clone(),
1559            query_cursor: QueryCursorHandle::new(),
1560        }
1561    }
1562}
1563
1564impl Deref for Snapshot {
1565    type Target = buffer::Snapshot;
1566
1567    fn deref(&self) -> &Self::Target {
1568        &self.text
1569    }
1570}
1571
1572impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1573    type I = ByteChunks<'a>;
1574
1575    fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1576        ByteChunks(self.0.chunks_in_range(node.byte_range()))
1577    }
1578}
1579
1580struct ByteChunks<'a>(rope::Chunks<'a>);
1581
1582impl<'a> Iterator for ByteChunks<'a> {
1583    type Item = &'a [u8];
1584
1585    fn next(&mut self) -> Option<Self::Item> {
1586        self.0.next().map(str::as_bytes)
1587    }
1588}
1589
1590impl<'a> HighlightedChunks<'a> {
1591    pub fn seek(&mut self, offset: usize) {
1592        self.range.start = offset;
1593        self.chunks.seek(self.range.start);
1594        if let Some(highlights) = self.highlights.as_mut() {
1595            highlights
1596                .stack
1597                .retain(|(end_offset, _)| *end_offset > offset);
1598            if let Some((mat, capture_ix)) = &highlights.next_capture {
1599                let capture = mat.captures[*capture_ix as usize];
1600                if offset >= capture.node.start_byte() {
1601                    let next_capture_end = capture.node.end_byte();
1602                    if offset < next_capture_end {
1603                        highlights.stack.push((
1604                            next_capture_end,
1605                            highlights.highlight_map.get(capture.index),
1606                        ));
1607                    }
1608                    highlights.next_capture.take();
1609                }
1610            }
1611            highlights.captures.set_byte_range(self.range.clone());
1612        }
1613    }
1614
1615    pub fn offset(&self) -> usize {
1616        self.range.start
1617    }
1618
1619    fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
1620        let depth = match endpoint.severity {
1621            DiagnosticSeverity::ERROR => &mut self.error_depth,
1622            DiagnosticSeverity::WARNING => &mut self.warning_depth,
1623            DiagnosticSeverity::INFORMATION => &mut self.information_depth,
1624            DiagnosticSeverity::HINT => &mut self.hint_depth,
1625            _ => return,
1626        };
1627        if endpoint.is_start {
1628            *depth += 1;
1629        } else {
1630            *depth -= 1;
1631        }
1632    }
1633
1634    fn current_diagnostic_severity(&mut self) -> Option<DiagnosticSeverity> {
1635        if self.error_depth > 0 {
1636            Some(DiagnosticSeverity::ERROR)
1637        } else if self.warning_depth > 0 {
1638            Some(DiagnosticSeverity::WARNING)
1639        } else if self.information_depth > 0 {
1640            Some(DiagnosticSeverity::INFORMATION)
1641        } else if self.hint_depth > 0 {
1642            Some(DiagnosticSeverity::HINT)
1643        } else {
1644            None
1645        }
1646    }
1647}
1648
1649impl<'a> Iterator for HighlightedChunks<'a> {
1650    type Item = HighlightedChunk<'a>;
1651
1652    fn next(&mut self) -> Option<Self::Item> {
1653        let mut next_capture_start = usize::MAX;
1654        let mut next_diagnostic_endpoint = usize::MAX;
1655
1656        if let Some(highlights) = self.highlights.as_mut() {
1657            while let Some((parent_capture_end, _)) = highlights.stack.last() {
1658                if *parent_capture_end <= self.range.start {
1659                    highlights.stack.pop();
1660                } else {
1661                    break;
1662                }
1663            }
1664
1665            if highlights.next_capture.is_none() {
1666                highlights.next_capture = highlights.captures.next();
1667            }
1668
1669            while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
1670                let capture = mat.captures[*capture_ix as usize];
1671                if self.range.start < capture.node.start_byte() {
1672                    next_capture_start = capture.node.start_byte();
1673                    break;
1674                } else {
1675                    let highlight_id = highlights.highlight_map.get(capture.index);
1676                    highlights
1677                        .stack
1678                        .push((capture.node.end_byte(), highlight_id));
1679                    highlights.next_capture = highlights.captures.next();
1680                }
1681            }
1682        }
1683
1684        while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
1685            if endpoint.offset <= self.range.start {
1686                self.update_diagnostic_depths(endpoint);
1687                self.diagnostic_endpoints.next();
1688            } else {
1689                next_diagnostic_endpoint = endpoint.offset;
1690                break;
1691            }
1692        }
1693
1694        if let Some(chunk) = self.chunks.peek() {
1695            let chunk_start = self.range.start;
1696            let mut chunk_end = (self.chunks.offset() + chunk.len())
1697                .min(next_capture_start)
1698                .min(next_diagnostic_endpoint);
1699            let mut highlight_id = HighlightId::default();
1700            if let Some((parent_capture_end, parent_highlight_id)) =
1701                self.highlights.as_ref().and_then(|h| h.stack.last())
1702            {
1703                chunk_end = chunk_end.min(*parent_capture_end);
1704                highlight_id = *parent_highlight_id;
1705            }
1706
1707            let slice =
1708                &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
1709            self.range.start = chunk_end;
1710            if self.range.start == self.chunks.offset() + chunk.len() {
1711                self.chunks.next().unwrap();
1712            }
1713
1714            Some(HighlightedChunk {
1715                text: slice,
1716                highlight_id,
1717                diagnostic: self.current_diagnostic_severity(),
1718            })
1719        } else {
1720            None
1721        }
1722    }
1723}
1724
1725impl QueryCursorHandle {
1726    fn new() -> Self {
1727        QueryCursorHandle(Some(
1728            QUERY_CURSORS
1729                .lock()
1730                .pop()
1731                .unwrap_or_else(|| QueryCursor::new()),
1732        ))
1733    }
1734}
1735
1736impl Deref for QueryCursorHandle {
1737    type Target = QueryCursor;
1738
1739    fn deref(&self) -> &Self::Target {
1740        self.0.as_ref().unwrap()
1741    }
1742}
1743
1744impl DerefMut for QueryCursorHandle {
1745    fn deref_mut(&mut self) -> &mut Self::Target {
1746        self.0.as_mut().unwrap()
1747    }
1748}
1749
1750impl Drop for QueryCursorHandle {
1751    fn drop(&mut self) {
1752        let mut cursor = self.0.take().unwrap();
1753        cursor.set_byte_range(0..usize::MAX);
1754        cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
1755        QUERY_CURSORS.lock().push(cursor)
1756    }
1757}
1758
1759trait ToTreeSitterPoint {
1760    fn to_ts_point(self) -> tree_sitter::Point;
1761    fn from_ts_point(point: tree_sitter::Point) -> Self;
1762}
1763
1764impl ToTreeSitterPoint for Point {
1765    fn to_ts_point(self) -> tree_sitter::Point {
1766        tree_sitter::Point::new(self.row as usize, self.column as usize)
1767    }
1768
1769    fn from_ts_point(point: tree_sitter::Point) -> Self {
1770        Point::new(point.row as u32, point.column as u32)
1771    }
1772}
1773
1774fn contiguous_ranges(
1775    values: impl IntoIterator<Item = u32>,
1776    max_len: usize,
1777) -> impl Iterator<Item = Range<u32>> {
1778    let mut values = values.into_iter();
1779    let mut current_range: Option<Range<u32>> = None;
1780    std::iter::from_fn(move || loop {
1781        if let Some(value) = values.next() {
1782            if let Some(range) = &mut current_range {
1783                if value == range.end && range.len() < max_len {
1784                    range.end += 1;
1785                    continue;
1786                }
1787            }
1788
1789            let prev_range = current_range.clone();
1790            current_range = Some(value..(value + 1));
1791            if prev_range.is_some() {
1792                return prev_range;
1793            }
1794        } else {
1795            return current_range.take();
1796        }
1797    })
1798}