aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/treesitter.c
diff options
context:
space:
mode:
authorQuentin Rasmont <qrasmont@gmail.com>2022-05-01 11:35:12 +0200
committerbfredl <bjorn.linse@gmail.com>2022-08-25 18:01:14 +0200
commitbaba43681e792db30318bdedc3e73e4fe12482a6 (patch)
tree4ac5218831574e3070ac1af42251b49afdebf749 /src/nvim/lua/treesitter.c
parenta577fb778addb6eb305ade82a229b52673ced234 (diff)
downloadrneovim-baba43681e792db30318bdedc3e73e4fe12482a6.tar.gz
rneovim-baba43681e792db30318bdedc3e73e4fe12482a6.tar.bz2
rneovim-baba43681e792db30318bdedc3e73e4fe12482a6.zip
feat(treesitter): upstream get_root_for_node() as a node method
Util from the nvim-treesitter project.
Diffstat (limited to 'src/nvim/lua/treesitter.c')
-rw-r--r--src/nvim/lua/treesitter.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c
index 2ef4d6052c..083042ad98 100644
--- a/src/nvim/lua/treesitter.c
+++ b/src/nvim/lua/treesitter.c
@@ -89,6 +89,8 @@ static struct luaL_Reg node_meta[] = {
{ "next_named_sibling", node_next_named_sibling },
{ "prev_named_sibling", node_prev_named_sibling },
{ "named_children", node_named_children },
+ { "root", node_root },
+
{ NULL, NULL }
};
@@ -1088,6 +1090,27 @@ static int node_named_children(lua_State *L)
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
static void set_match(lua_State *L, TSQueryMatch *match, int nodeidx)
{