Changing API name; Adding unit test and Rust bindings

This commit is contained in:
Niranjan Hasabnis 2021-05-21 01:50:10 +00:00
parent bd06b1a8b3
commit c31acb8fec
4 changed files with 38 additions and 2 deletions

View file

@ -434,6 +434,11 @@ extern "C" {
#[doc = " child."]
pub fn ts_node_child(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
#[doc = " Get the field name for node's child at the given index, where zero represents"]
#[doc = " the first child. Returns NULL, if no field is found."]
pub fn ts_node_field_name_for_child(arg1: TSNode, arg2: u32) -> *const ::std::os::raw::c_char;
}
extern "C" {
#[doc = " Get the node\'s number of children."]
pub fn ts_node_child_count(arg1: TSNode) -> u32;

View file

@ -859,6 +859,18 @@ impl<'tree> Node<'tree> {
Self::new(unsafe { ffi::ts_node_child_by_field_id(self.0, field_id) })
}
/// Get the field name of this node's child at the given index.
pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> {
unsafe {
let ptr = ffi::ts_node_field_name_for_child(self.0, child_index);
if ptr.is_null() {
None
} else {
Some(CStr::from_ptr(ptr).to_str().unwrap())
}
}
}
/// Iterate over this node's children.
///
/// A [TreeCursor] is used to retrieve the children efficiently. Obtain