From 485d19288027d9b92010c69b8951e7fb6ddd9df0 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Sun, 21 May 2023 01:05:13 +0300 Subject: [PATCH] binding_rust: `set_max_start_depth` accepts optional to reset limit --- cli/src/tests/query_test.rs | 4 ++-- lib/binding_rust/lib.rs | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 28c01f4c..c3bf54a2 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -4648,7 +4648,7 @@ fn test_query_max_start_depth() { eprintln!(" query example: {:?}", row.description); let query = Query::new(language, row.pattern).unwrap(); - cursor.set_max_start_depth(row.depth); + cursor.set_max_start_depth(Some(row.depth)); let matches = cursor.matches(&query, tree.root_node(), source.as_bytes()); let expected = row @@ -4782,7 +4782,7 @@ fn test_query_max_start_depth_more() { for row in rows.iter() { eprintln!(" depth: {}", row.depth); - cursor.set_max_start_depth(row.depth); + cursor.set_max_start_depth(Some(row.depth)); let matches = cursor.matches(&query, node, source.as_bytes()); let expected = row diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index ad20507e..cc3ebe9b 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -2174,10 +2174,25 @@ impl QueryCursor { self } + /// Set the maximum start depth for a query cursor. + /// + /// This prevents cursors from exploring children nodes at a certain depth. + /// Note if a pattern includes many children, then they will still be checked. + /// + /// The zero max start depth value can be used as a special behavior and + /// it helps to destructure a subtree by staying on a node and using captures + /// for interested parts. Note that the zero max start depth only limit a search + /// depth for a pattern's root node but other nodes that are parts of the pattern + /// may be searched at any depth what defined by the pattern structure. + /// + /// Set to `None` to remove the maximum start depth. #[doc(alias = "ts_query_cursor_set_max_start_depth")] - pub fn set_max_start_depth(&mut self, max_start_depth: u32) -> &mut Self { + pub fn set_max_start_depth(&mut self, max_start_depth: Option) -> &mut Self { unsafe { - ffi::ts_query_cursor_set_max_start_depth(self.ptr.as_ptr(), max_start_depth); + ffi::ts_query_cursor_set_max_start_depth( + self.ptr.as_ptr(), + max_start_depth.unwrap_or(u32::MAX), + ); } self }