refactor(codelens): flatten State:refresh with a collect closure
This commit is contained in:
+35
-32
@@ -190,7 +190,39 @@ function State:refresh()
|
||||
local params = {
|
||||
textDocument = vim.lsp.util.make_text_document_params(self.buf),
|
||||
}
|
||||
local new_rows = {}
|
||||
|
||||
local function collect(new_rows, client, lens)
|
||||
local row = new_rows[lens.range.start.line]
|
||||
if not row then
|
||||
row = Row.new(lens.range.start.line)
|
||||
new_rows[lens.range.start.line] = row
|
||||
end
|
||||
if lens.command then
|
||||
row:add(lens)
|
||||
return
|
||||
end
|
||||
if
|
||||
not client
|
||||
or not client:supports_method(
|
||||
vim.lsp.protocol.Methods.codeLens_resolve
|
||||
)
|
||||
then
|
||||
return
|
||||
end
|
||||
row:await()
|
||||
client:request(
|
||||
vim.lsp.protocol.Methods.codeLens_resolve,
|
||||
lens,
|
||||
function(_, resolved)
|
||||
if self.generation ~= gen then
|
||||
return
|
||||
end
|
||||
row:resolved(type(resolved) == "table" and resolved or nil)
|
||||
row:render(self.buf)
|
||||
end,
|
||||
self.buf
|
||||
)
|
||||
end
|
||||
|
||||
vim.lsp.buf_request_all(
|
||||
self.buf,
|
||||
@@ -200,41 +232,12 @@ function State:refresh()
|
||||
if self.generation ~= gen then
|
||||
return
|
||||
end
|
||||
local new_rows = {}
|
||||
for client_id, response in pairs(results) do
|
||||
if not response.err and type(response.result) == "table" then
|
||||
local client = vim.lsp.get_client_by_id(client_id)
|
||||
for _, lens in ipairs(response.result) do
|
||||
local row_num = lens.range.start.line
|
||||
local row = new_rows[row_num]
|
||||
if not row then
|
||||
row = Row.new(row_num)
|
||||
new_rows[row_num] = row
|
||||
end
|
||||
if lens.command then
|
||||
row:add(lens)
|
||||
elseif
|
||||
client
|
||||
and client:supports_method(
|
||||
vim.lsp.protocol.Methods.codeLens_resolve
|
||||
)
|
||||
then
|
||||
row:await()
|
||||
client:request(
|
||||
vim.lsp.protocol.Methods.codeLens_resolve,
|
||||
lens,
|
||||
function(_, resolved)
|
||||
if self.generation ~= gen then
|
||||
return
|
||||
end
|
||||
row:resolved(
|
||||
type(resolved) == "table" and resolved
|
||||
or nil
|
||||
)
|
||||
row:render(self.buf)
|
||||
end,
|
||||
self.buf
|
||||
)
|
||||
end
|
||||
collect(new_rows, client, lens)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user