diff options
| author | William Boman <william@redwill.se> | 2022-07-17 01:53:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-17 01:53:20 +0200 |
| commit | 7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa (patch) | |
| tree | 66ac5066ba415eeb3557ba129f0282c4feb44300 | |
| parent | fix(jdtls): download milestone versions instead of snapshots (#87) (diff) | |
| download | mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar.gz mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar.bz2 mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar.lz mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar.xz mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.tar.zst mason-7cb9f7aea300428fc38b5a71d1a6c48cf1f42efa.zip | |
fix(mason-lspconfig): deduplicate :LspInstall completion items (#88)
| -rw-r--r-- | lua/mason-core/functional/init.lua | 1 | ||||
| -rw-r--r-- | lua/mason-core/functional/list.lua | 18 | ||||
| -rw-r--r-- | lua/mason-lspconfig/api/command.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/functional/list_spec.lua | 5 |
4 files changed, 26 insertions, 2 deletions
diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index a7b0a369..97865f05 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -38,6 +38,7 @@ _.head = list.head _.length = list.length _.flatten = list.flatten _.sort_by = list.sort_by +_.uniq_by = list.uniq_by _.join = list.join -- relation diff --git a/lua/mason-core/functional/list.lua b/lua/mason-core/functional/list.lua index c3aaecd8..f30dd13d 100644 --- a/lua/mason-core/functional/list.lua +++ b/lua/mason-core/functional/list.lua @@ -172,4 +172,22 @@ _.join = fun.curryN(function(sep, list) return table.concat(list, sep) end, 2) +---@generic T +---@param fun (item: T): any +---@param list T[] +---@return T[] +_.uniq_by = fun.curryN(function(comp, list) + local set = {} + local result = {} + for i = 1, #list do + local item = list[i] + local uniq_key = comp(item) + if not set[uniq_key] then + set[uniq_key] = true + table.insert(result, item) + end + end + return result +end, 2) + return _ diff --git a/lua/mason-lspconfig/api/command.lua b/lua/mason-lspconfig/api/command.lua index 5f0da4bf..a782ac08 100644 --- a/lua/mason-lspconfig/api/command.lua +++ b/lua/mason-lspconfig/api/command.lua @@ -124,8 +124,8 @@ _G.mason_lspconfig_completion = { local package_names = _.filter_map(function(pkg_name) return Optional.of_nilable(server_mapping.package_to_lspconfig[pkg_name]) end, registry.get_all_package_names()) - local completion = _.concat(package_names, _.keys(language_mapping)) - table.sort(completion) + local completion = + _.compose(_.sort_by(_.identity), _.uniq_by(_.identity), _.concat(_.keys(language_mapping)))(package_names) return table.concat(completion, "\n") end, installed_server_completion = function() diff --git a/tests/mason-core/functional/list_spec.lua b/tests/mason-core/functional/list_spec.lua index 999b3625..ce0c285f 100644 --- a/tests/mason-core/functional/list_spec.lua +++ b/tests/mason-core/functional/list_spec.lua @@ -190,4 +190,9 @@ describe("functional: list", function() it("joins lists", function() assert.equals("Hello, John", _.join(", ", { "Hello", "John" })) end) + + it("should uniq_by lists", function() + local list = { "Person.", "Woman.", "Man.", "Person.", "Woman.", "Camera.", "TV." } + assert.same({ "Person.", "Woman.", "Man.", "Camera.", "TV." }, _.uniq_by(_.identity, list)) + end) end) |
