Skip to content

Commit 8d4bd69

Browse files
authored
Merge pull request #270 from piegamesde/let-in-comments
Don't move trailing comments in let anymore
2 parents a463903 + ebb3b25 commit 8d4bd69

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
@@ -646,31 +646,13 @@ instance Pretty Expression where
646646
convertTrailing Nothing = []
647647
convertTrailing (Just (TrailingComment t)) = [LineComment (" " <> t)]
648648

649-
-- Extract detached comments at the bottom.
650-
-- This uses a custom variant of span/spanJust/spanMaybe.
651-
-- Note that this is a foldr which walks from the bottom, but the lists
652-
-- are constructed in a way that they end up correct again.
653-
(binderComments, bindersWithoutComments) =
654-
foldr
655-
( \item (start, rest) -> case item of
656-
(Comments inner)
657-
| null rest ->
658-
-- Only move all non-empty-line trivia below the `in`
659-
let (comments, el) = break (== EmptyLine) (reverse inner)
660-
in (reverse comments : start, Comments (reverse el) : rest)
661-
_ -> (start, item : rest)
662-
)
663-
([], [])
664-
(unItems binders)
665-
666649
letPart = group $ pretty let_ <> hardline <> letBody
667-
letBody = nest $ prettyItems (Items bindersWithoutComments)
650+
letBody = nest $ prettyItems binders
668651
inPart =
669652
group $
670653
pretty in_
671654
<> hardline
672-
-- Take our trailing and inject it between `in` and body
673-
<> pretty (concat binderComments ++ preTrivia ++ convertTrailing trailComment)
655+
<> pretty (preTrivia ++ convertTrailing trailComment)
674656
<> pretty expr
675657
pretty (Assert assert cond semicolon expr) =
676658
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)