modules/lua-loader: make enable option nullable

This avoids having the option always "defined".

This also avoids luaLoader configuration in extra files by default.
Earlier `vim.loader.disable()` was always added to configs produced
by `files` option, effectively disabling luaLoader even if it was
explicitly enabled in a top-level configuration.
This commit is contained in:
Stanislav Asunkin 2024-07-21 09:59:04 +03:00
parent 7908729711
commit 8eb5763bbb
2 changed files with 76 additions and 26 deletions

View file

@ -1,30 +1,30 @@
{ lib, config, ... }: {
with lib; lib,
config,
helpers,
...
}:
let let
cfg = config.luaLoader; cfg = config.luaLoader;
in in
{ {
options.luaLoader = { options.luaLoader.enable = helpers.mkNullOrOption lib.types.bool ''
enable = mkOption { Whether to enable/disable the experimental lua loader:
type = types.bool;
default = false;
description = ''
Whether to enable/disable the experimental lua loader:
If `true`: Enables the experimental Lua module loader: If `true`: Enables the experimental Lua module loader:
- overrides loadfile - overrides loadfile
- adds the lua loader using the byte-compilation cache - adds the lua loader using the byte-compilation cache
- adds the libs loader - adds the libs loader
- removes the default Neovim loader - removes the default Neovim loader
If `false`: Disables the experimental Lua module loader: If `false`: Disables the experimental Lua module loader:
- removes the loaders - removes the loaders
- adds the default Neovim loader - adds the default Neovim loader
'';
};
};
config = { If `null`: Nothing is configured.
'';
config = helpers.mkIfNonNull' cfg.enable {
extraConfigLuaPre = if cfg.enable then "vim.loader.enable()" else "vim.loader.disable()"; extraConfigLuaPre = if cfg.enable then "vim.loader.enable()" else "vim.loader.disable()";
}; };
} }

View file

@ -1,9 +1,59 @@
{ {
enabled = { # Test that nothing is configured by default
luaLoader.enable = true; default.module =
}; { config, ... }:
{
files."files_test.lua" = { };
disabled = { assertions = [
luaLoader.enable = false; {
}; assertion = builtins.match ".*vim\.loader.*" config.content == null;
message = "No luaLoader configuration is expected in init.lua by default.";
}
{
assertion = builtins.match ".*vim\.loader.*" config.files."files_test.lua".content == null;
message = "No luaLoader configuration is expected in 'files' submodules.";
}
];
};
# Test lua loader enabled
enabled.module =
{ config, ... }:
{
luaLoader.enable = true;
files."files_test.lua" = { };
assertions = [
{
assertion = builtins.match ".*vim\.loader\.enable\(\).*" config.content != null;
message = "luaLoader is expected to be explicitly enabled.";
}
{
assertion = builtins.match ".*vim\.loader.*" config.files."files_test.lua".content == null;
message = "No luaLoader configuration is expected in 'files' submodules.";
}
];
};
# Test lua loader disabled
disabled.module =
{ config, ... }:
{
luaLoader.enable = false;
files."files_test.lua" = { };
assertions = [
{
assertion = builtins.match ".*vim\.loader\.disable\(\).*" config.content != null;
message = "luaLoader is expected to be explicitly disabled.";
}
{
assertion = builtins.match ".*vim\.loader.*" config.files."files_test.lua".content == null;
message = "No luaLoader configuration is expected in 'files' submodules.";
}
];
};
} }