mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(lsp): add function to clear codelens (#21504)
Currently once you retrieve the lenses you're pretty much stuck with them as saving new lenses is additive. Adding a dedicated method to reset lenses allows users to toggle lenses on/off which can be useful for language servers where they are noisy or expensive and you only want to see them temporary.
This commit is contained in:
parent
cce736218f
commit
6ba34e21fe
@ -1278,6 +1278,13 @@ on_publish_diagnostics({_}, {result}, {ctx}, {config})
|
||||
==============================================================================
|
||||
Lua module: vim.lsp.codelens *lsp-codelens*
|
||||
|
||||
clear({client_id}, {bufnr}) *vim.lsp.codelens.clear()*
|
||||
Clear the lenses
|
||||
|
||||
Parameters: ~
|
||||
• {client_id} (number|nil) filter by client_id. All clients if nil
|
||||
• {bufnr} (number|nil) filter by buffer. All buffers if nil
|
||||
|
||||
display({lenses}, {bufnr}, {client_id}) *vim.lsp.codelens.display()*
|
||||
Display the lenses using virtual text
|
||||
|
||||
|
@ -39,6 +39,8 @@ NEW FEATURES *news-features*
|
||||
|
||||
The following new APIs or features were added.
|
||||
|
||||
• Added a |vim.lsp.codelens.clear()| function to clear codelenses.
|
||||
|
||||
• |vim.inspect_pos()|, |vim.show_pos()| and |:Inspect| allow a user to get or show items
|
||||
at a given buffer postion. Currently this includes treesitter captures,
|
||||
semantic tokens, syntax groups and extmarks.
|
||||
|
@ -108,13 +108,36 @@ function M.run()
|
||||
end
|
||||
end
|
||||
|
||||
---@private
|
||||
local function resolve_bufnr(bufnr)
|
||||
return bufnr == 0 and api.nvim_get_current_buf() or bufnr
|
||||
end
|
||||
|
||||
--- Clear the lenses
|
||||
---
|
||||
---@param client_id number|nil filter by client_id. All clients if nil
|
||||
---@param bufnr number|nil filter by buffer. All buffers if nil
|
||||
function M.clear(client_id, bufnr)
|
||||
local buffers = bufnr and { resolve_bufnr(bufnr) } or vim.tbl_keys(lens_cache_by_buf)
|
||||
for _, iter_bufnr in pairs(buffers) do
|
||||
local client_ids = client_id and { client_id } or vim.tbl_keys(namespaces)
|
||||
for _, iter_client_id in pairs(client_ids) do
|
||||
local ns = namespaces[iter_client_id]
|
||||
lens_cache_by_buf[iter_bufnr][iter_client_id] = {}
|
||||
api.nvim_buf_clear_namespace(iter_bufnr, ns, 0, -1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Display the lenses using virtual text
|
||||
---
|
||||
---@param lenses table of lenses to display (`CodeLens[] | null`)
|
||||
---@param bufnr number
|
||||
---@param client_id number
|
||||
function M.display(lenses, bufnr, client_id)
|
||||
local ns = namespaces[client_id]
|
||||
if not lenses or not next(lenses) then
|
||||
api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
|
||||
return
|
||||
end
|
||||
local lenses_by_lnum = {}
|
||||
@ -126,7 +149,6 @@ function M.display(lenses, bufnr, client_id)
|
||||
end
|
||||
table.insert(line_lenses, lens)
|
||||
end
|
||||
local ns = namespaces[client_id]
|
||||
local num_lines = api.nvim_buf_line_count(bufnr)
|
||||
for i = 0, num_lines do
|
||||
local line_lenses = lenses_by_lnum[i] or {}
|
||||
|
@ -58,6 +58,41 @@ describe('vim.lsp.codelens', function()
|
||||
]], bufnr)
|
||||
|
||||
eq({[1] = {'Lens1', 'LspCodeLens'}}, virtual_text_chunks)
|
||||
end)
|
||||
|
||||
it('can clear all lens', function()
|
||||
local fake_uri = "file:///fake/uri"
|
||||
local bufnr = exec_lua([[
|
||||
fake_uri = ...
|
||||
local bufnr = vim.uri_to_bufnr(fake_uri)
|
||||
local lines = {'So', 'many', 'lines'}
|
||||
vim.fn.bufload(bufnr)
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
return bufnr
|
||||
]], fake_uri)
|
||||
|
||||
exec_lua([[
|
||||
local bufnr = ...
|
||||
local lenses = {
|
||||
{
|
||||
range = {
|
||||
start = { line = 0, character = 0, },
|
||||
['end'] = { line = 0, character = 0 }
|
||||
},
|
||||
command = { title = 'Lens1', command = 'Dummy' }
|
||||
},
|
||||
}
|
||||
vim.lsp.codelens.on_codelens(nil, lenses, {method='textDocument/codeLens', client_id=1, bufnr=bufnr})
|
||||
]], bufnr)
|
||||
|
||||
local stored_lenses = exec_lua('return vim.lsp.codelens.get(...)', bufnr)
|
||||
eq(1, #stored_lenses)
|
||||
|
||||
exec_lua([[
|
||||
vim.lsp.codelens.clear()
|
||||
]])
|
||||
|
||||
stored_lenses = exec_lua('return vim.lsp.codelens.get(...)', bufnr)
|
||||
eq(0, #stored_lenses)
|
||||
end)
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user