feat(lsp): vim.lsp.get_configs() #37237

Problem:
No way to iterate configs. Users need to reach
for `vim.lsp.config._configs`, an internal interface.

Solution:
Provide vim.lsp.get_configs().
Also indirectly improves :lsp enable/disable completion
by discarding invalid configs from completion.
This commit is contained in:
Olivia Kinnear
2026-03-19 06:33:34 -05:00
committed by GitHub
parent 4c48f19e51
commit e406c4efd6
5 changed files with 147 additions and 43 deletions

View File

@@ -387,6 +387,76 @@ lsp.config = setmetatable({ _configs = {} }, {
end,
})
--- @return string[]
local function get_config_names()
local config_names = vim
.iter(api.nvim_get_runtime_file('lsp/*.lua', true))
--- @param path string
:map(function(path)
local file_name = path:match('[^/]*.lua$')
return file_name:sub(0, #file_name - 4)
end)
:totable()
vim.list_extend(config_names, vim.tbl_keys(lsp.config._configs))
return vim
.iter(config_names)
:unique()
--- @param name string
:filter(function(name)
return name ~= '*'
end)
:totable()
end
--- Key-value pairs used to filter the returned configs.
--- @class vim.lsp.get_configs.Filter
--- @inlinedoc
---
--- If true, only return enabled configs. If false, only return configs that
--- aren't enabled.
--- @field enabled? boolean
---
--- Only return configs which attach to the given filetype.
--- @field filetype? string
--- Get LSP configs.
---
--- Note: Will eagerly evaluate config files in `'runtimepath'` if necessary.
--- @param filter? vim.lsp.get_configs.Filter
--- @return vim.lsp.Config[]: List of |vim.lsp.Config| objects
function lsp.get_configs(filter)
validate('filter', filter, 'table', true)
filter = filter or {}
local configs = {} --- @type vim.lsp.Config[]
local config_names --- @type string[]
if not filter.enabled then
config_names = get_config_names()
else
-- Shortcut filtering enabled configs by directly getting enabled configs
config_names = vim.tbl_keys(lsp._enabled_configs)
end
for _, config_name in ipairs(config_names) do
local config = lsp.config[config_name]
if
config
and (filter.enabled ~= false or not lsp.is_enabled(config_name))
and (
filter.filetype == nil
or (config.filetypes ~= nil and vim.list_contains(config.filetypes, filter.filetype))
)
then
configs[#configs + 1] = config
end
end
return configs
end
local lsp_enable_autocmd_id --- @type integer?
local function validate_cmd(v)