neovim-flake/lib/lua.nix

103 lines
2.9 KiB
Nix
Raw Permalink Normal View History

# Helpers for converting values to lua
2023-11-06 10:33:38 +01:00
{lib}: let
2024-03-24 01:14:39 +01:00
inherit (builtins) hasAttr head throw typeOf isList isAttrs isBool isInt isString isPath isFloat toJSON;
inherit (lib.attrsets) mapAttrsToList filterAttrs;
inherit (lib.strings) concatStringsSep concatMapStringsSep stringToCharacters;
inherit (lib.trivial) boolToString;
2023-11-06 10:33:38 +01:00
in rec {
wrapLuaConfig = {
luaBefore ? "",
luaConfig,
luaAfter ? "",
}: ''
lua << EOF
${concatStringsSep "\n" [luaBefore luaConfig luaAfter]}
EOF
'';
# Convert a null value to lua's nil
nullString = value:
if value == null
then "nil"
else "'${value}'";
2023-06-05 20:37:12 +02:00
# convert an expression to lua
2023-06-04 16:36:01 +02:00
expToLua = exp:
2024-03-24 01:14:39 +01:00
if isList exp
2023-07-30 10:41:52 +02:00
then listToLuaTable exp # if list, convert to lua table
2024-03-24 01:14:39 +01:00
else if isAttrs exp
2023-07-30 10:41:52 +02:00
then attrsetToLuaTable exp # if attrs, convert to table
2024-03-24 01:14:39 +01:00
else if isBool exp
then boolToString exp # if bool, convert to string
else if isInt exp
then toString exp # if int, convert to string
2024-02-17 16:44:05 +01:00
else if exp == null
then "nil"
2024-03-24 01:14:39 +01:00
else (toJSON exp); # otherwise jsonify the value and print as is
2023-06-04 16:36:01 +02:00
2023-06-05 20:37:12 +02:00
# convert list to a lua table
2023-06-04 16:36:01 +02:00
listToLuaTable = list:
2024-03-24 01:14:39 +01:00
"{ " + (concatStringsSep ", " (map expToLua list)) + " }";
2023-06-05 20:37:12 +02:00
# convert attrset to a lua table
attrsetToLuaTable = attrset:
"{ "
+ (
2024-03-24 01:14:39 +01:00
concatStringsSep ", "
2023-06-04 16:36:01 +02:00
(
2024-03-24 01:14:39 +01:00
mapAttrsToList (
name: value:
2023-06-04 16:36:01 +02:00
name
+ " = "
+ (expToLua value)
)
attrset
2023-06-04 16:36:01 +02:00
)
)
+ " }";
2023-10-24 09:18:44 +02:00
# Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first
2024-03-24 01:14:39 +01:00
luaTable = items: ''{${concatStringsSep "," items}}'';
2023-11-06 10:33:38 +01:00
2024-04-27 16:01:37 +02:00
isLuaInline = object: (object._type or null) == "lua-inline";
2024-03-16 10:29:07 +01:00
2023-11-06 10:33:38 +01:00
toLuaObject = args:
2024-03-24 01:14:39 +01:00
if isAttrs args
2023-11-06 10:33:38 +01:00
then
2024-03-16 10:29:07 +01:00
if isLuaInline args
then args.expr
2023-11-06 10:33:38 +01:00
else if hasAttr "__empty" args
then "{ }"
else
"{"
+ (concatStringsSep ","
(mapAttrsToList
(n: v:
if head (stringToCharacters n) == "@"
then toLuaObject v
else "[${toLuaObject n}] = " + (toLuaObject v))
(filterAttrs
(
_: v:
(v != null) && (toLuaObject v != "{}")
)
args)))
+ "}"
2024-03-24 01:14:39 +01:00
else if isList args
2023-11-06 10:33:38 +01:00
then "{" + concatMapStringsSep "," toLuaObject args + "}"
2024-03-24 01:14:39 +01:00
else if isString args
2023-11-06 10:33:38 +01:00
then
# This should be enough!
2024-03-24 01:14:39 +01:00
toJSON args
else if isPath args
then toJSON (toString args)
else if isBool args
2023-11-06 10:33:38 +01:00
then "${boolToString args}"
2024-03-24 01:14:39 +01:00
else if isFloat args
2023-11-06 10:33:38 +01:00
then "${toString args}"
2024-03-24 01:14:39 +01:00
else if isInt args
2023-11-06 10:33:38 +01:00
then "${toString args}"
else if (args == null)
2023-11-06 10:33:38 +01:00
then "nil"
else throw "could not convert object of type `${typeOf args}` to lua object";
}