Merge pull request #11100 from NixOS/pretty-print-idempotent

Pretty print idempotently
This commit is contained in:
Eelco Dolstra 2024-07-17 21:35:27 +02:00 committed by GitHub
commit b23da1ceca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 69 additions and 0 deletions

View File

@ -299,6 +299,9 @@ private:
output << ANSI_NORMAL;
}
/**
* @note This may force items.
*/
bool shouldPrettyPrintAttrs(AttrVec & v)
{
if (!options.shouldPrettyPrint() || v.empty()) {
@ -315,6 +318,9 @@ private:
return true;
}
// It is ok to force the item(s) here, because they will be printed anyway.
state.forceValue(*item, item->determinePos(noPos));
// Pretty-print single-item attrsets only if they contain nested
// structures.
auto itemType = item->type();
@ -371,6 +377,9 @@ private:
}
}
/**
* @note This may force items.
*/
bool shouldPrettyPrintList(std::span<Value * const> list)
{
if (!options.shouldPrettyPrint() || list.empty()) {
@ -387,6 +396,9 @@ private:
return true;
}
// It is ok to force the item(s) here, because they will be printed anyway.
state.forceValue(*item, item->determinePos(noPos));
// Pretty-print single-item lists only if they contain nested
// structures.
auto itemType = item->type();

View File

@ -0,0 +1,29 @@
Nix <nix version>
Type :? for help.
Added <number omitted> variables.
{ homepage = "https://example.com"; }
{ homepage = "https://example.com"; }
{
layerOne = { ... };
}
{
layerOne = { ... };
}
[ "https://example.com" ]
[ "https://example.com" ]
[
[ ... ]
]
[
[ ... ]
]

View File

@ -0,0 +1,9 @@
:l pretty-print-idempotent.nix
oneDeep
oneDeep
twoDeep
twoDeep
oneDeepList
oneDeepList
twoDeepList
twoDeepList

View File

@ -0,0 +1,19 @@
{
oneDeep = {
homepage = "https://" + "example.com";
};
twoDeep = {
layerOne = {
homepage = "https://" + "example.com";
};
};
oneDeepList = [
("https://" + "example.com")
];
twoDeepList = [
[
("https://" + "example.com")
]
];
}