diff options
author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-08-14 16:01:10 +0200 |
---|---|---|
committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-09-01 08:57:55 +0200 |
commit | 18217b987f79afa2f389262d7eb155b1c064f97c (patch) | |
tree | 2ed321fefd4c267d0f521e26aaf3ac76aaf3ff66 /src | |
parent | e123fd0a5dfbddc25cc872c6b4cf075a0c222bbe (diff) | |
download | rneovim-18217b987f79afa2f389262d7eb155b1c064f97c.tar.gz rneovim-18217b987f79afa2f389262d7eb155b1c064f97c.tar.bz2 rneovim-18217b987f79afa2f389262d7eb155b1c064f97c.zip |
treesitter: add node:field() to get field children
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/lua/treesitter.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 308bfe8cfb..9a58823d64 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -62,6 +62,7 @@ static struct luaL_Reg node_meta[] = { { "end_", node_end }, { "type", node_type }, { "symbol", node_symbol }, + { "field", node_field }, { "named", node_named }, { "missing", node_missing }, { "has_error", node_has_error }, @@ -653,6 +654,34 @@ static int node_symbol(lua_State *L) return 1; } +static int node_field(lua_State *L) +{ + TSNode node; + if (!node_check(L, 1, &node)) { + return 0; + } + + size_t name_len; + const char *field_name = luaL_checklstring(L, 2, &name_len); + + TSTreeCursor cursor = ts_tree_cursor_new(node); + + lua_newtable(L); // [table] + unsigned int curr_index = 0; + + if (ts_tree_cursor_goto_first_child(&cursor)) { + do { + if (!STRCMP(field_name, ts_tree_cursor_current_field_name(&cursor))) { + push_node(L, ts_tree_cursor_current_node(&cursor), 1); // [table, node] + lua_rawseti(L, -2, ++curr_index); + } + } while (ts_tree_cursor_goto_next_sibling(&cursor)); + } + + ts_tree_cursor_delete(&cursor); + return 1; +} + static int node_named(lua_State *L) { TSNode node; |