diff options
author | Quentin Rasmont <qrasmont@gmail.com> | 2022-04-30 20:54:25 +0200 |
---|---|---|
committer | bfredl <bjorn.linse@gmail.com> | 2022-08-25 18:01:14 +0200 |
commit | a577fb778addb6eb305ade82a229b52673ced234 (patch) | |
tree | fe28abc6010ee73753e97aace4c41648fcae6d0a /src/nvim/lua/treesitter.c | |
parent | 6b2d42eb0352d01923e4bf2e3ce0824c662b7be4 (diff) | |
download | rneovim-a577fb778addb6eb305ade82a229b52673ced234.tar.gz rneovim-a577fb778addb6eb305ade82a229b52673ced234.tar.bz2 rneovim-a577fb778addb6eb305ade82a229b52673ced234.zip |
feat(treesitter): upstream get_named_children() 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.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 954c3410d3..2ef4d6052c 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -88,6 +88,7 @@ static struct luaL_Reg node_meta[] = { { "prev_sibling", node_prev_sibling }, { "next_named_sibling", node_next_named_sibling }, { "prev_named_sibling", node_prev_named_sibling }, + { "named_children", node_named_children }, { NULL, NULL } }; @@ -1062,6 +1063,31 @@ static int node_prev_named_sibling(lua_State *L) return 1; } +static int node_named_children(lua_State *L) +{ + TSNode source; + if (!node_check(L, 1, &source)) { + return 0; + } + TSTreeCursor cursor = ts_tree_cursor_new(source); + + lua_newtable(L); + int curr_index = 0; + + if (ts_tree_cursor_goto_first_child(&cursor)) { + do { + TSNode node = ts_tree_cursor_current_node(&cursor); + if (ts_node_is_named(node)) { + push_node(L, node, 1); + lua_rawseti(L, -2, ++curr_index); + } + } while (ts_tree_cursor_goto_next_sibling(&cursor)); + } + + ts_tree_cursor_delete(&cursor); + return 1; +} + /// assumes the match table being on top of the stack static void set_match(lua_State *L, TSQueryMatch *match, int nodeidx) { |