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