mirror of
https://github.com/neovim/neovim.git
synced 2026-07-14 21:30:34 +00:00
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:
@@ -3,6 +3,7 @@ local ts = vim.treesitter
|
||||
local Range = require('vim.treesitter._range')
|
||||
|
||||
local api = vim.api
|
||||
local nvim_on = require('vim._core.util').nvim_on
|
||||
|
||||
---Treesitter folding is done in two steps:
|
||||
---(1) compute the fold levels with the syntax tree and cache the result (`compute_folds_levels`)
|
||||
@@ -226,14 +227,9 @@ function FoldInfo:foldupdate(bufnr, srow, erow)
|
||||
})) > 0 then
|
||||
return
|
||||
end
|
||||
api.nvim_create_autocmd('InsertLeave', {
|
||||
group = group,
|
||||
buf = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
self:do_foldupdate(bufnr)
|
||||
end,
|
||||
})
|
||||
nvim_on('InsertLeave', group, { buf = bufnr, once = true }, function()
|
||||
self:do_foldupdate(bufnr)
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
@@ -391,13 +387,9 @@ function M.foldexpr(lnum)
|
||||
|
||||
if not foldinfos[bufnr] then
|
||||
foldinfos[bufnr] = FoldInfo.new(bufnr)
|
||||
api.nvim_create_autocmd({ 'BufUnload', 'VimEnter', 'FileType' }, {
|
||||
buf = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
foldinfos[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
nvim_on({ 'BufUnload', 'VimEnter', 'FileType' }, nil, { buf = bufnr, once = true }, function()
|
||||
foldinfos[bufnr] = nil
|
||||
end)
|
||||
|
||||
local parser = foldinfos[bufnr].parser
|
||||
if not parser then
|
||||
@@ -441,28 +433,27 @@ function M.foldexpr(lnum)
|
||||
return foldinfos[bufnr].levels[lnum] or '0'
|
||||
end
|
||||
|
||||
api.nvim_create_autocmd('OptionSet', {
|
||||
nvim_on('OptionSet', group, {
|
||||
pattern = { 'foldminlines', 'foldnestmax' },
|
||||
desc = 'Refresh treesitter folds',
|
||||
callback = function()
|
||||
local buf = api.nvim_get_current_buf()
|
||||
local bufs = vim.v.option_type == 'global' and vim.tbl_keys(foldinfos)
|
||||
or foldinfos[buf] and { buf }
|
||||
or {}
|
||||
for _, bufnr in ipairs(bufs) do
|
||||
local foldinfo = FoldInfo.new(bufnr)
|
||||
foldinfos[bufnr] = foldinfo
|
||||
api.nvim_buf_call(bufnr, function()
|
||||
compute_folds_levels(bufnr, foldinfo, nil, nil, function()
|
||||
-- FileType/BufUnload can clear or replace the fold state while this
|
||||
-- async parse is in flight. Ignore callbacks for stale generations.
|
||||
if foldinfos[bufnr] ~= foldinfo then
|
||||
return
|
||||
end
|
||||
foldinfo:foldupdate(bufnr, 0, api.nvim_buf_line_count(bufnr))
|
||||
end)
|
||||
}, function()
|
||||
local buf = api.nvim_get_current_buf()
|
||||
local bufs = vim.v.option_type == 'global' and vim.tbl_keys(foldinfos)
|
||||
or foldinfos[buf] and { buf }
|
||||
or {}
|
||||
for _, bufnr in ipairs(bufs) do
|
||||
local foldinfo = FoldInfo.new(bufnr)
|
||||
foldinfos[bufnr] = foldinfo
|
||||
api.nvim_buf_call(bufnr, function()
|
||||
compute_folds_levels(bufnr, foldinfo, nil, nil, function()
|
||||
-- FileType/BufUnload can clear or replace the fold state while this
|
||||
-- async parse is in flight. Ignore callbacks for stale generations.
|
||||
if foldinfos[bufnr] ~= foldinfo then
|
||||
return
|
||||
end
|
||||
foldinfo:foldupdate(bufnr, 0, api.nvim_buf_line_count(bufnr))
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end)
|
||||
end
|
||||
end)
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user