mirror of
https://github.com/neovim/neovim.git
synced 2026-08-29 18:41:48 +00:00
feat(lua): add Iter:unique() (#37592)
This commit is contained in:
@@ -10,13 +10,13 @@ end
|
||||
|
||||
--- @return string[]
|
||||
local function get_client_names()
|
||||
local client_names = vim
|
||||
return vim
|
||||
.iter(lsp.get_clients())
|
||||
:map(function(client)
|
||||
return client.name
|
||||
end)
|
||||
:unique()
|
||||
:totable()
|
||||
return vim.list.unique(client_names)
|
||||
end
|
||||
|
||||
--- @return string[]
|
||||
@@ -34,7 +34,8 @@ local function get_config_names()
|
||||
vim.list_extend(config_names, vim.tbl_keys(lsp.config._configs))
|
||||
|
||||
return vim
|
||||
.iter(vim.list.unique(config_names))
|
||||
.iter(config_names)
|
||||
:unique()
|
||||
--- @param name string
|
||||
:filter(function(name)
|
||||
return name ~= '*'
|
||||
|
||||
@@ -365,9 +365,11 @@ end
|
||||
--- Only the first occurrence of each value is kept.
|
||||
--- The operation is performed in-place and the input table is modified.
|
||||
---
|
||||
--- Accepts an optional `key` argument that if provided is called for each
|
||||
--- Accepts an optional `key` argument, which if provided is called for each
|
||||
--- value in the list to compute a hash key for uniqueness comparison.
|
||||
--- This is useful for deduplicating table values or complex objects.
|
||||
--- If `key` returns `nil` for a value, that value will be considered unique,
|
||||
--- even if multiple values return `nil`.
|
||||
---
|
||||
--- Example:
|
||||
--- ```lua
|
||||
@@ -385,6 +387,7 @@ end
|
||||
--- @param t T[]
|
||||
--- @param key? fun(x: T): any Optional hash function to determine uniqueness of values
|
||||
--- @return T[] : The deduplicated list
|
||||
--- @see |Iter:unique()|
|
||||
function vim.list.unique(t, key)
|
||||
vim.validate('t', t, 'table')
|
||||
local seen = {} --- @type table<any,boolean>
|
||||
|
||||
Reference in New Issue
Block a user