Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/options: lib/neovim-plugin: Add lazyLoad option to plugins #1866

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ with lib;
extraPackages ? [ ],
callSetup ? true,
installPackage ? true,
allowLazyLoad ? true,
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
lazyLoad ? { },
}:
let
namespace = if isColorscheme then "colorschemes" else "plugins";
cfg = config.${namespace}.${name};
in
{
meta = {
Expand Down Expand Up @@ -99,21 +102,36 @@ with lib;
example = settingsExample;
};
}
// optionalAttrs allowLazyLoad {
lazyLoad = nixvimOptions.mkLazyLoadOption {
inherit originalName cfg;
optionsForPlugin = true;
lazyLoad = {
# TODO: extract extraConfigLua and extraConfigLuaPre from extraConfig
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
after = optionalString callSetup ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';
colorscheme = if (isColorscheme && (colorscheme != null)) then [ colorscheme ] else null;
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
} // lazyLoad;
};
}
// extraOptions;

config =
let
cfg = config.${namespace}.${name};
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
lazyLoaded = if (cfg ? lazyLoad) then cfg.lazyLoad.enable else false;
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
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 ];
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
}
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
Expand Down
8 changes: 7 additions & 1 deletion lib/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ rec {
originalName ? "this plugin",
cfg ? { },
lazyLoad ? { },
optionsForPlugin ? false,
}:
let
lazyLoadPluginDefault = {
Expand All @@ -390,10 +391,13 @@ rec {
with nixvimTypes;
submodule {
options = with defaultNullOpts; {
# enable option only has an effect when it is a sub-option for a plugin generated by mkNeovimPlugin
enable = mkOption {
type = bool;
default = getPluginDefault "enable";
description = "Enable lazy-loading for ${originalName}";
# Only show this option when it is a sub-option for a plugin
visible = optionsForPlugin;
};
name = mkOption {
type = str;
Expand All @@ -412,7 +416,9 @@ rec {
"Lazy-load on event. Events can be specified as BufEnter or with a pattern like BufEnter *.lua";
cmd = mkNullable (listOf str) (getPluginDefault "cmd") "Lazy-load on command.";
ft = mkNullable (listOf str) (getPluginDefault "ft") "Lazy-load on filetype.";
keys = mkNullable (listOf nixvimKeymaps.mapOptionSubmodule) (getPluginDefault "keys") "Lazy-load on key mapping. Use the same format as `config.keymaps`.";
keys =
mkNullable (listOf nixvimKeymaps.mapOptionSubmodule) (getPluginDefault "keys")
"Lazy-load on key mapping. Use the same format as `config.keymaps`.";
colorscheme = mkNullable (listOf str) (getPluginDefault "colorscheme") "Lazy-load on colorscheme.";
priority = mkNullable number (getPluginDefault "priority") ''
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
Expand Down