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()`.
This commit is contained in:
Jongwook Choi
2024-01-25 13:27:48 -05:00
committed by Lewis Russell
parent 8b21fe83da
commit 800134ea5e
5 changed files with 83 additions and 32 deletions

View File

@@ -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;