Skip to content

Commit e4d1c8b

Browse files
feat: support :LspStart/LspRestart in Nvim 0.11.2+ #3734
1 parent 83a5d19 commit e4d1c8b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

plugin/lspconfig.lua

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,82 @@ end
6767
-- script in scriptnames to be executed is lspconfig.
6868
api.nvim_create_user_command('LspInfo', ':checkhealth vim.lsp', { desc = 'Alias to `:checkhealth vim.lsp`' })
6969

70+
if vim.version.ge(vim.version(), { 0, 11, 2 }) then
71+
local complete_client = function(arg)
72+
return vim
73+
.iter(vim.lsp.get_clients())
74+
:map(function(client)
75+
return client.name
76+
end)
77+
:filter(function(name)
78+
return name:sub(1, #arg) == arg
79+
end)
80+
:totable()
81+
end
82+
83+
local complete_config = function(arg)
84+
return vim
85+
.iter(vim.api.nvim_get_runtime_file(('lsp/%s*.lua'):format(arg), true))
86+
:map(function(path)
87+
local file_name = path:match('[^/]*.lua$')
88+
return file_name:sub(0, #file_name - 4)
89+
end)
90+
:totable()
91+
end
92+
93+
api.nvim_create_user_command('LspStart', function(info)
94+
if vim.lsp.config[info.args] == nil then
95+
vim.notify(("Invalid server name '%s'"):format(info.args))
96+
return
97+
end
98+
99+
vim.lsp.enable(info.args)
100+
end, {
101+
desc = 'Enable and launch a language server',
102+
nargs = '?',
103+
complete = complete_config,
104+
})
105+
106+
api.nvim_create_user_command('LspRestart', function(info)
107+
for _, name in ipairs(info.fargs) do
108+
if vim.lsp.config[name] == nil then
109+
vim.notify(("Invalid server name '%s'"):format(info.args))
110+
else
111+
vim.lsp.enable(name, false)
112+
end
113+
end
114+
115+
local timer = assert(vim.uv.new_timer())
116+
timer:start(500, 0, function()
117+
for _, name in ipairs(info.fargs) do
118+
vim.schedule_wrap(function(x)
119+
vim.lsp.enable(x)
120+
end)(name)
121+
end
122+
end)
123+
end, {
124+
desc = 'Restart the given client(s)',
125+
nargs = '+',
126+
complete = complete_client,
127+
})
128+
129+
api.nvim_create_user_command('LspStop', function(info)
130+
for _, name in ipairs(info.fargs) do
131+
if vim.lsp.config[name] == nil then
132+
vim.notify(("Invalid server name '%s'"):format(info.args))
133+
else
134+
vim.lsp.enable(name, false)
135+
end
136+
end
137+
end, {
138+
desc = 'Disable and stop the given client(s)',
139+
nargs = '+',
140+
complete = complete_client,
141+
})
142+
143+
return
144+
end
145+
70146
api.nvim_create_user_command('LspStart', function(info)
71147
local server_name = string.len(info.args) > 0 and info.args or nil
72148
if server_name then

0 commit comments

Comments
 (0)