From 9a74f5cbf5cb5dc59c0729f16550b33c1eec5cc0 Mon Sep 17 00:00:00 2001 From: Jille Timmermans Date: Thu, 8 Jun 2023 19:37:04 +0100 Subject: [PATCH 1/2] Add ts_node_language() that returns the language of the node Since an input might have a nested sublanguage (like Javascript inside Vue) and symbols are per-language, we need to know which language a node is. --- lib/include/tree_sitter/api.h | 5 +++++ lib/src/node.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index e455e81d..306891df 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -436,6 +436,11 @@ const char *ts_node_type(TSNode); */ TSSymbol ts_node_symbol(TSNode); +/** + * Get the node's language. + */ +const TSLanguage *ts_node_language(TSNode); + /** * Get the node's start byte. */ diff --git a/lib/src/node.c b/lib/src/node.c index d6a190c0..aa947148 100644 --- a/lib/src/node.c +++ b/lib/src/node.c @@ -423,6 +423,10 @@ const char *ts_node_type(TSNode self) { return ts_language_symbol_name(self.tree->language, symbol); } +const TSLanguage *ts_node_language(TSNode self) { + return self.tree->language; +} + char *ts_node_string(TSNode self) { return ts_subtree_string(ts_node__subtree(self), self.tree->language, false); } From 2f2e4160316b3c60a8973950fa3968afbff8a827 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Thu, 13 Jul 2023 11:39:33 -0400 Subject: [PATCH 2/2] chore: update Rust bindings --- lib/binding_rust/bindings.rs | 6 +++++- lib/binding_rust/lib.rs | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 66406adc..a3de6a4a 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -1,4 +1,4 @@ -/* automatically generated by rust-bindgen 0.65.1 */ +/* automatically generated by rust-bindgen 0.66.1 */ pub type TSSymbol = u16; pub type TSFieldId = u16; @@ -278,6 +278,10 @@ extern "C" { #[doc = " Get the node's type as a numerical id."] pub fn ts_node_symbol(arg1: TSNode) -> TSSymbol; } +extern "C" { + #[doc = " Get the node's language."] + pub fn ts_node_language(arg1: TSNode) -> *const TSLanguage; +} extern "C" { #[doc = " Get the node's start byte."] pub fn ts_node_start_byte(arg1: TSNode) -> u32; diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index caa5af44..5bed0817 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -839,9 +839,9 @@ impl<'tree> Node<'tree> { } /// Get the [Language] that was used to parse this node's syntax tree. - #[doc(alias = "ts_tree_language")] + #[doc(alias = "ts_node_language")] pub fn language(&self) -> Language { - Language(unsafe { ffi::ts_tree_language(self.0.tree) }) + Language(unsafe { ffi::ts_node_language(self.0) }) } /// Check if this node is *named*.