nix-community.nixvim/lib/options.nix

275 lines
7.2 KiB
Nix
Raw Normal View History

2024-01-25 15:43:06 +01:00
{
lib,
nixvimTypes,
2024-01-25 16:58:58 +01:00
nixvimUtils,
2024-01-25 15:43:06 +01:00
}:
2024-01-25 16:58:58 +01:00
with lib;
2024-05-05 19:39:35 +02:00
with nixvimUtils;
rec {
2024-01-25 15:43:06 +01:00
# Creates an option with a nullable type that defaults to null.
mkNullOrOption' =
{
type,
default ? null,
...
}@args:
lib.mkOption (
args
// {
type = lib.types.nullOr type;
inherit default;
}
);
mkNullOrOption = type: description: mkNullOrOption' { inherit type description; };
2024-01-25 15:43:06 +01:00
mkCompositeOption' =
{ options, ... }@args:
mkNullOrOption' (
(filterAttrs (n: _: n != "options") args) // { type = types.submodule { inherit options; }; }
);
mkCompositeOption = description: options: mkCompositeOption' { inherit description options; };
2024-01-25 15:43:06 +01:00
mkNullOrStr' = args: mkNullOrOption' (args // { type = with nixvimTypes; maybeRaw str; });
mkNullOrStr = description: mkNullOrStr' { inherit description; };
2024-01-25 15:43:06 +01:00
mkNullOrLua' =
args:
mkNullOrOption' (
args
// {
type = nixvimTypes.strLua;
apply = mkRaw;
}
);
mkNullOrLua = description: mkNullOrLua' { inherit description; };
2024-01-25 15:43:06 +01:00
2024-05-05 19:39:35 +02:00
mkNullOrLuaFn =
desc:
2024-01-25 15:43:06 +01:00
lib.mkOption {
type = lib.types.nullOr nixvimTypes.strLuaFn;
default = null;
description = desc;
apply = mkRaw;
};
2024-05-05 19:39:35 +02:00
mkNullOrStrLuaOr =
ty: desc:
2024-01-25 15:43:06 +01:00
lib.mkOption {
type = lib.types.nullOr (types.either nixvimTypes.strLua ty);
default = null;
description = desc;
2024-05-05 19:39:35 +02:00
apply = v: if builtins.isString v then mkRaw v else v;
2024-01-25 15:43:06 +01:00
};
2024-05-05 19:39:35 +02:00
mkNullOrStrLuaFnOr =
ty: desc:
2024-01-25 15:43:06 +01:00
lib.mkOption {
type = lib.types.nullOr (types.either nixvimTypes.strLuaFn ty);
default = null;
description = desc;
2024-05-05 19:39:35 +02:00
apply = v: if builtins.isString v then mkRaw v else v;
2024-01-25 15:43:06 +01:00
};
defaultNullOpts = rec {
/**
Build a description with a plugin default.
The [default] can be any value, and it will be formatted using `lib.generators.toPretty`.
If [default] is a String, it will not be formatted.
This behavior will likely change in the future.
# Example
```nix
mkDesc 1 "foo"
=> ''
foo
Plugin default: `1`
''
```
# Type
```
mkDesc :: Any -> String -> String
```
# Arguments
- [default] The plugin's default
- [desc] The option's description
*/
2024-05-17 10:30:31 +02:00
mkDesc =
default: desc:
let
# Assume a string default is already formatted as intended,
# historically strings were the only type accepted here.
# TODO deprecate this behavior so we can properly quote strings
defaultString = if isString default then default else generators.toPretty { } default;
defaultDesc =
"Plugin default:"
+ (
# Detect whether `default` is multiline or inline:
if hasInfix "\n" defaultString then "\n\n```nix\n${defaultString}\n```" else " `${defaultString}`"
);
2024-05-17 10:30:31 +02:00
in
if desc == "" then
defaultDesc
else
''
${desc}
${defaultDesc}
'';
2024-05-05 19:39:35 +02:00
mkNullable =
type: default: desc:
2024-05-17 10:30:31 +02:00
mkNullOrOption type (mkDesc default desc);
2024-01-25 15:43:06 +01:00
mkNullableWithRaw = type: mkNullable (nixvimTypes.maybeRaw type);
2024-01-25 15:43:06 +01:00
2024-05-05 19:39:35 +02:00
mkStrLuaOr =
type: default: desc:
2024-05-17 10:30:31 +02:00
mkNullOrStrLuaOr type (mkDesc default desc);
2024-01-25 15:43:06 +01:00
2024-05-05 19:39:35 +02:00
mkStrLuaFnOr =
type: default: desc:
2024-05-17 10:30:31 +02:00
mkNullOrStrLuaFnOr type (mkDesc default desc);
2024-01-25 15:43:06 +01:00
2024-05-17 10:30:31 +02:00
mkLua = default: desc: mkNullOrLua (mkDesc default desc);
2024-01-25 15:43:06 +01:00
2024-05-17 10:30:31 +02:00
mkLuaFn = default: desc: mkNullOrLuaFn (mkDesc default desc);
2024-01-25 15:43:06 +01:00
mkNum = mkNullableWithRaw types.number;
mkInt = mkNullableWithRaw types.int;
2024-01-25 15:43:06 +01:00
# Positive: >0
mkPositiveInt = mkNullableWithRaw types.ints.positive;
2024-01-25 15:43:06 +01:00
# Unsigned: >=0
mkUnsignedInt = mkNullableWithRaw types.ints.unsigned;
mkBool = mkNullableWithRaw types.bool;
mkStr =
# TODO we should delegate rendering quoted string to `mkDefaultDesc`,
# once we remove its special case for strings.
default:
assert default == null || isString default;
mkNullableWithRaw types.str (generators.toPretty { } default);
mkAttributeSet = mkNullable nixvimTypes.attrs;
2024-01-25 15:43:06 +01:00
mkListOf = ty: default: mkNullable (with nixvimTypes; listOf (maybeRaw ty)) default;
mkAttrsOf = ty: default: mkNullable (with nixvimTypes; attrsOf (maybeRaw ty)) default;
mkEnum =
enumValues: default:
mkNullableWithRaw (types.enum enumValues) (
# TODO we should remove this once `mkDefaultDesc` no longer has a special case
if isString default then generators.toPretty { } default else default
);
2024-01-25 15:43:06 +01:00
mkEnumFirstDefault = enumValues: mkEnum enumValues (head enumValues);
2024-05-05 19:39:35 +02:00
mkBorder =
default: name: desc:
mkNullableWithRaw nixvimTypes.border default (
2024-05-05 19:39:35 +02:00
let
defaultDesc = ''
Defines the border to use for ${name}.
Accepts same border values as `nvim_open_win()`. See `:help nvim_open_win()` for more info.
'';
in
if desc == "" then
defaultDesc
else
''
${desc}
${defaultDesc}
''
);
mkSeverity =
default: desc:
2024-01-25 15:43:06 +01:00
mkOption {
2024-05-05 19:39:35 +02:00
type =
with types;
nullOr (
either ints.unsigned (enum [
"error"
"warn"
"info"
"hint"
])
2024-01-25 15:43:06 +01:00
);
default = null;
2024-05-05 19:39:35 +02:00
apply = mapNullable (
value: if isInt value then value else mkRaw "vim.diagnostic.severity.${strings.toUpper value}"
);
2024-05-17 10:30:31 +02:00
description = mkDesc default desc;
2024-01-25 15:43:06 +01:00
};
2024-05-05 19:39:35 +02:00
mkLogLevel =
default: desc:
2024-01-25 15:43:06 +01:00
mkOption {
2024-05-05 19:39:35 +02:00
type = with types; nullOr (either ints.unsigned nixvimTypes.logLevel);
2024-01-25 15:43:06 +01:00
default = null;
2024-05-05 19:39:35 +02:00
apply = mapNullable (
value: if isInt value then value else mkRaw "vim.log.levels.${strings.toUpper value}"
);
2024-05-17 10:30:31 +02:00
description = mkDesc default desc;
2024-01-25 15:43:06 +01:00
};
2024-05-05 19:39:35 +02:00
mkHighlight =
default: name: desc:
mkNullable nixvimTypes.highlight default (if desc == "" then "Highlight settings." else desc);
2024-01-25 15:43:06 +01:00
};
mkPackageOption =
{
name ? null, # Can be null if a custom description is given.
default,
description ? null,
example ? null,
}:
mkOption {
type = with types; nullOr package;
inherit default example;
description =
if description == null then
''
Which package to use for `${name}`.
Set to `null` to disable its automatic installation.
''
else
description;
};
mkPluginPackageOption =
2024-05-05 19:39:35 +02:00
name: default:
2024-01-25 15:43:06 +01:00
mkOption {
type = types.package;
inherit default;
description = "Which package to use for the ${name} plugin.";
2024-01-25 15:43:06 +01:00
};
2024-02-12 16:11:34 +01:00
2024-05-05 19:39:35 +02:00
mkSettingsOption =
{
options ? { },
description,
example ? null,
}:
2024-02-12 16:11:34 +01:00
mkOption {
2024-05-05 19:39:35 +02:00
type =
with types;
2024-02-12 16:11:34 +01:00
submodule {
freeformType = attrsOf anything;
2024-02-12 16:11:34 +01:00
inherit options;
};
2024-05-05 19:39:35 +02:00
default = { };
2024-02-12 16:11:34 +01:00
inherit description;
example =
2024-05-05 19:39:35 +02:00
if example == null then
{
foo_bar = 42;
hostname = "localhost:8080";
callback.__raw = ''
function()
print('nixvim')
end
'';
}
else
example;
2024-02-12 16:11:34 +01:00
};
2024-01-25 15:43:06 +01:00
}