Skip to content

Commit 0997b37

Browse files
committed
lib/neovim-plugin: freeform lazy settings
Instead of trying to manage upstream configuration options, just keep using our freeform options so we can do less finicky logic and workarounds.
1 parent d24dd31 commit 0997b37

File tree

3 files changed

+104
-119
lines changed

3 files changed

+104
-119
lines changed

lib/neovim-plugin.nix

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,14 @@
178178
(
179179
{
180180
__unkeyed-1 = originalName;
181-
# Use provided after, otherwise fallback to normal lua content
181+
# Use provided after, otherwise fallback to normal function wrapped lua content
182182
after =
183-
if cfg.lazyLoad.settings.after != null then
184-
cfg.lazyLoad.settings.after
185-
else
186-
# We need to wrap it in a function so it doesn't execute immediately
187-
"function()\n " + cfg.luaConfig.content + " \nend";
188-
colorscheme =
189-
if cfg.lazyLoad.settings.colorscheme != null then
190-
cfg.lazyLoad.settings.colorscheme
191-
else if (isColorscheme && colorscheme != null) then
192-
colorscheme
193-
else
194-
null;
183+
let
184+
after = cfg.lazyLoad.settings.after or null;
185+
default = "function()\n " + cfg.luaConfig.content + " \nend";
186+
in
187+
if (lib.isString after || lib.types.rawLua.check after) then after else default;
188+
colorscheme = lib.mkIf isColorscheme (cfg.lazyLoad.settings.colorscheme or colorscheme);
195189
}
196190
// lib.removeAttrs cfg.lazyLoad.settings [
197191
"after"

lib/options.nix

Lines changed: 37 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -339,108 +339,47 @@ rec {
339339
lib.mkOption {
340340
description = ''
341341
Lazy-load settings for ${originalName}.
342-
'';
343-
default = {
344-
enable = false;
345-
};
346-
type =
347-
let
348-
triggerType =
349-
with types;
350-
oneOf [
351-
rawLua
352-
str
353-
(listOf str)
354-
];
355-
in
356-
types.submodule (
357-
{ config, ... }:
358-
{
359-
options = with defaultNullOpts; {
360-
enable = lib.mkOption {
361-
default = lib.any (x: x != null) (builtins.attrValues config.settings);
362-
description = ''
363-
lazy-loading for ${originalName}
364-
'';
365-
};
366-
367-
settings = lib.mkOption {
368-
description = '''';
369-
default = { };
370-
type =
371-
with types;
372-
submodule {
373-
freeformType = attrsOf anything;
374-
options = {
375-
# Spec loading:
376-
enabled = mkStrLuaFnOr types.bool null ''
377-
When false, or if the function returns false, then ${originalName} will not be included in the spec.
378-
379-
Equivalence: lz.n => enabled; lazy.nvim => enabled
380-
'';
381-
382-
priority = mkNullable types.number null ''
383-
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
384-
385-
Equivalence: lz.n => priority; lazy.nvim => priority
386-
'';
387-
388-
# Spec setup
389-
# Actions
390-
beforeAll = mkLuaFn null ''
391-
Always executed before any plugins are loaded.
392-
393-
Equivalence: lz.n => beforeAll; lazy.nvim => init
394-
'';
395-
396-
before = mkLuaFn null ''
397-
Executed before ${originalName} is loaded.
398-
399-
Equivalence: lz.n => before; lazy.nvim => None
400-
'';
401-
402-
after = mkLuaFn null ''
403-
Executed after ${originalName} is loaded.
404342
405-
Equivalence: lz.n => after; lazy.nvim => config
406-
'';
407-
408-
# Triggers
409-
event = mkNullable triggerType null ''
410-
Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua`
411-
412-
Equivalence: lz.n => event; lazy.nvim => event
413-
'';
414-
415-
cmd = mkNullable triggerType null ''
416-
Lazy-load on command.
417-
418-
Equivalence: lz.n => cmd; lazy.nvim => cmd
419-
'';
420-
421-
ft = mkNullable triggerType null ''
422-
Lazy-load on filetype.
423-
424-
Equivalence: lz.n => ft; lazy.nvim => ft
425-
'';
426-
427-
keys = mkListOf (types.attrsOf types.anything) null ''
428-
Lazy-load on key mapping.
429-
430-
Equivalence: lz.n => keys; lazy.nvim => keys
431-
'';
432-
433-
colorscheme = mkNullable triggerType null ''
434-
Lazy-load on colorscheme.
343+
> [!WARNING]
344+
> This is an experimental option and may not work as expected with all plugins.
345+
> The API may change without notice.
346+
> Please report any issues you encounter.
347+
'';
348+
default = { };
349+
type = types.submodule (
350+
{ config, ... }:
351+
{
352+
options = {
353+
enable = lib.mkOption {
354+
default = lib.any (x: x != null) (builtins.attrValues config.settings);
355+
defaultText = lib.literalMD ''
356+
`true` when `settings` has a non-null attribute
357+
'';
358+
description = ''
359+
lazy-loading for ${originalName}
360+
'';
361+
};
435362

436-
Equivalence: lz.n => colorscheme; lazy.nvim => None
437-
'';
438-
};
439-
};
363+
settings = lib.nixvim.mkSettingsOption {
364+
description = ''
365+
Lazy provider configuration settings.
366+
367+
Check your lazy loading provider's documentation on settings to configure.
368+
'';
369+
example = {
370+
cmd = "Neotest";
371+
keys = [
372+
{
373+
__unkeyed-1 = "<leader>nt";
374+
__unkeyed-3 = "<CMD>Neotest summary<CR>";
375+
desc = "Summary toggle";
376+
}
377+
];
440378
};
441379
};
442-
}
443-
);
380+
};
381+
}
382+
);
444383
};
445384
}
446385
// removed

tests/test-sources/plugins/lazyloading/lz-n.nix

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
{
3333
assertion =
3434
let
35-
plugins = config.plugins.lz-n.plugins or [ ];
36-
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
35+
inherit (config.plugins.lz-n) plugins;
36+
plugin = if plugins == [ ] then null else builtins.head plugins;
3737
keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ];
3838
in
3939
(builtins.length keys) == 1;
@@ -42,8 +42,8 @@
4242
{
4343
assertion =
4444
let
45-
plugins = config.plugins.lz-n.plugins or [ ];
46-
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
45+
inherit (config.plugins.lz-n) plugins;
46+
plugin = if plugins == [ ] then null else builtins.head plugins;
4747
in
4848
plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.after.__raw;
4949
message = "`lz-n.plugins[0].after` should have contained `neotest` lua content.";
@@ -126,9 +126,10 @@
126126
assertion =
127127
let
128128
plugin = builtins.head config.plugins.lz-n.plugins;
129-
cmd = if builtins.isList plugin.cmd then plugin.cmd else [ ];
129+
cmd = plugin.cmd or null;
130+
cmd' = lib.optionals (builtins.isList cmd) cmd;
130131
in
131-
(builtins.length cmd) == 4;
132+
(builtins.length cmd') == 4;
132133
message =
133134
let
134135
plugin = builtins.head config.plugins.lz-n.plugins;
@@ -260,8 +261,8 @@
260261
{
261262
assertion =
262263
let
263-
plugins = config.plugins.lz-n.plugins or [ ];
264-
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
264+
inherit (config.plugins.lz-n) plugins;
265+
plugin = if plugins == [ ] then null else builtins.head plugins;
265266
keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ];
266267
in
267268
(builtins.length keys) == 1;
@@ -282,6 +283,9 @@
282283
enable = true;
283284
lazyLoad = {
284285
enable = true;
286+
settings = {
287+
cmd = [ "Telescope" ];
288+
};
285289
};
286290
};
287291
};
@@ -301,4 +305,52 @@
301305
}
302306
];
303307
};
308+
309+
use-provided-raw-after =
310+
{ config, ... }:
311+
{
312+
plugins = {
313+
lz-n = {
314+
enable = true;
315+
};
316+
web-devicons.enable = false;
317+
telescope = {
318+
enable = true;
319+
lazyLoad = {
320+
enable = true;
321+
settings = {
322+
after.__raw = ''
323+
function()
324+
-- test string
325+
${config.plugins.telescope.luaConfig.content}
326+
end
327+
'';
328+
cmd = [ "Telescope" ];
329+
};
330+
};
331+
};
332+
};
333+
334+
assertions =
335+
let
336+
plugin = getFirstLznPlugin config;
337+
in
338+
[
339+
{
340+
assertion = (builtins.length config.plugins.lz-n.plugins) == 1;
341+
message = "`lz-n.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
342+
}
343+
{
344+
assertion =
345+
plugin.after.__raw == ''
346+
function()
347+
-- test string
348+
${config.plugins.telescope.luaConfig.content}
349+
end
350+
'';
351+
message = "`lz-n.plugins[0].after` should have contained a function wrapped `telescope` lua content.";
352+
}
353+
];
354+
};
355+
304356
}

0 commit comments

Comments
 (0)