I'm trying to implement a Lua script that can dynamically load NeoVim .lua configuration files, given a single folder name.
Here's the requireaPath
function
function requirePath(path)
local files = io.popen('find ./lua/' .. path .. ' -type f')
for file in files:lines() do
local req_file = file:gmatch('(' .. path .. '%/.+)%.lua$'){0}:gsub('/', '.')
status_ok, _ = pcall(require, req_file)
if status_ok then
vim.notify('Loaded ' .. req_file)
else
vim.notify('Failed loading ' .. req_file, vim.log.levels.ERROR)
end
end
end
So instead of manually requiring the files, which will be quite a large list eventually:
require ('plugins.plugins')
require ('config.keymap')
require ('config.colorscheme')
require ('config.options')
require ('config.cmp')
One just needs to specify the following:
requirePath('plugins')
requirePath('config')
PS: Could be made recursive so that only requirePath('foo')
is needed.
I can see that my files are loading when Neovim starts:
and the above is based of the following folder structure:
But in comparison to just having the require
statements straight, no loading magic, the initialization appears to be somehwat different, as some plugins, like nvim-cmp, doesn't work completely with my require script.
Any ideas what I'm missing?
io.popen('find ./lua/' .. path .. ' -type f')
is wrong.