mirror of
https://github.com/neovim/neovim.git
synced 2025-09-24 12:08:33 +00:00
feat(treesitter): upstream get_root_for_node() as a node method
Util from the nvim-treesitter project.
This commit is contained in:
@@ -89,6 +89,8 @@ static struct luaL_Reg node_meta[] = {
|
|||||||
{ "next_named_sibling", node_next_named_sibling },
|
{ "next_named_sibling", node_next_named_sibling },
|
||||||
{ "prev_named_sibling", node_prev_named_sibling },
|
{ "prev_named_sibling", node_prev_named_sibling },
|
||||||
{ "named_children", node_named_children },
|
{ "named_children", node_named_children },
|
||||||
|
{ "root", node_root },
|
||||||
|
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1088,6 +1090,27 @@ static int node_named_children(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int node_root(lua_State *L)
|
||||||
|
{
|
||||||
|
TSNode parent;
|
||||||
|
TSNode result;
|
||||||
|
|
||||||
|
TSNode node;
|
||||||
|
if (!node_check(L, 1, &node)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
parent = node;
|
||||||
|
result = node;
|
||||||
|
|
||||||
|
while (!ts_node_is_null(parent)){
|
||||||
|
result = parent;
|
||||||
|
parent = ts_node_parent(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
push_node(L, result, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/// assumes the match table being on top of the stack
|
/// assumes the match table being on top of the stack
|
||||||
static void set_match(lua_State *L, TSQueryMatch *match, int nodeidx)
|
static void set_match(lua_State *L, TSQueryMatch *match, int nodeidx)
|
||||||
{
|
{
|
||||||
|
@@ -77,4 +77,19 @@ describe('treesitter node API', function()
|
|||||||
eq(3, len)
|
eq(3, len)
|
||||||
eq('<node compound_statement>', lua_eval('tostring(children[3])'))
|
eq('<node compound_statement>', lua_eval('tostring(children[3])'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('can retrieve the tree root given a node', function()
|
||||||
|
insert([[
|
||||||
|
int main() {
|
||||||
|
int x = 3;
|
||||||
|
}]])
|
||||||
|
|
||||||
|
exec_lua([[
|
||||||
|
tree = vim.treesitter.get_parser(0, "c"):parse()[1]
|
||||||
|
root = tree:root()
|
||||||
|
node = root:child(0):child(2)
|
||||||
|
]])
|
||||||
|
|
||||||
|
eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user