Skip to content

Commit 528b8a6

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 6bd17ff commit 528b8a6

File tree

3 files changed

+53
-117
lines changed

3 files changed

+53
-117
lines changed

lib/neovim-plugin.nix

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,10 @@
180180
__unkeyed-1 = originalName;
181181
# Use provided after, otherwise fallback to normal 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+
cfg.lazyLoad.settings.after or
184+
# We need to wrap it in a function so it doesn't execute immediately
185+
("function()\n " + cfg.luaConfig.content + " \nend");
186+
colorscheme = lib.mkIf isColorscheme (cfg.lazyLoad.settings.colorscheme or colorscheme);
195187
}
196188
// (lib.removeAttrs cfg.lazyLoad.settings [
197189
"after"

lib/options.nix

Lines changed: 37 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -339,108 +339,48 @@ 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-
'';
407343
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.
344+
> [!WARNING]
345+
> This is an experimental option and may not work as expected with all plugins.
346+
> The API may change without notice.
347+
> Please report any issues you encounter.
348+
'';
349+
default = { };
350+
type = types.submodule (
351+
{ config, ... }:
352+
{
353+
options = {
354+
enable = lib.mkOption {
355+
default = lib.any (x: x != null) (builtins.attrValues config.settings);
356+
defaultText = lib.literalMD ''
357+
`true` when `settings` has a non-null attribute
358+
'';
359+
description = ''
360+
lazy-loading for ${originalName}
361+
'';
362+
};
435363

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

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

Lines changed: 12 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
};

0 commit comments

Comments
 (0)