mirror of
https://github.com/neovim/neovim.git
synced 2025-12-09 08:02:38 +00:00
feat(lsp): options to filter and auto-apply code actions (#18221)
Implement two new options to vim.lsp.buf.code_action():
- filter (function): predicate taking an Action as input, and returning
a boolean.
- apply (boolean): when set to true, and there is just one remaining
action (after filtering), the action is applied without user query.
These options can, for example, be used to filter out, and automatically
apply, the action indicated by the server to be preferred:
vim.lsp.buf.code_action({
filter = function(action)
return action.isPreferred
end,
apply = true,
})
Fix #17514.
This commit is contained in:
@@ -2665,6 +2665,42 @@ describe('LSP', function()
|
||||
end
|
||||
}
|
||||
end)
|
||||
it('Filters and automatically applies action if requested', function()
|
||||
local client
|
||||
local expected_handlers = {
|
||||
{NIL, {}, {method="shutdown", client_id=1}};
|
||||
{NIL, {}, {method="start", client_id=1}};
|
||||
}
|
||||
test_rpc_server {
|
||||
test_name = 'code_action_filter',
|
||||
on_init = function(client_)
|
||||
client = client_
|
||||
end,
|
||||
on_setup = function()
|
||||
end,
|
||||
on_exit = function(code, signal)
|
||||
eq(0, code, "exit code", fake_lsp_logfile)
|
||||
eq(0, signal, "exit signal", fake_lsp_logfile)
|
||||
end,
|
||||
on_handler = function(err, result, ctx)
|
||||
eq(table.remove(expected_handlers), {err, result, ctx})
|
||||
if ctx.method == 'start' then
|
||||
exec_lua([[
|
||||
vim.lsp.commands['preferred_command'] = function(cmd)
|
||||
vim.lsp.commands['executed_preferred'] = function()
|
||||
end
|
||||
end
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
|
||||
vim.lsp.buf.code_action({ filter = function(a) return a.isPreferred end, apply = true, })
|
||||
]])
|
||||
elseif ctx.method == 'shutdown' then
|
||||
eq('function', exec_lua[[return type(vim.lsp.commands['executed_preferred'])]])
|
||||
client.stop()
|
||||
end
|
||||
end
|
||||
}
|
||||
end)
|
||||
end)
|
||||
describe('vim.lsp.commands', function()
|
||||
it('Accepts only string keys', function()
|
||||
|
||||
Reference in New Issue
Block a user