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