feat(treesitter): add TSNode:child_containing_descendant()

This commit is contained in:
vanaigr 2024-04-25 18:46:48 -05:00
parent d855c7a2fb
commit aa36e84474
2 changed files with 33 additions and 0 deletions

View File

@ -725,6 +725,7 @@ static struct luaL_Reg node_meta[] = {
{ "descendant_for_range", node_descendant_for_range },
{ "named_descendant_for_range", node_named_descendant_for_range },
{ "parent", node_parent },
{ "child_containing_descendant", node_child_containing_descendant },
{ "iter_children", node_iter_children },
{ "next_sibling", node_next_sibling },
{ "prev_sibling", node_prev_sibling },
@ -1052,6 +1053,15 @@ static int node_parent(lua_State *L)
return 1;
}
static int node_child_containing_descendant(lua_State *L)
{
TSNode node = node_check(L, 1);
TSNode descendant = node_check(L, 2);
TSNode child = ts_node_child_containing_descendant(node, descendant);
push_node(L, child, 1);
return 1;
}
static int node_next_sibling(lua_State *L)
{
TSNode node = node_check(L, 1);

View File

@ -143,4 +143,27 @@ describe('treesitter node API', function()
eq(28, lua_eval('root:byte_length()'))
eq(3, lua_eval('child:byte_length()'))
end)
it('child_containing_descendant() works', function()
insert([[
int main() {
int x = 3;
}]])
exec_lua([[
tree = vim.treesitter.get_parser(0, "c"):parse()[1]
root = tree:root()
main = root:child(0)
body = main:child(2)
statement = body:child(1)
declarator = statement:child(1)
value = declarator:child(1)
]])
eq(lua_eval('main:type()'), lua_eval('root:child_containing_descendant(value):type()'))
eq(lua_eval('body:type()'), lua_eval('main:child_containing_descendant(value):type()'))
eq(lua_eval('statement:type()'), lua_eval('body:child_containing_descendant(value):type()'))
eq(lua_eval('declarator:type()'), lua_eval('statement:child_containing_descendant(value):type()'))
eq(vim.NIL, lua_eval('declarator:child_containing_descendant(value)'))
end)
end)