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

@@ -1,4 +1,5 @@
local api = vim.api
local nvim_on = require('vim._core.util').nvim_on
local severity_module = require('vim.diagnostic._severity')
@@ -14,13 +15,9 @@ setmetatable(diagnostic_cache, {
--- @param bufnr integer
__index = function(t, bufnr)
assert(bufnr > 0, 'Invalid buffer number')
api.nvim_create_autocmd('BufWipeout', {
group = group,
buf = bufnr,
callback = function()
rawset(t, bufnr, nil)
end,
})
nvim_on('BufWipeout', group, { buf = bufnr }, function()
rawset(t, bufnr, nil)
end)
t[bufnr] = {}
return t[bufnr]
end,
@@ -50,13 +47,12 @@ local function once_buf_loaded(bufnr, fn)
if api.nvim_buf_is_loaded(bufnr) then
fn()
else
return api.nvim_create_autocmd('BufRead', {
return nvim_on('BufRead', nil, {
buf = bufnr,
once = true,
callback = function()
fn()
end,
})
}, function()
fn()
end)
end
end