diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2019-06-15 13:43:30 +0200 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2019-09-28 14:55:43 +0200 |
commit | a88a9f128e29b27315a87d0119fbc649196999bc (patch) | |
tree | a4da08e711be9c6f69d969608abffb49ef9b97d6 | |
parent | c1dc1bedba4e0e3db2cd2e52d9241991567f8218 (diff) | |
download | rneovim-a88a9f128e29b27315a87d0119fbc649196999bc.tar.gz rneovim-a88a9f128e29b27315a87d0119fbc649196999bc.tar.bz2 rneovim-a88a9f128e29b27315a87d0119fbc649196999bc.zip |
tree-sitter: add some more API
-rw-r--r-- | src/nvim/lua/treesitter.c | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 2337d598b3..9d599da85f 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -54,11 +54,19 @@ static struct luaL_Reg node_meta[] = { { "__len", node_child_count }, { "range", node_range }, { "start", node_start }, + { "end_", node_end }, { "type", node_type }, { "symbol", node_symbol }, + { "named", node_named }, + { "missing", node_missing }, + { "has_error", node_has_error }, + { "sexpr", node_sexpr }, { "child_count", node_child_count }, + { "named_child_count", node_named_child_count }, { "child", node_child }, + { "named_child", node_named_child }, { "descendant_for_point_range", node_descendant_for_point_range }, + { "named_descendant_for_point_range", node_named_descendant_for_point_range }, { "parent", node_parent }, { NULL, NULL } }; @@ -417,6 +425,20 @@ static int node_start(lua_State *L) return 3; } +static int node_end(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint end = ts_node_end_point(node); + uint32_t end_byte = ts_node_end_byte(node); + lua_pushnumber(L, end.row); + lua_pushnumber(L, end.column); + lua_pushnumber(L, end_byte); + return 3; +} + static int node_child_count(lua_State *L) { TSNode node; @@ -428,6 +450,17 @@ static int node_child_count(lua_State *L) return 1; } +static int node_named_child_count(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + uint32_t count = ts_node_named_child_count(node); + lua_pushnumber(L, count); + return 1; +} + static int node_type(lua_State *L) { TSNode node; @@ -449,6 +482,48 @@ static int node_symbol(lua_State *L) return 1; } +static int node_named(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_is_named(node)); + return 1; +} + +static int node_sexpr(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + char *allocated = ts_node_string(node); + lua_pushstring(L, allocated); + xfree(allocated); + return 1; +} + +static int node_missing(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_is_missing(node)); + return 1; +} + +static int node_has_error(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_has_error(node)); + return 1; +} + static int node_child(lua_State *L) { TSNode node; @@ -463,6 +538,20 @@ static int node_child(lua_State *L) return 1; } +static int node_named_child(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + long num = lua_tointeger(L, 2); + TSNode child = ts_node_named_child(node, (uint32_t)num); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + static int node_descendant_for_point_range(lua_State *L) { TSNode node; @@ -480,6 +569,23 @@ static int node_descendant_for_point_range(lua_State *L) return 1; } +static int node_named_descendant_for_point_range(lua_State *L) +{ + TSNode node; + if (!node_check(L, &node)) { + return 0; + } + TSPoint start = { (uint32_t)lua_tointeger(L, 2), + (uint32_t)lua_tointeger(L, 3) }; + TSPoint end = { (uint32_t)lua_tointeger(L, 4), + (uint32_t)lua_tointeger(L, 5) }; + TSNode child = ts_node_named_descendant_for_point_range(node, start, end); + + lua_pushvalue(L, 1); + push_node(L, child); + return 1; +} + static int node_parent(lua_State *L) { TSNode node; |