From dda84e6dd99c7d67fd3f9b68ea2162521a2312a7 Mon Sep 17 00:00:00 2001 From: Guilherme Soares <48023091+guilhas07@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:46:51 +0100 Subject: fix(LspStop): correctly stop servers and notify user #3378 ## Problem The current `LspStop` behavior is confusing and wrong: **Server name:** - If the server with the given `server_name` is **not attached**: - No notification is shown, and **all** LSP servers are stopped. - If the server with the given `server_name` is **attached**: - **Incorrectly** closes all LSP servers. - If no servers are attached: - `server_name` is notified as missing. **Server ID:** - If the server with the given `server_id` is **not attached**: - Uses `get_managed_clients()` function https://github.com/neovim/nvim-lspconfig/blob/541f3a2781de481bb84883889e4d9f0904250a56/plugin/lspconfig.lua#L45-L47 Which doesn't return all servers (e.g., `null-ls`), so it doesn't close all LSP clients. - If the server with the given `server_id` is **attached**: - The correct LSP server is stopped (including `null-ls`). **No arguments:** - If servers are **attached**: - Stops all servers. - If no servers are attached: - **Incorrectly** notifies the user with: `config "" not found`. ## Solution **Server name:** - If the server with the given `server_name` is **not attached**: - Notify the user, but **do not close** any servers. - If the server with the given `server_name` is **attached**: - Close the specified server. **Server ID:** - If the server with the given `server_id` is **not attached**: - Notify the user, but **do not close** any servers. - If the server with the given `server_id` is **attached**: - Close the specified server. **No arguments:** - If servers are **attached**: - Stops all servers. - If no servers are attached: - No-op. --- plugin/lspconfig.lua | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'plugin') diff --git a/plugin/lspconfig.lua b/plugin/lspconfig.lua index e687e330..f9f22e5f 100644 --- a/plugin/lspconfig.lua +++ b/plugin/lspconfig.lua @@ -110,36 +110,47 @@ end, { api.nvim_create_user_command('LspStop', function(info) local current_buf = vim.api.nvim_get_current_buf() - local server_id, force, server_name + local server_id, force, server_name, err_msg local arguments = vim.split(info.args, '%s') + + local filter = function() + return true + end + local found = true + for _, v in pairs(arguments) do if v == '++force' then force = true elseif v:find '^[0-9]+$' then - server_id = v - else + server_id = tonumber(v) + ---@param client vim.lsp.Client + filter = function(client) + return server_id == client.id + end + found = false + err_msg = ('nvim-lspconfig: client id "%s" not found'):format(server_id) + elseif v ~= '' then server_name = v - end - end - - if not server_id then - local servers_on_buffer = require('lspconfig.util').get_lsp_clients { bufnr = current_buf } - local found = false - for _, client in ipairs(servers_on_buffer) do - if client.attached_buffers[current_buf] and (server_name and (server_name == client.config.name) or true) then - client.stop(force) - found = true + ---@param client vim.lsp.Client + filter = function(client) + return server_name == client.config.name end + err_msg = ('nvim-lspconfig: config "%s" not found'):format(server_name) + found = false end + end - if server_name and not found then - vim.notify(('nvim-lspconfig: config "%s" not found'):format(server_name), vim.log.levels.WARN) - end - else - for _, client in ipairs(get_clients_from_cmd_args(server_id)) do + local servers_on_buffer = require('lspconfig.util').get_lsp_clients { bufnr = current_buf } + for _, client in ipairs(servers_on_buffer) do + if client.attached_buffers[current_buf] and filter(client) then client.stop(force) + found = true end end + + if not found then + vim.notify(err_msg, vim.log.levels.WARN) + end end, { desc = 'Manually stops the given language client(s)', nargs = '?', -- cgit v1.2.3-70-g09d2