Skip to content

Commit 1f4982f

Browse files
committed
use old 'tbl_flatten()' impl from neovim
Unlike the `iter(...):flatten()` alternative, this handles lists with holes.
1 parent 8397e11 commit 1f4982f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lua/diffview/utils.lua

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,11 +1344,31 @@ function M.merge_sort(t, comparator)
13441344
split_merge(t, 1, #t, comparator)
13451345
end
13461346

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

1349-
M.flatten = vim.fn.has("nvim-0.10") == 1 and function(...)
1350-
return vim.iter(...):flatten(math.huge):totable()
1351-
end or vim.tbl_flatten ---@diagnostic disable-line: deprecated
1350+
--- @param t table
1351+
--- @return any[]
1352+
function M.flatten(t)
1353+
local result = {}
1354+
1355+
--- @param _t table<any,any>
1356+
local function recurse(_t)
1357+
local n = #_t
1358+
for i = 1, n do
1359+
local v = _t[i]
1360+
if type(v) == 'table' then
1361+
recurse(v)
1362+
elseif v then
1363+
table.insert(result, v)
1364+
end
1365+
end
1366+
end
1367+
1368+
recurse(t)
1369+
1370+
return result
1371+
end
13521372

13531373
M.path_sep = path_sep
13541374

0 commit comments

Comments
 (0)