fix(lsp): fix multi client handling in code action (#18869)

Fixes https://github.com/neovim/neovim/issues/18860
This commit is contained in:
Mathias Fußenegger 2022-06-05 16:43:32 +02:00 committed by GitHub
parent 214f866fe5
commit e4df1c9b9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 1 deletions

View File

@ -752,7 +752,14 @@ local function on_code_action_results(results, ctx, options)
enriched_ctx.client_id = client.id
fn(command, enriched_ctx)
else
M.execute_command(command)
-- Not using command directly to exclude extra properties,
-- see https://github.com/python-lsp/python-lsp-server/issues/146
local params = {
command = command.command,
arguments = command.arguments,
workDoneToken = command.workDoneToken,
}
client.request('workspace/executeCommand', params, nil, ctx.bufnr)
end
end
end

View File

@ -757,6 +757,35 @@ function tests.code_action_with_resolve()
}
end
function tests.code_action_server_side_command()
skeleton({
on_init = function()
return {
capabilities = {
codeActionProvider = {
resolveProvider = false,
},
},
}
end,
body = function()
notify('start')
local cmd = {
title = 'Command 1',
command = 'dummy1',
}
expect_request('textDocument/codeAction', function()
return nil, { cmd }
end)
expect_request('workspace/executeCommand', function()
return nil, cmd
end)
notify('shutdown')
end,
})
end
function tests.code_action_filter()
skeleton {
on_init = function()

View File

@ -2793,6 +2793,45 @@ describe('LSP', function()
end
}
end)
it('Calls workspace/executeCommand if no client side command', function()
local client
local expected_handlers = {
{ NIL, {}, { method = 'shutdown', client_id = 1 } },
{
NIL,
{ command = 'dummy1', title = 'Command 1' },
{ bufnr = 1, method = 'workspace/executeCommand', client_id = 1 },
},
{ NIL, {}, { method = 'start', client_id = 1 } },
}
test_rpc_server({
test_name = 'code_action_server_side_command',
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)
ctx.params = nil -- don't compare in assert
eq(table.remove(expected_handlers), { err, result, ctx })
if ctx.method == 'start' then
exec_lua([[
local bufnr = vim.api.nvim_get_current_buf()
vim.lsp.buf_attach_client(bufnr, TEST_RPC_CLIENT_ID)
vim.fn.inputlist = function()
return 1
end
vim.lsp.buf.code_action()
]])
elseif ctx.method == 'shutdown' then
client.stop()
end
end,
})
end)
it('Filters and automatically applies action if requested', function()
local client
local expected_handlers = {