fix(lsp): cancel pending requests before refreshing (#31500)

Problem:
Diagnostics and inlay hints can be expensive to calculate, and we
shouldn't stack them as this can cause noticeable lag.

Solution:
Check for duplicate inflight requests and cancel them before issuing a new one.
This ensures that only the latest request is processed, improving
performance and preventing potential conflicts.
This commit is contained in:
Tristan Knight 2024-12-16 11:17:40 +00:00 committed by GitHub
parent 424a452401
commit bf66871113
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2306,6 +2306,11 @@ 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
for rid, req in pairs(client.requests) do
if req.method == method and req.type == 'pending' and req.bufnr == bufnr then
client.cancel_request(rid)
end
end
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),