refactor: introduce nvim_on internally #39883

Problem:
`nvim_create_autocmd` is too verbose and its `callback` requires extra
"nesting".

Solution:
Introduce `nvim_on`. Start using it internally. Then we can get a feel
for how it should look before making it public.
This commit is contained in:
Justin M. Keyes
2026-05-20 17:33:01 -04:00
committed by GitHub
parent 799cbfff85
commit 9aa4608401
32 changed files with 842 additions and 1040 deletions

View File

@@ -2,6 +2,7 @@
--- text. See |vim.treesitter.query.parse()| for a working example.
local api = vim.api
local nvim_on = require('vim._core.util').nvim_on
local language = require('vim.treesitter.language')
local memoize = vim.func._memoize
local cmp_ge = require('vim.treesitter._range').cmp_pos.ge
@@ -333,14 +334,17 @@ M.get = memoize('concat-2', function(lang, query_name)
return M.parse(lang, query_string)
end, false)
api.nvim_create_autocmd('OptionSet', {
pattern = { 'runtimepath' },
group = api.nvim_create_augroup('nvim.treesitter.query_cache_reset', { clear = true }),
callback = function()
nvim_on(
'OptionSet',
api.nvim_create_augroup('nvim.treesitter.query_cache_reset', { clear = true }),
{
pattern = { 'runtimepath' },
},
function()
--- @diagnostic disable-next-line: undefined-field LuaLS bad at generics
M.get:clear()
end,
})
end
)
--- Parses a {query} string and returns a `Query` object (|lua-treesitter-query|), which can be used
--- to search the tree for the query patterns (via |Query:iter_captures()|, |Query:iter_matches()|),