Merge pull request #1109 from nhasabni/nhasabni/ts_node_child_field_name

Adding API to get field name of a TSNode
This commit is contained in:
Max Brunsfeld 2021-05-21 20:41:17 -07:00 committed by GitHub
commit e4e5ffe517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 0 deletions

View file

@ -248,6 +248,31 @@ fn test_node_parent_of_child_by_field_name() {
);
}
#[test]
fn test_node_field_name_for_child() {
let mut parser = Parser::new();
parser.set_language(get_language("c")).unwrap();
let tree = parser.parse("x + y;", None).unwrap();
let translation_unit_node = tree.root_node();
let binary_expression_node = translation_unit_node
.named_child(0)
.unwrap()
.named_child(0)
.unwrap();
assert_eq!(binary_expression_node.field_name_for_child(0), Some("left"));
assert_eq!(
binary_expression_node.field_name_for_child(1),
Some("operator")
);
assert_eq!(
binary_expression_node.field_name_for_child(2),
Some("right")
);
// Negative test - Not a valid child index
assert_eq!(binary_expression_node.field_name_for_child(3), None);
}
#[test]
fn test_node_child_by_field_name_with_extra_hidden_children() {
let mut parser = Parser::new();

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

View file

@ -487,6 +487,12 @@ TSNode ts_node_parent(TSNode);
*/
TSNode ts_node_child(TSNode, uint32_t);
/**
* Get the field name for node's child at the given index, where zero represents
* the first child. Returns NULL, if no field is found.
*/
const char *ts_node_field_name_for_child(TSNode, uint32_t);
/**
* Get the node's number of children.
*/

View file

@ -569,6 +569,23 @@ recur:
return ts_node__null();
}
const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) {
const TSFieldMapEntry *field_map_start = NULL, *field_map_end = NULL;
ts_language_field_map(
self.tree->language,
ts_node__subtree(self).ptr->production_id,
&field_map_start,
&field_map_end
);
for (const TSFieldMapEntry *i = field_map_start; i < field_map_end; i++) {
if (i->child_index == child_index) {
return self.tree->language->field_names[i->field_id];
}
}
return NULL;
}
TSNode ts_node_child_by_field_name(
TSNode self,
const char *name,