Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,24 @@ renderItems separator (Items items) = go items
go (item : rest) =
pretty item <> if null rest then mempty else separator <> go rest

hasOnlyComments :: Items a -> Bool
hasOnlyComments (Items xs) = not (null xs) && all isComment xs
where
isComment (Comments _) = True
isComment _ = False

-- Render a list given the separator between items
renderList :: Doc -> Ann Token -> Items Term -> Ann Token -> Doc
renderList itemSep paropen@Ann{trailComment = post} items parclose =
pretty (paropen{trailComment = Nothing})
<> surroundWith sur (nest $ pretty post <> renderItems itemSep items)
<> pretty parclose
where
-- If the brackets are on different lines, keep them like that
sur
| sourceLine paropen /= sourceLine parclose = hardline
| null $ unItems items = hardspace
| sourceLine paropen /= sourceLine parclose = hardline -- If the brackets are on different lines, keep them like that
| hasOnlyComments items = hardline -- If the list has only comments, use hardline to ensure idempotency. https://github.com/NixOS/nixfmt/issues/362
| hasTrivia paropen && null items = hardline -- even if the comment got associated with the opening bracket, keep the hardline
| null items = hardspace -- making sure we're not potentially adding extra newlines for empty lists
| otherwise = line

instance Pretty Trivia where
Expand Down Expand Up @@ -597,6 +604,11 @@ isAbsorbable (Set _ (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2
| line1 /= line2 = True
isAbsorbable (List (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2}))
| line1 /= line2 = True
-- Lists/sets with only comments are absorbable (https://github.com/NixOS/nixfmt/issues/362)
isAbsorbable (List paropen items _)
| hasTrivia paropen || hasOnlyComments items = True
isAbsorbable (Set _ paropen items _)
| hasTrivia paropen || hasOnlyComments items = True
isAbsorbable (Parenthesized (LoneAnn _) (Term t) _) = isAbsorbable t
isAbsorbable _ = False

Expand Down
55 changes: 55 additions & 0 deletions test/diff/attr_set/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,61 @@
++ baz # newline!
++ meow;

# https://github.com/NixOS/nixfmt/issues/362
foobar = [ /* foobar */ ];

foobar =
[
# foobar
];

foobar =
[ # foobar
];

foobar =
[ # foobar
];

foobar = [
# foobar
];

foobar2 = [ /* foobar */

];

foobar3 = [

/* foobar */ ];

foobar = { /* foobar */ };

foobar =
{
# foobar
};

foobar =
{ # foobar
};

foobar =
{ # foobar
};

foobar = {
# foobar
};

foobar2 = { /* foobar */

};

foobar3 = {

/* foobar */ };

environment.systemPackages =
# Include the PAM modules in the system path mostly for the manpages.
[ package ]
Expand Down
61 changes: 61 additions & 0 deletions test/diff/attr_set/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,67 @@
++ baz # newline!
++ meow;

# https://github.com/NixOS/nixfmt/issues/362
foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar2 = [
# foobar

];

foobar3 = [

# foobar
];

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar2 = {
# foobar

};

foobar3 = {

# foobar
};

environment.systemPackages =
# Include the PAM modules in the system path mostly for the manpages.
[ package ] ++ lib.optional config.users.ldap.enable pam_ldap;
Expand Down
61 changes: 61 additions & 0 deletions test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,67 @@
++ baz # newline!
++ meow;

# https://github.com/NixOS/nixfmt/issues/362
foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar = [
# foobar
];

foobar2 = [
# foobar

];

foobar3 = [

# foobar
];

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar = {
# foobar
};

foobar2 = {
# foobar

};

foobar3 = {

# foobar
};

environment.systemPackages =
# Include the PAM modules in the system path mostly for the manpages.
[ package ] ++ lib.optional config.users.ldap.enable pam_ldap;
Expand Down
9 changes: 9 additions & 0 deletions test/diff/lists/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@

]

[ /* foobar */ ]

[ # foobar
]

[
# foobar
]


[ [ multi line ] ]
[ [ [ singleton ] ] ]
Expand Down
12 changes: 12 additions & 0 deletions test/diff/lists/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@

]

[
# foobar
]

[
# foobar
]

[
# foobar
]

[
[
multi
Expand Down
12 changes: 12 additions & 0 deletions test/diff/lists/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@

]

[
# foobar
]

[
# foobar
]

[
# foobar
]

[
[
multi
Expand Down