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