about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorMatthieu Coudron <teto@users.noreply.github.com>2023-04-23 20:45:01 +0200
committerGitHub <noreply@github.com>2023-04-23 20:45:01 +0200
commit377b6c7bc1c586dfd049520eaba8b9381206048e (patch)
tree069030f46b3ce97ce3436d03a47b03c3543bba2c /lib
parent8c5ec460034ec26ce37dd71e0c4d8fd79b7b48d3 (diff)
parente9b416168a8dc4ce8d9ebdd571e83b5162d18df5 (diff)
downloadnixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar.gz
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar.bz2
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar.lz
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar.xz
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.tar.zst
nixlib-377b6c7bc1c586dfd049520eaba8b9381206048e.zip
Merge pull request #225496 from ony/feature/generators-toLua
lib/generators: add toLua/mkLuaInline
Diffstat (limited to 'lib')
-rw-r--r--lib/generators.nix77
-rw-r--r--lib/tests/misc.nix66
2 files changed, 143 insertions, 0 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 4357a0353398..4ecbdac3c125 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -426,4 +426,81 @@ ${expr "" v}
       abort "generators.toDhall: cannot convert a null to Dhall"
     else
       builtins.toJSON v;
+
+  /*
+   Translate a simple Nix expression to Lua representation with occasional
+   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:
+     Regardless of multiline parameter there is no trailing newline.
+
+   Example:
+     generators.toLua {}
+       {
+         cmd = [ "typescript-language-server" "--stdio" ];
+         settings.workspace.library = mkLuaInline ''vim.api.nvim_get_runtime_file("", true)'';
+       }
+     ->
+      {
+        ["cmd"] = {
+          "typescript-language-server",
+          "--stdio"
+        },
+        ["settings"] = {
+          ["workspace"] = {
+            ["library"] = (vim.api.nvim_get_runtime_file("", true))
+          }
+        }
+      }
+
+   Type:
+     toLua :: AttrSet -> Any -> String
+  */
+  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
+      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
+      "nil"
+    else if isInt v || isFloat v || isString v || isBool v then
+      builtins.toJSON v
+    else if isList v then
+      (if v == [ ] then "{}" else
+      "{${introSpace}${concatItems (map (value: "${toLua innerArgs value}") v)}${outroSpace}}")
+    else if isAttrs v then
+      (
+        if isLuaInline v then
+          "(${v.expr})"
+        else if v == { } then
+          "{}"
+        else
+          "{${introSpace}${concatItems (
+            lib.attrsets.mapAttrsToList (key: value: "[${builtins.toJSON key}] = ${toLua innerArgs value}") v
+            )}${outroSpace}}"
+      )
+    else
+      abort "generators.toLua: type ${typeOf v} is unsupported";
+
+  /*
+   Mark string as Lua expression to be inlined when processed by toLua.
+
+   Type:
+     mkLuaInline :: String -> AttrSet
+  */
+  mkLuaInline = expr: { _type = "lua-inline"; inherit expr; };
 }
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index baa382f3e589..49336b8b9630 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -915,6 +915,72 @@ runTests {
   };
 
 
+  testToLuaEmptyAttrSet = {
+    expr = generators.toLua {} {};
+    expected = ''{}'';
+  };
+
+  testToLuaEmptyList = {
+    expr = generators.toLua {} [];
+    expected = ''{}'';
+  };
+
+  testToLuaListOfVariousTypes = {
+    expr = generators.toLua {} [ null 43 3.14159 true ];
+    expected = ''
+      {
+        nil,
+        43,
+        3.14159,
+        true
+      }'';
+  };
+
+  testToLuaString = {
+    expr = generators.toLua {} ''double-quote (") and single quotes (')'';
+    expected = ''"double-quote (\") and single quotes (')"'';
+  };
+
+  testToLuaAttrsetWithLuaInline = {
+    expr = generators.toLua {} { x = generators.mkLuaInline ''"abc" .. "def"''; };
+    expected = ''
+      {
+        ["x"] = ("abc" .. "def")
+      }'';
+  };
+
+  testToLuaAttrsetWithSpaceInKey = {
+    expr = generators.toLua {} { "some space and double-quote (\")" = 42; };
+    expected = ''
+      {
+        ["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" ];
+      settings.workspace.library = generators.mkLuaInline ''vim.api.nvim_get_runtime_file("", true)'';
+    };
+    expected = ''
+      {
+        ["cmd"] = {
+          "typescript-language-server",
+          "--stdio"
+        },
+        ["settings"] = {
+          ["workspace"] = {
+            ["library"] = (vim.api.nvim_get_runtime_file("", true))
+          }
+        }
+      }'';
+  };
+
 # CLI
 
   testToGNUCommandLine = {