helpers: Add option helpers for types that are either a lua str or a value (#876)

Those types needed the following code in apply:

if builtins.isString value
then mkRaw value
else value

This commit avoids this boilerplate, and clarifies that the `str` is lua
code in the documentation.
This commit is contained in:
traxys 2024-01-02 00:30:10 +01:00 committed by GitHub
parent 8b8a1c0f4b
commit 8aa4b7e4ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 109 additions and 129 deletions

View file

@ -95,6 +95,28 @@ with lib; rec {
apply = mkRaw;
};
mkNullOrStrLuaOr = ty: desc:
lib.mkOption {
type = lib.types.nullOr (types.either nixvimTypes.strLua ty);
default = null;
description = desc;
apply = v:
if builtins.isString v
then mkRaw v
else v;
};
mkNullOrStrLuaFnOr = ty: desc:
lib.mkOption {
type = lib.types.nullOr (types.either nixvimTypes.strLuaFn ty);
default = null;
description = desc;
apply = v:
if builtins.isString v
then mkRaw v
else v;
};
defaultNullOpts = let
maybeRaw = t: lib.types.either t nixvimTypes.rawLua;
in rec {
@ -116,6 +138,30 @@ with lib; rec {
# documentation
mkNullableWithRaw = type: mkNullable (maybeRaw type);
mkStrLuaOr = type: default: desc:
mkNullOrStrLuaOr type (let
defaultDesc = "default: `${default}`";
in
if desc == ""
then defaultDesc
else ''
${desc}
${defaultDesc}
'');
mkStrLuaFnOr = type: default: desc:
mkNullOrStrLuaFnOr type (let
defaultDesc = "default: `${default}`";
in
if desc == ""
then defaultDesc
else ''
${desc}
${defaultDesc}
'');
mkLua = default: desc:
mkNullOrLua
(

View file

@ -21,13 +21,8 @@ in {
'';
overrides =
helpers.defaultNullOpts.mkNullable
(
with types;
either
(attrsOf helpers.nixvimTypes.highlight)
str
)
helpers.defaultNullOpts.mkStrLuaOr
(with helpers.nixvimTypes; attrsOf highlight)
"{}"
''
A dictionary of group names, each associated with a dictionary of parameters
@ -44,10 +39,7 @@ in {
config = let
setupOptions = with cfg;
{
overrides =
if isString overrides
then helpers.mkRaw overrides
else overrides;
inherit overrides;
}
// cfg.extraOptions;
in

View file

@ -37,7 +37,7 @@ in {
If neither is found "unittest" is used.
'';
testRunners = helpers.mkNullOrOption (types.attrsOf types.str) ''
testRunners = helpers.mkNullOrOption (with helpers.nixvimTypes; attrsOf strLuaFn) ''
Set to register test runners.
Built-in are test runners for unittest, pytest and django.
The key is the test runner name, the value a function to generate the

View file

@ -12,7 +12,7 @@ in rec {
For most debug adapters setting this is not necessary.
'';
enrichConfig = helpers.mkNullOrOption types.str ''
enrichConfig = helpers.mkNullOrLuaFn ''
A lua function (`func(config, on_config)`) which allows an adapter to enrich a
configuration with additional information. It receives a configuration as first
argument, and a callback that must be called with the final configuration as second argument.
@ -129,7 +129,7 @@ in rec {
adapter
// {
inherit type;
enrich_config = helpers.mkRaw adapter.enrichConfig;
enrich_config = adapter.enrichConfig;
}
))
adapters;

View file

@ -769,8 +769,8 @@ in {
"This is determined automatically, you probably don't need to set it";
findArgs =
helpers.mkNullOrOption
(types.either types.str (types.submodule {
helpers.mkNullOrStrLuaFnOr
(types.submodule {
options = {
fd =
helpers.defaultNullOpts.mkNullable (types.listOf types.str)
@ -784,7 +784,7 @@ in {
''
"You can specify extra args to pass to the find command.";
};
}))
})
''
Find arguments
@ -1154,10 +1154,7 @@ in {
};
find_by_full_path_words = findByFullPathWords;
find_command = findCommand;
find_args =
if isString findArgs
then mkRaw findArgs
else findArgs;
find_args = findArgs;
group_empty_dirs = groupEmptyDirs;
search_limit = searchLimit;
follow_current_file = followCurrentFile;

View file

@ -81,7 +81,7 @@ with lib; let
};
parser = {
type = str;
type = helpers.nixvimTypes.strLuaFn;
description = "The code for your parser function.";
example = ''
require('lint.parser').from_pattern(pattern, groups, severity_map, defaults, opts)

View file

@ -84,11 +84,7 @@ with lib; {
selection mode per capture, default is `v`(charwise).
'';
includeSurroundingWhitespace =
helpers.defaultNullOpts.mkNullable
(with types; either bool str)
"`false`"
''
includeSurroundingWhitespace = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`false`" ''
`true` or `false`, when `true` textobjects are extended to include preceding or
succeeding whitespace.
@ -227,10 +223,7 @@ with lib; {
;
keymaps = processKeymapsOpt keymaps;
selection_modes = selectionModes;
include_surrounding_whitespace =
if isString includeSurroundingWhitespace
then helpers.mkRaw includeSurroundingWhitespace
else includeSurroundingWhitespace;
include_surrounding_whitespace = includeSurroundingWhitespace;
};
swap = with swap; {
inherit

View file

@ -218,8 +218,7 @@ in {
'';
clearOnDetach =
helpers.defaultNullOpts.mkNullable
(with types; either str (enum [false]))
helpers.defaultNullOpts.mkStrLuaFnOr (types.enum [false])
''
function(client_id)
local client = vim.lsp.get_client_by_id(client_id)
@ -264,11 +263,7 @@ in {
per-server limit.
'';
doneTtl =
helpers.defaultNullOpts.mkNullable
(with types; either str ints.unsigned)
"3"
''
doneTtl = helpers.defaultNullOpts.mkStrLuaOr types.ints.unsigned "3" ''
How long a message should persist after completion.
Set to `0` to use notification group config default, and `math.huge` to show
@ -283,11 +278,7 @@ in {
Highlight group for completed LSP tasks.
'';
progressTtl =
helpers.defaultNullOpts.mkNullable
(with types; either str ints.unsigned)
"math.huge"
''
progressTtl = helpers.defaultNullOpts.mkStrLuaFnOr types.ints.unsigned "math.huge" ''
How long a message should persist when in progress.
'';
@ -441,8 +432,7 @@ in {
'';
redirect =
helpers.defaultNullOpts.mkNullable
(with types; either str (enum [false]))
helpers.defaultNullOpts.mkStrLuaFnOr (types.enum [false])
''
function(msg, level, opts)
if opts and opts.on_open then
@ -654,23 +644,14 @@ in {
ignore_done_already = ignoreDoneAlready;
ignore_empty_message = ignoreEmptyMessage;
notification_group = notificationGroup;
clear_on_detach =
if isString clearOnDetach
then helpers.mkRaw clearOnDetach
else clearOnDetach;
clear_on_detach = clearOnDetach;
inherit ignore;
display = with display; {
render_limit = renderLimit;
done_ttl =
if isString doneTtl
then helpers.mkRaw doneTtl
else doneTtl;
done_ttl = doneTtl;
done_icon = doneIcon;
done_style = doneStyle;
progress_ttl =
if isString progressTtl
then helpers.mkRaw progressTtl
else progressTtl;
progress_ttl = progressTtl;
progress_icon = progressIcon;
progress_style = progressStyle;
group_style = groupStyle;
@ -711,10 +692,7 @@ in {
)
configs
);
redirect =
if isString redirect
then helpers.mkRaw redirect
else redirect;
inherit redirect;
view = with view; {
stack_upwards = stackUpwards;
icon_separator = iconSeparator;

View file

@ -29,7 +29,7 @@ in {
the side-by-side view even if this is set to false.
'';
diffContextLines = helpers.defaultNullOpts.mkNullable (with types; either ints.unsigned str) "vim.o.scrolloff" ''
diffContextLines = helpers.defaultNullOpts.mkStrLuaOr types.ints.unsigned "vim.o.scrolloff" ''
Defaults to the scrolloff.
'';
@ -78,10 +78,7 @@ in {
use_delta = useDelta;
use_custom_command = useCustomCommand;
side_by_side = sideBySide;
diff_context_lines =
if isString diffContextLines
then helpers.mkRaw diffContextLines
else diffContextLines;
diff_context_lines = diffContextLines;
entry_format = entryFormat;
time_format = timeFormat;
mappings = with mappings; {

View file

@ -20,11 +20,7 @@ in {
'';
timeout =
helpers.defaultNullOpts.mkNullable
(
with types;
either ints.unsigned str
)
helpers.defaultNullOpts.mkStrLuaOr types.ints.unsigned
"vim.o.timeoutlen"
''
The time in which the keys must be hit in ms.
@ -58,11 +54,7 @@ in {
config = let
setupOptions = with cfg;
{
inherit mapping;
timeout =
if isString timeout
then helpers.mkRaw timeout
else timeout;
inherit mapping timeout;
clear_empty_lines = clearEmptyLines;
inherit keys;
}

View file

@ -219,8 +219,7 @@ in {
'';
transformExplicit =
helpers.defaultNullOpts.mkNullable
(with types; either str (enum [false]))
helpers.defaultNullOpts.mkStrLuaFnOr (types.enum [false])
"false"
''
A function that transforms the text to be inserted as the source/path of a link when a
@ -241,8 +240,7 @@ in {
'';
transformImplicit =
helpers.defaultNullOpts.mkNullable
(with types; either str (enum [false]))
helpers.defaultNullOpts.mkStrLuaFnOr (types.enum [false])
''
function(text)
text = text:gsub(" ", "-")
@ -578,14 +576,8 @@ in {
context
;
implicit_extension = implicitExtension;
transform_implicit =
if isString transformImplicit
then helpers.mkRaw transformImplicit
else transformImplicit;
transform_explicit =
if isString transformExplicit
then helpers.mkRaw transformExplicit
else transformExplicit;
transform_implicit = transformImplicit;
transform_explicit = transformExplicit;
};
to_do = with toDo; {
inherit symbols;

View file

@ -13,9 +13,7 @@ in {
package = helpers.mkPackageOption "toggleterm" pkgs.vimPlugins.toggleterm-nvim;
size =
helpers.defaultNullOpts.mkNullable
(with types; either number str) "12" ''
size = helpers.defaultNullOpts.mkStrLuaFnOr types.number "12" ''
Size of the terminal.
`size` can be a number or function
Example:
@ -170,12 +168,7 @@ in {
};
config = let
setupOptions = with cfg; {
inherit autochdir highlights direction shell;
size = helpers.ifNonNull' size (
if isInt size
then size
else helpers.mkRaw size
);
inherit autochdir highlights direction shell size;
open_mapping = helpers.ifNonNull' openMapping (helpers.mkRaw "[[${openMapping}]]");
on_create = onCreate;
on_open = onOpen;

View file

@ -151,7 +151,7 @@ in {
Setting the option after the first run has no effect.
'';
pipeline = helpers.mkNullOrOption (with types; listOf str) ''
pipeline = helpers.mkNullOrOption (with helpers.nixvimTypes; listOf strLua) ''
Sets the pipeline to use to get completions.
See `|wilder-pipeline|`.