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
++ [ (raw "") ]
);
pipe = join ".";
isRaw = expr: getType expr == "raw";
raw = expr:
@ -69,6 +70,10 @@ let
++ [ (join ", " args) ]
++ [ (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";
LuaNil = { _type = "nil"; };
@ -111,7 +116,6 @@ let
else error "Value '${toString expr}' is not supported yet";
toLua = val: toLuaInternal 0 val;
in
{
# Deprecated
@ -123,6 +127,6 @@ in
inherit toLua;
inherit LuaNil;
inherit raw join concat concatLines;
inherit call namedField;
inherit raw join concat concatLines pipe;
inherit namedField call require set lset;
}

View file

@ -133,4 +133,19 @@ with nix2lua; pkgs.lib.runTests {
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 = { }";
};
}