Skip to content

Commit

Permalink
Only move non-empty lines below the in
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed May 14, 2024
1 parent 48e00a9 commit 7071b1a
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ instance Pretty Expression where
(binderComments, bindersWithoutComments) =
foldr
( \item (start, rest) -> case item of
(Comments inner) | null rest -> (inner : start, rest)
(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)
)
([], [])
Expand Down
4 changes: 2 additions & 2 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let
libAttr = lib.attrsets;

inherit (lib) isFunction;
in

in
rec {

## -- HELPER FUNCTIONS & DEFAULTS --
Expand Down Expand Up @@ -529,8 +529,8 @@ rec {
) x
)
);
in

in
''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Expand Down
3 changes: 2 additions & 1 deletion test/diff/idioms_lib_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let
assert type.check value;
setType type.name ({ inherit name; } // value)
);

in

rec {
Expand Down Expand Up @@ -887,8 +888,8 @@ rec {
else
abis.unknown;
};
in

in
mkSystem parsed;

mkSystemFromString =
Expand Down
2 changes: 1 addition & 1 deletion test/diff/idioms_lib_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ let
.${validity.valid};

};
in

in
{
inherit assertValidity commonMeta;
}
1 change: 1 addition & 0 deletions test/diff/idioms_nixos_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let
kernelModulesConf = pkgs.writeText "nixos.conf" ''
${concatStringsSep "\n" config.boot.kernelModules}
'';

in

{
Expand Down
6 changes: 3 additions & 3 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ let
'';

inherit (config.system) stateVersion;
in

in
{

imports = [
Expand Down Expand Up @@ -764,8 +764,8 @@ in
The package can be upgraded by explicitly declaring the service-option
`services.nextcloud.package`.
'';
in

in
(optional (cfg.poolConfig != null) ''
Using config.services.nextcloud.poolConfig is deprecated and will become unsupported in a future release.
Please migrate your configuration to config.services.nextcloud.poolSettings.
Expand Down Expand Up @@ -1019,8 +1019,8 @@ in
${toString i} --value="${toString v}"
'') ([ cfg.hostName ] ++ cfg.config.extraTrustedDomains)
);
in

in
{
wantedBy = [ "multi-user.target" ];
before = [ "phpfpm-nextcloud.service" ];
Expand Down
1 change: 1 addition & 0 deletions test/diff/idioms_pkgs_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ let
'') defaultPrefs
)
);

in

buildStdenv.mkDerivation ({
Expand Down
1 change: 1 addition & 0 deletions test/diff/idioms_pkgs_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ let
config
;
};

in

[
Expand Down
5 changes: 3 additions & 2 deletions test/diff/idioms_pkgs_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ let
);

finalPackage = mkDerivationSimple overrideAttrs args;
in

in
finalPackage;

#makeDerivationExtensibleConst = attrs: makeDerivationExtensible (_: attrs);
Expand Down Expand Up @@ -728,6 +728,7 @@ let
"The ‘env’ attribute set can only contain derivation, string, boolean or integer attributes. The ‘${n}’ attribute is of type ${builtins.typeOf v}.";
v
) env;

in

extendDerivation validity.handled (
Expand Down Expand Up @@ -786,8 +787,8 @@ let
# derivation (e.g., in assertions).
passthru
) (derivation (derivationArg // optionalAttrs envIsExportable checkedEnv));
in

in
fnOrAttrs:
if builtins.isFunction fnOrAttrs then
makeDerivationExtensible fnOrAttrs
Expand Down
11 changes: 11 additions & 0 deletions test/diff/let_in/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ let
2
];

a =
let
b = 0;


# foo
# bar
in # baz
# qux
null;

in


Expand Down
12 changes: 12 additions & 0 deletions test/diff/let_in/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ let
1
2
];

a =
let
b = 0;

in
# foo
# bar
# baz
# qux
null;

in

a
1 change: 1 addition & 0 deletions test/diff/monsters_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ let
''
${concatStringsSep "\n" config.boot.kernelModules}
'';

in

{
Expand Down

0 comments on commit 7071b1a

Please sign in to comment.