From 800134ea5ec60338a40280c8536db6a6a4a10249 Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Thu, 25 Jan 2024 13:27:48 -0500 Subject: refactor(treesitter): typing for Query, TSQuery, and TSQueryInfo - `TSQuery`: userdata object for parsed query. - `vim.treesitter.Query`: renamed from `Query`. - Add a new field `lang`. - `TSQueryInfo`: - Move to `vim/treesitter/_meta.lua`, because C code owns it. - Correct typing for `patterns`, should be a map from `integer` (pattern_id) to `(integer|string)[][]` (list of predicates or directives). - `vim.treesitter.QueryInfo` is added. - This currently has the same structure as `TSQueryInfo` (exported from C code). - Document the fields (see `TSQuery:inspect`). - Add typing for `vim._ts_parse_query()`. --- src/nvim/lua/treesitter.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index de17aabca2..c1816a8860 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -56,6 +56,7 @@ typedef struct { # include "lua/treesitter.c.generated.h" #endif +// TSParser static struct luaL_Reg parser_meta[] = { { "__gc", parser_gc }, { "__tostring", parser_tostring }, @@ -70,6 +71,7 @@ static struct luaL_Reg parser_meta[] = { { NULL, NULL } }; +// TSTree static struct luaL_Reg tree_meta[] = { { "__gc", tree_gc }, { "__tostring", tree_tostring }, @@ -80,6 +82,7 @@ static struct luaL_Reg tree_meta[] = { { NULL, NULL } }; +// TSNode static struct luaL_Reg node_meta[] = { { "__tostring", node_tostring }, { "__eq", node_eq }, @@ -119,6 +122,7 @@ static struct luaL_Reg node_meta[] = { { NULL, NULL } }; +// TSQuery static struct luaL_Reg query_meta[] = { { "__gc", query_gc }, { "__tostring", query_tostring }, @@ -1649,8 +1653,10 @@ static int query_inspect(lua_State *L) return 0; } - uint32_t n_pat = ts_query_pattern_count(query); + // TSQueryInfo lua_createtable(L, 0, 2); // [retval] + + uint32_t n_pat = ts_query_pattern_count(query); lua_createtable(L, (int)n_pat, 1); // [retval, patterns] for (size_t i = 0; i < n_pat; i++) { uint32_t len; -- cgit