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 { local: bool },
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, true, 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 local: bool,
1165 cx: &mut ModelContext<Self>,
1166 ) {
1167 if self.edits_since::<usize>(old_version).next().is_none() {
1168 return;
1169 }
1170
1171 self.reparse(cx);
1172
1173 cx.emit(Event::Edited { local });
1174 if !was_dirty {
1175 cx.emit(Event::Dirtied);
1176 }
1177 cx.notify();
1178 }
1179
1180 fn grammar(&self) -> Option<&Arc<Grammar>> {
1181 self.language.as_ref().and_then(|l| l.grammar.as_ref())
1182 }
1183
1184 pub fn apply_ops<I: IntoIterator<Item = Operation>>(
1185 &mut self,
1186 ops: I,
1187 cx: &mut ModelContext<Self>,
1188 ) -> Result<()> {
1189 self.pending_autoindent.take();
1190 let was_dirty = self.is_dirty();
1191 let old_version = self.version.clone();
1192 let mut deferred_ops = Vec::new();
1193 let buffer_ops = ops
1194 .into_iter()
1195 .filter_map(|op| match op {
1196 Operation::Buffer(op) => Some(op),
1197 _ => {
1198 if self.can_apply_op(&op) {
1199 self.apply_op(op, cx);
1200 } else {
1201 deferred_ops.push(op);
1202 }
1203 None
1204 }
1205 })
1206 .collect::<Vec<_>>();
1207 self.text.apply_ops(buffer_ops)?;
1208 self.deferred_ops.insert(deferred_ops);
1209 self.flush_deferred_ops(cx);
1210 self.did_edit(&old_version, was_dirty, false, cx);
1211 // Notify independently of whether the buffer was edited as the operations could include a
1212 // selection update.
1213 cx.notify();
1214 Ok(())
1215 }
1216
1217 fn flush_deferred_ops(&mut self, cx: &mut ModelContext<Self>) {
1218 let mut deferred_ops = Vec::new();
1219 for op in self.deferred_ops.drain().iter().cloned() {
1220 if self.can_apply_op(&op) {
1221 self.apply_op(op, cx);
1222 } else {
1223 deferred_ops.push(op);
1224 }
1225 }
1226 self.deferred_ops.insert(deferred_ops);
1227 }
1228
1229 fn can_apply_op(&self, operation: &Operation) -> bool {
1230 match operation {
1231 Operation::Buffer(_) => {
1232 unreachable!("buffer operations should never be applied at this layer")
1233 }
1234 Operation::UpdateDiagnostics {
1235 diagnostics: diagnostic_set,
1236 ..
1237 } => diagnostic_set.iter().all(|diagnostic| {
1238 self.text.can_resolve(&diagnostic.range.start)
1239 && self.text.can_resolve(&diagnostic.range.end)
1240 }),
1241 Operation::UpdateSelections { selections, .. } => selections
1242 .iter()
1243 .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
1244 Operation::UpdateCompletionTriggers { .. } => true,
1245 }
1246 }
1247
1248 fn apply_op(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1249 match operation {
1250 Operation::Buffer(_) => {
1251 unreachable!("buffer operations should never be applied at this layer")
1252 }
1253 Operation::UpdateDiagnostics {
1254 diagnostics: diagnostic_set,
1255 lamport_timestamp,
1256 } => {
1257 let snapshot = self.snapshot();
1258 self.apply_diagnostic_update(
1259 DiagnosticSet::from_sorted_entries(diagnostic_set.iter().cloned(), &snapshot),
1260 lamport_timestamp,
1261 cx,
1262 );
1263 }
1264 Operation::UpdateSelections {
1265 selections,
1266 lamport_timestamp,
1267 } => {
1268 if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
1269 if set.lamport_timestamp > lamport_timestamp {
1270 return;
1271 }
1272 }
1273
1274 self.remote_selections.insert(
1275 lamport_timestamp.replica_id,
1276 SelectionSet {
1277 selections,
1278 lamport_timestamp,
1279 },
1280 );
1281 self.text.lamport_clock.observe(lamport_timestamp);
1282 self.selections_update_count += 1;
1283 }
1284 Operation::UpdateCompletionTriggers {
1285 triggers,
1286 lamport_timestamp,
1287 } => {
1288 self.completion_triggers = triggers;
1289 self.text.lamport_clock.observe(lamport_timestamp);
1290 }
1291 }
1292 }
1293
1294 fn apply_diagnostic_update(
1295 &mut self,
1296 diagnostics: DiagnosticSet,
1297 lamport_timestamp: clock::Lamport,
1298 cx: &mut ModelContext<Self>,
1299 ) {
1300 if lamport_timestamp > self.diagnostics_timestamp {
1301 self.diagnostics = diagnostics;
1302 self.diagnostics_timestamp = lamport_timestamp;
1303 self.diagnostics_update_count += 1;
1304 self.text.lamport_clock.observe(lamport_timestamp);
1305 cx.notify();
1306 cx.emit(Event::DiagnosticsUpdated);
1307 }
1308 }
1309
1310 fn send_operation(&mut self, operation: Operation, cx: &mut ModelContext<Self>) {
1311 cx.emit(Event::Operation(operation));
1312 }
1313
1314 pub fn remove_peer(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Self>) {
1315 self.remote_selections.remove(&replica_id);
1316 cx.notify();
1317 }
1318
1319 pub fn undo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1320 let was_dirty = self.is_dirty();
1321 let old_version = self.version.clone();
1322
1323 if let Some((transaction_id, operation)) = self.text.undo() {
1324 self.send_operation(Operation::Buffer(operation), cx);
1325 self.did_edit(&old_version, was_dirty, true, cx);
1326 Some(transaction_id)
1327 } else {
1328 None
1329 }
1330 }
1331
1332 pub fn undo_to_transaction(
1333 &mut self,
1334 transaction_id: TransactionId,
1335 cx: &mut ModelContext<Self>,
1336 ) -> bool {
1337 let was_dirty = self.is_dirty();
1338 let old_version = self.version.clone();
1339
1340 let operations = self.text.undo_to_transaction(transaction_id);
1341 let undone = !operations.is_empty();
1342 for operation in operations {
1343 self.send_operation(Operation::Buffer(operation), cx);
1344 }
1345 if undone {
1346 self.did_edit(&old_version, was_dirty, true, cx)
1347 }
1348 undone
1349 }
1350
1351 pub fn redo(&mut self, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
1352 let was_dirty = self.is_dirty();
1353 let old_version = self.version.clone();
1354
1355 if let Some((transaction_id, operation)) = self.text.redo() {
1356 self.send_operation(Operation::Buffer(operation), cx);
1357 self.did_edit(&old_version, was_dirty, true, cx);
1358 Some(transaction_id)
1359 } else {
1360 None
1361 }
1362 }
1363
1364 pub fn redo_to_transaction(
1365 &mut self,
1366 transaction_id: TransactionId,
1367 cx: &mut ModelContext<Self>,
1368 ) -> bool {
1369 let was_dirty = self.is_dirty();
1370 let old_version = self.version.clone();
1371
1372 let operations = self.text.redo_to_transaction(transaction_id);
1373 let redone = !operations.is_empty();
1374 for operation in operations {
1375 self.send_operation(Operation::Buffer(operation), cx);
1376 }
1377 if redone {
1378 self.did_edit(&old_version, was_dirty, true, cx)
1379 }
1380 redone
1381 }
1382
1383 pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
1384 self.completion_triggers = triggers.clone();
1385 let lamport_timestamp = self.text.lamport_clock.tick();
1386 self.send_operation(
1387 Operation::UpdateCompletionTriggers {
1388 triggers,
1389 lamport_timestamp,
1390 },
1391 cx,
1392 );
1393 cx.notify();
1394 }
1395
1396 pub fn completion_triggers(&self) -> &[String] {
1397 &self.completion_triggers
1398 }
1399}
1400
1401#[cfg(any(test, feature = "test-support"))]
1402impl Buffer {
1403 pub fn set_group_interval(&mut self, group_interval: Duration) {
1404 self.text.set_group_interval(group_interval);
1405 }
1406
1407 pub fn randomly_edit<T>(
1408 &mut self,
1409 rng: &mut T,
1410 old_range_count: usize,
1411 cx: &mut ModelContext<Self>,
1412 ) where
1413 T: rand::Rng,
1414 {
1415 let mut old_ranges: Vec<Range<usize>> = Vec::new();
1416 for _ in 0..old_range_count {
1417 let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
1418 if last_end > self.len() {
1419 break;
1420 }
1421 old_ranges.push(self.text.random_byte_range(last_end, rng));
1422 }
1423 let new_text_len = rng.gen_range(0..10);
1424 let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1425 .take(new_text_len)
1426 .collect();
1427 log::info!(
1428 "mutating buffer {} at {:?}: {:?}",
1429 self.replica_id(),
1430 old_ranges,
1431 new_text
1432 );
1433 self.edit(old_ranges.iter().cloned(), new_text.as_str(), cx);
1434 }
1435
1436 pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng, cx: &mut ModelContext<Self>) {
1437 let was_dirty = self.is_dirty();
1438 let old_version = self.version.clone();
1439
1440 let ops = self.text.randomly_undo_redo(rng);
1441 if !ops.is_empty() {
1442 for op in ops {
1443 self.send_operation(Operation::Buffer(op), cx);
1444 self.did_edit(&old_version, was_dirty, true, cx);
1445 }
1446 }
1447 }
1448}
1449
1450impl Entity for Buffer {
1451 type Event = Event;
1452}
1453
1454impl Deref for Buffer {
1455 type Target = TextBuffer;
1456
1457 fn deref(&self) -> &Self::Target {
1458 &self.text
1459 }
1460}
1461
1462impl BufferSnapshot {
1463 fn suggest_autoindents<'a>(
1464 &'a self,
1465 row_range: Range<u32>,
1466 ) -> Option<impl Iterator<Item = IndentSuggestion> + 'a> {
1467 let mut query_cursor = QueryCursorHandle::new();
1468 if let Some((grammar, tree)) = self.grammar().zip(self.tree.as_ref()) {
1469 let prev_non_blank_row = self.prev_non_blank_row(row_range.start);
1470
1471 // Get the "indentation ranges" that intersect this row range.
1472 let indent_capture_ix = grammar.indents_query.capture_index_for_name("indent");
1473 let end_capture_ix = grammar.indents_query.capture_index_for_name("end");
1474 query_cursor.set_point_range(
1475 Point::new(prev_non_blank_row.unwrap_or(row_range.start), 0).to_ts_point()
1476 ..Point::new(row_range.end, 0).to_ts_point(),
1477 );
1478 let mut indentation_ranges = Vec::<(Range<Point>, &'static str)>::new();
1479 for mat in query_cursor.matches(
1480 &grammar.indents_query,
1481 tree.root_node(),
1482 TextProvider(self.as_rope()),
1483 ) {
1484 let mut node_kind = "";
1485 let mut start: Option<Point> = None;
1486 let mut end: Option<Point> = None;
1487 for capture in mat.captures {
1488 if Some(capture.index) == indent_capture_ix {
1489 node_kind = capture.node.kind();
1490 start.get_or_insert(Point::from_ts_point(capture.node.start_position()));
1491 end.get_or_insert(Point::from_ts_point(capture.node.end_position()));
1492 } else if Some(capture.index) == end_capture_ix {
1493 end = Some(Point::from_ts_point(capture.node.start_position().into()));
1494 }
1495 }
1496
1497 if let Some((start, end)) = start.zip(end) {
1498 if start.row == end.row {
1499 continue;
1500 }
1501
1502 let range = start..end;
1503 match indentation_ranges.binary_search_by_key(&range.start, |r| r.0.start) {
1504 Err(ix) => indentation_ranges.insert(ix, (range, node_kind)),
1505 Ok(ix) => {
1506 let prev_range = &mut indentation_ranges[ix];
1507 prev_range.0.end = prev_range.0.end.max(range.end);
1508 }
1509 }
1510 }
1511 }
1512
1513 let mut prev_row = prev_non_blank_row.unwrap_or(0);
1514 Some(row_range.map(move |row| {
1515 let row_start = Point::new(row, self.indent_column_for_line(row));
1516
1517 let mut indent_from_prev_row = false;
1518 let mut outdent_to_row = u32::MAX;
1519 for (range, _node_kind) in &indentation_ranges {
1520 if range.start.row >= row {
1521 break;
1522 }
1523
1524 if range.start.row == prev_row && range.end > row_start {
1525 indent_from_prev_row = true;
1526 }
1527 if range.end.row >= prev_row && range.end <= row_start {
1528 outdent_to_row = outdent_to_row.min(range.start.row);
1529 }
1530 }
1531
1532 let suggestion = if outdent_to_row == prev_row {
1533 IndentSuggestion {
1534 basis_row: prev_row,
1535 indent: false,
1536 }
1537 } else if indent_from_prev_row {
1538 IndentSuggestion {
1539 basis_row: prev_row,
1540 indent: true,
1541 }
1542 } else if outdent_to_row < prev_row {
1543 IndentSuggestion {
1544 basis_row: outdent_to_row,
1545 indent: false,
1546 }
1547 } else {
1548 IndentSuggestion {
1549 basis_row: prev_row,
1550 indent: false,
1551 }
1552 };
1553
1554 prev_row = row;
1555 suggestion
1556 }))
1557 } else {
1558 None
1559 }
1560 }
1561
1562 fn prev_non_blank_row(&self, mut row: u32) -> Option<u32> {
1563 while row > 0 {
1564 row -= 1;
1565 if !self.is_line_blank(row) {
1566 return Some(row);
1567 }
1568 }
1569 None
1570 }
1571
1572 pub fn chunks<'a, T: ToOffset>(
1573 &'a self,
1574 range: Range<T>,
1575 language_aware: bool,
1576 ) -> BufferChunks<'a> {
1577 let range = range.start.to_offset(self)..range.end.to_offset(self);
1578
1579 let mut tree = None;
1580 let mut diagnostic_endpoints = Vec::new();
1581 if language_aware {
1582 tree = self.tree.as_ref();
1583 for entry in self.diagnostics_in_range::<_, usize>(range.clone(), false) {
1584 diagnostic_endpoints.push(DiagnosticEndpoint {
1585 offset: entry.range.start,
1586 is_start: true,
1587 severity: entry.diagnostic.severity,
1588 is_unnecessary: entry.diagnostic.is_unnecessary,
1589 });
1590 diagnostic_endpoints.push(DiagnosticEndpoint {
1591 offset: entry.range.end,
1592 is_start: false,
1593 severity: entry.diagnostic.severity,
1594 is_unnecessary: entry.diagnostic.is_unnecessary,
1595 });
1596 }
1597 diagnostic_endpoints
1598 .sort_unstable_by_key(|endpoint| (endpoint.offset, !endpoint.is_start));
1599 }
1600
1601 BufferChunks::new(
1602 self.text.as_rope(),
1603 range,
1604 tree,
1605 self.grammar(),
1606 diagnostic_endpoints,
1607 )
1608 }
1609
1610 pub fn language(&self) -> Option<&Arc<Language>> {
1611 self.language.as_ref()
1612 }
1613
1614 fn grammar(&self) -> Option<&Arc<Grammar>> {
1615 self.language
1616 .as_ref()
1617 .and_then(|language| language.grammar.as_ref())
1618 }
1619
1620 pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
1621 let tree = self.tree.as_ref()?;
1622 let range = range.start.to_offset(self)..range.end.to_offset(self);
1623 let mut cursor = tree.root_node().walk();
1624
1625 // Descend to smallest leaf that touches or exceeds the start of the range.
1626 while cursor.goto_first_child_for_byte(range.start).is_some() {}
1627
1628 // Ascend to the smallest ancestor that strictly contains the range.
1629 loop {
1630 let node_range = cursor.node().byte_range();
1631 if node_range.start <= range.start
1632 && node_range.end >= range.end
1633 && node_range.len() > range.len()
1634 {
1635 break;
1636 }
1637 if !cursor.goto_parent() {
1638 break;
1639 }
1640 }
1641
1642 let left_node = cursor.node();
1643
1644 // For an empty range, try to find another node immediately to the right of the range.
1645 if left_node.end_byte() == range.start {
1646 let mut right_node = None;
1647 while !cursor.goto_next_sibling() {
1648 if !cursor.goto_parent() {
1649 break;
1650 }
1651 }
1652
1653 while cursor.node().start_byte() == range.start {
1654 right_node = Some(cursor.node());
1655 if !cursor.goto_first_child() {
1656 break;
1657 }
1658 }
1659
1660 if let Some(right_node) = right_node {
1661 if right_node.is_named() || !left_node.is_named() {
1662 return Some(right_node.byte_range());
1663 }
1664 }
1665 }
1666
1667 Some(left_node.byte_range())
1668 }
1669
1670 pub fn outline(&self, theme: Option<&SyntaxTheme>) -> Option<Outline<Anchor>> {
1671 let tree = self.tree.as_ref()?;
1672 let grammar = self
1673 .language
1674 .as_ref()
1675 .and_then(|language| language.grammar.as_ref())?;
1676
1677 let mut cursor = QueryCursorHandle::new();
1678 let matches = cursor.matches(
1679 &grammar.outline_query,
1680 tree.root_node(),
1681 TextProvider(self.as_rope()),
1682 );
1683
1684 let mut chunks = self.chunks(0..self.len(), true);
1685
1686 let item_capture_ix = grammar.outline_query.capture_index_for_name("item")?;
1687 let name_capture_ix = grammar.outline_query.capture_index_for_name("name")?;
1688 let context_capture_ix = grammar
1689 .outline_query
1690 .capture_index_for_name("context")
1691 .unwrap_or(u32::MAX);
1692
1693 let mut stack = Vec::<Range<usize>>::new();
1694 let items = matches
1695 .filter_map(|mat| {
1696 let item_node = mat.nodes_for_capture_index(item_capture_ix).next()?;
1697 let range = item_node.start_byte()..item_node.end_byte();
1698 let mut text = String::new();
1699 let mut name_ranges = Vec::new();
1700 let mut highlight_ranges = Vec::new();
1701
1702 for capture in mat.captures {
1703 let node_is_name;
1704 if capture.index == name_capture_ix {
1705 node_is_name = true;
1706 } else if capture.index == context_capture_ix {
1707 node_is_name = false;
1708 } else {
1709 continue;
1710 }
1711
1712 let range = capture.node.start_byte()..capture.node.end_byte();
1713 if !text.is_empty() {
1714 text.push(' ');
1715 }
1716 if node_is_name {
1717 let mut start = text.len();
1718 let end = start + range.len();
1719
1720 // When multiple names are captured, then the matcheable text
1721 // includes the whitespace in between the names.
1722 if !name_ranges.is_empty() {
1723 start -= 1;
1724 }
1725
1726 name_ranges.push(start..end);
1727 }
1728
1729 let mut offset = range.start;
1730 chunks.seek(offset);
1731 while let Some(mut chunk) = chunks.next() {
1732 if chunk.text.len() > range.end - offset {
1733 chunk.text = &chunk.text[0..(range.end - offset)];
1734 offset = range.end;
1735 } else {
1736 offset += chunk.text.len();
1737 }
1738 let style = chunk
1739 .syntax_highlight_id
1740 .zip(theme)
1741 .and_then(|(highlight, theme)| highlight.style(theme));
1742 if let Some(style) = style {
1743 let start = text.len();
1744 let end = start + chunk.text.len();
1745 highlight_ranges.push((start..end, style));
1746 }
1747 text.push_str(chunk.text);
1748 if offset >= range.end {
1749 break;
1750 }
1751 }
1752 }
1753
1754 while stack.last().map_or(false, |prev_range| {
1755 !prev_range.contains(&range.start) || !prev_range.contains(&range.end)
1756 }) {
1757 stack.pop();
1758 }
1759 stack.push(range.clone());
1760
1761 Some(OutlineItem {
1762 depth: stack.len() - 1,
1763 range: self.anchor_after(range.start)..self.anchor_before(range.end),
1764 text,
1765 highlight_ranges,
1766 name_ranges,
1767 })
1768 })
1769 .collect::<Vec<_>>();
1770
1771 if items.is_empty() {
1772 None
1773 } else {
1774 Some(Outline::new(items))
1775 }
1776 }
1777
1778 pub fn enclosing_bracket_ranges<T: ToOffset>(
1779 &self,
1780 range: Range<T>,
1781 ) -> Option<(Range<usize>, Range<usize>)> {
1782 let (grammar, tree) = self.grammar().zip(self.tree.as_ref())?;
1783 let open_capture_ix = grammar.brackets_query.capture_index_for_name("open")?;
1784 let close_capture_ix = grammar.brackets_query.capture_index_for_name("close")?;
1785
1786 // Find bracket pairs that *inclusively* contain the given range.
1787 let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
1788 let mut cursor = QueryCursorHandle::new();
1789 let matches = cursor.set_byte_range(range).matches(
1790 &grammar.brackets_query,
1791 tree.root_node(),
1792 TextProvider(self.as_rope()),
1793 );
1794
1795 // Get the ranges of the innermost pair of brackets.
1796 matches
1797 .filter_map(|mat| {
1798 let open = mat.nodes_for_capture_index(open_capture_ix).next()?;
1799 let close = mat.nodes_for_capture_index(close_capture_ix).next()?;
1800 Some((open.byte_range(), close.byte_range()))
1801 })
1802 .min_by_key(|(open_range, close_range)| close_range.end - open_range.start)
1803 }
1804
1805 pub fn remote_selections_in_range<'a>(
1806 &'a self,
1807 range: Range<Anchor>,
1808 ) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = &'a Selection<Anchor>>)>
1809 {
1810 self.remote_selections
1811 .iter()
1812 .filter(|(replica_id, set)| {
1813 **replica_id != self.text.replica_id() && !set.selections.is_empty()
1814 })
1815 .map(move |(replica_id, set)| {
1816 let start_ix = match set.selections.binary_search_by(|probe| {
1817 probe
1818 .end
1819 .cmp(&range.start, self)
1820 .unwrap()
1821 .then(Ordering::Greater)
1822 }) {
1823 Ok(ix) | Err(ix) => ix,
1824 };
1825 let end_ix = match set.selections.binary_search_by(|probe| {
1826 probe
1827 .start
1828 .cmp(&range.end, self)
1829 .unwrap()
1830 .then(Ordering::Less)
1831 }) {
1832 Ok(ix) | Err(ix) => ix,
1833 };
1834
1835 (*replica_id, set.selections[start_ix..end_ix].iter())
1836 })
1837 }
1838
1839 pub fn diagnostics_in_range<'a, T, O>(
1840 &'a self,
1841 search_range: Range<T>,
1842 reversed: bool,
1843 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1844 where
1845 T: 'a + Clone + ToOffset,
1846 O: 'a + FromAnchor,
1847 {
1848 self.diagnostics
1849 .range(search_range.clone(), self, true, reversed)
1850 }
1851
1852 pub fn diagnostic_groups(&self) -> Vec<DiagnosticGroup<Anchor>> {
1853 let mut groups = Vec::new();
1854 self.diagnostics.groups(&mut groups, self);
1855 groups
1856 }
1857
1858 pub fn diagnostic_group<'a, O>(
1859 &'a self,
1860 group_id: usize,
1861 ) -> impl 'a + Iterator<Item = DiagnosticEntry<O>>
1862 where
1863 O: 'a + FromAnchor,
1864 {
1865 self.diagnostics.group(group_id, self)
1866 }
1867
1868 pub fn diagnostics_update_count(&self) -> usize {
1869 self.diagnostics_update_count
1870 }
1871
1872 pub fn parse_count(&self) -> usize {
1873 self.parse_count
1874 }
1875
1876 pub fn selections_update_count(&self) -> usize {
1877 self.selections_update_count
1878 }
1879
1880 pub fn path(&self) -> Option<&Arc<Path>> {
1881 self.path.as_ref()
1882 }
1883
1884 pub fn file_update_count(&self) -> usize {
1885 self.file_update_count
1886 }
1887
1888 pub fn indent_size(&self) -> u32 {
1889 self.indent_size
1890 }
1891}
1892
1893impl Clone for BufferSnapshot {
1894 fn clone(&self) -> Self {
1895 Self {
1896 text: self.text.clone(),
1897 tree: self.tree.clone(),
1898 path: self.path.clone(),
1899 remote_selections: self.remote_selections.clone(),
1900 diagnostics: self.diagnostics.clone(),
1901 selections_update_count: self.selections_update_count,
1902 diagnostics_update_count: self.diagnostics_update_count,
1903 file_update_count: self.file_update_count,
1904 language: self.language.clone(),
1905 parse_count: self.parse_count,
1906 indent_size: self.indent_size,
1907 }
1908 }
1909}
1910
1911impl Deref for BufferSnapshot {
1912 type Target = text::BufferSnapshot;
1913
1914 fn deref(&self) -> &Self::Target {
1915 &self.text
1916 }
1917}
1918
1919impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1920 type I = ByteChunks<'a>;
1921
1922 fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1923 ByteChunks(self.0.chunks_in_range(node.byte_range()))
1924 }
1925}
1926
1927pub(crate) struct ByteChunks<'a>(rope::Chunks<'a>);
1928
1929impl<'a> Iterator for ByteChunks<'a> {
1930 type Item = &'a [u8];
1931
1932 fn next(&mut self) -> Option<Self::Item> {
1933 self.0.next().map(str::as_bytes)
1934 }
1935}
1936
1937unsafe impl<'a> Send for BufferChunks<'a> {}
1938
1939impl<'a> BufferChunks<'a> {
1940 pub(crate) fn new(
1941 text: &'a Rope,
1942 range: Range<usize>,
1943 tree: Option<&'a Tree>,
1944 grammar: Option<&'a Arc<Grammar>>,
1945 diagnostic_endpoints: Vec<DiagnosticEndpoint>,
1946 ) -> Self {
1947 let mut highlights = None;
1948 if let Some((grammar, tree)) = grammar.zip(tree) {
1949 let mut query_cursor = QueryCursorHandle::new();
1950
1951 // TODO - add a Tree-sitter API to remove the need for this.
1952 let cursor = unsafe {
1953 std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
1954 };
1955 let captures = cursor.set_byte_range(range.clone()).captures(
1956 &grammar.highlights_query,
1957 tree.root_node(),
1958 TextProvider(text),
1959 );
1960 highlights = Some(BufferChunkHighlights {
1961 captures,
1962 next_capture: None,
1963 stack: Default::default(),
1964 highlight_map: grammar.highlight_map(),
1965 _query_cursor: query_cursor,
1966 })
1967 }
1968
1969 let diagnostic_endpoints = diagnostic_endpoints.into_iter().peekable();
1970 let chunks = text.chunks_in_range(range.clone());
1971
1972 BufferChunks {
1973 range,
1974 chunks,
1975 diagnostic_endpoints,
1976 error_depth: 0,
1977 warning_depth: 0,
1978 information_depth: 0,
1979 hint_depth: 0,
1980 unnecessary_depth: 0,
1981 highlights,
1982 }
1983 }
1984
1985 pub fn seek(&mut self, offset: usize) {
1986 self.range.start = offset;
1987 self.chunks.seek(self.range.start);
1988 if let Some(highlights) = self.highlights.as_mut() {
1989 highlights
1990 .stack
1991 .retain(|(end_offset, _)| *end_offset > offset);
1992 if let Some((mat, capture_ix)) = &highlights.next_capture {
1993 let capture = mat.captures[*capture_ix as usize];
1994 if offset >= capture.node.start_byte() {
1995 let next_capture_end = capture.node.end_byte();
1996 if offset < next_capture_end {
1997 highlights.stack.push((
1998 next_capture_end,
1999 highlights.highlight_map.get(capture.index),
2000 ));
2001 }
2002 highlights.next_capture.take();
2003 }
2004 }
2005 highlights.captures.set_byte_range(self.range.clone());
2006 }
2007 }
2008
2009 pub fn offset(&self) -> usize {
2010 self.range.start
2011 }
2012
2013 fn update_diagnostic_depths(&mut self, endpoint: DiagnosticEndpoint) {
2014 let depth = match endpoint.severity {
2015 DiagnosticSeverity::ERROR => &mut self.error_depth,
2016 DiagnosticSeverity::WARNING => &mut self.warning_depth,
2017 DiagnosticSeverity::INFORMATION => &mut self.information_depth,
2018 DiagnosticSeverity::HINT => &mut self.hint_depth,
2019 _ => return,
2020 };
2021 if endpoint.is_start {
2022 *depth += 1;
2023 } else {
2024 *depth -= 1;
2025 }
2026
2027 if endpoint.is_unnecessary {
2028 if endpoint.is_start {
2029 self.unnecessary_depth += 1;
2030 } else {
2031 self.unnecessary_depth -= 1;
2032 }
2033 }
2034 }
2035
2036 fn current_diagnostic_severity(&self) -> Option<DiagnosticSeverity> {
2037 if self.error_depth > 0 {
2038 Some(DiagnosticSeverity::ERROR)
2039 } else if self.warning_depth > 0 {
2040 Some(DiagnosticSeverity::WARNING)
2041 } else if self.information_depth > 0 {
2042 Some(DiagnosticSeverity::INFORMATION)
2043 } else if self.hint_depth > 0 {
2044 Some(DiagnosticSeverity::HINT)
2045 } else {
2046 None
2047 }
2048 }
2049
2050 fn current_code_is_unnecessary(&self) -> bool {
2051 self.unnecessary_depth > 0
2052 }
2053}
2054
2055impl<'a> Iterator for BufferChunks<'a> {
2056 type Item = Chunk<'a>;
2057
2058 fn next(&mut self) -> Option<Self::Item> {
2059 let mut next_capture_start = usize::MAX;
2060 let mut next_diagnostic_endpoint = usize::MAX;
2061
2062 if let Some(highlights) = self.highlights.as_mut() {
2063 while let Some((parent_capture_end, _)) = highlights.stack.last() {
2064 if *parent_capture_end <= self.range.start {
2065 highlights.stack.pop();
2066 } else {
2067 break;
2068 }
2069 }
2070
2071 if highlights.next_capture.is_none() {
2072 highlights.next_capture = highlights.captures.next();
2073 }
2074
2075 while let Some((mat, capture_ix)) = highlights.next_capture.as_ref() {
2076 let capture = mat.captures[*capture_ix as usize];
2077 if self.range.start < capture.node.start_byte() {
2078 next_capture_start = capture.node.start_byte();
2079 break;
2080 } else {
2081 let highlight_id = highlights.highlight_map.get(capture.index);
2082 highlights
2083 .stack
2084 .push((capture.node.end_byte(), highlight_id));
2085 highlights.next_capture = highlights.captures.next();
2086 }
2087 }
2088 }
2089
2090 while let Some(endpoint) = self.diagnostic_endpoints.peek().copied() {
2091 if endpoint.offset <= self.range.start {
2092 self.update_diagnostic_depths(endpoint);
2093 self.diagnostic_endpoints.next();
2094 } else {
2095 next_diagnostic_endpoint = endpoint.offset;
2096 break;
2097 }
2098 }
2099
2100 if let Some(chunk) = self.chunks.peek() {
2101 let chunk_start = self.range.start;
2102 let mut chunk_end = (self.chunks.offset() + chunk.len())
2103 .min(next_capture_start)
2104 .min(next_diagnostic_endpoint);
2105 let mut highlight_id = None;
2106 if let Some(highlights) = self.highlights.as_ref() {
2107 if let Some((parent_capture_end, parent_highlight_id)) = highlights.stack.last() {
2108 chunk_end = chunk_end.min(*parent_capture_end);
2109 highlight_id = Some(*parent_highlight_id);
2110 }
2111 }
2112
2113 let slice =
2114 &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
2115 self.range.start = chunk_end;
2116 if self.range.start == self.chunks.offset() + chunk.len() {
2117 self.chunks.next().unwrap();
2118 }
2119
2120 Some(Chunk {
2121 text: slice,
2122 syntax_highlight_id: highlight_id,
2123 highlight_style: None,
2124 diagnostic_severity: self.current_diagnostic_severity(),
2125 is_unnecessary: self.current_code_is_unnecessary(),
2126 })
2127 } else {
2128 None
2129 }
2130 }
2131}
2132
2133impl QueryCursorHandle {
2134 pub(crate) fn new() -> Self {
2135 QueryCursorHandle(Some(
2136 QUERY_CURSORS
2137 .lock()
2138 .pop()
2139 .unwrap_or_else(|| QueryCursor::new()),
2140 ))
2141 }
2142}
2143
2144impl Deref for QueryCursorHandle {
2145 type Target = QueryCursor;
2146
2147 fn deref(&self) -> &Self::Target {
2148 self.0.as_ref().unwrap()
2149 }
2150}
2151
2152impl DerefMut for QueryCursorHandle {
2153 fn deref_mut(&mut self) -> &mut Self::Target {
2154 self.0.as_mut().unwrap()
2155 }
2156}
2157
2158impl Drop for QueryCursorHandle {
2159 fn drop(&mut self) {
2160 let mut cursor = self.0.take().unwrap();
2161 cursor.set_byte_range(0..usize::MAX);
2162 cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
2163 QUERY_CURSORS.lock().push(cursor)
2164 }
2165}
2166
2167trait ToTreeSitterPoint {
2168 fn to_ts_point(self) -> tree_sitter::Point;
2169 fn from_ts_point(point: tree_sitter::Point) -> Self;
2170}
2171
2172impl ToTreeSitterPoint for Point {
2173 fn to_ts_point(self) -> tree_sitter::Point {
2174 tree_sitter::Point::new(self.row as usize, self.column as usize)
2175 }
2176
2177 fn from_ts_point(point: tree_sitter::Point) -> Self {
2178 Point::new(point.row as u32, point.column as u32)
2179 }
2180}
2181
2182impl operation_queue::Operation for Operation {
2183 fn lamport_timestamp(&self) -> clock::Lamport {
2184 match self {
2185 Operation::Buffer(_) => {
2186 unreachable!("buffer operations should never be deferred at this layer")
2187 }
2188 Operation::UpdateDiagnostics {
2189 lamport_timestamp, ..
2190 }
2191 | Operation::UpdateSelections {
2192 lamport_timestamp, ..
2193 }
2194 | Operation::UpdateCompletionTriggers {
2195 lamport_timestamp, ..
2196 } => *lamport_timestamp,
2197 }
2198 }
2199}
2200
2201impl Default for Diagnostic {
2202 fn default() -> Self {
2203 Self {
2204 code: Default::default(),
2205 severity: DiagnosticSeverity::ERROR,
2206 message: Default::default(),
2207 group_id: Default::default(),
2208 is_primary: Default::default(),
2209 is_valid: true,
2210 is_disk_based: false,
2211 is_unnecessary: false,
2212 }
2213 }
2214}
2215
2216impl Completion {
2217 pub fn sort_key(&self) -> (usize, &str) {
2218 let kind_key = match self.lsp_completion.kind {
2219 Some(lsp::CompletionItemKind::VARIABLE) => 0,
2220 _ => 1,
2221 };
2222 (kind_key, &self.label.text[self.label.filter_range.clone()])
2223 }
2224
2225 pub fn is_snippet(&self) -> bool {
2226 self.lsp_completion.insert_text_format == Some(lsp::InsertTextFormat::SNIPPET)
2227 }
2228}
2229
2230pub fn contiguous_ranges(
2231 values: impl Iterator<Item = u32>,
2232 max_len: usize,
2233) -> impl Iterator<Item = Range<u32>> {
2234 let mut values = values.into_iter();
2235 let mut current_range: Option<Range<u32>> = None;
2236 std::iter::from_fn(move || loop {
2237 if let Some(value) = values.next() {
2238 if let Some(range) = &mut current_range {
2239 if value == range.end && range.len() < max_len {
2240 range.end += 1;
2241 continue;
2242 }
2243 }
2244
2245 let prev_range = current_range.clone();
2246 current_range = Some(value..(value + 1));
2247 if prev_range.is_some() {
2248 return prev_range;
2249 }
2250 } else {
2251 return current_range.take();
2252 }
2253 })
2254}
2255
2256pub fn char_kind(c: char) -> CharKind {
2257 if c == '\n' {
2258 CharKind::Newline
2259 } else if c.is_whitespace() {
2260 CharKind::Whitespace
2261 } else if c.is_alphanumeric() || c == '_' {
2262 CharKind::Word
2263 } else {
2264 CharKind::Punctuation
2265 }
2266}