aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/shared.lua
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2022-09-12 10:57:30 -0600
committerJosh Rahm <rahm@google.com>2022-09-12 10:57:30 -0600
commitfef78e339a10ac4bfcf973b8d14d6a759ec9d808 (patch)
tree76c92e54f8d1e5333e0e071005e48c52150a1304 /runtime/lua/vim/shared.lua
parent24c75e4f7be4eb4052939e7d5db7721d88288604 (diff)
parentfd70e2bff2440181f63fe124738cf2a025d1e6a5 (diff)
downloadrneovim-fef78e339a10ac4bfcf973b8d14d6a759ec9d808.tar.gz
rneovim-fef78e339a10ac4bfcf973b8d14d6a759ec9d808.tar.bz2
rneovim-fef78e339a10ac4bfcf973b8d14d6a759ec9d808.zip
Merge remote-tracking branch 'upstream/master' into userreg
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r--runtime/lua/vim/shared.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index e1b4ed4ea9..59cb669609 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -715,5 +715,30 @@ function vim.is_callable(f)
return type(m.__call) == 'function'
end
+--- Creates a table whose members are automatically created when accessed, if they don't already
+--- exist.
+---
+--- They mimic defaultdict in python.
+---
+--- If @p create is @c nil, this will create a defaulttable whose constructor function is
+--- this function, effectively allowing to create nested tables on the fly:
+---
+--- <pre>
+--- local a = vim.defaulttable()
+--- a.b.c = 1
+--- </pre>
+---
+---@param create function|nil The function called to create a missing value.
+---@return table Empty table with metamethod
+function vim.defaulttable(create)
+ create = create or vim.defaulttable
+ return setmetatable({}, {
+ __index = function(tbl, key)
+ rawset(tbl, key, create())
+ return rawget(tbl, key)
+ end,
+ })
+end
+
return vim
-- vim:sw=2 ts=2 et