Skip to content

Commit

Permalink
Always preserve expanded attribute sets
Browse files Browse the repository at this point in the history
This changes the formatting to take into account whether attribute sets
are expanded onto multiple lines already, and if so, preserves that.

This is desirable because if a user writes an attribute set over
multiple lines, there's most likely an intention to add more attributes,
which would become harder when formatted.

An example of this is module system options, which usually start out
with just a type (or even no attributes), but usually get more
attributes later. This would've been contracted onto a single line
before this change:

    options.foo = lib.mkOption {
      type = types.str;
    };

This change conforms to the standard due to this line:

> The formatter may take the input formatting into account in some cases in order to preserve multi-line syntax elements (which would otherwise have been contracted by the rules).
  • Loading branch information
infinisil committed Aug 8, 2024
1 parent aff0520 commit 23b4c1f
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ instance Pretty Binder where
prettySet :: Bool -> (Maybe Leaf, Leaf, Items Binder, Leaf) -> Doc
-- Empty attribute set
prettySet _ (krec, paropen@(LoneAnn _), Items [], parclose@Ann{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 sourceLine paropen /= sourceLine parclose 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}, binders, parclose) =
Expand All @@ -168,7 +171,12 @@ prettySet wide (krec, paropen@Ann{trailComment = post}, binders, parclose) =
<> surroundWith sep (nest $ pretty post <> prettyItems binders)
<> pretty parclose
where
sep = if wide && not (null (unItems binders)) then hardline else line
sep =
if wide && not (null (unItems binders))
-- If the braces are on different lines, keep them like that
|| sourceLine paropen /= sourceLine parclose
then hardline
else line

prettyTermWide :: Term -> Doc
prettyTermWide (Set krec paropen items parclose) = prettySet True (krec, paropen, items, parclose)
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
2 changes: 2 additions & 0 deletions test/diff/attr_set/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{a=1;
}

{
}
{

}
Expand Down
1 change: 1 addition & 0 deletions test/diff/attr_set/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{ a = 1; }
{ a = 1; }

{ }
{

}
Expand Down
6 changes: 5 additions & 1 deletion test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
# 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 23b4c1f

Please sign in to comment.