feat(lsp): add vim.lsp.config and vim.lsp.enable

Design goals/requirements:
- Default configuration of a server can be distributed across multiple sources.
  - And via RTP discovery.
- Default configuration can be specified for all servers.
- Configuration _can_ be project specific.

Solution:

- Two new API's:
  - `vim.lsp.config(name, cfg)`:
    - Used to define default configurations for servers of name.
    - Can be used like a table or called as a function.
    - Use `vim.lsp.confg('*', cfg)` to specify default config for all
      servers.
  - `vim.lsp.enable(name)`
    - Used to enable servers of name. Uses configuration defined
    via `vim.lsp.config()`.
This commit is contained in:
Lewis Russell
2024-11-01 16:31:51 +00:00
committed by Lewis Russell
parent ca760e645b
commit 3f1d09bc94
11 changed files with 600 additions and 75 deletions

View File

@@ -6098,15 +6098,6 @@ describe('LSP', function()
end
eq(is_os('mac') or is_os('win'), check_registered(nil)) -- start{_client}() defaults to make_client_capabilities().
eq(false, check_registered(vim.empty_dict()))
eq(
false,
check_registered({
workspace = {
ignoreMe = true,
},
})
)
eq(
false,
check_registered({
@@ -6129,4 +6120,88 @@ describe('LSP', function()
)
end)
end)
describe('vim.lsp.config() and vim.lsp.enable()', function()
it('can merge settings from "*"', function()
eq(
{
name = 'foo',
cmd = { 'foo' },
root_markers = { '.git' },
},
exec_lua(function()
vim.lsp.config('*', { root_markers = { '.git' } })
vim.lsp.config('foo', { cmd = { 'foo' } })
return vim.lsp._resolve_config('foo')
end)
)
end)
it('sets up an autocmd', function()
eq(
1,
exec_lua(function()
vim.lsp.config('foo', {
cmd = { 'foo' },
root_markers = { '.foorc' },
})
vim.lsp.enable('foo')
return #vim.api.nvim_get_autocmds({
group = 'nvim.lsp.enable',
event = 'FileType',
})
end)
)
end)
it('attaches to buffers', function()
exec_lua(create_server_definition)
local tmp1 = t.tmpname(true)
local tmp2 = t.tmpname(true)
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' },
root_markers = { '.foorc' },
})
vim.lsp.config('bar', {
cmd = server.cmd,
filetypes = { 'bar' },
root_markers = { '.foorc' },
})
vim.lsp.enable('foo')
vim.lsp.enable('bar')
vim.cmd.edit(tmp1)
vim.bo.filetype = 'foo'
_G.foo_buf = vim.api.nvim_get_current_buf()
vim.cmd.edit(tmp2)
vim.bo.filetype = 'bar'
_G.bar_buf = vim.api.nvim_get_current_buf()
end)
eq(
{ 1, 'foo', 1, 'bar' },
exec_lua(function()
local foos = vim.lsp.get_clients({ bufnr = assert(_G.foo_buf) })
local bars = vim.lsp.get_clients({ bufnr = assert(_G.bar_buf) })
return { #foos, foos[1].name, #bars, bars[1].name }
end)
)
end)
end)
end)