Compare commits

...

2 commits

3 changed files with 55 additions and 20 deletions

View file

@ -104,14 +104,14 @@ Add `nix2lua` as input to your `flake.nix`
`pipe expr`
> Joins expressions with a '.' (dot) separator.
> If list of expressions contains a string it will
> convert to the raw.
> Joins expressions with a '.' (dot) separator. If list of expressions contains
> a string it will convert to the raw.
>
> ```nix
> toLua (pipe ["vim" "opt" "line"]);
> # vim.opt.line
> ```
>
> ```nix
> toLua (pipe ["luasnip" (call0 "setup")]);
> # luasnip.setup()
@ -128,8 +128,8 @@ Add `nix2lua` as input to your `flake.nix`
`spaceBetween expr`
> Join expressions with a space separator. You can use it to separate
> all expressions in the Lua configuration.
> Join expressions with a space separator. You can use it to separate all
> expressions in the Lua configuration.
>
> This is alias for `join " "`
>
@ -144,14 +144,15 @@ Add `nix2lua` as input to your `flake.nix`
`call fnName args`
> Util to call a Lua function with arguments.
>
> If `args` is list that each list item render as function argument.
> Otherwise, it will be rendered as first argument.
>
> If `args` is list that each list item render as function argument. Otherwise,
> it will be rendered as first argument.
>
> ```nix
> toLua (call "setup" [ "foo" "bar" ]);
> # setup("foo", "bar")
> ```
>
> ```nix
> toLua (call "setup" { foo = "bar" });
> # or: toLua (call "setup" [{ foo = "bar" }]);
@ -171,9 +172,8 @@ Add `nix2lua` as input to your `flake.nix`
`call1 fnName arg`
> Call a Lua function with one argument.
> This is useful when a Lua function has only one argument of type list,
> and we want to avoid using an inner list.
> Call a Lua function with one argument. This is useful when a Lua function has
> only one argument of type list, and we want to avoid using an inner list.
>
> This is alias for `call fnName [arg]`.
>
@ -187,6 +187,7 @@ Add `nix2lua` as input to your `flake.nix`
> Call a require function to import module with `name`.
>
> This is alias for `call "require" name`.
>
> ```nix
> toLua (require "luasnip")
> # require("luasnip")
@ -233,6 +234,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> lset = l: r: local (set l r);
> ```
@ -247,6 +249,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> func0 = fnName: func fnName [ ];
>
@ -256,8 +259,8 @@ Add `nix2lua` as input to your `flake.nix`
`lambda params body`
> Define a lambda function with specific parameters and body.
> Useful to use Lua function inline.
> Define a lambda function with specific parameters and body. Useful to use Lua
> function inline.
>
> ```nix
> toLua (call "setup" {
@ -267,6 +270,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> lambda0 = lambda [ ];
> ```
@ -295,6 +299,7 @@ Add `nix2lua` as input to your `flake.nix`
> ```
>
> Useful aliases:
>
> ```
> return_void = return null;
> ```
@ -317,6 +322,21 @@ Add `nix2lua` as input to your `flake.nix`
> # if (10 == 10) print("yes") end
> ```
`do body`
> Limit scope of the variables
>
> ```nix
> toLua (do [(requireTo "foo" "foo")])
> # do local set foo = require("foo") end
> ```
>
> Aliases:
>
> ```
> scope = do
> ```
# License
GNU General Public License v3.0 or later

25
lib.nix
View file

@ -127,7 +127,9 @@ let
if isJoin expr && expr.sep == "." then expr
else if isRaw expr then expr
else raw (validString expr);
set = var: val: join " = " [ (validSetVariable var) val ];
set = var: val:
if val == null then null
else join " = " [ (validSetVariable var) val ];
# Type: validBlockBody :: a -> [b]
validBlockBody = body:
@ -170,6 +172,8 @@ let
]));
if' = condition: trueBody: ifelse condition trueBody [ ];
do = body: spaceBetween [ kw_do ] ++ body ++ [ kw_end ];
isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; };
@ -228,10 +232,19 @@ in
inherit toLua;
inherit LuaNil;
inherit raw join concat spaceBetween;
inherit raw;
var = raw;
vars = map raw;
inherit join concat spaceBetween;
inherit pipe pipe1;
inherit namedField local;
inherit local;
inherit namedField;
nf = namedField;
inherit ifelse if';
inherit set;
lset = l: r: local (set l r);
@ -249,8 +262,6 @@ in
inherit return return_void;
# useful aliases
var = raw;
vars = map raw;
nf = namedField;
inherit do;
scope = do;
}

View file

@ -193,6 +193,10 @@ with nix2lua; pkgs.lib.runTests {
expr = toLua (lset "parser_config.d2" { });
expected = "local parser_config.d2 = { }";
};
"test skips a var definition if value is null" = {
expr = toLua (set "vim.opt.number" null);
expected = null;
};
"test returns all operations" = {
expr = toLua (spaceBetween [
(set "a" (eq (mul (add 1 2) (sub 2 1)) 3))