nix-community.nixvim/lib/option-warnings.nix

82 lines
2.3 KiB
Nix
Raw Normal View History

2023-01-20 00:15:58 +01:00
{ lib, ... }:
with lib;
2023-01-22 21:10:36 +01:00
rec {
2023-01-22 20:31:23 +01:00
# This should be used instead of mkRemovedOptionModule, when the option still works,
# but is just deprecated and should be changed now and for the future
2023-01-20 00:15:58 +01:00
mkDeprecatedOption =
{ option # an array of the path to the option
, alternative ? null
, message ? null
, visible ? false
}:
2023-01-22 21:42:23 +01:00
{ options, config, ... }:
2023-01-20 00:15:58 +01:00
let
fromOpt = getAttrFromPath option options;
2023-01-22 21:42:23 +01:00
fromValue = getAttrFromPath option config;
2023-01-22 20:31:23 +01:00
fullMessage = "The option `${showOption option}` has been deprecated, but might still work." +
2023-01-20 00:15:58 +01:00
(optionalString (alternative != null) " You may want to use `${showOption alternative}` instead.") +
(optionalString (message != null) " Message: ${message}");
in
{
2023-01-22 21:42:23 +01:00
config = mkIf (fromOpt.isDefined && fromValue != fromOpt.default) {
2023-01-20 00:15:58 +01:00
warnings = [
("Nixvim: ${fullMessage}")
];
};
};
2023-01-22 20:31:23 +01:00
2023-01-22 21:10:36 +01:00
mkRenamedOption =
{ option
, newOption
, visible ? false
, warn ? true
, overrideDescription ? null
}:
{ options, ... }:
let
fromOpt = getAttrFromPath option options;
2023-01-22 21:42:23 +01:00
# toOf = attrByPath newOption
# (abort "Renaming error: option `${showOption newOption}` does not exist.");
2023-01-22 21:10:36 +01:00
toType = let opt = attrByPath newOption { } options; in
opt.type or (types.submodule { });
message = "`${showOption option}` has been renamed to `${showOption newOption}`, but can still be used for compatibility";
in
{
options = setAttrByPath option (mkOption
{
inherit visible;
description =
if overrideDescription == null
then message
else overrideDescription;
} // optionalAttrs (toType != null) {
type = toType;
});
config = mkMerge [
{
2023-01-22 21:42:23 +01:00
warnings = mkIf (warn && fromOpt.isDefined) [ "Nixvim: ${message}" ];
2023-01-22 21:10:36 +01:00
}
(mkAliasAndWrapDefinitions (setAttrByPath newOption) fromOpt)
];
};
mkAliasOption = option: newOption: mkRenamedOption {
inherit option newOption;
visible = true;
warn = false;
overrideDescription = "Alias of ${showOption newOption}";
};
2023-01-22 20:31:23 +01:00
2023-01-22 21:10:36 +01:00
# TODO:
# mkRemovedOption =
# { option
# , visible ? false
# }:
# { options, ... }:
# {
# options = { };
# config = { };
# };
2023-01-20 00:15:58 +01:00
}