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/neovim-plugin: lazyLoad support for lazy.nvim #2620

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@
)
];
};
plugins.lazy = lib.mkIf config.plugins.lazy.enable {
plugins = [
(
{
name = originalName;
main = luaName;
pkg = cfg.package;
# Use provided opts, otherwise fallback to settings
opts = cfg.lazyLoad.settings.opts or cfg.settings;
}
// (lib.removeAttrs cfg.lazyLoad.settings [
"opts"
])
)
];
};
})
])
)
Expand Down
3 changes: 2 additions & 1 deletion modules/lazyload.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ in
) (builtins.attrNames config.plugins);
count = builtins.length pluginsWithLazyLoad;
in
lib.optionals (count > 0 && !config.plugins.lz-n.enable) [
lib.optionals (count > 0 && !config.plugins.lz-n.enable && !config.plugins.lazy.enable) [
''
You have enabled lazy loading support for the following plugins but have not enabled a lazy loading provider.
${lib.concatImapStringsSep "\n" (i: x: "${toString i}. plugins.${x}") pluginsWithLazyLoad}

Currently supported lazy providers:
- lz-n
- lazy
''
];
};
Expand Down
109 changes: 109 additions & 0 deletions tests/test-sources/plugins/lazyloading/lazy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
let
getFirstLazyPlugin =
config:
let
inherit (config.plugins.lazy) plugins;
in
if plugins == [ ] then null else builtins.head plugins;

getPluginKeys = plugin: if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ];
getPluginCmd = plugin: if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ];
in
{
lazy-load-neovim-plugin-configured =
{ config, lib, ... }:
{
plugins = {
lazy = {
enable = true;
};

neotest = {
enable = true;
lazyLoad = {
enable = true;
settings = {
cmd = [ "Neotest" ];
};
};
};
};

assertions =
let
plugin = getFirstLazyPlugin config;
cmd = getPluginCmd plugin;
in
[
{
assertion = (builtins.length config.plugins.lazy.plugins) == 1;
message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
{
assertion = (builtins.length cmd) == 1;
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
}
{
assertion = plugin != null && config.plugins.neotest.settings == plugin.opts;
message = "`lazy.plugins[0].opts` should have contained `neotest` settings.";
}
];
};

dont-lazy-load-unconfigured =
{ config, ... }:
{
plugins = {
neotest = {
enable = true;
# Empty attrset shouldn't trigger lazy loading
lazyLoad = { };
};
lazy = {
enable = true;
};
};

assertions = [
{
assertion = (builtins.length config.plugins.lazy.plugins) == 0;
message = "`lazy.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
];
};

lazy-load-enabled-automatically =
{ config, ... }:
{
plugins = {
lazy = {
enable = true;
};
neotest = {
enable = true;
lazyLoad = {
# Not setting lazyLoad.enable with configuration should enable
settings = {
cmd = [ "Neotest" ];
};
};
};
};

assertions =
let
plugin = getFirstLazyPlugin config;
cmd = getPluginCmd plugin;
in
[
{
assertion = (builtins.length config.plugins.lazy.plugins) == 1;
message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
{
assertion = (builtins.length cmd) == 1;
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
}
];
};
}