diff options
author | Chris AtLee <chris@atlee.ca> | 2023-08-01 08:13:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-01 05:13:52 -0700 |
commit | e55e80d51ca5d85770981bffb9254badc3662e0c (patch) | |
tree | dddf5a414335cb9e9ddd37c9967bb1aff44d99b2 /runtime/lua/vim/lsp/diagnostic.lua | |
parent | 20bfdbe83253cf5ec0a42bd75bd4ed7df945ab37 (diff) | |
download | rneovim-e55e80d51ca5d85770981bffb9254badc3662e0c.tar.gz rneovim-e55e80d51ca5d85770981bffb9254badc3662e0c.tar.bz2 rneovim-e55e80d51ca5d85770981bffb9254badc3662e0c.zip |
fix(lsp): inlay hints: "Failed to delete autocmd" when closing buffer #24469
Problem:
"Failed to delete autocmd" error when deleting LspNotify autocmd. #24456
Solution:
Change a few things in the inlay_hint and diagnostic LSP code:
1. Re-introduce the `enabled` flag for the buffer state tables. Previously I was
relying on the presence of an autocmd id in the state table to track whether
inlay_hint / diagnostic was enabled for a buffer. There are two reasons why
this doesn't work well:
- Each time inlay_hint / diagnostic is enabled, we call `nvim_buf_attach` on
the buffer, resulting in multiple `on_reload` or `on_detach` callbacks being
registered.
- Commands like `bwipeout` delete buffer local autocmds, sometimes before our
`on_detach` callbacks have a chance to delete them first. This causes the
- Use module local enabled state for diagnostic as well. bwipeout can race
with on_detach callbacks for deleting autocmds. Error referenced in #24456.
2. Change the `LspDetach` autocmd to run each time (i.e., remove the `once`
flag). Since we're only registering autocmds once per buffer now, we
need to make sure that we set the enabled flag properly each time the LSP
client detaches from the buffer.
- Remove `once` from the LspDetach autocmds for inlay_hint and diagnostic.
We only set up the autocmd once now. Gets removed when buffer is deleted.
3. Have the `LspNotify` handler also refresh the inlay_hint / diagnostics when
receiving the `textDocument/didOpen` event. Before this point, the LSP
backend doesn't have the contents of the buffer, so can't provide inlay hints
or diagnostics.
Downsides of this approach:
* When inlay_hint / diagnostics are disabled on a buffer, it will continue to
receive `LspNotify` events for that buffer. The callback exits early since the
`enabled` flag is false.
Alternatives:
* Can we wrap the call to `nvim_del_autocmd` in `pcall` to swallow any errors
resulting from trying to delete the autocmd?
Fixes #24456
Helped-by: Maria José Solano <majosolano99@gmail.com>
Diffstat (limited to 'runtime/lua/vim/lsp/diagnostic.lua')
-rw-r--r-- | runtime/lua/vim/lsp/diagnostic.lua | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index 34be13096d..44bb90d985 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -392,19 +392,18 @@ local function clear(bufnr) end end ---- autocmd ids for LspNotify handlers per buffer ---- @private ---- @type table<integer,integer> -local _autocmd_ids = {} +---@class lsp.diagnostic.bufstate +---@field enabled boolean Whether inlay hints are enabled for this buffer +---@type table<integer, lsp.diagnostic.bufstate> +local bufstates = {} --- Disable pull diagnostics for a buffer --- @private local function disable(bufnr) - if not _autocmd_ids[bufnr] then - return + local bufstate = bufstates[bufnr] + if bufstate then + bufstate.enabled = false end - api.nvim_del_autocmd(_autocmd_ids[bufnr]) - _autocmd_ids[bufnr] = nil clear(bufnr) end @@ -416,38 +415,46 @@ function M._enable(bufnr) bufnr = api.nvim_get_current_buf() end - if _autocmd_ids[bufnr] then - return + if not bufstates[bufnr] then + bufstates[bufnr] = { enabled = true } + + api.nvim_create_autocmd('LspNotify', { + buffer = bufnr, + callback = function(opts) + if + opts.data.method ~= 'textDocument/didChange' + and opts.data.method ~= 'textDocument/didOpen' + then + return + end + if bufstates[bufnr] and bufstates[bufnr].enabled then + util._refresh('textDocument/diagnostic', { bufnr = bufnr, only_visible = true }) + end + end, + group = augroup, + }) + + api.nvim_buf_attach(bufnr, false, { + on_reload = function() + if bufstates[bufnr] and bufstates[bufnr].enabled then + util._refresh('textDocument/diagnostic', { bufnr = bufnr }) + end + end, + on_detach = function() + disable(bufnr) + end, + }) + + api.nvim_create_autocmd('LspDetach', { + buffer = bufnr, + callback = function() + disable(bufnr) + end, + group = augroup, + }) + else + bufstates[bufnr].enabled = true end - - _autocmd_ids[bufnr] = api.nvim_create_autocmd('LspNotify', { - buffer = bufnr, - callback = function(opts) - if opts.data.method ~= 'textDocument/didChange' then - return - end - util._refresh('textDocument/diagnostic', { bufnr = bufnr, only_visible = true }) - end, - group = augroup, - }) - - api.nvim_buf_attach(bufnr, false, { - on_reload = function() - util._refresh('textDocument/diagnostic', { bufnr = bufnr }) - end, - on_detach = function() - disable(bufnr) - end, - }) - - api.nvim_create_autocmd('LspDetach', { - buffer = bufnr, - callback = function() - disable(bufnr) - end, - once = true, - group = augroup, - }) end return M |