0

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:

enter image description here

and the above is based of the following folder structure:

enter image description here

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?

1
  • Possibly found the issue! The script is not including the scripts, when opening Neovim from a different path. It only works when I open it from ~/.config/nvim. So means that the part where I call io.popen('find ./lua/' .. path .. ' -type f') is wrong. Jan 28, 2022 at 15:08

1 Answer 1

0

The problem had to do with the following line:

local files = io.popen('find ./lua/' .. path .. ' -type f')

This tries to find the files in the local working dir, of wherever Neovim was started from.

The solution was to change that line to look at the Neovim home config path instead:

local files = io.popen('find "$HOME"/.config/nvim/lua/' .. path .. ' -type f')

and update the line with

local req_file = file:gmatch('(' .. path .. '%/.+)%.lua$'){0}:gsub('/', '.')

to the following

local req_file = file:gmatch('%/lua%/(.+).lua$'){0}:gsub('/', '.')

The final result then looks as follows:

function requirePath(path) 
  local files = io.popen('find "$HOME"/.config/nvim/lua/' .. path .. ' -type f')

  for file in files:lines() do
    local req_file = file:gmatch('%/lua%/(.+).lua$'){0}:gsub('/', '.')
    status_ok, _ = pcall(require, req_file)

    if not status_ok then
      vim.notify('Failed loading ' .. req_file, vim.log.levels.ERROR)
    end
  end
end

I should probably look into a better way of getting the proper config path from the shell env and perhaps get rid of that risky regex, it might not work in every situation.

1
  • You should probably try using stdpath (I think that's what it's called)
    – D. Ben Knoble
    Jan 28, 2022 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.