diff options
| author | Olivia Kinnear <account@superatomic.dev> | 2025-10-23 09:01:08 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-23 07:01:08 -0700 |
| commit | 0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5 (patch) | |
| tree | a3180c11afac302b475fd332a9ff2c850809390f | |
| parent | docs: update configs.md (diff) | |
| download | nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar.gz nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar.bz2 nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar.lz nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar.xz nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.tar.zst nvim-lspconfig-0d90bb9bfb8e8b6b64f4a19b7dd3619f5d2788b5.zip | |
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.
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | doc/lspconfig.txt | 7 | ||||
| -rw-r--r-- | plugin/lspconfig.lua | 34 |
3 files changed, 22 insertions, 21 deletions
@@ -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 <config_name>` Start the requested server name. Will only successfully start if the command detects a root directory matching the current config. -* `:LspStop [<client_id_or_name>]` Stops the given server. Defaults to stopping all servers active on the current buffer. +* `:LspStop [<client_id_or_name>]` Stops the given server. Defaults to stopping all servers active on the current buffer. To force stop use `:LspStop!` * `:LspRestart [<client_id_or_name>]` 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, }) |
