lib/utils: add mkWarnings

This commit is contained in:
Gaetan Lepage 2025-01-17 16:14:52 +01:00 committed by nix-infra-bot
parent 1036b8f8b6
commit 02e16b2a76
3 changed files with 56 additions and 0 deletions

View file

@ -86,6 +86,7 @@ lib.makeExtensible (
mkIfNonNull'
mkRaw
mkRawKey
mkWarnings
nestedLiteral
nestedLiteralLua
override

View file

@ -158,6 +158,7 @@ rec {
```
# Type
```
literalLua :: String -> AttrSet
```
@ -172,6 +173,34 @@ rec {
in
lib.literalExpression exp;
/**
Convert one or several conditional warnings to a final warning list.
The second argument can either be a list of _conditional warnings_ or a single one.
# Example
```nix
warnings = mkWarnings "plugins.foo" {
when = plugins.foo.settings.barIntegration && (!plugins.bar.enable);
message = "`barIntegration` is enabled but the `bar` plugin is not."
}
```
# Type
```
mkWarnings :: String -> List -> List
```
*/
mkWarnings =
scope: warnings:
let
processWarning =
warning:
lib.optional (warning.when or true) "Nixvim (${scope}): ${lib.trim (warning.message or warning)}";
in
builtins.concatMap processWarning (lib.toList warnings);
/**
Convert the given string into a `__pretty` printed mkRaw expression.
@ -190,6 +219,7 @@ rec {
```
# Type
```
nestedLiteralLua :: String -> AttrSet
```
@ -221,6 +251,7 @@ rec {
```
# Type
```
nestedLiteral :: (String | literalExpression) -> AttrSet
```

View file

@ -490,6 +490,30 @@ let
"mkAfter list".foo.bar = lib.mkAfter "Hello!";
};
};
testMkWarnings = {
expr =
(lib.nixvim.mkWarnings "foo-bar" [
{
when = true;
message = "This is the message";
}
{
when = false;
message = "This should not be included";
}
"This is an unconditional warning"
])
++ (lib.nixvim.mkWarnings "single-element") {
when = true;
message = "Hello";
};
expected = [
"Nixvim (foo-bar): This is the message"
"Nixvim (foo-bar): This is an unconditional warning"
"Nixvim (single-element): Hello"
];
};
};
in
if results == [ ] then