nix-community.nixvim/lib/utils.nix

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

117 lines
2.4 KiB
Nix
Raw Permalink Normal View History

{
lib,
_nixvimTests,
}:
rec {
/**
Transforms a list to an _"unkeyed"_ attribute set.
This allows to define mixed table/list in lua:
```nix
listToUnkeyedAttrs ["a" "b"] // { foo = "bar"; }
```
Resulting in the following lua:
```lua
{"a", "b", foo = "bar"}
```
*/
2024-01-25 16:58:58 +01:00
listToUnkeyedAttrs =
list:
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
/**
Usually `true`, except when nixvim is being evaluated by
`mkTestDerivationFromNixvimModule`, where it is `false`.
This can be used to dynamically enable plugins that can't be run in the
test environment.
*/
# TODO: replace and deprecate
# We shouldn't need to use another instance of `lib` when building a test drv
enableExceptInTests = !_nixvimTests;
/**
An empty lua table `{ }` that will be included in the final lua configuration.
This is equivalent to `{ __empty = { }; }`.
This form can allow to do `option.__empty = { }`.
*/
2024-01-25 16:58:58 +01:00
emptyTable = {
# Keep nested to bind docs to `emptyTable`, not `__empty`.
2024-01-25 16:58:58 +01:00
"__empty" = null;
};
2024-05-31 10:14:48 +02:00
/**
Convert the keys of the given attrset to _"raw lua"_.
2024-05-31 10:14:48 +02:00
# Example
```nix
toRawKeys { foo = 1; bar = 2; }
=> { __rawKey__foo = 1; __rawKey__bar = 2; }
```
Resulting in the following lua:
```lua
{[foo] = 1, [bar] = 2}
```
If "raw keys" are **not** used, the attr names are treated as string keys:
```lua
{foo = 1, bar = 2}
```
2024-05-31 10:14:48 +02:00
# 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; };
/**
Write the string `str` as raw lua in the final lua configuration.
This is equivalent to `{ __raw = "lua code"; }`.
This form can allow to do `option.__raw = "lua code"`.
*/
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
}