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. --- README.md | 2 +- doc/lspconfig.txt | 7 +++++-- plugin/lspconfig.lua | 34 ++++++++++++++++------------------ 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ad41c4e6..354327da 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ Most of the time, the reason for failure is present in the logs. * `:LspInfo` (alias to `:checkhealth vim.lsp`) shows the status of active and configured language servers. * `:LspStart ` Start the requested server name. Will only successfully start if the command detects a root directory matching the current config. -* `:LspStop []` Stops the given server. Defaults to stopping all servers active on the current buffer. +* `:LspStop []` Stops the given server. Defaults to stopping all servers active on the current buffer. To force stop use `:LspStop!` * `:LspRestart []` Restarts the given client, and attempts to reattach to all previously attached buffers. Defaults to restarting all active servers. ## Contributions diff --git a/doc/lspconfig.txt b/doc/lspconfig.txt index fab073b6..ce0181e1 100644 --- a/doc/lspconfig.txt +++ b/doc/lspconfig.txt @@ -87,12 +87,15 @@ the current buffer filetype. :LspStop [client_id] or [config_name] *:LspStop* Stops the server with the given client-id or config name. Defaults to -stopping all servers active on the current buffer. +stopping all servers active on the current buffer. To force stop language +servers: >vim + :LspStop! :LspRestart [client_id] or [config_name] *:LspRestart* Restarts the client with the given client-id or config name, and attempts to reattach to all previously attached buffers. Defaults to restarting all -active servers. +active servers. To force stop language servers when restarting: >vim + :LspRestart! ============================================================================== SERVER CONFIGS *lspconfig-configurations* 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