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