Skip to content

Commit 0683136

Browse files
ribru17amaanq
authored andcommitted
feat(api): expose function to check if symbol represents a supertype
1 parent 939e61c commit 0683136

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

cli/src/tests/language_test.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use tree_sitter::Parser;
1+
use std::ffi::CStr;
2+
3+
use tree_sitter::{
4+
ffi::{
5+
ts_language_symbol_name, ts_language_symbol_type, TSSymbol, TSSymbolTypeAnonymous,
6+
TSSymbolTypeAuxiliary, TSSymbolTypeRegular, TSSymbolTypeSupertype,
7+
},
8+
Parser,
9+
};
210

311
use super::helpers::fixtures::get_language;
412

@@ -63,3 +71,68 @@ fn test_lookahead_iterator_modifiable_only_by_mut() {
6371
let mut names = lookahead.iter_names();
6472
let _ = names.next();
6573
}
74+
75+
#[test]
76+
fn test_symbol_metadata_checks() {
77+
let language = get_language("rust");
78+
let ts_language = language.clone().into_raw();
79+
for i in 0..language.node_kind_count() {
80+
let ts_symbol: TSSymbol = i.try_into().unwrap();
81+
unsafe {
82+
let name = CStr::from_ptr(ts_language_symbol_name(ts_language, ts_symbol))
83+
.to_str()
84+
.unwrap();
85+
match name {
86+
"_type"
87+
| "_expression"
88+
| "_pattern"
89+
| "_literal"
90+
| "_literal_pattern"
91+
| "_declaration_statement" => {
92+
assert_eq!(
93+
ts_language_symbol_type(ts_language, ts_symbol),
94+
TSSymbolTypeSupertype
95+
);
96+
assert_ne!(
97+
ts_language_symbol_type(ts_language, ts_symbol),
98+
TSSymbolTypeAuxiliary
99+
);
100+
}
101+
"_raw_string_literal_start"
102+
| "_raw_string_literal_end"
103+
| "_line_doc_comment"
104+
| "_error_sentinel" => {
105+
assert_eq!(
106+
ts_language_symbol_type(ts_language, ts_symbol),
107+
TSSymbolTypeAuxiliary
108+
);
109+
assert_ne!(
110+
ts_language_symbol_type(ts_language, ts_symbol),
111+
TSSymbolTypeSupertype
112+
);
113+
}
114+
"enum_item" | "struct_item" | "type_item" => {
115+
assert_ne!(
116+
ts_language_symbol_type(ts_language, ts_symbol),
117+
TSSymbolTypeSupertype
118+
);
119+
assert_eq!(
120+
ts_language_symbol_type(ts_language, ts_symbol),
121+
TSSymbolTypeRegular
122+
);
123+
}
124+
"=>" | "[" | "]" | "(" | ")" | "{" | "}" => {
125+
assert_ne!(
126+
ts_language_symbol_type(ts_language, ts_symbol),
127+
TSSymbolTypeSupertype
128+
);
129+
assert_eq!(
130+
ts_language_symbol_type(ts_language, ts_symbol),
131+
TSSymbolTypeAnonymous
132+
);
133+
}
134+
_ => {}
135+
}
136+
}
137+
}
138+
}

lib/binding_rust/bindings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ pub const TSInputEncodingUTF16: TSInputEncoding = 1;
4040
pub type TSInputEncoding = ::core::ffi::c_uint;
4141
pub const TSSymbolTypeRegular: TSSymbolType = 0;
4242
pub const TSSymbolTypeAnonymous: TSSymbolType = 1;
43-
pub const TSSymbolTypeAuxiliary: TSSymbolType = 2;
43+
pub const TSSymbolTypeSupertype: TSSymbolType = 2;
44+
pub const TSSymbolTypeAuxiliary: TSSymbolType = 3;
4445
pub type TSSymbolType = ::core::ffi::c_uint;
4546
#[repr(C)]
4647
#[derive(Debug, Copy, Clone)]

lib/include/tree_sitter/api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef enum TSInputEncoding {
5656
typedef enum TSSymbolType {
5757
TSSymbolTypeRegular,
5858
TSSymbolTypeAnonymous,
59+
TSSymbolTypeSupertype,
5960
TSSymbolTypeAuxiliary,
6061
} TSSymbolType;
6162

lib/src/language.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ TSSymbolType ts_language_symbol_type(
138138
return TSSymbolTypeRegular;
139139
} else if (metadata.visible) {
140140
return TSSymbolTypeAnonymous;
141+
} else if (metadata.supertype) {
142+
return TSSymbolTypeSupertype;
141143
} else {
142144
return TSSymbolTypeAuxiliary;
143145
}

0 commit comments

Comments
 (0)