Compare commits
2 commits
00364dee14
...
654eda0737
Author | SHA1 | Date | |
---|---|---|---|
654eda0737 | |||
61cd06db30 |
3 changed files with 55 additions and 20 deletions
44
README.md
44
README.md
|
@ -104,14 +104,14 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
|
|
||||||
`pipe expr`
|
`pipe expr`
|
||||||
|
|
||||||
> Joins expressions with a '.' (dot) separator.
|
> Joins expressions with a '.' (dot) separator. If list of expressions contains
|
||||||
> If list of expressions contains a string it will
|
> a string it will convert to the raw.
|
||||||
> convert to the raw.
|
|
||||||
>
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (pipe ["vim" "opt" "line"]);
|
> toLua (pipe ["vim" "opt" "line"]);
|
||||||
> # vim.opt.line
|
> # vim.opt.line
|
||||||
> ```
|
> ```
|
||||||
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (pipe ["luasnip" (call0 "setup")]);
|
> toLua (pipe ["luasnip" (call0 "setup")]);
|
||||||
> # luasnip.setup()
|
> # luasnip.setup()
|
||||||
|
@ -128,8 +128,8 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
|
|
||||||
`spaceBetween expr`
|
`spaceBetween expr`
|
||||||
|
|
||||||
> Join expressions with a space separator. You can use it to separate
|
> Join expressions with a space separator. You can use it to separate all
|
||||||
> all expressions in the Lua configuration.
|
> expressions in the Lua configuration.
|
||||||
>
|
>
|
||||||
> This is alias for `join " "`
|
> This is alias for `join " "`
|
||||||
>
|
>
|
||||||
|
@ -145,13 +145,14 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
|
|
||||||
> Util to call a Lua function with arguments.
|
> Util to call a Lua function with arguments.
|
||||||
>
|
>
|
||||||
> If `args` is list that each list item render as function argument.
|
> If `args` is list that each list item render as function argument. Otherwise,
|
||||||
> Otherwise, it will be rendered as first argument.
|
> it will be rendered as first argument.
|
||||||
>
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (call "setup" [ "foo" "bar" ]);
|
> toLua (call "setup" [ "foo" "bar" ]);
|
||||||
> # setup("foo", "bar")
|
> # setup("foo", "bar")
|
||||||
> ```
|
> ```
|
||||||
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (call "setup" { foo = "bar" });
|
> toLua (call "setup" { foo = "bar" });
|
||||||
> # or: 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`
|
`call1 fnName arg`
|
||||||
|
|
||||||
> Call a Lua function with one argument.
|
> Call a Lua function with one argument. This is useful when a Lua function has
|
||||||
> This is useful when a Lua function has only one argument of type list,
|
> only one argument of type list, and we want to avoid using an inner list.
|
||||||
> and we want to avoid using an inner list.
|
|
||||||
>
|
>
|
||||||
> This is alias for `call fnName [arg]`.
|
> 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`.
|
> Call a require function to import module with `name`.
|
||||||
>
|
>
|
||||||
> This is alias for `call "require" name`.
|
> This is alias for `call "require" name`.
|
||||||
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (require "luasnip")
|
> toLua (require "luasnip")
|
||||||
> # require("luasnip")
|
> # require("luasnip")
|
||||||
|
@ -233,6 +234,7 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
> Useful aliases:
|
> Useful aliases:
|
||||||
|
>
|
||||||
> ```
|
> ```
|
||||||
> lset = l: r: local (set l r);
|
> lset = l: r: local (set l r);
|
||||||
> ```
|
> ```
|
||||||
|
@ -247,6 +249,7 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
> Useful aliases:
|
> Useful aliases:
|
||||||
|
>
|
||||||
> ```
|
> ```
|
||||||
> func0 = fnName: func fnName [ ];
|
> func0 = fnName: func fnName [ ];
|
||||||
>
|
>
|
||||||
|
@ -256,8 +259,8 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
|
|
||||||
`lambda params body`
|
`lambda params body`
|
||||||
|
|
||||||
> Define a lambda function with specific parameters and body.
|
> Define a lambda function with specific parameters and body. Useful to use Lua
|
||||||
> Useful to use Lua function inline.
|
> function inline.
|
||||||
>
|
>
|
||||||
> ```nix
|
> ```nix
|
||||||
> toLua (call "setup" {
|
> toLua (call "setup" {
|
||||||
|
@ -267,6 +270,7 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
> Useful aliases:
|
> Useful aliases:
|
||||||
|
>
|
||||||
> ```
|
> ```
|
||||||
> lambda0 = lambda [ ];
|
> lambda0 = lambda [ ];
|
||||||
> ```
|
> ```
|
||||||
|
@ -295,6 +299,7 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
> ```
|
> ```
|
||||||
>
|
>
|
||||||
> Useful aliases:
|
> Useful aliases:
|
||||||
|
>
|
||||||
> ```
|
> ```
|
||||||
> return_void = return null;
|
> return_void = return null;
|
||||||
> ```
|
> ```
|
||||||
|
@ -317,6 +322,21 @@ Add `nix2lua` as input to your `flake.nix`
|
||||||
> # if (10 == 10) print("yes") end
|
> # 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
|
# License
|
||||||
|
|
||||||
GNU General Public License v3.0 or later
|
GNU General Public License v3.0 or later
|
||||||
|
|
25
lib.nix
25
lib.nix
|
@ -127,7 +127,9 @@ let
|
||||||
if isJoin expr && expr.sep == "." then expr
|
if isJoin expr && expr.sep == "." then expr
|
||||||
else if isRaw expr then expr
|
else if isRaw expr then expr
|
||||||
else raw (validString 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]
|
# Type: validBlockBody :: a -> [b]
|
||||||
validBlockBody = body:
|
validBlockBody = body:
|
||||||
|
@ -170,6 +172,8 @@ let
|
||||||
]));
|
]));
|
||||||
if' = condition: trueBody: ifelse condition trueBody [ ];
|
if' = condition: trueBody: ifelse condition trueBody [ ];
|
||||||
|
|
||||||
|
do = body: spaceBetween [ kw_do ] ++ body ++ [ kw_end ];
|
||||||
|
|
||||||
isLuaNil = expr: getType expr == "nil";
|
isLuaNil = expr: getType expr == "nil";
|
||||||
LuaNil = { _type = "nil"; };
|
LuaNil = { _type = "nil"; };
|
||||||
|
|
||||||
|
@ -228,10 +232,19 @@ in
|
||||||
inherit toLua;
|
inherit toLua;
|
||||||
|
|
||||||
inherit LuaNil;
|
inherit LuaNil;
|
||||||
inherit raw join concat spaceBetween;
|
|
||||||
|
inherit raw;
|
||||||
|
var = raw;
|
||||||
|
vars = map raw;
|
||||||
|
|
||||||
|
inherit join concat spaceBetween;
|
||||||
inherit pipe pipe1;
|
inherit pipe pipe1;
|
||||||
|
|
||||||
inherit namedField local;
|
inherit local;
|
||||||
|
|
||||||
|
inherit namedField;
|
||||||
|
nf = namedField;
|
||||||
|
|
||||||
inherit ifelse if';
|
inherit ifelse if';
|
||||||
inherit set;
|
inherit set;
|
||||||
lset = l: r: local (set l r);
|
lset = l: r: local (set l r);
|
||||||
|
@ -249,8 +262,6 @@ in
|
||||||
|
|
||||||
inherit return return_void;
|
inherit return return_void;
|
||||||
|
|
||||||
# useful aliases
|
inherit do;
|
||||||
var = raw;
|
scope = do;
|
||||||
vars = map raw;
|
|
||||||
nf = namedField;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,6 +193,10 @@ with nix2lua; pkgs.lib.runTests {
|
||||||
expr = toLua (lset "parser_config.d2" { });
|
expr = toLua (lset "parser_config.d2" { });
|
||||||
expected = "local 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" = {
|
"test returns all operations" = {
|
||||||
expr = toLua (spaceBetween [
|
expr = toLua (spaceBetween [
|
||||||
(set "a" (eq (mul (add 1 2) (sub 2 1)) 3))
|
(set "a" (eq (mul (add 1 2) (sub 2 1)) 3))
|
||||||
|
|
Loading…
Reference in a new issue