Skip to content

Commit

Permalink
fix(LspStop): correctly stop servers and notify user #3378
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
guilhas07 authored Oct 18, 2024
1 parent 541f3a2 commit dda84e6
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions plugin/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '?',
Expand Down

0 comments on commit dda84e6

Please sign in to comment.