From 1d50fa4f63e27c30d44fa2ceab6ec4ce7d7d6df8 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Tue, 17 Dec 2024 21:18:57 +0100 Subject: [PATCH] lib: add applyPrefixToAttrs --- lib/default.nix | 1 + lib/utils.nix | 17 +++++++++++++++++ lib/vim-plugin.nix | 2 +- tests/lib-tests.nix | 11 +++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 0b7d17b2..88523b9e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -67,6 +67,7 @@ lib.fix ( ; inherit (self.utils) + applyPrefixToAttrs concatNonEmptyLines emptyTable enableExceptInTests diff --git a/lib/utils.nix b/lib/utils.nix index cde5485a..aaf65928 100644 --- a/lib/utils.nix +++ b/lib/utils.nix @@ -20,6 +20,23 @@ rec { "__empty" = null; }; + /** + Add a prefix to the keys of an attrs. + + # Example + ```nix + applyPrefixToAttrs "prefix_" { foo = 1; bar = 2; } + => { prefix_foo = 1; prefix_bar = 2; } + ``` + + # Type + + ``` + applyPrefixToAttrs :: String -> AttrSet -> AttrSet + ``` + */ + applyPrefixToAttrs = prefix: lib.mapAttrs' (n: lib.nameValuePair (prefix + n)); + /** Turn all the keys of an attrs into raw lua. diff --git a/lib/vim-plugin.nix b/lib/vim-plugin.nix index 8b18ead0..b5060486 100644 --- a/lib/vim-plugin.nix +++ b/lib/vim-plugin.nix @@ -113,7 +113,7 @@ extraPlugins = extraPlugins ++ [ (cfg.packageDecorator cfg.package) ]; - globals = lib.mapAttrs' (n: lib.nameValuePair (globalPrefix + n)) (cfg.settings or { }); + globals = lib.nixvim.applyPrefixToAttrs globalPrefix (cfg.settings or { }); } (lib.optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = lib.mkDefault colorscheme; diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index c719b170..98400f85 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -61,6 +61,17 @@ let expected = ''{ foo = "bar", qux = { 1, 2, 3 } }''; }; + testApplyPrefixToAttrs = { + expr = lib.nixvim.applyPrefixToAttrs "prefix_" { + foo = 1; + bar = 2; + }; + expected = { + prefix_foo = 1; + prefix_bar = 2; + }; + }; + testToLuaObjectRawLua = { expr = lib.nixvim.toLuaObject { __raw = ""; }; expected = "";