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 match self.diagnostics.binary_search_by_key(&server_id, |e| e.0) {
1648 Err(ix) => self.diagnostics.insert(ix, (server_id, diagnostics)),
1649 Ok(ix) => self.diagnostics[ix].1 = diagnostics,
1650 };
1651 self.diagnostics_timestamp = lamport_timestamp;
1652 self.diagnostics_update_count += 1;
1653 self.text.lamport_clock.observe(lamport_timestamp);
1654 cx.notify();
1655 cx.emit(Event::DiagnosticsUpdated);
1656 }
1657 }
1658
1659 fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1660 cx.emit(Event::Operation(operation));
1661 }
1662
1663 pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1664 self.remote_selections.remove(&replica_id);
1665 cx.notify();
1666 }
1667
1668 pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1669 let was_dirty = self.is_dirty();
1670 let old_version = self.version.clone();
1671
1672 if let Some((transaction_id, operation)) = self.text.undo() {
1673 self.send_operation(Operation::Buffer(operation), cx);
1674 self.did_edit(&old_version, was_dirty, cx);
1675 Some(transaction_id)
1676 } else {
1677 None
1678 }
1679 }
1680
1681 pub fn undo_to_transaction(
1682 &mut self,
1683 transaction_id: TransactionId,
1684 cx: &mut ModelContext<Self>,
1685 ) -> bool {
1686 let was_dirty = self.is_dirty();
1687 let old_version = self.version.clone();
1688
1689 let operations = self.text.undo_to_transaction(transaction_id);
1690 let undone = !operations.is_empty();
1691 for operation in operations {
1692 self.send_operation(Operation::Buffer(operation), cx);
1693 }
1694 if undone {
1695 self.did_edit(&old_version, was_dirty, cx)
1696 }
1697 undone
1698 }
1699
1700 pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1701 let was_dirty = self.is_dirty();
1702 let old_version = self.version.clone();
1703
1704 if let Some((transaction_id, operation)) = self.text.redo() {
1705 self.send_operation(Operation::Buffer(operation), cx);
1706 self.did_edit(&old_version, was_dirty, cx);
1707 Some(transaction_id)
1708 } else {
1709 None
1710 }
1711 }
1712
1713 pub fn redo_to_transaction(
1714 &mut self,
1715 transaction_id: TransactionId,
1716 cx: &mut ModelContext<Self>,
1717 ) -> bool {
1718 let was_dirty = self.is_dirty();
1719 let old_version = self.version.clone();
1720
1721 let operations = self.text.redo_to_transaction(transaction_id);
1722 let redone = !operations.is_empty();
1723 for operation in operations {
1724 self.send_operation(Operation::Buffer(operation), cx);
1725 }
1726 if redone {
1727 self.did_edit(&old_version, was_dirty, cx)
1728 }
1729 redone
1730 }
1731
1732 pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1733 self.completion_triggers = triggers.clone();
1734 self.completion_triggers_timestamp = self.text.lamport_clock.tick();
1735 self.send_operation(
1736 Operation::UpdateCompletionTriggers {
1737 triggers,
1738 lamport_timestamp: self.completion_triggers_timestamp,
1739 },
1740 cx,
1741 );
1742 cx.notify();
1743 }
1744
1745 pub fn completion_triggers(&self) -> &[String] {
1746 &self.completion_triggers
1747 }
1748}
1749
1750#[cfg(any(test, feature = "test-support"))]
1751impl Buffer {
1752 pub fn edit_via_marked_text(
1753 &mut self,
1754 marked_string: &str,
1755 autoindent_mode: Option<AutoindentMode>,
1756 cx: &mut ModelContext<Self>,
1757 ) {
1758 let edits = self.edits_for_marked_text(marked_string);
1759 self.edit(edits, autoindent_mode, cx);
1760 }
1761
1762 pub fn set_group_interval(&mut self, group_interval: Duration) {
1763 self.text.set_group_interval(group_interval);
1764 }
1765
1766 pub fn randomly_edit<T>(
1767 &mut self,
1768 rng: &mut T,
1769 old_range_count: usize,
1770 cx: &mut ModelContext<Self>,
1771 ) where
1772 T: rand::Rng,
1773 {
1774 let mut edits: Vec<(Range<usize>, String)> = Vec::new();
1775 let mut last_end = None;
1776 for _ in 0..old_range_count {
1777 if last_end.map_or(false, |last_end| last_end >= self.len()) {
1778 break;
1779 }
1780
1781 let new_start = last_end.map_or(0, |last_end| last_end + 1);
1782 let mut range = self.random_byte_range(new_start, rng);
1783 if rng.gen_bool(0.2) {
1784 mem::swap(&mut range.start, &mut range.end);
1785 }
1786 last_end = Some(range.end);
1787
1788 let new_text_len = rng.gen_range(0..10);
1789 let new_text: String = RandomCharIter::new(&mut *rng).take(new_text_len).collect();
1790
1791 edits.push((range, new_text));
1792 }
1793 log::info!("mutating buffer {} with {:?}", self.replica_id(), edits);
1794 self.edit(edits, None, cx);
1795 }
1796
1797 pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1798 let was_dirty = self.is_dirty();
1799 let old_version = self.version.clone();
1800
1801 let ops = self.text.randomly_undo_redo(rng);
1802 if !ops.is_empty() {
1803 for op in ops {
1804 self.send_operation(Operation::Buffer(op), cx);
1805 self.did_edit(&old_version, was_dirty, cx);
1806 }
1807 }
1808 }
1809}
1810
1811impl Entity for Buffer {
1812 type Event = Event;
1813}
1814
1815impl Deref for Buffer {
1816 type Target = TextBuffer;
1817
1818 fn deref(&self) -> &Self::Target {
1819 &self.text
1820 }
1821}
1822
1823impl BufferSnapshot {
1824 pub fn indent_size_for_line(&self, row: u32) -> IndentSize {
1825 indent_size_for_line(self, row)
1826 }
1827
1828 pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
1829 let language_name = self.language_at(position).map(|language| language.name());
1830 let settings = language_settings(language_name.as_deref(), cx);
1831 if settings.hard_tabs {
1832 IndentSize::tab()
1833 } else {
1834 IndentSize::spaces(settings.tab_size.get())
1835 }
1836 }
1837
1838 pub fn suggested_indents(
1839 &self,
1840 rows: impl Iterator<Item = u32>,
1841 single_indent_size: IndentSize,
1842 ) -> BTreeMap<u32, IndentSize> {
1843 let mut result = BTreeMap::new();
1844
1845 for row_range in contiguous_ranges(rows, 10) {
1846 let suggestions = match self.suggest_autoindents(row_range.clone()) {
1847 Some(suggestions) => suggestions,
1848 _ => break,
1849 };
1850
1851 for (row, suggestion) in row_range.zip(suggestions) {
1852 let indent_size = if let Some(suggestion) = suggestion {
1853 result
1854 .get(&suggestion.basis_row)
1855 .copied()
1856 .unwrap_or_else(|| self.indent_size_for_line(suggestion.basis_row))
1857 .with_delta(suggestion.delta, single_indent_size)
1858 } else {
1859 self.indent_size_for_line(row)
1860 };
1861
1862 result.insert(row, indent_size);
1863 }
1864 }
1865
1866 result
1867 }
1868
1869 fn suggest_autoindents(
1870 &self,
1871 row_range: Range<u32>,
1872 ) -> Option<impl Iterator<Item = Option<IndentSuggestion>> + '_> {
1873 let config = &self.language.as_ref()?.config;
1874 let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1875
1876 // Find the suggested indentation ranges based on the syntax tree.
1877 let start = Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0);
1878 let end = Point::new(row_range.end, 0);
1879 let range = (start..end).to_offset(&self.text);
1880 let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1881 Some(&grammar.indents_config.as_ref()?.query)
1882 });
1883 let indent_configs = matches
1884 .grammars()
1885 .iter()
1886 .map(|grammar| grammar.indents_config.as_ref().unwrap())
1887 .collect::<Vec<_>>();
1888
1889 let mut indent_ranges = Vec::<Range<Point>>::new();
1890 let mut outdent_positions = Vec::<Point>::new();
1891 while let Some(mat) = matches.peek() {
1892 let mut start: Option<Point> = None;
1893 let mut end: Option<Point> = None;
1894
1895 let config = &indent_configs[mat.grammar_index];
1896 for capture in mat.captures {
1897 if capture.index == config.indent_capture_ix {
1898 start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1899 end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1900 } else if Some(capture.index) == config.start_capture_ix {
1901 start = Some(Point::from_ts_point(capture.node.end_position()));
1902 } else if Some(capture.index) == config.end_capture_ix {
1903 end = Some(Point::from_ts_point(capture.node.start_position()));
1904 } else if Some(capture.index) == config.outdent_capture_ix {
1905 outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
1906 }
1907 }
1908
1909 matches.advance();
1910 if let Some((start, end)) = start.zip(end) {
1911 if start.row == end.row {
1912 continue;
1913 }
1914
1915 let range = start..end;
1916 match indent_ranges.binary_search_by_key(&range.start, |r| r.start) {
1917 Err(ix) => indent_ranges.insert(ix, range),
1918 Ok(ix) => {
1919 let prev_range = &mut indent_ranges[ix];
1920 prev_range.end = prev_range.end.max(range.end);
1921 }
1922 }
1923 }
1924 }
1925
1926 let mut error_ranges = Vec::<Range<Point>>::new();
1927 let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
1928 Some(&grammar.error_query)
1929 });
1930 while let Some(mat) = matches.peek() {
1931 let node = mat.captures[0].node;
1932 let start = Point::from_ts_point(node.start_position());
1933 let end = Point::from_ts_point(node.end_position());
1934 let range = start..end;
1935 let ix = match error_ranges.binary_search_by_key(&range.start, |r| r.start) {
1936 Ok(ix) | Err(ix) => ix,
1937 };
1938 let mut end_ix = ix;
1939 while let Some(existing_range) = error_ranges.get(end_ix) {
1940 if existing_range.end < end {
1941 end_ix += 1;
1942 } else {
1943 break;
1944 }
1945 }
1946 error_ranges.splice(ix..end_ix, [range]);
1947 matches.advance();
1948 }
1949
1950 outdent_positions.sort();
1951 for outdent_position in outdent_positions {
1952 // find the innermost indent range containing this outdent_position
1953 // set its end to the outdent position
1954 if let Some(range_to_truncate) = indent_ranges
1955 .iter_mut()
1956 .filter(|indent_range| indent_range.contains(&outdent_position))
1957 .last()
1958 {
1959 range_to_truncate.end = outdent_position;
1960 }
1961 }
1962
1963 // Find the suggested indentation increases and decreased based on regexes.
1964 let mut indent_change_rows = Vec::<(u32, Ordering)>::new();
1965 self.for_each_line(
1966 Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0)
1967 ..Point::new(row_range.end, 0),
1968 |row, line| {
1969 if config
1970 .decrease_indent_pattern
1971 .as_ref()
1972 .map_or(false, |regex| regex.is_match(line))
1973 {
1974 indent_change_rows.push((row, Ordering::Less));
1975 }
1976 if config
1977 .increase_indent_pattern
1978 .as_ref()
1979 .map_or(false, |regex| regex.is_match(line))
1980 {
1981 indent_change_rows.push((row + 1, Ordering::Greater));
1982 }
1983 },
1984 );
1985
1986 let mut indent_changes = indent_change_rows.into_iter().peekable();
1987 let mut prev_row = if config.auto_indent_using_last_non_empty_line {
1988 prev_non_blank_row.unwrap_or(0)
1989 } else {
1990 row_range.start.saturating_sub(1)
1991 };
1992 let mut prev_row_start = Point::new(prev_row, self.indent_size_for_line(prev_row).len);
1993 Some(row_range.map(move |row| {
1994 let row_start = Point::new(row, self.indent_size_for_line(row).len);
1995
1996 let mut indent_from_prev_row = false;
1997 let mut outdent_from_prev_row = false;
1998 let mut outdent_to_row = u32::MAX;
1999
2000 while let Some((indent_row, delta)) = indent_changes.peek() {
2001 match indent_row.cmp(&row) {
2002 Ordering::Equal => match delta {
2003 Ordering::Less => outdent_from_prev_row = true,
2004 Ordering::Greater => indent_from_prev_row = true,
2005 _ => {}
2006 },
2007
2008 Ordering::Greater => break,
2009 Ordering::Less => {}
2010 }
2011
2012 indent_changes.next();
2013 }
2014
2015 for range in &indent_ranges {
2016 if range.start.row >= row {
2017 break;
2018 }
2019 if range.start.row == prev_row && range.end > row_start {
2020 indent_from_prev_row = true;
2021 }
2022 if range.end > prev_row_start && range.end <= row_start {
2023 outdent_to_row = outdent_to_row.min(range.start.row);
2024 }
2025 }
2026
2027 let within_error = error_ranges
2028 .iter()
2029 .any(|e| e.start.row < row && e.end > row_start);
2030
2031 let suggestion = if outdent_to_row == prev_row
2032 || (outdent_from_prev_row && indent_from_prev_row)
2033 {
2034 Some(IndentSuggestion {
2035 basis_row: prev_row,
2036 delta: Ordering::Equal,
2037 within_error,
2038 })
2039 } else if indent_from_prev_row {
2040 Some(IndentSuggestion {
2041 basis_row: prev_row,
2042 delta: Ordering::Greater,
2043 within_error,
2044 })
2045 } else if outdent_to_row < prev_row {
2046 Some(IndentSuggestion {
2047 basis_row: outdent_to_row,
2048 delta: Ordering::Equal,
2049 within_error,
2050 })
2051 } else if outdent_from_prev_row {
2052 Some(IndentSuggestion {
2053 basis_row: prev_row,
2054 delta: Ordering::Less,
2055 within_error,
2056 })
2057 } else if config.auto_indent_using_last_non_empty_line || !self.is_line_blank(prev_row)
2058 {
2059 Some(IndentSuggestion {
2060 basis_row: prev_row,
2061 delta: Ordering::Equal,
2062 within_error,
2063 })
2064 } else {
2065 None
2066 };
2067
2068 prev_row = row;
2069 prev_row_start = row_start;
2070 suggestion
2071 }))
2072 }
2073
2074 fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
2075 while row > 0 {
2076 row -= 1;
2077 if !self.is_line_blank(row) {
2078 return Some(row);
2079 }
2080 }
2081 None
2082 }
2083
2084 pub fn chunks<T: ToOffset>(&self, range: Range<T>, language_aware: bool) -> BufferChunks {
2085 let range = range.start.to_offset(self)..range.end.to_offset(self);
2086
2087 let mut syntax = None;
2088 let mut diagnostic_endpoints = Vec::new();
2089 if language_aware {
2090 let captures = self.syntax.captures(range.clone(), &self.text, |grammar| {
2091 grammar.highlights_query.as_ref()
2092 });
2093 let highlight_maps = captures
2094 .grammars()
2095 .into_iter()
2096 .map(|grammar| grammar.highlight_map())
2097 .collect();
2098 syntax = Some((captures, highlight_maps));
2099 for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
2100 diagnostic_endpoints.push(DiagnosticEndpoint {
2101 offset: entry.range.start,
2102 is_start: true,
2103 severity: entry.diagnostic.severity,
2104 is_unnecessary: entry.diagnostic.is_unnecessary,
2105 });
2106 diagnostic_endpoints.push(DiagnosticEndpoint {
2107 offset: entry.range.end,
2108 is_start: false,
2109 severity: entry.diagnostic.severity,
2110 is_unnecessary: entry.diagnostic.is_unnecessary,
2111 });
2112 }
2113 diagnostic_endpoints
2114 .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
2115 }
2116
2117 BufferChunks::new(self.text.as_rope(), range, syntax, diagnostic_endpoints)
2118 }
2119
2120 pub fn for_each_line(&self, range: Range<Point>, mut callback: impl FnMut(u32, &str)) {
2121 let mut line = String::new();
2122 let mut row = range.start.row;
2123 for chunk in self
2124 .as_rope()
2125 .chunks_in_range(range.to_offset(self))
2126 .chain(["\n"])
2127 {
2128 for (newline_ix, text) in chunk.split('\n').enumerate() {
2129 if newline_ix > 0 {
2130 callback(row, &line);
2131 row += 1;
2132 line.clear();
2133 }
2134 line.push_str(text);
2135 }
2136 }
2137 }
2138
2139 pub fn language_at<D: ToOffset>(&self, position: D) -> Option<&Arc<Language>> {
2140 let offset = position.to_offset(self);
2141 self.syntax
2142 .layers_for_range(offset..offset, &self.text)
2143 .filter(|l| l.node.end_byte() > offset)
2144 .last()
2145 .map(|info| info.language)
2146 .or(self.language.as_ref())
2147 }
2148
2149 pub fn settings_at<'a, D: ToOffset>(
2150 &self,
2151 position: D,
2152 cx: &'a AppContext,
2153 ) -> &'a LanguageSettings {
2154 let language = self.language_at(position);
2155 language_settings(language.map(|l| l.name()).as_deref(), cx)
2156 }
2157
2158 pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {
2159 let offset = position.to_offset(self);
2160
2161 if let Some(layer_info) = self
2162 .syntax
2163 .layers_for_range(offset..offset, &self.text)
2164 .filter(|l| l.node.end_byte() > offset)
2165 .last()
2166 {
2167 Some(LanguageScope {
2168 language: layer_info.language.clone(),
2169 override_id: layer_info.override_id(offset, &self.text),
2170 })
2171 } else {
2172 self.language.clone().map(|language| LanguageScope {
2173 language,
2174 override_id: None,
2175 })
2176 }
2177 }
2178
2179 pub fn surrounding_word<T: ToOffset>(&self, start: T) -> (Range<usize>, Option<CharKind>) {
2180 let mut start = start.to_offset(self);
2181 let mut end = start;
2182 let mut next_chars = self.chars_at(start).peekable();
2183 let mut prev_chars = self.reversed_chars_at(start).peekable();
2184 let word_kind = cmp::max(
2185 prev_chars.peek().copied().map(char_kind),
2186 next_chars.peek().copied().map(char_kind),
2187 );
2188
2189 for ch in prev_chars {
2190 if Some(char_kind(ch)) == word_kind && ch != '\n' {
2191 start -= ch.len_utf8();
2192 } else {
2193 break;
2194 }
2195 }
2196
2197 for ch in next_chars {
2198 if Some(char_kind(ch)) == word_kind && ch != '\n' {
2199 end += ch.len_utf8();
2200 } else {
2201 break;
2202 }
2203 }
2204
2205 (start..end, word_kind)
2206 }
2207
2208 pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
2209 let range = range.start.to_offset(self)..range.end.to_offset(self);
2210 let mut result: Option<Range<usize>> = None;
2211 'outer: for layer in self.syntax.layers_for_range(range.clone(), &self.text) {
2212 let mut cursor = layer.node.walk();
2213
2214 // Descend to the first leaf that touches the start of the range,
2215 // and if the range is non-empty, extends beyond the start.
2216 while cursor.goto_first_child_for_byte(range.start).is_some() {
2217 if !range.is_empty() && cursor.node().end_byte() == range.start {
2218 cursor.goto_next_sibling();
2219 }
2220 }
2221
2222 // Ascend to the smallest ancestor that strictly contains the range.
2223 loop {
2224 let node_range = cursor.node().byte_range();
2225 if node_range.start <= range.start
2226 && node_range.end >= range.end
2227 && node_range.len() > range.len()
2228 {
2229 break;
2230 }
2231 if !cursor.goto_parent() {
2232 continue 'outer;
2233 }
2234 }
2235
2236 let left_node = cursor.node();
2237 let mut layer_result = left_node.byte_range();
2238
2239 // For an empty range, try to find another node immediately to the right of the range.
2240 if left_node.end_byte() == range.start {
2241 let mut right_node = None;
2242 while !cursor.goto_next_sibling() {
2243 if !cursor.goto_parent() {
2244 break;
2245 }
2246 }
2247
2248 while cursor.node().start_byte() == range.start {
2249 right_node = Some(cursor.node());
2250 if !cursor.goto_first_child() {
2251 break;
2252 }
2253 }
2254
2255 // If there is a candidate node on both sides of the (empty) range, then
2256 // decide between the two by favoring a named node over an anonymous token.
2257 // If both nodes are the same in that regard, favor the right one.
2258 if let Some(right_node) = right_node {
2259 if right_node.is_named() || !left_node.is_named() {
2260 layer_result = right_node.byte_range();
2261 }
2262 }
2263 }
2264
2265 if let Some(previous_result) = &result {
2266 if previous_result.len() < layer_result.len() {
2267 continue;
2268 }
2269 }
2270 result = Some(layer_result);
2271 }
2272
2273 result
2274 }
2275
2276 pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
2277 self.outline_items_containing(0..self.len(), theme)
2278 .map(Outline::new)
2279 }
2280
2281 pub fn symbols_containing<T: ToOffset>(
2282 &self,
2283 position: T,
2284 theme: Option<&SyntaxTheme>,
2285 ) -> Option<Vec<OutlineItem<Anchor>>> {
2286 let position = position.to_offset(self);
2287 let mut items = self.outline_items_containing(
2288 position.saturating_sub(1)..self.len().min(position + 1),
2289 theme,
2290 )?;
2291 let mut prev_depth = None;
2292 items.retain(|item| {
2293 let result = prev_depth.map_or(true, |prev_depth| item.depth > prev_depth);
2294 prev_depth = Some(item.depth);
2295 result
2296 });
2297 Some(items)
2298 }
2299
2300 fn outline_items_containing(
2301 &self,
2302 range: Range<usize>,
2303 theme: Option<&SyntaxTheme>,
2304 ) -> Option<Vec<OutlineItem<Anchor>>> {
2305 let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2306 grammar.outline_config.as_ref().map(|c| &c.query)
2307 });
2308 let configs = matches
2309 .grammars()
2310 .iter()
2311 .map(|g| g.outline_config.as_ref().unwrap())
2312 .collect::<Vec<_>>();
2313
2314 let mut stack = Vec::<Range<usize>>::new();
2315 let mut items = Vec::new();
2316 while let Some(mat) = matches.peek() {
2317 let config = &configs[mat.grammar_index];
2318 let item_node = mat.captures.iter().find_map(|cap| {
2319 if cap.index == config.item_capture_ix {
2320 Some(cap.node)
2321 } else {
2322 None
2323 }
2324 })?;
2325
2326 let item_range = item_node.byte_range();
2327 if item_range.end < range.start || item_range.start > range.end {
2328 matches.advance();
2329 continue;
2330 }
2331
2332 let mut buffer_ranges = Vec::new();
2333 for capture in mat.captures {
2334 let node_is_name;
2335 if capture.index == config.name_capture_ix {
2336 node_is_name = true;
2337 } else if Some(capture.index) == config.context_capture_ix {
2338 node_is_name = false;
2339 } else {
2340 continue;
2341 }
2342
2343 let mut range = capture.node.start_byte()..capture.node.end_byte();
2344 let start = capture.node.start_position();
2345 if capture.node.end_position().row > start.row {
2346 range.end =
2347 range.start + self.line_len(start.row as u32) as usize - start.column;
2348 }
2349
2350 buffer_ranges.push((range, node_is_name));
2351 }
2352
2353 if buffer_ranges.is_empty() {
2354 continue;
2355 }
2356
2357 let mut text = String::new();
2358 let mut highlight_ranges = Vec::new();
2359 let mut name_ranges = Vec::new();
2360 let mut chunks = self.chunks(
2361 buffer_ranges.first().unwrap().0.start..buffer_ranges.last().unwrap().0.end,
2362 true,
2363 );
2364 for (buffer_range, is_name) in buffer_ranges {
2365 if !text.is_empty() {
2366 text.push(' ');
2367 }
2368 if is_name {
2369 let mut start = text.len();
2370 let end = start + buffer_range.len();
2371
2372 // When multiple names are captured, then the matcheable text
2373 // includes the whitespace in between the names.
2374 if !name_ranges.is_empty() {
2375 start -= 1;
2376 }
2377
2378 name_ranges.push(start..end);
2379 }
2380
2381 let mut offset = buffer_range.start;
2382 chunks.seek(offset);
2383 for mut chunk in chunks.by_ref() {
2384 if chunk.text.len() > buffer_range.end - offset {
2385 chunk.text = &chunk.text[0..(buffer_range.end - offset)];
2386 offset = buffer_range.end;
2387 } else {
2388 offset += chunk.text.len();
2389 }
2390 let style = chunk
2391 .syntax_highlight_id
2392 .zip(theme)
2393 .and_then(|(highlight, theme)| highlight.style(theme));
2394 if let Some(style) = style {
2395 let start = text.len();
2396 let end = start + chunk.text.len();
2397 highlight_ranges.push((start..end, style));
2398 }
2399 text.push_str(chunk.text);
2400 if offset >= buffer_range.end {
2401 break;
2402 }
2403 }
2404 }
2405
2406 matches.advance();
2407 while stack.last().map_or(false, |prev_range| {
2408 prev_range.start > item_range.start || prev_range.end < item_range.end
2409 }) {
2410 stack.pop();
2411 }
2412 stack.push(item_range.clone());
2413
2414 items.push(OutlineItem {
2415 depth: stack.len() - 1,
2416 range: self.anchor_after(item_range.start)..self.anchor_before(item_range.end),
2417 text,
2418 highlight_ranges,
2419 name_ranges,
2420 })
2421 }
2422 Some(items)
2423 }
2424
2425 /// Returns bracket range pairs overlapping or adjacent to `range`
2426 pub fn bracket_ranges<'a, T: ToOffset>(
2427 &'a self,
2428 range: Range<T>,
2429 ) -> impl Iterator<Item = (Range<usize>, Range<usize>)> + 'a {
2430 // Find bracket pairs that *inclusively* contain the given range.
2431 let range = range.start.to_offset(self).saturating_sub(1)
2432 ..self.len().min(range.end.to_offset(self) + 1);
2433
2434 let mut matches = self.syntax.matches(range.clone(), &self.text, |grammar| {
2435 grammar.brackets_config.as_ref().map(|c| &c.query)
2436 });
2437 let configs = matches
2438 .grammars()
2439 .iter()
2440 .map(|grammar| grammar.brackets_config.as_ref().unwrap())
2441 .collect::<Vec<_>>();
2442
2443 iter::from_fn(move || {
2444 while let Some(mat) = matches.peek() {
2445 let mut open = None;
2446 let mut close = None;
2447 let config = &configs[mat.grammar_index];
2448 for capture in mat.captures {
2449 if capture.index == config.open_capture_ix {
2450 open = Some(capture.node.byte_range());
2451 } else if capture.index == config.close_capture_ix {
2452 close = Some(capture.node.byte_range());
2453 }
2454 }
2455
2456 matches.advance();
2457
2458 let Some((open, close)) = open.zip(close) else { continue };
2459
2460 let bracket_range = open.start..=close.end;
2461 if !bracket_range.overlaps(&range) {
2462 continue;
2463 }
2464
2465 return Some((open, close));
2466 }
2467 None
2468 })
2469 }
2470
2471 #[allow(clippy::type_complexity)]
2472 pub fn remote_selections_in_range(
2473 &self,
2474 range: Range<Anchor>,
2475 ) -> impl Iterator<
2476 Item = (
2477 ReplicaId,
2478 bool,
2479 CursorShape,
2480 impl Iterator<Item = &Selection<Anchor>> + '_,
2481 ),
2482 > + '_ {
2483 self.remote_selections
2484 .iter()
2485 .filter(|(replica_id, set)| {
2486 **replica_id != self.text.replica_id() && !set.selections.is_empty()
2487 })
2488 .map(move |(replica_id, set)| {
2489 let start_ix = match set.selections.binary_search_by(|probe| {
2490 probe.end.cmp(&range.start, self).then(Ordering::Greater)
2491 }) {
2492 Ok(ix) | Err(ix) => ix,
2493 };
2494 let end_ix = match set.selections.binary_search_by(|probe| {
2495 probe.start.cmp(&range.end, self).then(Ordering::Less)
2496 }) {
2497 Ok(ix) | Err(ix) => ix,
2498 };
2499
2500 (
2501 *replica_id,
2502 set.line_mode,
2503 set.cursor_shape,
2504 set.selections[start_ix..end_ix].iter(),
2505 )
2506 })
2507 }
2508
2509 pub fn git_diff_hunks_in_row_range<'a>(
2510 &'a self,
2511 range: Range<u32>,
2512 ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2513 self.git_diff.hunks_in_row_range(range, self)
2514 }
2515
2516 pub fn git_diff_hunks_intersecting_range<'a>(
2517 &'a self,
2518 range: Range<Anchor>,
2519 ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2520 self.git_diff.hunks_intersecting_range(range, self)
2521 }
2522
2523 pub fn git_diff_hunks_intersecting_range_rev<'a>(
2524 &'a self,
2525 range: Range<Anchor>,
2526 ) -> impl 'a + Iterator<Item = git::diff::DiffHunk<u32>> {
2527 self.git_diff.hunks_intersecting_range_rev(range, self)
2528 }
2529
2530 pub fn diagnostics_in_range<'a, T, O>(
2531 &'a self,
2532 search_range: Range<T>,
2533 reversed: bool,
2534 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2535 where
2536 T: 'a + Clone + ToOffset,
2537 O: 'a + FromAnchor + Ord,
2538 {
2539 let mut iterators: Vec<_> = self
2540 .diagnostics
2541 .iter()
2542 .map(|(_, collection)| {
2543 collection
2544 .range::<T, O>(search_range.clone(), self, true, reversed)
2545 .peekable()
2546 })
2547 .collect();
2548
2549 std::iter::from_fn(move || {
2550 let (next_ix, _) = iterators
2551 .iter_mut()
2552 .enumerate()
2553 .flat_map(|(ix, iter)| Some((ix, iter.peek()?)))
2554 .min_by(|(_, a), (_, b)| a.range.start.cmp(&b.range.start))?;
2555 iterators[next_ix].next()
2556 })
2557 }
2558
2559 pub fn diagnostic_groups(
2560 &self,
2561 language_server_id: Option<LanguageServerId>,
2562 ) -> Vec<(LanguageServerId, DiagnosticGroup<Anchor>)> {
2563 let mut groups = Vec::new();
2564
2565 if let Some(language_server_id) = language_server_id {
2566 if let Ok(ix) = self
2567 .diagnostics
2568 .binary_search_by_key(&language_server_id, |e| e.0)
2569 {
2570 self.diagnostics[ix]
2571 .1
2572 .groups(language_server_id, &mut groups, self);
2573 }
2574 } else {
2575 for (language_server_id, diagnostics) in self.diagnostics.iter() {
2576 diagnostics.groups(*language_server_id, &mut groups, self);
2577 }
2578 }
2579
2580 groups.sort_by(|(id_a, group_a), (id_b, group_b)| {
2581 let a_start = &group_a.entries[group_a.primary_ix].range.start;
2582 let b_start = &group_b.entries[group_b.primary_ix].range.start;
2583 a_start.cmp(b_start, self).then_with(|| id_a.cmp(&id_b))
2584 });
2585
2586 groups
2587 }
2588
2589 pub fn diagnostic_group<'a, O>(
2590 &'a self,
2591 group_id: usize,
2592 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
2593 where
2594 O: 'a + FromAnchor,
2595 {
2596 self.diagnostics
2597 .iter()
2598 .flat_map(move |(_, set)| set.group(group_id, self))
2599 }
2600
2601 pub fn diagnostics_update_count(&self) -> usize {
2602 self.diagnostics_update_count
2603 }
2604
2605 pub fn parse_count(&self) -> usize {
2606 self.parse_count
2607 }
2608
2609 pub fn selections_update_count(&self) -> usize {
2610 self.selections_update_count
2611 }
2612
2613 pub fn file(&self) -> Option<&Arc<dyn File>> {
2614 self.file.as_ref()
2615 }
2616
2617 pub fn resolve_file_path(&self, cx: &AppContext, include_root: bool) -> Option<PathBuf> {
2618 if let Some(file) = self.file() {
2619 if file.path().file_name().is_none() || include_root {
2620 Some(file.full_path(cx))
2621 } else {
2622 Some(file.path().to_path_buf())
2623 }
2624 } else {
2625 None
2626 }
2627 }
2628
2629 pub fn file_update_count(&self) -> usize {
2630 self.file_update_count
2631 }
2632
2633 pub fn git_diff_update_count(&self) -> usize {
2634 self.git_diff_update_count
2635 }
2636}
2637
2638fn indent_size_for_line(text: &text::BufferSnapshot, row: u32) -> IndentSize {
2639 indent_size_for_text(text.chars_at(Point::new(row, 0)))
2640}
2641
2642pub fn indent_size_for_text(text: impl Iterator<Item = char>) -> IndentSize {
2643 let mut result = IndentSize::spaces(0);
2644 for c in text {
2645 let kind = match c {
2646 ' ' => IndentKind::Space,
2647 '\t' => IndentKind::Tab,
2648 _ => break,
2649 };
2650 if result.len == 0 {
2651 result.kind = kind;
2652 }
2653 result.len += 1;
2654 }
2655 result
2656}
2657
2658impl Clone for BufferSnapshot {
2659 fn clone(&self) -> Self {
2660 Self {
2661 text: self.text.clone(),
2662 git_diff: self.git_diff.clone(),
2663 syntax: self.syntax.clone(),
2664 file: self.file.clone(),
2665 remote_selections: self.remote_selections.clone(),
2666 diagnostics: self.diagnostics.clone(),
2667 selections_update_count: self.selections_update_count,
2668 diagnostics_update_count: self.diagnostics_update_count,
2669 file_update_count: self.file_update_count,
2670 git_diff_update_count: self.git_diff_update_count,
2671 language: self.language.clone(),
2672 parse_count: self.parse_count,
2673 }
2674 }
2675}
2676
2677impl Deref for BufferSnapshot {
2678 type Target = text::BufferSnapshot;
2679
2680 fn deref(&self) -> &Self::Target {
2681 &self.text
2682 }
2683}
2684
2685unsafe impl<'a> Send for BufferChunks<'a> {}
2686
2687impl<'a> BufferChunks<'a> {
2688 pub(crate) fn new(
2689 text: &'a Rope,
2690 range: Range<usize>,
2691 syntax: Option<(SyntaxMapCaptures<'a>, Vec<HighlightMap>)>,
2692 diagnostic_endpoints: Vec<DiagnosticEndpoint>,
2693 ) -> Self {
2694 let mut highlights = None;
2695 if let Some((captures, highlight_maps)) = syntax {
2696 highlights = Some(BufferChunkHighlights {
2697 captures,
2698 next_capture: None,
2699 stack: Default::default(),
2700 highlight_maps,
2701 })
2702 }
2703
2704 let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
2705 let chunks = text.chunks_in_range(range.clone());
2706
2707 BufferChunks {
2708 range,
2709 chunks,
2710 diagnostic_endpoints,
2711 error_depth: 0,
2712 warning_depth: 0,
2713 information_depth: 0,
2714 hint_depth: 0,
2715 unnecessary_depth: 0,
2716 highlights,
2717 }
2718 }
2719
2720 pub fn seek(&mut self, offset: usize) {
2721 self.range.start = offset;
2722 self.chunks.seek(self.range.start);
2723 if let Some(highlights) = self.highlights.as_mut() {
2724 highlights
2725 .stack
2726 .retain(|(end_offset, _)| *end_offset > offset);
2727 if let Some(capture) = &highlights.next_capture {
2728 if offset >= capture.node.start_byte() {
2729 let next_capture_end = capture.node.end_byte();
2730 if offset < next_capture_end {
2731 highlights.stack.push((
2732 next_capture_end,
2733 highlights.highlight_maps[capture.grammar_index].get(capture.index),
2734 ));
2735 }
2736 highlights.next_capture.take();
2737 }
2738 }
2739 highlights.captures.set_byte_range(self.range.clone());
2740 }
2741 }
2742
2743 pub fn offset(&self) -> usize {
2744 self.range.start
2745 }
2746
2747 fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2748 let depth = match endpoint.severity {
2749 DiagnosticSeverity::ERROR => &mut self.error_depth,
2750 DiagnosticSeverity::WARNING => &mut self.warning_depth,
2751 DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2752 DiagnosticSeverity::HINT => &mut self.hint_depth,
2753 _ => return,
2754 };
2755 if endpoint.is_start {
2756 *depth += 1;
2757 } else {
2758 *depth -= 1;
2759 }
2760
2761 if endpoint.is_unnecessary {
2762 if endpoint.is_start {
2763 self.unnecessary_depth += 1;
2764 } else {
2765 self.unnecessary_depth -= 1;
2766 }
2767 }
2768 }
2769
2770 fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2771 if self.error_depth > 0 {
2772 Some(DiagnosticSeverity::ERROR)
2773 } else if self.warning_depth > 0 {
2774 Some(DiagnosticSeverity::WARNING)
2775 } else if self.information_depth > 0 {
2776 Some(DiagnosticSeverity::INFORMATION)
2777 } else if self.hint_depth > 0 {
2778 Some(DiagnosticSeverity::HINT)
2779 } else {
2780 None
2781 }
2782 }
2783
2784 fn current_code_is_unnecessary(&self) -> bool {
2785 self.unnecessary_depth > 0
2786 }
2787}
2788
2789impl<'a> Iterator for BufferChunks<'a> {
2790 type Item = Chunk<'a>;
2791
2792 fn next(&mut self) -> Option<Self::Item> {
2793 let mut next_capture_start = usize::MAX;
2794 let mut next_diagnostic_endpoint = usize::MAX;
2795
2796 if let Some(highlights) = self.highlights.as_mut() {
2797 while let Some((parent_capture_end, _)) = highlights.stack.last() {
2798 if *parent_capture_end <= self.range.start {
2799 highlights.stack.pop();
2800 } else {
2801 break;
2802 }
2803 }
2804
2805 if highlights.next_capture.is_none() {
2806 highlights.next_capture = highlights.captures.next();
2807 }
2808
2809 while let Some(capture) = highlights.next_capture.as_ref() {
2810 if self.range.start < capture.node.start_byte() {
2811 next_capture_start = capture.node.start_byte();
2812 break;
2813 } else {
2814 let highlight_id =
2815 highlights.highlight_maps[capture.grammar_index].get(capture.index);
2816 highlights
2817 .stack
2818 .push((capture.node.end_byte(), highlight_id));
2819 highlights.next_capture = highlights.captures.next();
2820 }
2821 }
2822 }
2823
2824 while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2825 if endpoint.offset <= self.range.start {
2826 self.update_diagnostic_depths(endpoint);
2827 self.diagnostic_endpoints.next();
2828 } else {
2829 next_diagnostic_endpoint = endpoint.offset;
2830 break;
2831 }
2832 }
2833
2834 if let Some(chunk) = self.chunks.peek() {
2835 let chunk_start = self.range.start;
2836 let mut chunk_end = (self.chunks.offset() + chunk.len())
2837 .min(next_capture_start)
2838 .min(next_diagnostic_endpoint);
2839 let mut highlight_id = None;
2840 if let Some(highlights) = self.highlights.as_ref() {
2841 if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2842 chunk_end = chunk_end.min(*parent_capture_end);
2843 highlight_id = Some(*parent_highlight_id);
2844 }
2845 }
2846
2847 let slice =
2848 &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2849 self.range.start = chunk_end;
2850 if self.range.start == self.chunks.offset() + chunk.len() {
2851 self.chunks.next().unwrap();
2852 }
2853
2854 Some(Chunk {
2855 text: slice,
2856 syntax_highlight_id: highlight_id,
2857 diagnostic_severity: self.current_diagnostic_severity(),
2858 is_unnecessary: self.current_code_is_unnecessary(),
2859 ..Default::default()
2860 })
2861 } else {
2862 None
2863 }
2864 }
2865}
2866
2867impl operation_queue::Operation for Operation {
2868 fn lamport_timestamp(&self) -> clock::Lamport {
2869 match self {
2870 Operation::Buffer(_) => {
2871 unreachable!("buffer operations should never be deferred at this layer")
2872 }
2873 Operation::UpdateDiagnostics {
2874 lamport_timestamp, ..
2875 }
2876 | Operation::UpdateSelections {
2877 lamport_timestamp, ..
2878 }
2879 | Operation::UpdateCompletionTriggers {
2880 lamport_timestamp, ..
2881 } => *lamport_timestamp,
2882 }
2883 }
2884}
2885
2886impl Default for Diagnostic {
2887 fn default() -> Self {
2888 Self {
2889 source: Default::default(),
2890 code: None,
2891 severity: DiagnosticSeverity::ERROR,
2892 message: Default::default(),
2893 group_id: 0,
2894 is_primary: false,
2895 is_valid: true,
2896 is_disk_based: false,
2897 is_unnecessary: false,
2898 }
2899 }
2900}
2901
2902impl IndentSize {
2903 pub fn spaces(len: u32) -> Self {
2904 Self {
2905 len,
2906 kind: IndentKind::Space,
2907 }
2908 }
2909
2910 pub fn tab() -> Self {
2911 Self {
2912 len: 1,
2913 kind: IndentKind::Tab,
2914 }
2915 }
2916
2917 pub fn chars(&self) -> impl Iterator<Item = char> {
2918 iter::repeat(self.char()).take(self.len as usize)
2919 }
2920
2921 pub fn char(&self) -> char {
2922 match self.kind {
2923 IndentKind::Space => ' ',
2924 IndentKind::Tab => '\t',
2925 }
2926 }
2927
2928 pub fn with_delta(mut self, direction: Ordering, size: IndentSize) -> Self {
2929 match direction {
2930 Ordering::Less => {
2931 if self.kind == size.kind && self.len >= size.len {
2932 self.len -= size.len;
2933 }
2934 }
2935 Ordering::Equal => {}
2936 Ordering::Greater => {
2937 if self.len == 0 {
2938 self = size;
2939 } else if self.kind == size.kind {
2940 self.len += size.len;
2941 }
2942 }
2943 }
2944 self
2945 }
2946}
2947
2948impl Completion {
2949 pub fn sort_key(&self) -> (usize, &str) {
2950 let kind_key = match self.lsp_completion.kind {
2951 Some(lsp::CompletionItemKind::VARIABLE) => 0,
2952 _ => 1,
2953 };
2954 (kind_key, &self.label.text[self.label.filter_range.clone()])
2955 }
2956
2957 pub fn is_snippet(&self) -> bool {
2958 self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2959 }
2960}
2961
2962pub fn contiguous_ranges(
2963 values: impl Iterator<Item = u32>,
2964 max_len: usize,
2965) -> impl Iterator<Item = Range<u32>> {
2966 let mut values = values;
2967 let mut current_range: Option<Range<u32>> = None;
2968 std::iter::from_fn(move || loop {
2969 if let Some(value) = values.next() {
2970 if let Some(range) = &mut current_range {
2971 if value == range.end && range.len() < max_len {
2972 range.end += 1;
2973 continue;
2974 }
2975 }
2976
2977 let prev_range = current_range.clone();
2978 current_range = Some(value..(value + 1));
2979 if prev_range.is_some() {
2980 return prev_range;
2981 }
2982 } else {
2983 return current_range.take();
2984 }
2985 })
2986}
2987
2988pub fn char_kind(c: char) -> CharKind {
2989 if c.is_whitespace() {
2990 CharKind::Whitespace
2991 } else if c.is_alphanumeric() || c == '_' {
2992 CharKind::Word
2993 } else {
2994 CharKind::Punctuation
2995 }
2996}
2997
2998/// Find all of the ranges of whitespace that occur at the ends of lines
2999/// in the given rope.
3000///
3001/// This could also be done with a regex search, but this implementation
3002/// avoids copying text.
3003pub fn trailing_whitespace_ranges(rope: &Rope) -> Vec<Range<usize>> {
3004 let mut ranges = Vec::new();
3005
3006 let mut offset = 0;
3007 let mut prev_chunk_trailing_whitespace_range = 0..0;
3008 for chunk in rope.chunks() {
3009 let mut prev_line_trailing_whitespace_range = 0..0;
3010 for (i, line) in chunk.split('\n').enumerate() {
3011 let line_end_offset = offset + line.len();
3012 let trimmed_line_len = line.trim_end_matches(|c| matches!(c, ' ' | '\t')).len();
3013 let mut trailing_whitespace_range = (offset + trimmed_line_len)..line_end_offset;
3014
3015 if i == 0 && trimmed_line_len == 0 {
3016 trailing_whitespace_range.start = prev_chunk_trailing_whitespace_range.start;
3017 }
3018 if !prev_line_trailing_whitespace_range.is_empty() {
3019 ranges.push(prev_line_trailing_whitespace_range);
3020 }
3021
3022 offset = line_end_offset + 1;
3023 prev_line_trailing_whitespace_range = trailing_whitespace_range;
3024 }
3025
3026 offset -= 1;
3027 prev_chunk_trailing_whitespace_range = prev_line_trailing_whitespace_range;
3028 }
3029
3030 if !prev_chunk_trailing_whitespace_range.is_empty() {
3031 ranges.push(prev_chunk_trailing_whitespace_range);
3032 }
3033
3034 ranges
3035}