aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/F.lua
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2023-04-07 08:22:47 -0600
committerGitHub <noreply@github.com>2023-04-07 08:22:47 -0600
commitd675bd01b1e78b93e559320b262bdae40b3b54b2 (patch)
tree2ede624b4ad283bcadebbd26382d7521d6114937 /runtime/lua/vim/F.lua
parent82cfedab5009c0f5003451ef0b105a019887edb3 (diff)
downloadrneovim-d675bd01b1e78b93e559320b262bdae40b3b54b2.tar.gz
rneovim-d675bd01b1e78b93e559320b262bdae40b3b54b2.tar.bz2
rneovim-d675bd01b1e78b93e559320b262bdae40b3b54b2.zip
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)
Diffstat (limited to 'runtime/lua/vim/F.lua')
-rw-r--r--runtime/lua/vim/F.lua31
1 files changed, 21 insertions, 10 deletions
diff --git a/runtime/lua/vim/F.lua b/runtime/lua/vim/F.lua
index 3e370c0a84..16c834b371 100644
--- a/runtime/lua/vim/F.lua
+++ b/runtime/lua/vim/F.lua
@@ -1,18 +1,29 @@
local F = {}
---- Returns {a} if it is not nil, otherwise returns {b}.
+--- Returns the first argument which is not nil.
---
----@generic A
----@generic B
+--- If all arguments are nil, returns nil.
---
----@param a A
----@param b B
----@return A | B
-function F.if_nil(a, b)
- if a == nil then
- return b
+--- Examples:
+--- <pre>
+--- local a = nil
+--- local b = nil
+--- local c = 42
+--- 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
- return a
+ return nil
end
-- Use in combination with pcall