Skip to content

Commit 3282838

Browse files
committed
lib/neovim-plugin: refactor mkLazyLoadOption
1 parent 9f4f00e commit 3282838

File tree

3 files changed

+295
-123
lines changed

3 files changed

+295
-123
lines changed

lib/neovim-plugin.nix

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
options.${namespace}.${name} =
9090
{
9191
enable = lib.mkEnableOption originalName;
92+
lazyLoad = lib.nixvim.mkLazyLoadOption originalName;
9293
package =
9394
if lib.isOption package then
9495
package
@@ -113,13 +114,6 @@
113114
'';
114115
internal = true;
115116
};
116-
117-
lazyLoad = lib.nixvim.mkLazyLoadOption {
118-
inherit originalName;
119-
lazyLoadDefaults = lib.optionalAttrs (isColorscheme && colorscheme != null) {
120-
inherit colorscheme;
121-
};
122-
};
123117
}
124118
// lib.optionalAttrs hasSettings {
125119
settings = lib.nixvim.mkSettingsOption {
@@ -168,33 +162,42 @@
168162
# Add the plugin setup code `require('foo').setup(...)` to the lua configuration
169163
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
170164

171-
# Write the lua configuration `luaConfig.content` to the config file
165+
# Write the lua configuration `luaConfig.content` to the config file when lazy loading is not enabled
172166
(lib.mkIf (!cfg.lazyLoad.enable) (setLuaConfig cfg.luaConfig.content))
173-
])
174-
++ (lib.optionals hasConfigAttrs [
175-
(lib.mkIf (cfg.lazyLoad.backend == "lz.n") {
176-
plugins.lz-n = {
177-
# infinite recursion?
178-
# enable = true;
167+
168+
# When lazy loading is enabled for this plugin, route its configuration to the enabled provider
169+
(lib.mkIf cfg.lazyLoad.enable {
170+
assertions = [
171+
{
172+
assertion = (isColorscheme && colorscheme != null) || cfg.lazyLoad.settings != { };
173+
message = "You have enabled lazy loading for ${originalName} but have not provided any configuration.";
174+
}
175+
];
176+
plugins.lz-n = lib.mkIf config.plugins.lz-n.enable {
179177
plugins = [
180-
{
181-
# TODO: handle this for every plugin properly
182-
__unkeyed-1 = originalName;
183-
# Use provided after, otherwise fallback to normal lua content
184-
after = if cfg.lazyLoad.after != null then cfg.lazyLoad.after else cfg.luaConfig.content;
185-
inherit (cfg.lazyLoad)
186-
enabled
187-
priority
188-
before
189-
beforeAll
190-
event
191-
cmd
192-
ft
193-
keys
194-
colorscheme
195-
extraSettings
196-
;
197-
}
178+
(
179+
{
180+
__unkeyed-1 = originalName;
181+
# Use provided after, otherwise fallback to normal lua content
182+
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;
195+
}
196+
// (lib.removeAttrs cfg.lazyLoad.settings [
197+
"after"
198+
"colorscheme"
199+
])
200+
)
198201
];
199202
};
200203
})

lib/options.nix

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ rec {
335335
};
336336

337337
mkLazyLoadOption =
338-
{
339-
originalName,
340-
lazyLoadDefaults ? { },
341-
}:
338+
originalName:
342339
lib.mkOption {
343340
description = ''
344341
Lazy-load settings for ${originalName}.
345342
'';
343+
default = {
344+
enable = false;
345+
};
346346
type =
347347
let
348348
triggerType =
@@ -353,100 +353,94 @@ rec {
353353
(listOf str)
354354
];
355355
in
356-
types.submodule {
357-
options = with defaultNullOpts; {
358-
359-
enable = lib.mkEnableOption ''
360-
lazy-loading for ${originalName}
361-
'';
362-
363-
backend =
364-
mkEnumFirstDefault
365-
[
366-
"lz.n"
367-
]
368-
''
369-
The lazy-loading backend to use.
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}
370364
'';
365+
};
371366

372-
# Spec loading:
373-
enabled = mkStrLuaFnOr types.bool (lazyLoadDefaults.enabledInSpec or null) ''
374-
When false, or if the function returns false, then ${originalName} will not be included in the spec.
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.
375378
376-
Equivalence: lz.n => enabled; lazy.nvim => enabled
377-
'';
379+
Equivalence: lz.n => enabled; lazy.nvim => enabled
380+
'';
378381

379-
priority = mkNullable types.number (lazyLoadDefaults.priority or null) ''
380-
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
382+
priority = mkNullable types.number null ''
383+
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
381384
382-
Equivalence: lz.n => priority; lazy.nvim => priority
383-
'';
385+
Equivalence: lz.n => priority; lazy.nvim => priority
386+
'';
384387

385-
# Spec setup
386-
# Actions
387-
beforeAll = mkLuaFn (lazyLoadDefaults.beforeAll or null) ''
388-
Always executed before any plugins are loaded.
388+
# Spec setup
389+
# Actions
390+
beforeAll = mkLuaFn null ''
391+
Always executed before any plugins are loaded.
389392
390-
Equivalence: lz.n => beforeAll; lazy.nvim => init
391-
'';
393+
Equivalence: lz.n => beforeAll; lazy.nvim => init
394+
'';
392395

393-
before = mkLuaFn (lazyLoadDefaults.before or null) ''
394-
Executed before ${originalName} is loaded.
396+
before = mkLuaFn null ''
397+
Executed before ${originalName} is loaded.
395398
396-
Equivalence: lz.n => before; lazy.nvim => None
397-
'';
399+
Equivalence: lz.n => before; lazy.nvim => None
400+
'';
398401

399-
after = mkLuaFn (lazyLoadDefaults.after or null) ''
400-
Executed after ${originalName} is loaded.
402+
after = mkLuaFn null ''
403+
Executed after ${originalName} is loaded.
401404
402-
Equivalence: lz.n => after; lazy.nvim => config
403-
'';
405+
Equivalence: lz.n => after; lazy.nvim => config
406+
'';
404407

405-
# Triggers
406-
event = mkNullable triggerType (lazyLoadDefaults.event or null) ''
407-
Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua`
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`
408411
409-
Equivalence: lz.n => event; lazy.nvim => event
410-
'';
412+
Equivalence: lz.n => event; lazy.nvim => event
413+
'';
411414

412-
cmd = mkNullable triggerType (lazyLoadDefaults.cmd or null) ''
413-
Lazy-load on command.
415+
cmd = mkNullable triggerType null ''
416+
Lazy-load on command.
414417
415-
Equivalence: lz.n => cmd; lazy.nvim => cmd
416-
'';
418+
Equivalence: lz.n => cmd; lazy.nvim => cmd
419+
'';
417420

418-
ft = mkNullable triggerType (lazyLoadDefaults.ft or null) ''
419-
Lazy-load on filetype.
420-
421-
Equivalence: lz.n => ft; lazy.nvim => ft
422-
'';
421+
ft = mkNullable triggerType null ''
422+
Lazy-load on filetype.
423423
424-
keys = mkListOf (types.attrsOf types.anything) (lazyLoadDefaults.keys or null) ''
425-
Lazy-load on key mapping.
424+
Equivalence: lz.n => ft; lazy.nvim => ft
425+
'';
426426

427-
Equivalence: lz.n => keys; lazy.nvim => keys
428-
'';
427+
keys = mkListOf (types.attrsOf types.anything) null ''
428+
Lazy-load on key mapping.
429429
430-
colorscheme = mkNullable triggerType (lazyLoadDefaults.colorscheme or null) ''
431-
Lazy-load on colorscheme.
430+
Equivalence: lz.n => keys; lazy.nvim => keys
431+
'';
432432

433-
Equivalence: lz.n => colorscheme; lazy.nvim => None
434-
'';
433+
colorscheme = mkNullable triggerType null ''
434+
Lazy-load on colorscheme.
435435
436-
extraSettings = mkSettingsOption {
437-
description = ''
438-
Extra settings to pass to the lazy loader backend.
439-
'';
440-
example = {
441-
dependencies = {
442-
__unkeyed-1 = "nvim-lua/plenary.nvim";
443-
lazy = true;
444-
};
436+
Equivalence: lz.n => colorscheme; lazy.nvim => None
437+
'';
438+
};
439+
};
445440
};
446441
};
447-
};
448-
};
449-
default = lazyLoadDefaults;
442+
}
443+
);
450444
};
451445
}
452446
// removed

0 commit comments

Comments
 (0)