Skip to content

Commit

Permalink
feat: get_available_servers now accepts a filter (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: William Boman <william@redwill.se>
  • Loading branch information
kylo252 and williamboman committed Aug 5, 2022
1 parent af46a16 commit e48a41e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
8 changes: 7 additions & 1 deletion doc/mason-lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ get_installed_servers()
|mason-registry.get_installed_package_names()|

*mason-lspconfig.get_available_servers()*
get_available_servers()
get_available_servers({filter})
Returns the available (both installed & uninstalled) LSP servers.

Note: ~
Expand All @@ -244,6 +244,12 @@ get_available_servers()
table and use its values to directly interact with lspconfig (for
example setting up all installed servers).

Parameters: ~
{filter} (table|nil) A table with key-value pairs used to
filter the list of server names. The available keys are:
- filetype (string | string[]): Only return servers with
matching filetype

Returns: ~
string[]

Expand Down
45 changes: 41 additions & 4 deletions lua/mason-lspconfig/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,53 @@ function M.get_installed_servers()
end, registry.get_installed_package_names())
end

---@param filetype string | string[]
local function is_server_in_filetype(filetype)
local filetype_mapping = require "mason-lspconfig.mappings.filetype"

local function get_servers_by_filetype(ft)
return filetype_mapping[ft] or {}
end

local server_candidates = _.compose(
_.set_of,
_.cond {
{ _.is "string", get_servers_by_filetype },
{ _.is "table", _.compose(_.flatten, _.map(get_servers_by_filetype)) },
{ _.T, _.always {} },
}
)(filetype)

---@param server_name string
---@return boolean
return function(server_name)
return server_candidates[server_name]
end
end

---Get a list of available servers in mason-registry
---@param filter { filetype: string | string[] }?: (optional) Used to filter the list of server names.
--- The available keys are
--- - filetype (string | string[]): Only return servers with matching filetype
---@return string[]
function M.get_available_servers()
function M.get_available_servers(filter)
local registry = require "mason-registry"
local server_mapping = require "mason-lspconfig.mappings.server"
local Optional = require "mason-core.optional"
local server_names = _.filter_map(function(pkg_name)
return Optional.of_nilable(server_mapping.package_to_lspconfig[pkg_name])
filter = filter or {}
local predicates = {}

if filter.filetype then
table.insert(predicates, is_server_in_filetype(filter.filetype))
end

return _.filter_map(function(pkg_name)
return Optional.of_nilable(server_mapping.package_to_lspconfig[pkg_name]):map(function(server_name)
if #predicates == 0 or _.all_pass(predicates, server_name) then
return server_name
end
end)
end, registry.get_all_package_names())
return server_names
end

return M
43 changes: 43 additions & 0 deletions tests/mason-lspconfig/api/api_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local mason_lspconfig = require "mason-lspconfig"
local _ = require "mason-core.functional"

describe("mason-lspconfig API", function()
it("should return all available servers", function()
local server_mappings = require "mason-lspconfig.mappings.server"
local available_servers = mason_lspconfig.get_available_servers()
assert.equal(#vim.tbl_keys(server_mappings.package_to_lspconfig), #available_servers)
end)

it("should return all available servers for given filetype", function()
assert.same(
{ "terraformls", "tflint" },
_.sort_by(
_.identity,
mason_lspconfig.get_available_servers {
filetype = "terraform",
}
)
)
end)

it("should return all available servers for given filetypes", function()
assert.same(
{ "lemminx", "taplo" },
_.sort_by(
_.identity,
mason_lspconfig.get_available_servers {
filetype = { "xml", "xsd", "xsl", "toml" },
}
)
)
end)

it("should return no servers if filetype predicate has no matches", function()
assert.same(
{},
mason_lspconfig.get_available_servers {
filetype = { "thisfiletypesimplydoesntexist" },
}
)
end)
end)

0 comments on commit e48a41e

Please sign in to comment.