From 9912a4c81b0856200f44a38e99d38eae44cef5c9 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 22 Apr 2024 00:58:48 +0200 Subject: refactor(lua): deprecate tbl_flatten Problem: Besides being redundant with vim.iter():flatten(), `tbl_flatten` has these problems: - Has `tbl_` prefix but only accepts lists. - Discards some results! Compare the following: - iter.flatten(): ``` vim.iter({1, { { a = 2 } }, { 3 } }):flatten():totable() ``` - tbl_flatten: ``` vim.tbl_flatten({1, { { a = 2 } }, { 3 } }) ``` Solution: Deprecate tbl_flatten. Note: iter:flatten() currently fails ("flatten() requires a list-like table") on this code from gen_lsp.lua: local anonym = vim.iter({ -- remove nil anonymous_num > 1 and '' or nil, '---@class ' .. anonymous_classname, }):flatten():totable() Should we enhance :flatten() to work for arrays? --- scripts/gen_lsp.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'scripts/gen_lsp.lua') diff --git a/scripts/gen_lsp.lua b/scripts/gen_lsp.lua index 19fad7bab4..04d19f22e6 100644 --- a/scripts/gen_lsp.lua +++ b/scripts/gen_lsp.lua @@ -259,10 +259,13 @@ function M.gen(opt) if prefix then anonymous_classname = anonymous_classname .. '.' .. prefix end - local anonym = vim.tbl_flatten { -- remove nil - anonymous_num > 1 and '' or nil, - '---@class ' .. anonymous_classname, - } + local anonym = vim + .iter({ + (anonymous_num > 1 and { '' } or {}), + { '---@class ' .. anonymous_classname }, + }) + :flatten() + :totable() --- @class vim._gen_lsp.StructureLiteral translated to anonymous @class. --- @field deprecated? string -- cgit