Skip to content

Commit

Permalink
use old 'tbl_flatten()' impl from neovim
Browse files Browse the repository at this point in the history
Unlike the `iter(...):flatten()` alternative, this handles lists with
holes.
  • Loading branch information
sindrets committed May 18, 2024
1 parent 8397e11 commit 1f4982f
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lua/diffview/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1344,11 +1344,31 @@ function M.merge_sort(t, comparator)
split_merge(t, 1, #t, comparator)
end

M.islist = vim.fn.has("nvim-0.10") == 1 and vim.islist or vim.tbl_islist ---@diagnostic disable-line: deprecated
--- @diagnostic disable-next-line: deprecated
M.islist = vim.fn.has("nvim-0.10") == 1 and vim.islist or vim.tbl_islist

M.flatten = vim.fn.has("nvim-0.10") == 1 and function(...)
return vim.iter(...):flatten(math.huge):totable()
end or vim.tbl_flatten ---@diagnostic disable-line: deprecated
--- @param t table
--- @return any[]
function M.flatten(t)
local result = {}

--- @param _t table<any,any>
local function recurse(_t)
local n = #_t
for i = 1, n do
local v = _t[i]
if type(v) == 'table' then
recurse(v)
elseif v then
table.insert(result, v)
end
end
end

recurse(t)

return result
end

M.path_sep = path_sep

Expand Down

0 comments on commit 1f4982f

Please sign in to comment.