mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
refactor(lsp): buf_request_all
This commit is contained in:
parent
54249d051c
commit
1471dfc859
@ -915,36 +915,30 @@ end
|
||||
---
|
||||
---@param bufnr (integer) Buffer handle, or 0 for current.
|
||||
---@param method (string) LSP method name
|
||||
---@param params (table|nil) Parameters to send to the server
|
||||
---@param handler fun(results: table<integer, {error: lsp.ResponseError?, result: any}>) (function)
|
||||
---@param params? table|(fun(client: vim.lsp.Client, bufnr: integer): table?) Parameters to send to the server
|
||||
---@param handler lsp.MultiHandler (function)
|
||||
--- Handler called after all requests are completed. Server results are passed as
|
||||
--- a `client_id:result` map.
|
||||
---@return function cancel Function that cancels all requests.
|
||||
function lsp.buf_request_all(bufnr, method, params, handler)
|
||||
local results = {} --- @type table<integer,{error: lsp.ResponseError?, result: any}>
|
||||
local result_count = 0
|
||||
local expected_result_count = 0
|
||||
local results = {} --- @type table<integer,{err: lsp.ResponseError?, result: any}>
|
||||
local remaining --- @type integer?
|
||||
|
||||
local set_expected_result_count = once(function()
|
||||
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
|
||||
if client.supports_method(method, { bufnr = bufnr }) then
|
||||
expected_result_count = expected_result_count + 1
|
||||
end
|
||||
local _, cancel = lsp.buf_request(bufnr, method, params, function(err, result, ctx, config)
|
||||
if not remaining then
|
||||
-- Calculate as late as possible in case a client is removed during the request
|
||||
remaining = #lsp.get_clients({ bufnr = bufnr, method = method })
|
||||
end
|
||||
|
||||
-- The error key is deprecated and will be removed in 0.13
|
||||
results[ctx.client_id] = { err = err, error = err, result = result }
|
||||
remaining = remaining - 1
|
||||
|
||||
if remaining == 0 then
|
||||
handler(results, ctx, config)
|
||||
end
|
||||
end)
|
||||
|
||||
local function _sync_handler(err, result, ctx)
|
||||
results[ctx.client_id] = { error = err, result = result }
|
||||
result_count = result_count + 1
|
||||
set_expected_result_count()
|
||||
|
||||
if result_count >= expected_result_count then
|
||||
handler(results)
|
||||
end
|
||||
end
|
||||
|
||||
local _, cancel = lsp.buf_request(bufnr, method, params, _sync_handler)
|
||||
|
||||
return cancel
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user