lib/options: drop special case for string defaults

No longer assume that string-type plugin defaults are already
pre-formatted.

Instead, pre-formatted values should be defined using
`literalExpression` or `literalMD`.
This commit is contained in:
Matt Sturgeon 2024-06-11 16:45:56 +01:00
parent 735fbeece8
commit 9000e69f4b
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299

View file

@ -8,29 +8,13 @@ with nixvimUtils;
let let
# Render a plugin default string # Render a plugin default string
pluginDefaultText = pluginDefaultText =
let
# Assume a string `pluginDefault` is already formatted as intended,
# TODO: remove this behavior so we can quote strings properly
# historically strings were the only type accepted by mkDesc.
legacyRenderOptionValue =
v:
literalExpression (
if isString v then
v
else
generators.toPretty {
allowPrettyValues = true;
multiline = true;
} v
);
in
{ {
# plugin default: any value or literal expression # plugin default: any value or literal expression
pluginDefault, pluginDefault,
# nix option default value, used if `defaultText` is missing # nix option default value, used if `defaultText` is missing
default ? null, default ? null,
# nix option default string or literal expression # nix option default string or literal expression
defaultText ? (options.renderOptionValue default) // { defaultText ? options.renderOptionValue default // {
__lang = "nix"; __lang = "nix";
}, },
... ...
@ -41,7 +25,7 @@ let
if pluginDefault ? _type && pluginDefault ? text then if pluginDefault ? _type && pluginDefault ? text then
pluginDefault pluginDefault
else else
(legacyRenderOptionValue pluginDefault) // { __lang = "nix"; }; options.renderOptionValue pluginDefault // { __lang = "nix"; };
# Format text using markdown code block or inline code # Format text using markdown code block or inline code
# Handle `v` being a literalExpression or literalMD type # Handle `v` being a literalExpression or literalMD type
@ -204,18 +188,7 @@ rec {
mkUnsignedInt = pluginDefault: description: mkUnsignedInt' { inherit pluginDefault description; }; mkUnsignedInt = pluginDefault: description: mkUnsignedInt' { inherit pluginDefault description; };
mkBool' = args: mkNullableWithRaw' (args // { type = types.bool; }); mkBool' = args: mkNullableWithRaw' (args // { type = types.bool; });
mkBool = pluginDefault: description: mkBool' { inherit pluginDefault description; }; mkBool = pluginDefault: description: mkBool' { inherit pluginDefault description; };
mkStr' = mkStr' = args: mkNullableWithRaw' (args // { type = types.str; });
args:
mkNullableWithRaw' (
args
// {
type = types.str;
}
# TODO we should remove this once `mkDesc` no longer has a special case
// (optionalAttrs (args ? pluginDefault) {
pluginDefault = generators.toPretty { } args.pluginDefault;
})
);
mkStr = pluginDefault: description: mkStr' { inherit pluginDefault description; }; mkStr = pluginDefault: description: mkStr' { inherit pluginDefault description; };
mkAttributeSet' = args: mkNullable' (args // { type = nixvimTypes.attrs; }); mkAttributeSet' = args: mkNullable' (args // { type = nixvimTypes.attrs; });
@ -235,19 +208,20 @@ rec {
mkEnum' = mkEnum' =
{ values, ... }@args: { values, ... }@args:
# `values` is a list. If `pluginDefault` is present, then it is either null or one of `values` let
showInline = generators.toPretty { multiline = false; };
# Check `v` is either null, one of `values`, or a literal type
assertIsValid =
v:
v == null
|| elem v values
|| (v ? _type && v ? text)
|| abort "Default value ${showInline v} is not valid for enum ${showInline values}.";
in
# Ensure `values` is a list and `pluginDefault` is valid if present
assert isList values; assert isList values;
assert args ? pluginDefault -> (args.pluginDefault == null || elem args.pluginDefault values); assert args ? pluginDefault -> assertIsValid args.pluginDefault;
mkNullableWithRaw' ( mkNullableWithRaw' (removeAttrs args [ "values" ] // { type = types.enum values; });
(filterAttrs (n: v: n != "values") args)
// {
type = types.enum values;
}
# TODO we should remove this once `mkDesc` no longer has a special case
// (optionalAttrs (args ? pluginDefault) {
pluginDefault = generators.toPretty { } args.pluginDefault;
})
);
mkEnum = mkEnum =
values: pluginDefault: description: values: pluginDefault: description:
mkEnum' { inherit values pluginDefault description; }; mkEnum' { inherit values pluginDefault description; };