2021-09-20 11:46:38 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-08-30 11:54:23 +00:00
|
|
|
local M = {}
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
M.sep = package.config:sub(1, 1)
|
2021-08-30 11:54:23 +00:00
|
|
|
|
2022-03-27 08:03:24 +00:00
|
|
|
-- Figures out full path of lualine installation
|
2021-09-03 18:28:20 +00:00
|
|
|
local source = debug.getinfo(1, 'S').source
|
|
|
|
if source:sub(1, 1) == '@' then
|
|
|
|
local base_start = source:find(table.concat({ 'lualine.nvim', 'lua', 'lualine_require.lua' }, M.sep))
|
2021-08-30 11:54:23 +00:00
|
|
|
if base_start then
|
|
|
|
source = source:sub(2, base_start + 12 + 1 + 3) -- #lualine.nvim = 12 , #lua = 3.
|
2021-09-03 18:28:20 +00:00
|
|
|
if source then
|
|
|
|
M.plugin_dir = source
|
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- checks if name is valied
|
|
|
|
---@param name string
|
|
|
|
---@return boolean
|
2021-08-30 11:54:23 +00:00
|
|
|
function M.is_valid_filename(name)
|
2021-09-03 18:28:20 +00:00
|
|
|
local invalid_chars = '[^a-zA-Z0-9_. -]'
|
2021-08-30 11:54:23 +00:00
|
|
|
return name:find(invalid_chars) == nil
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---require module module
|
|
|
|
---@param module string mogule arraived
|
|
|
|
---@return any the required module
|
2021-08-30 11:54:23 +00:00
|
|
|
function M.require(module)
|
2021-09-03 18:28:20 +00:00
|
|
|
if package.loaded[module] then
|
|
|
|
return package.loaded[module]
|
|
|
|
end
|
2021-10-10 18:04:55 +00:00
|
|
|
local pattern_dir = module:gsub('%.', M.sep)
|
|
|
|
local pattern_path = pattern_dir .. '.lua'
|
2021-08-30 11:54:23 +00:00
|
|
|
if M.plugin_dir then
|
2021-10-10 18:04:55 +00:00
|
|
|
local path = M.plugin_dir .. pattern_path
|
2021-09-03 18:28:20 +00:00
|
|
|
assert(M.is_valid_filename(module), 'Invalid filename')
|
2021-10-10 18:04:55 +00:00
|
|
|
local file_stat, dir_stat
|
|
|
|
file_stat = vim.loop.fs_stat(path)
|
|
|
|
if not file_stat then
|
|
|
|
path = M.plugin_dir .. pattern_dir
|
|
|
|
dir_stat = vim.loop.fs_stat(path)
|
|
|
|
if dir_stat and dir_stat.type == 'directory' then
|
|
|
|
path = path .. M.sep .. 'init.lua'
|
|
|
|
file_stat = vim.loop.fs_stat(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if file_stat and file_stat.type == 'file' then
|
2021-08-30 11:54:23 +00:00
|
|
|
local mod_result = dofile(path)
|
|
|
|
package.loaded[module] = mod_result
|
|
|
|
return mod_result
|
|
|
|
end
|
|
|
|
end
|
2021-08-30 17:44:28 +00:00
|
|
|
|
2022-02-01 08:04:03 +00:00
|
|
|
pattern_path = table.concat { 'lua/', module:gsub('%.', '/'), '.lua' }
|
2021-10-10 18:04:55 +00:00
|
|
|
local paths = vim.api.nvim_get_runtime_file(pattern_path, false)
|
|
|
|
if #paths <= 0 then
|
2022-02-01 08:04:03 +00:00
|
|
|
pattern_path = table.concat { 'lua/', module:gsub('%.', '/'), '/init.lua' }
|
2021-10-10 18:04:55 +00:00
|
|
|
paths = vim.api.nvim_get_runtime_file(pattern_path, false)
|
|
|
|
end
|
2021-08-30 17:44:28 +00:00
|
|
|
if #paths > 0 then
|
|
|
|
local mod_result = dofile(paths[1])
|
|
|
|
package.loaded[module] = mod_result
|
|
|
|
return mod_result
|
|
|
|
end
|
|
|
|
|
2021-08-30 11:54:23 +00:00
|
|
|
return require(module)
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---requires modules when they are used
|
|
|
|
---@param modules table k-v table where v is module path and k is name that will
|
|
|
|
--- be indexed
|
|
|
|
---@return table metatable where when a key is indexed it gets required and cached
|
2021-08-30 11:54:23 +00:00
|
|
|
function M.lazy_require(modules)
|
|
|
|
return setmetatable({}, {
|
|
|
|
__index = function(self, key)
|
|
|
|
local loaded = rawget(self, key)
|
2021-09-03 18:28:20 +00:00
|
|
|
if loaded ~= nil then
|
|
|
|
return loaded
|
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
local module_location = modules[key]
|
2021-09-03 18:28:20 +00:00
|
|
|
if module_location == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
local module = M.require(module_location)
|
|
|
|
rawset(self, key, module)
|
|
|
|
return module
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-08-30 11:54:23 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|