feat(rust): add Language::node_kind_is_supertype

This commit is contained in:
Amaan Qureshi 2024-10-14 17:00:48 -04:00
parent fb23de9261
commit 38e3e51fca
2 changed files with 32 additions and 66 deletions

View file

@ -1,12 +1,4 @@
use std::ffi::CStr;
use tree_sitter::{
ffi::{
ts_language_symbol_name, ts_language_symbol_type, TSSymbol, TSSymbolTypeAnonymous,
TSSymbolTypeAuxiliary, TSSymbolTypeRegular, TSSymbolTypeSupertype,
},
Parser,
};
use tree_sitter::{self, Parser};
use super::helpers::fixtures::get_language;
@ -75,64 +67,31 @@ fn test_lookahead_iterator_modifiable_only_by_mut() {
#[test]
fn test_symbol_metadata_checks() {
let language = get_language("rust");
let ts_language = language.clone().into_raw();
for i in 0..language.node_kind_count() {
let ts_symbol: TSSymbol = i.try_into().unwrap();
unsafe {
let name = CStr::from_ptr(ts_language_symbol_name(ts_language, ts_symbol))
.to_str()
.unwrap();
match name {
"_type"
| "_expression"
| "_pattern"
| "_literal"
| "_literal_pattern"
| "_declaration_statement" => {
assert_eq!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeSupertype
);
assert_ne!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeAuxiliary
);
}
"_raw_string_literal_start"
| "_raw_string_literal_end"
| "_line_doc_comment"
| "_error_sentinel" => {
assert_eq!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeAuxiliary
);
assert_ne!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeSupertype
);
}
"enum_item" | "struct_item" | "type_item" => {
assert_ne!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeSupertype
);
assert_eq!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeRegular
);
}
"=>" | "[" | "]" | "(" | ")" | "{" | "}" => {
assert_ne!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeSupertype
);
assert_eq!(
ts_language_symbol_type(ts_language, ts_symbol),
TSSymbolTypeAnonymous
);
}
_ => {}
let sym = i as u16;
let name = language.node_kind_for_id(sym).unwrap();
match name {
"_type"
| "_expression"
| "_pattern"
| "_literal"
| "_literal_pattern"
| "_declaration_statement" => assert!(language.node_kind_is_supertype(sym)),
"_raw_string_literal_start"
| "_raw_string_literal_end"
| "_line_doc_comment"
| "_error_sentinel" => assert!(!language.node_kind_is_supertype(sym)),
"enum_item" | "struct_item" | "type_item" => {
assert!(language.node_kind_is_named(sym));
}
"=>" | "[" | "]" | "(" | ")" | "{" | "}" => {
assert!(language.node_kind_is_visible(sym));
}
_ => {}
}
}
}

View file

@ -349,12 +349,19 @@ impl Language {
unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeRegular }
}
#[doc(alias = "ts_language_symbol_type")]
/// Check if the node type for the given numerical id is visible (as opposed
/// to a hidden node type).
#[must_use]
pub fn node_kind_is_visible(&self, id: u16) -> bool {
unsafe { ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolTypeAnonymous }
}
/// Check if the node type for the given numerical id is a supertype.
#[must_use]
pub fn node_kind_is_supertype(&self, id: u16) -> bool {
unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeSupertype }
}
/// Get the number of distinct field names in this language.
#[doc(alias = "ts_language_field_count")]
#[must_use]