1use std::cmp::Ordering;
2use std::collections::Bound;
3use std::error::Error;
4use std::fmt::{Debug, Display, Formatter};
5use std::hash::{Hash, Hasher};
6use std::iter::{once, FusedIterator, Once};
7use std::ops::{BitAnd, BitOr, BitOrAssign, RangeBounds};
8#[cfg(unix)]
9use std::os::unix::io::AsRawFd;
10#[cfg(windows)]
11use std::os::windows::io::AsRawHandle;
12use std::path::{Path, PathBuf};
13use std::ptr::NonNull;
14use std::str::Utf8Error;
15use streaming_iterator::StreamingIterator;
16use utf8_error_offset_by::Utf8ErrorOffsetBy;
17
18mod utf8_error_offset_by;
19
20#[derive(Clone, Debug)]
24pub struct Tree {
25 tree: tree_sitter::Tree,
26 byte_text: Vec<u8>,
27 path: Option<PathBuf>,
28}
29
30#[derive(Clone, Copy)]
35pub struct Node<'tree> {
36 node: tree_sitter::Node<'tree>,
37 tree: &'tree Tree,
38}
39
40#[derive(Clone, Copy)]
42pub struct NodePtr {
43 node_data: TsNodePtr,
44 tree: NonNull<Tree>,
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
49#[repr(C)]
50struct TsNodePtr {
51 context: [u32; 4usize],
52 id: *const (),
53 tree: *const (),
54}
55
56#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
58#[repr(transparent)]
59pub struct NodeId(usize);
60
61#[derive(Clone)]
66pub struct TreeCursor<'tree> {
67 cursor: tree_sitter::TreeCursor<'tree>,
68 tree: &'tree Tree,
69 child_depth: usize,
70}
71
72pub struct QueryCursor {
74 query_cursor: tree_sitter::QueryCursor,
75}
76
77pub struct QueryMatches<'query, 'tree> {
83 query_matches: tree_sitter::QueryMatches<'query, 'tree, &'tree Tree, &'tree str>,
84 current_match: Option<QueryMatch<'query, 'tree>>,
85 query: &'query Query,
86 tree: &'tree Tree,
87}
88
89pub struct QueryMatch<'query, 'tree> {
91 query_match: *const tree_sitter::QueryMatch<'query, 'tree>,
92 query: &'query Query,
93 tree: &'tree Tree,
94}
95
96pub struct QueryCaptures<'query, 'tree> {
98 query_captures: tree_sitter::QueryCaptures<'query, 'tree, &'tree Tree, &'tree str>,
99 query: &'query Query,
100 tree: &'tree Tree,
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
105pub struct QueryCapture<'query, 'tree> {
106 pub node: Node<'tree>,
107 pub index: usize,
108 pub name: &'query str,
109}
110
111#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
113pub struct PointRange {
114 pub start: Point,
115 pub end: Point,
116}
117
118#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
120pub struct Point {
121 pub row: usize,
122 pub column: usize,
123}
124
125#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
128pub struct Range {
129 pub start_byte: usize,
130 pub end_byte: usize,
131 pub start_point: Point,
132 pub end_point: Point,
133}
134
135#[repr(transparent)]
137pub struct Parser(tree_sitter::Parser);
138
139pub type Language = tree_sitter::Language;
143pub type LanguageRef<'a> = tree_sitter::LanguageRef<'a>;
145pub type LanguageError = tree_sitter::LanguageError;
147pub type Query = tree_sitter::Query;
149pub type QueryProperty = tree_sitter::QueryProperty;
151pub type IncludedRangesError = tree_sitter::IncludedRangesError;
153pub type InputEdit = tree_sitter::InputEdit;
155
156#[derive(Debug)]
158pub enum TreeParseError {
159 IO(std::io::Error),
160 ParsingFailed,
161 NotUtf8(Utf8Error),
162}
163
164#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
167pub enum TraversalState {
168 Start,
169 Down,
170 Right,
171 Up,
172 End,
173}
174
175#[derive(Clone)]
177pub struct PreorderTraversal<'tree> {
178 cursor: TreeCursor<'tree>,
179 last_state: TraversalState,
180}
181
182#[derive(Clone, Copy, Debug)]
184pub struct TraversalItem<'tree> {
185 pub node: Node<'tree>,
187 pub field_name: Option<&'static str>,
189 pub last_state: TraversalState,
191}
192
193impl Parser {
194 #[inline]
196 pub fn new(language: &Language) -> Result<Self, LanguageError> {
197 let mut parser = tree_sitter::Parser::new();
198 parser.set_language(language)?;
199 Ok(Self(parser))
200 }
201
202 #[inline]
204 pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
205 self.0.set_language(language)
206 }
207
208 #[inline]
211 pub fn set_included_ranges(&mut self, ranges: &[Range]) -> Result<(), IncludedRangesError> {
212 let ranges = ranges
213 .iter()
214 .copied()
215 .map(tree_sitter::Range::from)
216 .collect::<Vec<_>>();
217 self.0.set_included_ranges(&ranges)
218 }
219
220 #[inline]
225 pub fn parse_file(
226 &mut self,
227 path: &Path,
228 old_tree: Option<&Tree>,
229 ) -> Result<Tree, TreeParseError> {
230 let byte_text = std::fs::read(path)?;
231 self.parse_bytes(byte_text, Some(path), old_tree)
232 }
233
234 #[inline]
239 pub fn parse_string(
240 &mut self,
241 text: String,
242 path: Option<&Path>,
243 old_tree: Option<&Tree>,
244 ) -> Result<Tree, TreeParseError> {
245 self.parse_bytes(text.into_bytes(), path, old_tree)
246 }
247
248 #[inline]
256 pub fn parse_bytes(
257 &mut self,
258 byte_text: Vec<u8>,
259 path: Option<&Path>,
260 old_tree: Option<&Tree>,
261 ) -> Result<Tree, TreeParseError> {
262 let tree = self
263 .0
264 .parse(&byte_text, old_tree.map(|t| &t.tree))
265 .ok_or(TreeParseError::ParsingFailed)?;
266 Ok(Tree::new(tree, byte_text, path.map(|p| p.to_path_buf()))?)
267 }
268}
269
270impl Tree {
271 #[inline]
276 fn new(
277 tree: tree_sitter::Tree,
278 byte_text: Vec<u8>,
279 path: Option<PathBuf>,
280 ) -> Result<Self, Utf8Error> {
281 Self::validate_utf8(&tree, &byte_text)?;
282 Ok(Self {
283 tree,
284 byte_text,
285 path,
286 })
287 }
288
289 fn validate_utf8(tree: &tree_sitter::Tree, byte_text: &[u8]) -> Result<(), Utf8Error> {
290 let _ = std::str::from_utf8(byte_text)?;
291 let mut cursor = tree.walk();
292 let mut went_up = false;
293 while (!went_up && cursor.goto_first_child()) || cursor.goto_next_sibling() || {
294 went_up = true;
295 cursor.goto_parent()
296 } {
297 let node = cursor.node();
298 let range = node.byte_range();
299 let _ = std::str::from_utf8(&byte_text[range])
300 .map_err(|e| e.offset_by(node.start_byte()))?;
301 }
302 Ok(())
303 }
304
305 #[inline]
307 pub fn text(&self) -> &str {
308 unsafe { std::str::from_utf8_unchecked(&self.byte_text) }
310 }
311
312 #[inline]
316 pub fn path(&self) -> Option<&Path> {
317 self.path.as_deref()
318 }
319
320 #[inline]
322 pub fn root_node(&self) -> Node<'_> {
323 unsafe { Node::new(self.tree.root_node(), self) }
325 }
326
327 #[inline]
329 pub fn walk(&self) -> TreeCursor<'_> {
330 TreeCursor::new(self.tree.walk(), self, true)
331 }
332
333 #[inline]
335 pub fn included_ranges(&self) -> Vec<Range> {
336 self.tree
337 .included_ranges()
338 .into_iter()
339 .map(Range::from)
340 .collect()
341 }
342
343 #[inline]
345 pub fn changed_ranges(&self, other: &Tree) -> impl ExactSizeIterator<Item = Range> {
346 self.tree.changed_ranges(&other.tree).map(Range::from)
347 }
348
349 #[inline]
351 pub fn language(&self) -> LanguageRef<'_> {
352 self.tree.language()
353 }
354
355 #[inline]
358 pub fn print_dot_graph(
359 &self,
360 #[cfg(unix)] file: &impl AsRawFd,
361 #[cfg(windows)] file: &impl AsRawHandle,
362 ) {
363 self.tree.print_dot_graph(file)
364 }
365
366 #[inline]
368 pub fn edit(&mut self, edit: &InputEdit) {
369 self.tree.edit(edit)
370 }
371}
372
373impl<'tree> tree_sitter::TextProvider<&'tree str> for &'tree Tree {
374 type I = Once<&'tree str>;
375
376 #[inline]
377 fn text(&mut self, node: tree_sitter::Node<'_>) -> Self::I {
378 once(unsafe { std::str::from_utf8_unchecked(&self.byte_text[node.byte_range()]) })
380 }
381}
382
383impl<'tree> Node<'tree> {
384 #[inline]
389 pub unsafe fn new(node: tree_sitter::Node<'tree>, tree: &'tree Tree) -> Self {
390 Self { node, tree }
391 }
392
393 #[inline]
402 pub fn id(&self) -> NodeId {
403 NodeId::of_ts(self.node)
404 }
405
406 #[inline]
408 pub fn kind(&self) -> &'static str {
409 self.node.kind()
410 }
411
412 #[inline]
414 pub fn is_named(&self) -> bool {
415 self.node.is_named()
416 }
417
418 #[inline]
420 pub fn is_missing(&self) -> bool {
421 self.node.is_missing()
422 }
423
424 #[inline]
426 pub fn is_extra(&self) -> bool {
427 self.node.is_extra()
428 }
429
430 #[inline]
432 pub fn is_error(&self) -> bool {
433 self.node.is_error()
434 }
435
436 #[inline]
438 pub fn has_error(&self) -> bool {
439 self.node.has_error()
440 }
441
442 #[inline]
444 pub fn has_changes(&self) -> bool {
445 self.node.has_changes()
446 }
447
448 #[inline]
450 pub fn edit(&mut self, edit: &InputEdit) {
451 self.node.edit(edit)
452 }
453
454 #[inline]
456 pub fn start_byte(&self) -> usize {
457 self.node.start_byte()
458 }
459
460 #[inline]
462 pub fn end_byte(&self) -> usize {
463 self.node.end_byte()
464 }
465
466 #[inline]
468 pub fn start_position(&self) -> Point {
469 Point::from(self.node.start_position())
470 }
471
472 #[inline]
474 pub fn end_position(&self) -> Point {
475 Point::from(self.node.end_position())
476 }
477
478 #[inline]
480 pub fn byte_range(&self) -> std::ops::Range<usize> {
481 self.node.byte_range()
482 }
483
484 #[inline]
486 pub fn position_range(&self) -> PointRange {
487 PointRange {
488 start: self.start_position(),
489 end: self.end_position(),
490 }
491 }
492
493 #[inline]
495 pub fn range(&self) -> Range {
496 Range::from(self.node.range())
497 }
498
499 #[inline]
503 fn byte_text(&self) -> &'tree [u8] {
504 &self.tree.byte_text[self.byte_range()]
505 }
506
507 #[inline]
511 pub fn text(&self) -> &'tree str {
512 unsafe { std::str::from_utf8_unchecked(self.byte_text()) }
514 }
515
516 #[inline]
520 pub fn path(&self) -> Option<&'tree Path> {
521 self.tree.path()
522 }
523
524 #[inline]
526 pub fn all_children<'a>(
527 &self,
528 cursor: &'a mut TreeCursor<'tree>,
529 ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'a {
530 let tree = self.tree;
531 self.node
533 .children(&mut cursor.cursor)
534 .map(move |node| unsafe { Node::new(node, tree) })
535 }
536
537 #[inline]
539 pub fn named_children<'a>(
540 &self,
541 cursor: &'a mut TreeCursor<'tree>,
542 ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'a {
543 let tree = self.tree;
544 self.node
546 .named_children(&mut cursor.cursor)
547 .map(move |node| unsafe { Node::new(node, tree) })
548 }
549
550 #[inline]
552 pub fn child_count(&self) -> usize {
553 self.node.child_count()
554 }
555
556 #[inline]
558 pub fn named_child_count(&self) -> usize {
559 self.node.named_child_count()
560 }
561
562 #[inline]
564 pub fn parent(&self) -> Option<Node<'tree>> {
565 self.node
567 .parent()
568 .map(|node| unsafe { Node::new(node, self.tree) })
569 }
570
571 #[inline]
573 pub fn next_sibling(&self) -> Option<Node<'tree>> {
574 self.node
576 .next_sibling()
577 .map(|node| unsafe { Node::new(node, self.tree) })
578 }
579
580 #[inline]
582 pub fn next_named_sibling(&self) -> Option<Node<'tree>> {
583 self.node
585 .next_named_sibling()
586 .map(|node| unsafe { Node::new(node, self.tree) })
587 }
588
589 #[inline]
591 pub fn prev_sibling(&self) -> Option<Node<'tree>> {
592 self.node
594 .prev_sibling()
595 .map(|node| unsafe { Node::new(node, self.tree) })
596 }
597
598 #[inline]
600 pub fn prev_named_sibling(&self) -> Option<Node<'tree>> {
601 self.node
603 .prev_named_sibling()
604 .map(|node| unsafe { Node::new(node, self.tree) })
605 }
606
607 #[inline]
609 pub fn child(&self, i: usize) -> Option<Node<'tree>> {
610 self.node
612 .child(u32::try_from(i).ok()?)
613 .map(|node| unsafe { Node::new(node, self.tree) })
614 }
615
616 #[inline]
618 pub fn named_child(&self, i: usize) -> Option<Node<'tree>> {
619 self.node
621 .named_child(i.try_into().ok()?)
622 .map(|node| unsafe { Node::new(node, self.tree) })
623 }
624
625 #[inline]
627 pub fn last_child(&self) -> Option<Node<'tree>> {
628 self.child(self.child_count().wrapping_sub(1))
630 }
631
632 #[inline]
634 pub fn last_named_child(&self) -> Option<Node<'tree>> {
635 self.named_child(self.named_child_count().wrapping_sub(1))
637 }
638
639 #[inline]
643 pub fn child_of_kind(&self, kind: &str, cursor: &mut TreeCursor<'tree>) -> Option<Node<'tree>> {
644 self.node
646 .named_children(&mut cursor.cursor)
647 .find(|node| node.kind() == kind)
648 .map(|node| unsafe { Node::new(node, self.tree) })
649 }
650
651 #[inline]
655 pub fn children_of_kind<'a>(
656 &self,
657 kind: &'a str,
658 cursor: &'a mut TreeCursor<'tree>,
659 ) -> impl Iterator<Item = Node<'tree>> + 'a {
660 self.node
662 .named_children(&mut cursor.cursor)
663 .filter(move |node| node.kind() == kind)
664 .map(|node| unsafe { Node::new(node, self.tree) })
665 }
666
667 #[inline]
669 pub fn child_by_field_name(&self, field_name: &str) -> Option<Node<'tree>> {
670 self.node
672 .child_by_field_name(field_name)
673 .map(|node| unsafe { Node::new(node, self.tree) })
674 }
675
676 #[inline]
678 pub fn children_by_field_name<'a>(
679 &self,
680 field_name: &str,
681 c: &'a mut TreeCursor<'tree>,
682 ) -> impl Iterator<Item = Node<'tree>> + 'a {
683 self.node
685 .children_by_field_name(field_name, &mut c.cursor)
686 .map(|node| unsafe { Node::new(node, self.tree) })
687 }
688
689 #[inline]
691 pub fn field_name_for_child(&self, i: usize) -> Option<&'static str> {
692 self.node.field_name_for_child(i as u32)
693 }
694
695 #[inline]
697 pub fn field_name_for_named_child(&self, i: usize) -> Option<&'static str> {
698 self.node.field_name_for_named_child(i as u32)
699 }
700
701 pub fn field_name(&self, cursor: &mut TreeCursor<'tree>) -> Option<&'static str> {
706 self.parent().and_then(|parent| {
707 let i = parent
708 .all_children(cursor)
709 .position(|x| x == *self)
710 .expect("node not one of its parent's children");
711 parent.field_name_for_child(i)
712 })
713 }
714
715 #[inline]
720 pub fn walk(&self) -> TreeCursor<'tree> {
721 TreeCursor::new(self.node.walk(), self.tree, false)
722 }
723
724 #[inline]
726 pub fn to_sexp(&self) -> String {
727 self.node.to_sexp()
728 }
729
730 #[inline]
732 pub fn to_ptr(&self) -> NodePtr {
733 NodePtr {
734 node_data: TsNodePtr::from(self.node),
735 tree: NonNull::from(self.tree),
736 }
737 }
738}
739
740impl<'tree> PartialEq for Node<'tree> {
741 #[inline]
742 fn eq(&self, other: &Node<'tree>) -> bool {
743 self.id() == other.id()
744 }
745}
746
747impl<'tree> Eq for Node<'tree> {}
748
749impl<'tree> PartialOrd for Node<'tree> {
750 #[inline]
752 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
753 Some(self.cmp(other))
754 }
755}
756
757impl<'tree> Ord for Node<'tree> {
758 fn cmp(&self, other: &Self) -> Ordering {
760 if self.id() == other.id() {
761 Ordering::Equal
762 } else if std::ptr::eq(self.tree as *const _, other.tree as *const _) {
763 self.start_byte()
764 .cmp(&other.start_byte())
765 .then(self.end_byte().cmp(&other.end_byte()))
766 } else {
767 self.path()
768 .cmp(&other.path())
769 .then(self.id().0.cmp(&other.id().0))
770 }
771 }
772}
773
774impl<'tree> Hash for Node<'tree> {
775 #[inline]
776 fn hash<H: Hasher>(&self, state: &mut H) {
777 self.id().hash(state)
778 }
779}
780
781impl NodePtr {
782 #[inline]
787 pub unsafe fn to_node<'a>(self) -> Node<'a> {
788 Node {
789 node: self.node_data.to_node(),
790 tree: self.tree.as_ref(),
791 }
792 }
793}
794
795impl PartialEq for NodePtr {
796 #[inline]
797 fn eq(&self, other: &NodePtr) -> bool {
798 self.node_data == other.node_data
799 }
800}
801
802impl Eq for NodePtr {}
803
804impl Hash for NodePtr {
805 #[inline]
806 fn hash<H: Hasher>(&self, state: &mut H) {
807 self.node_data.hash(state)
808 }
809}
810
811impl TsNodePtr {
812 #[inline]
817 pub unsafe fn to_node<'tree>(self) -> tree_sitter::Node<'tree> {
818 std::mem::transmute(self)
821 }
822}
823
824impl<'tree> From<tree_sitter::Node<'tree>> for TsNodePtr {
825 #[inline]
826 fn from(node: tree_sitter::Node) -> Self {
827 unsafe { std::mem::transmute::<tree_sitter::Node<'_>, TsNodePtr>(node) }
830 }
831}
832
833impl<'tree> TreeCursor<'tree> {
834 #[inline]
839 fn new(cursor: tree_sitter::TreeCursor<'tree>, tree: &'tree Tree, is_rooted: bool) -> Self {
840 Self {
841 cursor,
842 tree,
843 child_depth: match is_rooted {
844 false => 0,
845 true => 1,
846 },
847 }
848 }
849
850 #[inline]
852 pub fn node(&self) -> Node<'tree> {
853 unsafe { Node::new(self.cursor.node(), self.tree) }
855 }
856
857 pub fn field_name(&mut self) -> Option<&'static str> {
859 self.cursor.field_name().or_else(|| {
860 if self.child_depth > 0 {
861 None
862 } else {
863 match self.node().parent() {
864 None => None,
865 Some(parent) => {
866 let original_node = self.node();
867 self.reset(parent);
868 self.goto_first_child();
869 while self.node() != original_node {
870 self.goto_next_sibling();
871 }
872 self.cursor.field_name()
873 }
874 }
875 }
876 })
877 }
878
879 #[inline]
881 pub fn reset(&mut self, node: Node<'tree>) {
882 if self.cursor.node() != node.node {
883 self.cursor.reset(node.node);
884 self.child_depth = if node.tree.root_node() == node { 1 } else { 0 };
885 }
886 }
887
888 #[inline]
891 pub fn goto_first_child(&mut self) -> bool {
892 if self.cursor.goto_first_child() {
893 self.child_depth += 1;
894 true
895 } else {
896 false
897 }
898 }
899
900 #[inline]
905 pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option<usize> {
906 self.cursor.goto_first_child_for_byte(index).map(|index| {
907 self.child_depth += 1;
908 index
909 })
910 }
911
912 #[inline]
917 pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
918 self.cursor
919 .goto_first_child_for_point(point.into())
920 .map(|index| {
921 self.child_depth += 1;
922 index
923 })
924 }
925
926 pub fn goto_next_sibling(&mut self) -> bool {
932 self.cursor.goto_next_sibling()
933 || if self.child_depth > 0 {
934 false
935 } else {
936 match self.node().parent() {
937 None => false,
938 Some(parent) => {
939 let original_node = self.node();
940 self.reset(parent);
941 self.goto_first_child();
942 while self.node() != original_node {
943 self.goto_next_sibling();
944 }
945 self.goto_next_sibling()
946 }
947 }
948 }
949 }
950
951 pub fn goto_parent(&mut self) -> bool {
957 if self.cursor.goto_parent() {
958 debug_assert!(self.child_depth != 0);
959 self.child_depth -= 1;
960 true
961 } else {
962 if self.child_depth > 0 {
963 false
964 } else {
965 match self.node().parent() {
966 None => false,
967 Some(parent) => {
968 self.reset(parent);
969 true
970 }
971 }
972 }
973 }
974 }
975
976 pub fn goto_preorder(&mut self, last_state: TraversalState) -> TraversalState {
983 if !last_state.is_up() && self.goto_first_child() {
984 TraversalState::Down
985 } else if self.goto_next_sibling() {
986 TraversalState::Right
987 } else if self.goto_parent() {
988 TraversalState::Up
989 } else {
990 TraversalState::End
991 }
992 }
993}
994
995impl QueryCursor {
996 #[inline]
998 pub fn new() -> Self {
999 Self {
1000 query_cursor: tree_sitter::QueryCursor::new(),
1001 }
1002 }
1003
1004 #[inline]
1007 pub fn matches<'query, 'cursor: 'query, 'tree>(
1008 &'cursor mut self,
1009 query: &'query Query,
1010 node: Node<'tree>,
1011 ) -> QueryMatches<'query, 'tree> {
1012 QueryMatches {
1013 query_matches: self.query_cursor.matches(&query, node.node, node.tree),
1014 current_match: None,
1015 tree: node.tree,
1016 query,
1017 }
1018 }
1019
1020 #[inline]
1023 pub fn captures<'query, 'cursor: 'query, 'tree>(
1024 &'cursor mut self,
1025 query: &'query Query,
1026 node: Node<'tree>,
1027 ) -> QueryCaptures<'query, 'tree> {
1028 QueryCaptures {
1029 query_captures: self.query_cursor.captures(query, node.node, node.tree),
1030 tree: node.tree,
1031 query,
1032 }
1033 }
1034
1035 #[inline]
1037 pub fn set_match_limit(&mut self, limit: u16) {
1038 self.query_cursor.set_match_limit(limit as u32);
1039 }
1040
1041 #[inline]
1043 pub fn match_limit(&self) -> u16 {
1044 self.query_cursor.match_limit() as u16
1045 }
1046
1047 #[inline]
1050 pub fn did_exceed_match_limit(&self) -> bool {
1051 self.query_cursor.did_exceed_match_limit()
1052 }
1053
1054 #[inline]
1059 pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) -> &mut Self {
1060 self.query_cursor.set_byte_range(range);
1061 self
1062 }
1063
1064 #[inline]
1069 pub fn set_point_range(&mut self, range: PointRange) -> &mut Self {
1070 self.query_cursor.set_point_range(range.to_ts_point_range());
1071 self
1072 }
1073}
1074
1075impl<'query, 'tree> QueryMatches<'query, 'tree> {
1076 #[inline]
1078 pub fn query(&self) -> &'query Query {
1079 self.query
1080 }
1081
1082 #[inline]
1084 pub fn tree(&self) -> &'tree Tree {
1085 self.tree
1086 }
1087
1088 #[inline]
1090 pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) {
1091 self.query_matches.set_byte_range(range);
1092 }
1093
1094 #[inline]
1096 pub fn set_point_range(&mut self, range: PointRange) {
1097 self.query_matches
1098 .set_point_range(range.to_ts_point_range());
1099 }
1100
1101 #[inline]
1103 pub fn as_inner(&self) -> &tree_sitter::QueryMatches<'query, 'tree, &'tree Tree, &'tree str> {
1104 &self.query_matches
1105 }
1106
1107 #[inline]
1109 pub fn as_inner_mut(
1110 &mut self,
1111 ) -> &mut tree_sitter::QueryMatches<'query, 'tree, &'tree Tree, &'tree str> {
1112 &mut self.query_matches
1113 }
1114
1115 #[inline]
1117 pub fn into_inner(
1118 self,
1119 ) -> (
1120 tree_sitter::QueryMatches<'query, 'tree, &'tree Tree, &'tree str>,
1121 &'query Query,
1122 &'tree Tree,
1123 ) {
1124 (self.query_matches, self.query, self.tree)
1125 }
1126}
1127
1128impl<'query, 'tree: 'query> StreamingIterator for QueryMatches<'query, 'tree> {
1129 type Item = QueryMatch<'query, 'tree>;
1130
1131 #[inline]
1132 fn advance(&mut self) {
1133 self.query_matches.advance();
1134 self.current_match = self.query_matches.get().map(|query_match| QueryMatch {
1135 query_match: query_match as *const _,
1136 query: self.query,
1137 tree: self.tree,
1138 });
1139 }
1140
1141 #[inline]
1142 fn get(&self) -> Option<&Self::Item> {
1143 self.current_match.as_ref()
1144 }
1145
1146 fn size_hint(&self) -> (usize, Option<usize>) {
1147 self.query_matches.size_hint()
1148 }
1149}
1150
1151impl<'query, 'tree> QueryCaptures<'query, 'tree> {
1152 #[inline]
1154 pub fn query(&self) -> &'query Query {
1155 self.query
1156 }
1157
1158 #[inline]
1160 pub fn tree(&self) -> &'tree Tree {
1161 self.tree
1162 }
1163
1164 #[inline]
1166 pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) {
1167 self.query_captures.set_byte_range(range);
1168 }
1169
1170 #[inline]
1172 pub fn set_point_range(&mut self, range: PointRange) {
1173 self.query_captures
1174 .set_point_range(range.to_ts_point_range());
1175 }
1176
1177 #[inline]
1179 pub fn as_inner(&self) -> &tree_sitter::QueryCaptures<'query, 'tree, &'tree Tree, &'tree str> {
1180 &self.query_captures
1181 }
1182
1183 #[inline]
1185 pub fn as_inner_mut(
1186 &mut self,
1187 ) -> &mut tree_sitter::QueryCaptures<'query, 'tree, &'tree Tree, &'tree str> {
1188 &mut self.query_captures
1189 }
1190
1191 #[inline]
1193 pub fn into_inner(
1194 self,
1195 ) -> (
1196 tree_sitter::QueryCaptures<'query, 'tree, &'tree Tree, &'tree str>,
1197 &'query Query,
1198 &'tree Tree,
1199 ) {
1200 (self.query_captures, self.query, self.tree)
1201 }
1202}
1203
1204impl<'query, 'tree: 'query> Iterator for QueryCaptures<'query, 'tree> {
1205 type Item = QueryCapture<'query, 'tree>;
1206
1207 #[inline]
1208 fn next(&mut self) -> Option<Self::Item> {
1209 self.query_captures.next().map(|(query_match, index)| {
1210 QueryCapture::new(query_match.captures[*index], self.query, self.tree)
1211 })
1212 }
1213}
1214
1215impl<'query, 'tree> QueryMatch<'query, 'tree> {
1216 #[inline]
1225 pub unsafe fn new(
1226 query_match: &tree_sitter::QueryMatch<'query, 'tree>,
1227 query: &'query Query,
1228 tree: &'tree Tree,
1229 ) -> Self {
1230 Self {
1231 query_match: query_match as *const _,
1232 query,
1233 tree,
1234 }
1235 }
1236
1237 #[inline]
1239 pub fn query(&self) -> &'query Query {
1240 self.query
1241 }
1242
1243 #[inline]
1245 pub fn tree(&self) -> &'tree Tree {
1246 self.tree
1247 }
1248
1249 #[inline]
1251 pub fn iter_captures(&self) -> impl Iterator<Item = QueryCapture<'query, 'tree>> {
1252 self.as_inner()
1253 .captures
1254 .iter()
1255 .map(|&query_capture| QueryCapture::new(query_capture, self.query, self.tree))
1256 }
1257
1258 #[inline]
1260 pub fn capture(&self, index: usize) -> Option<QueryCapture<'query, 'tree>> {
1261 self.as_inner()
1262 .captures
1263 .get(index)
1264 .map(|&query_capture| QueryCapture::new(query_capture, self.query, self.tree))
1265 }
1266
1267 #[inline]
1269 pub fn capture_named(&self, name: &str) -> Option<QueryCapture<'query, 'tree>> {
1270 self.iter_captures().find(|capture| capture.name == name)
1271 }
1272
1273 #[inline]
1275 pub fn captures_named<'a>(
1276 &'a self,
1277 name: &'a str,
1278 ) -> impl Iterator<Item = QueryCapture<'query, 'tree>> + 'a {
1279 self.iter_captures()
1280 .filter(move |capture| capture.name == name)
1281 }
1282
1283 #[inline]
1285 pub fn capture_count(&self) -> usize {
1286 self.as_inner().captures.len()
1287 }
1288
1289 #[inline]
1291 pub fn pattern_index(&self) -> usize {
1292 self.as_inner().pattern_index
1293 }
1294
1295 #[inline]
1297 pub fn id(&self) -> u32 {
1298 self.as_inner().id()
1299 }
1300
1301 #[inline]
1303 pub fn remove(&self) {
1304 self.as_inner().remove()
1305 }
1306
1307 #[inline]
1309 pub fn nodes_for_capture_index(
1310 &self,
1311 capture_index: u32,
1312 ) -> impl Iterator<Item = Node<'tree>> + '_ {
1313 self.as_inner()
1315 .nodes_for_capture_index(capture_index)
1316 .map(move |node| unsafe { Node::new(node, self.tree) })
1317 }
1318
1319 #[inline]
1321 pub fn as_inner(&self) -> &tree_sitter::QueryMatch<'query, 'tree> {
1322 unsafe { &*self.query_match }
1324 }
1325
1326 #[inline]
1329 pub fn into_inner(
1330 self,
1331 ) -> (
1332 *const tree_sitter::QueryMatch<'query, 'tree>,
1333 &'query Query,
1334 &'tree Tree,
1335 ) {
1336 (self.query_match, self.query, self.tree)
1337 }
1338}
1339
1340impl<'query, 'tree> QueryCapture<'query, 'tree> {
1341 #[inline]
1343 fn new(
1344 query_capture: tree_sitter::QueryCapture<'tree>,
1345 query: &'query Query,
1346 tree: &'tree Tree,
1347 ) -> Self {
1348 unsafe {
1350 Self {
1351 node: Node::new(query_capture.node, tree),
1352 index: query_capture.index as usize,
1353 name: &query.capture_names()[query_capture.index as usize],
1354 }
1355 }
1356 }
1357}
1358
1359impl From<tree_sitter::Point> for Point {
1360 #[inline]
1361 fn from(value: tree_sitter::Point) -> Self {
1362 Self {
1363 row: value.row,
1364 column: value.column,
1365 }
1366 }
1367}
1368
1369impl From<Point> for tree_sitter::Point {
1370 #[inline]
1371 fn from(value: Point) -> Self {
1372 Self {
1373 row: value.row,
1374 column: value.column,
1375 }
1376 }
1377}
1378
1379impl Display for Point {
1380 #[inline]
1381 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1382 write!(f, "{}:{}", self.row + 1, self.column + 1)
1383 }
1384}
1385
1386impl PointRange {
1387 #[inline]
1389 pub fn to_ops_range(self) -> std::ops::Range<Point> {
1390 self.start..self.end
1391 }
1392
1393 #[inline]
1395 pub fn to_ts_point_range(self) -> std::ops::Range<tree_sitter::Point> {
1396 self.start.into()..self.end.into()
1397 }
1398}
1399
1400impl From<std::ops::Range<Point>> for PointRange {
1401 #[inline]
1402 fn from(value: std::ops::Range<Point>) -> Self {
1403 Self {
1404 start: value.start,
1405 end: value.end,
1406 }
1407 }
1408}
1409
1410impl RangeBounds<Point> for PointRange {
1411 fn start_bound(&self) -> Bound<&Point> {
1412 Bound::Excluded(&self.start)
1413 }
1414
1415 fn end_bound(&self) -> Bound<&Point> {
1416 Bound::Excluded(&self.end)
1417 }
1418}
1419
1420impl Display for PointRange {
1421 #[inline]
1422 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1423 if self.start == self.end {
1424 write!(f, "{}", self.start)
1425 } else if self.start.row == self.end.row {
1426 write!(f, "{}-{}", self.start, self.end.column + 1)
1427 } else {
1428 write!(f, "{}-{}", self.start, self.end)
1429 }
1430 }
1431}
1432
1433impl From<tree_sitter::Range> for Range {
1434 #[inline]
1435 fn from(value: tree_sitter::Range) -> Self {
1436 Self {
1437 start_byte: value.start_byte,
1438 end_byte: value.end_byte,
1439 start_point: Point::from(value.start_point),
1440 end_point: Point::from(value.end_point),
1441 }
1442 }
1443}
1444
1445impl From<Range> for tree_sitter::Range {
1446 #[inline]
1447 fn from(value: Range) -> Self {
1448 Self {
1449 start_byte: value.start_byte,
1450 end_byte: value.end_byte,
1451 start_point: value.start_point.into(),
1452 end_point: value.end_point.into(),
1453 }
1454 }
1455}
1456
1457impl From<Range> for PointRange {
1458 #[inline]
1459 fn from(value: Range) -> Self {
1460 Self {
1461 start: value.start_point,
1462 end: value.end_point,
1463 }
1464 }
1465}
1466
1467impl BitOr for Range {
1468 type Output = Range;
1469
1470 #[inline]
1472 fn bitor(self, rhs: Self) -> Self::Output {
1473 Range {
1474 start_byte: self.start_byte.min(rhs.start_byte),
1475 end_byte: self.end_byte.max(rhs.end_byte),
1476 start_point: self.start_point.min(rhs.start_point),
1477 end_point: self.end_point.max(rhs.end_point),
1478 }
1479 }
1480}
1481
1482impl BitOrAssign for Range {
1483 #[inline]
1485 fn bitor_assign(&mut self, rhs: Self) {
1486 *self = *self | rhs;
1487 }
1488}
1489
1490impl BitAnd for Range {
1491 type Output = Option<Range>;
1492
1493 #[inline]
1495 fn bitand(self, rhs: Self) -> Self::Output {
1496 let start_byte = self.start_byte.max(rhs.start_byte);
1497 let end_byte = self.end_byte.min(rhs.end_byte);
1498 let start_point = self.start_point.max(rhs.start_point);
1499 let end_point = self.end_point.min(rhs.end_point);
1500
1501 if start_byte <= end_byte && start_point <= end_point {
1502 Some(Range {
1503 start_byte,
1504 end_byte,
1505 start_point,
1506 end_point,
1507 })
1508 } else {
1509 None
1510 }
1511 }
1512}
1513
1514impl Display for Range {
1515 #[inline]
1516 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1517 write!(f, "{}", PointRange::from(*self))
1518 }
1519}
1520
1521impl NodeId {
1522 pub const INVALID: Self = Self(usize::MAX);
1524
1525 #[inline]
1527 fn of_ts(node: tree_sitter::Node<'_>) -> Self {
1528 NodeId(node.id())
1529 }
1530}
1531
1532impl From<u64> for NodeId {
1533 #[inline]
1534 fn from(value: u64) -> Self {
1535 NodeId(value as usize)
1536 }
1537}
1538
1539impl Into<u64> for NodeId {
1540 #[inline]
1541 fn into(self) -> u64 {
1542 self.0 as u64
1543 }
1544}
1545
1546impl Display for NodeId {
1547 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1548 write!(f, "#{}", self.0)
1549 }
1550}
1551
1552impl Clone for TreeParseError {
1553 #[inline]
1554 fn clone(&self) -> Self {
1555 match self {
1556 TreeParseError::IO(e) => TreeParseError::IO(std::io::Error::from(e.kind())),
1557 TreeParseError::ParsingFailed => TreeParseError::ParsingFailed,
1558 TreeParseError::NotUtf8(e) => TreeParseError::NotUtf8(*e),
1559 }
1560 }
1561}
1562
1563impl From<std::io::Error> for TreeParseError {
1564 fn from(e: std::io::Error) -> Self {
1565 TreeParseError::IO(e)
1566 }
1567}
1568
1569impl From<Utf8Error> for TreeParseError {
1570 fn from(e: Utf8Error) -> Self {
1571 TreeParseError::NotUtf8(e)
1572 }
1573}
1574
1575impl Display for TreeParseError {
1576 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1577 match self {
1578 TreeParseError::IO(e) => write!(f, "IO error: {}", e),
1579 TreeParseError::ParsingFailed => write!(f, "Parsing failed"),
1580 TreeParseError::NotUtf8(e) => write!(f, "Not UTF-8: {}", e),
1581 }
1582 }
1583}
1584
1585impl Error for TreeParseError {
1586 fn source(&self) -> Option<&(dyn Error + 'static)> {
1587 match self {
1588 TreeParseError::IO(e) => Some(e),
1589 TreeParseError::ParsingFailed => None,
1590 TreeParseError::NotUtf8(e) => Some(e),
1591 }
1592 }
1593}
1594
1595impl TraversalState {
1596 #[inline]
1598 pub fn is_up(&self) -> bool {
1599 match self {
1600 TraversalState::Up => true,
1601 _ => false,
1602 }
1603 }
1604
1605 #[inline]
1607 pub fn is_end(&self) -> bool {
1608 match self {
1609 TraversalState::End => true,
1610 _ => false,
1611 }
1612 }
1613}
1614
1615impl<'tree> PreorderTraversal<'tree> {
1616 #[inline]
1618 pub fn with_cursor(cursor: TreeCursor<'tree>) -> Self {
1619 Self {
1620 cursor,
1621 last_state: TraversalState::Start,
1622 }
1623 }
1624
1625 #[inline]
1627 pub fn of_tree(tree: &'tree Tree) -> Self {
1628 Self::with_cursor(tree.walk())
1629 }
1630
1631 #[inline]
1633 pub fn of_node(node: Node<'tree>) -> Self {
1634 Self::with_cursor(node.walk())
1635 }
1636
1637 #[inline]
1639 pub fn peek(&mut self) -> TraversalItem<'tree> {
1640 TraversalItem {
1641 node: self.cursor.node(),
1642 field_name: self.cursor.field_name(),
1643 last_state: self.last_state,
1644 }
1645 }
1646
1647 #[inline]
1649 pub fn goto_next(&mut self) -> bool {
1650 if self.last_state.is_end() {
1651 false
1652 } else {
1653 self.last_state = self.cursor.goto_preorder(self.last_state);
1654 true
1655 }
1656 }
1657}
1658
1659impl<'tree> Iterator for PreorderTraversal<'tree> {
1660 type Item = TraversalItem<'tree>;
1661
1662 #[inline]
1663 fn next(&mut self) -> Option<TraversalItem<'tree>> {
1664 if self.last_state.is_end() {
1665 return None;
1666 }
1667 let item = self.peek();
1668 self.last_state = self.cursor.goto_preorder(self.last_state);
1669 Some(item)
1670 }
1671}
1672
1673impl<'tree> FusedIterator for PreorderTraversal<'tree> {}
1674
1675impl<'tree> Debug for Node<'tree> {
1677 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1678 write!(f, "{:?}", self.node)
1679 }
1680}
1681
1682impl Debug for NodePtr {
1683 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1684 write!(f, "{:?}", self.node_data)
1685 }
1686}
1687
1688impl<'query, 'tree> Debug for QueryMatch<'query, 'tree> {
1689 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1690 write!(f, "{:?}", self.query_match)
1691 }
1692}
1693impl<'tree> PartialEq for TraversalItem<'tree> {
1696 fn eq(&self, other: &Self) -> bool {
1697 if self.node == other.node {
1698 debug_assert_eq!(
1699 self.field_name, other.field_name,
1700 "field_name must be the same if node is the same"
1701 );
1702 }
1703 self.node == other.node && self.last_state == other.last_state
1704 }
1705}
1706
1707impl<'tree> Eq for TraversalItem<'tree> {}
1708