From 55b165ac15a7528a3c679d928b1edf9d701f850b Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Sat, 15 Feb 2025 13:33:38 -0800 Subject: fix(treesitter): `TSNode:field()` returns all children with the given field --- src/nvim/lua/treesitter.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/nvim/lua/treesitter.c') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 3e33fcd142..259f2d4739 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -998,16 +998,21 @@ static int node_symbol(lua_State *L) static int node_field(lua_State *L) { TSNode node = node_check(L, 1); + uint32_t count = ts_node_child_count(node); + int curr_index = 0; size_t name_len; const char *field_name = luaL_checklstring(L, 2, &name_len); - lua_newtable(L); // [table] + lua_newtable(L); - TSNode field = ts_node_child_by_field_name(node, field_name, (uint32_t)name_len); - if (!ts_node_is_null(field)) { - push_node(L, field, 1); // [table, node] - lua_rawseti(L, -2, 1); + for (uint32_t i = 0; i < count; i++) { + const char *child_field_name = ts_node_field_name_for_child(node, i); + if (strequal(field_name, child_field_name)) { + TSNode child = ts_node_child(node, i); + push_node(L, child, 1); + lua_rawseti(L, -2, ++curr_index); + } } return 1; -- cgit