Skip to content

Commit

Permalink
lib/neovim-plugin: add lazy provider
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Dec 10, 2024
1 parent b752606 commit 06a4828
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@
)
];
};
plugins.lazy = lib.mkIf config.plugins.lazy.enable {
plugins = [
(
{
name = originalName;
main = luaName;
pkg = cfg.package;
# Use provided config, otherwise fallback to normal lua content
config =
cfg.lazyLoad.settings.config or
# We need to wrap it in a function so it doesn't execute immediately
("function()\n " + cfg.luaConfig.content + " \nend");
}
// (lib.removeAttrs cfg.lazyLoad.settings [
"config"
])
)
];
};
})
])
)
Expand Down
139 changes: 139 additions & 0 deletions tests/test-sources/plugins/lazyloading/lazy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
lazy-load-neovim-plugin-configured =
{ config, lib, ... }:
{
plugins = {
lazy = {
enable = true;
};

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

assertions = [
{
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 =
let
plugins = config.plugins.lazy.plugins or [ ];
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ];
in
(builtins.length cmd) == 1;
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
}
{
assertion =
let
plugins = config.plugins.lazy.plugins or [ ];
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
in
plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.config.__raw;
message = "`lazy.plugins[0].after` should have contained `neotest` lua content.";
}
];
};

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 = [
{
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 =
let
plugins = config.plugins.lazy.plugins or [ ];
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ];
in
(builtins.length cmd) == 1;
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
}
];
};
wrap-functionless-luaConfig =
{ config, ... }:
{
plugins = {
lazy = {
enable = true;
};
web-devicons.enable = false;
telescope = {
enable = true;
lazyLoad = {
enable = true;
settings = {
cmd = [ "Telescope" ];
};
};
};
};

assertions = [
{
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 =
let
plugin = builtins.head config.plugins.lazy.plugins;
in
plugin.config.__raw == "function()\n " + config.plugins.telescope.luaConfig.content + " \nend";
message = "`lazy.plugins[0].config` should have contained a function wrapped `telescope` lua content.";
}
];
};
}

0 comments on commit 06a4828

Please sign in to comment.