1#[cfg(test)]
2mod syntax_map_tests;
3
4use crate::{Grammar, InjectionConfig, Language, LanguageRegistry};
5use collections::HashMap;
6use futures::FutureExt;
7use parking_lot::Mutex;
8use std::{
9 borrow::Cow,
10 cell::RefCell,
11 cmp::{self, Ordering, Reverse},
12 collections::BinaryHeap,
13 fmt, iter,
14 ops::{Deref, DerefMut, Range},
15 sync::Arc,
16};
17use sum_tree::{Bias, SeekTarget, SumTree};
18use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, Rope, ToOffset, ToPoint};
19use tree_sitter::{
20 Node, Parser, Query, QueryCapture, QueryCaptures, QueryCursor, QueryMatches, Tree,
21};
22
23thread_local! {
24 static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
25}
26
27static QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Mutex::new(vec![]);
28
29#[derive(Default)]
30pub struct SyntaxMap {
31 snapshot: SyntaxSnapshot,
32 language_registry: Option<Arc<LanguageRegistry>>,
33}
34
35#[derive(Clone, Default)]
36pub struct SyntaxSnapshot {
37 layers: SumTree<SyntaxLayer>,
38 parsed_version: clock::Global,
39 interpolated_version: clock::Global,
40 language_registry_version: usize,
41}
42
43#[derive(Default)]
44pub struct SyntaxMapCaptures<'a> {
45 layers: Vec<SyntaxMapCapturesLayer<'a>>,
46 active_layer_count: usize,
47 grammars: Vec<&'a Grammar>,
48}
49
50#[derive(Default)]
51pub struct SyntaxMapMatches<'a> {
52 layers: Vec<SyntaxMapMatchesLayer<'a>>,
53 active_layer_count: usize,
54 grammars: Vec<&'a Grammar>,
55}
56
57#[derive(Debug)]
58pub struct SyntaxMapCapture<'a> {
59 pub depth: usize,
60 pub node: Node<'a>,
61 pub index: u32,
62 pub grammar_index: usize,
63}
64
65#[derive(Debug)]
66pub struct SyntaxMapMatch<'a> {
67 pub depth: usize,
68 pub pattern_index: usize,
69 pub captures: &'a [QueryCapture<'a>],
70 pub grammar_index: usize,
71}
72
73struct SyntaxMapCapturesLayer<'a> {
74 depth: usize,
75 captures: QueryCaptures<'a, 'a, TextProvider<'a>>,
76 next_capture: Option<QueryCapture<'a>>,
77 grammar_index: usize,
78 _query_cursor: QueryCursorHandle,
79}
80
81struct SyntaxMapMatchesLayer<'a> {
82 depth: usize,
83 next_pattern_index: usize,
84 next_captures: Vec<QueryCapture<'a>>,
85 has_next: bool,
86 matches: QueryMatches<'a, 'a, TextProvider<'a>>,
87 grammar_index: usize,
88 _query_cursor: QueryCursorHandle,
89}
90
91#[derive(Clone)]
92struct SyntaxLayer {
93 depth: usize,
94 range: Range<Anchor>,
95 content: SyntaxLayerContent,
96}
97
98#[derive(Clone)]
99enum SyntaxLayerContent {
100 Parsed {
101 tree: tree_sitter::Tree,
102 language: Arc<Language>,
103 },
104 Pending {
105 language_name: Arc<str>,
106 },
107}
108
109impl SyntaxLayerContent {
110 fn language_id(&self) -> Option<usize> {
111 match self {
112 SyntaxLayerContent::Parsed { language, .. } => language.id(),
113 SyntaxLayerContent::Pending { .. } => None,
114 }
115 }
116
117 fn tree(&self) -> Option<&Tree> {
118 match self {
119 SyntaxLayerContent::Parsed { tree, .. } => Some(tree),
120 SyntaxLayerContent::Pending { .. } => None,
121 }
122 }
123}
124
125#[derive(Debug)]
126pub struct SyntaxLayerInfo<'a> {
127 pub depth: usize,
128 pub language: &'a Arc<Language>,
129 tree: &'a Tree,
130 offset: (usize, tree_sitter::Point),
131}
132
133#[derive(Clone)]
134pub struct OwnedSyntaxLayerInfo {
135 pub depth: usize,
136 pub language: Arc<Language>,
137 tree: tree_sitter::Tree,
138 offset: (usize, tree_sitter::Point),
139}
140
141#[derive(Debug, Clone)]
142struct SyntaxLayerSummary {
143 min_depth: usize,
144 max_depth: usize,
145 range: Range<Anchor>,
146 last_layer_range: Range<Anchor>,
147 last_layer_language: Option<usize>,
148 contains_unknown_injections: bool,
149}
150
151#[derive(Clone, Debug)]
152struct SyntaxLayerPosition {
153 depth: usize,
154 range: Range<Anchor>,
155 language: Option<usize>,
156}
157
158#[derive(Clone, Debug)]
159struct ChangeStartPosition {
160 depth: usize,
161 position: Anchor,
162}
163
164#[derive(Clone, Debug)]
165struct SyntaxLayerPositionBeforeChange {
166 position: SyntaxLayerPosition,
167 change: ChangeStartPosition,
168}
169
170struct ParseStep {
171 depth: usize,
172 language: ParseStepLanguage,
173 range: Range<Anchor>,
174 included_ranges: Vec<tree_sitter::Range>,
175 mode: ParseMode,
176}
177
178#[derive(Debug)]
179enum ParseStepLanguage {
180 Loaded { language: Arc<Language> },
181 Pending { name: Arc<str> },
182}
183
184impl ParseStepLanguage {
185 fn id(&self) -> Option<usize> {
186 match self {
187 ParseStepLanguage::Loaded { language } => language.id(),
188 ParseStepLanguage::Pending { .. } => None,
189 }
190 }
191}
192
193enum ParseMode {
194 Single,
195 Combined {
196 parent_layer_range: Range<usize>,
197 parent_layer_changed_ranges: Vec<Range<usize>>,
198 },
199}
200
201#[derive(Debug, PartialEq, Eq)]
202struct ChangedRegion {
203 depth: usize,
204 range: Range<Anchor>,
205}
206
207#[derive(Default)]
208struct ChangeRegionSet(Vec<ChangedRegion>);
209
210struct TextProvider<'a>(&'a Rope);
211
212struct ByteChunks<'a>(text::Chunks<'a>);
213
214struct QueryCursorHandle(Option<QueryCursor>);
215
216impl SyntaxMap {
217 pub fn new() -> Self {
218 Self::default()
219 }
220
221 pub fn set_language_registry(&mut self, registry: Arc<LanguageRegistry>) {
222 self.language_registry = Some(registry);
223 }
224
225 pub fn snapshot(&self) -> SyntaxSnapshot {
226 self.snapshot.clone()
227 }
228
229 pub fn language_registry(&self) -> Option<Arc<LanguageRegistry>> {
230 self.language_registry.clone()
231 }
232
233 pub fn interpolate(&mut self, text: &BufferSnapshot) {
234 self.snapshot.interpolate(text);
235 }
236
237 #[cfg(test)]
238 pub fn reparse(&mut self, language: Arc<Language>, text: &BufferSnapshot) {
239 self.snapshot
240 .reparse(text, self.language_registry.clone(), language);
241 }
242
243 pub fn did_parse(&mut self, snapshot: SyntaxSnapshot) {
244 self.snapshot = snapshot;
245 }
246
247 pub fn clear(&mut self) {
248 self.snapshot = SyntaxSnapshot::default();
249 }
250}
251
252impl SyntaxSnapshot {
253 pub fn is_empty(&self) -> bool {
254 self.layers.is_empty()
255 }
256
257 fn interpolate(&mut self, text: &BufferSnapshot) {
258 let edits = text
259 .anchored_edits_since::<(usize, Point)>(&self.interpolated_version)
260 .collect::<Vec<_>>();
261 self.interpolated_version = text.version().clone();
262
263 if edits.is_empty() {
264 return;
265 }
266
267 let mut layers = SumTree::new();
268 let mut first_edit_ix_for_depth = 0;
269 let mut prev_depth = 0;
270 let mut cursor = self.layers.cursor::<SyntaxLayerSummary>();
271 cursor.next(text);
272
273 'outer: loop {
274 let depth = cursor.end(text).max_depth;
275 if depth > prev_depth {
276 first_edit_ix_for_depth = 0;
277 prev_depth = depth;
278 }
279
280 // Preserve any layers at this depth that precede the first edit.
281 if let Some((_, edit_range)) = edits.get(first_edit_ix_for_depth) {
282 let target = ChangeStartPosition {
283 depth,
284 position: edit_range.start,
285 };
286 if target.cmp(&cursor.start(), text).is_gt() {
287 let slice = cursor.slice(&target, Bias::Left, text);
288 layers.append(slice, text);
289 }
290 }
291 // If this layer follows all of the edits, then preserve it and any
292 // subsequent layers at this same depth.
293 else if cursor.item().is_some() {
294 let slice = cursor.slice(
295 &SyntaxLayerPosition {
296 depth: depth + 1,
297 range: Anchor::MIN..Anchor::MAX,
298 language: None,
299 },
300 Bias::Left,
301 text,
302 );
303 layers.append(slice, text);
304 continue;
305 };
306
307 let Some(layer) = cursor.item() else { break };
308 let (start_byte, start_point) = layer.range.start.summary::<(usize, Point)>(text);
309
310 // Ignore edits that end before the start of this layer, and don't consider them
311 // for any subsequent layers at this same depth.
312 loop {
313 let Some((_, edit_range)) = edits.get(first_edit_ix_for_depth) else { continue 'outer };
314 if edit_range.end.cmp(&layer.range.start, text).is_le() {
315 first_edit_ix_for_depth += 1;
316 } else {
317 break;
318 }
319 }
320
321 let mut layer = layer.clone();
322 if let SyntaxLayerContent::Parsed { tree, .. } = &mut layer.content {
323 for (edit, edit_range) in &edits[first_edit_ix_for_depth..] {
324 // Ignore any edits that follow this layer.
325 if edit_range.start.cmp(&layer.range.end, text).is_ge() {
326 break;
327 }
328
329 // Apply any edits that intersect this layer to the layer's syntax tree.
330 let tree_edit = if edit_range.start.cmp(&layer.range.start, text).is_ge() {
331 tree_sitter::InputEdit {
332 start_byte: edit.new.start.0 - start_byte,
333 old_end_byte: edit.new.start.0 - start_byte
334 + (edit.old.end.0 - edit.old.start.0),
335 new_end_byte: edit.new.end.0 - start_byte,
336 start_position: (edit.new.start.1 - start_point).to_ts_point(),
337 old_end_position: (edit.new.start.1 - start_point
338 + (edit.old.end.1 - edit.old.start.1))
339 .to_ts_point(),
340 new_end_position: (edit.new.end.1 - start_point).to_ts_point(),
341 }
342 } else {
343 let node = tree.root_node();
344 tree_sitter::InputEdit {
345 start_byte: 0,
346 old_end_byte: node.end_byte(),
347 new_end_byte: 0,
348 start_position: Default::default(),
349 old_end_position: node.end_position(),
350 new_end_position: Default::default(),
351 }
352 };
353
354 tree.edit(&tree_edit);
355 }
356
357 debug_assert!(
358 tree.root_node().end_byte() <= text.len(),
359 "tree's size {}, is larger than text size {}",
360 tree.root_node().end_byte(),
361 text.len(),
362 );
363 }
364
365 layers.push(layer, text);
366 cursor.next(text);
367 }
368
369 layers.append(cursor.suffix(&text), &text);
370 drop(cursor);
371 self.layers = layers;
372 }
373
374 pub fn reparse(
375 &mut self,
376 text: &BufferSnapshot,
377 registry: Option<Arc<LanguageRegistry>>,
378 root_language: Arc<Language>,
379 ) {
380 let edit_ranges = text
381 .edits_since::<usize>(&self.parsed_version)
382 .map(|edit| edit.new)
383 .collect::<Vec<_>>();
384 self.reparse_with_ranges(text, root_language.clone(), edit_ranges, registry.as_ref());
385
386 if let Some(registry) = registry {
387 if registry.version() != self.language_registry_version {
388 let mut resolved_injection_ranges = Vec::new();
389 let mut cursor = self
390 .layers
391 .filter::<_, ()>(|summary| summary.contains_unknown_injections);
392 cursor.next(text);
393 while let Some(layer) = cursor.item() {
394 let SyntaxLayerContent::Pending { language_name } = &layer.content else { unreachable!() };
395 if registry
396 .language_for_name_or_extension(language_name)
397 .now_or_never()
398 .and_then(|language| language.ok())
399 .is_some()
400 {
401 resolved_injection_ranges.push(layer.range.to_offset(text));
402 }
403
404 cursor.next(text);
405 }
406 drop(cursor);
407
408 if !resolved_injection_ranges.is_empty() {
409 self.reparse_with_ranges(
410 text,
411 root_language,
412 resolved_injection_ranges,
413 Some(®istry),
414 );
415 }
416 self.language_registry_version = registry.version();
417 }
418 }
419 }
420
421 fn reparse_with_ranges(
422 &mut self,
423 text: &BufferSnapshot,
424 root_language: Arc<Language>,
425 invalidated_ranges: Vec<Range<usize>>,
426 registry: Option<&Arc<LanguageRegistry>>,
427 ) {
428 log::trace!("reparse. invalidated ranges:{:?}", invalidated_ranges);
429
430 let max_depth = self.layers.summary().max_depth;
431 let mut cursor = self.layers.cursor::<SyntaxLayerSummary>();
432 cursor.next(&text);
433 let mut layers = SumTree::new();
434
435 let mut changed_regions = ChangeRegionSet::default();
436 let mut queue = BinaryHeap::new();
437 let mut combined_injection_ranges = HashMap::default();
438 queue.push(ParseStep {
439 depth: 0,
440 language: ParseStepLanguage::Loaded {
441 language: root_language,
442 },
443 included_ranges: vec![tree_sitter::Range {
444 start_byte: 0,
445 end_byte: text.len(),
446 start_point: Point::zero().to_ts_point(),
447 end_point: text.max_point().to_ts_point(),
448 }],
449 range: Anchor::MIN..Anchor::MAX,
450 mode: ParseMode::Single,
451 });
452
453 loop {
454 let step = queue.pop();
455 let position = if let Some(step) = &step {
456 SyntaxLayerPosition {
457 depth: step.depth,
458 range: step.range.clone(),
459 language: step.language.id(),
460 }
461 } else {
462 SyntaxLayerPosition {
463 depth: max_depth + 1,
464 range: Anchor::MAX..Anchor::MAX,
465 language: None,
466 }
467 };
468
469 let mut done = cursor.item().is_none();
470 while !done && position.cmp(&cursor.end(text), &text).is_gt() {
471 done = true;
472
473 let bounded_position = SyntaxLayerPositionBeforeChange {
474 position: position.clone(),
475 change: changed_regions.start_position(),
476 };
477 if bounded_position.cmp(&cursor.start(), &text).is_gt() {
478 let slice = cursor.slice(&bounded_position, Bias::Left, text);
479 if !slice.is_empty() {
480 layers.append(slice, &text);
481 if changed_regions.prune(cursor.end(text), text) {
482 done = false;
483 }
484 }
485 }
486
487 while position.cmp(&cursor.end(text), text).is_gt() {
488 let Some(layer) = cursor.item() else { break };
489
490 if changed_regions.intersects(&layer, text) {
491 if let SyntaxLayerContent::Parsed { language, .. } = &layer.content {
492 log::trace!(
493 "discard layer. language:{}, range:{:?}. changed_regions:{:?}",
494 language.name(),
495 LogAnchorRange(&layer.range, text),
496 LogChangedRegions(&changed_regions, text),
497 );
498 }
499
500 changed_regions.insert(
501 ChangedRegion {
502 depth: layer.depth + 1,
503 range: layer.range.clone(),
504 },
505 text,
506 );
507 } else {
508 layers.push(layer.clone(), text);
509 }
510
511 cursor.next(text);
512 if changed_regions.prune(cursor.end(text), text) {
513 done = false;
514 }
515 }
516 }
517
518 let Some(step) = step else { break };
519 let (step_start_byte, step_start_point) =
520 step.range.start.summary::<(usize, Point)>(text);
521 let step_end_byte = step.range.end.to_offset(text);
522
523 let mut old_layer = cursor.item();
524 if let Some(layer) = old_layer {
525 if layer.range.to_offset(text) == (step_start_byte..step_end_byte)
526 && layer.content.language_id() == step.language.id()
527 {
528 cursor.next(&text);
529 } else {
530 old_layer = None;
531 }
532 }
533
534 let content = match step.language {
535 ParseStepLanguage::Loaded { language } => {
536 let Some(grammar) = language.grammar() else { continue };
537 let tree;
538 let changed_ranges;
539
540 let mut included_ranges = step.included_ranges;
541 for range in &mut included_ranges {
542 range.start_byte -= step_start_byte;
543 range.end_byte -= step_start_byte;
544 range.start_point = (Point::from_ts_point(range.start_point)
545 - step_start_point)
546 .to_ts_point();
547 range.end_point = (Point::from_ts_point(range.end_point)
548 - step_start_point)
549 .to_ts_point();
550 }
551
552 if let Some((SyntaxLayerContent::Parsed { tree: old_tree, .. }, layer_start)) =
553 old_layer.map(|layer| (&layer.content, layer.range.start))
554 {
555 log::trace!(
556 "existing layer. language:{}, start:{:?}, ranges:{:?}",
557 language.name(),
558 LogPoint(layer_start.to_point(&text)),
559 LogIncludedRanges(&old_tree.included_ranges())
560 );
561
562 if let ParseMode::Combined {
563 mut parent_layer_changed_ranges,
564 ..
565 } = step.mode
566 {
567 for range in &mut parent_layer_changed_ranges {
568 range.start = range.start.saturating_sub(step_start_byte);
569 range.end = range.end.saturating_sub(step_start_byte);
570 }
571
572 included_ranges = splice_included_ranges(
573 old_tree.included_ranges(),
574 &parent_layer_changed_ranges,
575 &included_ranges,
576 );
577 }
578
579 if included_ranges.is_empty() {
580 included_ranges.push(tree_sitter::Range {
581 start_byte: 0,
582 end_byte: 0,
583 start_point: Default::default(),
584 end_point: Default::default(),
585 });
586 }
587
588 log::trace!(
589 "update layer. language:{}, start:{:?}, ranges:{:?}",
590 language.name(),
591 LogAnchorRange(&step.range, text),
592 LogIncludedRanges(&included_ranges),
593 );
594
595 tree = parse_text(
596 grammar,
597 text.as_rope(),
598 step_start_byte,
599 included_ranges,
600 Some(old_tree.clone()),
601 );
602 changed_ranges = join_ranges(
603 invalidated_ranges.iter().cloned().filter(|range| {
604 range.start <= step_end_byte && range.end >= step_start_byte
605 }),
606 old_tree.changed_ranges(&tree).map(|r| {
607 step_start_byte + r.start_byte..step_start_byte + r.end_byte
608 }),
609 );
610 } else {
611 if included_ranges.is_empty() {
612 included_ranges.push(tree_sitter::Range {
613 start_byte: 0,
614 end_byte: 0,
615 start_point: Default::default(),
616 end_point: Default::default(),
617 });
618 }
619
620 log::trace!(
621 "create layer. language:{}, range:{:?}, included_ranges:{:?}",
622 language.name(),
623 LogAnchorRange(&step.range, text),
624 LogIncludedRanges(&included_ranges),
625 );
626
627 tree = parse_text(
628 grammar,
629 text.as_rope(),
630 step_start_byte,
631 included_ranges,
632 None,
633 );
634 changed_ranges = vec![step_start_byte..step_end_byte];
635 }
636
637 if let (Some((config, registry)), false) = (
638 grammar.injection_config.as_ref().zip(registry.as_ref()),
639 changed_ranges.is_empty(),
640 ) {
641 for range in &changed_ranges {
642 changed_regions.insert(
643 ChangedRegion {
644 depth: step.depth + 1,
645 range: text.anchor_before(range.start)
646 ..text.anchor_after(range.end),
647 },
648 text,
649 );
650 }
651 get_injections(
652 config,
653 text,
654 step.range.clone(),
655 tree.root_node_with_offset(
656 step_start_byte,
657 step_start_point.to_ts_point(),
658 ),
659 registry,
660 step.depth + 1,
661 &changed_ranges,
662 &mut combined_injection_ranges,
663 &mut queue,
664 );
665 }
666
667 SyntaxLayerContent::Parsed { tree, language }
668 }
669 ParseStepLanguage::Pending { name } => SyntaxLayerContent::Pending {
670 language_name: name,
671 },
672 };
673
674 layers.push(
675 SyntaxLayer {
676 depth: step.depth,
677 range: step.range,
678 content,
679 },
680 &text,
681 );
682 }
683
684 drop(cursor);
685 self.layers = layers;
686 self.interpolated_version = text.version.clone();
687 self.parsed_version = text.version.clone();
688 #[cfg(debug_assertions)]
689 self.check_invariants(text);
690 }
691
692 #[cfg(debug_assertions)]
693 fn check_invariants(&self, text: &BufferSnapshot) {
694 let mut max_depth = 0;
695 let mut prev_range: Option<Range<Anchor>> = None;
696 for layer in self.layers.iter() {
697 if layer.depth == max_depth {
698 if let Some(prev_range) = prev_range {
699 match layer.range.start.cmp(&prev_range.start, text) {
700 Ordering::Less => panic!("layers out of order"),
701 Ordering::Equal => {
702 assert!(layer.range.end.cmp(&prev_range.end, text).is_ge())
703 }
704 Ordering::Greater => {}
705 }
706 }
707 } else if layer.depth < max_depth {
708 panic!("layers out of order")
709 }
710 max_depth = layer.depth;
711 prev_range = Some(layer.range.clone());
712 }
713 }
714
715 pub fn single_tree_captures<'a>(
716 range: Range<usize>,
717 text: &'a Rope,
718 tree: &'a Tree,
719 language: &'a Arc<Language>,
720 query: fn(&Grammar) -> Option<&Query>,
721 ) -> SyntaxMapCaptures<'a> {
722 SyntaxMapCaptures::new(
723 range.clone(),
724 text,
725 [SyntaxLayerInfo {
726 language,
727 tree,
728 depth: 0,
729 offset: (0, tree_sitter::Point::new(0, 0)),
730 }]
731 .into_iter(),
732 query,
733 )
734 }
735
736 pub fn captures<'a>(
737 &'a self,
738 range: Range<usize>,
739 buffer: &'a BufferSnapshot,
740 query: fn(&Grammar) -> Option<&Query>,
741 ) -> SyntaxMapCaptures {
742 SyntaxMapCaptures::new(
743 range.clone(),
744 buffer.as_rope(),
745 self.layers_for_range(range, buffer).into_iter(),
746 query,
747 )
748 }
749
750 pub fn matches<'a>(
751 &'a self,
752 range: Range<usize>,
753 buffer: &'a BufferSnapshot,
754 query: fn(&Grammar) -> Option<&Query>,
755 ) -> SyntaxMapMatches {
756 SyntaxMapMatches::new(
757 range.clone(),
758 buffer.as_rope(),
759 self.layers_for_range(range, buffer).into_iter(),
760 query,
761 )
762 }
763
764 #[cfg(test)]
765 pub fn layers<'a>(&'a self, buffer: &'a BufferSnapshot) -> Vec<SyntaxLayerInfo> {
766 self.layers_for_range(0..buffer.len(), buffer).collect()
767 }
768
769 pub fn layers_for_range<'a, T: ToOffset>(
770 &'a self,
771 range: Range<T>,
772 buffer: &'a BufferSnapshot,
773 ) -> impl 'a + Iterator<Item = SyntaxLayerInfo> {
774 let start = buffer.anchor_before(range.start.to_offset(buffer));
775 let end = buffer.anchor_after(range.end.to_offset(buffer));
776
777 let mut cursor = self.layers.filter::<_, ()>(move |summary| {
778 if summary.max_depth > summary.min_depth {
779 true
780 } else {
781 let is_before_start = summary.range.end.cmp(&start, buffer).is_lt();
782 let is_after_end = summary.range.start.cmp(&end, buffer).is_gt();
783 !is_before_start && !is_after_end
784 }
785 });
786
787 cursor.next(buffer);
788 iter::from_fn(move || {
789 while let Some(layer) = cursor.item() {
790 if let SyntaxLayerContent::Parsed { tree, language } = &layer.content {
791 let info = SyntaxLayerInfo {
792 tree,
793 language,
794 depth: layer.depth,
795 offset: (
796 layer.range.start.to_offset(buffer),
797 layer.range.start.to_point(buffer).to_ts_point(),
798 ),
799 };
800 cursor.next(buffer);
801 return Some(info);
802 } else {
803 cursor.next(buffer);
804 }
805 }
806 None
807 })
808 }
809
810 pub fn contains_unknown_injections(&self) -> bool {
811 self.layers.summary().contains_unknown_injections
812 }
813
814 pub fn language_registry_version(&self) -> usize {
815 self.language_registry_version
816 }
817}
818
819impl<'a> SyntaxMapCaptures<'a> {
820 fn new(
821 range: Range<usize>,
822 text: &'a Rope,
823 layers: impl Iterator<Item = SyntaxLayerInfo<'a>>,
824 query: fn(&Grammar) -> Option<&Query>,
825 ) -> Self {
826 let mut result = Self {
827 layers: Vec::new(),
828 grammars: Vec::new(),
829 active_layer_count: 0,
830 };
831 for layer in layers {
832 let grammar = match &layer.language.grammar {
833 Some(grammar) => grammar,
834 None => continue,
835 };
836 let query = match query(&grammar) {
837 Some(query) => query,
838 None => continue,
839 };
840
841 let mut query_cursor = QueryCursorHandle::new();
842
843 // TODO - add a Tree-sitter API to remove the need for this.
844 let cursor = unsafe {
845 std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
846 };
847
848 cursor.set_byte_range(range.clone());
849 let captures = cursor.captures(query, layer.node(), TextProvider(text));
850 let grammar_index = result
851 .grammars
852 .iter()
853 .position(|g| g.id == grammar.id())
854 .unwrap_or_else(|| {
855 result.grammars.push(grammar);
856 result.grammars.len() - 1
857 });
858 let mut layer = SyntaxMapCapturesLayer {
859 depth: layer.depth,
860 grammar_index,
861 next_capture: None,
862 captures,
863 _query_cursor: query_cursor,
864 };
865
866 layer.advance();
867 if layer.next_capture.is_some() {
868 let key = layer.sort_key();
869 let ix = match result.layers[..result.active_layer_count]
870 .binary_search_by_key(&key, |layer| layer.sort_key())
871 {
872 Ok(ix) | Err(ix) => ix,
873 };
874 result.layers.insert(ix, layer);
875 result.active_layer_count += 1;
876 } else {
877 result.layers.push(layer);
878 }
879 }
880
881 result
882 }
883
884 pub fn grammars(&self) -> &[&'a Grammar] {
885 &self.grammars
886 }
887
888 pub fn peek(&self) -> Option<SyntaxMapCapture<'a>> {
889 let layer = self.layers[..self.active_layer_count].first()?;
890 let capture = layer.next_capture?;
891 Some(SyntaxMapCapture {
892 depth: layer.depth,
893 grammar_index: layer.grammar_index,
894 index: capture.index,
895 node: capture.node,
896 })
897 }
898
899 pub fn advance(&mut self) -> bool {
900 let layer = if let Some(layer) = self.layers[..self.active_layer_count].first_mut() {
901 layer
902 } else {
903 return false;
904 };
905
906 layer.advance();
907 if layer.next_capture.is_some() {
908 let key = layer.sort_key();
909 let i = 1 + self.layers[1..self.active_layer_count]
910 .iter()
911 .position(|later_layer| key < later_layer.sort_key())
912 .unwrap_or(self.active_layer_count - 1);
913 self.layers[0..i].rotate_left(1);
914 } else {
915 self.layers[0..self.active_layer_count].rotate_left(1);
916 self.active_layer_count -= 1;
917 }
918
919 true
920 }
921
922 pub fn set_byte_range(&mut self, range: Range<usize>) {
923 for layer in &mut self.layers {
924 layer.captures.set_byte_range(range.clone());
925 if let Some(capture) = &layer.next_capture {
926 if capture.node.end_byte() > range.start {
927 continue;
928 }
929 }
930 layer.advance();
931 }
932 self.layers.sort_unstable_by_key(|layer| layer.sort_key());
933 self.active_layer_count = self
934 .layers
935 .iter()
936 .position(|layer| layer.next_capture.is_none())
937 .unwrap_or(self.layers.len());
938 }
939}
940
941impl<'a> SyntaxMapMatches<'a> {
942 fn new(
943 range: Range<usize>,
944 text: &'a Rope,
945 layers: impl Iterator<Item = SyntaxLayerInfo<'a>>,
946 query: fn(&Grammar) -> Option<&Query>,
947 ) -> Self {
948 let mut result = Self::default();
949 for layer in layers {
950 let grammar = match &layer.language.grammar {
951 Some(grammar) => grammar,
952 None => continue,
953 };
954 let query = match query(&grammar) {
955 Some(query) => query,
956 None => continue,
957 };
958
959 let mut query_cursor = QueryCursorHandle::new();
960
961 // TODO - add a Tree-sitter API to remove the need for this.
962 let cursor = unsafe {
963 std::mem::transmute::<_, &'static mut QueryCursor>(query_cursor.deref_mut())
964 };
965
966 cursor.set_byte_range(range.clone());
967 let matches = cursor.matches(query, layer.node(), TextProvider(text));
968 let grammar_index = result
969 .grammars
970 .iter()
971 .position(|g| g.id == grammar.id())
972 .unwrap_or_else(|| {
973 result.grammars.push(grammar);
974 result.grammars.len() - 1
975 });
976 let mut layer = SyntaxMapMatchesLayer {
977 depth: layer.depth,
978 grammar_index,
979 matches,
980 next_pattern_index: 0,
981 next_captures: Vec::new(),
982 has_next: false,
983 _query_cursor: query_cursor,
984 };
985
986 layer.advance();
987 if layer.has_next {
988 let key = layer.sort_key();
989 let ix = match result.layers[..result.active_layer_count]
990 .binary_search_by_key(&key, |layer| layer.sort_key())
991 {
992 Ok(ix) | Err(ix) => ix,
993 };
994 result.layers.insert(ix, layer);
995 result.active_layer_count += 1;
996 } else {
997 result.layers.push(layer);
998 }
999 }
1000 result
1001 }
1002
1003 pub fn grammars(&self) -> &[&'a Grammar] {
1004 &self.grammars
1005 }
1006
1007 pub fn peek(&self) -> Option<SyntaxMapMatch> {
1008 let layer = self.layers.first()?;
1009 if !layer.has_next {
1010 return None;
1011 }
1012 Some(SyntaxMapMatch {
1013 depth: layer.depth,
1014 grammar_index: layer.grammar_index,
1015 pattern_index: layer.next_pattern_index,
1016 captures: &layer.next_captures,
1017 })
1018 }
1019
1020 pub fn advance(&mut self) -> bool {
1021 let layer = if let Some(layer) = self.layers.first_mut() {
1022 layer
1023 } else {
1024 return false;
1025 };
1026
1027 layer.advance();
1028 if layer.has_next {
1029 let key = layer.sort_key();
1030 let i = 1 + self.layers[1..self.active_layer_count]
1031 .iter()
1032 .position(|later_layer| key < later_layer.sort_key())
1033 .unwrap_or(self.active_layer_count - 1);
1034 self.layers[0..i].rotate_left(1);
1035 } else {
1036 self.layers[0..self.active_layer_count].rotate_left(1);
1037 self.active_layer_count -= 1;
1038 }
1039
1040 true
1041 }
1042}
1043
1044impl<'a> SyntaxMapCapturesLayer<'a> {
1045 fn advance(&mut self) {
1046 self.next_capture = self.captures.next().map(|(mat, ix)| mat.captures[ix]);
1047 }
1048
1049 fn sort_key(&self) -> (usize, Reverse<usize>, usize) {
1050 if let Some(capture) = &self.next_capture {
1051 let range = capture.node.byte_range();
1052 (range.start, Reverse(range.end), self.depth)
1053 } else {
1054 (usize::MAX, Reverse(0), usize::MAX)
1055 }
1056 }
1057}
1058
1059impl<'a> SyntaxMapMatchesLayer<'a> {
1060 fn advance(&mut self) {
1061 if let Some(mat) = self.matches.next() {
1062 self.next_captures.clear();
1063 self.next_captures.extend_from_slice(&mat.captures);
1064 self.next_pattern_index = mat.pattern_index;
1065 self.has_next = true;
1066 } else {
1067 self.has_next = false;
1068 }
1069 }
1070
1071 fn sort_key(&self) -> (usize, Reverse<usize>, usize) {
1072 if self.has_next {
1073 let captures = &self.next_captures;
1074 if let Some((first, last)) = captures.first().zip(captures.last()) {
1075 return (
1076 first.node.start_byte(),
1077 Reverse(last.node.end_byte()),
1078 self.depth,
1079 );
1080 }
1081 }
1082 (usize::MAX, Reverse(0), usize::MAX)
1083 }
1084}
1085
1086impl<'a> Iterator for SyntaxMapCaptures<'a> {
1087 type Item = SyntaxMapCapture<'a>;
1088
1089 fn next(&mut self) -> Option<Self::Item> {
1090 let result = self.peek();
1091 self.advance();
1092 result
1093 }
1094}
1095
1096fn join_ranges(
1097 a: impl Iterator<Item = Range<usize>>,
1098 b: impl Iterator<Item = Range<usize>>,
1099) -> Vec<Range<usize>> {
1100 let mut result = Vec::<Range<usize>>::new();
1101 let mut a = a.peekable();
1102 let mut b = b.peekable();
1103 loop {
1104 let range = match (a.peek(), b.peek()) {
1105 (Some(range_a), Some(range_b)) => {
1106 if range_a.start < range_b.start {
1107 a.next().unwrap()
1108 } else {
1109 b.next().unwrap()
1110 }
1111 }
1112 (None, Some(_)) => b.next().unwrap(),
1113 (Some(_), None) => a.next().unwrap(),
1114 (None, None) => break,
1115 };
1116
1117 if let Some(last) = result.last_mut() {
1118 if range.start <= last.end {
1119 last.end = last.end.max(range.end);
1120 continue;
1121 }
1122 }
1123 result.push(range);
1124 }
1125 result
1126}
1127
1128fn parse_text(
1129 grammar: &Grammar,
1130 text: &Rope,
1131 start_byte: usize,
1132 ranges: Vec<tree_sitter::Range>,
1133 old_tree: Option<Tree>,
1134) -> Tree {
1135 PARSER.with(|parser| {
1136 let mut parser = parser.borrow_mut();
1137 let mut chunks = text.chunks_in_range(start_byte..text.len());
1138 parser
1139 .set_included_ranges(&ranges)
1140 .expect("overlapping ranges");
1141 parser
1142 .set_language(grammar.ts_language)
1143 .expect("incompatible grammar");
1144 parser
1145 .parse_with(
1146 &mut move |offset, _| {
1147 chunks.seek(start_byte + offset);
1148 chunks.next().unwrap_or("").as_bytes()
1149 },
1150 old_tree.as_ref(),
1151 )
1152 .expect("invalid language")
1153 })
1154}
1155
1156fn get_injections(
1157 config: &InjectionConfig,
1158 text: &BufferSnapshot,
1159 outer_range: Range<Anchor>,
1160 node: Node,
1161 language_registry: &Arc<LanguageRegistry>,
1162 depth: usize,
1163 changed_ranges: &[Range<usize>],
1164 combined_injection_ranges: &mut HashMap<Arc<Language>, Vec<tree_sitter::Range>>,
1165 queue: &mut BinaryHeap<ParseStep>,
1166) {
1167 let mut query_cursor = QueryCursorHandle::new();
1168 let mut prev_match = None;
1169
1170 // Ensure that a `ParseStep` is created for every combined injection language, even
1171 // if there currently no matches for that injection.
1172 combined_injection_ranges.clear();
1173 for pattern in &config.patterns {
1174 if let (Some(language_name), true) = (pattern.language.as_ref(), pattern.combined) {
1175 if let Some(language) = language_registry
1176 .language_for_name_or_extension(language_name)
1177 .now_or_never()
1178 .and_then(|language| language.ok())
1179 {
1180 combined_injection_ranges.insert(language, Vec::new());
1181 }
1182 }
1183 }
1184
1185 for query_range in changed_ranges {
1186 query_cursor.set_byte_range(query_range.start.saturating_sub(1)..query_range.end + 1);
1187 for mat in query_cursor.matches(&config.query, node, TextProvider(text.as_rope())) {
1188 let content_ranges = mat
1189 .nodes_for_capture_index(config.content_capture_ix)
1190 .map(|node| node.range())
1191 .collect::<Vec<_>>();
1192 if content_ranges.is_empty() {
1193 continue;
1194 }
1195
1196 let content_range =
1197 content_ranges.first().unwrap().start_byte..content_ranges.last().unwrap().end_byte;
1198
1199 // Avoid duplicate matches if two changed ranges intersect the same injection.
1200 if let Some((prev_pattern_ix, prev_range)) = &prev_match {
1201 if mat.pattern_index == *prev_pattern_ix && content_range == *prev_range {
1202 continue;
1203 }
1204 }
1205
1206 prev_match = Some((mat.pattern_index, content_range.clone()));
1207 let combined = config.patterns[mat.pattern_index].combined;
1208
1209 let mut language_name = None;
1210 let mut step_range = content_range.clone();
1211 if let Some(name) = config.patterns[mat.pattern_index].language.as_ref() {
1212 language_name = Some(Cow::Borrowed(name.as_ref()))
1213 } else if let Some(language_node) = config
1214 .language_capture_ix
1215 .and_then(|ix| mat.nodes_for_capture_index(ix).next())
1216 {
1217 step_range.start = cmp::min(content_range.start, language_node.start_byte());
1218 step_range.end = cmp::max(content_range.end, language_node.end_byte());
1219 language_name = Some(Cow::Owned(
1220 text.text_for_range(language_node.byte_range()).collect(),
1221 ))
1222 };
1223
1224 if let Some(language_name) = language_name {
1225 let language = language_registry
1226 .language_for_name_or_extension(&language_name)
1227 .now_or_never()
1228 .and_then(|language| language.ok());
1229 let range = text.anchor_before(step_range.start)..text.anchor_after(step_range.end);
1230 if let Some(language) = language {
1231 if combined {
1232 combined_injection_ranges
1233 .entry(language.clone())
1234 .or_default()
1235 .extend(content_ranges);
1236 } else {
1237 queue.push(ParseStep {
1238 depth,
1239 language: ParseStepLanguage::Loaded { language },
1240 included_ranges: content_ranges,
1241 range,
1242 mode: ParseMode::Single,
1243 });
1244 }
1245 } else {
1246 queue.push(ParseStep {
1247 depth,
1248 language: ParseStepLanguage::Pending {
1249 name: language_name.into(),
1250 },
1251 included_ranges: content_ranges,
1252 range,
1253 mode: ParseMode::Single,
1254 });
1255 }
1256 }
1257 }
1258 }
1259
1260 for (language, mut included_ranges) in combined_injection_ranges.drain() {
1261 included_ranges.sort_unstable();
1262 queue.push(ParseStep {
1263 depth,
1264 language: ParseStepLanguage::Loaded { language },
1265 range: outer_range.clone(),
1266 included_ranges,
1267 mode: ParseMode::Combined {
1268 parent_layer_range: node.start_byte()..node.end_byte(),
1269 parent_layer_changed_ranges: changed_ranges.to_vec(),
1270 },
1271 })
1272 }
1273}
1274
1275pub(crate) fn splice_included_ranges(
1276 mut ranges: Vec<tree_sitter::Range>,
1277 removed_ranges: &[Range<usize>],
1278 new_ranges: &[tree_sitter::Range],
1279) -> Vec<tree_sitter::Range> {
1280 let mut removed_ranges = removed_ranges.iter().cloned().peekable();
1281 let mut new_ranges = new_ranges.into_iter().cloned().peekable();
1282 let mut ranges_ix = 0;
1283 loop {
1284 let next_new_range = new_ranges.peek();
1285 let next_removed_range = removed_ranges.peek();
1286
1287 let (remove, insert) = match (next_removed_range, next_new_range) {
1288 (None, None) => break,
1289 (Some(_), None) => (removed_ranges.next().unwrap(), None),
1290 (Some(next_removed_range), Some(next_new_range)) => {
1291 if next_removed_range.end < next_new_range.start_byte {
1292 (removed_ranges.next().unwrap(), None)
1293 } else {
1294 let mut start = next_new_range.start_byte;
1295 let mut end = next_new_range.end_byte;
1296
1297 while let Some(next_removed_range) = removed_ranges.peek() {
1298 if next_removed_range.start > next_new_range.end_byte {
1299 break;
1300 }
1301 let next_removed_range = removed_ranges.next().unwrap();
1302 start = cmp::min(start, next_removed_range.start);
1303 end = cmp::max(end, next_removed_range.end);
1304 }
1305
1306 (start..end, Some(new_ranges.next().unwrap()))
1307 }
1308 }
1309 (None, Some(next_new_range)) => (
1310 next_new_range.start_byte..next_new_range.end_byte,
1311 Some(new_ranges.next().unwrap()),
1312 ),
1313 };
1314
1315 let mut start_ix = ranges_ix
1316 + match ranges[ranges_ix..].binary_search_by_key(&remove.start, |r| r.end_byte) {
1317 Ok(ix) => ix,
1318 Err(ix) => ix,
1319 };
1320 let mut end_ix = ranges_ix
1321 + match ranges[ranges_ix..].binary_search_by_key(&remove.end, |r| r.start_byte) {
1322 Ok(ix) => ix + 1,
1323 Err(ix) => ix,
1324 };
1325
1326 // If there are empty ranges, then there may be multiple ranges with the same
1327 // start or end. Expand the splice to include any adjacent ranges that touch
1328 // the changed range.
1329 while start_ix > 0 {
1330 if ranges[start_ix - 1].end_byte == remove.start {
1331 start_ix -= 1;
1332 } else {
1333 break;
1334 }
1335 }
1336 while let Some(range) = ranges.get(end_ix) {
1337 if range.start_byte == remove.end {
1338 end_ix += 1;
1339 } else {
1340 break;
1341 }
1342 }
1343
1344 ranges.splice(start_ix..end_ix, insert);
1345 ranges_ix = start_ix;
1346 }
1347
1348 ranges
1349}
1350
1351impl OwnedSyntaxLayerInfo {
1352 pub fn node(&self) -> Node {
1353 self.tree
1354 .root_node_with_offset(self.offset.0, self.offset.1)
1355 }
1356}
1357
1358impl<'a> SyntaxLayerInfo<'a> {
1359 pub fn to_owned(&self) -> OwnedSyntaxLayerInfo {
1360 OwnedSyntaxLayerInfo {
1361 tree: self.tree.clone(),
1362 offset: self.offset,
1363 depth: self.depth,
1364 language: self.language.clone(),
1365 }
1366 }
1367
1368 pub fn node(&self) -> Node<'a> {
1369 self.tree
1370 .root_node_with_offset(self.offset.0, self.offset.1)
1371 }
1372
1373 pub(crate) fn override_id(&self, offset: usize, text: &text::BufferSnapshot) -> Option<u32> {
1374 let text = TextProvider(text.as_rope());
1375 let config = self.language.grammar.as_ref()?.override_config.as_ref()?;
1376
1377 let mut query_cursor = QueryCursorHandle::new();
1378 query_cursor.set_byte_range(offset..offset);
1379
1380 let mut smallest_match: Option<(u32, Range<usize>)> = None;
1381 for mat in query_cursor.matches(&config.query, self.node(), text) {
1382 for capture in mat.captures {
1383 if !config.values.contains_key(&capture.index) {
1384 continue;
1385 }
1386
1387 let range = capture.node.byte_range();
1388 if offset <= range.start || offset >= range.end {
1389 continue;
1390 }
1391
1392 if let Some((_, smallest_range)) = &smallest_match {
1393 if range.len() < smallest_range.len() {
1394 smallest_match = Some((capture.index, range))
1395 }
1396 continue;
1397 }
1398
1399 smallest_match = Some((capture.index, range));
1400 }
1401 }
1402
1403 smallest_match.map(|(index, _)| index)
1404 }
1405}
1406
1407impl std::ops::Deref for SyntaxMap {
1408 type Target = SyntaxSnapshot;
1409
1410 fn deref(&self) -> &Self::Target {
1411 &self.snapshot
1412 }
1413}
1414
1415impl PartialEq for ParseStep {
1416 fn eq(&self, _: &Self) -> bool {
1417 false
1418 }
1419}
1420
1421impl Eq for ParseStep {}
1422
1423impl PartialOrd for ParseStep {
1424 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1425 Some(self.cmp(&other))
1426 }
1427}
1428
1429impl Ord for ParseStep {
1430 fn cmp(&self, other: &Self) -> Ordering {
1431 let range_a = self.range();
1432 let range_b = other.range();
1433 Ord::cmp(&other.depth, &self.depth)
1434 .then_with(|| Ord::cmp(&range_b.start, &range_a.start))
1435 .then_with(|| Ord::cmp(&range_a.end, &range_b.end))
1436 .then_with(|| self.language.id().cmp(&other.language.id()))
1437 }
1438}
1439
1440impl ParseStep {
1441 fn range(&self) -> Range<usize> {
1442 if let ParseMode::Combined {
1443 parent_layer_range, ..
1444 } = &self.mode
1445 {
1446 parent_layer_range.clone()
1447 } else {
1448 let start = self.included_ranges.first().map_or(0, |r| r.start_byte);
1449 let end = self.included_ranges.last().map_or(0, |r| r.end_byte);
1450 start..end
1451 }
1452 }
1453}
1454
1455impl ChangedRegion {
1456 fn cmp(&self, other: &Self, buffer: &BufferSnapshot) -> Ordering {
1457 let range_a = &self.range;
1458 let range_b = &other.range;
1459 Ord::cmp(&self.depth, &other.depth)
1460 .then_with(|| range_a.start.cmp(&range_b.start, buffer))
1461 .then_with(|| range_b.end.cmp(&range_a.end, buffer))
1462 }
1463}
1464
1465impl ChangeRegionSet {
1466 fn start_position(&self) -> ChangeStartPosition {
1467 self.0.first().map_or(
1468 ChangeStartPosition {
1469 depth: usize::MAX,
1470 position: Anchor::MAX,
1471 },
1472 |region| ChangeStartPosition {
1473 depth: region.depth,
1474 position: region.range.start,
1475 },
1476 )
1477 }
1478
1479 fn intersects(&self, layer: &SyntaxLayer, text: &BufferSnapshot) -> bool {
1480 for region in &self.0 {
1481 if region.depth < layer.depth {
1482 continue;
1483 }
1484 if region.depth > layer.depth {
1485 break;
1486 }
1487 if region.range.end.cmp(&layer.range.start, text).is_le() {
1488 continue;
1489 }
1490 if region.range.start.cmp(&layer.range.end, text).is_ge() {
1491 break;
1492 }
1493 return true;
1494 }
1495 false
1496 }
1497
1498 fn insert(&mut self, region: ChangedRegion, text: &BufferSnapshot) {
1499 if let Err(ix) = self.0.binary_search_by(|probe| probe.cmp(®ion, text)) {
1500 self.0.insert(ix, region);
1501 }
1502 }
1503
1504 fn prune(&mut self, summary: SyntaxLayerSummary, text: &BufferSnapshot) -> bool {
1505 let prev_len = self.0.len();
1506 self.0.retain(|region| {
1507 region.depth > summary.max_depth
1508 || (region.depth == summary.max_depth
1509 && region
1510 .range
1511 .end
1512 .cmp(&summary.last_layer_range.start, text)
1513 .is_gt())
1514 });
1515 self.0.len() < prev_len
1516 }
1517}
1518
1519impl Default for SyntaxLayerSummary {
1520 fn default() -> Self {
1521 Self {
1522 max_depth: 0,
1523 min_depth: 0,
1524 range: Anchor::MAX..Anchor::MIN,
1525 last_layer_range: Anchor::MIN..Anchor::MAX,
1526 last_layer_language: None,
1527 contains_unknown_injections: false,
1528 }
1529 }
1530}
1531
1532impl sum_tree::Summary for SyntaxLayerSummary {
1533 type Context = BufferSnapshot;
1534
1535 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
1536 if other.max_depth > self.max_depth {
1537 self.max_depth = other.max_depth;
1538 self.range = other.range.clone();
1539 } else {
1540 if self.range == (Anchor::MAX..Anchor::MAX) {
1541 self.range.start = other.range.start;
1542 }
1543 if other.range.end.cmp(&self.range.end, buffer).is_gt() {
1544 self.range.end = other.range.end;
1545 }
1546 }
1547 self.last_layer_range = other.last_layer_range.clone();
1548 self.last_layer_language = other.last_layer_language;
1549 self.contains_unknown_injections |= other.contains_unknown_injections;
1550 }
1551}
1552
1553impl<'a> SeekTarget<'a, SyntaxLayerSummary, SyntaxLayerSummary> for SyntaxLayerPosition {
1554 fn cmp(&self, cursor_location: &SyntaxLayerSummary, buffer: &BufferSnapshot) -> Ordering {
1555 Ord::cmp(&self.depth, &cursor_location.max_depth)
1556 .then_with(|| {
1557 self.range
1558 .start
1559 .cmp(&cursor_location.last_layer_range.start, buffer)
1560 })
1561 .then_with(|| {
1562 cursor_location
1563 .last_layer_range
1564 .end
1565 .cmp(&self.range.end, buffer)
1566 })
1567 .then_with(|| self.language.cmp(&cursor_location.last_layer_language))
1568 }
1569}
1570
1571impl<'a> SeekTarget<'a, SyntaxLayerSummary, SyntaxLayerSummary> for ChangeStartPosition {
1572 fn cmp(&self, cursor_location: &SyntaxLayerSummary, text: &BufferSnapshot) -> Ordering {
1573 Ord::cmp(&self.depth, &cursor_location.max_depth)
1574 .then_with(|| self.position.cmp(&cursor_location.range.end, text))
1575 }
1576}
1577
1578impl<'a> SeekTarget<'a, SyntaxLayerSummary, SyntaxLayerSummary>
1579 for SyntaxLayerPositionBeforeChange
1580{
1581 fn cmp(&self, cursor_location: &SyntaxLayerSummary, buffer: &BufferSnapshot) -> Ordering {
1582 if self.change.cmp(cursor_location, buffer).is_le() {
1583 return Ordering::Less;
1584 } else {
1585 self.position.cmp(cursor_location, buffer)
1586 }
1587 }
1588}
1589
1590impl sum_tree::Item for SyntaxLayer {
1591 type Summary = SyntaxLayerSummary;
1592
1593 fn summary(&self) -> Self::Summary {
1594 SyntaxLayerSummary {
1595 min_depth: self.depth,
1596 max_depth: self.depth,
1597 range: self.range.clone(),
1598 last_layer_range: self.range.clone(),
1599 last_layer_language: self.content.language_id(),
1600 contains_unknown_injections: matches!(self.content, SyntaxLayerContent::Pending { .. }),
1601 }
1602 }
1603}
1604
1605impl std::fmt::Debug for SyntaxLayer {
1606 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1607 f.debug_struct("SyntaxLayer")
1608 .field("depth", &self.depth)
1609 .field("range", &self.range)
1610 .field("tree", &self.content.tree())
1611 .finish()
1612 }
1613}
1614
1615impl<'a> tree_sitter::TextProvider<'a> for TextProvider<'a> {
1616 type I = ByteChunks<'a>;
1617
1618 fn text(&mut self, node: tree_sitter::Node) -> Self::I {
1619 ByteChunks(self.0.chunks_in_range(node.byte_range()))
1620 }
1621}
1622
1623impl<'a> Iterator for ByteChunks<'a> {
1624 type Item = &'a [u8];
1625
1626 fn next(&mut self) -> Option<Self::Item> {
1627 self.0.next().map(str::as_bytes)
1628 }
1629}
1630
1631impl QueryCursorHandle {
1632 pub(crate) fn new() -> Self {
1633 let mut cursor = QUERY_CURSORS.lock().pop().unwrap_or_else(QueryCursor::new);
1634 cursor.set_match_limit(64);
1635 QueryCursorHandle(Some(cursor))
1636 }
1637}
1638
1639impl Deref for QueryCursorHandle {
1640 type Target = QueryCursor;
1641
1642 fn deref(&self) -> &Self::Target {
1643 self.0.as_ref().unwrap()
1644 }
1645}
1646
1647impl DerefMut for QueryCursorHandle {
1648 fn deref_mut(&mut self) -> &mut Self::Target {
1649 self.0.as_mut().unwrap()
1650 }
1651}
1652
1653impl Drop for QueryCursorHandle {
1654 fn drop(&mut self) {
1655 let mut cursor = self.0.take().unwrap();
1656 cursor.set_byte_range(0..usize::MAX);
1657 cursor.set_point_range(Point::zero().to_ts_point()..Point::MAX.to_ts_point());
1658 QUERY_CURSORS.lock().push(cursor)
1659 }
1660}
1661
1662pub(crate) trait ToTreeSitterPoint {
1663 fn to_ts_point(self) -> tree_sitter::Point;
1664 fn from_ts_point(point: tree_sitter::Point) -> Self;
1665}
1666
1667impl ToTreeSitterPoint for Point {
1668 fn to_ts_point(self) -> tree_sitter::Point {
1669 tree_sitter::Point::new(self.row as usize, self.column as usize)
1670 }
1671
1672 fn from_ts_point(point: tree_sitter::Point) -> Self {
1673 Point::new(point.row as u32, point.column as u32)
1674 }
1675}
1676
1677struct LogIncludedRanges<'a>(&'a [tree_sitter::Range]);
1678struct LogPoint(Point);
1679struct LogAnchorRange<'a>(&'a Range<Anchor>, &'a text::BufferSnapshot);
1680struct LogChangedRegions<'a>(&'a ChangeRegionSet, &'a text::BufferSnapshot);
1681
1682impl<'a> fmt::Debug for LogIncludedRanges<'a> {
1683 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1684 f.debug_list()
1685 .entries(self.0.iter().map(|range| {
1686 let start = range.start_point;
1687 let end = range.end_point;
1688 (start.row, start.column)..(end.row, end.column)
1689 }))
1690 .finish()
1691 }
1692}
1693
1694impl<'a> fmt::Debug for LogAnchorRange<'a> {
1695 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1696 let range = self.0.to_point(self.1);
1697 (LogPoint(range.start)..LogPoint(range.end)).fmt(f)
1698 }
1699}
1700
1701impl<'a> fmt::Debug for LogChangedRegions<'a> {
1702 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1703 f.debug_list()
1704 .entries(
1705 self.0
1706 .0
1707 .iter()
1708 .map(|region| LogAnchorRange(®ion.range, self.1)),
1709 )
1710 .finish()
1711 }
1712}
1713
1714impl fmt::Debug for LogPoint {
1715 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1716 (self.0.row, self.0.column).fmt(f)
1717 }
1718}