Skip to content

Commit

Permalink
Don't move trailing comments in let anymore
Browse files Browse the repository at this point in the history
While this was initially introduced for good reasons, as can also be
seen in the diff, that code was written with documentation comments in
mind and not code comments. As per the bug report, the downsides were
deemed to high to keep it in

Fixes NixOS#259
piegamesde committed Dec 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 91939f4 commit ebb3b25
Showing 7 changed files with 10 additions and 28 deletions.
22 changes: 2 additions & 20 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
@@ -632,31 +632,13 @@ instance Pretty Expression where
convertTrailing Nothing = []
convertTrailing (Just (TrailingComment t)) = [LineComment (" " <> t)]

-- Extract detached comments at the bottom.
-- This uses a custom variant of span/spanJust/spanMaybe.
-- Note that this is a foldr which walks from the bottom, but the lists
-- are constructed in a way that they end up correct again.
(binderComments, bindersWithoutComments) =
foldr
( \item (start, rest) -> case item of
(Comments inner)
| null rest ->
-- Only move all non-empty-line trivia below the `in`
let (comments, el) = break (== EmptyLine) (reverse inner)
in (reverse comments : start, Comments (reverse el) : rest)
_ -> (start, item : rest)
)
([], [])
(unItems binders)

letPart = group $ pretty let_ <> hardline <> letBody
letBody = nest $ prettyItems (Items bindersWithoutComments)
letBody = nest $ prettyItems binders
inPart =
group $
pretty in_
<> hardline
-- Take our trailing and inject it between `in` and body
<> pretty (concat binderComments ++ preTrivia ++ convertTrailing trailComment)
<> pretty (preTrivia ++ convertTrailing trailComment)
<> pretty expr
pretty (Assert assert cond semicolon expr) =
group $
2 changes: 1 addition & 1 deletion test/diff/comment/out-pure.nix
Original file line number Diff line number Diff line change
@@ -146,8 +146,8 @@
#6

d = 1;
#7
in
#7
d
)

2 changes: 1 addition & 1 deletion test/diff/comment/out.nix
Original file line number Diff line number Diff line change
@@ -146,8 +146,8 @@
#6

d = 1;
#7
in
#7
d
)

2 changes: 1 addition & 1 deletion test/diff/idioms_lib_3/out-pure.nix
Original file line number Diff line number Diff line change
@@ -148,8 +148,8 @@ rec {
[${mkSectionName sectName}]
''
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
# map input to ini sections
in
# map input to ini sections
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;

# Generate an INI-style config file from an attrset
2 changes: 1 addition & 1 deletion test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
@@ -151,8 +151,8 @@ rec {
[${mkSectionName sectName}]
''
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
# map input to ini sections
in
# map input to ini sections
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;

# Generate an INI-style config file from an attrset
4 changes: 2 additions & 2 deletions test/diff/let_in/out-pure.nix
Original file line number Diff line number Diff line change
@@ -65,9 +65,9 @@ let
let
b = 0;

# foo
# bar
in
# foo
# bar
# baz
# qux
null;
4 changes: 2 additions & 2 deletions test/diff/let_in/out.nix
Original file line number Diff line number Diff line change
@@ -65,9 +65,9 @@ let
let
b = 0;

# foo
# bar
in
# foo
# bar
# baz
# qux
null;

0 comments on commit ebb3b25

Please sign in to comment.