refactor(lsp): encapsulate rpc uv handle

To prepare for different transports like TCP where the handle won't have
a kill method.
This commit is contained in:
Mathias Fussenegger
2022-08-23 22:39:34 +02:00
parent f9641d1ac6
commit 7d3e4aee6a
3 changed files with 16 additions and 12 deletions

View File

@@ -1904,7 +1904,7 @@ notify({method}, {params}) *vim.lsp.rpc.notify()*
Parameters: ~ Parameters: ~
{method} (string) The invoked LSP method {method} (string) The invoked LSP method
{params} (table): Parameters for the invoked LSP method {params} (table|nil): Parameters for the invoked LSP method
Return: ~ Return: ~
(bool) `true` if notification could be sent, `false` if not (bool) `true` if notification could be sent, `false` if not
@@ -1915,7 +1915,8 @@ request({method}, {params}, {callback}, {notify_reply_callback})
Parameters: ~ Parameters: ~
{method} (string) The invoked LSP method {method} (string) The invoked LSP method
{params} (table) Parameters for the invoked LSP method {params} (table|nil) Parameters for the invoked LSP
method
{callback} (function) Callback to invoke {callback} (function) Callback to invoke
{notify_reply_callback} (function|nil) Callback to invoke as soon as {notify_reply_callback} (function|nil) Callback to invoke as soon as
a request is no longer pending a request is no longer pending

View File

@@ -1464,14 +1464,13 @@ function lsp.start_client(config)
--- you request to stop a client which has previously been requested to --- you request to stop a client which has previously been requested to
--- shutdown, it will automatically escalate and force shutdown. --- shutdown, it will automatically escalate and force shutdown.
--- ---
---@param force (bool, optional) ---@param force boolean|nil
function client.stop(force) function client.stop(force)
local handle = rpc.handle if rpc.is_closing() then
if handle:is_closing() then
return return
end end
if force or not client.initialized or graceful_shutdown_failed then if force or not client.initialized or graceful_shutdown_failed then
handle:kill(15) rpc.terminate()
return return
end end
-- Sending a signal after a process has exited is acceptable. -- Sending a signal after a process has exited is acceptable.
@@ -1480,7 +1479,7 @@ function lsp.start_client(config)
rpc.notify('exit') rpc.notify('exit')
else else
-- If there was an error in the shutdown request, then term to be safe. -- If there was an error in the shutdown request, then term to be safe.
handle:kill(15) rpc.terminate()
graceful_shutdown_failed = true graceful_shutdown_failed = true
end end
end) end)
@@ -1492,7 +1491,7 @@ function lsp.start_client(config)
---@returns (bool) true if client is stopped or in the process of being ---@returns (bool) true if client is stopped or in the process of being
---stopped; false otherwise ---stopped; false otherwise
function client.is_stopped() function client.is_stopped()
return rpc.handle:is_closing() return rpc.is_closing()
end end
---@private ---@private

View File

@@ -405,7 +405,7 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
-- --
--- Sends a notification to the LSP server. --- Sends a notification to the LSP server.
---@param method (string) The invoked LSP method ---@param method (string) The invoked LSP method
---@param params (table): Parameters for the invoked LSP method ---@param params (table|nil): Parameters for the invoked LSP method
---@returns (bool) `true` if notification could be sent, `false` if not ---@returns (bool) `true` if notification could be sent, `false` if not
local function notify(method, params) local function notify(method, params)
return encode_and_send({ return encode_and_send({
@@ -432,7 +432,7 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
--- Sends a request to the LSP server and runs {callback} upon response. --- Sends a request to the LSP server and runs {callback} upon response.
--- ---
---@param method (string) The invoked LSP method ---@param method (string) The invoked LSP method
---@param params (table) Parameters for the invoked LSP method ---@param params (table|nil) Parameters for the invoked LSP method
---@param callback (function) Callback to invoke ---@param callback (function) Callback to invoke
---@param notify_reply_callback (function|nil) Callback to invoke as soon as a request is no longer pending ---@param notify_reply_callback (function|nil) Callback to invoke as soon as a request is no longer pending
---@returns (bool, number) `(true, message_id)` if request could be sent, `false` if not ---@returns (bool, number) `(true, message_id)` if request could be sent, `false` if not
@@ -626,8 +626,12 @@ local function start(cmd, cmd_args, dispatchers, extra_spawn_params)
end)) end))
return { return {
pid = pid, is_closing = function()
handle = handle, return handle:is_closing()
end,
terminate = function()
handle:kill(15)
end,
request = request, request = request,
notify = notify, notify = notify,
} }