nix-community.nixvim/lib/utils.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
1.3 KiB
Nix
Raw Normal View History

{
lib,
_nixvimTests,
}:
rec {
2024-01-25 16:58:58 +01:00
listToUnkeyedAttrs =
list:
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
# TODO: replace and deprecate
# We shouldn't need to use another instance of `lib` when building a test drv
enableExceptInTests = !_nixvimTests;
2024-01-25 16:58:58 +01:00
emptyTable = {
"__empty" = null;
};
2024-05-31 10:14:48 +02:00
/**
Turn all the keys of an attrs into raw lua.
# Example
```nix
toRawKeys { foo = 1; bar = 2; }
=> { __rawKey__foo = 1; __rawKey__bar = 2; }
```
# Type
```
toRawKeys :: AttrSet -> AttrSet
```
*/
2024-08-30 14:49:20 -05:00
toRawKeys = lib.mapAttrs' (n: v: lib.nameValuePair "__rawKey__${n}" v);
2024-05-31 10:14:48 +02:00
/**
Create a 1-element attrs with a raw lua key.
# Example
```nix
mkRawKey "foo" 1
=> { __rawKey__foo = 1; }
```
# Type
```
mkRawKey :: String -> String -> AttrSet
```
# Arguments
- [n] The attribute name (raw lua)
- [v] The attribute value
*/
mkRawKey = n: v: toRawKeys { "${n}" = v; };
mkRaw =
r:
if r == null || r == "" then
null
2024-08-30 14:49:20 -05:00
else if lib.isString r then
{ __raw = r; }
2024-08-30 14:49:20 -05:00
else if lib.types.isRawType r then
r
else
2024-08-30 14:49:20 -05:00
throw "mkRaw: invalid input: ${lib.generators.toPretty { multiline = false; } r}";
2024-01-25 16:58:58 +01:00
}