add pipe, set, lset, require helpers

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-17 12:12:50 +03:00
parent eea936e1c5
commit bfa12f0d3f
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
2 changed files with 22 additions and 3 deletions

10
lib.nix
View File

@ -48,6 +48,7 @@ let
# add an empty line at the end # add an empty line at the end
++ [ (raw "") ] ++ [ (raw "") ]
); );
pipe = join ".";
isRaw = expr: getType expr == "raw"; isRaw = expr: getType expr == "raw";
raw = expr: raw = expr:
@ -69,6 +70,10 @@ let
++ [ (join ", " args) ] ++ [ (join ", " args) ]
++ [ (raw ")") ] ++ [ (raw ")") ]
); );
require = name: call "require" [ name ];
set = variable: value: join " = " [ (raw variable) value ];
lset = variable: value: join " " [ (raw "local") (set variable value) ];
isLuaNil = expr: getType expr == "nil"; isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; }; LuaNil = { _type = "nil"; };
@ -111,7 +116,6 @@ let
else error "Value '${toString expr}' is not supported yet"; else error "Value '${toString expr}' is not supported yet";
toLua = val: toLuaInternal 0 val; toLua = val: toLuaInternal 0 val;
in in
{ {
# Deprecated # Deprecated
@ -123,6 +127,6 @@ in
inherit toLua; inherit toLua;
inherit LuaNil; inherit LuaNil;
inherit raw join concat concatLines; inherit raw join concat concatLines pipe;
inherit call namedField; inherit namedField call require set lset;
} }

View File

@ -133,4 +133,19 @@ with nix2lua; pkgs.lib.runTests {
bar("baz", "biz") bar("baz", "biz")
''; '';
}; };
"test returns a pipe with many function call" = {
expr = toLua (pipe [
(require "nvim-treesitter.parsers")
(call "get_parser_configs" [ ])
]);
expected = "require(\"nvim-treesitter.parsers\").get_parser_configs()";
};
"test returns lua with setting value to the variable" = {
expr = toLua (set "parser_config.d2" { });
expected = "parser_config.d2 = { }";
};
"test returns lua with setting value to the local variable" = {
expr = toLua (lset "parser_config.d2" { });
expected = "local parser_config.d2 = { }";
};
} }