lazy.nvim
lazy.nvim copied to clipboard
Simplify config syntax for common case
Is your feature request related to a problem? Please describe. I notice in the majority of the plugins I use, the configuration required is minimal: just execute the setup function:
{
"folke/tokyonight.nvim",
config = function() require("tokyonight").setup() end,
}
But that ends up being quite a lot of extra code just to call a single function with no parameters. I wonder if there's a way to do this in a shorter way.
Describe the solution you'd like
My first thought was that Lazy could simply call the setup()
method with no parameters if no config
function is given.
{
"folke/tokyonight.nvim",
-- implicitly has a default config = function() require("tokyonight").setup() end,
}
But I don't know if it's possible for Lazy to identify which module contains the setup method. So an alternative possibility might be:
{
"folke/tokyonight.nvim",
setup_module = "tokyonight",
}
Additionally, while the default case is to pass no parameter, it might also be nice to have this for more complex configurations too — any where only a table is passed to setup()
without any more complex functions needing to be called in the config
function. Perhaps this could be the behaviour when config
is a table instead of a function. So for example:
{
"folke/tokyonight.nvim",
setup_module = "tokyonight",
config = { style = "day" },
}
This might also not be desirable to enable by default so having a global config parameter (like for lazy-loading) might be desirable too?
Of course I realise this might be impractical or introduce too much complexity; just an idea.
two things
you could just do config=require"tokyonight".setup
like this it will no longer be lazy loaded though
the second option is to do something yourself
something like this
local function setup(module,config)
return function()
require(module).setup(config)
end
end
...
config=setup("tokyonight")
config=setup("tokyonight",{style="day"})
I think adding something for this inside this plugin doesn't make sense since it makes assumptions about which functions to plugins expose and it can be done as easy as the above code anyway
Just to add on this, previously in packer accept string like following if you don't want to write function for config.
I hope lazy can at least support this convention. For now, lazy just show error loading string for config. It looks cleaner for simple config, at least me.
{ 'kyazdani42/nvim-tree.lua', config = [[require('config.nvimtree')]], cmd = 'NvimTreeToggle' },
{ 'mhinz/vim-startify', config = [[require('config.homepage').stratify()]] },
wouldn't it be possible to just create a function that returns a function (like the example I gave above) that evaluates the string? that would only add a short function name to the string
Then, I also have something hideous like this which I don't know how to convert.
{ 'mbbill/undotree', config = [[
vim.g.undotree_WindowLayout = 2
vim.g.undotree_HelpLine = 0
vim.g.undotree_ShortIndicators = 1
vim.g.undotree_TreeNodeShape = '⧂'
vim.g.undotree_TreeVertShape = '│'
vim.g.undotree_TreeSplitShape = '╱'
vim.g.undotree_TreeReturnShape = '╲'
vim.g.undotree_SetFocusWhenToggle = 1
]], cmd = 'UndotreeToggle' },
wouldn't it be possible to just create a function that returns a function (like the example I gave above) that evaluates the string? that would only add a short function name to the string
config=function vim.g..... end
you could just do config=require"tokyonight".setup
That's not really the same thing, and as you say, it would also prevent lazy-loading.
the second option is to do something yourself something like this
Yes, I do realise I could implement something for my own configuration to simplify this. I just wondered if it would be a useful feature in general.
I think adding something for this inside this plugin doesn't make sense since it makes assumptions about which functions to plugins expose
It appears that it's an increasingly common convention for plugins to expose a setup
function, which is the reason I wondered if this was possible. Of course, if every plugin named their function something different, it would be impossible.
But if it's possible to detect which module to load, and if it's possible to detect whether that module contains a setup
function, this might simplify some repetitive code.
perhaps it would be possible to detect that but it would also decrease performance (perhaps only slightly)
and I don't see how it would be repetitive to just add that function at the top and then a call at all the plugins you want this for
I see no reason why this should be done by lazy.nvim
in the end it's folkes choice
Not live yet, but you'll be able to do this :)
just a guess
config=true
calls setup
in the main module and confg={...}
calls setup({...})
?
if so
does lazy.nvim check if those functions exist or should this be done by the user?
if it does check
isn't this bad for performance?
The user should only use this format if the module obviously has a setup()
method.
https://github.com/folke/lazy.nvim/commit/2a7b0047dd25f543b147b692fe100e1b2d88ffb2
fyi: for tokyonight
in your example you dont actually need to run setup if you want to use the default settings.