Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't move trailing comments in let anymore #270

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
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
Loading