From 4ec4c6fda90cec38b0878bf6e601f5df82ae5a8f Mon Sep 17 00:00:00 2001 From: Mykola Orliuk Date: Sun, 9 Apr 2023 22:21:36 +0200 Subject: [PATCH 1/3] lib/generators: add toLua/mkLuaInline Suitable to simplify Lua-based configurations like neovim-lspconfig that might need to interpolate Nix package paths. --- lib/generators.nix | 55 +++++++++++++++++++++++++++++++++++++++++ lib/tests/misc.nix | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/lib/generators.nix b/lib/generators.nix index 4357a0353398..41c2862973f1 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -426,4 +426,59 @@ ${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. + * + * 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)) + *> } + *> } + *> } + */ + toLua = { indent ? "" }@args: v: + with builtins; + let + indent' = "${indent} "; + args' = args // { indent = indent'; }; + concatItems = concatStringsSep ",\n"; + 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 + "{\n${concatItems (map (value: "${indent'}${toLua args' value}") v)}\n${indent}}") + else if isAttrs v then + ( + if isLuaInline v then + "(${v.expr})" + else if v == { } then + "{}" + else + "{\n${concatItems ( + lib.attrsets.mapAttrsToList (key: value: "${indent'}[${builtins.toJSON key}] = ${toLua args' value}") v + )}\n${indent}}" + ) + else + abort "generators.toLua: type ${typeOf v} is unsupported"; + + /* + * Mark string as Lua expression to be inlined when processed by toLua. + */ + mkLuaInline = expr: { _type = "lua-inline"; inherit expr; }; } diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index baa382f3e589..682b1a379fc3 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -915,6 +915,67 @@ 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 (\")" = generators.mkLuaInline ''"abc" .. "def"''; }; + expected = '' + { + ["some space and double-quote (\")"] = ("abc" .. "def") + }''; + }; + + 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 = { From a48fd10c8699c4ae3cd6b4565cf1ebf1d9b38333 Mon Sep 17 00:00:00 2001 From: Mykola Orliuk Date: Sun, 23 Apr 2023 19:33:11 +0200 Subject: [PATCH 2/3] lib.generators.toLua: tune comment for noogle use See https://github.com/nix-community/noogle --- lib/generators.nix | 58 ++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 41c2862973f1..9e663abbd57b 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -428,26 +428,37 @@ ${expr "" v} builtins.toJSON v; /* - * Translate a simple Nix expression to Lua representation with occasional - * Lua-inlines that can be construted by mkLuaInline function. - * - * 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)) - *> } - *> } - *> } - */ + Translate a simple Nix expression to Lua representation with occasional + Lua-inlines that can be construted by mkLuaInline function. + + Configuration: + * 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 = { indent ? "" }@args: v: with builtins; let @@ -478,7 +489,10 @@ ${expr "" v} abort "generators.toLua: type ${typeOf v} is unsupported"; /* - * Mark string as Lua expression to be inlined when processed by toLua. - */ + Mark string as Lua expression to be inlined when processed by toLua. + + Type: + mkLuaInline :: String -> AttrSet + */ mkLuaInline = expr: { _type = "lua-inline"; inherit expr; }; } From e9b416168a8dc4ce8d9ebdd571e83b5162d18df5 Mon Sep 17 00:00:00 2001 From: Mykola Orliuk Date: Sun, 23 Apr 2023 19:35:52 +0200 Subject: [PATCH 3/3] lib.generators.toLua: allow disabling multiline --- lib/generators.nix | 24 ++++++++++++++++-------- lib/tests/misc.nix | 9 +++++++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index 9e663abbd57b..4ecbdac3c125 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 682b1a379fc3..49336b8b9630 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" ];