Skip to content

Commit

Permalink
Merge pull request #270 from piegamesde/let-in-comments
Browse files Browse the repository at this point in the history
Don't move trailing comments in let anymore
  • Loading branch information
dasJ authored Jan 7, 2025
2 parents a463903 + ebb3b25 commit 8d4bd69
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 8d4bd69

Please sign in to comment.