mirror of
https://github.com/neovim/neovim
synced 2025-07-16 09:11:51 +00:00
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>
This commit is contained in:
@ -392,19 +392,18 @@ local function clear(bufnr)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- autocmd ids for LspNotify handlers per buffer
|
---@class lsp.diagnostic.bufstate
|
||||||
--- @private
|
---@field enabled boolean Whether inlay hints are enabled for this buffer
|
||||||
--- @type table<integer,integer>
|
---@type table<integer, lsp.diagnostic.bufstate>
|
||||||
local _autocmd_ids = {}
|
local bufstates = {}
|
||||||
|
|
||||||
--- Disable pull diagnostics for a buffer
|
--- Disable pull diagnostics for a buffer
|
||||||
--- @private
|
--- @private
|
||||||
local function disable(bufnr)
|
local function disable(bufnr)
|
||||||
if not _autocmd_ids[bufnr] then
|
local bufstate = bufstates[bufnr]
|
||||||
return
|
if bufstate then
|
||||||
|
bufstate.enabled = false
|
||||||
end
|
end
|
||||||
api.nvim_del_autocmd(_autocmd_ids[bufnr])
|
|
||||||
_autocmd_ids[bufnr] = nil
|
|
||||||
clear(bufnr)
|
clear(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -416,38 +415,46 @@ function M._enable(bufnr)
|
|||||||
bufnr = api.nvim_get_current_buf()
|
bufnr = api.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
|
|
||||||
if _autocmd_ids[bufnr] then
|
if not bufstates[bufnr] then
|
||||||
return
|
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
|
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -3,12 +3,12 @@ local log = require('vim.lsp.log')
|
|||||||
local api = vim.api
|
local api = vim.api
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@class lsp._inlay_hint.bufstate
|
---@class lsp.inlay_hint.bufstate
|
||||||
---@field version integer
|
---@field version integer
|
||||||
---@field client_hint table<integer, table<integer, lsp.InlayHint[]>> client_id -> (lnum -> hints)
|
---@field client_hint table<integer, table<integer, lsp.InlayHint[]>> client_id -> (lnum -> hints)
|
||||||
---@field applied table<integer, integer> Last version of hints applied to this line
|
---@field applied table<integer, integer> Last version of hints applied to this line
|
||||||
---@field autocmd_id integer The autocmd id for the buffer
|
---@field enabled boolean Whether inlay hints are enabled for this buffer
|
||||||
---@type table<integer, lsp._inlay_hint.bufstate>
|
---@type table<integer, lsp.inlay_hint.bufstate>
|
||||||
local bufstates = {}
|
local bufstates = {}
|
||||||
|
|
||||||
local namespace = api.nvim_create_namespace('vim_lsp_inlayhint')
|
local namespace = api.nvim_create_namespace('vim_lsp_inlayhint')
|
||||||
@ -31,6 +31,9 @@ function M.on_inlayhint(err, result, ctx, _)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
|
if not bufstate or not bufstate.enabled then
|
||||||
|
return
|
||||||
|
end
|
||||||
if not (bufstate.client_hint and bufstate.version) then
|
if not (bufstate.client_hint and bufstate.version) then
|
||||||
bufstate.client_hint = vim.defaulttable()
|
bufstate.client_hint = vim.defaulttable()
|
||||||
bufstate.version = ctx.version
|
bufstate.version = ctx.version
|
||||||
@ -122,10 +125,9 @@ local function disable(bufnr)
|
|||||||
bufnr = api.nvim_get_current_buf()
|
bufnr = api.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
clear(bufnr)
|
clear(bufnr)
|
||||||
if bufstates[bufnr] and bufstates[bufnr].autocmd_id then
|
if bufstates[bufnr] then
|
||||||
api.nvim_del_autocmd(bufstates[bufnr].autocmd_id)
|
bufstates[bufnr] = { enabled = false, applied = {} }
|
||||||
end
|
end
|
||||||
bufstates[bufnr] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Enable inlay hints for a buffer
|
--- Enable inlay hints for a buffer
|
||||||
@ -136,24 +138,27 @@ local function enable(bufnr)
|
|||||||
end
|
end
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
if not bufstate then
|
if not bufstate then
|
||||||
bufstates[bufnr] = { applied = {} }
|
bufstates[bufnr] = { applied = {}, enabled = true }
|
||||||
bufstates[bufnr].autocmd_id = api.nvim_create_autocmd('LspNotify', {
|
api.nvim_create_autocmd('LspNotify', {
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
callback = function(opts)
|
callback = function(opts)
|
||||||
if opts.data.method ~= 'textDocument/didChange' then
|
if
|
||||||
|
opts.data.method ~= 'textDocument/didChange'
|
||||||
|
and opts.data.method ~= 'textDocument/didOpen'
|
||||||
|
then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if bufstates[bufnr] then
|
if bufstates[bufnr] and bufstates[bufnr].enabled then
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
group = augroup,
|
group = augroup,
|
||||||
})
|
})
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
||||||
api.nvim_buf_attach(bufnr, true, {
|
api.nvim_buf_attach(bufnr, false, {
|
||||||
on_reload = function(_, cb_bufnr)
|
on_reload = function(_, cb_bufnr)
|
||||||
clear(cb_bufnr)
|
clear(cb_bufnr)
|
||||||
if bufstates[cb_bufnr] then
|
if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then
|
||||||
bufstates[cb_bufnr].applied = {}
|
bufstates[cb_bufnr].applied = {}
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = cb_bufnr })
|
util._refresh('textDocument/inlayHint', { bufnr = cb_bufnr })
|
||||||
end
|
end
|
||||||
@ -167,9 +172,11 @@ local function enable(bufnr)
|
|||||||
callback = function()
|
callback = function()
|
||||||
disable(bufnr)
|
disable(bufnr)
|
||||||
end,
|
end,
|
||||||
once = true,
|
|
||||||
group = augroup,
|
group = augroup,
|
||||||
})
|
})
|
||||||
|
else
|
||||||
|
bufstate.enabled = true
|
||||||
|
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -180,7 +187,7 @@ local function toggle(bufnr)
|
|||||||
bufnr = api.nvim_get_current_buf()
|
bufnr = api.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
if bufstate then
|
if bufstate and bufstate.enabled then
|
||||||
disable(bufnr)
|
disable(bufnr)
|
||||||
else
|
else
|
||||||
enable(bufnr)
|
enable(bufnr)
|
||||||
|
Reference in New Issue
Block a user