summary refs log tree commit diff
diff options
context:
space:
mode:
authorMykola Orliuk <virkony@gmail.com>2023-04-23 19:35:52 +0200
committerMykola Orliuk <virkony@gmail.com>2023-04-23 19:46:14 +0200
commite9b416168a8dc4ce8d9ebdd571e83b5162d18df5 (patch)
treecb127a9537a8c6a1eec5c6b397375828efb9bd2b
parenta48fd10c8699c4ae3cd6b4565cf1ebf1d9b38333 (diff)
downloadnixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar.gz
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar.bz2
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar.lz
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar.xz
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.tar.zst
nixpkgs-e9b416168a8dc4ce8d9ebdd571e83b5162d18df5.zip
lib.generators.toLua: allow disabling multiline
-rw-r--r--lib/generators.nix24
-rw-r--r--lib/tests/misc.nix9
2 files changed, 23 insertions, 10 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 9e663abbd57..4ecbdac3c12 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -432,6 +432,7 @@ ${expr "" v}
    Lua-inlines that can be construted by mkLuaInline function.
 
    Configuration:
+     * multiline - by default is true which results in indented block-like view.
      * indent - initial indent.
 
    Attention:
@@ -459,12 +460,19 @@ ${expr "" v}
    Type:
      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;
     let
-      indent' = "${indent}  ";
-      args' = args // { indent = indent'; };
-      concatItems = concatStringsSep ",\n";
+      innerIndent = "${indent}  ";
+      introSpace = if multiline then "\n${innerIndent}" else " ";
+      outroSpace = if multiline then "\n${indent}" else " ";
+      innerArgs = args // { indent = innerIndent; };
+      concatItems = concatStringsSep ",${introSpace}";
       isLuaInline = { _type ? null, ... }: _type == "lua-inline";
     in
     if v == null then
@@ -473,7 +481,7 @@ ${expr "" v}
       builtins.toJSON v
     else if isList v then
       (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
       (
         if isLuaInline v then
@@ -481,9 +489,9 @@ ${expr "" v}
         else if v == { } then
           "{}"
         else
-          "{\n${concatItems (
-            lib.attrsets.mapAttrsToList (key: value: "${indent'}[${builtins.toJSON key}] = ${toLua args' value}") v
-            )}\n${indent}}"
+          "{${introSpace}${concatItems (
+            lib.attrsets.mapAttrsToList (key: value: "[${builtins.toJSON key}] = ${toLua innerArgs value}") v
+            )}${outroSpace}}"
       )
     else
       abort "generators.toLua: type ${typeOf v} is unsupported";
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 682b1a379fc..49336b8b963 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -950,13 +950,18 @@ runTests {
   };
 
   testToLuaAttrsetWithSpaceInKey = {
-    expr = generators.toLua {} { "some space and double-quote (\")" = generators.mkLuaInline ''"abc" .. "def"''; };
+    expr = generators.toLua {} { "some space and double-quote (\")" = 42; };
     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 = {
     expr = generators.toLua {} {
       cmd = [ "typescript-language-server" "--stdio" ];