From a577fb778addb6eb305ade82a229b52673ced234 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sat, 30 Apr 2022 20:54:25 +0200 Subject: feat(treesitter): upstream get_named_children() as a node method Util from the nvim-treesitter project. --- src/nvim/lua/treesitter.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src') 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) { -- cgit From baba43681e792db30318bdedc3e73e4fe12482a6 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sun, 1 May 2022 11:35:12 +0200 Subject: feat(treesitter): upstream get_root_for_node() as a node method Util from the nvim-treesitter project. --- src/nvim/lua/treesitter.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src') 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) { -- cgit From f57341a4b69398ff0c58686e66c2f4138be164aa Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sun, 1 May 2022 21:13:47 +0200 Subject: feat(treesitter): upstream node_length() as a node method Util from the nvim-treesitter project. --- src/nvim/lua/treesitter.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 083042ad98..8803fd452c 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -90,6 +90,7 @@ static struct luaL_Reg node_meta[] = { { "prev_named_sibling", node_prev_named_sibling }, { "named_children", node_named_children }, { "root", node_root }, + { "byte_length", node_byte_length }, { NULL, NULL } }; @@ -1111,6 +1112,20 @@ static int node_root(lua_State *L) return 1; } +static int node_byte_length(lua_State *L) +{ + TSNode node; + if (!node_check(L, 1, &node)) { + return 0; + } + + uint32_t start_byte = ts_node_start_byte(node); + uint32_t end_byte = ts_node_end_byte(node); + + lua_pushnumber(L, end_byte - start_byte); + return 1; +} + /// assumes the match table being on top of the stack static void set_match(lua_State *L, TSQueryMatch *match, int nodeidx) { -- cgit From e5fe41198c57a746b282fdfcde5ccb7c5adad605 Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 25 Aug 2022 18:24:56 +0200 Subject: fix(treesitter): more efficient node:root() --- src/nvim/lua/treesitter.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 8803fd452c..7ff4fbbff4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -623,7 +623,7 @@ void push_tree(lua_State *L, TSTree *tree, bool do_copy) lua_setfenv(L, -2); // [udata] } -static TSTree **tree_check(lua_State *L, uint16_t index) +static TSTree **tree_check(lua_State *L, int index) { TSTree **ud = luaL_checkudata(L, index, TS_META_TREE); return ud; @@ -1093,22 +1093,13 @@ static int node_named_children(lua_State *L) 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); + TSNode root = ts_tree_root_node(node.tree); + push_node(L, root, 1); return 1; } -- cgit