From 02e16b2a761f79058d3635d50579edb41cf6f46c Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Fri, 17 Jan 2025 16:14:52 +0100 Subject: [PATCH] lib/utils: add mkWarnings --- lib/default.nix | 1 + lib/utils.nix | 31 +++++++++++++++++++++++++++++++ tests/lib-tests.nix | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/lib/default.nix b/lib/default.nix index c0d1b935..599f74de 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -86,6 +86,7 @@ lib.makeExtensible ( mkIfNonNull' mkRaw mkRawKey + mkWarnings nestedLiteral nestedLiteralLua override diff --git a/lib/utils.nix b/lib/utils.nix index aaf65928..d2e240c5 100644 --- a/lib/utils.nix +++ b/lib/utils.nix @@ -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 ``` diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index c531d89b..c3c61458 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -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