feat(lsp)!: promote LspRequest to a full autocmd and enrich with additional data (#23694)

BREAKING CHANGE: LspRequest is no longer a User autocmd but is now a
first class citizen.

LspRequest as a User autocmd had limited functionality. Namely, the only
thing you could do was use the notification to do a lookup on all the
clients' requests tables to figure out what changed.

Promoting the autocmd to a full autocmd lets us set the buffer the
request was initiated on (so people can set buffer-local autocmds for
listening to these events).

Additionally, when used from Lua, we can pass additional metadata about
the request along with the notification, including the client ID, the
request ID, and the actual request object stored on the client's
requests table. Users can now listen for these events and act on them
proactively instead of polling all of the requests tables and looking
for changes.
This commit is contained in:
jdrouhard
2023-05-30 13:56:29 -05:00
committed by GitHub
parent dd3fa64573
commit 58618d208a
5 changed files with 75 additions and 13 deletions

View File

@@ -799,7 +799,9 @@ end
--- to the server. Entries are key-value pairs with the key
--- being the request ID while the value is a table with `type`,
--- `bufnr`, and `method` key-value pairs. `type` is either "pending"
--- for an active request, or "cancel" for a cancel request.
--- for an active request, or "cancel" for a cancel request. It will
--- be "complete" ephemerally while executing |LspRequest| autocmds
--- when replies are received from the server.
---
--- - {config} (table): copy of the table that was passed by the user
--- to |vim.lsp.start_client()|.
@@ -1408,13 +1410,24 @@ function lsp.start_client(config)
{ method = method, client_id = client_id, bufnr = bufnr, params = params }
)
end, function(request_id)
local request = client.requests[request_id]
request.type = 'complete'
nvim_exec_autocmds('LspRequest', {
buffer = bufnr,
modeline = false,
data = { client_id = client_id, request_id = request_id, request = request },
})
client.requests[request_id] = nil
nvim_exec_autocmds('User', { pattern = 'LspRequest', modeline = false })
end)
if success and request_id then
client.requests[request_id] = { type = 'pending', bufnr = bufnr, method = method }
nvim_exec_autocmds('User', { pattern = 'LspRequest', modeline = false })
local request = { type = 'pending', bufnr = bufnr, method = method }
client.requests[request_id] = request
nvim_exec_autocmds('LspRequest', {
buffer = bufnr,
modeline = false,
data = { client_id = client_id, request_id = request_id, request = request },
})
end
return success, request_id
@@ -1486,7 +1499,11 @@ function lsp.start_client(config)
local request = client.requests[id]
if request and request.type == 'pending' then
request.type = 'cancel'
nvim_exec_autocmds('User', { pattern = 'LspRequest', modeline = false })
nvim_exec_autocmds('LspRequest', {
buffer = request.bufnr,
modeline = false,
data = { client_id = client_id, request_id = id, request = request },
})
end
return rpc.notify('$/cancelRequest', { id = id })
end