From fb81d8b2b917f13e52035c9b2d6fe6147c694057 Mon Sep 17 00:00:00 2001 From: Bazaah Date: Sun, 28 Aug 2022 08:53:27 +0000 Subject: [PATCH] nvim/util: reorganize to allow for multiple submodules This change moves the original module loading utilities to util/mload.lua, while providing a shim object that lazily attempts to locate a function that matches the name of the indexed object. This changes allows me to better organize utility functions --- nvim/lua/psoxizsh/util/init.lua | 49 +++++++++++++++++++ .../lua/psoxizsh/{util.lua => util/mload.lua} | 22 ++++----- 2 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 nvim/lua/psoxizsh/util/init.lua rename nvim/lua/psoxizsh/{util.lua => util/mload.lua} (79%) diff --git a/nvim/lua/psoxizsh/util/init.lua b/nvim/lua/psoxizsh/util/init.lua new file mode 100644 index 0000000..0568727 --- /dev/null +++ b/nvim/lua/psoxizsh/util/init.lua @@ -0,0 +1,49 @@ + +local Util = { mt = {} } +local Lazy = { mt = {} } + +local Submodules = { + mload = 'psoxizsh.util.mload', +} + +function Util.new() + local this = { _submodules = Util.submodules() } + setmetatable(this, Util.mt) + + return this +end + +function Util.submodules() + local modules = {} + + for key, mod in pairs(Submodules) do + modules[key] = Lazy.new(mod) + end + + return modules +end + +function Util.mt.__index(self, key) + for _, submod in pairs(self._submodules) do + if submod[key] then return submod[key] end + end + + return nil +end + +function Lazy.new(mod_name) + local this = { _module = false, _mod_name = mod_name } + setmetatable(this, Lazy.mt) + + return this +end + +function Lazy.mt.__index(self, key) + if not self._module then + self._module = require(self._mod_name) + end + + return self._module[key] +end + +return Util.new() diff --git a/nvim/lua/psoxizsh/util.lua b/nvim/lua/psoxizsh/util/mload.lua similarity index 79% rename from nvim/lua/psoxizsh/util.lua rename to nvim/lua/psoxizsh/util/mload.lua index 1b6d80c..c956c27 100644 --- a/nvim/lua/psoxizsh/util.lua +++ b/nvim/lua/psoxizsh/util/mload.lua @@ -1,8 +1,8 @@ -local Util = {} +local M = {} -- Reload a given module, returning the result of loading it. -function Util.mreload(module) +function M.mreload(module) if package.loaded[module] then package.loaded[module] = nil end @@ -11,8 +11,8 @@ function Util.mreload(module) end -- Try reloading the given module, returning ok, module -function Util.try_mreload(module) - return pcall(Util.mreload, module) +function M.try_mreload(module) + return pcall(M.mreload, module) end -- Try reloading the given config module, returning either @@ -26,7 +26,7 @@ end -- -- It the latter cases, *configuration will not be reloaded*, this is -- primaraily meant for inline, static configuration -function Util.try_mconfig(module) +function M.try_mconfig(module) if type(module) ~= "string" then module = type(module) == "function" and module() or module module = type(module) == "table" and module or {} @@ -34,7 +34,7 @@ function Util.try_mconfig(module) return module end - local ok, config = Util.try_mreload(module) + local ok, config = M.try_mreload(module) config = type(config) == "function" and config() or config return (ok and type(config) == "table") and config or {} @@ -50,13 +50,13 @@ end -- 4. fn() --> table -- -- It the latter cases, *configuration will not be reloaded*, this is --- primaraily meant for inline, static configuration +-- primarily meant for inline, static configuration -- -- Note: the config returned from the given module may optionally -- set 'no_defaults = true' to opt out of the merging -function Util.mconfig(module, defaults) - module = Util.try_mconfig(module) - defaults = Util.try_mconfig(defaults) +function M.mconfig(module, defaults) + module = M.try_mconfig(module) + defaults = M.try_mconfig(defaults) if module.no_defaults then return module @@ -65,4 +65,4 @@ function Util.mconfig(module, defaults) end end -return Util +return M