lazy: use attrsOf for lazy.plugins

This commit is contained in:
Pei Yang Ching 2024-10-17 16:53:36 +02:00
parent d76d673dd4
commit 854fd340e3
2 changed files with 26 additions and 22 deletions

View file

@ -4,6 +4,7 @@
... ...
}: let }: let
inherit (builtins) toJSON typeOf head length tryEval filter concatLists concatStringsSep; inherit (builtins) toJSON typeOf head length tryEval filter concatLists concatStringsSep;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
@ -21,14 +22,7 @@
else keySpec.action; else keySpec.action;
}; };
toLuaLznSpec = spec: let toLuaLznSpec = name: spec:
name =
if typeOf spec.package == "string"
then spec.package
else if (spec.package ? pname && (tryEval spec.package.pname).success)
then spec.package.pname
else spec.package.name;
in
(removeAttrs spec ["package" "setupModule" "setupOpts" "keys"]) (removeAttrs spec ["package" "setupModule" "setupOpts" "keys"])
// { // {
"@1" = name; "@1" = name;
@ -62,9 +56,9 @@
# empty list or str or (listOf str) # empty list or str or (listOf str)
else spec.keys; else spec.keys;
}; };
lznSpecs = map toLuaLznSpec cfg.plugins; lznSpecs = mapAttrsToList toLuaLznSpec cfg.plugins;
specToNotLazyConfig = spec: '' specToNotLazyConfig = _: spec: ''
do do
${optionalString (spec.before != null) spec.before} ${optionalString (spec.before != null) spec.before}
${optionalString (spec.setupModule != null) ${optionalString (spec.setupModule != null)
@ -73,20 +67,20 @@
end end
''; '';
specToKeymaps = spec: specToKeymaps = _: spec:
if typeOf spec.keys == "list" if typeOf spec.keys == "list"
then map (x: removeAttrs x ["ft"]) (filter (lznKey: lznKey.action != null && lznKey.ft == null) spec.keys) then map (x: removeAttrs x ["ft"]) (filter (lznKey: lznKey.action != null && lznKey.ft == null) spec.keys)
else if spec.keys == null || typeOf spec.keys == "string" else if spec.keys == null || typeOf spec.keys == "string"
then [] then []
else [spec.keys]; else [spec.keys];
notLazyConfig = concatStringsSep "\n" (map specToNotLazyConfig cfg.plugins); notLazyConfig = concatStringsSep "\n" (mapAttrsToList specToNotLazyConfig cfg.plugins);
in { in {
config.vim = mkMerge [ config.vim = mkMerge [
(mkIf cfg.enable { (mkIf cfg.enable {
startPlugins = ["lz-n" "lzn-auto-require"]; startPlugins = ["lz-n" "lzn-auto-require"];
optPlugins = map (plugin: plugin.package) cfg.plugins; optPlugins = mapAttrsToList (_: plugin: plugin.package) cfg.plugins;
luaConfigRC.lzn-load = entryBefore ["pluginConfigs"] '' luaConfigRC.lzn-load = entryBefore ["pluginConfigs"] ''
require('lz.n').load(${toLuaObject lznSpecs}) require('lz.n').load(${toLuaObject lznSpecs})
@ -94,12 +88,12 @@ in {
}) })
( (
mkIf (!cfg.enable) { mkIf (!cfg.enable) {
startPlugins = map (plugin: plugin.package) cfg.plugins; startPlugins = mapAttrsToList (_: plugin: plugin.package) cfg.plugins;
luaConfigPre = luaConfigPre =
concatStringsSep "\n" concatStringsSep "\n"
(filter (x: x != null) (map (spec: spec.beforeAll) cfg.plugins)); (filter (x: x != null) (mapAttrsToList (_: spec: spec.beforeAll) cfg.plugins));
luaConfigRC.unlazy = entryAfter ["pluginConfigs"] notLazyConfig; luaConfigRC.unlazy = entryAfter ["pluginConfigs"] notLazyConfig;
keymaps = concatLists (map specToKeymaps cfg.plugins); keymaps = concatLists (mapAttrsToList specToKeymaps cfg.plugins);
} }
) )
]; ];

View file

@ -188,19 +188,29 @@ in {
plugins = mkOption { plugins = mkOption {
default = []; default = [];
type = listOf lznPluginType; type = attrsOf lznPluginType;
description = "list of plugins to lazy load"; description = ''
Plugins to lazy load.
The attribute key is used as the plugin name: for the default `vim.g.lz_n.load`
function this should be either the `package.pname` or `package.name`.
'';
example = '' example = ''
[
{ {
toggleterm-nvim = {
package = "toggleterm-nvim"; package = "toggleterm-nvim";
setupModule = "toggleterm"; setupModule = "toggleterm";
setupOpts = cfg.setupOpts; setupOpts = cfg.setupOpts;
after = "require('toggleterm').do_something()"; after = "require('toggleterm').do_something()";
cmd = ["ToggleTerm"]; cmd = ["ToggleTerm"];
};
$${pkgs.vimPlugins.vim-bbye.pname} = {
package = pkgs.vimPlugins.vim-bbye;
cmd = ["Bdelete" "Bwipeout"];
};
} }
]
''; '';
}; };
}; };