fix(lsp): reuse client if configs match and no root dir

Problem:
An LSP configuration that creates client with no root_dir or
workspace_folders can result in vim.lsp.enable attaching to it multiple
times.

Solution:
When checking existing clients, reuse a client if it wasn't initially
configured have any workspace_folders. This more closely matches the
behaviour we had prior to d9235ef
This commit is contained in:
Lewis Russell
2024-12-13 10:51:33 +00:00
committed by Lewis Russell
parent 7940ec6913
commit 9c20342297
3 changed files with 50 additions and 16 deletions

View File

@@ -5184,8 +5184,8 @@ describe('LSP', function()
local win = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { 'local x = 10', '', 'print(x)' })
vim.api.nvim_win_set_cursor(win, { 3, 6 })
local client_id1 = assert(vim.lsp.start({ name = 'dummy', cmd = server1.cmd }))
local client_id2 = assert(vim.lsp.start({ name = 'dummy', cmd = server2.cmd }))
local client_id1 = assert(vim.lsp.start({ name = 'dummy1', cmd = server1.cmd }))
local client_id2 = assert(vim.lsp.start({ name = 'dummy2', cmd = server2.cmd }))
local response
vim.lsp.buf.definition({
on_list = function(r)
@@ -6203,5 +6203,33 @@ describe('LSP', function()
end)
)
end)
it('does not attach to buffers more than once if no root_dir', function()
exec_lua(create_server_definition)
local tmp1 = t.tmpname(true)
eq(
1,
exec_lua(function()
local server = _G._create_server({
handlers = {
initialize = function(_, _, callback)
callback(nil, { capabilities = {} })
end,
},
})
vim.lsp.config('foo', { cmd = server.cmd, filetypes = { 'foo' } })
vim.lsp.enable('foo')
vim.cmd.edit(assert(tmp1))
vim.bo.filetype = 'foo'
vim.bo.filetype = 'foo'
return #vim.lsp.get_clients({ bufnr = vim.api.nvim_get_current_buf() })
end)
)
end)
end)
end)