aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Fussenegger <f.mathias@zignar.net>2024-12-29 13:44:42 +0100
committerMathias Fußenegger <mfussenegger@users.noreply.github.com>2024-12-31 13:18:05 +0100
commite00cd1ab4060915d86b8536b082e663818268b69 (patch)
treef7689b634f6b6d03bff8b9d81f7249da4bbf29d7
parent1877cd5fcdc0bbe5f3d0685d42d4e295fb819724 (diff)
downloadrneovim-e00cd1ab4060915d86b8536b082e663818268b69.tar.gz
rneovim-e00cd1ab4060915d86b8536b082e663818268b69.tar.bz2
rneovim-e00cd1ab4060915d86b8536b082e663818268b69.zip
feat(lsp): return resolved config for vim.lsp.config[name]
Allows to retrieve the configuration as it will be used by `lsp.enable` - including the parts merged from `*` and rtp. This is useful for explicit startup control (`vim.lsp.start(vim.lsp.config[name])`) Closes https://github.com/neovim/neovim/issues/31640
-rw-r--r--runtime/lua/vim/lsp.lua71
-rw-r--r--runtime/lua/vim/lsp/health.lua2
-rw-r--r--test/functional/plugin/lsp_spec.lua2
3 files changed, 32 insertions, 43 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index 1c8356d64d..5b92926a21 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -420,9 +420,33 @@ lsp.config = setmetatable({ _configs = {} }, {
--- @return vim.lsp.Config
__index = function(self, name)
validate('name', name, 'string')
- invalidate_enabled_config(name)
+
+ local rconfig = lsp._enabled_configs[name] or {}
self._configs[name] = self._configs[name] or {}
- return self._configs[name]
+
+ if not rconfig.resolved_config then
+ -- Resolve configs from lsp/*.lua
+ -- Calls to vim.lsp.config in lsp/* have a lower precedence than calls from other sites.
+ local rtp_config = {} ---@type vim.lsp.Config
+ for _, v in ipairs(api.nvim_get_runtime_file(('lsp/%s.lua'):format(name), true)) do
+ local config = assert(loadfile(v))() ---@type any?
+ if type(config) == 'table' then
+ rtp_config = vim.tbl_deep_extend('force', rtp_config, config)
+ else
+ log.warn(string.format('%s does not return a table, ignoring', v))
+ end
+ end
+
+ rconfig.resolved_config = vim.tbl_deep_extend(
+ 'force',
+ lsp.config._configs['*'] or {},
+ rtp_config,
+ lsp.config._configs[name] or {}
+ )
+ rconfig.resolved_config.name = name
+ end
+
+ return rconfig.resolved_config
end,
--- @param self vim.lsp.config
@@ -446,44 +470,6 @@ lsp.config = setmetatable({ _configs = {} }, {
end,
})
---- @private
---- @param name string
---- @return vim.lsp.Config
-function lsp._resolve_config(name)
- local econfig = lsp._enabled_configs[name] or {}
-
- if not econfig.resolved_config then
- -- Resolve configs from lsp/*.lua
- -- Calls to vim.lsp.config in lsp/* have a lower precedence than calls from other sites.
- local rtp_config = {} ---@type vim.lsp.Config
- for _, v in ipairs(api.nvim_get_runtime_file(('lsp/%s.lua'):format(name), true)) do
- local config = assert(loadfile(v))() ---@type any?
- if type(config) == 'table' then
- rtp_config = vim.tbl_deep_extend('force', rtp_config, config)
- else
- log.warn(string.format('%s does not return a table, ignoring', v))
- end
- end
-
- local config = vim.tbl_deep_extend(
- 'force',
- lsp.config._configs['*'] or {},
- rtp_config,
- lsp.config._configs[name] or {}
- )
-
- config.name = name
-
- validate('cmd', config.cmd, { 'function', 'table' })
- validate('cmd', config.reuse_client, 'function', true)
- -- All other fields are validated in client.create
-
- econfig.resolved_config = config
- end
-
- return assert(econfig.resolved_config)
-end
-
local lsp_enable_autocmd_id --- @type integer?
--- @param bufnr integer
@@ -514,7 +500,9 @@ local function lsp_enable_callback(bufnr)
end
for name in vim.spairs(lsp._enabled_configs) do
- local config = lsp._resolve_config(name)
+ local config = lsp.config[name]
+ validate('cmd', config.cmd, { 'function', 'table' })
+ validate('cmd', config.reuse_client, 'function', true)
if can_start(config) then
-- Deepcopy config so changes done in the client
@@ -522,6 +510,7 @@ local function lsp_enable_callback(bufnr)
config = vim.deepcopy(config)
if type(config.root_dir) == 'function' then
+ ---@param root_dir string
config.root_dir(function(root_dir)
config.root_dir = root_dir
start(config)
diff --git a/runtime/lua/vim/lsp/health.lua b/runtime/lua/vim/lsp/health.lua
index d94bc70a65..8af9f2f791 100644
--- a/runtime/lua/vim/lsp/health.lua
+++ b/runtime/lua/vim/lsp/health.lua
@@ -184,7 +184,7 @@ local function check_enabled_configs()
vim.health.start('vim.lsp: Enabled Configurations')
for name in vim.spairs(vim.lsp._enabled_configs) do
- local config = vim.lsp._resolve_config(name)
+ local config = vim.lsp.config[name]
local text = {} --- @type string[]
text[#text + 1] = ('%s:'):format(name)
for k, v in
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua
index f396c837f9..9cefe96e79 100644
--- a/test/functional/plugin/lsp_spec.lua
+++ b/test/functional/plugin/lsp_spec.lua
@@ -6147,7 +6147,7 @@ describe('LSP', function()
vim.lsp.config('*', { root_markers = { '.git' } })
vim.lsp.config('foo', { cmd = { 'foo' } })
- return vim.lsp._resolve_config('foo')
+ return vim.lsp.config['foo']
end)
)
end)