refactor(lsp): inline on_client_exit

This commit is contained in:
Yi Ming
2025-07-20 22:09:27 +08:00
parent 5400df0f7f
commit 6831c7de65
2 changed files with 22 additions and 34 deletions

View File

@@ -172,37 +172,6 @@ local function reuse_client_default(client, config)
return true
end
--- @param code integer
--- @param signal integer
--- @param client_id integer
local function on_client_exit(code, signal, client_id)
local client = lsp.get_client_by_id(client_id)
local name = client and client.name or 'unknown'
-- Schedule the deletion of the client object so that it exists in the execution of LspDetach
-- autocommands
vim.schedule(function()
lsp.client._all[client_id] = nil
-- Client can be absent if executable starts, but initialize fails
-- init/attach won't have happened
if client then
changetracking.reset(client)
end
if code ~= 0 or (signal ~= 0 and signal ~= 15) then
local msg = string.format(
'Client %s quit with exit code %s and signal %s. Check log for errors: %s',
name,
code,
signal,
lsp.get_log_path()
)
vim.notify(msg, vim.log.levels.WARN)
end
end)
end
--- Creates and initializes a client with the given configuration.
--- @param config vim.lsp.ClientConfig Configuration for the server.
--- @return integer? client_id |vim.lsp.get_client_by_id()| Note: client may not be
@@ -217,9 +186,6 @@ local function create_and_init_client(config)
local client = assert(res)
--- @diagnostic disable-next-line: invisible
table.insert(client._on_exit_cbs, on_client_exit)
client:initialize()
return client.id, nil

View File

@@ -1252,6 +1252,28 @@ function Client:_on_exit(code, signal)
end
end)
-- Schedule the deletion of the client object so that it exists in the execution of LspDetach
-- autocommands
vim.schedule(function()
all_clients[self.id] = nil
-- Client can be absent if executable starts, but initialize fails
-- init/attach won't have happened
if self then
changetracking.reset(self)
end
if code ~= 0 or (signal ~= 0 and signal ~= 15) then
local msg = string.format(
'Client %s quit with exit code %s and signal %s. Check log for errors: %s',
self and self.name or 'unknown',
code,
signal,
lsp.get_log_path()
)
vim.notify(msg, vim.log.levels.WARN)
end
end)
self:_run_callbacks(
self._on_exit_cbs,
lsp.client_errors.ON_EXIT_CALLBACK_ERROR,