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; 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 defaultNullOpts = let
maybeRaw = t: lib.types.either t nixvimTypes.rawLua; maybeRaw = t: lib.types.either t nixvimTypes.rawLua;
in rec { in rec {
@ -116,6 +138,30 @@ with lib; rec {
# documentation # documentation
mkNullableWithRaw = type: mkNullable (maybeRaw type); 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: mkLua = default: desc:
mkNullOrLua mkNullOrLua
( (

View file

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

View file

@ -37,7 +37,7 @@ in {
If neither is found "unittest" is used. 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. Set to register test runners.
Built-in are test runners for unittest, pytest and django. Built-in are test runners for unittest, pytest and django.
The key is the test runner name, the value a function to generate the 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. 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 A lua function (`func(config, on_config)`) which allows an adapter to enrich a
configuration with additional information. It receives a configuration as first 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. argument, and a callback that must be called with the final configuration as second argument.
@ -129,7 +129,7 @@ in rec {
adapter adapter
// { // {
inherit type; inherit type;
enrich_config = helpers.mkRaw adapter.enrichConfig; enrich_config = adapter.enrichConfig;
} }
)) ))
adapters; adapters;

View file

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

View file

@ -81,7 +81,7 @@ with lib; let
}; };
parser = { parser = {
type = str; type = helpers.nixvimTypes.strLuaFn;
description = "The code for your parser function."; description = "The code for your parser function.";
example = '' example = ''
require('lint.parser').from_pattern(pattern, groups, severity_map, defaults, opts) 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). selection mode per capture, default is `v`(charwise).
''; '';
includeSurroundingWhitespace = includeSurroundingWhitespace = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`false`" ''
helpers.defaultNullOpts.mkNullable
(with types; either bool str)
"`false`"
''
`true` or `false`, when `true` textobjects are extended to include preceding or `true` or `false`, when `true` textobjects are extended to include preceding or
succeeding whitespace. succeeding whitespace.
@ -227,10 +223,7 @@ with lib; {
; ;
keymaps = processKeymapsOpt keymaps; keymaps = processKeymapsOpt keymaps;
selection_modes = selectionModes; selection_modes = selectionModes;
include_surrounding_whitespace = include_surrounding_whitespace = includeSurroundingWhitespace;
if isString includeSurroundingWhitespace
then helpers.mkRaw includeSurroundingWhitespace
else includeSurroundingWhitespace;
}; };
swap = with swap; { swap = with swap; {
inherit inherit

View file

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

View file

@ -29,7 +29,7 @@ in {
the side-by-side view even if this is set to false. 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. Defaults to the scrolloff.
''; '';
@ -78,10 +78,7 @@ in {
use_delta = useDelta; use_delta = useDelta;
use_custom_command = useCustomCommand; use_custom_command = useCustomCommand;
side_by_side = sideBySide; side_by_side = sideBySide;
diff_context_lines = diff_context_lines = diffContextLines;
if isString diffContextLines
then helpers.mkRaw diffContextLines
else diffContextLines;
entry_format = entryFormat; entry_format = entryFormat;
time_format = timeFormat; time_format = timeFormat;
mappings = with mappings; { mappings = with mappings; {

View file

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

View file

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

View file

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

View file

@ -151,7 +151,7 @@ in {
Setting the option after the first run has no effect. 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. Sets the pipeline to use to get completions.
See `|wilder-pipeline|`. See `|wilder-pipeline|`.