mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 16:39:00 +02:00
lib/to-lua: fix removing empties nested in lists
- Add options for removing empty/null list entries (default false) - Fix recursion into attrs that are themselves list entries Fixes #1804
This commit is contained in:
parent
87f50db84d
commit
9b25eaaa6f
2 changed files with 125 additions and 11 deletions
|
@ -84,9 +84,21 @@ rec {
|
||||||
*/
|
*/
|
||||||
removeEmptyAttrValues ? true,
|
removeEmptyAttrValues ? true,
|
||||||
|
|
||||||
|
/**
|
||||||
|
If this option is true, lists like [ { } [ ] "" ]
|
||||||
|
will render as { "" }
|
||||||
|
*/
|
||||||
|
removeEmptyListEntries ? false,
|
||||||
|
|
||||||
|
/**
|
||||||
|
If this option is true, lists like [ null "" ]
|
||||||
|
will render as { "" }
|
||||||
|
*/
|
||||||
|
removeNullListEntries ? false,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
If this option is true, attrsets like { a.__empty = null; }
|
If this option is true, attrsets like { a.__empty = null; }
|
||||||
will render as { ["a"] = { } }, ignoring removeEmptyAttrValues and removeNullAttrValues.
|
will render as { ["a"] = { } }, ignoring other filters such as removeEmptyAttrValues.
|
||||||
*/
|
*/
|
||||||
allowExplicitEmpty ? true,
|
allowExplicitEmpty ? true,
|
||||||
|
|
||||||
|
@ -111,7 +123,12 @@ rec {
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
# If any of these are options are set, we need to preprocess the value
|
# If any of these are options are set, we need to preprocess the value
|
||||||
needsPreprocessing = removeNullAttrValues || removeEmptyAttrValues || allowExplicitEmpty;
|
needsPreprocessing =
|
||||||
|
allowExplicitEmpty
|
||||||
|
|| removeEmptyAttrValues
|
||||||
|
|| removeNullAttrValues
|
||||||
|
|| removeEmptyListEntries
|
||||||
|
|| removeNullListEntries;
|
||||||
|
|
||||||
# Slight optimization: only preprocess if we actually need to
|
# Slight optimization: only preprocess if we actually need to
|
||||||
preprocessValue = value: if needsPreprocessing then removeEmptiesRecursive value else value;
|
preprocessValue = value: if needsPreprocessing then removeEmptiesRecursive value else value;
|
||||||
|
@ -125,23 +142,30 @@ rec {
|
||||||
else if allowRawValues && value ? __raw then
|
else if allowRawValues && value ? __raw then
|
||||||
value
|
value
|
||||||
else if isList value then
|
else if isList value then
|
||||||
map removeEmptiesRecursive value
|
let
|
||||||
|
needsFiltering = removeNullListEntries || removeEmptyListEntries;
|
||||||
|
fn =
|
||||||
|
v: (removeNullListEntries -> (v != null)) && (removeEmptyListEntries -> (v != [ ] && v != { }));
|
||||||
|
v' = map removeEmptiesRecursive value;
|
||||||
|
in
|
||||||
|
if needsFiltering then filter fn v' else v'
|
||||||
else if isAttrs value then
|
else if isAttrs value then
|
||||||
concatMapAttrs (
|
concatMapAttrs (
|
||||||
n: v:
|
n: v:
|
||||||
|
let
|
||||||
|
v' = removeEmptiesRecursive v;
|
||||||
|
in
|
||||||
if removeNullAttrValues && v == null then
|
if removeNullAttrValues && v == null then
|
||||||
{ }
|
{ }
|
||||||
else if removeEmptyAttrValues && (v == [ ] || v == { }) then
|
|
||||||
{ }
|
|
||||||
else if allowExplicitEmpty && v ? __empty then
|
else if allowExplicitEmpty && v ? __empty then
|
||||||
{ ${n} = { }; }
|
{ ${n} = { }; }
|
||||||
else if isAttrs v then
|
# Optimisation: check if v is empty before evaluating v'
|
||||||
let
|
else if removeEmptyAttrValues && (v == [ ] || v == { }) then
|
||||||
v' = removeEmptiesRecursive v;
|
{ }
|
||||||
in
|
else if removeEmptyAttrValues && (v' == [ ] || v' == { }) then
|
||||||
if v' == { } then { } else { ${n} = v'; }
|
{ }
|
||||||
else
|
else
|
||||||
{ ${n} = v; }
|
{ ${n} = v'; }
|
||||||
) value
|
) value
|
||||||
else
|
else
|
||||||
value;
|
value;
|
||||||
|
|
|
@ -137,6 +137,55 @@ let
|
||||||
expected = ''{ }'';
|
expected = ''{ }'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testToLuaObjectAttrListFilters = {
|
||||||
|
expr = helpers.toLuaObject {
|
||||||
|
a = null;
|
||||||
|
b = { };
|
||||||
|
c = [ ];
|
||||||
|
d = {
|
||||||
|
e = null;
|
||||||
|
f = { };
|
||||||
|
};
|
||||||
|
g = [
|
||||||
|
{
|
||||||
|
x = null;
|
||||||
|
y = null;
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
x = { };
|
||||||
|
y = [ ];
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
__unkeyed-1 = [
|
||||||
|
{
|
||||||
|
x = null;
|
||||||
|
y = null;
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
x = { };
|
||||||
|
y = [ ];
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
[
|
||||||
|
{
|
||||||
|
x = null;
|
||||||
|
y = null;
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
x = { };
|
||||||
|
y = [ ];
|
||||||
|
z = null;
|
||||||
|
}
|
||||||
|
]
|
||||||
|
];
|
||||||
|
};
|
||||||
|
expected = ''{ { { }, { }, { { }, { } } }, g = { { }, { } } }'';
|
||||||
|
};
|
||||||
|
|
||||||
testToLuaObjectEmptyTable = {
|
testToLuaObjectEmptyTable = {
|
||||||
expr = helpers.toLuaObject {
|
expr = helpers.toLuaObject {
|
||||||
a = null;
|
a = null;
|
||||||
|
@ -153,6 +202,47 @@ let
|
||||||
expected = ''{ c = { }, d = { g = { } } }'';
|
expected = ''{ c = { }, d = { g = { } } }'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testToLuaObjectEmptyListEntries = {
|
||||||
|
expr = helpers.lua.toLua' { removeEmptyListEntries = true; } [
|
||||||
|
{ }
|
||||||
|
[ ]
|
||||||
|
[ { } ]
|
||||||
|
null
|
||||||
|
1
|
||||||
|
2
|
||||||
|
[
|
||||||
|
null
|
||||||
|
3
|
||||||
|
4
|
||||||
|
{ }
|
||||||
|
[ ]
|
||||||
|
[ { } ]
|
||||||
|
]
|
||||||
|
5
|
||||||
|
];
|
||||||
|
expected = "{ nil, 1, 2, { nil, 3, 4 }, 5 }";
|
||||||
|
};
|
||||||
|
|
||||||
|
testToLuaObjectNullListEntries = {
|
||||||
|
expr = helpers.lua.toLua' { removeNullListEntries = true; } [
|
||||||
|
null
|
||||||
|
1
|
||||||
|
2
|
||||||
|
[
|
||||||
|
null
|
||||||
|
3
|
||||||
|
4
|
||||||
|
[
|
||||||
|
null
|
||||||
|
5
|
||||||
|
6
|
||||||
|
]
|
||||||
|
]
|
||||||
|
7
|
||||||
|
];
|
||||||
|
expected = "{ 1, 2, { 3, 4, { 5, 6 } }, 7 }";
|
||||||
|
};
|
||||||
|
|
||||||
testIsLuaKeyword = {
|
testIsLuaKeyword = {
|
||||||
expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isKeyword) luaNames;
|
expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isKeyword) luaNames;
|
||||||
expected = {
|
expected = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue