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,5 +1,6 @@
local api = vim.api
local fn = vim.fn
local nvim_on = require('vim._core.util').nvim_on
local M = {}
@@ -42,41 +43,21 @@ function M.enable()
local group = api.nvim_create_augroup('matchparen', { clear = true })
-- Replace all matchparen autocommands
api.nvim_create_autocmd({
'CursorMoved',
'CursorMovedI',
'WinEnter',
'WinScrolled',
'TextChanged',
'TextChangedI',
}, {
group = group,
callback = function()
nvim_on(
{ 'CursorMoved', 'CursorMovedI', 'WinEnter', 'WinScrolled', 'TextChanged', 'TextChangedI' },
group,
function()
M.highlight_matching_pair()
end,
})
api.nvim_create_autocmd('BufWinEnter', {
group = group,
callback = function()
api.nvim_create_autocmd('SafeState', {
group = group,
once = true,
callback = function()
M.highlight_matching_pair()
end,
})
end,
})
api.nvim_create_autocmd({
'WinLeave',
'BufLeave',
'TextChangedP',
}, {
group = group,
callback = function()
M.remove_matches()
end,
})
end
)
nvim_on('BufWinEnter', group, function()
nvim_on('SafeState', group, { once = true }, function()
M.highlight_matching_pair()
end)
end)
nvim_on({ 'WinLeave', 'BufLeave', 'TextChangedP' }, group, function()
M.remove_matches()
end)
-- Define commands that will disable and enable the plugin.
api.nvim_create_user_command('DoMatchParen', function()