1
mirror of https://github.com/neovim/neovim.git synced 2024-12-27 14:21:31 -07:00

feat(lsp): deprecate non-method client functions

Deprecated:
- `client.request()` -> `client:request()`
- `client.request_sync()` -> `client:request_sync()`
- `client.notify()` -> `client:notify()`
- `client.cancel_request()` -> `client:cancel_request()`
- `client.stop()` -> `client:stop()`
- `client.is_stopped()` `client:is_stopped()`
- `client.supports_method()` -> `client:supports_method()`
- `client.on_attach()` -> `client:on_attach()`

Fixed docgen to link class fields to the full function doc.
This commit is contained in:
Lewis Russell 2024-11-14 11:53:20 +00:00 committed by Lewis Russell
parent f55c842ec7
commit 454ae672aa
19 changed files with 403 additions and 358 deletions

View File

@ -56,6 +56,14 @@ LSP
No longer support client to server response handlers. Only server to No longer support client to server response handlers. Only server to
client requests/notification handlers are supported. client requests/notification handlers are supported.
• *vim.lsp.handlers.signature_help()* Use |vim.lsp.buf.signature_help()| instead. • *vim.lsp.handlers.signature_help()* Use |vim.lsp.buf.signature_help()| instead.
• `client.request()` Use |Client:request()| instead.
• `client.request_sync()` Use |Client:request_sync()| instead.
• `client.notify()` Use |Client:notify()| instead.
• `client.cancel_request()` Use |Client:cancel_request()| instead.
• `client.stop()` Use |Client:stop()| instead.
• `client.is_stopped()` Use |Client:is_stopped()| instead.
• `client.supports_method()` Use |Client:supports_method()| instead.
• `client.on_attach()` Use |Client:on_attach()| instead.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
DEPRECATED IN 0.10 *deprecated-0.10* DEPRECATED IN 0.10 *deprecated-0.10*

View File

@ -972,54 +972,24 @@ Lua module: vim.lsp.client *lsp-client*
• {capabilities} (`lsp.ClientCapabilities`) The capabilities • {capabilities} (`lsp.ClientCapabilities`) The capabilities
provided by the client (editor or tool) provided by the client (editor or tool)
• {dynamic_capabilities} (`lsp.DynamicCapabilities`) • {dynamic_capabilities} (`lsp.DynamicCapabilities`)
• {request} (`fun(method: string, params: table?, handler: lsp.Handler?, bufnr: integer?): boolean, integer?`) • {request} (`fun(self: vim.lsp.Client, method: string, params: table?, handler: lsp.Handler?, bufnr: integer?): boolean, integer?`)
Sends a request to the server. This is a thin See |Client:request()|.
wrapper around {client.rpc.request} with some • {request_sync} (`fun(self: vim.lsp.Client, method: string, params: table, timeout_ms: integer?, bufnr: integer): {err: lsp.ResponseError?, result:any}?, string?`)
additional checking. If {handler} is not See |Client:request_sync()|.
specified and if there's no respective global • {notify} (`fun(self: vim.lsp.Client, method: string, params: table?): boolean`)
handler, then an error will occur. Returns: See |Client:notify()|.
{status}, {client_id}?. {status} is a boolean • {cancel_request} (`fun(self: vim.lsp.Client, id: integer): boolean`)
indicating if the notification was successful. See |Client:cancel_request()|.
If it is `false`, then it will always be • {stop} (`fun(self: vim.lsp.Client, force: boolean?)`)
`false` (the client has shutdown). If {status} See |Client:stop()|.
is `true`, the function returns {request_id} • {is_stopped} (`fun(self: vim.lsp.Client): boolean`) See
as the second result. You can use this with |Client:is_stopped()|.
`client.cancel_request(request_id)` to cancel
the request.
• {request_sync} (`fun(method: string, params: table?, timeout_ms: integer?, bufnr: integer): {err: lsp.ResponseError?, result:any}?, string?`)
err # a dict
• {notify} (`fun(method: string, params: table?): boolean`)
Sends a notification to an LSP server.
Returns: a boolean to indicate if the
notification was successful. If it is false,
then it will always be false (the client has
shutdown).
• {cancel_request} (`fun(id: integer): boolean`) Cancels a
request with a given request id. Returns: same
as `notify()`.
• {stop} (`fun(force?: boolean)`) Stops a client,
optionally with force. By default, it will
just ask the server to shutdown without force.
If you request to stop a client which has
previously been requested to shutdown, it will
automatically escalate and force shutdown.
• {on_attach} (`fun(bufnr: integer)`) Runs the on_attach
function from the client's config if it was
defined. Useful for buffer-local setup.
• {supports_method} (`fun(method: string, opts?: {bufnr: integer?}): boolean`)
Checks if a client supports a given method.
Always returns true for unknown off-spec
methods. {opts} is a optional
`{bufnr?: integer}` table. Some language
server capabilities can be file specific.
• {is_stopped} (`fun(): boolean`) Checks whether a client is
stopped. Returns: true if the client is fully
stopped.
• {exec_cmd} (`fun(self: vim.lsp.Client, command: lsp.Command, context: {bufnr?: integer}?, handler: lsp.Handler?)`) • {exec_cmd} (`fun(self: vim.lsp.Client, command: lsp.Command, context: {bufnr?: integer}?, handler: lsp.Handler?)`)
Execute a lsp command, either via client See |Client:exec_cmd()|.
command function (if available) or via • {on_attach} (`fun(self: vim.lsp.Client, bufnr: integer)`)
workspace/executeCommand (if supported by the See |Client:on_attach()|.
server) • {supports_method} (`fun(self: vim.lsp.Client, method: string, bufnr: integer?)`)
See |Client:supports_method()|.
*vim.lsp.Client.Progress* *vim.lsp.Client.Progress*
Extends: |vim.Ringbuf| Extends: |vim.Ringbuf|
@ -1150,6 +1120,18 @@ Lua module: vim.lsp.client *lsp-client*
on initialization. on initialization.
Client:cancel_request({id}) *Client:cancel_request()*
Cancels a request with a given request id.
Parameters: ~
• {id} (`integer`) id of request to cancel
Return: ~
(`boolean`) status indicating if the notification was successful.
See also: ~
• |Client:notify()|
Client:exec_cmd({command}, {context}, {handler}) *Client:exec_cmd()* Client:exec_cmd({command}, {context}, {handler}) *Client:exec_cmd()*
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)
@ -1159,6 +1141,95 @@ Client:exec_cmd({command}, {context}, {handler}) *Client:exec_cmd()*
• {context} (`{bufnr?: integer}?`) • {context} (`{bufnr?: integer}?`)
• {handler} (`lsp.Handler?`) only called if a server command • {handler} (`lsp.Handler?`) only called if a server command
Client:is_stopped() *Client:is_stopped()*
Checks whether a client is stopped.
Return: ~
(`boolean`) true if client is stopped or in the process of being
stopped; false otherwise
Client:notify({method}, {params}) *Client:notify()*
Sends a notification to an LSP server.
Parameters: ~
• {method} (`string`) LSP method name.
• {params} (`table?`) LSP request params.
Return: ~
(`boolean`) status indicating if the notification was successful. If
it is false, then the client has shutdown.
Client:on_attach({bufnr}) *Client:on_attach()*
Runs the on_attach function from the client's config if it was defined.
Useful for buffer-local setup.
Parameters: ~
• {bufnr} (`integer`) Buffer number
*Client:request()*
Client:request({method}, {params}, {handler}, {bufnr})
Sends a request to the server.
This is a thin wrapper around {client.rpc.request} with some additional
checks for capabilities and handler availability.
Parameters: ~
• {method} (`string`) LSP method name.
• {params} (`table?`) LSP request params.
• {handler} (`lsp.Handler?`) Response |lsp-handler| for this method.
• {bufnr} (`integer?`) Buffer handle. 0 for current (default).
Return (multiple): ~
(`boolean`) status indicates whether the request was successful. If it
is `false`, then it will always be `false` (the client has shutdown).
(`integer?`) request_id Can be used with |Client:cancel_request()|.
`nil` is request failed.
See also: ~
• |vim.lsp.buf_request_all()|
*Client:request_sync()*
Client:request_sync({method}, {params}, {timeout_ms}, {bufnr})
Sends a request to the server and synchronously waits for the response.
This is a wrapper around |Client:request()|
Parameters: ~
• {method} (`string`) LSP method name.
• {params} (`table`) LSP request params.
• {timeout_ms} (`integer?`) Maximum time in milliseconds to wait for a
result. Defaults to 1000
• {bufnr} (`integer`) Buffer handle (0 for current).
Return (multiple): ~
(`{err: lsp.ResponseError?, result:any}?`) `result` and `err` from the
|lsp-handler|. `nil` is the request was unsuccessful
(`string?`) err On timeout, cancel or error, where `err` is a string
describing the failure reason.
See also: ~
• |vim.lsp.buf_request_sync()|
Client:stop({force}) *Client:stop()*
Stops a client, optionally with force.
By default, it will just request the server to shutdown without force. If
you request to stop a client which has previously been requested to
shutdown, it will automatically escalate and force shutdown.
Parameters: ~
• {force} (`boolean?`)
Client:supports_method({method}, {bufnr}) *Client:supports_method()*
Checks if a client supports a given method. Always returns true for
unknown off-spec methods.
Note: Some language server capabilities can be file specific.
Parameters: ~
• {method} (`string`)
• {bufnr} (`integer?`)
============================================================================== ==============================================================================
Lua module: vim.lsp.buf *lsp-buf* Lua module: vim.lsp.buf *lsp-buf*
@ -1599,12 +1670,12 @@ get({filter}) *vim.lsp.inlay_hint.get()*
local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer
local client = vim.lsp.get_client_by_id(hint.client_id) local client = vim.lsp.get_client_by_id(hint.client_id)
local resp = client.request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0) local resp = client:request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0)
local resolved_hint = assert(resp and resp.result, resp.err) local resolved_hint = assert(resp and resp.result, resp.err)
vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding) vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding)
location = resolved_hint.label[1].location location = resolved_hint.label[1].location
client.request('textDocument/hover', { client:request('textDocument/hover', {
textDocument = { uri = location.uri }, textDocument = { uri = location.uri },
position = location.range.start, position = location.range.start,
}) })

View File

@ -1937,12 +1937,10 @@ vim.show_pos({bufnr}, {row}, {col}, {filter}) *vim.show_pos()*
*vim.Ringbuf* *vim.Ringbuf*
Fields: ~ Fields: ~
• {clear} (`fun()`) Clear all items • {clear} (`fun()`) See |Ringbuf:clear()|.
• {push} (`fun(item: T)`) Adds an item, overriding the oldest item if • {push} (`fun(item: T)`) See |Ringbuf:push()|.
the buffer is full. • {pop} (`fun(): T?`) See |Ringbuf:pop()|.
• {pop} (`fun(): T?`) Removes and returns the first unread item • {peek} (`fun(): T?`) See |Ringbuf:peek()|.
• {peek} (`fun(): T?`) Returns the first unread item without removing
it
Ringbuf:clear() *Ringbuf:clear()* Ringbuf:clear() *Ringbuf:clear()*

View File

@ -218,6 +218,7 @@ LSP
• The client now supports `'utf-8'` and `'utf-32'` position encodings. • The client now supports `'utf-8'` and `'utf-32'` position encodings.
• |vim.lsp.buf.hover()| now highlights hover ranges using the • |vim.lsp.buf.hover()| now highlights hover ranges using the
|hl-LspReferenceTarget| highlight group. |hl-LspReferenceTarget| highlight group.
• Functions in |vim.lsp.Client| can now be called as methods.
LUA LUA

View File

@ -349,17 +349,17 @@ end
---@param bufnr integer ---@param bufnr integer
function lsp._set_defaults(client, bufnr) function lsp._set_defaults(client, bufnr)
if if
client.supports_method(ms.textDocument_definition) and is_empty_or_default(bufnr, 'tagfunc') client:supports_method(ms.textDocument_definition) and is_empty_or_default(bufnr, 'tagfunc')
then then
vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc' vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc'
end end
if if
client.supports_method(ms.textDocument_completion) and is_empty_or_default(bufnr, 'omnifunc') client:supports_method(ms.textDocument_completion) and is_empty_or_default(bufnr, 'omnifunc')
then then
vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc'
end end
if if
client.supports_method(ms.textDocument_rangeFormatting) client:supports_method(ms.textDocument_rangeFormatting)
and is_empty_or_default(bufnr, 'formatprg') and is_empty_or_default(bufnr, 'formatprg')
and is_empty_or_default(bufnr, 'formatexpr') and is_empty_or_default(bufnr, 'formatexpr')
then then
@ -367,14 +367,14 @@ function lsp._set_defaults(client, bufnr)
end end
vim._with({ buf = bufnr }, function() vim._with({ buf = bufnr }, function()
if if
client.supports_method(ms.textDocument_hover) client:supports_method(ms.textDocument_hover)
and is_empty_or_default(bufnr, 'keywordprg') and is_empty_or_default(bufnr, 'keywordprg')
and vim.fn.maparg('K', 'n', false, false) == '' and vim.fn.maparg('K', 'n', false, false) == ''
then then
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr, desc = 'vim.lsp.buf.hover()' }) vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr, desc = 'vim.lsp.buf.hover()' })
end end
end) end)
if client.supports_method(ms.textDocument_diagnostic) then if client:supports_method(ms.textDocument_diagnostic) then
lsp.diagnostic._enable(bufnr) lsp.diagnostic._enable(bufnr)
end end
end end
@ -485,12 +485,12 @@ local function text_document_did_save_handler(bufnr)
local name = api.nvim_buf_get_name(bufnr) local name = api.nvim_buf_get_name(bufnr)
local old_name = changetracking._get_and_set_name(client, bufnr, name) local old_name = changetracking._get_and_set_name(client, bufnr, name)
if old_name and name ~= old_name then if old_name and name ~= old_name then
client.notify(ms.textDocument_didClose, { client:notify(ms.textDocument_didClose, {
textDocument = { textDocument = {
uri = vim.uri_from_fname(old_name), uri = vim.uri_from_fname(old_name),
}, },
}) })
client.notify(ms.textDocument_didOpen, { client:notify(ms.textDocument_didOpen, {
textDocument = { textDocument = {
version = 0, version = 0,
uri = uri, uri = uri,
@ -506,7 +506,7 @@ local function text_document_did_save_handler(bufnr)
if type(save_capability) == 'table' and save_capability.includeText then if type(save_capability) == 'table' and save_capability.includeText then
included_text = text(bufnr) included_text = text(bufnr)
end end
client.notify(ms.textDocument_didSave, { client:notify(ms.textDocument_didSave, {
textDocument = { textDocument = {
uri = uri, uri = uri,
}, },
@ -527,10 +527,10 @@ local function buf_detach_client(bufnr, client)
changetracking.reset_buf(client, bufnr) changetracking.reset_buf(client, bufnr)
if client.supports_method(ms.textDocument_didClose) then if client:supports_method(ms.textDocument_didClose) then
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
local params = { textDocument = { uri = uri } } local params = { textDocument = { uri = uri } }
client.notify(ms.textDocument_didClose, params) client:notify(ms.textDocument_didClose, params)
end end
client.attached_buffers[bufnr] = nil client.attached_buffers[bufnr] = nil
@ -564,12 +564,12 @@ local function buf_attach(bufnr)
}, },
reason = protocol.TextDocumentSaveReason.Manual, ---@type integer reason = protocol.TextDocumentSaveReason.Manual, ---@type integer
} }
if client.supports_method(ms.textDocument_willSave) then if client:supports_method(ms.textDocument_willSave) then
client.notify(ms.textDocument_willSave, params) client:notify(ms.textDocument_willSave, params)
end end
if client.supports_method(ms.textDocument_willSaveWaitUntil) then if client:supports_method(ms.textDocument_willSaveWaitUntil) then
local result, err = local result, err =
client.request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf) client:request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf)
if result and result.result then if result and result.result then
util.apply_text_edits(result.result, ctx.buf, client.offset_encoding) util.apply_text_edits(result.result, ctx.buf, client.offset_encoding)
elseif err then elseif err then
@ -603,8 +603,8 @@ local function buf_attach(bufnr)
local params = { textDocument = { uri = uri } } local params = { textDocument = { uri = uri } }
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
changetracking.reset_buf(client, bufnr) changetracking.reset_buf(client, bufnr)
if client.supports_method(ms.textDocument_didClose) then if client:supports_method(ms.textDocument_didClose) then
client.notify(ms.textDocument_didClose, params) client:notify(ms.textDocument_didClose, params)
end end
end end
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
@ -662,7 +662,7 @@ function lsp.buf_attach_client(bufnr, client_id)
-- Send didOpen for the client if it is initialized. If it isn't initialized -- Send didOpen for the client if it is initialized. If it isn't initialized
-- then it will send didOpen on initialize. -- then it will send didOpen on initialize.
if client.initialized then if client.initialized then
client:_on_attach(bufnr) client:on_attach(bufnr)
end end
return true return true
end end
@ -740,13 +740,13 @@ function lsp.stop_client(client_id, force)
for _, id in ipairs(ids) do for _, id in ipairs(ids) do
if type(id) == 'table' then if type(id) == 'table' then
if id.stop then if id.stop then
id.stop(force) id:stop(force)
end end
else else
--- @cast id -vim.lsp.Client --- @cast id -vim.lsp.Client
local client = all_clients[id] local client = all_clients[id]
if client then if client then
client.stop(force) client:stop(force)
end end
end end
end end
@ -790,7 +790,7 @@ function lsp.get_clients(filter)
and (filter.id == nil or client.id == filter.id) and (filter.id == nil or client.id == filter.id)
and (filter.bufnr == nil or client.attached_buffers[bufnr]) and (filter.bufnr == nil or client.attached_buffers[bufnr])
and (filter.name == nil or client.name == filter.name) and (filter.name == nil or client.name == filter.name)
and (filter.method == nil or client.supports_method(filter.method, { bufnr = filter.bufnr })) and (filter.method == nil or client:supports_method(filter.method, filter.bufnr))
and (filter._uninitialized or client.initialized) and (filter._uninitialized or client.initialized)
then then
clients[#clients + 1] = client clients[#clients + 1] = client
@ -812,7 +812,7 @@ api.nvim_create_autocmd('VimLeavePre', {
local active_clients = lsp.get_clients() local active_clients = lsp.get_clients()
log.info('exit_handler', active_clients) log.info('exit_handler', active_clients)
for _, client in pairs(all_clients) do for _, client in pairs(all_clients) do
client.stop() client:stop()
end end
local timeouts = {} --- @type table<integer,integer> local timeouts = {} --- @type table<integer,integer>
@ -847,7 +847,7 @@ api.nvim_create_autocmd('VimLeavePre', {
if not vim.wait(max_timeout, check_clients_closed, poll_time) then if not vim.wait(max_timeout, check_clients_closed, poll_time) then
for client_id, client in pairs(active_clients) do for client_id, client in pairs(active_clients) do
if timeouts[client_id] ~= nil then if timeouts[client_id] ~= nil then
client.stop(true) client:stop(true)
end end
end end
end end
@ -883,11 +883,11 @@ function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
local clients = lsp.get_clients({ bufnr = bufnr }) local clients = lsp.get_clients({ bufnr = bufnr })
local client_request_ids = {} --- @type table<integer,integer> local client_request_ids = {} --- @type table<integer,integer>
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
if client.supports_method(method, { bufnr = bufnr }) then if client:supports_method(method, bufnr) then
method_supported = true method_supported = true
local cparams = type(params) == 'function' and params(client, bufnr) or params --[[@as table?]] local cparams = type(params) == 'function' and params(client, bufnr) or params --[[@as table?]]
local request_success, request_id = client.request(method, cparams, handler, bufnr) local request_success, request_id = client:request(method, cparams, handler, bufnr)
-- This could only fail if the client shut down in the time since we looked -- This could only fail if the client shut down in the time since we looked
-- it up and we did the request, which should be rare. -- it up and we did the request, which should be rare.
if request_success then if request_success then
@ -910,7 +910,7 @@ function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
local function _cancel_all_requests() local function _cancel_all_requests()
for client_id, request_id in pairs(client_request_ids) do for client_id, request_id in pairs(client_request_ids) do
local client = all_clients[client_id] local client = all_clients[client_id]
client.cancel_request(request_id) client:cancel_request(request_id)
end end
end end
@ -1049,7 +1049,7 @@ function lsp.formatexpr(opts)
end end
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
if client.supports_method(ms.textDocument_rangeFormatting) then if client:supports_method(ms.textDocument_rangeFormatting) then
local params = util.make_formatting_params() local params = util.make_formatting_params()
local end_line = vim.fn.getline(end_lnum) --[[@as string]] local end_line = vim.fn.getline(end_lnum) --[[@as string]]
local end_col = vim.str_utfindex(end_line, client.offset_encoding) local end_col = vim.str_utfindex(end_line, client.offset_encoding)
@ -1065,7 +1065,7 @@ function lsp.formatexpr(opts)
}, },
} }
local response = local response =
client.request_sync(ms.textDocument_rangeFormatting, params, timeout_ms, bufnr) client:request_sync(ms.textDocument_rangeFormatting, params, timeout_ms, bufnr)
if response and response.result then if response and response.result then
lsp.util.apply_text_edits(response.result, bufnr, client.offset_encoding) lsp.util.apply_text_edits(response.result, bufnr, client.offset_encoding)
return 0 return 0

View File

@ -40,7 +40,7 @@ local M = {}
--- @class vim.lsp.CTGroupState --- @class vim.lsp.CTGroupState
--- @field buffers table<integer,vim.lsp.CTBufferState> --- @field buffers table<integer,vim.lsp.CTBufferState>
--- @field debounce integer debounce duration in ms --- @field debounce integer debounce duration in ms
--- @field clients table<integer, table> clients using this state. {client_id, client} --- @field clients table<integer, vim.lsp.Client> clients using this state. {client_id, client}
---@param group vim.lsp.CTGroup ---@param group vim.lsp.CTGroup
---@return string ---@return string
@ -273,8 +273,8 @@ local function send_changes(bufnr, sync_kind, state, buf_state)
end end
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
for _, client in pairs(state.clients) do for _, client in pairs(state.clients) do
if not client.is_stopped() and vim.lsp.buf_is_attached(bufnr, client.id) then if not client:is_stopped() and vim.lsp.buf_is_attached(bufnr, client.id) then
client.notify(protocol.Methods.textDocument_didChange, { client:notify(protocol.Methods.textDocument_didChange, {
textDocument = { textDocument = {
uri = uri, uri = uri,
version = util.buf_versions[bufnr], version = util.buf_versions[bufnr],

View File

@ -59,7 +59,7 @@ local function query_definition(pattern)
remaining = remaining - 1 remaining = remaining - 1
end end
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
client.request(ms.textDocument_definition, params, on_response, bufnr) client:request(ms.textDocument_definition, params, on_response, bufnr)
end end
vim.wait(1000, function() vim.wait(1000, function()
return remaining == 0 return remaining == 0

View File

@ -116,7 +116,7 @@ function M.register(reg, client_id)
local params = { local params = {
changes = change_queues[client_id], changes = change_queues[client_id],
} }
client.notify(ms.workspace_didChangeWatchedFiles, params) client:notify(ms.workspace_didChangeWatchedFiles, params)
queue_timers[client_id] = nil queue_timers[client_id] = nil
change_queues[client_id] = nil change_queues[client_id] = nil
change_cache[client_id] = nil change_cache[client_id] = nil

View File

@ -232,7 +232,7 @@ local function get_locations(method, opts)
end end
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
client.request(method, params, function(_, result) client:request(method, params, function(_, result)
on_response(_, result, client) on_response(_, result, client)
end) end)
end end
@ -568,12 +568,14 @@ function M.format(opts)
end end
if opts.async then if opts.async then
--- @param idx integer
--- @param client vim.lsp.Client
local function do_format(idx, client) local function do_format(idx, client)
if not client then if not client then
return return
end end
local params = set_range(client, util.make_formatting_params(opts.formatting_options)) local params = set_range(client, util.make_formatting_params(opts.formatting_options))
client.request(method, params, function(...) client:request(method, params, function(...)
local handler = client.handlers[method] or lsp.handlers[method] local handler = client.handlers[method] or lsp.handlers[method]
handler(...) handler(...)
do_format(next(clients, idx)) do_format(next(clients, idx))
@ -584,7 +586,7 @@ function M.format(opts)
local timeout_ms = opts.timeout_ms or 1000 local timeout_ms = opts.timeout_ms or 1000
for _, client in pairs(clients) do for _, client in pairs(clients) do
local params = set_range(client, util.make_formatting_params(opts.formatting_options)) local params = set_range(client, util.make_formatting_params(opts.formatting_options))
local result, err = client.request_sync(method, params, timeout_ms, bufnr) local result, err = client:request_sync(method, params, timeout_ms, bufnr)
if result and result.result then if result and result.result then
util.apply_text_edits(result.result, bufnr, client.offset_encoding) util.apply_text_edits(result.result, bufnr, client.offset_encoding)
elseif err then elseif err then
@ -648,6 +650,8 @@ function M.rename(new_name, opts)
)[1] )[1]
end end
--- @param idx integer
--- @param client? vim.lsp.Client
local function try_use_client(idx, client) local function try_use_client(idx, client)
if not client then if not client then
return return
@ -659,15 +663,15 @@ function M.rename(new_name, opts)
params.newName = name params.newName = name
local handler = client.handlers[ms.textDocument_rename] local handler = client.handlers[ms.textDocument_rename]
or lsp.handlers[ms.textDocument_rename] or lsp.handlers[ms.textDocument_rename]
client.request(ms.textDocument_rename, params, function(...) client:request(ms.textDocument_rename, params, function(...)
handler(...) handler(...)
try_use_client(next(clients, idx)) try_use_client(next(clients, idx))
end, bufnr) end, bufnr)
end end
if client.supports_method(ms.textDocument_prepareRename) then if client:supports_method(ms.textDocument_prepareRename) then
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
client.request(ms.textDocument_prepareRename, params, function(err, result) client:request(ms.textDocument_prepareRename, params, function(err, result)
if err or result == nil then if err or result == nil then
if next(clients, idx) then if next(clients, idx) then
try_use_client(next(clients, idx)) try_use_client(next(clients, idx))
@ -706,7 +710,7 @@ function M.rename(new_name, opts)
end, bufnr) end, bufnr)
else else
assert( assert(
client.supports_method(ms.textDocument_rename), client:supports_method(ms.textDocument_rename),
'Client must support textDocument/rename' 'Client must support textDocument/rename'
) )
if new_name then if new_name then
@ -781,7 +785,7 @@ function M.references(context, opts)
params.context = context or { params.context = context or {
includeDeclaration = true, includeDeclaration = true,
} }
client.request(ms.textDocument_references, params, function(_, result) client:request(ms.textDocument_references, params, function(_, result)
local items = util.locations_to_items(result or {}, client.offset_encoding) local items = util.locations_to_items(result or {}, client.offset_encoding)
vim.list_extend(all_items, items) vim.list_extend(all_items, items)
remaining = remaining - 1 remaining = remaining - 1
@ -813,7 +817,7 @@ local function request_with_id(client_id, method, params, handler, bufnr)
) )
return return
end end
client.request(method, params, handler, bufnr) client:request(method, params, handler, bufnr)
end end
--- @param item lsp.TypeHierarchyItem|lsp.CallHierarchyItem --- @param item lsp.TypeHierarchyItem|lsp.CallHierarchyItem
@ -880,7 +884,7 @@ local function hierarchy(method)
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
--- @param result lsp.CallHierarchyItem[]|lsp.TypeHierarchyItem[]? --- @param result lsp.CallHierarchyItem[]|lsp.TypeHierarchyItem[]?
client.request(prepare_method, params, function(err, result, ctx) client:request(prepare_method, params, function(err, result, ctx)
if err then if err then
vim.notify(err.message, vim.log.levels.WARN) vim.notify(err.message, vim.log.levels.WARN)
elseif result then elseif result then
@ -1131,8 +1135,8 @@ local function on_code_action_results(results, opts)
local action = choice.action local action = choice.action
local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number') local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number')
if not action.edit and client.supports_method(ms.codeAction_resolve) then if not action.edit and client:supports_method(ms.codeAction_resolve) then
client.request(ms.codeAction_resolve, action, function(err, resolved_action) client:request(ms.codeAction_resolve, action, function(err, resolved_action)
if err then if err then
if action.command then if action.command then
apply_action(action, client, choice.ctx) apply_action(action, client, choice.ctx)
@ -1253,7 +1257,7 @@ function M.code_action(opts)
}) })
end end
client.request(ms.textDocument_codeAction, params, on_result, bufnr) client:request(ms.textDocument_codeAction, params, on_result, bufnr)
end end
end end

View File

@ -219,70 +219,28 @@ local validate = vim.validate
--- @field private registrations table<string,lsp.Registration[]> --- @field private registrations table<string,lsp.Registration[]>
--- @field dynamic_capabilities lsp.DynamicCapabilities --- @field dynamic_capabilities lsp.DynamicCapabilities
--- ---
--- Sends a request to the server.
--- This is a thin wrapper around {client.rpc.request} with some additional
--- checking.
--- If {handler} is not specified and if there's no respective global
--- handler, then an error will occur.
--- Returns: {status}, {client_id}?. {status} is a boolean indicating if
--- the notification was successful. If it is `false`, then it will always
--- be `false` (the client has shutdown).
--- If {status} is `true`, the function returns {request_id} as the second
--- result. You can use this with `client.cancel_request(request_id)` to cancel
--- the request.
--- @field request fun(method: string, params: table?, handler: lsp.Handler?, bufnr: integer?): boolean, integer?
---
--- Sends a request to the server and synchronously waits for the response.
--- This is a wrapper around {client.request}
--- Returns: { err=err, result=result }, a dict, where `err` and `result`
--- come from the |lsp-handler|. On timeout, cancel or error, returns `(nil,
--- err)` where `err` is a string describing the failure reason. If the request
--- was unsuccessful returns `nil`.
--- @field request_sync fun(method: string, params: table?, timeout_ms: integer?, bufnr: integer): {err: lsp.ResponseError|nil, result:any}|nil, string|nil err # a dict
---
--- Sends a notification to an LSP server.
--- Returns: a boolean to indicate if the notification was successful. If
--- it is false, then it will always be false (the client has shutdown).
--- @field notify fun(method: string, params: table?): boolean
---
--- Cancels a request with a given request id.
--- Returns: same as `notify()`.
--- @field cancel_request fun(id: integer): boolean
---
--- Stops a client, optionally with force.
--- By default, it will just ask the server to shutdown without force.
--- If you request to stop a client which has previously been requested to
--- shutdown, it will automatically escalate and force shutdown.
--- @field stop fun(force?: boolean)
---
--- Runs the on_attach function from the client's config if it was defined.
--- Useful for buffer-local setup.
--- @field on_attach fun(bufnr: integer)
---
--- @field private _before_init_cb? vim.lsp.client.before_init_cb --- @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_attach_cbs vim.lsp.client.on_attach_cb[]
--- @field private _on_init_cbs vim.lsp.client.on_init_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_exit_cbs vim.lsp.client.on_exit_cb[]
--- @field private _on_error_cb? fun(code: integer, err: string) --- @field private _on_error_cb? fun(code: integer, err: string)
---
--- Checks if a client supports a given method.
--- Always returns true for unknown off-spec methods.
--- {opts} is a optional `{bufnr?: integer}` table.
--- Some language server capabilities can be file specific.
--- @field supports_method fun(method: string, opts?: {bufnr: integer?}): boolean
---
--- Checks whether a client is stopped.
--- Returns: true if the client is fully stopped.
--- @field is_stopped fun(): boolean
local Client = {} local Client = {}
Client.__index = Client Client.__index = Client
--- @param cls table --- @param obj table<string,any>
--- @param meth any --- @param cls table<string,function>
--- @return function --- @param name string
local function method_wrapper(cls, meth) local function method_wrapper(obj, cls, name)
return function(...) local meth = assert(cls[name])
return meth(cls, ...) obj[name] = function(...)
local arg = select(1, ...)
if arg and getmetatable(arg) == cls then
-- First argument is self, call meth directly
return meth(...)
end
vim.deprecate('client.' .. name, 'client:' .. name, '0.13')
-- First argument is not self, insert it
return meth(obj, ...)
end end
end end
@ -499,24 +457,23 @@ function Client.create(config)
end, end,
} }
self.request = method_wrapper(self, Client._request)
self.request_sync = method_wrapper(self, Client._request_sync)
self.notify = method_wrapper(self, Client._notify)
self.cancel_request = method_wrapper(self, Client._cancel_request)
self.stop = method_wrapper(self, Client._stop)
self.is_stopped = method_wrapper(self, Client._is_stopped)
self.on_attach = method_wrapper(self, Client._on_attach)
self.supports_method = method_wrapper(self, Client._supports_method)
--- @type table<string|integer, string> title of unfinished progress sequences by token --- @type table<string|integer, string> title of unfinished progress sequences by token
self.progress.pending = {} self.progress.pending = {}
--- @type vim.lsp.rpc.Dispatchers --- @type vim.lsp.rpc.Dispatchers
local dispatchers = { local dispatchers = {
notification = method_wrapper(self, Client._notification), notification = function(...)
server_request = method_wrapper(self, Client._server_request), return self:_notification(...)
on_error = method_wrapper(self, Client._on_error), end,
on_exit = method_wrapper(self, Client._on_exit), server_request = function(...)
return self:_server_request(...)
end,
on_error = function(...)
return self:_on_error(...)
end,
on_exit = function(...)
return self:_on_exit(...)
end,
} }
-- Start the RPC client. -- Start the RPC client.
@ -533,6 +490,15 @@ function Client.create(config)
setmetatable(self, Client) setmetatable(self, Client)
method_wrapper(self, Client, 'request')
method_wrapper(self, Client, 'request_sync')
method_wrapper(self, Client, 'notify')
method_wrapper(self, Client, 'cancel_request')
method_wrapper(self, Client, 'stop')
method_wrapper(self, Client, 'is_stopped')
method_wrapper(self, Client, 'on_attach')
method_wrapper(self, Client, 'supports_method')
return self return self
end end
@ -616,7 +582,7 @@ function Client:initialize()
end end
if next(self.settings) then if next(self.settings) then
self:_notify(ms.workspace_didChangeConfiguration, { settings = self.settings }) self:notify(ms.workspace_didChangeConfiguration, { settings = self.settings })
end end
-- If server is being restarted, make sure to re-attach to any previously attached buffers. -- If server is being restarted, make sure to re-attach to any previously attached buffers.
@ -628,7 +594,7 @@ function Client:initialize()
for buf in pairs(reattach_bufs) do for buf in pairs(reattach_bufs) do
-- The buffer may have been detached in the on_init callback. -- The buffer may have been detached in the on_init callback.
if self.attached_buffers[buf] then if self.attached_buffers[buf] then
self:_on_attach(buf) self:on_attach(buf)
end end
end end
@ -645,14 +611,14 @@ end
--- Returns the default handler if the user hasn't set a custom one. --- Returns the default handler if the user hasn't set a custom one.
--- ---
--- @param method (string) LSP method name --- @param method (string) LSP method name
--- @return lsp.Handler|nil handler for the given method, if defined, or the default from |vim.lsp.handlers| --- @return lsp.Handler? handler for the given method, if defined, or the default from |vim.lsp.handlers|
function Client:_resolve_handler(method) function Client:_resolve_handler(method)
return self.handlers[method] or lsp.handlers[method] return self.handlers[method] or lsp.handlers[method]
end end
--- Returns the buffer number for the given {bufnr}. --- Returns the buffer number for the given {bufnr}.
--- ---
--- @param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer --- @param bufnr integer? Buffer number to resolve. Defaults to current buffer
--- @return integer bufnr --- @return integer bufnr
local function resolve_bufnr(bufnr) local function resolve_bufnr(bufnr)
validate('bufnr', bufnr, 'number', true) validate('bufnr', bufnr, 'number', true)
@ -662,7 +628,6 @@ local function resolve_bufnr(bufnr)
return bufnr return bufnr
end end
--- @private
--- Sends a request to the server. --- Sends a request to the server.
--- ---
--- This is a thin wrapper around {client.rpc.request} with some additional --- This is a thin wrapper around {client.rpc.request} with some additional
@ -671,15 +636,14 @@ end
--- @param method string LSP method name. --- @param method string LSP method name.
--- @param params? table LSP request params. --- @param params? table LSP request params.
--- @param handler? lsp.Handler Response |lsp-handler| for this method. --- @param handler? lsp.Handler Response |lsp-handler| for this method.
--- @param bufnr integer Buffer handle (0 for current). --- @param bufnr? integer Buffer handle. 0 for current (default).
--- @return boolean status, integer? request_id {status} is a bool indicating --- @return boolean status indicates whether the request was successful.
--- whether the request was successful. If it is `false`, then it will --- If it is `false`, then it will always be `false` (the client has shutdown).
--- always be `false` (the client has shutdown). If it was --- @return integer? request_id Can be used with |Client:cancel_request()|.
--- successful, then it will return {request_id} as the --- `nil` is request failed.
--- second result. You can use this with `client.cancel_request(request_id)`
--- to cancel the-request. --- to cancel the-request.
--- @see |vim.lsp.buf_request_all()| --- @see |vim.lsp.buf_request_all()|
function Client:_request(method, params, handler, bufnr) function Client:request(method, params, handler, bufnr)
if not handler then if not handler then
handler = assert( handler = assert(
self:_resolve_handler(method), self:_resolve_handler(method),
@ -688,8 +652,8 @@ function Client:_request(method, params, handler, bufnr)
end end
-- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state -- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state
changetracking.flush(self, bufnr) changetracking.flush(self, bufnr)
local version = lsp.util.buf_versions[bufnr]
bufnr = resolve_bufnr(bufnr) bufnr = resolve_bufnr(bufnr)
local version = lsp.util.buf_versions[bufnr]
log.debug(self._log_prefix, 'client.request', self.id, method, params, handler, bufnr) log.debug(self._log_prefix, 'client.request', self.id, method, params, handler, bufnr)
local success, request_id = self.rpc.request(method, params, function(err, result) local success, request_id = self.rpc.request(method, params, function(err, result)
local context = { local context = {
@ -743,29 +707,27 @@ local function err_message(...)
end end
end end
--- @private
--- Sends a request to the server and synchronously waits for the response. --- Sends a request to the server and synchronously waits for the response.
--- ---
--- This is a wrapper around {client.request} --- This is a wrapper around |Client:request()|
--- ---
--- @param method (string) LSP method name. --- @param method string LSP method name.
--- @param params (table) LSP request params. --- @param params table LSP request params.
--- @param timeout_ms (integer|nil) Maximum time in milliseconds to wait for --- @param timeout_ms integer? Maximum time in milliseconds to wait for
--- a result. Defaults to 1000 --- a result. Defaults to 1000
--- @param bufnr (integer) Buffer handle (0 for current). --- @param bufnr integer Buffer handle (0 for current).
--- @return {err: lsp.ResponseError|nil, result:any}|nil, string|nil err # a dict, where --- @return {err: lsp.ResponseError?, result:any}? `result` and `err` from the |lsp-handler|.
--- `err` and `result` come from the |lsp-handler|. --- `nil` is the request was unsuccessful
--- On timeout, cancel or error, returns `(nil, err)` where `err` is a --- @return string? err On timeout, cancel or error, where `err` is a
--- string describing the failure reason. If the request was unsuccessful --- string describing the failure reason.
--- returns `nil`.
--- @see |vim.lsp.buf_request_sync()| --- @see |vim.lsp.buf_request_sync()|
function Client:_request_sync(method, params, timeout_ms, bufnr) function Client:request_sync(method, params, timeout_ms, bufnr)
local request_result = nil local request_result = nil
local function _sync_handler(err, result) local function _sync_handler(err, result)
request_result = { err = err, result = result } request_result = { err = err, result = result }
end end
local success, request_id = self:_request(method, params, _sync_handler, bufnr) local success, request_id = self:request(method, params, _sync_handler, bufnr)
if not success then if not success then
return nil return nil
end end
@ -776,22 +738,20 @@ function Client:_request_sync(method, params, timeout_ms, bufnr)
if not wait_result then if not wait_result then
if request_id then if request_id then
self:_cancel_request(request_id) self:cancel_request(request_id)
end end
return nil, wait_result_reason[reason] return nil, wait_result_reason[reason]
end end
return request_result return request_result
end end
--- @package
--- Sends a notification to an LSP server. --- Sends a notification to an LSP server.
--- ---
--- @param method string LSP method name. --- @param method string LSP method name.
--- @param params table|nil LSP request params. --- @param params table? LSP request params.
--- @return boolean status true if the notification was successful. --- @return boolean status indicating if the notification was successful.
--- If it is false, then it will always be false --- If it is false, then the client has shutdown.
--- (the client has shutdown). function Client:notify(method, params)
function Client:_notify(method, params)
if method ~= ms.textDocument_didChange then if method ~= ms.textDocument_didChange then
changetracking.flush(self) changetracking.flush(self)
end end
@ -814,13 +774,12 @@ function Client:_notify(method, params)
return client_active return client_active
end end
--- @private
--- Cancels a request with a given request id. --- Cancels a request with a given request id.
--- ---
--- @param id (integer) id of request to cancel --- @param id integer id of request to cancel
--- @return boolean status true if notification was successful. false otherwise --- @return boolean status indicating if the notification was successful.
--- @see |vim.lsp.client.notify()| --- @see |Client:notify()|
function Client:_cancel_request(id) function Client:cancel_request(id)
validate('id', id, 'number') validate('id', id, 'number')
local request = self.requests[id] local request = self.requests[id]
if request and request.type == 'pending' then if request and request.type == 'pending' then
@ -834,15 +793,14 @@ function Client:_cancel_request(id)
return self.rpc.notify(ms.dollar_cancelRequest, { id = id }) return self.rpc.notify(ms.dollar_cancelRequest, { id = id })
end end
--- @private
--- Stops a client, optionally with force. --- Stops a client, optionally with force.
--- ---
--- By default, it will just ask the - server to shutdown without force. If --- By default, it will just request the server to shutdown without force. If
--- 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 boolean|nil --- @param force? boolean
function Client:_stop(force) function Client:stop(force)
local rpc = self.rpc local rpc = self.rpc
if rpc.is_closing() then if rpc.is_closing() then
@ -966,12 +924,11 @@ function Client:_get_registration(method, bufnr)
end end
end end
--- @private
--- Checks whether a client is stopped. --- Checks whether a client is stopped.
--- ---
--- @return boolean # true if client is stopped or in the process of being --- @return boolean # 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 self.rpc.is_closing() return self.rpc.is_closing()
end end
@ -1013,7 +970,7 @@ function Client:exec_cmd(command, context, handler)
command = cmdname, command = cmdname,
arguments = command.arguments, arguments = command.arguments,
} }
self.request(ms.workspace_executeCommand, params, handler, context.bufnr) self:request(ms.workspace_executeCommand, params, handler, context.bufnr)
end end
--- Default handler for the 'textDocument/didOpen' LSP notification. --- Default handler for the 'textDocument/didOpen' LSP notification.
@ -1021,14 +978,14 @@ end
--- @param bufnr integer Number of the buffer, or 0 for current --- @param bufnr integer Number of the buffer, or 0 for current
function Client:_text_document_did_open_handler(bufnr) function Client:_text_document_did_open_handler(bufnr)
changetracking.init(self, bufnr) changetracking.init(self, bufnr)
if not self.supports_method(ms.textDocument_didOpen) then if not self:supports_method(ms.textDocument_didOpen) then
return return
end end
if not api.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
return return
end end
self.notify(ms.textDocument_didOpen, { self:notify(ms.textDocument_didOpen, {
textDocument = { textDocument = {
version = lsp.util.buf_versions[bufnr], version = lsp.util.buf_versions[bufnr],
uri = vim.uri_from_bufnr(bufnr), uri = vim.uri_from_bufnr(bufnr),
@ -1049,8 +1006,9 @@ function Client:_text_document_did_open_handler(bufnr)
end end
--- 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.
--- Useful for buffer-local setup.
--- @param bufnr integer Buffer number --- @param bufnr integer Buffer number
function Client:_on_attach(bufnr) function Client:on_attach(bufnr)
self:_text_document_did_open_handler(bufnr) self:_text_document_did_open_handler(bufnr)
lsp._set_defaults(self, bufnr) lsp._set_defaults(self, bufnr)
@ -1085,10 +1043,18 @@ function Client:write_error(code, err)
err_message(self._log_prefix, ': Error ', client_error, ': ', vim.inspect(err)) err_message(self._log_prefix, ': Error ', client_error, ': ', vim.inspect(err))
end end
--- @private --- Checks if a client supports a given method.
--- Always returns true for unknown off-spec methods.
---
--- Note: Some language server capabilities can be file specific.
--- @param method string --- @param method string
--- @param opts? {bufnr: integer?} --- @param bufnr? integer
function Client:_supports_method(method, opts) function Client:supports_method(method, bufnr)
-- Deprecated form
if type(bufnr) == 'table' then
--- @diagnostic disable-next-line:no-unknown
bufnr = bufnr.bufnr
end
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
@ -1101,12 +1067,12 @@ function Client:_supports_method(method, opts)
local rmethod = lsp._resolve_to_request[method] local rmethod = lsp._resolve_to_request[method]
if rmethod then if rmethod then
if self:_supports_registration(rmethod) then if self:_supports_registration(rmethod) then
local reg = self:_get_registration(rmethod, opts and opts.bufnr) local reg = self:_get_registration(rmethod, bufnr)
return vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider') or false return vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider') or false
end end
else else
if self:_supports_registration(method) then if self:_supports_registration(method) then
return self:_get_registration(method, opts and opts.bufnr) ~= nil return self:_get_registration(method, bufnr) ~= nil
end end
end end
return false return false
@ -1207,7 +1173,7 @@ function Client:_add_workspace_folder(dir)
local wf = assert(get_workspace_folders(dir)) local wf = assert(get_workspace_folders(dir))
self:_notify(ms.workspace_didChangeWorkspaceFolders, { self:notify(ms.workspace_didChangeWorkspaceFolders, {
event = { added = wf, removed = {} }, event = { added = wf, removed = {} },
}) })
@ -1222,7 +1188,7 @@ end
function Client:_remove_workspace_folder(dir) function Client:_remove_workspace_folder(dir)
local wf = assert(get_workspace_folders(dir)) local wf = assert(get_workspace_folders(dir))
self:_notify(ms.workspace_didChangeWorkspaceFolders, { self:notify(ms.workspace_didChangeWorkspaceFolders, {
event = { added = {}, removed = wf }, event = { added = {}, removed = wf },
}) })

View File

@ -231,7 +231,7 @@ local function resolve_lenses(lenses, bufnr, client_id, callback)
countdown() countdown()
else else
assert(client) assert(client)
client.request(ms.codeLens_resolve, lens, function(_, result) client:request(ms.codeLens_resolve, lens, function(_, result)
if api.nvim_buf_is_loaded(bufnr) and result and result.command then if api.nvim_buf_is_loaded(bufnr) and result and result.command then
lens.command = result.command lens.command = result.command
-- Eager display to have some sort of incremental feedback -- Eager display to have some sort of incremental feedback

View File

@ -404,7 +404,7 @@ local function request(clients, bufnr, win, callback)
for _, client in pairs(clients) do for _, client in pairs(clients) do
local client_id = client.id local client_id = client.id
local params = lsp.util.make_position_params(win, client.offset_encoding) local params = lsp.util.make_position_params(win, client.offset_encoding)
local ok, request_id = client.request(ms.textDocument_completion, params, function(err, result) local ok, request_id = client:request(ms.textDocument_completion, params, function(err, result)
responses[client_id] = { err = err, result = result } responses[client_id] = { err = err, result = result }
remaining_requests = remaining_requests - 1 remaining_requests = remaining_requests - 1
if remaining_requests == 0 then if remaining_requests == 0 then
@ -421,7 +421,7 @@ local function request(clients, bufnr, win, callback)
for client_id, request_id in pairs(request_ids) do for client_id, request_id in pairs(request_ids) do
local client = lsp.get_client_by_id(client_id) local client = lsp.get_client_by_id(client_id)
if client then if client then
client.cancel_request(request_id) client:cancel_request(request_id)
end end
end end
end end
@ -582,7 +582,7 @@ local function on_complete_done()
local changedtick = vim.b[bufnr].changedtick local changedtick = vim.b[bufnr].changedtick
--- @param result lsp.CompletionItem --- @param result lsp.CompletionItem
client.request(ms.completionItem_resolve, completion_item, function(err, result) client:request(ms.completionItem_resolve, completion_item, function(err, result)
if changedtick ~= vim.b[bufnr].changedtick then if changedtick ~= vim.b[bufnr].changedtick then
return return
end end

View File

@ -122,12 +122,12 @@ end
--- local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer --- local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer
--- ---
--- local client = vim.lsp.get_client_by_id(hint.client_id) --- local client = vim.lsp.get_client_by_id(hint.client_id)
--- local resp = client.request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0) --- local resp = client:request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0)
--- local resolved_hint = assert(resp and resp.result, resp.err) --- local resolved_hint = assert(resp and resp.result, resp.err)
--- vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding) --- vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding)
--- ---
--- location = resolved_hint.label[1].location --- location = resolved_hint.label[1].location
--- client.request('textDocument/hover', { --- client:request('textDocument/hover', {
--- textDocument = { uri = location.uri }, --- textDocument = { uri = location.uri },
--- position = location.range.start, --- position = location.range.start,
--- }) --- })

View File

@ -273,7 +273,7 @@ function STHighlighter:send_request()
if client and current_result.version ~= version and active_request.version ~= version then if client and current_result.version ~= version and active_request.version ~= version then
-- cancel stale in-flight request -- cancel stale in-flight request
if active_request.request_id then if active_request.request_id then
client.cancel_request(active_request.request_id) client:cancel_request(active_request.request_id)
active_request = {} active_request = {}
state.active_request = active_request state.active_request = active_request
end end
@ -288,7 +288,7 @@ function STHighlighter:send_request()
method = method .. '/delta' method = method .. '/delta'
params.previousResultId = current_result.result_id params.previousResultId = current_result.result_id
end end
local success, request_id = client.request(method, params, function(err, response, ctx) local success, request_id = client:request(method, params, function(err, response, ctx)
-- look client up again using ctx.client_id instead of using a captured -- look client up again using ctx.client_id instead of using a captured
-- client object -- client object
local c = vim.lsp.get_client_by_id(ctx.client_id) local c = vim.lsp.get_client_by_id(ctx.client_id)
@ -519,7 +519,7 @@ function STHighlighter:reset()
if state.active_request.request_id then if state.active_request.request_id then
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
assert(client) assert(client)
client.cancel_request(state.active_request.request_id) client:cancel_request(state.active_request.request_id)
state.active_request = {} state.active_request = {}
end end
end end
@ -547,7 +547,7 @@ function STHighlighter:mark_dirty(client_id)
if state.active_request.request_id then if state.active_request.request_id then
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
assert(client) assert(client)
client.cancel_request(state.active_request.request_id) client:cancel_request(state.active_request.request_id)
state.active_request = {} state.active_request = {}
end end
end end

View File

@ -2122,7 +2122,7 @@ function M._refresh(method, opts)
local first = vim.fn.line('w0', window) local first = vim.fn.line('w0', window)
local last = vim.fn.line('w$', window) local last = vim.fn.line('w$', window)
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
client.request(method, { client:request(method, {
textDocument = textDocument, textDocument = textDocument,
range = make_line_range_params(bufnr, first - 1, last - 1, client.offset_encoding), range = make_line_range_params(bufnr, first - 1, last - 1, client.offset_encoding),
}, nil, bufnr) }, nil, bufnr)
@ -2131,7 +2131,7 @@ function M._refresh(method, opts)
end end
else else
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
client.request(method, { client:request(method, {
textDocument = textDocument, textDocument = textDocument,
range = make_line_range_params( range = make_line_range_params(
bufnr, bufnr,

View File

@ -538,7 +538,8 @@ end
--- @param generics? table<string,string> --- @param generics? table<string,string>
--- @param classes? table<string,nvim.luacats.parser.class> --- @param classes? table<string,nvim.luacats.parser.class>
--- @param exclude_types? true --- @param exclude_types? true
local function render_fields_or_params(xs, generics, classes, exclude_types) --- @param cfg nvim.gen_vimdoc.Config
local function render_fields_or_params(xs, generics, classes, exclude_types, cfg)
local ret = {} --- @type string[] local ret = {} --- @type string[]
xs = vim.tbl_filter(should_render_field_or_param, xs) xs = vim.tbl_filter(should_render_field_or_param, xs)
@ -558,7 +559,9 @@ local function render_fields_or_params(xs, generics, classes, exclude_types)
p.desc = pdesc p.desc = pdesc
inline_type(p, classes) inline_type(p, classes)
local nm, ty, desc = p.name, p.type, p.desc local nm, ty = p.name, p.type
local desc = p.classvar and string.format('See |%s|.', cfg.fn_helptag_fmt(p)) or p.desc
local fnm = p.kind == 'operator' and fmt('op(%s)', nm) or fmt_field_name(nm) local fnm = p.kind == 'operator' and fmt('op(%s)', nm) or fmt_field_name(nm)
local pnm = fmt(' • %-' .. indent .. 's', fnm) local pnm = fmt(' • %-' .. indent .. 's', fnm)
@ -591,7 +594,8 @@ end
--- @param class nvim.luacats.parser.class --- @param class nvim.luacats.parser.class
--- @param classes table<string,nvim.luacats.parser.class> --- @param classes table<string,nvim.luacats.parser.class>
local function render_class(class, classes) --- @param cfg nvim.gen_vimdoc.Config
local function render_class(class, classes, cfg)
if class.access or class.nodoc or class.inlinedoc then if class.access or class.nodoc or class.inlinedoc then
return return
end end
@ -610,7 +614,7 @@ local function render_class(class, classes)
table.insert(ret, md_to_vimdoc(class.desc, INDENTATION, INDENTATION, TEXT_WIDTH)) table.insert(ret, md_to_vimdoc(class.desc, INDENTATION, INDENTATION, TEXT_WIDTH))
end end
local fields_txt = render_fields_or_params(class.fields, nil, classes) local fields_txt = render_fields_or_params(class.fields, nil, classes, nil, cfg)
if not fields_txt:match('^%s*$') then if not fields_txt:match('^%s*$') then
table.insert(ret, '\n Fields: ~\n') table.insert(ret, '\n Fields: ~\n')
table.insert(ret, fields_txt) table.insert(ret, fields_txt)
@ -621,11 +625,12 @@ local function render_class(class, classes)
end end
--- @param classes table<string,nvim.luacats.parser.class> --- @param classes table<string,nvim.luacats.parser.class>
local function render_classes(classes) --- @param cfg nvim.gen_vimdoc.Config
local function render_classes(classes, cfg)
local ret = {} --- @type string[] local ret = {} --- @type string[]
for _, class in vim.spairs(classes) do for _, class in vim.spairs(classes) do
ret[#ret + 1] = render_class(class, classes) ret[#ret + 1] = render_class(class, classes, cfg)
end end
return table.concat(ret) return table.concat(ret)
@ -656,10 +661,6 @@ local function render_fun_header(fun, cfg)
local proto = fun.table and nm or nm .. '(' .. table.concat(args, ', ') .. ')' local proto = fun.table and nm or nm .. '(' .. table.concat(args, ', ') .. ')'
if not cfg.fn_helptag_fmt then
cfg.fn_helptag_fmt = fn_helptag_fmt_common
end
local tag = '*' .. cfg.fn_helptag_fmt(fun) .. '*' local tag = '*' .. cfg.fn_helptag_fmt(fun) .. '*'
if #proto + #tag > TEXT_WIDTH - 8 then if #proto + #tag > TEXT_WIDTH - 8 then
@ -774,7 +775,8 @@ local function render_fun(fun, classes, cfg)
end end
if fun.params and #fun.params > 0 then if fun.params and #fun.params > 0 then
local param_txt = render_fields_or_params(fun.params, fun.generics, classes, cfg.exclude_types) local param_txt =
render_fields_or_params(fun.params, fun.generics, classes, cfg.exclude_types, cfg)
if not param_txt:match('^%s*$') then if not param_txt:match('^%s*$') then
table.insert(ret, '\n Parameters: ~\n') table.insert(ret, '\n Parameters: ~\n')
ret[#ret + 1] = param_txt ret[#ret + 1] = param_txt
@ -957,6 +959,7 @@ end
--- @param cfg nvim.gen_vimdoc.Config --- @param cfg nvim.gen_vimdoc.Config
local function gen_target(cfg) local function gen_target(cfg)
cfg.fn_helptag_fmt = cfg.fn_helptag_fmt or fn_helptag_fmt_common
print('Target:', cfg.filename) print('Target:', cfg.filename)
local sections = {} --- @type table<string,nvim.gen_vimdoc.Section> local sections = {} --- @type table<string,nvim.gen_vimdoc.Section>
@ -987,7 +990,7 @@ local function gen_target(cfg)
print(' Processing file:', f) print(' Processing file:', f)
local funs_txt = render_funs(funs, all_classes, cfg) local funs_txt = render_funs(funs, all_classes, cfg)
if next(classes) then if next(classes) then
local classes_txt = render_classes(classes) local classes_txt = render_classes(classes, cfg)
if vim.trim(classes_txt) ~= '' then if vim.trim(classes_txt) ~= '' then
funs_txt = classes_txt .. '\n' .. funs_txt funs_txt = classes_txt .. '\n' .. funs_txt
end end

View File

@ -1,9 +1,6 @@
local luacats_grammar = require('scripts.luacats_grammar') local luacats_grammar = require('scripts.luacats_grammar')
--- @class nvim.luacats.parser.param --- @class nvim.luacats.parser.param : nvim.luacats.Param
--- @field name string
--- @field type string
--- @field desc string
--- @class nvim.luacats.parser.return --- @class nvim.luacats.parser.return
--- @field name string --- @field name string
@ -41,21 +38,14 @@ local luacats_grammar = require('scripts.luacats_grammar')
--- @field notes? nvim.luacats.parser.note[] --- @field notes? nvim.luacats.parser.note[]
--- @field see? nvim.luacats.parser.note[] --- @field see? nvim.luacats.parser.note[]
--- @class nvim.luacats.parser.field --- @class nvim.luacats.parser.field : nvim.luacats.Field
--- @field name string --- @field classvar? string
--- @field type string
--- @field desc string
--- @field access? 'private'|'package'|'protected'
--- @field nodoc? true --- @field nodoc? true
--- @class nvim.luacats.parser.class --- @class nvim.luacats.parser.class : nvim.luacats.Class
--- @field kind 'class' --- @field desc? string
--- @field parent? string
--- @field name string
--- @field desc string
--- @field nodoc? true --- @field nodoc? true
--- @field inlinedoc? true --- @field inlinedoc? true
--- @field access? 'private'|'package'|'protected'
--- @field fields nvim.luacats.parser.field[] --- @field fields nvim.luacats.parser.field[]
--- @field notes? string[] --- @field notes? string[]
@ -332,7 +322,10 @@ local function process_lua_line(line, state, classes, classvars, has_indent)
end end
-- Add method as the field to the class -- Add method as the field to the class
table.insert(classes[class].fields, fun2field(cur_obj)) local cls = classes[class]
local field = fun2field(cur_obj)
field.classvar = cur_obj.classvar
table.insert(cls.fields, field)
return return
end end

View File

@ -182,16 +182,17 @@ function M.test_rpc_server(config)
) )
end end
local client = setmetatable({}, { local client = setmetatable({}, {
__index = function(_, name) __index = function(t, name)
-- Workaround for not being able to yield() inside __index for Lua 5.1 :( -- Workaround for not being able to yield() inside __index for Lua 5.1 :(
-- Otherwise I would just return the value here. -- Otherwise I would just return the value here.
return function(...) return function(arg1, ...)
local ismethod = arg1 == t
return exec_lua(function(...) return exec_lua(function(...)
if type(_G.TEST_RPC_CLIENT[name]) == 'function' then local client = _G.TEST_RPC_CLIENT
return _G.TEST_RPC_CLIENT[name](...) if type(client[name]) == 'function' then
else return client[name](ismethod and client or arg1, ...)
return _G.TEST_RPC_CLIENT[name]
end end
return client[name]
end, ...) end, ...)
end end
end, end,

View File

@ -232,7 +232,7 @@ describe('LSP', function()
-- client is a dummy object which will queue up commands to be run -- client is a dummy object which will queue up commands to be run
-- once the server initializes. It can't accept lua callbacks or -- once the server initializes. It can't accept lua callbacks or
-- other types that may be unserializable for now. -- other types that may be unserializable for now.
client.stop() client:stop()
end, end,
-- If the program timed out, then code will be nil. -- If the program timed out, then code will be nil.
on_exit = function(code, signal) on_exit = function(code, signal)
@ -254,8 +254,8 @@ describe('LSP', function()
test_rpc_server { test_rpc_server {
test_name = 'basic_init', test_name = 'basic_init',
on_init = function(client) on_init = function(client)
client.notify('test') client:notify('test')
client.stop() client:stop()
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(101, code, 'exit code') -- See fake-lsp-server.lua eq(101, code, 'exit code') -- See fake-lsp-server.lua
@ -275,7 +275,7 @@ describe('LSP', function()
test_rpc_server({ test_rpc_server({
test_name = 'basic_init_did_change_configuration', test_name = 'basic_init_did_change_configuration',
on_init = function(client, _) on_init = function(client, _)
client.stop() client:stop()
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -333,9 +333,9 @@ describe('LSP', function()
test_name = 'basic_init', test_name = 'basic_init',
on_init = function(client) on_init = function(client)
eq(0, client.server_capabilities().textDocumentSync.change) eq(0, client.server_capabilities().textDocumentSync.change)
client.request('shutdown') client:request('shutdown')
client.notify('exit') client:notify('exit')
client.stop() client:stop()
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -377,7 +377,7 @@ describe('LSP', function()
end, end,
on_init = function(_client) on_init = function(_client)
client = _client client = _client
client.notify('finish') client:notify('finish')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -395,7 +395,7 @@ describe('LSP', function()
return vim.lsp.buf_is_attached(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) return vim.lsp.buf_is_attached(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)
end) end)
) )
client.stop() client:stop()
end end
end, end,
} }
@ -430,7 +430,7 @@ describe('LSP', function()
return vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) return vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)
end) end)
) )
client.notify('finish') client:notify('finish')
end, end,
on_handler = function(_, _, ctx) on_handler = function(_, _, ctx)
if ctx.method == 'finish' then if ctx.method == 'finish' then
@ -439,7 +439,7 @@ describe('LSP', function()
return vim.lsp.buf_detach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID) return vim.lsp.buf_detach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)
end) end)
eq('basic_init', api.nvim_get_var('lsp_detached')) eq('basic_init', api.nvim_get_var('lsp_detached'))
client.stop() client:stop()
end end
end, end,
} }
@ -472,7 +472,7 @@ describe('LSP', function()
return keymap.callback == vim.lsp.buf.hover return keymap.callback == vim.lsp.buf.hover
end) end)
) )
client.stop() client:stop()
end end
end, end,
on_exit = function(_, _) on_exit = function(_, _)
@ -524,7 +524,7 @@ describe('LSP', function()
eq('v:lua.vim.lsp.tagfunc', get_buf_option('tagfunc', BUFFER_1)) eq('v:lua.vim.lsp.tagfunc', get_buf_option('tagfunc', BUFFER_1))
eq('v:lua.vim.lsp.omnifunc', get_buf_option('omnifunc', BUFFER_2)) eq('v:lua.vim.lsp.omnifunc', get_buf_option('omnifunc', BUFFER_2))
eq('v:lua.vim.lsp.formatexpr()', get_buf_option('formatexpr', BUFFER_2)) eq('v:lua.vim.lsp.formatexpr()', get_buf_option('formatexpr', BUFFER_2))
client.stop() client:stop()
end end
end, end,
on_exit = function(_, _) on_exit = function(_, _)
@ -554,7 +554,7 @@ describe('LSP', function()
eq('tfu', get_buf_option('tagfunc')) eq('tfu', get_buf_option('tagfunc'))
eq('ofu', get_buf_option('omnifunc')) eq('ofu', get_buf_option('omnifunc'))
eq('fex', get_buf_option('formatexpr')) eq('fex', get_buf_option('formatexpr'))
client.stop() client:stop()
end end
end, end,
on_exit = function(_, _) on_exit = function(_, _)
@ -711,10 +711,10 @@ describe('LSP', function()
ctx.method, ctx.method,
result result
) )
client.notify('workspace/configuration', server_result) client:notify('workspace/configuration', server_result)
end end
if ctx.method == 'shutdown' then if ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -756,7 +756,7 @@ describe('LSP', function()
test_rpc_server { test_rpc_server {
test_name = 'basic_check_capabilities', test_name = 'basic_check_capabilities',
on_init = function(client) on_init = function(client)
client.stop() client:stop()
local full_kind = exec_lua(function() local full_kind = exec_lua(function()
return require 'vim.lsp.protocol'.TextDocumentSyncKind.Full return require 'vim.lsp.protocol'.TextDocumentSyncKind.Full
end) end)
@ -798,7 +798,7 @@ describe('LSP', function()
vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false }) vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false })
end) end)
else else
client.stop() client:stop()
end end
end, end,
} }
@ -898,7 +898,7 @@ describe('LSP', function()
end) end)
end) end)
else else
client.stop() client:stop()
end end
end, end,
}) })
@ -929,20 +929,20 @@ describe('LSP', function()
vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false }) vim.api.nvim_exec_autocmds('BufWritePost', { buffer = _G.BUFFER, modeline = false })
end) end)
else else
client.stop() client:stop()
end end
end, end,
} }
end) end)
it('client.supports_methods() should validate capabilities', function() it('client:supports_methods() should validate capabilities', function()
local expected_handlers = { local expected_handlers = {
{ NIL, {}, { method = 'shutdown', client_id = 1 } }, { NIL, {}, { method = 'shutdown', client_id = 1 } },
} }
test_rpc_server { test_rpc_server {
test_name = 'capabilities_for_client_supports_method', test_name = 'capabilities_for_client_supports_method',
on_init = function(client) on_init = function(client)
client.stop() client:stop()
local expected_sync_capabilities = { local expected_sync_capabilities = {
change = 1, change = 1,
openClose = true, openClose = true,
@ -958,11 +958,11 @@ describe('LSP', function()
eq(true, client.server_capabilities().codeLensProvider.resolveProvider) eq(true, client.server_capabilities().codeLensProvider.resolveProvider)
-- known methods for resolved capabilities -- known methods for resolved capabilities
eq(true, client.supports_method('textDocument/hover')) eq(true, client:supports_method('textDocument/hover'))
eq(false, client.supports_method('textDocument/definition')) eq(false, client:supports_method('textDocument/definition'))
-- unknown methods are assumed to be supported. -- unknown methods are assumed to be supported.
eq(true, client.supports_method('unknown-method')) eq(true, client:supports_method('unknown-method'))
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -989,7 +989,7 @@ describe('LSP', function()
end) end)
end, end,
on_init = function(client) on_init = function(client)
client.stop() client:stop()
exec_lua(function() exec_lua(function()
vim.lsp.buf.type_definition() vim.lsp.buf.type_definition()
end) end)
@ -1018,7 +1018,7 @@ describe('LSP', function()
end) end)
end, end,
on_init = function(client) on_init = function(client)
client.stop() client:stop()
exec_lua(function() exec_lua(function()
vim.lsp.buf.type_definition() vim.lsp.buf.type_definition()
end) end)
@ -1042,7 +1042,7 @@ describe('LSP', function()
test_rpc_server { test_rpc_server {
test_name = 'check_forward_request_cancelled', test_name = 'check_forward_request_cancelled',
on_init = function(_client) on_init = function(_client)
_client.request('error_code_test') _client:request('error_code_test')
client = _client client = _client
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
@ -1053,7 +1053,7 @@ describe('LSP', function()
on_handler = function(err, _, ctx) on_handler = function(err, _, ctx)
eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1072,7 +1072,7 @@ describe('LSP', function()
test_rpc_server { test_rpc_server {
test_name = 'check_forward_content_modified', test_name = 'check_forward_content_modified',
on_init = function(_client) on_init = function(_client)
_client.request('error_code_test') _client:request('error_code_test')
client = _client client = _client
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
@ -1084,10 +1084,10 @@ describe('LSP', function()
eq(table.remove(expected_handlers), { err, _, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, _, ctx }, 'expected handler')
-- if ctx.method == 'error_code_test' then client.notify("finish") end -- if ctx.method == 'error_code_test' then client.notify("finish") end
if ctx.method ~= 'finish' then if ctx.method ~= 'finish' then
client.notify('finish') client:notify('finish')
end end
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1103,13 +1103,13 @@ describe('LSP', function()
test_name = 'check_pending_request_tracked', test_name = 'check_pending_request_tracked',
on_init = function(_client) on_init = function(_client)
client = _client client = _client
client.request('slow_request') client:request('slow_request')
local request = exec_lua(function() local request = exec_lua(function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq('slow_request', request.method) eq('slow_request', request.method)
eq('pending', request.type) eq('pending', request.type)
client.notify('release') client:notify('release')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1123,10 +1123,10 @@ describe('LSP', function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq(nil, request) eq(nil, request)
client.notify('finish') client:notify('finish')
end end
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1141,14 +1141,14 @@ describe('LSP', function()
test_name = 'check_cancel_request_tracked', test_name = 'check_cancel_request_tracked',
on_init = function(_client) on_init = function(_client)
client = _client client = _client
client.request('slow_request') client:request('slow_request')
client.cancel_request(2) client:cancel_request(2)
local request = exec_lua(function() local request = exec_lua(function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq('slow_request', request.method) eq('slow_request', request.method)
eq('cancel', request.type) eq('cancel', request.type)
client.notify('release') client:notify('release')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1162,7 +1162,7 @@ describe('LSP', function()
end) end)
eq(nil, request) eq(nil, request)
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1178,19 +1178,19 @@ describe('LSP', function()
test_name = 'check_tracked_requests_cleared', test_name = 'check_tracked_requests_cleared',
on_init = function(_client) on_init = function(_client)
client = _client client = _client
client.request('slow_request') client:request('slow_request')
local request = exec_lua(function() local request = exec_lua(function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq('slow_request', request.method) eq('slow_request', request.method)
eq('pending', request.type) eq('pending', request.type)
client.cancel_request(2) client:cancel_request(2)
request = exec_lua(function() request = exec_lua(function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq('slow_request', request.method) eq('slow_request', request.method)
eq('cancel', request.type) eq('cancel', request.type)
client.notify('release') client:notify('release')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1204,10 +1204,10 @@ describe('LSP', function()
return _G.TEST_RPC_CLIENT.requests[2] return _G.TEST_RPC_CLIENT.requests[2]
end) end)
eq(nil, request) eq(nil, request)
client.notify('finish') client:notify('finish')
end end
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1225,11 +1225,11 @@ describe('LSP', function()
command('let g:requests = 0') command('let g:requests = 0')
command('autocmd LspRequest * let g:requests+=1') command('autocmd LspRequest * let g:requests+=1')
client = _client client = _client
client.request('slow_request') client:request('slow_request')
eq(1, eval('g:requests')) eq(1, eval('g:requests'))
client.cancel_request(2) client:cancel_request(2)
eq(2, eval('g:requests')) eq(2, eval('g:requests'))
client.notify('release') client:notify('release')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1240,10 +1240,10 @@ describe('LSP', function()
on_handler = function(err, _, ctx) on_handler = function(err, _, ctx)
eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, {}, ctx }, 'expected handler')
if ctx.method == 'slow_request' then if ctx.method == 'slow_request' then
client.notify('finish') client:notify('finish')
end end
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1277,7 +1277,7 @@ describe('LSP', function()
end) end)
eq(full_kind, client.server_capabilities().textDocumentSync.change) eq(full_kind, client.server_capabilities().textDocumentSync.change)
eq(true, client.server_capabilities().textDocumentSync.openClose) eq(true, client.server_capabilities().textDocumentSync.openClose)
client.notify('finish') client:notify('finish')
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1286,7 +1286,7 @@ describe('LSP', function()
on_handler = function(err, result, ctx) on_handler = function(err, result, ctx)
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1333,11 +1333,11 @@ describe('LSP', function()
end, end,
on_handler = function(err, result, ctx) on_handler = function(err, result, ctx)
if ctx.method == 'start' then if ctx.method == 'start' then
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1378,11 +1378,11 @@ describe('LSP', function()
end, end,
on_handler = function(err, result, ctx) on_handler = function(err, result, ctx)
if ctx.method == 'start' then if ctx.method == 'start' then
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1428,11 +1428,11 @@ describe('LSP', function()
'boop', 'boop',
}) })
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1479,11 +1479,11 @@ describe('LSP', function()
'boop', 'boop',
}) })
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1529,7 +1529,7 @@ describe('LSP', function()
end, end,
on_init = function(_client) on_init = function(_client)
client = _client client = _client
eq(true, client.supports_method('textDocument/inlayHint')) eq(true, client:supports_method('textDocument/inlayHint'))
exec_lua(function() exec_lua(function()
assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID)) assert(vim.lsp.buf_attach_client(_G.BUFFER, _G.TEST_RPC_CLIENT_ID))
end) end)
@ -1545,11 +1545,11 @@ describe('LSP', function()
end) end)
end end
if ctx.method == 'textDocument/inlayHint' then if ctx.method == 'textDocument/inlayHint' then
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1598,11 +1598,11 @@ describe('LSP', function()
'123boop', '123boop',
}) })
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1652,11 +1652,11 @@ describe('LSP', function()
'123boop', '123boop',
}) })
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1699,11 +1699,11 @@ describe('LSP', function()
on_handler = function(err, result, ctx) on_handler = function(err, result, ctx)
if ctx.method == 'start' then if ctx.method == 'start' then
n.command('normal! 1Go') n.command('normal! 1Go')
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1752,11 +1752,11 @@ describe('LSP', function()
'boop', 'boop',
}) })
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1806,11 +1806,11 @@ describe('LSP', function()
}) })
vim.api.nvim_command(_G.BUFFER .. 'bwipeout') vim.api.nvim_command(_G.BUFFER .. 'bwipeout')
end) end)
client.notify('finish') client:notify('finish')
end end
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -1830,7 +1830,7 @@ describe('LSP', function()
on_setup = function() end, on_setup = function() end,
on_init = function(_client) on_init = function(_client)
client = _client client = _client
client.stop(true) client:stop(true)
end, end,
on_exit = function(code, signal) on_exit = function(code, signal)
eq(0, code, 'exit code') eq(0, code, 'exit code')
@ -1882,7 +1882,7 @@ describe('LSP', function()
on_handler = function(err, result, ctx) on_handler = function(err, result, ctx)
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'finish' then if ctx.method == 'finish' then
client.stop() client:stop()
end end
end, end,
} }
@ -2348,7 +2348,7 @@ describe('LSP', function()
test_rpc_server { test_rpc_server {
test_name = 'basic_init', test_name = 'basic_init',
on_init = function(client, _) on_init = function(client, _)
client.stop() client:stop()
end, end,
-- If the program timed out, then code will be nil. -- If the program timed out, then code will be nil.
on_exit = function(code, signal) on_exit = function(code, signal)
@ -4284,7 +4284,7 @@ describe('LSP', function()
end) end)
) )
end end
client.stop() client:stop()
end end
end, end,
} }
@ -4331,7 +4331,7 @@ describe('LSP', function()
return type(vim.lsp.commands['dummy2']) return type(vim.lsp.commands['dummy2'])
end) end)
) )
client.stop() client:stop()
end end
end, end,
} }
@ -4372,7 +4372,7 @@ describe('LSP', function()
vim.lsp.buf.code_action() vim.lsp.buf.code_action()
end) end)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
}) })
@ -4447,7 +4447,7 @@ describe('LSP', function()
return type(vim.lsp.commands['executed_type_annotate']) return type(vim.lsp.commands['executed_type_annotate'])
end) end)
) )
client.stop() client:stop()
end end
end, end,
} }
@ -4564,7 +4564,7 @@ describe('LSP', function()
end) end)
eq({ command = 'Dummy', title = 'Lens1' }, cmd) eq({ command = 'Dummy', title = 'Lens1' }, cmd)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -4653,7 +4653,7 @@ describe('LSP', function()
end) end)
eq({ command = 'Dummy', title = 'Lens2' }, response) eq({ command = 'Dummy', title = 'Lens2' }, response)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -4762,7 +4762,7 @@ describe('LSP', function()
return notify_msg return notify_msg
end) end)
eq('[LSP] Format request failed, no matching language servers.', notify_msg) eq('[LSP] Format request failed, no matching language servers.', notify_msg)
client.stop() client:stop()
end, end,
} }
end) end)
@ -4795,7 +4795,7 @@ describe('LSP', function()
end) end)
eq(nil, notify_msg) eq(nil, notify_msg)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -4836,7 +4836,7 @@ describe('LSP', function()
end) end)
eq(nil, notify_msg) eq(nil, notify_msg)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -4883,7 +4883,7 @@ describe('LSP', function()
end) end)
eq(nil, notify_msg) eq(nil, notify_msg)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -4930,7 +4930,7 @@ describe('LSP', function()
end) end)
eq({ handler_called = true }, result) eq({ handler_called = true }, result)
elseif ctx.method == 'shutdown' then elseif ctx.method == 'shutdown' then
client.stop() client:stop()
end end
end, end,
} }
@ -5477,7 +5477,7 @@ describe('LSP', function()
result[#result + 1] = { result[#result + 1] = {
method = method, method = method,
fname = fname, fname = fname,
supported = client.supports_method(method, { bufnr = bufnr }), supported = client:supports_method(method, { bufnr = bufnr }),
} }
end end