mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 08:35:43 +02:00
lib/lua: only quote table keys when needed
This commit is contained in:
parent
01cf43dbaa
commit
00ce71f51a
2 changed files with 25 additions and 13 deletions
|
@ -47,16 +47,18 @@ rec {
|
||||||
mapAttrsToList (
|
mapAttrsToList (
|
||||||
n: v:
|
n: v:
|
||||||
let
|
let
|
||||||
|
keyString =
|
||||||
|
if n == "__emptyString" then
|
||||||
|
"['']"
|
||||||
|
else if hasPrefix "__rawKey__" n then
|
||||||
|
"[${removePrefix "__rawKey__" n}]"
|
||||||
|
else if isIdentifier n then
|
||||||
|
n
|
||||||
|
else
|
||||||
|
"[${toLuaObject n}]";
|
||||||
valueString = toLuaObject v;
|
valueString = toLuaObject v;
|
||||||
in
|
in
|
||||||
if hasPrefix "__unkeyed" n then
|
if hasPrefix "__unkeyed" n then valueString else "${keyString} = ${valueString}"
|
||||||
valueString
|
|
||||||
else if hasPrefix "__rawKey__" n then
|
|
||||||
''[${removePrefix "__rawKey__" n}] = '' + valueString
|
|
||||||
else if n == "__emptyString" then
|
|
||||||
"[''] = " + valueString
|
|
||||||
else
|
|
||||||
"[${toLuaObject n}] = " + valueString
|
|
||||||
) (filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args)
|
) (filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args)
|
||||||
))
|
))
|
||||||
+ "}"
|
+ "}"
|
||||||
|
|
|
@ -55,7 +55,7 @@ let
|
||||||
3
|
3
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
expected = ''{["foo"] = "bar",["qux"] = {1,2,3}}'';
|
expected = ''{foo = "bar",qux = {1,2,3}}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testToLuaObjectRawLua = {
|
testToLuaObjectRawLua = {
|
||||||
|
@ -68,7 +68,7 @@ let
|
||||||
"__unkeyed...." = "foo";
|
"__unkeyed...." = "foo";
|
||||||
bar = "baz";
|
bar = "baz";
|
||||||
};
|
};
|
||||||
expected = ''{"foo",["bar"] = "baz"}'';
|
expected = ''{"foo",bar = "baz"}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testToLuaObjectNestedAttrs = {
|
testToLuaObjectNestedAttrs = {
|
||||||
|
@ -81,7 +81,7 @@ let
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
expected = ''{["a"] = {["b"] = 1,["c"] = 2,["d"] = {["e"] = 3}}}'';
|
expected = ''{a = {b = 1,c = 2,d = {e = 3}}}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testToLuaObjectNestedList = {
|
testToLuaObjectNestedList = {
|
||||||
|
@ -109,7 +109,7 @@ let
|
||||||
d = false;
|
d = false;
|
||||||
e = null;
|
e = null;
|
||||||
};
|
};
|
||||||
expected = ''{["a"] = 1.000000,["b"] = 2,["c"] = true,["d"] = false}'';
|
expected = ''{a = 1.000000,b = 2,c = true,d = false}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testToLuaObjectNilPrim = {
|
testToLuaObjectNilPrim = {
|
||||||
|
@ -150,7 +150,17 @@ let
|
||||||
g = helpers.emptyTable;
|
g = helpers.emptyTable;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
expected = ''{["c"] = { },["d"] = {["g"] = { }}}'';
|
expected = ''{c = { },d = {g = { }}}'';
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaObjectQuotedKeys = {
|
||||||
|
expr = helpers.toLuaObject {
|
||||||
|
"1_a" = "a";
|
||||||
|
_b = "b";
|
||||||
|
c = "c";
|
||||||
|
d-d = "d";
|
||||||
|
};
|
||||||
|
expected = ''{["1_a"] = "a",_b = "b",c = "c",["d-d"] = "d"}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testIsLuaKeyword = {
|
testIsLuaKeyword = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue