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