Skip to content

Commit a182334

Browse files
authored
fix: support :LspStart/LspStop without arguments #3890
Problem: After the refactoring in e4d1c8b for Neovim 0.11.2 these commands now require an argument. Solution: Restore the previous behaviour where `:LspStart` defaults to enabling all servers matching the filetype of the current buffer, and `:LspStop` defaults to disabling all servers attached to the current buffer.
1 parent cd864ac commit a182334

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

plugin/lspconfig.lua

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,22 @@ if vim.version.ge(vim.version(), { 0, 11, 2 }) then
9797
end
9898

9999
api.nvim_create_user_command('LspStart', function(info)
100-
if vim.lsp.config[info.args] == nil then
101-
vim.notify(("Invalid server name '%s'"):format(info.args))
102-
return
100+
local servers = info.fargs
101+
102+
-- Default to enabling all servers matching the filetype of the current buffer.
103+
-- This assumes that they've been explicitly configured through `vim.lsp.config`,
104+
-- otherwise they won't be present in the private `vim.lsp.config._configs` table.
105+
if #servers == 0 then
106+
local filetype = vim.bo.filetype
107+
for name, _ in pairs(vim.lsp.config._configs) do
108+
local filetypes = vim.lsp.config[name].filetypes
109+
if filetypes and vim.tbl_contains(filetypes, filetype) then
110+
table.insert(servers, name)
111+
end
112+
end
103113
end
104114

105-
vim.lsp.enable(info.args)
115+
vim.lsp.enable(servers)
106116
end, {
107117
desc = 'Enable and launch a language server',
108118
nargs = '?',
@@ -133,16 +143,28 @@ if vim.version.ge(vim.version(), { 0, 11, 2 }) then
133143
})
134144

135145
api.nvim_create_user_command('LspStop', function(info)
136-
for _, name in ipairs(info.fargs) do
146+
local clients = info.fargs
147+
148+
-- Default to disabling all servers on current buffer
149+
if #clients == 0 then
150+
clients = vim
151+
.iter(vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf() }))
152+
:map(function(client)
153+
return client.name
154+
end)
155+
:totable()
156+
end
157+
158+
for _, name in ipairs(clients) do
137159
if vim.lsp.config[name] == nil then
138-
vim.notify(("Invalid server name '%s'"):format(info.args))
160+
vim.notify(("Invalid server name '%s'"):format(name))
139161
else
140162
vim.lsp.enable(name, false)
141163
end
142164
end
143165
end, {
144166
desc = 'Disable and stop the given client(s)',
145-
nargs = '+',
167+
nargs = '*',
146168
complete = complete_client,
147169
})
148170

0 commit comments

Comments
 (0)