From ba7f73797cd7fdb8b1097ee0040bbfac8baae11b Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Sun, 8 Dec 2024 13:57:36 +0800 Subject: [PATCH] fix(lsp): cancel pending requests on detach --- runtime/lua/vim/lsp/_folding_range.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/runtime/lua/vim/lsp/_folding_range.lua b/runtime/lua/vim/lsp/_folding_range.lua index 2f1767aaf5..5d3d961311 100644 --- a/runtime/lua/vim/lsp/_folding_range.lua +++ b/runtime/lua/vim/lsp/_folding_range.lua @@ -197,6 +197,22 @@ local function setup(bufnr) -- `on_detach` also runs on buffer reload (`:e`). -- Ensure `bufstate` and hooks are cleared to avoid duplication or leftover states. on_detach = function() + for _, client in + ipairs(vim.lsp.get_clients({ + bufnr = bufnr, + method = ms.textDocument_foldingRange, + })) + do + for id, req in pairs(client.requests) do + if + req.type == 'pending' + and req.bufnr == bufnr + and req.method == ms.textDocument_foldingRange + then + client:cancel_request(id) + end + end + end bufstates[bufnr] = nil api.nvim_clear_autocmds({ buffer = bufnr, group = augroup_setup }) end,