Skip to content

Commit

Permalink
lib/neovim-plugin: add lazyLoad
Browse files Browse the repository at this point in the history
Using mkLazyLoadOption generate lazyLoad option for all plugins
generated using mkNeovimPlugin.
Plugin maintainers can opt out by setting `allowLazyLoad = false`.
Plugin maintainers can provide default vaules by setting option in
lazyLoad.
  • Loading branch information
psfloyd committed Jul 16, 2024
1 parent b8f7ce4 commit c971cce
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ with lib;
extraPackages ? [ ],
callSetup ? true,
installPackage ? true,
# lazyLoad
allowLazyLoad ? true,
lazyLoad ? { },
}:
let
namespace = if isColorscheme then "colorschemes" else "plugins";
cfg = config.${namespace}.${name};
in
{
meta = {
Expand Down Expand Up @@ -99,21 +103,44 @@ with lib;
example = settingsExample;
};
}
// optionalAttrs allowLazyLoad {
lazyLoad = nixvimOptions.mkLazyLoadOption {
inherit originalName cfg;
optionsForPlugin = true;
lazyLoad = {
after = optionalString callSetup ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';
} // (optionalAttrs (isColorscheme && colorscheme != null) { inherit colorscheme; }) // lazyLoad;
};
}
// extraOptions;

config =
let
cfg = config.${namespace}.${name};
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
lazyLoaded = cfg.lazyLoad.enable or false;
in
mkIf cfg.enable (mkMerge [
{
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;

${extraConfigNamespace} = optionalString callSetup ''
${extraConfigNamespace} = optionalString (callSetup && !lazyLoaded) ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';

plugins.lz-n.plugins = mkIf lazyLoaded [ cfg.lazyLoad ];
assertions = [
{
assertion = config.plugins.lz-n.enable || !lazyLoaded;
# TODO: replace with global option after adding backend
message = ''
Nixvim (${namespace}.${name}): You have to enable `plugins.lz-n` for `${namespace}.${name}.lazyLoad` to work.
'';
}
];

}
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
Expand Down

0 comments on commit c971cce

Please sign in to comment.