refactor: plugin normalization

main
LeMarsu 2024-05-29 23:15:31 +02:00
parent 81d82e05a2
commit b087119333
3 changed files with 24 additions and 17 deletions

View File

@ -18,6 +18,9 @@
enabled = true;
init = null;
config = null;
dependencies = [];
lazy = false;
cmd = [];
};
remotePluginToNeovimPlugin = p:
@ -29,7 +32,7 @@
withPluginDefaults = dep: defaultPlugin // dep;
normalizePlugin = d: let
dep = types.dependency d;
p =
plugin =
if ! dep ? plugin
then {plugin = dep;}
else let
@ -38,8 +41,13 @@
if attrNames plugin == ["name" "src"]
then {plugin = remotePluginToNeovimPlugin plugin;}
else dep;
p = withPluginDefaults plugin;
in
withPluginDefaults p;
p
// rec {
hasCommands = p.cmd != [];
lazy = p.lazy || hasCommands;
};
normalizeOrImportPlugin = dep:
if isPath dep
@ -96,9 +104,6 @@
if plugin ? pname
then plugin.pname
else plugin.name;
hasDeps = plugin ? dependencies && plugin.dependencies != [];
isLazy = plugin ? lazy && plugin.lazy || hasCommands;
hasCommands = plugin ? cmd;
name = pluginName plugin.plugin;
in
memo
@ -107,21 +112,21 @@
{name = pluginName plugin.plugin;}
// (mkTypeFn "init")
// (mkTypeFn "config")
// (optionalAttrs hasDeps {
// (optionalAttrs (plugin.dependencies != []) {
dependencies = map pluginName plugin.dependencies;
}
// (optionalAttrs isLazy {
})
// (optionalAttrs plugin.lazy {
lazy = true;
}))
// (optionalAttrs hasCommands {
})
// (optionalAttrs plugin.hasCommands {
inherit (plugin) cmd;
});
};
pluginsLuaDef = plugins: lua.nix2lua (foldl' pluginLuaDef {} plugins);
in {
inherit normalizePlugin;
inherit normalizePlugins;
inherit mkSlothFlakePlugin;
inherit mkRuntimePlugin;
inherit textOrContent;
inherit pluginsLuaDef;
}

View File

@ -1,5 +1,4 @@
{lib, ...}: let
inherit (lib.strings) removeSuffix;
{...}: let
inherit (builtins) match isNull typeOf concatStringsSep attrNames concatMap;
commaJoin = concatStringsSep ", ";

View File

@ -18,13 +18,16 @@
deps = callPackage ./deps.nix {inherit dependenciesExtraArgs types;};
sloth-flake.plugin = deps.mkSlothFlakePlugin version plugins;
runtimePlugin.plugin = deps.mkRuntimePlugin runtime;
plugins = deps.normalizePlugins (dependencies ++ [runtimePlugin sloth-flake]);
normalizedPlugins = deps.normalizePlugins dependencies;
sloth-flake = deps.mkSlothFlakePlugin version normalizedPlugins;
runtimePlugin = deps.mkRuntimePlugin runtime;
plugins =
normalizedPlugins
++ (deps.normalizePlugins [runtimePlugin sloth-flake]);
extractPlugin = p: {
inherit (p) plugin;
optional = p ? lazy && p.lazy || p ? cmd;
optional = p.lazy;
};
extractPlugins = map extractPlugin;