fix(lsp): check window is still valid after async request #39396

Problem:
Since `foldclose` is async, it must wait for the request to return before actually executing, at which point the original window may no longer be valid.

Solution:
Check whether the window is valid before actually performing `foldclose`.

(cherry picked from commit 775c7d1b53)
This commit is contained in:
Yi Ming
2026-04-26 00:11:09 +08:00
committed by github-actions[bot]
parent f0baa18043
commit 5e6c8d4edf

View File

@@ -314,8 +314,9 @@ function M.foldclose(kind, winid)
local params = { textDocument = util.make_text_document_params(bufnr) }
vim.lsp.buf_request_all(bufnr, 'textDocument/foldingRange', params, function(...)
state:multi_handler(...)
-- Ensure this buffer stays as the current buffer after the async request
if api.nvim_win_get_buf(winid) == bufnr then
-- Ensure this window is still valid and buffer stays as the current buffer
-- after the async request.
if api.nvim_win_is_valid(winid) and api.nvim_win_get_buf(winid) == bufnr then
state:foldclose(kind, winid)
end
end)