From a2119cb6914d62e626fcc40684ef900d7fa90d86 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 12 Jun 2023 11:50:44 -0700 Subject: [PATCH] Add APIs for retrieving tree cursor's depth and descendant index --- lib/binding_rust/bindings.rs | 7 +++++- lib/binding_rust/lib.rs | 15 +++++++++++++ lib/binding_web/README.md | 2 +- lib/include/tree_sitter/api.h | 8 ++++++- lib/src/tree_cursor.c | 42 ++++++++++++++--------------------- script/generate-bindings | 1 + 6 files changed, 47 insertions(+), 28 deletions(-) diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 1d3b6a30..b584a509 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -615,7 +615,12 @@ extern "C" { extern "C" { #[doc = " Get the index of the cursor's current node out of all of the"] #[doc = " descendants of the original node that the cursor was constructed with."] - pub fn ts_tree_cursor_descendant_index(arg1: *mut TSTreeCursor) -> u32; + pub fn ts_tree_cursor_current_descendant_index(arg1: *const TSTreeCursor) -> u32; +} +extern "C" { + #[doc = " Get the depth of the cursor's current node relative to the original"] + #[doc = " node that the cursor was constructed with."] + pub fn ts_tree_cursor_current_depth(arg1: *const TSTreeCursor) -> u32; } extern "C" { #[doc = " Move the cursor to the first child of its current node that extends beyond"] diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index b717a161..b6255085 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -1266,6 +1266,21 @@ impl<'a> TreeCursor<'a> { } } + /// Get the numerical field id of this tree cursor's current node. + /// + /// See also [field_name](TreeCursor::field_name). + #[doc(alias = "ts_tree_cursor_current_depth")] + pub fn depth(&self) -> u32 { + unsafe { ffi::ts_tree_cursor_current_depth(&self.0) } + } + + /// Get the index of the cursor's current node out of all of the + /// descendants of the original node that the cursor was constructed with + #[doc(alias = "ts_tree_cursor_current_descendant_index")] + pub fn descendant_index(&self) -> usize { + unsafe { ffi::ts_tree_cursor_current_descendant_index(&self.0) as usize } + } + /// Move this cursor to the first child of its current node. /// /// This returns `true` if the cursor successfully moved, and returns `false` diff --git a/lib/binding_web/README.md b/lib/binding_web/README.md index b0ffc3ef..bfddd9ef 100644 --- a/lib/binding_web/README.md +++ b/lib/binding_web/README.md @@ -12,7 +12,7 @@ WebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-si You can download the `tree-sitter.js` and `tree-sitter.wasm` files from [the latest GitHub release](https://github.com/tree-sitter/tree-sitter/releases/latest) and load them using a standalone script: ```html -