libexpr/print: do not show elided nested items when there are none

When the configured maximum depth has been reached, attribute sets and lists
are printed with ellipsis to indicate the elision of nested items. Previously,
this happened even in case the structure being printed is empty, so that such
items do not in fact exist. This is confusing, so stop doing it.

Change-Id: I0016970dad3e42625e085dc896e6f476b21226c9
This commit is contained in:
alois31 2024-07-15 18:38:37 +02:00
parent b5da823138
commit 40c39aa5d2
Signed by untrusted user: alois31
GPG key ID: E0F59EA5E5216914
2 changed files with 15 additions and 6 deletions

View file

@ -281,7 +281,7 @@ private:
printDerivation(v); printDerivation(v);
} else if (seen && !v.attrs->empty() && !seen->insert(v.attrs).second) { } else if (seen && !v.attrs->empty() && !seen->insert(v.attrs).second) {
printRepeated(); printRepeated();
} else if (depth < options.maxDepth) { } else if (depth < options.maxDepth || v.attrs->empty()) {
increaseIndent(); increaseIndent();
output << "{"; output << "{";
@ -355,7 +355,7 @@ private:
return; return;
} }
if (depth < options.maxDepth) { if (depth < options.maxDepth || v.listSize() == 0) {
increaseIndent(); increaseIndent();
output << "["; output << "[";
auto listItems = v.listItems(); auto listItems = v.listItems();

View file

@ -193,6 +193,9 @@ TEST_F(ValuePrintingTests, vBlackhole)
TEST_F(ValuePrintingTests, depthAttrs) TEST_F(ValuePrintingTests, depthAttrs)
{ {
Value vZero;
vZero.mkInt(0);
Value vOne; Value vOne;
vOne.mkInt(1); vOne.mkInt(1);
@ -203,10 +206,16 @@ TEST_F(ValuePrintingTests, depthAttrs)
Value vAttrsEmpty; Value vAttrsEmpty;
vAttrsEmpty.mkAttrs(builderEmpty.finish()); vAttrsEmpty.mkAttrs(builderEmpty.finish());
BindingsBuilder builderNested(state, state.allocBindings(1));
builderNested.insert(state.symbols.create("zero"), &vZero);
Value vAttrsNested;
vAttrsNested.mkAttrs(builderNested.finish());
BindingsBuilder builder(state, state.allocBindings(10)); BindingsBuilder builder(state, state.allocBindings(10));
builder.insert(state.symbols.create("one"), &vOne); builder.insert(state.symbols.create("one"), &vOne);
builder.insert(state.symbols.create("two"), &vTwo); builder.insert(state.symbols.create("two"), &vTwo);
builder.insert(state.symbols.create("nested"), &vAttrsEmpty); builder.insert(state.symbols.create("empty"), &vAttrsEmpty);
builder.insert(state.symbols.create("nested"), &vAttrsNested);
Value vAttrs; Value vAttrs;
vAttrs.mkAttrs(builder.finish()); vAttrs.mkAttrs(builder.finish());
@ -220,9 +229,9 @@ TEST_F(ValuePrintingTests, depthAttrs)
vNested.mkAttrs(builder2.finish()); vNested.mkAttrs(builder2.finish());
test(vNested, "{ nested = { ... }; one = 1; two = 2; }", PrintOptions { .maxDepth = 1 }); test(vNested, "{ nested = { ... }; one = 1; two = 2; }", PrintOptions { .maxDepth = 1 });
test(vNested, "{ nested = { nested = { ... }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 2 }); test(vNested, "{ nested = { empty = { }; nested = { ... }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 2 });
test(vNested, "{ nested = { nested = { }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 3 }); test(vNested, "{ nested = { empty = { }; nested = { zero = 0; }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 3 });
test(vNested, "{ nested = { nested = { }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 4 }); test(vNested, "{ nested = { empty = { }; nested = { zero = 0; }; one = 1; two = 2; }; one = 1; two = 2; }", PrintOptions { .maxDepth = 4 });
} }
TEST_F(ValuePrintingTests, depthList) TEST_F(ValuePrintingTests, depthList)