feat(treesitter): allow disabling captures and patterns on TSQuery (#32790)

Problem: Cannot disable individual captures and patterns in treesitter queries.

Solution: 
* Expose the corresponding tree-sitter API functions for `TSQuery` object. 
* Add documentation for `TSQuery`.
* Return the pattern ID from `get_captures_at_pos()` (and hence `:Inspect!`).
This commit is contained in:
Ian Chamberlain
2025-03-11 09:45:01 -04:00
committed by GitHub
parent 0829e7575d
commit 8b5a0a00c8
10 changed files with 212 additions and 15 deletions

View File

@@ -1491,6 +1491,8 @@ static struct luaL_Reg query_meta[] = {
{ "__gc", query_gc },
{ "__tostring", query_tostring },
{ "inspect", query_inspect },
{ "disable_capture", query_disable_capture },
{ "disable_pattern", query_disable_pattern },
{ NULL, NULL }
};
@@ -1689,6 +1691,23 @@ static int query_inspect(lua_State *L)
return 1;
}
static int query_disable_capture(lua_State *L)
{
TSQuery *query = query_check(L, 1);
size_t name_len;
const char *name = luaL_checklstring(L, 2, &name_len);
ts_query_disable_capture(query, name, (uint32_t)name_len);
return 0;
}
static int query_disable_pattern(lua_State *L)
{
TSQuery *query = query_check(L, 1);
const uint32_t pattern_index = (uint32_t)luaL_checkinteger(L, 2);
ts_query_disable_pattern(query, pattern_index - 1);
return 0;
}
// Library init
static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)