Change callback resolution to be dynamic.

This allows default_callbacks to be specified after client creation to
be considered. Also it simplifies the code.
This commit is contained in:
Ashkan Kiani
2019-11-20 16:09:03 -08:00
parent a4b7004f48
commit 03eb88848c
2 changed files with 12 additions and 11 deletions

View File

@@ -178,8 +178,8 @@ vim.lsp.start_client({config})
`callbacks` `callbacks`
A {table} of whose keys are language server method names and the values A {table} of whose keys are language server method names and the values
are `function(err, method, params, client_id)` See |lsp-callbacks| for are `function(err, method, params, client_id)` See |lsp-callbacks| for
more. This will be combined with |lsp-builtin-callbacks| to provide more. This will be combined with |lsp-default-callbacks| to resolve
defaults. the callbacks for a client as a fallback.
`init_options` `init_options`
A {table} of values to pass in the initialization request as A {table} of values to pass in the initialization request as

View File

@@ -284,19 +284,19 @@ function lsp.start_client(config)
local client_id = next_client_id() local client_id = next_client_id()
local callbacks = tbl_extend("keep", config.callbacks or {}, default_callbacks) local callbacks = config.callbacks or {}
-- Copy metatable if it has one.
if config.callbacks and config.callbacks.__metatable then
setmetatable(callbacks, getmetatable(config.callbacks))
end
local name = config.name or tostring(client_id) local name = config.name or tostring(client_id)
local log_prefix = string.format("LSP[%s]", name) local log_prefix = string.format("LSP[%s]", name)
local handlers = {} local handlers = {}
local function resolve_callback(method)
return callbacks[method] or default_callbacks[method]
end
function handlers.notification(method, params) function handlers.notification(method, params)
local _ = log.debug() and log.debug('notification', method, params) local _ = log.debug() and log.debug('notification', method, params)
local callback = callbacks[method] local callback = resolve_callback(method)
if callback then if callback then
-- Method name is provided here for convenience. -- Method name is provided here for convenience.
callback(nil, method, params, client_id) callback(nil, method, params, client_id)
@@ -305,7 +305,7 @@ function lsp.start_client(config)
function handlers.server_request(method, params) function handlers.server_request(method, params)
local _ = log.debug() and log.debug('server_request', method, params) local _ = log.debug() and log.debug('server_request', method, params)
local callback = callbacks[method] local callback = resolve_callback(method)
if callback then if callback then
local _ = log.debug() and log.debug("server_request: found callback for", method) local _ = log.debug() and log.debug("server_request: found callback for", method)
return callback(nil, method, params, client_id) return callback(nil, method, params, client_id)
@@ -316,7 +316,8 @@ function lsp.start_client(config)
function handlers.on_error(code, err) function handlers.on_error(code, err)
local _ = log.error() and log.error(log_prefix, "on_error", { code = lsp.client_errors[code], err = err }) local _ = log.error() and log.error(log_prefix, "on_error", { code = lsp.client_errors[code], err = err })
nvim_err_writeln(string.format('%s: Error %s: %q', log_prefix, lsp.client_errors[code], vim.inspect(err))) print(string.format('%s: Error %s: %q', log_prefix, lsp.client_errors[code], vim.inspect(err)))
-- nvim_err_writeln(string.format('%s: Error %s: %q', log_prefix, lsp.client_errors[code], vim.inspect(err)))
if config.on_error then if config.on_error then
local status, usererr = pcall(config.on_error, code, err) local status, usererr = pcall(config.on_error, code, err)
if not status then if not status then
@@ -440,7 +441,7 @@ function lsp.start_client(config)
--- Checks capabilities before rpc.request-ing. --- Checks capabilities before rpc.request-ing.
function client.request(method, params, callback) function client.request(method, params, callback)
if not callback then if not callback then
callback = client.callbacks[method] callback = resolve_callback(method)
or error(string.format("request callback is empty and no default was found for client %s", client.name)) or error(string.format("request callback is empty and no default was found for client %s", client.name))
end end
local _ = log.debug() and log.debug(log_prefix, "client.request", client_id, method, params, callback) local _ = log.debug() and log.debug(log_prefix, "client.request", client_id, method, params, callback)