From bfa12f0d3f1d74d693561e3b4d78dca43b4064c1 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 17 Apr 2024 12:12:50 +0300 Subject: [PATCH] add pipe, set, lset, require helpers --- lib.nix | 10 +++++++--- lib.test.nix | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib.nix b/lib.nix index 7dce83a..32ec9f2 100644 --- a/lib.nix +++ b/lib.nix @@ -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; } diff --git a/lib.test.nix b/lib.test.nix index 4611ce1..4cff866 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -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 = { }"; + }; }