wbthomason / packer.nvim

A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config
MIT License
7.72k stars 262 forks source link

Source plugins from other files in startup() #1136

Closed markfaine closed 1 year ago

markfaine commented 1 year ago

I'm trying to keep each plugin (installation and configuration) together in separate files. I get overwhelmed easily and my packer.lua is getting unwieldy.

My thinking was just to keep each plugin in a separate file and load them in startup(), like this:

return require("packer").startup(function(use)
  require("plugins.nvim-webdev-icons")
  require(...)  - other plugin
  require(...)  - other plugin
  ...
end)

Then in plugins/nvim-webdev-icons.lua

use("nvim-tree/nvim-web-devicons")

Granted, in this example it's hardly necessary but in some cases there is extensive configuration and while I can put the configuration elsewhere I can't seem to put both the installation (use) and the setup() in another file.

Long-term, I'd like to include this list of plugins dynamically but for now, the first step is just figuring out how to get each use(...) in a separate file.

Every time I've tried this I get an error:

attempt to call global 'use' (a nil value) 

I have seen some mention of getting around this in the readme, but I'm a very new newbie with lua and neovim and I'm not sure it applies in this situation.

Thanks for packer, I'm enjoying learning how to use it.

EdenEast commented 1 year ago

I will link to my own configuration where I do have all my plugins located in separate files. All of my plugins are defined in a folder called modules. An example of these file module file is nav. Any plugin that contains more complex configuration has is own file that can be required in the config section.

I have created my own custom handles called conf as a shorthand for config = function() require() end.

I then go though all the files in that modules folder and collect all the plugin tables and pass that to packer. This full setup is in my pack file.

Hope this example help with creating your own setup.

markfaine commented 1 year ago

@EdenEast Thank you, that is very helpful. You have done a great job with your configuration and I will be going through it using it as an educational material ;)

Of course, I see the value in learning how to configure this. I'm learning it now, however, I don't know why a configuration such as yours couldn't be provided as defaults to neovim. I know everyone loves to customize but I am starting to wonder why neovim doesn't provide something like this out-of-the-box.

They could just use a giant lookup table and let the configuration look up the values from the table, that would be a relatively easy way to make it customizable and provide sane defaults. Those who want to customize still could do so but those who don't or can't could just set options in a config file and accept the defaults for the rest.

Sorry for the speculation, I just can't get over how much work goes into reinventing the wheel every time someone creates another neovim config.