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