fix(lsp): reapplying already-applied hints #24114

Problem:
The decoration provider clears the whole buffer then redraws all the hints every
time the window was redrawn. This may lead to an infinite loop.

Solution:
Store the last applied version for a line and only clear and redraw the line if
the buffer version has changed.
This commit is contained in:
Chinmay Dalal 2023-06-23 17:19:54 +05:30 committed by GitHub
parent 3688735c2b
commit 94a904b453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ local M = {}
---@field client_hint table<integer, table<integer, lsp.InlayHint[]>> client_id -> (lnum -> hints)
---@field enabled boolean Whether inlay hints are enabled for the buffer
---@field timer uv.uv_timer_t? Debounce timer associated with the buffer
---@field applied table<integer, integer> Last version of hints applied to this line
---@type table<integer, lsp._inlay_hint.bufstate>
local bufstates = {}
@ -169,7 +170,7 @@ function M.enable(bufnr)
bufnr = resolve_bufnr(bufnr)
local bufstate = bufstates[bufnr]
if not (bufstate and bufstate.enabled) then
bufstates[bufnr] = { enabled = true, timer = nil }
bufstates[bufnr] = { enabled = true, timer = nil, applied = {} }
M.refresh({ bufnr = bufnr })
api.nvim_buf_attach(bufnr, true, {
on_lines = function(_, cb_bufnr)
@ -238,9 +239,10 @@ api.nvim_set_decoration_provider(namespace, {
return
end
local hints_by_client = bufstate.client_hint
api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
for lnum = topline, botline do
if bufstate.applied[lnum] ~= bufstate.version then
api.nvim_buf_clear_namespace(bufnr, namespace, lnum, lnum + 1)
for _, hints_by_lnum in pairs(hints_by_client) do
local line_hints = hints_by_lnum[lnum] or {}
for _, hint in pairs(line_hints) do
@ -268,6 +270,8 @@ api.nvim_set_decoration_provider(namespace, {
})
end
end
bufstate.applied[lnum] = bufstate.version
end
end
end,
})