Skip to content

Commit

Permalink
lib/neovim-plugin: refactor mkLazyLoadOption
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Dec 5, 2024
1 parent e83d388 commit df9ee3f
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 124 deletions.
77 changes: 42 additions & 35 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
options.${namespace}.${name} =
{
enable = lib.mkEnableOption originalName;
lazyLoad = lib.nixvim.mkLazyLoadOption originalName;
# TODO:
# lazyLoadDefaults = {
# # FIXME: infinite recursion
# # enable = lib.mkDefault (cfg.lazyLoad.backendSettings != { });
# };
# };
package =
if lib.isOption package then
package
Expand All @@ -113,13 +120,6 @@
'';
internal = true;
};

lazyLoad = lib.nixvim.mkLazyLoadOption {
inherit originalName;
lazyLoadDefaults = lib.optionalAttrs (isColorscheme && colorscheme != null) {
inherit colorscheme;
};
};
}
// lib.optionalAttrs hasSettings {
settings = lib.nixvim.mkSettingsOption {
Expand Down Expand Up @@ -159,43 +159,50 @@
}
))
]
++ (lib.optionals (!hasConfigAttrs) [
++ lib.optionals (!hasConfigAttrs) [
(lib.optionalAttrs callSetup (setLuaConfig setupCode))
])
++ (lib.optionals hasConfigAttrs [
]
++ lib.optionals hasConfigAttrs [
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
(lib.mkIf (!cfg.lazyLoad.enable) (
lib.optionalAttrs (configLocation != null) (setLuaConfig cfg.luaConfig.content)
))
])
++ (lib.optionals hasConfigAttrs [
(lib.mkIf (cfg.lazyLoad.backend == "lz.n") {
plugins.lz-n = {
# infinite recursion?
# enable = true;
(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 = lib.mkIf config.plugins.lz-n.enable {
plugins = [
{
# TODO: handle this for every plugin properly
__unkeyed-1 = originalName;
# Use provided after, otherwise fallback to normal lua content
after = if cfg.lazyLoad.after != null then cfg.lazyLoad.after else cfg.luaConfig.content;
inherit (cfg.lazyLoad)
enabled
priority
before
beforeAll
event
cmd
ft
keys
colorscheme
extraSettings
;
}
(
{
__unkeyed-1 = originalName;
# Use provided after, otherwise fallback to normal lua content
after =
if cfg.lazyLoad.settings.after != null then
cfg.lazyLoad.settings.after
else
# We need to wrap it in a function so it doesn't execute immediately
"function()\n " + cfg.luaConfig.content + " \nend";
colorscheme =
if cfg.lazyLoad.settings.colorscheme != null then
cfg.lazyLoad.settings.colorscheme
else if (isColorscheme && colorscheme != null) then
colorscheme
else
null;
}
// (lib.removeAttrs cfg.lazyLoad.settings [
"after"
"colorscheme"
])
)
];
};
})
])
]
)
);
};
Expand Down
144 changes: 69 additions & 75 deletions lib/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,14 @@ rec {
};

mkLazyLoadOption =
{
originalName,
lazyLoadDefaults ? { },
}:
originalName:
lib.mkOption {
description = ''
Lazy-load settings for ${originalName}.
'';
default = {
enable = lib.mkDefault false;
};
type =
let
triggerType =
Expand All @@ -353,100 +353,94 @@ rec {
(listOf str)
];
in
types.submodule {
options = with defaultNullOpts; {

enable = lib.mkEnableOption ''
lazy-loading for ${originalName}
'';

backend =
mkEnumFirstDefault
[
"lz.n"
]
''
The lazy-loading backend to use.
types.submodule (
{ config, ... }:
{
options = with defaultNullOpts; {
enable = lib.mkOption {
default = config.settings != { };
description = ''
lazy-loading for ${originalName}
'';
};

# Spec loading:
enabled = mkStrLuaFnOr types.bool (lazyLoadDefaults.enabledInSpec or null) ''
When false, or if the function returns false, then ${originalName} will not be included in the spec.
settings = lib.mkOption {
description = '''';
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) ''
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
priority = mkNullable types.number null ''
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
# Actions
beforeAll = mkLuaFn (lazyLoadDefaults.beforeAll or null) ''
Always executed before any plugins are loaded.
# Spec setup
# Actions
beforeAll = mkLuaFn null ''
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) ''
Executed before ${originalName} is loaded.
before = mkLuaFn null ''
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) ''
Executed after ${originalName} is loaded.
after = mkLuaFn null ''
Executed after ${originalName} is loaded.
Equivalence: lz.n => after; lazy.nvim => config
'';
Equivalence: lz.n => after; lazy.nvim => config
'';

# Triggers
event = mkNullable triggerType (lazyLoadDefaults.event or null) ''
Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua`
# Triggers
event = mkNullable triggerType null ''
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) ''
Lazy-load on command.
cmd = mkNullable triggerType null ''
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) ''
Lazy-load on filetype.
Equivalence: lz.n => ft; lazy.nvim => ft
'';
ft = mkNullable triggerType null ''
Lazy-load on filetype.
keys = mkListOf (types.attrsOf types.anything) (lazyLoadDefaults.keys or null) ''
Lazy-load on key mapping.
Equivalence: lz.n => ft; lazy.nvim => ft
'';

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

Equivalence: lz.n => colorscheme; lazy.nvim => None
'';
colorscheme = mkNullable triggerType null ''
Lazy-load on colorscheme.
extraSettings = mkSettingsOption {
description = ''
Extra settings to pass to the lazy loader backend.
'';
example = {
dependencies = {
__unkeyed-1 = "nvim-lua/plenary.nvim";
lazy = true;
};
Equivalence: lz.n => colorscheme; lazy.nvim => None
'';
};
};
};
};
};
};
default = lazyLoadDefaults;
}
);
};
}
// removed
Loading

0 comments on commit df9ee3f

Please sign in to comment.