feat(lsp): add opts paramater to vim.lsp.codelens.refresh

This commit is contained in:
Maria José Solano 2024-01-28 22:21:37 -08:00
parent 32b49448b2
commit 0fe86f7e24
3 changed files with 36 additions and 13 deletions

View File

@ -1428,7 +1428,8 @@ clear({client_id}, {bufnr}) *vim.lsp.codelens.clear()*
Parameters: ~ Parameters: ~
• {client_id} (`integer?`) filter by client_id. All clients if nil • {client_id} (`integer?`) filter by client_id. All clients if nil
• {bufnr} (`integer?`) filter by buffer. All buffers if nil • {bufnr} (`integer?`) filter by buffer. All buffers if nil, 0 for
current buffer
display({lenses}, {bufnr}, {client_id}) *vim.lsp.codelens.display()* display({lenses}, {bufnr}, {client_id}) *vim.lsp.codelens.display()*
Display the lenses using virtual text Display the lenses using virtual text
@ -1455,15 +1456,21 @@ on_codelens({err}, {result}, {ctx}, {_})
Parameters: ~ Parameters: ~
• {ctx} (`lsp.HandlerContext`) • {ctx} (`lsp.HandlerContext`)
refresh() *vim.lsp.codelens.refresh()* refresh({opts}) *vim.lsp.codelens.refresh()*
Refresh the codelens for the current buffer Refresh the lenses.
It is recommended to trigger this using an autocmd or via keymap. It is recommended to trigger this using an autocmd or via keymap.
Example: >vim Example: >vim
autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh() autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh({ bufnr = 0 })
< <
Parameters: ~
• {opts} (`vim.lsp.codelens.RefreshOptions?`) Table with the following
fields:
• `bufnr` (integer|nil): filter by buffer. All buffers if nil,
0 for current buffer
run() *vim.lsp.codelens.run()* run() *vim.lsp.codelens.run()*
Run the code lens in the current line Run the code lens in the current line

View File

@ -116,6 +116,10 @@ The following changes may require adaptations in user config or plugins.
upstream tree-sitter and Helix to make it easier to share queries. The full upstream tree-sitter and Helix to make it easier to share queries. The full
list is documented in |treesitter-highlight-groups|. list is documented in |treesitter-highlight-groups|.
• |vim.lsp.codelens.refresh()| now takes an `opts` argument. With this change,
the default behavior of just refreshing the current buffer has been replaced by
refreshing all buffers.
============================================================================== ==============================================================================
BREAKING CHANGES IN HEAD *news-breaking-dev* BREAKING CHANGES IN HEAD *news-breaking-dev*

View File

@ -48,6 +48,7 @@ local function execute_lens(lens, bufnr, client_id)
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
assert(client, 'Client is required to execute lens, client_id=' .. client_id) assert(client, 'Client is required to execute lens, client_id=' .. client_id)
---@diagnostic disable-next-line: invisible
client:_exec_cmd(lens.command, { bufnr = bufnr }, function(...) client:_exec_cmd(lens.command, { bufnr = bufnr }, function(...)
vim.lsp.handlers[ms.workspace_executeCommand](...) vim.lsp.handlers[ms.workspace_executeCommand](...)
M.refresh() M.refresh()
@ -111,7 +112,7 @@ end
--- Clear the lenses --- Clear the lenses
--- ---
---@param client_id integer|nil filter by client_id. All clients if nil ---@param client_id integer|nil filter by client_id. All clients if nil
---@param bufnr integer|nil filter by buffer. All buffers if nil ---@param bufnr integer|nil filter by buffer. All buffers if nil, 0 for current buffer
function M.clear(client_id, bufnr) function M.clear(client_id, bufnr)
bufnr = bufnr and resolve_bufnr(bufnr) bufnr = bufnr and resolve_bufnr(bufnr)
local buffers = bufnr and { bufnr } local buffers = bufnr and { bufnr }
@ -277,25 +278,36 @@ function M.on_codelens(err, result, ctx, _)
end) end)
end end
--- Refresh the codelens for the current buffer --- @class vim.lsp.codelens.RefreshOptions
--- @field bufnr integer? filter by buffer. All buffers if nil, 0 for current buffer
--- Refresh the lenses.
--- ---
--- It is recommended to trigger this using an autocmd or via keymap. --- It is recommended to trigger this using an autocmd or via keymap.
--- ---
--- Example: --- Example:
--- ---
--- ```vim --- ```vim
--- autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh() --- autocmd BufEnter,CursorHold,InsertLeave <buffer> lua vim.lsp.codelens.refresh({ bufnr = 0 })
--- ``` --- ```
function M.refresh() ---
--- @param opts? vim.lsp.codelens.RefreshOptions Table with the following fields:
--- - `bufnr` (integer|nil): filter by buffer. All buffers if nil, 0 for current buffer
function M.refresh(opts)
opts = opts or {}
local bufnr = opts.bufnr and resolve_bufnr(opts.bufnr)
local buffers = bufnr and { bufnr }
or vim.tbl_filter(api.nvim_buf_is_loaded, api.nvim_list_bufs())
local params = { local params = {
textDocument = util.make_text_document_params(), textDocument = util.make_text_document_params(),
} }
local bufnr = api.nvim_get_current_buf()
if active_refreshes[bufnr] then for _, buf in ipairs(buffers) do
return if not active_refreshes[buf] then
active_refreshes[buf] = true
vim.lsp.buf_request(buf, ms.textDocument_codeLens, params, M.on_codelens)
end
end end
active_refreshes[bufnr] = true
vim.lsp.buf_request(0, ms.textDocument_codeLens, params, M.on_codelens)
end end
return M return M