lib.generators.toLua: allow disabling multiline

This commit is contained in:
Mykola Orliuk
2023-04-23 19:35:52 +02:00
parent a48fd10c86
commit e9b416168a
2 changed files with 23 additions and 10 deletions

View File

@@ -432,6 +432,7 @@ ${expr "" v}
Lua-inlines that can be construted by mkLuaInline function. Lua-inlines that can be construted by mkLuaInline function.
Configuration: Configuration:
* multiline - by default is true which results in indented block-like view.
* indent - initial indent. * indent - initial indent.
Attention: Attention:
@@ -459,12 +460,19 @@ ${expr "" v}
Type: Type:
toLua :: AttrSet -> Any -> String toLua :: AttrSet -> Any -> String
*/ */
toLua = { indent ? "" }@args: v: toLua = {
/* If this option is true, the output is indented with newlines for attribute sets and lists */
multiline ? true,
/* Initial indentation level */
indent ? ""
}@args: v:
with builtins; with builtins;
let let
indent' = "${indent} "; innerIndent = "${indent} ";
args' = args // { indent = indent'; }; introSpace = if multiline then "\n${innerIndent}" else " ";
concatItems = concatStringsSep ",\n"; outroSpace = if multiline then "\n${indent}" else " ";
innerArgs = args // { indent = innerIndent; };
concatItems = concatStringsSep ",${introSpace}";
isLuaInline = { _type ? null, ... }: _type == "lua-inline"; isLuaInline = { _type ? null, ... }: _type == "lua-inline";
in in
if v == null then if v == null then
@@ -473,7 +481,7 @@ ${expr "" v}
builtins.toJSON v builtins.toJSON v
else if isList v then else if isList v then
(if v == [ ] then "{}" else (if v == [ ] then "{}" else
"{\n${concatItems (map (value: "${indent'}${toLua args' value}") v)}\n${indent}}") "{${introSpace}${concatItems (map (value: "${toLua innerArgs value}") v)}${outroSpace}}")
else if isAttrs v then else if isAttrs v then
( (
if isLuaInline v then if isLuaInline v then
@@ -481,9 +489,9 @@ ${expr "" v}
else if v == { } then else if v == { } then
"{}" "{}"
else else
"{\n${concatItems ( "{${introSpace}${concatItems (
lib.attrsets.mapAttrsToList (key: value: "${indent'}[${builtins.toJSON key}] = ${toLua args' value}") v lib.attrsets.mapAttrsToList (key: value: "[${builtins.toJSON key}] = ${toLua innerArgs value}") v
)}\n${indent}}" )}${outroSpace}}"
) )
else else
abort "generators.toLua: type ${typeOf v} is unsupported"; abort "generators.toLua: type ${typeOf v} is unsupported";

View File

@@ -950,13 +950,18 @@ runTests {
}; };
testToLuaAttrsetWithSpaceInKey = { testToLuaAttrsetWithSpaceInKey = {
expr = generators.toLua {} { "some space and double-quote (\")" = generators.mkLuaInline ''"abc" .. "def"''; }; expr = generators.toLua {} { "some space and double-quote (\")" = 42; };
expected = '' expected = ''
{ {
["some space and double-quote (\")"] = ("abc" .. "def") ["some space and double-quote (\")"] = 42
}''; }'';
}; };
testToLuaWithoutMultiline = {
expr = generators.toLua { multiline = false; } [ 41 43 ];
expected = ''{ 41, 43 }'';
};
testToLuaBasicExample = { testToLuaBasicExample = {
expr = generators.toLua {} { expr = generators.toLua {} {
cmd = [ "typescript-language-server" "--stdio" ]; cmd = [ "typescript-language-server" "--stdio" ];