Skip to content

Commit

Permalink
Always preserve expanded attribute sets
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Jul 19, 2024
1 parent 67408c7 commit feef8e8
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 24 deletions.
20 changes: 15 additions & 5 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,33 @@ prettySet
_
( krec,
paropen@Ann
{ preTrivia = [],
{ sourceLine = openLine,
preTrivia = [],
trailComment = Nothing
},
Items [],
parclose@Ann
{ preTrivia = []
{ sourceLine = closeLine,
preTrivia = []
}
) =
pretty (fmap (,hardspace) krec) <> pretty paropen <> hardspace <> pretty parclose
pretty (fmap (,hardspace) krec) <> pretty paropen <> sep <> pretty parclose
where
-- If the braces are on different lines, keep them like that
sep = if openLine /= closeLine then hardline else hardspace
-- Singleton sets are allowed to fit onto one line,
-- but apart from that always expand.
prettySet
wide
( krec,
paropen@Ann
{ trailComment = post
{ sourceLine = openLine,
trailComment = post
},
binders,
parclose
parclose@Ann
{ sourceLine = closeLine
}
) =
pretty (fmap (,hardspace) krec)
<> pretty (paropen{trailComment = Nothing})
Expand All @@ -189,6 +197,8 @@ prettySet
where
sep =
if wide && not (null (unItems binders))
-- If the braces are on different lines, keep them like that
|| openLine /= closeLine
then hardline
else line

Expand Down
18 changes: 13 additions & 5 deletions test/diff/apply/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,25 @@
name3 = function arg { asdf = 1; } { qwer = 12345; } argument;
}
{
name1 = function arg { asdf = 1; };
name1 = function arg {
asdf = 1;
};

name2 = function arg {
asdf = 1;
# multiline
} argument;

name3 = function arg {
asdf = 1;
# multiline
} { qwer = 12345; } argument;
name3 =
function arg
{
asdf = 1;
# multiline
}
{
qwer = 12345;
}
argument;
}
{
name4 = function arg { asdf = 1; } {
Expand Down
4 changes: 3 additions & 1 deletion test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# a
}
{ a = 1; }
{ a = 1; }
{
a = 1;
}

{

Expand Down
8 changes: 6 additions & 2 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,13 @@ rec {
if isAttrs value && !lib.isDerivation value then
lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
else if length path > 1 then
{ ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; }
{
${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value;
}
else
{ ${head path} = value; };
{
${head path} = value;
};
in
attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));

Expand Down
5 changes: 4 additions & 1 deletion test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ let
post_max_size = cfg.maxUploadSize;
memory_limit = cfg.maxUploadSize;
}
// cfg.phpOptions // optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; };
// cfg.phpOptions
// optionalAttrs cfg.caching.apcu {
"apc.enable_cli" = "1";
};

occ = pkgs.writeScriptBin "nextcloud-occ" ''
#! ${pkgs.runtimeShell}
Expand Down
4 changes: 3 additions & 1 deletion test/diff/idioms_pkgs_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
stdenv.mkDerivation rec {
pname = "test";
version = "0.0";
src = fetchFrom { url = "example/${version}"; };
src = fetchFrom {
url = "example/${version}";
};
meta = with lib; {
maintainers = with maintainers; [ someone ];
description = "something";
Expand Down
10 changes: 7 additions & 3 deletions test/diff/idioms_pkgs_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ in
# First build a stdenv based only on tools outside the store.
(prevStage: {
inherit config overlays;
stdenv = makeStdenv { inherit (prevStage) cc fetchurl; } // {
inherit (prevStage) fetchurl;
};
stdenv =
makeStdenv {
inherit (prevStage) cc fetchurl;
}
// {
inherit (prevStage) fetchurl;
};
})

# Using that, build a stdenv that adds the ‘xz’ command (which most systems
Expand Down
11 changes: 8 additions & 3 deletions test/diff/idioms_pkgs_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,14 @@ let
enableParallelChecking = attrs.enableParallelChecking or true;
enableParallelInstalling = attrs.enableParallelInstalling or true;
}
// optionalAttrs (
hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl
) { NIX_HARDENING_ENABLE = enabledHardeningOptions; }
//
optionalAttrs
(
hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl
)
{
NIX_HARDENING_ENABLE = enabledHardeningOptions;
}
//
optionalAttrs (stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform ? gcc.arch)
{
Expand Down
11 changes: 10 additions & 1 deletion test/diff/if_else/out.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
[
(if true then { version = "1.2.3"; } else { version = "3.2.1"; })
(
if true then
{
version = "1.2.3";
}
else
{
version = "3.2.1";
}
)
(
if true then
''
Expand Down
4 changes: 3 additions & 1 deletion test/diff/inherit/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
j
;
}
{ inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; }
{
inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
}
{ inherit b d; }
{
inherit
Expand Down
4 changes: 3 additions & 1 deletion test/diff/with/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
b = 2;
}
)
{ a = with b; with b; with b; 1; }
{
a = with b; with b; with b; 1;
}
{
binPath =
with pkgs;
Expand Down

0 comments on commit feef8e8

Please sign in to comment.