Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up buffer-local and other dangling callbacks #110

Open
wincent opened this issue Jul 10, 2021 · 0 comments
Open

Clean up buffer-local and other dangling callbacks #110

wincent opened this issue Jul 10, 2021 · 0 comments

Comments

@wincent
Copy link
Owner

wincent commented Jul 10, 2021

Was originally thinking of callbacks created via the map() function, for example:

if rhs_type == 'function' then
key = wincent.util.get_key_for_fn(rhs, wincent.g.map_callbacks)
wincent.g.map_callbacks[key] = rhs
if opts.expr then
rhs = 'v:lua.wincent.g.map_callbacks.' .. key .. '()'
else
rhs = ':lua wincent.g.map_callbacks.' .. key .. '()<CR>'
end
elseif rhs_type ~= 'string' then
error('map(): unsupported rhs type: ' .. rhs_type)
end
local buffer = opts.buffer
opts.buffer = nil
if buffer == true then
vim.api.nvim_buf_set_keymap(0, mode, lhs, rhs, opts)
else
vim.api.nvim_set_keymap(mode, lhs, rhs, opts)
end
return {
dispose = function()
if buffer == true then
vim.api.nvim_buf_del_keymap(0, mode, lhs)
else
vim.api.nvim_del_keymap(mode, lhs)
end
if key ~= nil then
wincent.g.map_callbacks[key] = nil
end
end,
}

But that was removed (migrated to native Neovim API) in fdb2015.

With that API, it was possible for a caller to keep a reference to the returned dispose function and call it in order to clean-up all the book-keeping, including the stored reference to the function, but in practice I didn't actually do that anywhere. It would have been nice if we could have registered an autocmd that would do this clean-up whenever the buffer is destroyed (Vim itself gets rid of the mapping, but we still have that dangling reference to the callback function itself).

Keeping this open though, because there's a similar thing going on with autocmd() callbacks here:

wincent.g.autocommand_callbacks[key] = cmd

If the autocmd is set to run only once with once, Vim will clean up the autocmd itself, but our reference to the callback will linger on, forever.

And here's the last one, where we hang on to augroup() callbacks forever:

wincent.g.augroup_callbacks[name] = callback

@wincent wincent changed the title Clean up buffer-local callbacks Clean up buffer-local and other dangling callbacks Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant