1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::{raw, Node, QueryCaptures, QueryMatches};
#[cfg(not(feature = "yak-sitter"))]
use tree_sitter::Point;
#[cfg(feature = "yak-sitter")]
use yak_sitter::PointRange;
/// Wraps [tree-sitter's `QueryCursor`](raw::QueryCursor) where `matches` and `captures` are always typed.
#[repr(transparent)]
pub struct QueryCursor(pub raw::QueryCursor);
impl QueryCursor {
/// Create a new cursor for executing a given query.
///
/// The cursor stores the state that is needed to iteratively search for matches.
#[inline]
pub fn new() -> Self {
Self(raw::QueryCursor::new())
}
/// Return the maximum number of in-progress matches for this cursor.
#[inline]
#[cfg(feature = "yak-sitter")]
pub fn match_limit(&self) -> u16 {
self.0.match_limit()
}
/// Return the maximum number of in-progress matches for this cursor.
#[inline]
#[cfg(not(feature = "yak-sitter"))]
pub fn match_limit(&self) -> u16 {
u16::try_from(self.0.match_limit()).unwrap()
}
/// Set the maximum number of in-progress matches for this cursor.
#[inline]
#[cfg(feature = "yak-sitter")]
pub fn set_match_limit(&mut self, limit: u16) {
self.0.set_match_limit(limit)
}
/// Set the maximum number of in-progress matches for this cursor.
#[inline]
#[cfg(not(feature = "yak-sitter"))]
pub fn set_match_limit(&mut self, limit: u16) {
self.0.set_match_limit(u32::from(limit))
}
/// Check if, on its last execution, this cursor exceeded its maximum number of in-progress matches.
#[inline]
pub fn did_exceed_match_limit(&self) -> bool {
self.0.did_exceed_match_limit()
}
/// Set the range in which the query will be executed, in terms of byte offsets.
///
/// Like [tree-sitter's `QueryCursor::set_byte_range`](raw::QueryCursor::set_byte_range),
/// returns `self` for convenience (builder pattern).
#[inline]
pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) -> &mut Self {
self.0.set_byte_range(range);
self
}
/// Set the range in which the query will be executed, in terms of rows and columns.
///
/// Like [tree-sitter's `QueryCursor::set_point_range`](raw::QueryCursor::set_point_range),
/// returns `self` for convenience (builder pattern).
#[inline]
#[cfg(feature = "yak-sitter")]
pub fn set_point_range(&mut self, range: PointRange) -> &mut Self {
self.0.set_point_range(range);
self
}
/// Set the range in which the query will be executed, in terms of rows and columns.
///
/// Like [tree-sitter's `QueryCursor::set_point_range`](raw::QueryCursor::set_point_range),
/// returns `self` for convenience (builder pattern).
#[inline]
#[cfg(not(feature = "yak-sitter"))]
pub fn set_point_range(&mut self, range: std::ops::Range<Point>) -> &mut Self {
self.0.set_point_range(range);
self
}
/// Run a typed query on the cursor, iterating over the matches in order they are found.
///
/// Each match contains the index of the pattern that matched, and a list of captures. Because
/// multiple patterns can match the same set of nodes, one match may contain captures that
/// appear before some of the captures from a previous match.
#[inline]
#[cfg(feature = "yak-sitter")]
pub fn matches<'query, 'cursor: 'query, 'tree, Query: crate::Query + 'tree>(
&'cursor mut self,
query: &'query Query,
node: impl Node<'tree>,
) -> QueryMatches<'query, 'tree, Query> {
unsafe { QueryMatches::from_raw(query, self.0.matches(query.raw(), node.into_raw())) }
}
/// Run a typed query on the cursor, iterating over the matches in order they are found.
///
/// Each match contains the index of the pattern that matched, and a list of captures. Because
/// multiple patterns can match the same set of nodes, one match may contain captures that
/// appear before some of the captures from a previous match.
#[inline]
#[cfg(not(feature = "yak-sitter"))]
pub fn matches<
'query,
'cursor: 'query,
'tree,
Query: crate::Query,
Text: tree_sitter::TextProvider<I> + 'query,
I: AsRef<[u8]>,
>(
&'cursor mut self,
query: &'query Query,
node: impl Node<'tree>,
text: Text,
) -> QueryMatches<'query, 'tree, Query, Text, I> {
unsafe { QueryMatches::from_raw(query, self.0.matches(query.raw(), node.into_raw(), text)) }
}
/// Run a typed query on the cursor, iterating over the captures in order they appear.
///
/// This is useful if you don’t care about which pattern matched, and just want a single,
/// ordered sequence of captures.
#[inline]
#[cfg(feature = "yak-sitter")]
pub fn captures<'query, 'cursor: 'query, 'tree, Query: crate::Query>(
&'cursor mut self,
query: &'query Query,
node: impl Node<'tree>,
) -> QueryCaptures<'query, 'tree, Query> {
unsafe { QueryCaptures::new(query, self.0.captures(query.raw(), node.into_raw())) }
}
/// Run a typed query on the cursor, iterating over the captures in order they appear.
///
/// This is useful if you don’t care about which pattern matched, and just want a single,
/// ordered sequence of captures.
#[inline]
#[cfg(not(feature = "yak-sitter"))]
pub fn captures<
'query,
'cursor: 'query,
'tree,
Query: crate::Query,
Text: tree_sitter::TextProvider<I> + 'query,
I: AsRef<[u8]>,
>(
&'cursor mut self,
query: &'query Query,
node: impl Node<'tree>,
text: Text,
) -> QueryCaptures<'query, 'tree, Query, Text, I> {
unsafe { QueryCaptures::new(query, self.0.captures(query.raw(), node.into_raw(), text)) }
}
}