aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/rpc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/lsp/rpc.lua')
-rw-r--r--runtime/lua/vim/lsp/rpc.lua103
1 files changed, 44 insertions, 59 deletions
diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua
index e79dbd2db3..6c8564845f 100644
--- a/runtime/lua/vim/lsp/rpc.lua
+++ b/runtime/lua/vim/lsp/rpc.lua
@@ -1,7 +1,7 @@
local uv = vim.uv
local log = require('vim.lsp.log')
local protocol = require('vim.lsp.protocol')
-local validate, schedule, schedule_wrap = vim.validate, vim.schedule, vim.schedule_wrap
+local validate, schedule_wrap = vim.validate, vim.schedule_wrap
local is_win = vim.fn.has('win32') == 1
@@ -152,9 +152,7 @@ end
---@param err table The error object
---@return string error_message The formatted error message
function M.format_rpc_error(err)
- validate({
- err = { err, 't' },
- })
+ validate('err', err, 'table')
-- There is ErrorCodes in the LSP specification,
-- but in ResponseError.code it is not used and the actual type is number.
@@ -329,10 +327,8 @@ end
---@return boolean success `true` if request could be sent, `false` if not
---@return integer? message_id if request could be sent, `nil` if not
function Client:request(method, params, callback, notify_reply_callback)
- validate({
- callback = { callback, 'f' },
- notify_reply_callback = { notify_reply_callback, 'f', true },
- })
+ validate('callback', callback, 'function')
+ validate('notify_reply_callback', notify_reply_callback, 'function', true)
self.message_index = self.message_index + 1
local message_id = self.message_index
local result = self:encode_and_send({
@@ -413,49 +409,44 @@ function Client:handle_body(body)
local err --- @type lsp.ResponseError|nil
-- Schedule here so that the users functions don't trigger an error and
-- we can still use the result.
- schedule(function()
- coroutine.wrap(function()
- local status, result
- status, result, err = self:try_call(
- M.client_errors.SERVER_REQUEST_HANDLER_ERROR,
- self.dispatchers.server_request,
- decoded.method,
- decoded.params
- )
- log.debug(
- 'server_request: callback result',
- { status = status, result = result, err = err }
- )
- if status then
- if result == nil and err == nil then
- error(
- string.format(
- 'method %q: either a result or an error must be sent to the server in response',
- decoded.method
- )
- )
- end
- if err then
- ---@cast err lsp.ResponseError
- assert(
- type(err) == 'table',
- 'err must be a table. Use rpc_response_error to help format errors.'
- )
- ---@type string
- local code_name = assert(
- protocol.ErrorCodes[err.code],
- 'Errors must use protocol.ErrorCodes. Use rpc_response_error to help format errors.'
+ vim.schedule(coroutine.wrap(function()
+ local status, result
+ status, result, err = self:try_call(
+ M.client_errors.SERVER_REQUEST_HANDLER_ERROR,
+ self.dispatchers.server_request,
+ decoded.method,
+ decoded.params
+ )
+ log.debug('server_request: callback result', { status = status, result = result, err = err })
+ if status then
+ if result == nil and err == nil then
+ error(
+ string.format(
+ 'method %q: either a result or an error must be sent to the server in response',
+ decoded.method
)
- err.message = err.message or code_name
- end
- else
- -- On an exception, result will contain the error message.
- err = M.rpc_response_error(protocol.ErrorCodes.InternalError, result)
- result = nil
+ )
+ end
+ if err then
+ ---@cast err lsp.ResponseError
+ assert(
+ type(err) == 'table',
+ 'err must be a table. Use rpc_response_error to help format errors.'
+ )
+ ---@type string
+ local code_name = assert(
+ protocol.ErrorCodes[err.code],
+ 'Errors must use protocol.ErrorCodes. Use rpc_response_error to help format errors.'
+ )
+ err.message = err.message or code_name
end
- self:send_response(decoded.id, err, result)
- end)()
- end)
+ else
+ -- On an exception, result will contain the error message.
+ err = M.rpc_response_error(protocol.ErrorCodes.InternalError, result)
+ result = nil
+ end
+ self:send_response(decoded.id, err, result)
+ end))
-- This works because we are expecting vim.NIL here
elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then
-- We sent a number, so we expect a number.
@@ -465,9 +456,7 @@ function Client:handle_body(body)
local notify_reply_callbacks = self.notify_reply_callbacks
local notify_reply_callback = notify_reply_callbacks and notify_reply_callbacks[result_id]
if notify_reply_callback then
- validate({
- notify_reply_callback = { notify_reply_callback, 'f' },
- })
+ validate('notify_reply_callback', notify_reply_callback, 'function')
notify_reply_callback(result_id)
notify_reply_callbacks[result_id] = nil
end
@@ -498,9 +487,7 @@ function Client:handle_body(body)
local callback = message_callbacks and message_callbacks[result_id]
if callback then
message_callbacks[result_id] = nil
- validate({
- callback = { callback, 'f' },
- })
+ validate('callback', callback, 'function')
if decoded.error then
decoded.error = setmetatable(decoded.error, {
__tostring = M.format_rpc_error,
@@ -734,10 +721,8 @@ end
function M.start(cmd, dispatchers, extra_spawn_params)
log.info('Starting RPC client', { cmd = cmd, extra = extra_spawn_params })
- validate({
- cmd = { cmd, 't' },
- dispatchers = { dispatchers, 't', true },
- })
+ validate('cmd', cmd, 'table')
+ validate('dispatchers', dispatchers, 'table', true)
extra_spawn_params = extra_spawn_params or {}