From 5e6c8d4edf61ffe8f7899e1c3edbad1337db9d97 Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Sun, 26 Apr 2026 00:11:09 +0800 Subject: [PATCH] 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 775c7d1b538275ccb560c4cdd806ef1db322164d) --- runtime/lua/vim/lsp/_folding_range.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/lsp/_folding_range.lua b/runtime/lua/vim/lsp/_folding_range.lua index 39998bb626..82ab157173 100644 --- a/runtime/lua/vim/lsp/_folding_range.lua +++ b/runtime/lua/vim/lsp/_folding_range.lua @@ -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)