lib/neovim-plugin: refactor mkLazyLoadOption

This commit is contained in:
Austin Horstman 2024-12-02 13:50:48 -06:00
parent 3cfde1554c
commit c3f9cb721c
No known key found for this signature in database
3 changed files with 294 additions and 122 deletions

View file

@ -89,6 +89,7 @@
options.${namespace}.${name} = options.${namespace}.${name} =
{ {
enable = lib.mkEnableOption originalName; enable = lib.mkEnableOption originalName;
lazyLoad = lib.nixvim.mkLazyLoadOption originalName;
package = package =
if lib.isOption package then if lib.isOption package then
package package
@ -113,13 +114,6 @@
''; '';
internal = true; internal = true;
}; };
lazyLoad = lib.nixvim.mkLazyLoadOption {
inherit originalName;
lazyLoadDefaults = lib.optionalAttrs (isColorscheme && colorscheme != null) {
inherit colorscheme;
};
};
} }
// lib.optionalAttrs hasSettings { // lib.optionalAttrs hasSettings {
settings = lib.nixvim.mkSettingsOption { settings = lib.nixvim.mkSettingsOption {
@ -168,33 +162,42 @@
# Add the plugin setup code `require('foo').setup(...)` to the lua configuration # Add the plugin setup code `require('foo').setup(...)` to the lua configuration
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; }) (lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
# Write the lua configuration `luaConfig.content` to the config file # Write the lua configuration `luaConfig.content` to the config file when lazy loading is not enabled
(lib.mkIf (!cfg.lazyLoad.enable) (setLuaConfig cfg.luaConfig.content)) (lib.mkIf (!cfg.lazyLoad.enable) (setLuaConfig cfg.luaConfig.content))
])
++ (lib.optionals hasConfigAttrs [ # When lazy loading is enabled for this plugin, route its configuration to the enabled provider
(lib.mkIf (cfg.lazyLoad.backend == "lz.n") { (lib.mkIf cfg.lazyLoad.enable {
assertions = [
{
assertion = (isColorscheme && colorscheme != null) || cfg.lazyLoad.settings != { };
message = "You have enabled lazy loading for ${originalName} but have not provided any configuration.";
}
];
plugins.lz-n = { plugins.lz-n = {
# infinite recursion?
# enable = true;
plugins = [ plugins = [
{ (
# TODO: handle this for every plugin properly {
__unkeyed-1 = originalName; __unkeyed-1 = originalName;
# Use provided after, otherwise fallback to normal lua content # Use provided after, otherwise fallback to normal lua content
after = if cfg.lazyLoad.after != null then cfg.lazyLoad.after else cfg.luaConfig.content; after =
inherit (cfg.lazyLoad) if cfg.lazyLoad.settings.after != null then
enabled cfg.lazyLoad.settings.after
priority else
before # We need to wrap it in a function so it doesn't execute immediately
beforeAll "function()\n " + cfg.luaConfig.content + " \nend";
event colorscheme =
cmd if cfg.lazyLoad.settings.colorscheme != null then
ft cfg.lazyLoad.settings.colorscheme
keys else if (isColorscheme && colorscheme != null) then
colorscheme colorscheme
extraSettings else
; null;
} }
// lib.removeAttrs cfg.lazyLoad.settings [
"after"
"colorscheme"
]
)
]; ];
}; };
}) })

View file

@ -335,14 +335,14 @@ rec {
}; };
mkLazyLoadOption = mkLazyLoadOption =
{ originalName:
originalName,
lazyLoadDefaults ? { },
}:
lib.mkOption { lib.mkOption {
description = '' description = ''
Lazy-load settings for ${originalName}. Lazy-load settings for ${originalName}.
''; '';
default = {
enable = false;
};
type = type =
let let
triggerType = triggerType =
@ -353,100 +353,94 @@ rec {
(listOf str) (listOf str)
]; ];
in in
types.submodule { types.submodule (
options = with defaultNullOpts; { { config, ... }:
{
enable = lib.mkEnableOption '' options = with defaultNullOpts; {
lazy-loading for ${originalName} enable = lib.mkOption {
''; default = lib.any (x: x != null) (builtins.attrValues config.settings);
description = ''
backend = lazy-loading for ${originalName}
mkEnumFirstDefault
[
"lz.n"
]
''
The lazy-loading backend to use.
''; '';
};
# Spec loading: settings = lib.mkOption {
enabled = mkStrLuaFnOr types.bool (lazyLoadDefaults.enabledInSpec or null) '' description = '''';
When false, or if the function returns false, then ${originalName} will not be included in the spec. default = { };
type =
with types;
submodule {
freeformType = attrsOf anything;
options = {
# Spec loading:
enabled = mkStrLuaFnOr types.bool null ''
When false, or if the function returns false, then ${originalName} will not be included in the spec.
Equivalence: lz.n => enabled; lazy.nvim => enabled Equivalence: lz.n => enabled; lazy.nvim => enabled
''; '';
priority = mkNullable types.number (lazyLoadDefaults.priority or null) '' priority = mkNullable types.number null ''
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first. Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
Equivalence: lz.n => priority; lazy.nvim => priority Equivalence: lz.n => priority; lazy.nvim => priority
''; '';
# Spec setup # Spec setup
# Actions # Actions
beforeAll = mkLuaFn (lazyLoadDefaults.beforeAll or null) '' beforeAll = mkLuaFn null ''
Always executed before any plugins are loaded. Always executed before any plugins are loaded.
Equivalence: lz.n => beforeAll; lazy.nvim => init Equivalence: lz.n => beforeAll; lazy.nvim => init
''; '';
before = mkLuaFn (lazyLoadDefaults.before or null) '' before = mkLuaFn null ''
Executed before ${originalName} is loaded. Executed before ${originalName} is loaded.
Equivalence: lz.n => before; lazy.nvim => None Equivalence: lz.n => before; lazy.nvim => None
''; '';
after = mkLuaFn (lazyLoadDefaults.after or null) '' after = mkLuaFn null ''
Executed after ${originalName} is loaded. Executed after ${originalName} is loaded.
Equivalence: lz.n => after; lazy.nvim => config Equivalence: lz.n => after; lazy.nvim => config
''; '';
# Triggers # Triggers
event = mkNullable triggerType (lazyLoadDefaults.event or null) '' event = mkNullable triggerType null ''
Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua` Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua`
Equivalence: lz.n => event; lazy.nvim => event Equivalence: lz.n => event; lazy.nvim => event
''; '';
cmd = mkNullable triggerType (lazyLoadDefaults.cmd or null) '' cmd = mkNullable triggerType null ''
Lazy-load on command. Lazy-load on command.
Equivalence: lz.n => cmd; lazy.nvim => cmd Equivalence: lz.n => cmd; lazy.nvim => cmd
''; '';
ft = mkNullable triggerType (lazyLoadDefaults.ft or null) '' ft = mkNullable triggerType null ''
Lazy-load on filetype. Lazy-load on filetype.
Equivalence: lz.n => ft; lazy.nvim => ft Equivalence: lz.n => ft; lazy.nvim => ft
''; '';
keys = mkListOf (types.attrsOf types.anything) (lazyLoadDefaults.keys or null) '' keys = mkListOf (types.attrsOf types.anything) null ''
Lazy-load on key mapping. Lazy-load on key mapping.
Equivalence: lz.n => keys; lazy.nvim => keys Equivalence: lz.n => keys; lazy.nvim => keys
''; '';
colorscheme = mkNullable triggerType (lazyLoadDefaults.colorscheme or null) '' colorscheme = mkNullable triggerType null ''
Lazy-load on colorscheme. Lazy-load on colorscheme.
Equivalence: lz.n => colorscheme; lazy.nvim => None Equivalence: lz.n => colorscheme; lazy.nvim => None
''; '';
};
extraSettings = mkSettingsOption { };
description = ''
Extra settings to pass to the lazy loader backend.
'';
example = {
dependencies = {
__unkeyed-1 = "nvim-lua/plenary.nvim";
lazy = true;
};
}; };
}; };
}; }
}; );
default = lazyLoadDefaults;
}; };
} }
// removed // removed

View file

@ -1,6 +1,6 @@
{ {
lazy-load-neovim-plugin = lazy-load-neovim-plugin-configured =
{ config, ... }: { config, lib, ... }:
{ {
plugins = { plugins = {
lz-n = { lz-n = {
@ -11,13 +11,15 @@
enable = true; enable = true;
lazyLoad = { lazyLoad = {
enable = true; enable = true;
keys = [ settings = {
{ keys = [
__unkeyed-1 = "<leader>nt"; {
__unkeyed-3 = "<CMD>Neotest summary<CR>"; __unkeyed-1 = "<leader>nt";
desc = "Summary toggle"; __unkeyed-3 = "<CMD>Neotest summary<CR>";
} desc = "Summary toggle";
]; }
];
};
}; };
}; };
}; };
@ -30,22 +32,31 @@
{ {
assertion = assertion =
let let
plugin = builtins.head config.plugins.lz-n.plugins; plugins = config.plugins.lz-n.plugins or [ ];
keys = if builtins.isList plugin.keys then plugin.keys else [ ]; plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ];
in in
(builtins.length keys) == 1; (builtins.length keys) == 1;
message = "`lz-n.plugins[0].keys` should have contained a configuration."; message = "`lz-n.plugins[0].keys` should have contained a configuration.";
} }
{
assertion =
let
plugins = config.plugins.lz-n.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.after.__raw;
message = "`lz-n.plugins[0].after` should have contained `neotest` lua content.";
}
]; ];
}; };
lazy-load-lz-n = lazy-load-lz-n-configured =
{ config, ... }: { config, lib, ... }:
{ {
plugins = { plugins = {
codesnap = { codesnap = {
enable = true; enable = true;
lazyLoad.enable = true;
}; };
lz-n = { lz-n = {
enable = true; enable = true;
@ -109,7 +120,7 @@
let let
plugin = builtins.head config.plugins.lz-n.plugins; plugin = builtins.head config.plugins.lz-n.plugins;
in in
"`lz-n.plugins[0].keys` should have contained 4 key configurations, but contained ${builtins.toJSON (plugin.keys)}"; "`lz-n.plugins[0].keys` should have contained 4 key configurations, but contained ${builtins.toJSON plugin.keys}";
} }
{ {
assertion = assertion =
@ -122,7 +133,171 @@
let let
plugin = builtins.head config.plugins.lz-n.plugins; plugin = builtins.head config.plugins.lz-n.plugins;
in in
"`lz-n.plugins[0].cmd` should have contained 4 cmd configurations, but contained ${builtins.toJSON (plugin.cmd)}"; "`lz-n.plugins[0].cmd` should have contained 4 cmd configurations, but contained ${builtins.toJSON plugin.cmd}";
}
{
assertion =
let
plugin = builtins.head config.plugins.lz-n.plugins;
in
lib.hasInfix config.plugins.codesnap.luaConfig.content plugin.after.__raw;
message = "`lz-n.plugins[0].after` should have contained `codesnap` lua content.";
}
];
};
dont-lazy-load-colorscheme-automatically =
{ config, ... }:
{
colorschemes.catppuccin.enable = true;
plugins = {
lz-n = {
enable = true;
};
};
assertions = [
{
assertion = (builtins.length config.plugins.lz-n.plugins) == 0;
message = "`lz-n.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
];
};
dont-lazy-load-unconfigured =
{ config, ... }:
{
plugins = {
neotest = {
enable = true;
# Empty attrset shouldn't trigger lazy loading
lazyLoad = { };
};
lz-n = {
enable = true;
};
};
assertions = [
{
assertion = (builtins.length config.plugins.lz-n.plugins) == 0;
message = "`lz-n.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
];
};
lazy-load-colorscheme-properly =
{ config, lib, ... }:
{
colorschemes.catppuccin = {
enable = true;
lazyLoad.enable = true;
};
plugins = {
lz-n = {
enable = true;
};
};
assertions = [
{
assertion = (builtins.length config.plugins.lz-n.plugins) == 1;
message = "`lz-n.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
{
assertion =
let
plugin = builtins.head config.plugins.lz-n.plugins;
in
plugin.colorscheme == "catppuccin";
message =
let
plugin = builtins.head config.plugins.lz-n.plugins;
in
"`lz-n.plugins[0].colorscheme` should have been `catppuccin`, but contained ${builtins.toJSON plugin.colorscheme}";
}
{
assertion =
let
plugin = builtins.head config.plugins.lz-n.plugins;
in
lib.hasInfix config.colorschemes.catppuccin.luaConfig.content plugin.after.__raw;
message = "`lz-n.plugins[0].after` should have contained `catppuccin` lua content.";
}
];
};
lazy-load-enabled-automatically =
{ config, ... }:
{
plugins = {
lz-n = {
enable = true;
};
neotest = {
enable = true;
lazyLoad = {
# Not setting lazyLoad.enable with configuration should enable
settings = {
keys = [
{
__unkeyed-1 = "<leader>nt";
__unkeyed-3 = "<CMD>Neotest summary<CR>";
desc = "Summary toggle";
}
];
};
};
};
};
assertions = [
{
assertion = (builtins.length config.plugins.lz-n.plugins) == 1;
message = "`lz-n.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
{
assertion =
let
plugins = config.plugins.lz-n.plugins or [ ];
plugin = if builtins.length plugins > 0 then builtins.head plugins else null;
keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ];
in
(builtins.length keys) == 1;
message = "`lz-n.plugins[0].keys` should have contained a configuration.";
}
];
};
wrap-functionless-luaConfig =
{ config, ... }:
{
plugins = {
lz-n = {
enable = true;
};
web-devicons.enable = false;
telescope = {
enable = true;
lazyLoad = {
enable = true;
};
};
};
assertions = [
{
assertion = (builtins.length config.plugins.lz-n.plugins) == 1;
message = "`lz-n.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}";
}
{
assertion =
let
plugin = builtins.head config.plugins.lz-n.plugins;
in
plugin.after.__raw == "function()\n " + config.plugins.telescope.luaConfig.content + " \nend";
message = "`lz-n.plugins[0].after` should have contained a function wrapped `telescope` lua content.";
} }
]; ];
}; };