refactor(lsp): extract common execute command functionality (#24065)

This commit is contained in:
Mathias Fußenegger
2023-06-20 18:36:18 +02:00
committed by GitHub
parent 19eef8156b
commit 64f2691a98
5 changed files with 49 additions and 38 deletions

View File

@@ -1653,6 +1653,46 @@ function lsp.start_client(config)
return rpc.is_closing() return rpc.is_closing()
end end
---@private
--- Execute a lsp command, either via client command function (if available)
--- or via workspace/executeCommand (if supported by the server)
---
---@param command lsp.Command
---@param context? {bufnr: integer}
---@param handler? lsp-handler only called if a server command
function client._exec_cmd(command, context, handler)
context = vim.deepcopy(context or {})
context.bufnr = context.bufnr or api.nvim_get_current_buf()
context.client_id = client.id
local cmdname = command.command
local fn = client.commands[cmdname] or lsp.commands[cmdname]
if fn then
fn(command, context)
return
end
local command_provider = client.server_capabilities.executeCommandProvider
local commands = type(command_provider) == 'table' and command_provider.commands or {}
if not vim.list_contains(commands, cmdname) then
vim.notify_once(
string.format(
'Language server `%s` does not support command `%s`. This command may require a client extension.',
client.name,
cmdname
),
vim.log.levels.WARN
)
return
end
-- 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,
}
client.request('workspace/executeCommand', params, handler, context.bufnr)
end
---@private ---@private
--- Runs the on_attach function from the client's config if it was defined. --- Runs the on_attach function from the client's config if it was defined.
---@param bufnr integer Buffer number ---@param bufnr integer Buffer number

View File

@@ -646,21 +646,7 @@ local function on_code_action_results(results, ctx, options)
end end
if action.command then if action.command then
local command = type(action.command) == 'table' and action.command or action local command = type(action.command) == 'table' and action.command or action
local fn = client.commands[command.command] or vim.lsp.commands[command.command] client._exec_cmd(command, ctx)
if fn then
local enriched_ctx = vim.deepcopy(ctx)
enriched_ctx.client_id = client.id
fn(command, enriched_ctx)
else
-- 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
end end
@@ -697,7 +683,7 @@ local function on_code_action_results(results, ctx, options)
return return
end end
apply_action(resolved_action, client) apply_action(resolved_action, client)
end) end, ctx.bufnr)
else else
apply_action(action, client) apply_action(action, client)
end end

View File

@@ -33,30 +33,12 @@ 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)
local command = lens.command
local fn = client.commands[command.command] or vim.lsp.commands[command.command] client._exec_cmd(lens.command, { bufnr = bufnr }, function(...)
if fn then
fn(command, { bufnr = bufnr, client_id = client_id })
return
end
-- Need to use the client that returned the lens → must not use buf_request
local command_provider = client.server_capabilities.executeCommandProvider
local commands = type(command_provider) == 'table' and command_provider.commands or {}
if not vim.list_contains(commands, command.command) then
vim.notify(
string.format(
'Language server does not support command `%s`. This command may require a client extension.',
command.command
),
vim.log.levels.WARN
)
return
end
client.request('workspace/executeCommand', command, function(...)
local result = vim.lsp.handlers['workspace/executeCommand'](...) local result = vim.lsp.handlers['workspace/executeCommand'](...)
M.refresh() M.refresh()
return result return result
end, bufnr) end)
end end
--- Return all lenses for the given buffer --- Return all lenses for the given buffer

View File

@@ -1,6 +1,6 @@
---@meta ---@meta
---@alias lsp-handler fun(err: lsp.ResponseError|nil, result: any, context: lsp.HandlerContext, config: table|nil) ---@alias lsp-handler fun(err: lsp.ResponseError|nil, result: any, context: lsp.HandlerContext, config: table|nil): any?
---@class lsp.HandlerContext ---@class lsp.HandlerContext
---@field method string ---@field method string

View File

@@ -788,6 +788,9 @@ function tests.code_action_server_side_command()
codeActionProvider = { codeActionProvider = {
resolveProvider = false, resolveProvider = false,
}, },
executeCommandProvider = {
commands = {"dummy1"}
},
}, },
} }
end, end,