Skip to content

Commit ebb3b25

Browse files
committed
Don't move trailing comments in let anymore
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
1 parent 91939f4 commit ebb3b25

File tree

7 files changed

+10
-28
lines changed

7 files changed

+10
-28
lines changed

src/Nixfmt/Pretty.hs

+2-20
Original file line numberDiff line numberDiff line change
@@ -632,31 +632,13 @@ instance Pretty Expression where
632632
convertTrailing Nothing = []
633633
convertTrailing (Just (TrailingComment t)) = [LineComment (" " <> t)]
634634

635-
-- Extract detached comments at the bottom.
636-
-- This uses a custom variant of span/spanJust/spanMaybe.
637-
-- Note that this is a foldr which walks from the bottom, but the lists
638-
-- are constructed in a way that they end up correct again.
639-
(binderComments, bindersWithoutComments) =
640-
foldr
641-
( \item (start, rest) -> case item of
642-
(Comments inner)
643-
| null rest ->
644-
-- Only move all non-empty-line trivia below the `in`
645-
let (comments, el) = break (== EmptyLine) (reverse inner)
646-
in (reverse comments : start, Comments (reverse el) : rest)
647-
_ -> (start, item : rest)
648-
)
649-
([], [])
650-
(unItems binders)
651-
652635
letPart = group $ pretty let_ <> hardline <> letBody
653-
letBody = nest $ prettyItems (Items bindersWithoutComments)
636+
letBody = nest $ prettyItems binders
654637
inPart =
655638
group $
656639
pretty in_
657640
<> hardline
658-
-- Take our trailing and inject it between `in` and body
659-
<> pretty (concat binderComments ++ preTrivia ++ convertTrailing trailComment)
641+
<> pretty (preTrivia ++ convertTrailing trailComment)
660642
<> pretty expr
661643
pretty (Assert assert cond semicolon expr) =
662644
group $

test/diff/comment/out-pure.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@
146146
#6
147147

148148
d = 1;
149+
#7
149150
in
150-
#7
151151
d
152152
)
153153

test/diff/comment/out.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@
146146
#6
147147

148148
d = 1;
149+
#7
149150
in
150-
#7
151151
d
152152
)
153153

test/diff/idioms_lib_3/out-pure.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ rec {
148148
[${mkSectionName sectName}]
149149
''
150150
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
151+
# map input to ini sections
151152
in
152-
# map input to ini sections
153153
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
154154

155155
# Generate an INI-style config file from an attrset

test/diff/idioms_lib_3/out.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ rec {
151151
[${mkSectionName sectName}]
152152
''
153153
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
154+
# map input to ini sections
154155
in
155-
# map input to ini sections
156156
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
157157

158158
# Generate an INI-style config file from an attrset

test/diff/let_in/out-pure.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ let
6565
let
6666
b = 0;
6767

68+
# foo
69+
# bar
6870
in
69-
# foo
70-
# bar
7171
# baz
7272
# qux
7373
null;

test/diff/let_in/out.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ let
6565
let
6666
b = 0;
6767

68+
# foo
69+
# bar
6870
in
69-
# foo
70-
# bar
7171
# baz
7272
# qux
7373
null;

0 commit comments

Comments
 (0)