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