Enhace: add a faster fallback for lualine_require for requiring files out of repo

If that fails it still falls back to original require a last resort
This commit is contained in:
shadmansaleh 2021-08-30 23:44:28 +06:00
parent 146ec73887
commit e409e9fc32
2 changed files with 24 additions and 12 deletions

View File

@ -8,7 +8,7 @@ local modules = lualine_require.lazy_require{
} }
local is_valid_filename = lualine_require.is_valid_filename local is_valid_filename = lualine_require.is_valid_filename
local sep = package.config:sub(1,1) local sep = lualine_require.sep
local component_types = { local component_types = {
luaf = function(component) luaf = function(component)
@ -132,20 +132,11 @@ local function load_all(config)
load_extensions(config) load_extensions(config)
end end
local function rtp_searcher(file)
local ret = {}
for dir in vim.gsplit(vim.api.nvim_get_option('rtp'), ',') do
local path = dir .. sep .. file
if vim.loop.fs_stat(path) then ret[#ret+1] = path end
end
return ret
end
local function load_theme(theme_name) local function load_theme(theme_name)
assert(is_valid_filename(theme_name), "Invalid filename") assert(is_valid_filename(theme_name), "Invalid filename")
local retval local retval
local path = table.concat({'lua', 'lualine', 'themes', theme_name}, sep)..'.lua' local path = table.concat({'lua', 'lualine', 'themes', theme_name}, sep)..'.lua'
local files = rtp_searcher(path) local files = lualine_require.rtp_searcher(path)
local n_files = #files local n_files = #files
if n_files == 0 then if n_files == 0 then
-- No match found -- No match found

View File

@ -18,8 +18,9 @@ end
function M.require(module) function M.require(module)
if package.loaded[module] then return package.loaded[module] end if package.loaded[module] then return package.loaded[module] end
local pattern = module:gsub('%.', M.sep) .. '.lua'
if M.plugin_dir then if M.plugin_dir then
local path = M.plugin_dir .. module:gsub('%.', M.sep) .. '.lua' local path = M.plugin_dir .. pattern
assert(M.is_valid_filename(module), "Invalid filename") assert(M.is_valid_filename(module), "Invalid filename")
if vim.loop.fs_stat(path) then if vim.loop.fs_stat(path) then
local mod_result = dofile(path) local mod_result = dofile(path)
@ -27,9 +28,29 @@ function M.require(module)
return mod_result return mod_result
end end
end end
local paths = M.rtp_searcher('lua'..M.sep..pattern, true)
if #paths > 0 then
local mod_result = dofile(paths[1])
package.loaded[module] = mod_result
return mod_result
end
return require(module) return require(module)
end end
function M.rtp_searcher(file, once)
local ret = {}
for dir in vim.gsplit(vim.api.nvim_get_option('rtp'), ',') do
local path = dir .. M.sep .. file
if vim.loop.fs_stat(path) then
ret[#ret+1] = path
if once then break end
end
end
return ret
end
function M.lazy_require(modules) function M.lazy_require(modules)
return setmetatable({}, { return setmetatable({}, {
__index = function(self, key) __index = function(self, key)