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 #259
  • Loading branch information
piegamesde committed Dec 2, 2024
1 parent 622ea8c commit 124eca6
Show file tree
Hide file tree
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
Expand Up @@ -646,31 +646,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 $
Expand Down
2 changes: 1 addition & 1 deletion test/diff/comment/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
#6

d = 1;
#7
in
#7
d
)

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

d = 1;
#7
in
#7
d
)

Expand Down
2 changes: 1 addition & 1 deletion test/diff/idioms_lib_3/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/diff/let_in/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ let
let
b = 0;

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

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

0 comments on commit 124eca6

Please sign in to comment.