Skip to content

Commit

Permalink
fix: no-argument :LspStop, :LspRestart with standalone files #1785
Browse files Browse the repository at this point in the history
The no-argument versions of `:LspStop` and `:LspRestart` currently only apply to
buffers that have a valid root directory. It seems that these commands should stop/restart
all clients, including those associated with standalone files.

Closes #1784
  • Loading branch information
rish987 authored Jun 1, 2022
1 parent 19717da commit c5dae15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ function configs.__newindex(t, config_name, config_def)
end

if root_dir then
-- Lazy-launching: attach when a buffer in this directory is opened.
api.nvim_command(
string.format(
"autocmd BufReadPost %s/* unsilent lua require'lspconfig'[%q].manager.try_add_wrapper()",
vim.fn.fnameescape(root_dir),
config.name
)
)
-- Attach for all existing buffers in this directory.
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local bufname = api.nvim_buf_get_name(bufnr)
if util.bufname_valid(bufname) then
Expand Down Expand Up @@ -157,7 +159,7 @@ function configs.__newindex(t, config_name, config_def)
client.offset_encoding = result.offsetEncoding
end

-- Send `settings to server via workspace/didChangeConfiguration
-- Send `settings` to server via workspace/didChangeConfiguration
function client.workspace_did_change_configuration(settings)
if not settings then
return
Expand Down Expand Up @@ -208,6 +210,8 @@ function configs.__newindex(t, config_name, config_def)
return make_config(root_dir)
end)

-- Try to attach the buffer `bufnr` to a client using this config, creating
-- a new client if one doesn't already exist for `bufnr`.
function manager.try_add(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()

Expand Down Expand Up @@ -240,6 +244,8 @@ function configs.__newindex(t, config_name, config_def)
end
end

-- Check that the buffer `bufnr` has a valid filetype according to
-- `config.filetypes`, then do `manager.try_add(bufnr)`.
function manager.try_add_wrapper(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
local buf_filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
Expand All @@ -250,6 +256,7 @@ function configs.__newindex(t, config_name, config_def)
return
end
end
-- `config.filetypes = nil` means all filetypes are valid.
else
manager.try_add(bufnr)
end
Expand Down
6 changes: 4 additions & 2 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,10 @@ function M.server_per_root_dir_manager(make_config)
return client_id
end

function manager.clients()
function manager.clients(single_file)
local res = {}
for _, id in pairs(clients) do
local client_list = single_file and single_file_clients or clients
for _, id in pairs(client_list) do
local client = lsp.get_client_by_id(id)
if client then
table.insert(res, client)
Expand Down Expand Up @@ -422,6 +423,7 @@ function M.get_managed_clients()
for _, config in pairs(configs) do
if config.manager then
vim.list_extend(clients, config.manager.clients())
vim.list_extend(clients, config.manager.clients(true))
end
end
return clients
Expand Down

0 comments on commit c5dae15

Please sign in to comment.