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
This commit is contained in:
parent
a4410d681f
commit
fb81d8b2b9
|
@ -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()
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
local Util = {}
|
local M = {}
|
||||||
|
|
||||||
-- Reload a given module, returning the result of loading it.
|
-- Reload a given module, returning the result of loading it.
|
||||||
function Util.mreload(module)
|
function M.mreload(module)
|
||||||
if package.loaded[module] then
|
if package.loaded[module] then
|
||||||
package.loaded[module] = nil
|
package.loaded[module] = nil
|
||||||
end
|
end
|
||||||
|
@ -11,8 +11,8 @@ function Util.mreload(module)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Try reloading the given module, returning ok, module
|
-- Try reloading the given module, returning ok, module
|
||||||
function Util.try_mreload(module)
|
function M.try_mreload(module)
|
||||||
return pcall(Util.mreload, module)
|
return pcall(M.mreload, module)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Try reloading the given config module, returning either
|
-- Try reloading the given config module, returning either
|
||||||
|
@ -26,7 +26,7 @@ end
|
||||||
--
|
--
|
||||||
-- It the latter cases, *configuration will not be reloaded*, this is
|
-- It the latter cases, *configuration will not be reloaded*, this is
|
||||||
-- primaraily meant for inline, static configuration
|
-- primaraily meant for inline, static configuration
|
||||||
function Util.try_mconfig(module)
|
function M.try_mconfig(module)
|
||||||
if type(module) ~= "string" then
|
if type(module) ~= "string" then
|
||||||
module = type(module) == "function" and module() or module
|
module = type(module) == "function" and module() or module
|
||||||
module = type(module) == "table" and module or {}
|
module = type(module) == "table" and module or {}
|
||||||
|
@ -34,7 +34,7 @@ function Util.try_mconfig(module)
|
||||||
return module
|
return module
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, config = Util.try_mreload(module)
|
local ok, config = M.try_mreload(module)
|
||||||
config = type(config) == "function" and config() or config
|
config = type(config) == "function" and config() or config
|
||||||
|
|
||||||
return (ok and type(config) == "table") and config or {}
|
return (ok and type(config) == "table") and config or {}
|
||||||
|
@ -50,13 +50,13 @@ end
|
||||||
-- 4. fn() --> table
|
-- 4. fn() --> table
|
||||||
--
|
--
|
||||||
-- It the latter cases, *configuration will not be reloaded*, this is
|
-- 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
|
-- Note: the config returned from the given module may optionally
|
||||||
-- set 'no_defaults = true' to opt out of the merging
|
-- set 'no_defaults = true' to opt out of the merging
|
||||||
function Util.mconfig(module, defaults)
|
function M.mconfig(module, defaults)
|
||||||
module = Util.try_mconfig(module)
|
module = M.try_mconfig(module)
|
||||||
defaults = Util.try_mconfig(defaults)
|
defaults = M.try_mconfig(defaults)
|
||||||
|
|
||||||
if module.no_defaults then
|
if module.no_defaults then
|
||||||
return module
|
return module
|
||||||
|
@ -65,4 +65,4 @@ function Util.mconfig(module, defaults)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return Util
|
return M
|
Loading…
Reference in New Issue