| 1 |
-- Copyright (c) 2020-2021 shadmansaleh |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local M = {} |
| 4 |
|
| 5 |
M.sep = package.config:sub(1, 1) |
| 6 |
local luv = vim.uv or vim.loop |
| 7 |
|
| 8 |
-- Figures out full path of lualine installation |
| 9 |
local source = debug.getinfo(1, 'S').source |
| 10 |
if source:sub(1, 1) == '@' then |
| 11 |
local base_start = source:find(table.concat({ 'lualine.nvim', 'lua', 'lualine_require.lua' }, M.sep)) |
| 12 |
if base_start then |
| 13 |
source = source:sub(2, base_start + #'lualine.nvim/lua') |
| 14 |
if source then |
| 15 |
M.plugin_dir = source |
| 16 |
end |
| 17 |
end |
| 18 |
end |
| 19 |
|
| 20 |
--- checks if name is valid |
| 21 |
---@param name string |
| 22 |
---@return boolean |
| 23 |
function M.is_valid_filename(name) |
| 24 |
local invalid_chars = '[^a-zA-Z0-9_. -]' |
| 25 |
return name:find(invalid_chars) == nil |
| 26 |
end |
| 27 |
|
| 28 |
---require module module |
| 29 |
---@param module string module_name |
| 30 |
---@return any the required module |
| 31 |
function M.require(module) |
| 32 |
if not package or not package.loaded then |
| 33 |
-- Something wrong with execution environment. Maybe plugin manager cleared it. |
| 34 |
-- Just let regular require handle. We'll use the cache next time. |
| 35 |
return require(module) |
| 36 |
end |
| 37 |
if package.loaded[module] then |
| 38 |
return package.loaded[module] |
| 39 |
end |
| 40 |
local pattern_dir = module:gsub('%.', M.sep) |
| 41 |
local pattern_path = pattern_dir .. '.lua' |
| 42 |
if M.plugin_dir then |
| 43 |
local path = M.plugin_dir .. pattern_path |
| 44 |
assert(M.is_valid_filename(module), 'Invalid filename') |
| 45 |
local file_stat, dir_stat |
| 46 |
file_stat = luv.fs_stat(path) |
| 47 |
if not file_stat then |
| 48 |
path = M.plugin_dir .. pattern_dir |
| 49 |
dir_stat = luv.fs_stat(path) |
| 50 |
if dir_stat and dir_stat.type == 'directory' then |
| 51 |
path = path .. M.sep .. 'init.lua' |
| 52 |
file_stat = luv.fs_stat(path) |
| 53 |
end |
| 54 |
end |
| 55 |
if file_stat and file_stat.type == 'file' then |
| 56 |
local mod_result = dofile(path) |
| 57 |
package.loaded[module] = mod_result |
| 58 |
return mod_result |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
pattern_path = table.concat { 'lua/', module:gsub('%.', '/'), '.lua' } |
| 63 |
local paths = vim.api.nvim_get_runtime_file(pattern_path, true) |
| 64 |
if #paths <= 0 then |
| 65 |
pattern_path = table.concat { 'lua/', module:gsub('%.', '/'), '/init.lua' } |
| 66 |
paths = vim.api.nvim_get_runtime_file(pattern_path, true) |
| 67 |
end |
| 68 |
if #paths > 0 then |
| 69 |
-- put entries from user config path in front |
| 70 |
local user_config_path = vim.fn.stdpath('config') |
| 71 |
table.sort(paths, function(a, b) |
| 72 |
local pattern = table.concat { user_config_path, M.sep } |
| 73 |
return string.match(a, pattern) or not string.match(b, pattern) |
| 74 |
end) |
| 75 |
local mod_result = dofile(paths[1]) |
| 76 |
package.loaded[module] = mod_result |
| 77 |
return mod_result |
| 78 |
end |
| 79 |
|
| 80 |
return require(module) |
| 81 |
end |
| 82 |
|
| 83 |
---requires modules when they are used |
| 84 |
---@param modules table k-v table where v is module path and k is name that will |
| 85 |
--- be indexed |
| 86 |
---@return table metatable where when a key is indexed it gets required and cached |
| 87 |
function M.lazy_require(modules) |
| 88 |
return setmetatable({}, { |
| 89 |
__index = function(self, key) |
| 90 |
local loaded = rawget(self, key) |
| 91 |
if loaded ~= nil then |
| 92 |
return loaded |
| 93 |
end |
| 94 |
local module_location = modules[key] |
| 95 |
if module_location == nil then |
| 96 |
return nil |
| 97 |
end |
| 98 |
local module = M.require(module_location) |
| 99 |
rawset(self, key, module) |
| 100 |
return module |
| 101 |
end, |
| 102 |
}) |
| 103 |
end |
| 104 |
|
| 105 |
return M |