Skip to content

Commit 07195d9

Browse files
committed
bindings: Require range and match limit in InactiveQueryCursor::new
Because of the way that cursors are cached, we need to set or reset the byte range and match limit when re-using from the cache. In most cases callers should set a byte range and match limit, so rather than resetting these we require them in `new`.
1 parent f9fb118 commit 07195d9

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

bindings/src/query_cursor.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,18 @@ pub struct InactiveQueryCursor {
133133
}
134134

135135
impl InactiveQueryCursor {
136-
pub fn new() -> Self {
137-
unsafe {
136+
#[must_use]
137+
pub fn new(range: Range<u32>, limit: u32) -> Self {
138+
let mut this = unsafe {
138139
with_cache(|cache| {
139140
cache.pop().unwrap_or_else(|| InactiveQueryCursor {
140141
ptr: NonNull::new_unchecked(ts_query_cursor_new()),
141142
})
142143
})
143-
}
144+
};
145+
this.set_byte_range(range);
146+
this.set_match_limit(limit);
147+
this
144148
}
145149

146150
/// Return the maximum number of in-progress matches for this cursor.
@@ -193,7 +197,7 @@ impl InactiveQueryCursor {
193197

194198
impl Default for InactiveQueryCursor {
195199
fn default() -> Self {
196-
Self::new()
200+
Self::new(0..u32::MAX, u32::MAX)
197201
}
198202
}
199203

highlighter/src/injections_query.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,8 @@ impl InjectionsQuery {
290290
source: RopeSlice<'a>,
291291
loader: &'a impl LanguageLoader,
292292
) -> impl Iterator<Item = InjectionQueryMatch<'a>> + 'a {
293-
let mut cursor = InactiveQueryCursor::new();
294-
cursor.set_byte_range(0..u32::MAX);
295-
cursor.set_match_limit(TREE_SITTER_MATCH_LIMIT);
296-
let mut cursor = cursor.execute_query(&self.injection_query, node, source);
293+
let mut cursor = InactiveQueryCursor::new(0..u32::MAX, TREE_SITTER_MATCH_LIMIT)
294+
.execute_query(&self.injection_query, node, source);
297295
let injection_content_capture = self.injection_content_capture.unwrap();
298296
let iter = iter::from_fn(move || loop {
299297
let (query_match, node_idx) = cursor.next_matched_node()?;

highlighter/src/locals.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,8 @@ impl Syntax {
189189
}
190190

191191
let root = layer_data.parse_tree.as_ref().unwrap().root_node();
192-
let mut cursor = InactiveQueryCursor::new();
193-
cursor.set_byte_range(0..u32::MAX);
194-
cursor.set_match_limit(TREE_SITTER_MATCH_LIMIT);
195-
let mut cursor = cursor.execute_query(&injection_query.local_query, &root, source);
192+
let mut cursor = InactiveQueryCursor::new(0..u32::MAX, TREE_SITTER_MATCH_LIMIT)
193+
.execute_query(&injection_query.local_query, &root, source);
196194
let mut locals = Locals::default();
197195
let mut scope = Scope::ROOT;
198196

highlighter/src/query_iter.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ where
120120
.get_query(layer.language)
121121
.and_then(|query| Some((query, layer.tree()?.root_node())))
122122
.map(|(query, node)| {
123-
let mut cursor = InactiveQueryCursor::new();
124-
cursor.set_match_limit(TREE_SITTER_MATCH_LIMIT);
125-
cursor.set_byte_range(self.range.clone());
126-
cursor.execute_query(query, &node, RopeInput::new(self.src))
123+
InactiveQueryCursor::new(self.range.clone(), TREE_SITTER_MATCH_LIMIT)
124+
.execute_query(query, &node, RopeInput::new(self.src))
127125
})
128126
};
129127
Box::new(ActiveLayer {

0 commit comments

Comments
 (0)