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.92k stars 263 forks source link

disable autoload of not 'used' plugins #646

Closed fedoranvar closed 3 years ago

fedoranvar commented 3 years ago

Good day to you!

I have constraint in neovim config,that gives me abillity to choose between minimal and full configs. image

I have all plugins installed (cloned) in packer directory.

When i try to load minimal config, even if i do not use some plugins - they still autoloaded (VimL). image

Question: is it possible to not autoload not use'd plugins? (Maybe some argument in use {}?

Thank you!

wbthomason commented 3 years ago

Hi! This is due to how native packages work in Neovim - start packages are sourced by default, and unfortunately cannot (easily) be skipped. However, packer already has a feature that sounds perfect for your use case - use the cond keyword to specify that each of your extra plugins should only load if vim.g.is_extra is true. That would look something like:


use {
  'somebody/someplugin',
  cond = 'vim.g.is_extra'
}
fedoranvar commented 3 years ago

@wbthomason

Yes, that did what i need, but i've encountered another thing:

i've templated each module like:

/module_name/[init.lua, config.lua, keymaps.lua]

and in init.lua i require config.lua and keymaps.lua and in requiring i'm using ...

require("packer").use({
    "tpope/vim-dadbod",
    "kristijanhusak/vim-dadbod-ui",
    config = function()
        require(... .. ".config")
        require(... .. ".keymaps")
    end,
    cond = "vim.g.is_extra",
})

How i can pass variable from outer scope to config function?

Thank you!

wbthomason commented 3 years ago

Sorry, but I'm not sure I understand what you're after here - you want to replace the ... with some module name, but automatically?

fedoranvar commented 3 years ago

in short, yes


using ... allows me to template plugins configuration.

So if i'm adding new plugin to my neovim config i've to cp -r template folder and edit init.lua config.lua keymaps.lua

for example, we have structure:

vim-daddbod/init.lua
vim-dadbod/config.lua
vim-dadbod/keymaps.lua

if i'll require('vim-dadbod') then print(...) in init.lua will print 'vim-dadbod'

so ... returns path by which file was required.


returning to previous post,

require(... .. ".config")
require(... .. ".keymaps")

works great by itself, so at the end its

require("vim-dadbod.config")
require("vim-dadbod.keymaps")

, but when i put it into config function of use, it returns:

image

is it possible to get that functionality in config function of use?

Thank you!

UPD.

I've come up with solution:

require("packer").use({
    cond="vim.env.NEOVIM_EXTRA",
    config = ""
        .. "require(\"" .. ... .. ".config\")"
        .. "require(\"" .. ... .. ".keymaps\")"
})

but i have question: by reading docs i assumed that cond and config are dedicated to one plugin, so i can't relate one config and cond to multiple plugins at once? e.g.

require("packer").use({
    "nvim-telescope/telescope.nvim",
    "nvim-telescope/telescope-symbols.nvim",
    "nvim-telescope/telescope-fzf-writer.nvim",
    "nvim-telescope/telescope-hop.nvim",
    {
        "nvim-telescope/telescope-fzf-native.nvim",
        run = "make",
    },
    requires = {
        "nvim-lua/popup.nvim",
        "nvim-lua/plenary.nvim",
    },
    cond="vim.env.NEOVIM_EXTRA",
    config = ""
        .. "require(" .. ... .. "\".config\")"
        .. "require(" .. ... .. "\".keymaps\")"
})

So my only option is to apply config and cond to each plugin-entry?

wbthomason commented 3 years ago

Sorry, I'm still quite confused about how this works in terms of basic Lua. You're saying that evaluating ... (which I only know as the vararg expression) in a non-function context gets you the directory path name, somehow? Would you mind linking me to docs for this?

As for reusing cond and config values, I should be merging #647 soon which will allow this.

fedoranvar commented 3 years ago

not docs, but i used this link: https://stackoverflow.com/questions/9145432/load-lua-files-by-relative-path