diff options
author | Mathias Fußenegger <mfussenegger@users.noreply.github.com> | 2023-06-30 11:33:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-30 11:33:28 +0200 |
commit | 37079fca58f396fd866dc7b7d87a0100c17ee760 (patch) | |
tree | 876851052cf0eecbb5e05c4d7ad2abd5ee887e57 /runtime/lua/vim | |
parent | d55d7646c129a9afe1da3a61813bb365d178c421 (diff) | |
download | rneovim-37079fca58f396fd866dc7b7d87a0100c17ee760.tar.gz rneovim-37079fca58f396fd866dc7b7d87a0100c17ee760.tar.bz2 rneovim-37079fca58f396fd866dc7b7d87a0100c17ee760.zip |
feat(lsp): move inlay_hint() to vim.lsp (#24130)
Allows to keep more functions hidden and gives a path forward for
further inlay_hint related functions - like applying textEdits.
See https://github.com/neovim/neovim/pull/23984#pullrequestreview-1486624668
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r-- | runtime/lua/vim/lsp.lua | 7 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 15 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/handlers.lua | 20 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/inlay_hint.lua (renamed from runtime/lua/vim/lsp/_inlay_hint.lua) | 45 |
4 files changed, 47 insertions, 40 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 1e5ce8fa10..ca4851f8d7 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -2474,6 +2474,13 @@ function lsp.with(handler, override_config) end end +--- Enable/disable/toggle inlay hints for a buffer +---@param bufnr (integer) Buffer handle, or 0 for current +---@param enable (boolean|nil) true/false to enable/disable, nil to toggle +function lsp.inlay_hint(bufnr, enable) + return require('vim.lsp.inlay_hint')(bufnr, enable) +end + --- Helper function to use when implementing a handler. --- This will check that all of the keys in the user configuration --- are valid keys and make sense to include for this handler. diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index c2e0179cc4..0369725216 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -796,19 +796,4 @@ function M.execute_command(command_params) request('workspace/executeCommand', command_params) end ---- Enable/disable/toggle inlay hints for a buffer ----@param bufnr (integer) Buffer handle, or 0 for current ----@param enable (boolean|nil) true/false to enable/disable, nil to toggle -function M.inlay_hint(bufnr, enable) - vim.validate({ enable = { enable, { 'boolean', 'nil' } }, bufnr = { bufnr, 'number' } }) - local inlay_hint = require('vim.lsp._inlay_hint') - if enable then - inlay_hint.enable(bufnr) - elseif enable == false then - inlay_hint.disable(bufnr) - else - inlay_hint.toggle(bufnr) - end -end - return M diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index 284e3ef2d0..625a2ed282 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -220,7 +220,7 @@ M['textDocument/codeLens'] = function(...) end M['textDocument/inlayHint'] = function(...) - return require('vim.lsp._inlay_hint').on_inlayhint(...) + return require('vim.lsp.inlay_hint').on_inlayhint(...) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references @@ -617,22 +617,8 @@ M['window/showDocument'] = function(_, result, ctx, _) end ---@see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh -M['workspace/inlayHint/refresh'] = function(err, _, ctx) - local inlay_hint = require('vim.lsp._inlay_hint') - if err then - return vim.NIL - end - - for _, bufnr in ipairs(vim.lsp.get_buffers_by_client_id(ctx.client_id)) do - for _, winid in ipairs(api.nvim_list_wins()) do - if api.nvim_win_get_buf(winid) == bufnr then - inlay_hint.refresh({ bufnr = bufnr }) - break - end - end - end - - return vim.NIL +M['workspace/inlayHint/refresh'] = function(err, result, ctx, config) + return require('vim.lsp.inlay_hint').on_refresh(err, result, ctx, config) end -- Add boilerplate error validation and logging for all of these. diff --git a/runtime/lua/vim/lsp/_inlay_hint.lua b/runtime/lua/vim/lsp/inlay_hint.lua index ccf1b5cca4..1af18bbc61 100644 --- a/runtime/lua/vim/lsp/_inlay_hint.lua +++ b/runtime/lua/vim/lsp/inlay_hint.lua @@ -101,7 +101,7 @@ end --- - only_visible (boolean, default: false): Whether to only refresh hints for the visible regions of the buffer --- ---@private -function M.refresh(opts) +local function refresh(opts) opts = opts or {} local bufnr = resolve_bufnr(opts.bufnr or 0) local bufstate = bufstates[bufnr] @@ -139,6 +139,24 @@ function M.refresh(opts) end end +--- |lsp-handler| for the method `textDocument/inlayHint/refresh` +---@private +function M.on_refresh(err, _, ctx, _) + if err then + return vim.NIL + end + for _, bufnr in ipairs(vim.lsp.get_buffers_by_client_id(ctx.client_id)) do + for _, winid in ipairs(api.nvim_list_wins()) do + if api.nvim_win_get_buf(winid) == bufnr then + refresh({ bufnr = bufnr }) + break + end + end + end + + return vim.NIL +end + --- Clear inlay hints ---@param bufnr (integer) Buffer handle, or 0 for current ---@private @@ -163,18 +181,18 @@ end ---@private local function make_request(request_bufnr) reset_timer(request_bufnr) - M.refresh({ bufnr = request_bufnr }) + refresh({ bufnr = request_bufnr }) end --- Enable inlay hints for a buffer ---@param bufnr (integer) Buffer handle, or 0 for current ---@private -function M.enable(bufnr) +local function enable(bufnr) bufnr = resolve_bufnr(bufnr) local bufstate = bufstates[bufnr] if not (bufstate and bufstate.enabled) then bufstates[bufnr] = { enabled = true, timer = nil, applied = {} } - M.refresh({ bufnr = bufnr }) + refresh({ bufnr = bufnr }) api.nvim_buf_attach(bufnr, true, { on_lines = function(_, cb_bufnr) if not bufstates[cb_bufnr].enabled then @@ -190,7 +208,7 @@ function M.enable(bufnr) if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then bufstates[cb_bufnr] = { enabled = true, applied = {} } end - M.refresh({ bufnr = cb_bufnr }) + refresh({ bufnr = cb_bufnr }) end, on_detach = function(_, cb_bufnr) clear(cb_bufnr) @@ -211,7 +229,7 @@ end --- Disable inlay hints for a buffer ---@param bufnr (integer) Buffer handle, or 0 for current ---@private -function M.disable(bufnr) +local function disable(bufnr) bufnr = resolve_bufnr(bufnr) if bufstates[bufnr] and bufstates[bufnr].enabled then clear(bufnr) @@ -223,7 +241,7 @@ end --- Toggle inlay hints for a buffer ---@param bufnr (integer) Buffer handle, or 0 for current ---@private -function M.toggle(bufnr) +local function toggle(bufnr) bufnr = resolve_bufnr(bufnr) local bufstate = bufstates[bufnr] if bufstate and bufstate.enabled then @@ -281,4 +299,15 @@ api.nvim_set_decoration_provider(namespace, { end, }) -return M +return setmetatable(M, { + __call = function(_, bufnr, enable_) + vim.validate({ enable = { enable_, { 'boolean', 'nil' } }, bufnr = { bufnr, 'number' } }) + if enable_ then + enable(bufnr) + elseif enable_ == false then + disable(bufnr) + else + toggle(bufnr) + end + end, +}) |