feat: add Supertype API

Introduces a new function that takes in a supertype symbol and returns
all associated subtypes. Can be used by query.c to give better errors
for invalid subtypes, as well as downstream applications like the query
LSP to give better diagnostics.
This commit is contained in:
Riley Bruins 2024-11-12 11:43:00 -08:00 committed by Amaan Qureshi
parent 07c08432ca
commit 19482834bd
11 changed files with 459 additions and 78 deletions

View file

@ -759,13 +759,6 @@ extern "C" {
#[doc = " Get the number of valid states in this language."]
pub fn ts_language_state_count(self_: *const TSLanguage) -> u32;
}
extern "C" {
#[doc = " Get a node type string for the given numerical id."]
pub fn ts_language_symbol_name(
self_: *const TSLanguage,
symbol: TSSymbol,
) -> *const ::core::ffi::c_char;
}
extern "C" {
#[doc = " Get the numerical id for the given node type string."]
pub fn ts_language_symbol_for_name(
@ -794,6 +787,25 @@ extern "C" {
name_length: u32,
) -> TSFieldId;
}
extern "C" {
#[doc = " Get a list of all supertype symbols for the language."]
pub fn ts_language_supertypes(self_: *const TSLanguage, length: *mut u32) -> *const TSSymbol;
}
extern "C" {
#[doc = " Get a list of all subtype symbol ids for a given supertype symbol.\n\n See [`ts_language_supertypes`] for fetching all supertype symbols."]
pub fn ts_language_subtypes(
self_: *const TSLanguage,
supertype: TSSymbol,
length: *mut u32,
) -> *const TSSymbol;
}
extern "C" {
#[doc = " Get a node type string for the given numerical id."]
pub fn ts_language_symbol_name(
self_: *const TSLanguage,
symbol: TSSymbol,
) -> *const ::core::ffi::c_char;
}
extern "C" {
#[doc = " Check whether the given node type id belongs to named nodes, anonymous nodes,\n or a hidden nodes.\n\n See also [`ts_node_is_named`]. Hidden nodes are never returned from the API."]
pub fn ts_language_symbol_type(self_: *const TSLanguage, symbol: TSSymbol) -> TSSymbolType;

View file

@ -420,6 +420,36 @@ impl Language {
unsafe { ffi::ts_language_state_count(self.0) as usize }
}
/// Get a list of all supertype symbols for the language.
#[doc(alias = "ts_language_supertypes")]
#[must_use]
pub fn supertypes(&self) -> &[u16] {
let mut length = 0u32;
unsafe {
let ptr = ffi::ts_language_supertypes(self.0, core::ptr::addr_of_mut!(length));
if length == 0 {
&[]
} else {
slice::from_raw_parts(ptr.cast_mut(), length as usize)
}
}
}
/// Get a list of all subtype symbol names for a given supertype symbol.
#[doc(alias = "ts_language_supertype_map")]
#[must_use]
pub fn subtypes_for_supertype(&self, supertype: u16) -> &[u16] {
unsafe {
let mut length = 0u32;
let ptr = ffi::ts_language_subtypes(self.0, supertype, core::ptr::addr_of_mut!(length));
if length == 0 {
&[]
} else {
slice::from_raw_parts(ptr.cast_mut(), length as usize)
}
}
}
/// Get the name of the node kind for the given numerical id.
#[doc(alias = "ts_language_symbol_name")]
#[must_use]