fix(lsp): fix some type annotations in lsp.rpc (#19714)

This commit is contained in:
Mathias Fußenegger
2022-08-11 15:17:05 +02:00
committed by GitHub
parent 6669fc94ae
commit 8b67f37798
2 changed files with 17 additions and 18 deletions

View File

@@ -1947,9 +1947,10 @@ rpc_response_error({code}, {message}, {data})
Creates an RPC response object/table. Creates an RPC response object/table.
Parameters: ~ Parameters: ~
{code} RPC error code defined in `vim.lsp.protocol.ErrorCodes` {code} (number) RPC error code defined in
{message} (optional) arbitrary message to send to server `vim.lsp.protocol.ErrorCodes`
{data} (optional) arbitrary data to send to server {message} (string|nil) arbitrary message to send to server
{data} any|nil arbitrary data to send to server
*vim.lsp.rpc.start()* *vim.lsp.rpc.start()*
start({cmd}, {cmd_args}, {dispatchers}, {extra_spawn_params}) start({cmd}, {cmd_args}, {dispatchers}, {extra_spawn_params})
@@ -1961,13 +1962,13 @@ start({cmd}, {cmd_args}, {dispatchers}, {extra_spawn_params})
{cmd} (string) Command to start the LSP server. {cmd} (string) Command to start the LSP server.
{cmd_args} (table) List of additional string arguments to {cmd_args} (table) List of additional string arguments to
pass to {cmd}. pass to {cmd}.
{dispatchers} (table, optional) Dispatchers for LSP message {dispatchers} (table|nil) Dispatchers for LSP message types.
types. Valid dispatcher names are: Valid dispatcher names are:
• `"notification"` • `"notification"`
• `"server_request"` • `"server_request"`
• `"on_error"` • `"on_error"`
• `"on_exit"` • `"on_exit"`
{extra_spawn_params} (table, optional) Additional context for the LSP {extra_spawn_params} (table|nil) Additional context for the LSP
server process. May contain: server process. May contain:
• {cwd} (string) Working directory for the LSP • {cwd} (string) Working directory for the LSP
server process server process

View File

@@ -58,12 +58,10 @@ end
---@private ---@private
--- Parses an LSP Message's header --- Parses an LSP Message's header
--- ---
---@param header: The header to parse. ---@param header string: The header to parse.
---@returns Parsed headers ---@return table parsed headers
local function parse_headers(header) local function parse_headers(header)
if type(header) ~= 'string' then assert(type(header) == 'string', 'header must be a string')
return nil
end
local headers = {} local headers = {}
for line in vim.gsplit(header, '\r\n', true) do for line in vim.gsplit(header, '\r\n', true) do
if line == '' then if line == '' then
@@ -189,9 +187,9 @@ end
--- Creates an RPC response object/table. --- Creates an RPC response object/table.
--- ---
---@param code RPC error code defined in `vim.lsp.protocol.ErrorCodes` ---@param code number RPC error code defined in `vim.lsp.protocol.ErrorCodes`
---@param message (optional) arbitrary message to send to server ---@param message string|nil arbitrary message to send to server
---@param data (optional) arbitrary data to send to server ---@param data any|nil arbitrary data to send to server
local function rpc_response_error(code, message, data) local function rpc_response_error(code, message, data)
-- TODO should this error or just pick a sane error (like InternalError)? -- TODO should this error or just pick a sane error (like InternalError)?
local code_name = assert(protocol.ErrorCodes[code], 'Invalid RPC error code') local code_name = assert(protocol.ErrorCodes[code], 'Invalid RPC error code')
@@ -248,13 +246,13 @@ end
--- ---
---@param cmd (string) Command to start the LSP server. ---@param cmd (string) Command to start the LSP server.
---@param cmd_args (table) List of additional string arguments to pass to {cmd}. ---@param cmd_args (table) List of additional string arguments to pass to {cmd}.
---@param dispatchers (table, optional) Dispatchers for LSP message types. Valid ---@param dispatchers table|nil Dispatchers for LSP message types. Valid
---dispatcher names are: ---dispatcher names are:
--- - `"notification"` --- - `"notification"`
--- - `"server_request"` --- - `"server_request"`
--- - `"on_error"` --- - `"on_error"`
--- - `"on_exit"` --- - `"on_exit"`
---@param extra_spawn_params (table, optional) Additional context for the LSP ---@param extra_spawn_params table|nil Additional context for the LSP
--- server process. May contain: --- server process. May contain:
--- - {cwd} (string) Working directory for the LSP server process --- - {cwd} (string) Working directory for the LSP server process
--- - {env} (table) Additional environment variables for LSP server process --- - {env} (table) Additional environment variables for LSP server process
@@ -434,7 +432,7 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
end end
end end
stderr:read_start(function(_err, chunk) stderr:read_start(function(_, chunk)
if chunk then if chunk then
local _ = log.error() and log.error('rpc', cmd, 'stderr', chunk) local _ = log.error() and log.error('rpc', cmd, 'stderr', chunk)
end end
@@ -520,7 +518,7 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
-- This works because we are expecting vim.NIL here -- This works because we are expecting vim.NIL here
elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then
-- We sent a number, so we expect a number. -- We sent a number, so we expect a number.
local result_id = tonumber(decoded.id) local result_id = assert(tonumber(decoded.id), 'response id must be a number')
-- Notify the user that a response was received for the request -- Notify the user that a response was received for the request
local notify_reply_callback = notify_reply_callbacks and notify_reply_callbacks[result_id] local notify_reply_callback = notify_reply_callbacks and notify_reply_callbacks[result_id]