mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
feat(lua): allow vim.F.if_nil to take multiple arguments (#22903)
The first argument which is non-nil is returned. This is useful when using nested default values (e.g. in the EditorConfig plugin). Before: local enable = vim.F.if_nil(vim.b.editorconfig, vim.F.if_nil(vim.g.editorconfig, true)) After: local enable = vim.F.if_nil(vim.b.editorconfig, vim.g.editorconfig, true)
This commit is contained in:
@@ -1,18 +1,29 @@
|
|||||||
local F = {}
|
local F = {}
|
||||||
|
|
||||||
--- Returns {a} if it is not nil, otherwise returns {b}.
|
--- Returns the first argument which is not nil.
|
||||||
---
|
---
|
||||||
---@generic A
|
--- If all arguments are nil, returns nil.
|
||||||
---@generic B
|
|
||||||
---
|
---
|
||||||
---@param a A
|
--- Examples:
|
||||||
---@param b B
|
--- <pre>
|
||||||
---@return A | B
|
--- local a = nil
|
||||||
function F.if_nil(a, b)
|
--- local b = nil
|
||||||
if a == nil then
|
--- local c = 42
|
||||||
return b
|
--- local d = true
|
||||||
|
--- assert(vim.F.if_nil(a, b, c, d) == 42)
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
---@param ... any
|
||||||
|
---@return any
|
||||||
|
function F.if_nil(...)
|
||||||
|
local nargs = select('#', ...)
|
||||||
|
for i = 1, nargs do
|
||||||
|
local v = select(i, ...)
|
||||||
|
if v ~= nil then
|
||||||
|
return v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return a
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Use in combination with pcall
|
-- Use in combination with pcall
|
||||||
|
@@ -3,7 +3,7 @@ vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead', 'BufFilePost' }, {
|
|||||||
group = group,
|
group = group,
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
-- Buffer-local enable has higher priority
|
-- Buffer-local enable has higher priority
|
||||||
local enable = vim.F.if_nil(vim.b.editorconfig, vim.F.if_nil(vim.g.editorconfig, true))
|
local enable = vim.F.if_nil(vim.b.editorconfig, vim.g.editorconfig, true)
|
||||||
if not enable then
|
if not enable then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@@ -2967,6 +2967,32 @@ describe('lua stdlib', function()
|
|||||||
}]],
|
}]],
|
||||||
eval[[execute('lua vim.print(42, "abc", { a = { b = 77 }})')]])
|
eval[[execute('lua vim.print(42, "abc", { a = { b = 77 }})')]])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('vim.F.if_nil', function()
|
||||||
|
local function if_nil(...)
|
||||||
|
return exec_lua([[
|
||||||
|
local args = {...}
|
||||||
|
local nargs = select('#', ...)
|
||||||
|
for i = 1, nargs do
|
||||||
|
if args[i] == vim.NIL then
|
||||||
|
args[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return vim.F.if_nil(unpack(args, 1, nargs))
|
||||||
|
]], ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
local a = NIL
|
||||||
|
local b = NIL
|
||||||
|
local c = 42
|
||||||
|
local d = false
|
||||||
|
eq(42, if_nil(a, c))
|
||||||
|
eq(false, if_nil(d, b))
|
||||||
|
eq(42, if_nil(a, b, c, d))
|
||||||
|
eq(false, if_nil(d))
|
||||||
|
eq(false, if_nil(d, c))
|
||||||
|
eq(NIL, if_nil(a))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('lua: builtin modules', function()
|
describe('lua: builtin modules', function()
|
||||||
|
Reference in New Issue
Block a user