Merge #28440 rename tbl_islist, tbl_isarray

This commit is contained in:
Justin M. Keyes 2024-04-21 09:16:16 -07:00 committed by GitHub
commit f694d020c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 110 additions and 94 deletions

View File

@ -173,6 +173,7 @@ LUA
- *vim.pretty_print()* Use |vim.print()| instead. - *vim.pretty_print()* Use |vim.print()| instead.
- *vim.loop* Use |vim.uv| instead. - *vim.loop* Use |vim.uv| instead.
- *vim.tbl_add_reverse_lookup()* - *vim.tbl_add_reverse_lookup()*
- *vim.tbl_islist()* Use |vim.islist()| instead.
NORMAL COMMANDS NORMAL COMMANDS
- *]f* *[f* Same as "gf". - *]f* *[f* Same as "gf".

View File

@ -2029,6 +2029,43 @@ vim.is_callable({f}) *vim.is_callable()*
Return: ~ Return: ~
(`boolean`) `true` if `f` is callable, else `false` (`boolean`) `true` if `f` is callable, else `false`
vim.isarray({t}) *vim.isarray()*
Tests if `t` is an "array": a table indexed only by integers (potentially
non-contiguous).
If the indexes start from 1 and are contiguous then the array is also a
list. |vim.islist()|
Empty table `{}` is an array, unless it was created by |vim.empty_dict()|
or returned as a dict-like |API| or Vimscript result, for example from
|rpcrequest()| or |vim.fn|.
Parameters: ~
• {t} (`table?`)
Return: ~
(`boolean`) `true` if array-like table, else `false`.
See also: ~
• https://github.com/openresty/luajit2#tableisarray
vim.islist({t}) *vim.islist()*
Tests if `t` is a "list": a table indexed only by contiguous integers
starting from 1 (what |lua-length| calls a "regular array").
Empty table `{}` is a list, unless it was created by |vim.empty_dict()| or
returned as a dict-like |API| or Vimscript result, for example from
|rpcrequest()| or |vim.fn|.
Parameters: ~
• {t} (`table?`)
Return: ~
(`boolean`) `true` if list-like table, else `false`.
See also: ~
• |vim.isarray()|
vim.list_contains({t}, {value}) *vim.list_contains()* vim.list_contains({t}, {value}) *vim.list_contains()*
Checks if a list-like table (integer keys without gaps) contains `value`. Checks if a list-like table (integer keys without gaps) contains `value`.
@ -2279,26 +2316,6 @@ vim.tbl_get({o}, {...}) *vim.tbl_get()*
Return: ~ Return: ~
(`any`) Nested value indexed by key (if it exists), else nil (`any`) Nested value indexed by key (if it exists), else nil
vim.tbl_isarray({t}) *vim.tbl_isarray()*
Tests if `t` is an "array": a table indexed only by integers (potentially
non-contiguous).
If the indexes start from 1 and are contiguous then the array is also a
list. |vim.tbl_islist()|
Empty table `{}` is an array, unless it was created by |vim.empty_dict()|
or returned as a dict-like |API| or Vimscript result, for example from
|rpcrequest()| or |vim.fn|.
Parameters: ~
• {t} (`table`)
Return: ~
(`boolean`) `true` if array-like table, else `false`.
See also: ~
• https://github.com/openresty/luajit2#tableisarray
vim.tbl_isempty({t}) *vim.tbl_isempty()* vim.tbl_isempty({t}) *vim.tbl_isempty()*
Checks if a table is empty. Checks if a table is empty.
@ -2311,23 +2328,6 @@ vim.tbl_isempty({t}) *vim.tbl_isempty()*
See also: ~ See also: ~
• https://github.com/premake/premake-core/blob/master/src/base/table.lua • https://github.com/premake/premake-core/blob/master/src/base/table.lua
vim.tbl_islist({t}) *vim.tbl_islist()*
Tests if `t` is a "list": a table indexed only by contiguous integers
starting from 1 (what |lua-length| calls a "regular array").
Empty table `{}` is a list, unless it was created by |vim.empty_dict()| or
returned as a dict-like |API| or Vimscript result, for example from
|rpcrequest()| or |vim.fn|.
Parameters: ~
• {t} (`table`)
Return: ~
(`boolean`) `true` if list-like table, else `false`.
See also: ~
• |vim.tbl_isarray()|
vim.tbl_keys({t}) *vim.tbl_keys()* vim.tbl_keys({t}) *vim.tbl_keys()*
Return a list of all keys used in a table. However, the order of the Return a list of all keys used in a table. However, the order of the
return table of keys is not guaranteed. return table of keys is not guaranteed.

View File

@ -22,10 +22,10 @@ The following changes may require adaptations in user config or plugins.
set guicursor+=n-v-c:blinkon500-blinkoff500 set guicursor+=n-v-c:blinkon500-blinkoff500
< <
• |vim.tbl_islist()| now checks whether a table is actually list-like (i.e., • |vim.islist()| now checks whether a table is actually list-like (i.e.,
has integer keys without gaps and starting from 1). For the previous has integer keys without gaps and starting from 1). For the previous
behavior (only check for integer keys, allow gaps or not starting with 1), behavior (only check for integer keys, allow gaps or not starting with 1),
use |vim.tbl_isarray()|. use |vim.isarray()|.
• "#" followed by a digit no longer stands for a function key at the start of • "#" followed by a digit no longer stands for a function key at the start of
the lhs of a mapping. the lhs of a mapping.
@ -155,6 +155,8 @@ unreleased features on Nvim HEAD.
• Removed vim.iter.map(), vim.iter.filter(), vim.iter.totable(). • Removed vim.iter.map(), vim.iter.filter(), vim.iter.totable().
• Renamed vim.tbl_isarray() to vim.isarray().
============================================================================== ==============================================================================
NEW FEATURES *news-features* NEW FEATURES *news-features*
@ -561,7 +563,8 @@ release.
populated. Background color detection is now performed in Lua by the Nvim populated. Background color detection is now performed in Lua by the Nvim
core, not the TUI. core, not the TUI.
vim.shared functions: Lua stdlib:
- |vim.tbl_add_reverse_lookup()| - |vim.tbl_add_reverse_lookup()|
- |vim.tbl_islist()|
vim:tw=78:ts=8:sw=2:et:ft=help:norl: vim:tw=78:ts=8:sw=2:et:ft=help:norl:

View File

@ -82,7 +82,7 @@ local vim9 = (function()
end end
M.index = function(obj, idx) M.index = function(obj, idx)
if vim.tbl_islist(obj) then if vim.islist(obj) then
if idx < 0 then if idx < 0 then
return obj[#obj + idx + 1] return obj[#obj + idx + 1]
else else
@ -127,7 +127,7 @@ local vim9 = (function()
assert(type(finish) == 'number') assert(type(finish) == 'number')
local slicer local slicer
if vim.tbl_islist(obj) then if vim.islist(obj) then
slicer = vim.list_slice slicer = vim.list_slice
elseif type(obj) == 'string' then elseif type(obj) == 'string' then
slicer = string.sub slicer = string.sub
@ -168,7 +168,7 @@ local vim9 = (function()
end end
M.iter = function(expr) M.iter = function(expr)
if vim.tbl_islist(expr) then if vim.islist(expr) then
return ipairs(expr) return ipairs(expr)
else else
return pairs(expr) return pairs(expr)
@ -234,7 +234,7 @@ vim9['convert'] = (function()
elseif type(val) == 'table' then elseif type(val) == 'table' then
if vim.tbl_isempty(val) then if vim.tbl_isempty(val) then
return vim.empty_dict() return vim.empty_dict()
elseif vim.tbl_islist(val) then elseif vim.islist(val) then
error(string.format('Cannot pass list to dictionary? %s', vim.inspect(val))) error(string.format('Cannot pass list to dictionary? %s', vim.inspect(val)))
else else
return val return val
@ -280,7 +280,7 @@ vim9['fn'] = (function()
error("haven't written this code yet") error("haven't written this code yet")
end end
if vim.tbl_islist(right) then if vim.islist(right) then
vim.list_extend(left, right) vim.list_extend(left, right)
return left return left
else else

View File

@ -642,7 +642,7 @@ end
--- @param t table<any,any> --- @param t table<any,any>
--- @param val any --- @param val any
local function remove_one_item(t, val) local function remove_one_item(t, val)
if vim.tbl_islist(t) then if vim.islist(t) then
local remove_index = nil local remove_index = nil
for i, v in ipairs(t) do for i, v in ipairs(t) do
if v == val then if v == val then

View File

@ -974,7 +974,7 @@ function M.set(namespace, bufnr, diagnostics, opts)
bufnr = { bufnr, 'n' }, bufnr = { bufnr, 'n' },
diagnostics = { diagnostics = {
diagnostics, diagnostics,
vim.tbl_islist, vim.islist,
'a list of diagnostics', 'a list of diagnostics',
}, },
opts = { opts, 't', true }, opts = { opts, 't', true },
@ -1186,7 +1186,7 @@ M.handlers.signs = {
bufnr = { bufnr, 'n' }, bufnr = { bufnr, 'n' },
diagnostics = { diagnostics = {
diagnostics, diagnostics,
vim.tbl_islist, vim.islist,
'a list of diagnostics', 'a list of diagnostics',
}, },
opts = { opts, 't', true }, opts = { opts, 't', true },
@ -1309,7 +1309,7 @@ M.handlers.underline = {
bufnr = { bufnr, 'n' }, bufnr = { bufnr, 'n' },
diagnostics = { diagnostics = {
diagnostics, diagnostics,
vim.tbl_islist, vim.islist,
'a list of diagnostics', 'a list of diagnostics',
}, },
opts = { opts, 't', true }, opts = { opts, 't', true },
@ -1382,7 +1382,7 @@ M.handlers.virtual_text = {
bufnr = { bufnr, 'n' }, bufnr = { bufnr, 'n' },
diagnostics = { diagnostics = {
diagnostics, diagnostics,
vim.tbl_islist, vim.islist,
'a list of diagnostics', 'a list of diagnostics',
}, },
opts = { opts, 't', true }, opts = { opts, 't', true },
@ -1576,7 +1576,7 @@ function M.show(namespace, bufnr, diagnostics, opts)
diagnostics = { diagnostics = {
diagnostics, diagnostics,
function(v) function(v)
return v == nil or vim.tbl_islist(v) return v == nil or vim.islist(v)
end, end,
'a list of diagnostics', 'a list of diagnostics',
}, },
@ -2120,7 +2120,7 @@ function M.toqflist(diagnostics)
vim.validate({ vim.validate({
diagnostics = { diagnostics = {
diagnostics, diagnostics,
vim.tbl_islist, vim.islist,
'a list of diagnostics', 'a list of diagnostics',
}, },
}) })
@ -2160,7 +2160,7 @@ function M.fromqflist(list)
vim.validate({ vim.validate({
list = { list = {
list, list,
vim.tbl_islist, vim.islist,
'a list of quickfix items', 'a list of quickfix items',
}, },
}) })

View File

@ -428,7 +428,7 @@ local function location_handler(_, result, ctx, config)
-- textDocument/definition can return Location or Location[] -- textDocument/definition can return Location or Location[]
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition -- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
if not vim.tbl_islist(result) then if not vim.islist(result) then
result = { result } result = { result }
end end

View File

@ -356,7 +356,7 @@ end
--- We only merge empty tables or tables that are not an array (indexed by integers) --- We only merge empty tables or tables that are not an array (indexed by integers)
local function can_merge(v) local function can_merge(v)
return type(v) == 'table' and (vim.tbl_isempty(v) or not vim.tbl_isarray(v)) return type(v) == 'table' and (vim.tbl_isempty(v) or not vim.isarray(v))
end end
local function tbl_extend(behavior, deep_extend, ...) local function tbl_extend(behavior, deep_extend, ...)
@ -502,7 +502,7 @@ end
--- ---
---@param o table Table to index ---@param o table Table to index
---@param ... any Optional keys (0 or more, variadic) via which to index the table ---@param ... any Optional keys (0 or more, variadic) via which to index the table
---@return any : Nested value indexed by key (if it exists), else nil ---@return any # Nested value indexed by key (if it exists), else nil
function vim.tbl_get(o, ...) function vim.tbl_get(o, ...)
local keys = { ... } local keys = { ... }
if #keys == 0 then if #keys == 0 then
@ -599,18 +599,24 @@ function vim.spairs(t)
t t
end end
--- @deprecated
function vim.tbl_isarray()
vim.deprecate('vim.tbl_isarray', 'vim.isarray', '0.10-dev')
error('vim.tbl_isarray was renamed to vim.isarray')
end
--- Tests if `t` is an "array": a table indexed _only_ by integers (potentially non-contiguous). --- Tests if `t` is an "array": a table indexed _only_ by integers (potentially non-contiguous).
--- ---
--- If the indexes start from 1 and are contiguous then the array is also a list. |vim.tbl_islist()| --- If the indexes start from 1 and are contiguous then the array is also a list. |vim.islist()|
--- ---
--- Empty table `{}` is an array, unless it was created by |vim.empty_dict()| or returned as --- Empty table `{}` is an array, unless it was created by |vim.empty_dict()| or returned as
--- a dict-like |API| or Vimscript result, for example from |rpcrequest()| or |vim.fn|. --- a dict-like |API| or Vimscript result, for example from |rpcrequest()| or |vim.fn|.
--- ---
---@see https://github.com/openresty/luajit2#tableisarray ---@see https://github.com/openresty/luajit2#tableisarray
--- ---
---@param t table ---@param t? table
---@return boolean `true` if array-like table, else `false`. ---@return boolean `true` if array-like table, else `false`.
function vim.tbl_isarray(t) function vim.isarray(t)
if type(t) ~= 'table' then if type(t) ~= 'table' then
return false return false
end end
@ -640,17 +646,23 @@ function vim.tbl_isarray(t)
end end
end end
--- @deprecated
function vim.tbl_islist(t)
vim.deprecate('vim.tbl_islist', 'vim.islist', '0.12')
return vim.islist(t)
end
--- Tests if `t` is a "list": a table indexed _only_ by contiguous integers starting from 1 (what --- Tests if `t` is a "list": a table indexed _only_ by contiguous integers starting from 1 (what
--- |lua-length| calls a "regular array"). --- |lua-length| calls a "regular array").
--- ---
--- Empty table `{}` is a list, unless it was created by |vim.empty_dict()| or returned as --- Empty table `{}` is a list, unless it was created by |vim.empty_dict()| or returned as
--- a dict-like |API| or Vimscript result, for example from |rpcrequest()| or |vim.fn|. --- a dict-like |API| or Vimscript result, for example from |rpcrequest()| or |vim.fn|.
--- ---
---@see |vim.tbl_isarray()| ---@see |vim.isarray()|
--- ---
---@param t table ---@param t? table
---@return boolean `true` if list-like table, else `false`. ---@return boolean `true` if list-like table, else `false`.
function vim.tbl_islist(t) function vim.islist(t)
if type(t) ~= 'table' then if type(t) ~= 'table' then
return false return false
end end

View File

@ -22,7 +22,7 @@ describe('lua vim.mpack', function()
{ { {}, 'foo', {} }, true, false }, { { {}, 'foo', {} }, true, false },
exec_lua [[ exec_lua [[
local var = vim.mpack.decode(vim.mpack.encode({{}, "foo", vim.empty_dict()})) local var = vim.mpack.decode(vim.mpack.encode({{}, "foo", vim.empty_dict()}))
return {var, vim.tbl_islist(var[1]), vim.tbl_islist(var[3])} return {var, vim.islist(var[1]), vim.islist(var[3])}
]] ]]
) )
end) end)

View File

@ -673,21 +673,21 @@ describe('lua stdlib', function()
local a = {} local a = {}
local b = vim.deepcopy(a) local b = vim.deepcopy(a)
return vim.tbl_islist(b) and vim.tbl_count(b) == 0 and tostring(a) ~= tostring(b) return vim.islist(b) and vim.tbl_count(b) == 0 and tostring(a) ~= tostring(b)
]])) ]]))
ok(exec_lua([[ ok(exec_lua([[
local a = vim.empty_dict() local a = vim.empty_dict()
local b = vim.deepcopy(a) local b = vim.deepcopy(a)
return not vim.tbl_islist(b) and vim.tbl_count(b) == 0 return not vim.islist(b) and vim.tbl_count(b) == 0
]])) ]]))
ok(exec_lua([[ ok(exec_lua([[
local a = {x = vim.empty_dict(), y = {}} local a = {x = vim.empty_dict(), y = {}}
local b = vim.deepcopy(a) local b = vim.deepcopy(a)
return not vim.tbl_islist(b.x) and vim.tbl_islist(b.y) return not vim.islist(b.x) and vim.islist(b.y)
and vim.tbl_count(b) == 2 and vim.tbl_count(b) == 2
and tostring(a) ~= tostring(b) and tostring(a) ~= tostring(b)
]])) ]]))
@ -825,30 +825,30 @@ describe('lua stdlib', function()
) )
end) end)
it('vim.tbl_isarray', function() it('vim.isarray', function()
eq(true, exec_lua('return vim.tbl_isarray({})')) eq(true, exec_lua('return vim.isarray({})'))
eq(false, exec_lua('return vim.tbl_isarray(vim.empty_dict())')) eq(false, exec_lua('return vim.isarray(vim.empty_dict())'))
eq(true, exec_lua("return vim.tbl_isarray({'a', 'b', 'c'})")) eq(true, exec_lua("return vim.isarray({'a', 'b', 'c'})"))
eq(false, exec_lua("return vim.tbl_isarray({'a', '32', a='hello', b='baz'})")) eq(false, exec_lua("return vim.isarray({'a', '32', a='hello', b='baz'})"))
eq(false, exec_lua("return vim.tbl_isarray({1, a='hello', b='baz'})")) eq(false, exec_lua("return vim.isarray({1, a='hello', b='baz'})"))
eq(false, exec_lua("return vim.tbl_isarray({a='hello', b='baz', 1})")) eq(false, exec_lua("return vim.isarray({a='hello', b='baz', 1})"))
eq(false, exec_lua("return vim.tbl_isarray({1, 2, nil, a='hello'})")) eq(false, exec_lua("return vim.isarray({1, 2, nil, a='hello'})"))
eq(true, exec_lua('return vim.tbl_isarray({1, 2, nil, 4})')) eq(true, exec_lua('return vim.isarray({1, 2, nil, 4})'))
eq(true, exec_lua('return vim.tbl_isarray({nil, 2, 3, 4})')) eq(true, exec_lua('return vim.isarray({nil, 2, 3, 4})'))
eq(false, exec_lua('return vim.tbl_isarray({1, [1.5]=2, [3]=3})')) eq(false, exec_lua('return vim.isarray({1, [1.5]=2, [3]=3})'))
end) end)
it('vim.tbl_islist', function() it('vim.islist', function()
eq(true, exec_lua('return vim.tbl_islist({})')) eq(true, exec_lua('return vim.islist({})'))
eq(false, exec_lua('return vim.tbl_islist(vim.empty_dict())')) eq(false, exec_lua('return vim.islist(vim.empty_dict())'))
eq(true, exec_lua("return vim.tbl_islist({'a', 'b', 'c'})")) eq(true, exec_lua("return vim.islist({'a', 'b', 'c'})"))
eq(false, exec_lua("return vim.tbl_islist({'a', '32', a='hello', b='baz'})")) eq(false, exec_lua("return vim.islist({'a', '32', a='hello', b='baz'})"))
eq(false, exec_lua("return vim.tbl_islist({1, a='hello', b='baz'})")) eq(false, exec_lua("return vim.islist({1, a='hello', b='baz'})"))
eq(false, exec_lua("return vim.tbl_islist({a='hello', b='baz', 1})")) eq(false, exec_lua("return vim.islist({a='hello', b='baz', 1})"))
eq(false, exec_lua("return vim.tbl_islist({1, 2, nil, a='hello'})")) eq(false, exec_lua("return vim.islist({1, 2, nil, a='hello'})"))
eq(false, exec_lua('return vim.tbl_islist({1, 2, nil, 4})')) eq(false, exec_lua('return vim.islist({1, 2, nil, 4})'))
eq(false, exec_lua('return vim.tbl_islist({nil, 2, 3, 4})')) eq(false, exec_lua('return vim.islist({nil, 2, 3, 4})'))
eq(false, exec_lua('return vim.tbl_islist({1, [1.5]=2, [3]=3})')) eq(false, exec_lua('return vim.islist({1, [1.5]=2, [3]=3})'))
end) end)
it('vim.tbl_isempty', function() it('vim.tbl_isempty', function()
@ -921,7 +921,7 @@ describe('lua stdlib', function()
local b = {} local b = {}
local c = vim.tbl_extend("keep", a, b) local c = vim.tbl_extend("keep", a, b)
return not vim.tbl_islist(c) and vim.tbl_count(c) == 0 return not vim.islist(c) and vim.tbl_count(c) == 0
]])) ]]))
ok(exec_lua([[ ok(exec_lua([[
@ -929,7 +929,7 @@ describe('lua stdlib', function()
local b = vim.empty_dict() local b = vim.empty_dict()
local c = vim.tbl_extend("keep", a, b) local c = vim.tbl_extend("keep", a, b)
return vim.tbl_islist(c) and vim.tbl_count(c) == 0 return vim.islist(c) and vim.tbl_count(c) == 0
]])) ]]))
ok(exec_lua([[ ok(exec_lua([[
@ -1029,7 +1029,7 @@ describe('lua stdlib', function()
local count = 0 local count = 0
for _ in pairs(c) do count = count + 1 end for _ in pairs(c) do count = count + 1 end
return not vim.tbl_islist(c) and count == 0 return not vim.islist(c) and count == 0
]])) ]]))
ok(exec_lua([[ ok(exec_lua([[
@ -1040,7 +1040,7 @@ describe('lua stdlib', function()
local count = 0 local count = 0
for _ in pairs(c) do count = count + 1 end for _ in pairs(c) do count = count + 1 end
return vim.tbl_islist(c) and count == 0 return vim.islist(c) and count == 0
]])) ]]))
eq( eq(
@ -1285,7 +1285,7 @@ describe('lua stdlib', function()
vim.rpcrequest(chan, 'nvim_exec', 'let xx = {}\nlet yy = []', false) vim.rpcrequest(chan, 'nvim_exec', 'let xx = {}\nlet yy = []', false)
local dict = vim.rpcrequest(chan, 'nvim_eval', 'xx') local dict = vim.rpcrequest(chan, 'nvim_eval', 'xx')
local list = vim.rpcrequest(chan, 'nvim_eval', 'yy') local list = vim.rpcrequest(chan, 'nvim_eval', 'yy')
return {dict, list, vim.tbl_islist(dict), vim.tbl_islist(list)} return {dict, list, vim.islist(dict), vim.islist(list)}
]]) ]])
) )
@ -1358,7 +1358,7 @@ describe('lua stdlib', function()
vim.api.nvim_set_var('dicty', vim.empty_dict()) vim.api.nvim_set_var('dicty', vim.empty_dict())
local listy = vim.fn.eval("listy") local listy = vim.fn.eval("listy")
local dicty = vim.fn.eval("dicty") local dicty = vim.fn.eval("dicty")
return {vim.tbl_islist(listy), vim.tbl_islist(dicty), next(listy) == nil, next(dicty) == nil} return {vim.islist(listy), vim.islist(dicty), next(listy) == nil, next(dicty) == nil}
]]) ]])
) )