mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-22 17:03:31 +02:00
lib/utils: add nestedLiteral
and nestedLiteralLua
`nestedLiteral` converts a `literalExpression` into a `toPretty` style pretty-printer. `nestedLiteralLua` composes `nestedLiteral` and `literalLua` together.
This commit is contained in:
parent
eb76e62a9b
commit
da6e3499d4
3 changed files with 88 additions and 0 deletions
|
@ -97,6 +97,8 @@ lib.fix (
|
||||||
mkIfNonNull'
|
mkIfNonNull'
|
||||||
mkRaw
|
mkRaw
|
||||||
mkRawKey
|
mkRawKey
|
||||||
|
nestedLiteral
|
||||||
|
nestedLiteralLua
|
||||||
override
|
override
|
||||||
overrideDerivation
|
overrideDerivation
|
||||||
toRawKeys
|
toRawKeys
|
||||||
|
|
|
@ -155,6 +155,64 @@ rec {
|
||||||
in
|
in
|
||||||
lib.literalExpression exp;
|
lib.literalExpression exp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convert the given string into a `__pretty` printed mkRaw expression.
|
||||||
|
|
||||||
|
For use in nested values that will be printed by `lib.generators.toPretty`,
|
||||||
|
or `lib.options.renderOptionValue`.
|
||||||
|
|
||||||
|
# Example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
nestedLiteralLua "print('hi')"
|
||||||
|
=> nestedLiteral (literalLua "print('hi')")
|
||||||
|
=> {
|
||||||
|
__pretty = lib.getAttr "text";
|
||||||
|
val = literalLua "print('hi')";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Type
|
||||||
|
```
|
||||||
|
nestedLiteralLua :: String -> AttrSet
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
nestedLiteralLua = r: nestedLiteral (literalLua r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convert the given string or literalExpression into a `__pretty` printed expression.
|
||||||
|
|
||||||
|
For use in nested values that will be printed by `lib.generators.toPretty`,
|
||||||
|
or `lib.options.renderOptionValue`.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```nix
|
||||||
|
nestedLiteral "example"
|
||||||
|
=> {
|
||||||
|
__pretty = lib.getAttr "text";
|
||||||
|
val = literalExpression "example";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```nix
|
||||||
|
nestedLiteral (literalExpression ''"hello, world"'')
|
||||||
|
=> {
|
||||||
|
__pretty = lib.getAttr "text";
|
||||||
|
val = literalExpression ''"hello, world"'';
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Type
|
||||||
|
```
|
||||||
|
nestedLiteral :: (String | literalExpression) -> AttrSet
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
nestedLiteral = val: {
|
||||||
|
__pretty = lib.getAttr "text";
|
||||||
|
val = if val._type or null == "literalExpression" then val else lib.literalExpression val;
|
||||||
|
};
|
||||||
|
|
||||||
wrapDo = string: ''
|
wrapDo = string: ''
|
||||||
do
|
do
|
||||||
${string}
|
${string}
|
||||||
|
|
|
@ -331,6 +331,34 @@ let
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Integration test for nestedLiteral and renderOptionValue
|
||||||
|
testNestedLiteral_withRenderOptionValue = {
|
||||||
|
expr =
|
||||||
|
builtins.mapAttrs
|
||||||
|
(
|
||||||
|
_: v:
|
||||||
|
(lib.options.renderOptionValue {
|
||||||
|
literal = helpers.nestedLiteral v;
|
||||||
|
}).text
|
||||||
|
)
|
||||||
|
{
|
||||||
|
empty = "";
|
||||||
|
sum = "1 + 1";
|
||||||
|
print = ''lib.mkRaw "print('hi')"'';
|
||||||
|
};
|
||||||
|
expected =
|
||||||
|
builtins.mapAttrs
|
||||||
|
(_: literal: ''
|
||||||
|
{
|
||||||
|
literal = ${literal};
|
||||||
|
}'')
|
||||||
|
{
|
||||||
|
empty = "";
|
||||||
|
sum = "1 + 1";
|
||||||
|
print = ''lib.mkRaw "print('hi')"'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
testUpperFirstChar = {
|
testUpperFirstChar = {
|
||||||
expr = map helpers.upperFirstChar [
|
expr = map helpers.upperFirstChar [
|
||||||
"foo"
|
"foo"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue