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