lib/neovim-plugin: Add lua configuration scoped to the plugin

This commit adds a `plugins.<name>.luaConfig` section controlling the
plugin specific configuration.

The section contains the internal `init` option, containing the plugin's
initialization code.

It also contains the public `pre` and `post` options, that allow to add
code before & after the `init` section

Finally, it contains the `final` option, being the concatenation of the
three previous options.
This commit is contained in:
Quentin Boyer 2024-08-19 23:31:57 +02:00 committed by nix-infra-bot
parent 2bc6a94992
commit d2f9e011d9
21 changed files with 129 additions and 35 deletions

View file

@ -25,6 +25,11 @@
# colorscheme
isColorscheme ? false,
colorscheme ? name,
# luaConfig
configLocation ? if isColorscheme then "extraConfigLuaPre" else "extraConfigLua",
# For some plugins it may not make sense to have a configuration attribute, as they are
# configured through some other mean, like global variables
hasConfigAttrs ? true,
# options
originalName ? name,
# Can be a string, a list of strings, or a module option:
@ -60,7 +65,14 @@
let
cfg = config.${namespace}.${name};
opt = options.${namespace}.${name};
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
setupCode = ''
require('${luaName}')${setup}(${
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
})
'';
setLuaConfig = lib.setAttrByPath (lib.toList configLocation);
in
{
meta = {
@ -100,27 +112,40 @@
example = settingsExample;
};
}
// lib.optionalAttrs hasConfigAttrs {
luaConfig = lib.mkOption {
type = lib.nixvim.nixvimTypes.pluginLuaConfig;
default = { };
description = "The plugin's lua configuration";
};
}
// extraOptions;
config = lib.mkIf cfg.enable (
lib.mkMerge [
{
extraPlugins = (lib.optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;
}
(lib.optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
config =
assert lib.assertMsg (
callSetup -> configLocation != null
) "When a plugin has no config attrs and has a setup function it must have a config location";
lib.mkIf cfg.enable (
lib.mkMerge (
[
{
extraPlugins = (lib.optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;
}
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme;
})
'';
})
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme;
})
(extraConfig cfg)
]
);
(extraConfig cfg)
]
++ (lib.optionals (!hasConfigAttrs) [
(lib.optionalAttrs callSetup (setLuaConfig setupCode))
])
++ (lib.optionals hasConfigAttrs [
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
(lib.optionalAttrs (configLocation != null) (setLuaConfig cfg.luaConfig.content))
])
)
);
};
in
{