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