From 2096e21ba5d573889304f52d48521e35e139b07a Mon Sep 17 00:00:00 2001 From: Asher Mancinelli Date: Fri, 25 Apr 2025 14:27:38 -0700 Subject: [PATCH] Add getFieldNameForNamedChild --- include/cpp-tree-sitter.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/cpp-tree-sitter.h b/include/cpp-tree-sitter.h index 0340887..473dcf3 100644 --- a/include/cpp-tree-sitter.h +++ b/include/cpp-tree-sitter.h @@ -4,7 +4,7 @@ #include #include #include - +#include #include // Including the API directly already pollutes the namespace, but the @@ -179,7 +179,20 @@ struct Node { [[nodiscard]] std::string_view getFieldNameForChild(uint32_t child_position) const { - return ts_node_field_name_for_child(impl, child_position); + const auto *name = ts_node_field_name_for_child(impl, child_position); + if (!name) { + return std::string_view{}; + } + return std::string_view{name}; + } + + [[nodiscard]] std::string_view + getFieldNameForNamedChild(uint32_t child_position) const { + const auto *name = ts_node_field_name_for_named_child(impl, child_position); + if (!name) { + return std::string_view{}; + } + return std::string_view{name}; } [[nodiscard]] Node