lib/options: allow pluginDefault in any helper

All helpers eventually go through `mkNullOrOption`, so we can move where
`pluginDefault` is handled there.

Added a private helper `processNixvimArgs` that can be used by any future
helper that needs to call `lib.mkOption` directly.

It might make sense to offer a `helpers.mkOption` which simply wraps
`lib.mkOption` but with support for custom args like `pluginDefault`?
This commit is contained in:
Matt Sturgeon 2024-06-11 02:15:26 +01:00
parent eb5c090e90
commit a8943f2502
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299

View file

@ -5,7 +5,7 @@
}: }:
with lib; with lib;
with nixvimUtils; with nixvimUtils;
rec { let
# Render a plugin default string # Render a plugin default string
pluginDefaultText = pluginDefaultText =
let let
@ -65,6 +65,19 @@ rec {
_Plugin default:_${toMD pluginDefaultText} _Plugin default:_${toMD pluginDefaultText}
''; '';
# Convert args into normal `mkOption`-style arguments, i.e. merge `pluginDefault` into `defaultText`.
#
# - `defaultText` is only set if `args` contains `pluginDefault`.
# - `pluginDefault` is removed from the resulting args.
# - All other args are untouched.
processNixvimArgs =
args:
(removeAttrs args [ "pluginDefault" ])
// (optionalAttrs (args ? pluginDefault) { defaultText = pluginDefaultText args; });
in
rec {
inherit pluginDefaultText;
# Creates an option with a nullable type that defaults to null. # Creates an option with a nullable type that defaults to null.
mkNullOrOption' = mkNullOrOption' =
{ {
@ -73,7 +86,7 @@ rec {
... ...
}@args: }@args:
lib.mkOption ( lib.mkOption (
args (processNixvimArgs args)
// { // {
type = lib.types.nullOr type; type = lib.types.nullOr type;
inherit default; inherit default;
@ -137,34 +150,20 @@ rec {
defaultNullOpts = defaultNullOpts =
let let
# Convert `defaultNullOpts`-style arguments into normal `mkOption`-style arguments, # Ensures that default is null and defaultText is not set
# i.e. merge `default` or `defaultText` into `defaultText`.
#
# "Plugin default" is only added if `args` has either a `default` or `defaultText` attribute.
convertArgs = convertArgs =
args: args:
( # TODO: uncomment once all call sites migrate to `pluginDefault`
# TODO filter pluginDefault # assert args ? default -> abort "defaultNullOpts: unexpected argument `default`. Did you mean `pluginDefault`?";
(filterAttrs ( assert
n: _: args ? defaultText
!(elem n [ -> abort "defaultNullOpts: unexpected argument `defaultText`. Did you mean `pluginDefault`?";
"pluginDefault" args
"defaultText" // {
]) default = null;
) args) }
// { # TODO: remove once all call sites migrate to `pluginDefault`
# TODO assert that args didn't attempt to set `default` or `defaultText` // (optionalAttrs (args ? default) { pluginDefault = args.pluginDefault or args.default; });
default = null;
}
// (optionalAttrs (args ? pluginDefault || args ? default || args ? defaultText) {
defaultText = pluginDefaultText {
# TODO: this is here for backwards compatibility:
# once `defaultNullOpts` migrates from `default` to `pluginDefault`
# then we can pass in `args` unmodified or simply inherit `pluginDefault`
pluginDefault = args.pluginDefault or args.defaultText or args.default;
};
})
);
in in
rec { rec {
# TODO: deprecated in favor of `helpers.pluginDefaultText` # TODO: deprecated in favor of `helpers.pluginDefaultText`