Skip to content

Commit 1b0d3e7

Browse files
committed
lib/neovim-plugin: add lazy provider
1 parent b752606 commit 1b0d3e7

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

lib/neovim-plugin.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@
194194
)
195195
];
196196
};
197+
plugins.lazy = lib.mkIf config.plugins.lazy.enable {
198+
plugins = [
199+
(
200+
{
201+
name = originalName;
202+
main = luaName;
203+
pkg = cfg.package;
204+
# Use provided config, otherwise fallback to normal lua content
205+
config =
206+
cfg.lazyLoad.settings.config or
207+
# We need to wrap it in a function so it doesn't execute immediately
208+
("function()\n " + cfg.luaConfig.content + " \nend");
209+
}
210+
// (lib.removeAttrs cfg.lazyLoad.settings [
211+
"config"
212+
])
213+
)
214+
];
215+
};
197216
})
198217
])
199218
)

modules/lazyload.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ in
4545
) (builtins.attrNames config.plugins);
4646
count = builtins.length pluginsWithLazyLoad;
4747
in
48-
lib.optionals (count > 0 && !config.plugins.lz-n.enable) [
48+
lib.optionals (count > 0 && !config.plugins.lz-n.enable && !config.plugins.lazy.enable) [
4949
''
5050
You have enabled lazy loading support for the following plugins but have not enabled a lazy loading provider.
5151
${lib.concatImapStringsSep "\n" (i: x: "${toString i}. plugins.${x}") pluginsWithLazyLoad}
5252
5353
Currently supported lazy providers:
5454
- lz-n
55+
- lazy
5556
''
5657
];
5758
};
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
lazy-load-neovim-plugin-configured =
3+
{ config, lib, ... }:
4+
{
5+
plugins = {
6+
lazy = {
7+
enable = true;
8+
};
9+
10+
neotest = {
11+
enable = true;
12+
lazyLoad = {
13+
enable = true;
14+
settings = {
15+
cmd = [ "Neotest" ];
16+
};
17+
};
18+
};
19+
};
20+
21+
assertions = [
22+
{
23+
assertion = (builtins.length config.plugins.lazy.plugins) == 1;
24+
message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
25+
}
26+
{
27+
assertion =
28+
let
29+
plugins = config.plugins.lazy.plugins or [ ];
30+
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
31+
cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ];
32+
in
33+
(builtins.length cmd) == 1;
34+
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
35+
}
36+
{
37+
assertion =
38+
let
39+
plugins = config.plugins.lazy.plugins or [ ];
40+
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
41+
in
42+
plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.config.__raw;
43+
message = "`lazy.plugins[0].after` should have contained `neotest` lua content.";
44+
}
45+
];
46+
};
47+
48+
dont-lazy-load-unconfigured =
49+
{ config, ... }:
50+
{
51+
plugins = {
52+
neotest = {
53+
enable = true;
54+
# Empty attrset shouldn't trigger lazy loading
55+
lazyLoad = { };
56+
};
57+
lazy = {
58+
enable = true;
59+
};
60+
};
61+
62+
assertions = [
63+
{
64+
assertion = (builtins.length config.plugins.lazy.plugins) == 0;
65+
message = "`lazy.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
66+
}
67+
];
68+
};
69+
70+
lazy-load-enabled-automatically =
71+
{ config, ... }:
72+
{
73+
plugins = {
74+
lazy = {
75+
enable = true;
76+
};
77+
neotest = {
78+
enable = true;
79+
lazyLoad = {
80+
# Not setting lazyLoad.enable with configuration should enable
81+
settings = {
82+
cmd = [ "Neotest" ];
83+
};
84+
};
85+
};
86+
};
87+
88+
assertions = [
89+
{
90+
assertion = (builtins.length config.plugins.lazy.plugins) == 1;
91+
message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
92+
}
93+
{
94+
assertion =
95+
let
96+
plugins = config.plugins.lazy.plugins or [ ];
97+
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
98+
cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ];
99+
in
100+
(builtins.length cmd) == 1;
101+
message = "`lazy.plugins[0].cmd` should have contained a configuration.";
102+
}
103+
];
104+
};
105+
wrap-functionless-luaConfig =
106+
{ config, ... }:
107+
{
108+
plugins = {
109+
lazy = {
110+
enable = true;
111+
};
112+
web-devicons.enable = false;
113+
telescope = {
114+
enable = true;
115+
lazyLoad = {
116+
enable = true;
117+
settings = {
118+
cmd = [ "Telescope" ];
119+
};
120+
};
121+
};
122+
};
123+
124+
assertions = [
125+
{
126+
assertion = (builtins.length config.plugins.lazy.plugins) == 1;
127+
message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
128+
}
129+
{
130+
assertion =
131+
let
132+
plugin = builtins.head config.plugins.lazy.plugins;
133+
in
134+
plugin.config.__raw == "function()\n " + config.plugins.telescope.luaConfig.content + " \nend";
135+
message = "`lazy.plugins[0].config` should have contained a function wrapped `telescope` lua content.";
136+
}
137+
];
138+
};
139+
}

0 commit comments

Comments
 (0)