refactor(lsp): resolve the config-client entanglement

Previously the LSP-Client object contained some fields that are also
in the client config, but for a lot of other fields, the config was used
directly making the two objects vaguely entangled with either not having
a clear role.

Now the config object is treated purely as config (read-only) from the
client, and any fields the client needs from the config are now copied
in as additional fields.

This means:
- the config object is no longet normalised and is left as the user
  provided it.
- the client only reads the config on creation of the client and all
  other implementations now read the clients version of the fields.

In addition, internal support for multiple callbacks has been added to
the client so the client tracking logic (done in lua.lsp) can be done
more robustly instead of wrapping the user callbacks which may error.
This commit is contained in:
Lewis Russell
2024-02-12 13:46:32 +00:00
committed by Lewis Russell
parent ce5a9bfe7e
commit 9f8c96240d
11 changed files with 242 additions and 169 deletions

View File

@@ -763,6 +763,10 @@ client() *vim.lsp.client*
before text is sent to the server. before text is sent to the server.
• {handlers} (table): The handlers used by the client as described in • {handlers} (table): The handlers used by the client as described in
|lsp-handler|. |lsp-handler|.
• {commands} (table): Table of command name to function which is called
if any LSP action (code action, code lenses, ...) triggers the
command. Client commands take precedence over the global command
registry.
• {requests} (table): The current pending requests in flight to the • {requests} (table): The current pending requests in flight to the
server. Entries are key-value pairs with the key being the request ID server. Entries are key-value pairs with the key being the request ID
while the value is a table with `type`, `bufnr`, and `method` while the value is a table with `type`, `bufnr`, and `method`
@@ -770,12 +774,16 @@ client() *vim.lsp.client*
"cancel" for a cancel request. It will be "complete" ephemerally while "cancel" for a cancel request. It will be "complete" ephemerally while
executing |LspRequest| autocmds when replies are received from the executing |LspRequest| autocmds when replies are received from the
server. server.
• {config} (table): copy of the table that was passed by the user to • {config} (table): Reference of the table that was passed by the user
|vim.lsp.start_client()|. to |vim.lsp.start_client()|.
• {server_capabilities} (table): Response from the server sent on • {server_capabilities} (table): Response from the server sent on
`initialize` describing the server's capabilities. `initialize` describing the server's capabilities.
• {progress} A ring buffer (|vim.ringbuf()|) containing progress • {progress} A ring buffer (|vim.ringbuf()|) containing progress
messages sent by the server. messages sent by the server.
• {settings} Map with language server specific settings. See {config} in
|vim.lsp.start_client()|
• {flags} A table with flags for the client. See {config} in
|vim.lsp.start_client()|
client_is_stopped({client_id}) *vim.lsp.client_is_stopped()* client_is_stopped({client_id}) *vim.lsp.client_is_stopped()*
Checks whether a client is stopped. Checks whether a client is stopped.

View File

@@ -1,5 +1,7 @@
--- @meta --- @meta
--- @alias elem_or_list<T> T|T[]
---@type uv ---@type uv
vim.uv = ... vim.uv = ...

View File

@@ -143,6 +143,14 @@ local function for_each_buffer_client(bufnr, fn, restrict_client_ids)
end end
end end
local client_errors_base = table.maxn(lsp.rpc.client_errors)
local client_errors_offset = 0
local function new_error_index()
client_errors_offset = client_errors_offset + 1
return client_errors_base + client_errors_offset
end
--- Error codes to be used with `on_error` from |vim.lsp.start_client|. --- Error codes to be used with `on_error` from |vim.lsp.start_client|.
--- Can be used to look up the string from a the number or the number --- Can be used to look up the string from a the number or the number
--- from the string. --- from the string.
@@ -151,9 +159,10 @@ lsp.client_errors = tbl_extend(
'error', 'error',
lsp.rpc.client_errors, lsp.rpc.client_errors,
vim.tbl_add_reverse_lookup({ vim.tbl_add_reverse_lookup({
BEFORE_INIT_CALLBACK_ERROR = table.maxn(lsp.rpc.client_errors) + 1, BEFORE_INIT_CALLBACK_ERROR = new_error_index(),
ON_INIT_CALLBACK_ERROR = table.maxn(lsp.rpc.client_errors) + 2, ON_INIT_CALLBACK_ERROR = new_error_index(),
ON_ATTACH_ERROR = table.maxn(lsp.rpc.client_errors) + 3, ON_ATTACH_ERROR = new_error_index(),
ON_EXIT_CALLBACK_ERROR = new_error_index(),
}) })
) )
@@ -262,6 +271,10 @@ end
--- ---
--- - {handlers} (table): The handlers used by the client as described in |lsp-handler|. --- - {handlers} (table): The handlers used by the client as described in |lsp-handler|.
--- ---
--- - {commands} (table): Table of command name to function which is called if
--- any LSP action (code action, code lenses, ...) triggers the command.
--- Client commands take precedence over the global command registry.
---
--- - {requests} (table): The current pending requests in flight --- - {requests} (table): The current pending requests in flight
--- to the server. Entries are key-value pairs with the key --- to the server. Entries are key-value pairs with the key
--- being the request ID while the value is a table with `type`, --- being the request ID while the value is a table with `type`,
@@ -270,7 +283,7 @@ end
--- be "complete" ephemerally while executing |LspRequest| autocmds --- be "complete" ephemerally while executing |LspRequest| autocmds
--- when replies are received from the server. --- when replies are received from the server.
--- ---
--- - {config} (table): copy of the table that was passed by the user --- - {config} (table): Reference of the table that was passed by the user
--- to |vim.lsp.start_client()|. --- to |vim.lsp.start_client()|.
--- ---
--- - {server_capabilities} (table): Response from the server sent on --- - {server_capabilities} (table): Response from the server sent on
@@ -278,6 +291,11 @@ end
--- ---
--- - {progress} A ring buffer (|vim.ringbuf()|) containing progress messages --- - {progress} A ring buffer (|vim.ringbuf()|) containing progress messages
--- sent by the server. --- sent by the server.
---
--- - {settings} Map with language server specific settings.
--- See {config} in |vim.lsp.start_client()|
---
--- - {flags} A table with flags for the client. See {config} in |vim.lsp.start_client()|
function lsp.client() function lsp.client()
error() error()
end end
@@ -337,7 +355,7 @@ function lsp.start(config, opts)
opts = opts or {} opts = opts or {}
local reuse_client = opts.reuse_client local reuse_client = opts.reuse_client
or function(client, conf) or function(client, conf)
return client.config.root_dir == conf.root_dir and client.name == conf.name return client.root_dir == conf.root_dir and client.name == conf.name
end end
local bufnr = resolve_bufnr(opts.bufnr) local bufnr = resolve_bufnr(opts.bufnr)
@@ -537,20 +555,6 @@ local function on_client_exit(code, signal, client_id)
end) end)
end end
--- @generic F: function
--- @param ... F
--- @return F
local function join_cbs(...)
local funcs = vim.F.pack_len(...)
return function(...)
for i = 1, funcs.n do
if funcs[i] ~= nil then
funcs[i](...)
end
end
end
end
-- FIXME: DOC: Currently all methods on the `vim.lsp.client` object are -- FIXME: DOC: Currently all methods on the `vim.lsp.client` object are
-- documented twice: Here, and on the methods themselves (e.g. -- documented twice: Here, and on the methods themselves (e.g.
-- `client.request()`). This is a workaround for the vimdoc generator script -- `client.request()`). This is a workaround for the vimdoc generator script
@@ -671,21 +675,22 @@ end
--- fully initialized. Use `on_init` to do any actions once --- fully initialized. Use `on_init` to do any actions once
--- the client has been initialized. --- the client has been initialized.
function lsp.start_client(config) function lsp.start_client(config)
config = vim.deepcopy(config, false) local client = require('vim.lsp.client').create(config)
config.on_init = join_cbs(config.on_init, on_client_init)
config.on_exit = join_cbs(config.on_exit, on_client_exit)
local client = require('vim.lsp.client').start(config)
if not client then if not client then
return return
end end
--- @diagnostic disable-next-line: invisible
table.insert(client._on_init_cbs, on_client_init)
--- @diagnostic disable-next-line: invisible
table.insert(client._on_exit_cbs, on_client_exit)
-- Store the uninitialized_clients for cleanup in case we exit before initialize finishes. -- Store the uninitialized_clients for cleanup in case we exit before initialize finishes.
-- TODO(lewis6991): do this on before_init(). Requires API change to before_init() so it
-- can access the client_id.
uninitialized_clients[client.id] = client uninitialized_clients[client.id] = client
client:initialize()
return client.id return client.id
end end
@@ -732,7 +737,7 @@ local function text_document_did_save_handler(bufnr)
textDocument = { textDocument = {
version = 0, version = 0,
uri = uri, uri = uri,
languageId = client.config.get_language_id(bufnr, vim.bo[bufnr].filetype), languageId = client.get_language_id(bufnr, vim.bo[bufnr].filetype),
text = lsp._buf_get_full_text(bufnr), text = lsp._buf_get_full_text(bufnr),
}, },
}) })
@@ -1034,7 +1039,7 @@ api.nvim_create_autocmd('VimLeavePre', {
local send_kill = false local send_kill = false
for client_id, client in pairs(active_clients) do for client_id, client in pairs(active_clients) do
local timeout = if_nil(client.config.flags.exit_timeout, false) local timeout = if_nil(client.flags.exit_timeout, false)
if timeout then if timeout then
send_kill = true send_kill = true
timeouts[client_id] = timeout timeouts[client_id] = timeout

View File

@@ -64,7 +64,7 @@ local state_by_group = setmetatable({}, {
---@param client lsp.Client ---@param client lsp.Client
---@return vim.lsp.CTGroup ---@return vim.lsp.CTGroup
local function get_group(client) local function get_group(client)
local allow_inc_sync = vim.F.if_nil(client.config.flags.allow_incremental_sync, true) --- @type boolean local allow_inc_sync = vim.F.if_nil(client.flags.allow_incremental_sync, true) --- @type boolean
local change_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'change') local change_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'change')
local sync_kind = change_capability or protocol.TextDocumentSyncKind.None local sync_kind = change_capability or protocol.TextDocumentSyncKind.None
if not allow_inc_sync and change_capability == protocol.TextDocumentSyncKind.Incremental then if not allow_inc_sync and change_capability == protocol.TextDocumentSyncKind.Incremental then
@@ -134,12 +134,12 @@ function M.init(client, bufnr)
local group = get_group(client) local group = get_group(client)
local state = state_by_group[group] local state = state_by_group[group]
if state then if state then
state.debounce = math.min(state.debounce, client.config.flags.debounce_text_changes or 150) state.debounce = math.min(state.debounce, client.flags.debounce_text_changes or 150)
state.clients[client.id] = client state.clients[client.id] = client
else else
state = { state = {
buffers = {}, buffers = {},
debounce = client.config.flags.debounce_text_changes or 150, debounce = client.flags.debounce_text_changes or 150,
clients = { clients = {
[client.id] = client, [client.id] = client,
}, },

View File

@@ -19,7 +19,7 @@ function M:supports_registration(method)
if not client then if not client then
return false return false
end end
local capability = vim.tbl_get(client.config.capabilities, unpack(vim.split(method, '/'))) local capability = vim.tbl_get(client.capabilities, unpack(vim.split(method, '/')))
return type(capability) == 'table' and capability.dynamicRegistration return type(capability) == 'table' and capability.dynamicRegistration
end end
@@ -91,7 +91,7 @@ function M:match(bufnr, documentSelector)
if not client then if not client then
return false return false
end end
local language = client.config.get_language_id(bufnr, vim.bo[bufnr].filetype) local language = client.get_language_id(bufnr, vim.bo[bufnr].filetype)
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
local fname = vim.uri_to_fname(uri) local fname = vim.uri_to_fname(uri)
for _, filter in ipairs(documentSelector) do for _, filter in ipairs(documentSelector) do

View File

@@ -44,12 +44,8 @@ function M.register(reg, ctx)
local client = assert(vim.lsp.get_client_by_id(client_id), 'Client must be running') local client = assert(vim.lsp.get_client_by_id(client_id), 'Client must be running')
-- Ill-behaved servers may not honor the client capability and try to register -- Ill-behaved servers may not honor the client capability and try to register
-- anyway, so ignore requests when the user has opted out of the feature. -- anyway, so ignore requests when the user has opted out of the feature.
local has_capability = vim.tbl_get( local has_capability =
client.config.capabilities or {}, vim.tbl_get(client.capabilities, 'workspace', 'didChangeWatchedFiles', 'dynamicRegistration')
'workspace',
'didChangeWatchedFiles',
'dynamicRegistration'
)
if not has_capability or not client.workspace_folders then if not has_capability or not client.workspace_folders then
return return
end end

View File

@@ -6,28 +6,33 @@ local ms = lsp.protocol.Methods
local changetracking = lsp._changetracking local changetracking = lsp._changetracking
local validate = vim.validate local validate = vim.validate
--- @alias vim.lsp.client.on_init_cb fun(client: lsp.Client, initialize_result: lsp.InitializeResult)
--- @alias vim.lsp.client.on_attach_cb fun(client: lsp.Client, bufnr: integer)
--- @alias vim.lsp.client.on_exit_cb fun(code: integer, signal: integer, client_id: integer)
--- @alias vim.lsp.client.before_init_cb fun(params: lsp.InitializeParams, config: lsp.ClientConfig)
--- @class lsp.ClientConfig --- @class lsp.ClientConfig
--- @field cmd (string[]|fun(dispatchers: table):table) --- @field cmd string[]|fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient?
--- @field cmd_cwd string --- @field cmd_cwd? string
--- @field cmd_env (table) --- @field cmd_env? table
--- @field detached boolean --- @field detached? boolean
--- @field workspace_folders (table) --- @field workspace_folders? lsp.WorkspaceFolder[]
--- @field capabilities lsp.ClientCapabilities --- @field capabilities? lsp.ClientCapabilities
--- @field handlers table<string,function> --- @field handlers? table<string,function>
--- @field settings table --- @field settings? table
--- @field commands table --- @field commands? table<string,fun(command: lsp.Command, ctx: table)>
--- @field init_options table --- @field init_options table
--- @field name? string --- @field name? string
--- @field get_language_id fun(bufnr: integer, filetype: string): string --- @field get_language_id? fun(bufnr: integer, filetype: string): string
--- @field offset_encoding string --- @field offset_encoding? string
--- @field on_error fun(code: integer) --- @field on_error? fun(code: integer, err: string)
--- @field before_init fun(params: lsp.InitializeParams, config: lsp.ClientConfig) --- @field before_init? vim.lsp.client.before_init_cb
--- @field on_init fun(client: lsp.Client, initialize_result: lsp.InitializeResult) --- @field on_init? elem_or_list<vim.lsp.client.on_init_cb>
--- @field on_exit fun(code: integer, signal: integer, client_id: integer) --- @field on_exit? elem_or_list<vim.lsp.client.on_exit_cb>
--- @field on_attach fun(client: lsp.Client, bufnr: integer) --- @field on_attach? elem_or_list<vim.lsp.client.on_attach_cb>
--- @field trace 'off'|'messages'|'verbose'|nil --- @field trace? 'off'|'messages'|'verbose'
--- @field flags table --- @field flags? table
--- @field root_dir string --- @field root_dir? string
--- @class lsp.Client.Progress: vim.Ringbuf<{token: integer|string, value: any}> --- @class lsp.Client.Progress: vim.Ringbuf<{token: integer|string, value: any}>
--- @field pending table<lsp.ProgressToken,lsp.LSPAny> --- @field pending table<lsp.ProgressToken,lsp.LSPAny>
@@ -66,21 +71,43 @@ local validate = vim.validate
--- ---
--- Response from the server sent on --- Response from the server sent on
--- initialize` describing the server's capabilities. --- initialize` describing the server's capabilities.
--- @field server_capabilities lsp.ServerCapabilities --- @field server_capabilities lsp.ServerCapabilities?
--- ---
--- A ring buffer (|vim.ringbuf()|) containing progress messages --- A ring buffer (|vim.ringbuf()|) containing progress messages
--- sent by the server. --- sent by the server.
--- @field progress lsp.Client.Progress --- @field progress lsp.Client.Progress
--- ---
--- @field initialized true? --- @field initialized true?
---
--- The workspace folders configured in the client when the server starts.
--- This property is only available if the client supports workspace folders.
--- It can be `null` if the client supports workspace folders but none are
--- configured.
--- @field workspace_folders lsp.WorkspaceFolder[]? --- @field workspace_folders lsp.WorkspaceFolder[]?
--- @field root_dir string
---
--- @field attached_buffers table<integer,true> --- @field attached_buffers table<integer,true>
--- @field private _log_prefix string --- @field private _log_prefix string
---
--- Track this so that we can escalate automatically if we've already tried a --- Track this so that we can escalate automatically if we've already tried a
--- graceful shutdown --- graceful shutdown
--- @field private _graceful_shutdown_failed true? --- @field private _graceful_shutdown_failed true?
--- @field private commands table
--- ---
--- The initial trace setting. If omitted trace is disabled ("off").
--- trace = "off" | "messages" | "verbose";
--- @field private _trace 'off'|'messages'|'verbose'
---
--- Table of command name to function which is called if any LSP action
--- (code action, code lenses, ...) triggers the command.
--- Client commands take precedence over the global command registry.
--- @field commands table<string,fun(command: lsp.Command, ctx: table)>
---
--- @field settings table
--- @field flags table
--- @field get_language_id fun(bufnr: integer, filetype: string): string
---
--- The capabilities provided by the client (editor or tool)
--- @field capabilities lsp.ClientCapabilities
--- @field dynamic_capabilities lsp.DynamicCapabilities --- @field dynamic_capabilities lsp.DynamicCapabilities
--- ---
--- Sends a request to the server. --- Sends a request to the server.
@@ -122,6 +149,12 @@ local validate = vim.validate
--- Useful for buffer-local setup. --- Useful for buffer-local setup.
--- @field on_attach fun(bufnr: integer) --- @field on_attach fun(bufnr: integer)
--- ---
--- @field private _before_init_cb? vim.lsp.client.before_init_cb
--- @field private _on_attach_cbs vim.lsp.client.on_attach_cb[]
--- @field private _on_init_cbs vim.lsp.client.on_init_cb[]
--- @field private _on_exit_cbs vim.lsp.client.on_exit_cb[]
--- @field private _on_error_cb? fun(code: integer, err: string)
---
--- Checks if a client supports a given method. --- Checks if a client supports a given method.
--- Always returns true for unknown off-spec methods. --- Always returns true for unknown off-spec methods.
--- [opts] is a optional `{bufnr?: integer}` table. --- [opts] is a optional `{bufnr?: integer}` table.
@@ -196,9 +229,18 @@ local function optional_validator(fn)
end end
end end
--- By default, get_language_id just returns the exact filetype it is passed.
--- It is possible to pass in something that will calculate a different filetype,
--- to be sent by the client.
--- @param _bufnr integer
--- @param filetype string
local function default_get_language_id(_bufnr, filetype)
return filetype
end
--- Validates a client configuration as given to |vim.lsp.start_client()|. --- Validates a client configuration as given to |vim.lsp.start_client()|.
--- @param config lsp.ClientConfig --- @param config lsp.ClientConfig
local function process_client_config(config) local function validate_config(config)
validate({ validate({
config = { config, 't' }, config = { config, 't' },
}) })
@@ -210,15 +252,17 @@ local function process_client_config(config)
detached = { config.detached, 'b', true }, detached = { config.detached, 'b', true },
name = { config.name, 's', true }, name = { config.name, 's', true },
on_error = { config.on_error, 'f', true }, on_error = { config.on_error, 'f', true },
on_exit = { config.on_exit, 'f', true }, on_exit = { config.on_exit, { 'f', 't' }, true },
on_init = { config.on_init, 'f', true }, on_init = { config.on_init, { 'f', 't' }, true },
on_attach = { config.on_attach, { 'f', 't' }, true },
settings = { config.settings, 't', true }, settings = { config.settings, 't', true },
commands = { config.commands, 't', true }, commands = { config.commands, 't', true },
before_init = { config.before_init, 'f', true }, before_init = { config.before_init, { 'f', 't' }, true },
offset_encoding = { config.offset_encoding, 's', true }, offset_encoding = { config.offset_encoding, 's', true },
flags = { config.flags, 't', true }, flags = { config.flags, 't', true },
get_language_id = { config.get_language_id, 'f', true }, get_language_id = { config.get_language_id, 'f', true },
}) })
assert( assert(
( (
not config.flags not config.flags
@@ -227,51 +271,98 @@ local function process_client_config(config)
), ),
'flags.debounce_text_changes must be a number with the debounce time in milliseconds' 'flags.debounce_text_changes must be a number with the debounce time in milliseconds'
) )
if not config.name and type(config.cmd) == 'table' then
config.name = config.cmd[1] and vim.fs.basename(config.cmd[1]) or nil
end end
config.offset_encoding = validate_encoding(config.offset_encoding) --- @param trace string
config.flags = config.flags or {} --- @return 'off'|'messages'|'verbose'
config.settings = config.settings or {} local function get_trace(trace)
config.handlers = config.handlers or {} local valid_traces = {
off = 'off',
-- By default, get_language_id just returns the exact filetype it is passed. messages = 'messages',
-- It is possible to pass in something that will calculate a different filetype, verbose = 'verbose',
-- to be sent by the client. }
config.get_language_id = config.get_language_id or function(_, filetype) return trace and valid_traces[trace] or 'off'
return filetype
end end
config.capabilities = config.capabilities or lsp.protocol.make_client_capabilities() --- @param id integer
config.commands = config.commands or {} --- @param config lsp.ClientConfig
--- @return string
local function get_name(id, config)
local name = config.name
if name then
return name
end
if type(config.cmd) == 'table' and config.cmd[1] then
return assert(vim.fs.basename(config.cmd[1]))
end
return tostring(id)
end
--- @param workspace_folders lsp.WorkspaceFolder[]?
--- @param root_dir string?
--- @return lsp.WorkspaceFolder[]?
local function get_workspace_folders(workspace_folders, root_dir)
if workspace_folders then
return workspace_folders
end
if root_dir then
return {
{
uri = vim.uri_from_fname(root_dir),
name = string.format('%s', root_dir),
},
}
end
end
--- @generic T
--- @param x elem_or_list<T>?
--- @return T[]
local function ensure_list(x)
if type(x) == 'table' then
return x
end
return { x }
end end
--- @package --- @package
--- @param config lsp.ClientConfig --- @param config lsp.ClientConfig
--- @return lsp.Client? --- @return lsp.Client?
function Client.start(config) function Client.create(config)
process_client_config(config) validate_config(config)
client_index = client_index + 1 client_index = client_index + 1
local id = client_index local id = client_index
local name = get_name(id, config)
local name = config.name or tostring(id)
--- @class lsp.Client --- @class lsp.Client
local self = { local self = {
id = id, id = id,
config = config, config = config,
handlers = config.handlers, handlers = config.handlers or {},
offset_encoding = config.offset_encoding, offset_encoding = validate_encoding(config.offset_encoding),
name = name, name = name,
_log_prefix = string.format('LSP[%s]', name), _log_prefix = string.format('LSP[%s]', name),
requests = {}, requests = {},
attached_buffers = {}, attached_buffers = {},
server_capabilities = {}, server_capabilities = {},
dynamic_capabilities = vim.lsp._dynamic.new(id), dynamic_capabilities = lsp._dynamic.new(id),
commands = config.commands, -- Remove in Nvim 0.11 commands = config.commands or {},
settings = config.settings or {},
flags = config.flags or {},
get_language_id = config.get_language_id or default_get_language_id,
capabilities = config.capabilities or lsp.protocol.make_client_capabilities(),
workspace_folders = get_workspace_folders(config.workspace_folders, config.root_dir),
root_dir = config.root_dir,
_before_init_cb = config.before_init,
_on_init_cbs = ensure_list(config.on_init),
_on_exit_cbs = ensure_list(config.on_exit),
_on_attach_cbs = ensure_list(config.on_attach),
_on_error_cb = config.on_error,
_root_dir = config.root_dir,
_trace = get_trace(config.trace),
--- Contains $/progress report messages. --- Contains $/progress report messages.
--- They have the format {token: integer|string, value: any} --- They have the format {token: integer|string, value: any}
@@ -327,41 +418,31 @@ function Client.start(config)
setmetatable(self, Client) setmetatable(self, Client)
self:initialize()
return self return self
end end
--- @private --- @param cbs function[]
function Client:initialize() --- @param error_id integer
local valid_traces = { --- @param ... any
off = 'off', function Client:_run_callbacks(cbs, error_id, ...)
messages = 'messages', for _, cb in pairs(cbs) do
verbose = 'verbose', --- @type boolean, string?
} local status, err = pcall(cb, ...)
if not status then
self:write_error(error_id, err)
end
end
end
--- @package
function Client:initialize()
local config = self.config local config = self.config
local workspace_folders --- @type lsp.WorkspaceFolder[]?
local root_uri --- @type string? local root_uri --- @type string?
local root_path --- @type string? local root_path --- @type string?
if config.workspace_folders or config.root_dir then if self.workspace_folders then
if config.root_dir and not config.workspace_folders then root_uri = self.workspace_folders[1].uri
workspace_folders = {
{
uri = vim.uri_from_fname(config.root_dir),
name = string.format('%s', config.root_dir),
},
}
else
workspace_folders = config.workspace_folders
end
root_uri = workspace_folders[1].uri
root_path = vim.uri_to_fname(root_uri) root_path = vim.uri_to_fname(root_uri)
else
workspace_folders = nil
root_uri = nil
root_path = nil
end end
local initialize_params = { local initialize_params = {
@@ -383,26 +464,19 @@ function Client:initialize()
-- The rootUri of the workspace. Is null if no folder is open. If both -- The rootUri of the workspace. Is null if no folder is open. If both
-- `rootPath` and `rootUri` are set `rootUri` wins. -- `rootPath` and `rootUri` are set `rootUri` wins.
rootUri = root_uri or vim.NIL, rootUri = root_uri or vim.NIL,
-- The workspace folders configured in the client when the server starts. workspaceFolders = self.workspace_folders or vim.NIL,
-- This property is only available if the client supports workspace folders.
-- It can be `null` if the client supports workspace folders but none are
-- configured.
workspaceFolders = workspace_folders or vim.NIL,
-- User provided initialization options. -- User provided initialization options.
initializationOptions = config.init_options, initializationOptions = config.init_options,
-- The capabilities provided by the client (editor or tool) capabilities = self.capabilities,
capabilities = config.capabilities, trace = self._trace,
-- The initial trace setting. If omitted trace is disabled ("off").
-- trace = "off" | "messages" | "verbose";
trace = valid_traces[config.trace] or 'off',
} }
if config.before_init then
--- @type boolean, string? self:_run_callbacks(
local status, err = pcall(config.before_init, initialize_params, config) { self._before_init_cb },
if not status then lsp.client_errors.BEFORE_INIT_CALLBACK_ERROR,
self:write_error(lsp.client_errors.BEFORE_INIT_CALLBACK_ERROR, err) initialize_params,
end config
end )
log.trace(self._log_prefix, 'initialize_params', initialize_params) log.trace(self._log_prefix, 'initialize_params', initialize_params)
@@ -413,7 +487,6 @@ function Client:initialize()
assert(result, 'server sent empty result') assert(result, 'server sent empty result')
rpc.notify('initialized', vim.empty_dict()) rpc.notify('initialized', vim.empty_dict())
self.initialized = true self.initialized = true
self.workspace_folders = workspace_folders
-- These are the cleaned up capabilities we use for dynamically deciding -- These are the cleaned up capabilities we use for dynamically deciding
-- when to send certain events to clients. -- when to send certain events to clients.
@@ -425,17 +498,11 @@ function Client:initialize()
self.offset_encoding = self.server_capabilities.positionEncoding self.offset_encoding = self.server_capabilities.positionEncoding
end end
if next(config.settings) then if next(self.settings) then
self:_notify(ms.workspace_didChangeConfiguration, { settings = config.settings }) self:_notify(ms.workspace_didChangeConfiguration, { settings = self.settings })
end end
if config.on_init then self:_run_callbacks(self._on_init_cbs, lsp.client_errors.ON_INIT_CALLBACK_ERROR, self, result)
--- @type boolean, string?
local status, err = pcall(config.on_init, self, result)
if not status then
self:write_error(lsp.client_errors.ON_INIT_CALLBACK_ERROR, err)
end
end
log.info( log.info(
self._log_prefix, self._log_prefix,
@@ -672,7 +739,7 @@ function Client:_is_stopped()
return self.rpc.is_closing() return self.rpc.is_closing()
end end
--- @private --- @package
--- Execute a lsp command, either via client command function (if available) --- Execute a lsp command, either via client command function (if available)
--- or via workspace/executeCommand (if supported by the server) --- or via workspace/executeCommand (if supported by the server)
--- ---
@@ -684,7 +751,7 @@ function Client:_exec_cmd(command, context, handler)
context.bufnr = context.bufnr or api.nvim_get_current_buf() context.bufnr = context.bufnr or api.nvim_get_current_buf()
context.client_id = self.id context.client_id = self.id
local cmdname = command.command local cmdname = command.command
local fn = self.config.commands[cmdname] or lsp.commands[cmdname] local fn = self.commands[cmdname] or lsp.commands[cmdname]
if fn then if fn then
fn(command, context) fn(command, context)
return return
@@ -730,7 +797,7 @@ function Client:_text_document_did_open_handler(bufnr)
textDocument = { textDocument = {
version = 0, version = 0,
uri = vim.uri_from_bufnr(bufnr), uri = vim.uri_from_bufnr(bufnr),
languageId = self.config.get_language_id(bufnr, filetype), languageId = self.get_language_id(bufnr, filetype),
text = lsp._buf_get_full_text(bufnr), text = lsp._buf_get_full_text(bufnr),
}, },
} }
@@ -742,13 +809,13 @@ function Client:_text_document_did_open_handler(bufnr)
-- Protect against a race where the buffer disappears -- Protect against a race where the buffer disappears
-- between `did_open_handler` and the scheduled function firing. -- between `did_open_handler` and the scheduled function firing.
if api.nvim_buf_is_valid(bufnr) then if api.nvim_buf_is_valid(bufnr) then
local namespace = vim.lsp.diagnostic.get_namespace(self.id) local namespace = lsp.diagnostic.get_namespace(self.id)
vim.diagnostic.show(namespace, bufnr) vim.diagnostic.show(namespace, bufnr)
end end
end) end)
end end
--- @private --- @package
--- 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
function Client:_on_attach(bufnr) function Client:_on_attach(bufnr)
@@ -762,13 +829,7 @@ function Client:_on_attach(bufnr)
data = { client_id = self.id }, data = { client_id = self.id },
}) })
if self.config.on_attach then self:_run_callbacks(self._on_attach_cbs, lsp.client_errors.ON_ATTACH_ERROR, self, bufnr)
--- @type boolean, string?
local status, err = pcall(self.config.on_attach, self, bufnr)
if not status then
self:write_error(lsp.client_errors.ON_ATTACH_ERROR, err)
end
end
-- schedule the initialization of semantic tokens to give the above -- schedule the initialization of semantic tokens to give the above
-- on_attach and LspAttach callbacks the ability to schedule wrap the -- on_attach and LspAttach callbacks the ability to schedule wrap the
@@ -795,7 +856,6 @@ end
--- @param method string --- @param method string
--- @param opts? {bufnr: integer?} --- @param opts? {bufnr: integer?}
function Client:_supports_method(method, opts) function Client:_supports_method(method, opts)
opts = opts or {}
local required_capability = lsp._request_name_to_capability[method] local required_capability = lsp._request_name_to_capability[method]
-- if we don't know about the method, assume that the client supports it. -- if we don't know about the method, assume that the client supports it.
if not required_capability then if not required_capability then
@@ -803,13 +863,12 @@ function Client:_supports_method(method, opts)
end end
if vim.tbl_get(self.server_capabilities, unpack(required_capability)) then if vim.tbl_get(self.server_capabilities, unpack(required_capability)) then
return true return true
else end
if self.dynamic_capabilities:supports_registration(method) then if self.dynamic_capabilities:supports_registration(method) then
return self.dynamic_capabilities:supports(method, opts) return self.dynamic_capabilities:supports(method, opts)
end end
return false return false
end end
end
--- @private --- @private
--- Handles a notification sent by an LSP server by invoking the --- Handles a notification sent by an LSP server by invoking the
@@ -853,9 +912,9 @@ end
--- `vim.lsp.rpc.client_errors[code]` to get a human-friendly name. --- `vim.lsp.rpc.client_errors[code]` to get a human-friendly name.
function Client:_on_error(code, err) function Client:_on_error(code, err)
self:write_error(code, err) self:write_error(code, err)
if self.config.on_error then if self._on_error_cb then
--- @type boolean, string --- @type boolean, string
local status, usererr = pcall(self.config.on_error, code, err) local status, usererr = pcall(self._on_error_cb, code, err)
if not status then if not status then
log.error(self._log_prefix, 'user on_error failed', { err = usererr }) log.error(self._log_prefix, 'user on_error failed', { err = usererr })
err_message(self._log_prefix, ' user on_error failed: ', tostring(usererr)) err_message(self._log_prefix, ' user on_error failed: ', tostring(usererr))
@@ -869,9 +928,13 @@ end
--- @param code integer) exit code of the process --- @param code integer) exit code of the process
--- @param signal integer the signal used to terminate (if any) --- @param signal integer the signal used to terminate (if any)
function Client:_on_exit(code, signal) function Client:_on_exit(code, signal)
if self.config.on_exit then self:_run_callbacks(
pcall(self.config.on_exit, code, signal, self.id) self._on_exit_cbs,
end lsp.client_errors.ON_EXIT_CALLBACK_ERROR,
code,
signal,
self.id
)
end end
return Client return Client

View File

@@ -48,7 +48,6 @@ 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)
---@diagnostic disable-next-line: invisible
client:_exec_cmd(lens.command, { bufnr = bufnr }, function(...) client:_exec_cmd(lens.command, { bufnr = bufnr }, function(...)
vim.lsp.handlers[ms.workspace_executeCommand](...) vim.lsp.handlers[ms.workspace_executeCommand](...)
M.refresh() M.refresh()

View File

@@ -197,10 +197,10 @@ M[ms.workspace_configuration] = function(_, result, ctx)
local response = {} local response = {}
for _, item in ipairs(result.items) do for _, item in ipairs(result.items) do
if item.section then if item.section then
local value = lookup_section(client.config.settings, item.section) local value = lookup_section(client.settings, item.section)
-- For empty sections with no explicit '' key, return settings as is -- For empty sections with no explicit '' key, return settings as is
if value == nil and item.section == '' then if value == nil and item.section == '' then
value = client.config.settings value = client.settings
end end
if value == nil then if value == nil then
value = vim.NIL value = vim.NIL

View File

@@ -38,7 +38,7 @@ function M.check()
'%s (id=%s, root_dir=%s, attached_to=[%s])', '%s (id=%s, root_dir=%s, attached_to=[%s])',
client.name, client.name,
client.id, client.id,
vim.fn.fnamemodify(client.config.root_dir, ':~'), vim.fn.fnamemodify(client.root_dir, ':~'),
attached_to attached_to
) )
) )

View File

@@ -523,7 +523,7 @@ describe('LSP', function()
if ctx.method == 'start' then if ctx.method == 'start' then
exec_lua([=[ exec_lua([=[
local client = vim.lsp.get_client_by_id(TEST_RPC_CLIENT_ID) local client = vim.lsp.get_client_by_id(TEST_RPC_CLIENT_ID)
client.config.settings = { client.settings = {
testSetting1 = true; testSetting1 = true;
testSetting2 = false; testSetting2 = false;
test = {Setting3 = 'nested' }; test = {Setting3 = 'nested' };