From 0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5 Mon Sep 17 00:00:00 2001 From: Olivia Kinnear Date: Thu, 23 Oct 2025 09:01:08 -0500 Subject: feat: force-stop server with :LspStop! (bang) #4140 Problem: Some servers don't stop properly. Calling `:LspStop` _twice_ will induce a force-stop, but that is not easy to discover: https://github.com/neovim/neovim/blob/b67eff38fe19876ab228007897224ec04b58aa40/runtime/lua/vim/lsp/client.lua#L864-L866 > By default, it will just request the server to shutdown without force. If > you request to stop a client which has previously been requested to > shutdown, it will automatically escalate and force shutdown. Solution: Nvim should automatically force-stop after X seconds, but until that is supported, adding a bang "!" variant is reasonable. --- plugin/lspconfig.lua | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'plugin') diff --git a/plugin/lspconfig.lua b/plugin/lspconfig.lua index 550f92e7..574945a3 100644 --- a/plugin/lspconfig.lua +++ b/plugin/lspconfig.lua @@ -128,33 +128,31 @@ if vim.fn.has('nvim-0.11.2') == 1 then -- Default to restarting all active servers if #clients == 0 then - clients = vim - .iter(vim.lsp.get_clients()) - :map(function(client) - return client.name - end) - :totable() + clients = vim.lsp.get_clients() end - for _, name in ipairs(clients) do + for client in vim.iter(clients) do + local name = client.name if vim.lsp.config[name] == nil then vim.notify(("Invalid server name '%s'"):format(name)) else vim.lsp.enable(name, false) + if info.bang then + client:stop(true) + end end end local timer = assert(vim.uv.new_timer()) timer:start(500, 0, function() - for _, name in ipairs(clients) do - vim.schedule_wrap(function(x) - vim.lsp.enable(x) - end)(name) + for client in vim.iter(clients) do + vim.schedule_wrap(vim.lsp.enable)(client.name) end end) end, { desc = 'Restart the given client', nargs = '?', + bang = true, complete = complete_client, }) @@ -163,24 +161,24 @@ if vim.fn.has('nvim-0.11.2') == 1 then -- Default to disabling all servers on current buffer if #clients == 0 then - clients = vim - .iter(vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf() })) - :map(function(client) - return client.name - end) - :totable() + clients = vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf() }) end - for _, name in ipairs(clients) do + for client in vim.iter(clients) do + local name = client.name if vim.lsp.config[name] == nil then vim.notify(("Invalid server name '%s'"):format(name)) else vim.lsp.enable(name, false) + if info.bang then + client:stop(true) + end end end end, { desc = 'Disable and stop the given client', nargs = '?', + bang = true, complete = complete_client, }) -- cgit v1.2.3-70-g09d2