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 }
}