mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
refactor(lsp): use LspNotify for inlay_hint (#24411)
This commit is contained in:
parent
24e3ee9d07
commit
4b57ff77fe
@ -1572,9 +1572,9 @@ function lsp.start_client(config)
|
|||||||
changetracking.flush(client)
|
changetracking.flush(client)
|
||||||
end
|
end
|
||||||
|
|
||||||
local result = rpc.notify(method, params)
|
local client_active = rpc.notify(method, params)
|
||||||
|
|
||||||
if result then
|
if client_active then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
nvim_exec_autocmds('LspNotify', {
|
nvim_exec_autocmds('LspNotify', {
|
||||||
modeline = false,
|
modeline = false,
|
||||||
@ -1587,7 +1587,7 @@ function lsp.start_client(config)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return result
|
return client_active
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
|
@ -6,28 +6,14 @@ 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 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
|
---@field applied table<integer, integer> Last version of hints applied to this line
|
||||||
|
---@field autocmd_id integer The autocmd id for the 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')
|
||||||
local augroup = api.nvim_create_augroup('vim_lsp_inlayhint', {})
|
local augroup = api.nvim_create_augroup('vim_lsp_inlayhint', {})
|
||||||
|
|
||||||
--- Reset the request debounce timer of a buffer
|
|
||||||
local function reset_timer(reset_bufnr)
|
|
||||||
local timer = bufstates[reset_bufnr].timer
|
|
||||||
if timer then
|
|
||||||
bufstates[reset_bufnr].timer = nil
|
|
||||||
if not timer:is_closing() then
|
|
||||||
timer:stop()
|
|
||||||
timer:close()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- |lsp-handler| for the method `textDocument/inlayHint`
|
--- |lsp-handler| for the method `textDocument/inlayHint`
|
||||||
--- Store hints for a specific buffer and client
|
--- Store hints for a specific buffer and client
|
||||||
---@private
|
---@private
|
||||||
@ -97,7 +83,7 @@ function M.on_refresh(err, _, ctx, _)
|
|||||||
for _, winid in ipairs(api.nvim_list_wins()) do
|
for _, winid in ipairs(api.nvim_list_wins()) do
|
||||||
if api.nvim_win_get_buf(winid) == bufnr then
|
if api.nvim_win_get_buf(winid) == bufnr then
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
if bufstate and bufstate.enabled then
|
if bufstate then
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -117,7 +103,6 @@ local function clear(bufnr)
|
|||||||
if not bufstates[bufnr] then
|
if not bufstates[bufnr] then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
reset_timer(bufnr)
|
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
local client_lens = (bufstate or {}).client_hint or {}
|
local client_lens = (bufstate or {}).client_hint or {}
|
||||||
local client_ids = vim.tbl_keys(client_lens)
|
local client_ids = vim.tbl_keys(client_lens)
|
||||||
@ -130,9 +115,17 @@ local function clear(bufnr)
|
|||||||
api.nvim__buf_redraw_range(bufnr, 0, -1)
|
api.nvim__buf_redraw_range(bufnr, 0, -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function make_request(request_bufnr)
|
--- Disable inlay hints for a buffer
|
||||||
reset_timer(request_bufnr)
|
---@param bufnr (integer) Buffer handle, or 0 for current
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = request_bufnr })
|
local function disable(bufnr)
|
||||||
|
if bufnr == nil or bufnr == 0 then
|
||||||
|
bufnr = api.nvim_get_current_buf()
|
||||||
|
end
|
||||||
|
clear(bufnr)
|
||||||
|
if bufstates[bufnr] and bufstates[bufnr].autocmd_id then
|
||||||
|
api.nvim_del_autocmd(bufstates[bufnr].autocmd_id)
|
||||||
|
end
|
||||||
|
bufstates[bufnr] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Enable inlay hints for a buffer
|
--- Enable inlay hints for a buffer
|
||||||
@ -142,35 +135,37 @@ local function enable(bufnr)
|
|||||||
bufnr = api.nvim_get_current_buf()
|
bufnr = api.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
local bufstate = bufstates[bufnr]
|
local bufstate = bufstates[bufnr]
|
||||||
if not (bufstate and bufstate.enabled) then
|
if not bufstate then
|
||||||
bufstates[bufnr] = { enabled = true, timer = nil, applied = {} }
|
bufstates[bufnr] = { applied = {} }
|
||||||
|
bufstates[bufnr].autocmd_id = api.nvim_create_autocmd('LspNotify', {
|
||||||
|
buffer = bufnr,
|
||||||
|
callback = function(opts)
|
||||||
|
if opts.data.method ~= 'textDocument/didChange' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if bufstates[bufnr] then
|
||||||
|
util._refresh('textDocument/inlayHint', { bufnr = bufnr })
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
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, true, {
|
||||||
on_lines = function(_, cb_bufnr)
|
|
||||||
if not bufstates[cb_bufnr].enabled then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
reset_timer(cb_bufnr)
|
|
||||||
bufstates[cb_bufnr].timer = vim.defer_fn(function()
|
|
||||||
make_request(cb_bufnr)
|
|
||||||
end, 200)
|
|
||||||
end,
|
|
||||||
on_reload = function(_, cb_bufnr)
|
on_reload = function(_, cb_bufnr)
|
||||||
clear(cb_bufnr)
|
clear(cb_bufnr)
|
||||||
if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then
|
if bufstates[cb_bufnr] then
|
||||||
bufstates[cb_bufnr] = { enabled = true, applied = {} }
|
bufstates[cb_bufnr].applied = {}
|
||||||
end
|
|
||||||
util._refresh('textDocument/inlayHint', { bufnr = cb_bufnr })
|
util._refresh('textDocument/inlayHint', { bufnr = cb_bufnr })
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
on_detach = function(_, cb_bufnr)
|
on_detach = function(_, cb_bufnr)
|
||||||
clear(cb_bufnr)
|
disable(cb_bufnr)
|
||||||
bufstates[cb_bufnr] = nil
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
api.nvim_create_autocmd('LspDetach', {
|
api.nvim_create_autocmd('LspDetach', {
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
callback = function(opts)
|
callback = function()
|
||||||
clear(opts.buf)
|
disable(bufnr)
|
||||||
end,
|
end,
|
||||||
once = true,
|
once = true,
|
||||||
group = augroup,
|
group = augroup,
|
||||||
@ -178,19 +173,6 @@ local function enable(bufnr)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Disable inlay hints for a buffer
|
|
||||||
---@param bufnr (integer) Buffer handle, or 0 for current
|
|
||||||
local function disable(bufnr)
|
|
||||||
if bufnr == nil or bufnr == 0 then
|
|
||||||
bufnr = api.nvim_get_current_buf()
|
|
||||||
end
|
|
||||||
if bufstates[bufnr] and bufstates[bufnr].enabled then
|
|
||||||
clear(bufnr)
|
|
||||||
bufstates[bufnr].enabled = nil
|
|
||||||
bufstates[bufnr].timer = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Toggle inlay hints for a buffer
|
--- Toggle inlay hints for a buffer
|
||||||
---@param bufnr (integer) Buffer handle, or 0 for current
|
---@param bufnr (integer) Buffer handle, or 0 for current
|
||||||
local function toggle(bufnr)
|
local function toggle(bufnr)
|
||||||
@ -198,7 +180,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 and bufstate.enabled then
|
if bufstate then
|
||||||
disable(bufnr)
|
disable(bufnr)
|
||||||
else
|
else
|
||||||
enable(bufnr)
|
enable(bufnr)
|
||||||
|
Loading…
Reference in New Issue
Block a user