Skip to content

Commit

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

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

An example of this is build inputs, which usually start out with just a
single element, but usually get more elements later. This would've been
contracted onto a single line before this change:

    buildInputs = [
      libfoo
    ];

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 authored and piegamesde committed Aug 7, 2024
1 parent 7e1fa74 commit 18689b4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,19 @@ prettyTerm (Selection term selectors rest) =

-- Empty list
prettyTerm (List paropen@Ann{trailComment = Nothing} (Items []) parclose@Ann{preTrivia = []}) =
pretty paropen <> hardspace <> pretty parclose
pretty paropen <> sep <> pretty parclose
where
-- If the brackets are on different lines, keep them like that
sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace
-- General list
-- Always expand if len > 1
prettyTerm (List paropen@Ann{trailComment = post} items parclose) =
pretty (paropen{trailComment = Nothing})
<> surroundWith line (nest $ pretty post <> prettyItems items)
<> surroundWith sur (nest $ pretty post <> prettyItems items)
<> pretty parclose
where
-- If the brackets are on different lines, keep them like that
sur = if sourceLine paropen /= sourceLine parclose then hardline else line
prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose)
-- Parentheses
prettyTerm (Parenthesized paropen expr parclose@Ann{preTrivia = closePre}) =
Expand Down
4 changes: 3 additions & 1 deletion test/diff/idioms_pkgs_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ buildStdenv.mkDerivation ({

inherit src unpackPhase meta;

outputs = [ "out" ] ++ lib.optionals crashreporterSupport [ "symbols" ];
outputs = [
"out"
] ++ lib.optionals crashreporterSupport [ "symbols" ];

# Add another configure-build-profiling run before the final configure phase if we build with pgo
preConfigurePhases = lib.optionals pgoSupport [
Expand Down
8 changes: 6 additions & 2 deletions test/diff/idioms_pkgs_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ let
]
++ (
if system == "i686-cygwin" then
[ ../cygwin/rebase-i686.sh ]
[
../cygwin/rebase-i686.sh
]
else if system == "x86_64-cygwin" then
[ ../cygwin/rebase-x86_64.sh ]
[
../cygwin/rebase-x86_64.sh
]
else
[ ]
);
Expand Down
3 changes: 3 additions & 0 deletions test/diff/lists/in.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[
[]
[
]
[

]
Expand Down
2 changes: 2 additions & 0 deletions test/diff/lists/out-pure.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[
[ ]
[ ]
[

]
Expand Down
7 changes: 6 additions & 1 deletion test/diff/lists/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[
[ ]
[
]
[

]
Expand Down Expand Up @@ -46,7 +49,9 @@
]
[ 1 ]

[ 1 ]
[
1
]

[
b
Expand Down
4 changes: 3 additions & 1 deletion test/diff/operation/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@
zip
zlib
]
++ [ (if (lib.versionAtLeast version "103") then nss_latest else nss_esr) ]
++ [
(if (lib.versionAtLeast version "103") then nss_latest else nss_esr)
]
)

# Indentation with parenthesized multiline function call
Expand Down

0 comments on commit 18689b4

Please sign in to comment.