From 8b5a0a00c8cfe776c4227862c3fb32a07d154663 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Tue, 11 Mar 2025 09:45:01 -0400 Subject: 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!`). --- src/gen/gen_vimdoc.lua | 2 ++ src/nvim/lua/treesitter.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/gen/gen_vimdoc.lua b/src/gen/gen_vimdoc.lua index 2fe7224ea5..68912cf0b5 100755 --- a/src/gen/gen_vimdoc.lua +++ b/src/gen/gen_vimdoc.lua @@ -328,10 +328,12 @@ local config = { 'treesitter.lua', 'language.lua', 'query.lua', + 'tsquery.lua', 'highlighter.lua', 'languagetree.lua', 'dev.lua', }, + append_only = { 'tsquery.lua' }, files = { 'runtime/lua/vim/treesitter/_meta/', 'runtime/lua/vim/treesitter.lua', diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index fa5cf1118d..a346bf5963 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -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) -- cgit