Start work on new ref API, for giving names to nodes' children

Co-Authored-By: Ayman Nadeem <aymannadeem@gmail.com>
This commit is contained in:
Max Brunsfeld 2019-01-22 12:02:17 -08:00
parent dac13af206
commit bf4e1304f8
7 changed files with 85 additions and 1 deletions

View file

@ -227,6 +227,9 @@ extern "C" {
extern "C" {
pub fn ts_node_child(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_child_by_ref(arg1: TSNode, arg2: *const ::std::os::raw::c_char) -> TSNode;
}
extern "C" {
pub fn ts_node_named_child(arg1: TSNode, arg2: u32) -> TSNode;
}

View file

@ -12,7 +12,7 @@ use std::os::unix::io::AsRawFd;
use regex::Regex;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
use std::fmt;
use std::marker::PhantomData;
use std::os::raw::{c_char, c_void};
@ -463,6 +463,14 @@ impl<'tree> Node<'tree> {
Self::new(unsafe { ffi::ts_node_child(self.0, i as u32) })
}
pub fn child_by_ref(&self, ref_name: &str) -> Option<Self> {
if let Ok(c_ref_name) = CString::new(ref_name) {
Self::new(unsafe { ffi::ts_node_child_by_ref(self.0, c_ref_name.as_ptr()) })
} else {
None
}
}
pub fn child_count(&self) -> usize {
unsafe { ffi::ts_node_child_count(self.0) as usize }
}

View file

@ -119,6 +119,7 @@ bool ts_node_has_changes(TSNode);
bool ts_node_has_error(TSNode);
TSNode ts_node_parent(TSNode);
TSNode ts_node_child(TSNode, uint32_t);
TSNode ts_node_child_by_ref(TSNode, const char *);
TSNode ts_node_named_child(TSNode, uint32_t);
uint32_t ts_node_child_count(TSNode);
uint32_t ts_node_named_child_count(TSNode);

View file

@ -453,6 +453,10 @@ TSNode ts_node_named_child(TSNode self, uint32_t child_index) {
return ts_node__child(self, child_index, false);
}
TSNode ts_node_child_by_ref(TSNode self, const char *ref_name) {
return ts_node__null();
}
uint32_t ts_node_child_count(TSNode self) {
Subtree tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0) {