diff options
Diffstat (limited to 'lua/telescope/utils.lua')
| -rw-r--r-- | lua/telescope/utils.lua | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index b95f626..6154889 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -31,7 +31,7 @@ utils.reversed_ipairs = function(t) return reversedipairsiter, t, #t + 1 end -utils.default_table_mt = { +utils.default_table_mt = { __index = function(t, k) local obj = {} rawset(t, k, obj) @@ -114,4 +114,28 @@ utils.path_shorten = (function() end end)() +-- local x = utils.make_default_callable(function(opts) +-- return function() +-- print(opts.example, opts.another) +-- end +-- end, { example = 7, another = 5 }) + +-- x() +-- x.new { example = 3 }() +function utils.make_default_callable(f, default_opts) + return setmetatable({ + new = function(opts) + opts = vim.tbl_extend("keep", opts, default_opts) + return f(opts) + end, + }, { + __call = function() + local ok, err = pcall(f(default_opts)) + if not ok then + error(debug.traceback(err)) + end + end + }) +end + return utils |
