From 7071b1a6f559d394911f1858bf5985a336bb944d Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 14 May 2024 23:25:01 +0200 Subject: [PATCH] Only move non-empty lines below the `in` --- src/Nixfmt/Pretty.hs | 6 +++++- test/diff/idioms_lib_3/out.nix | 4 ++-- test/diff/idioms_lib_4/out.nix | 3 ++- test/diff/idioms_lib_5/out.nix | 2 +- test/diff/idioms_nixos_1/out.nix | 1 + test/diff/idioms_nixos_2/out.nix | 6 +++--- test/diff/idioms_pkgs_3/out.nix | 1 + test/diff/idioms_pkgs_4/out.nix | 1 + test/diff/idioms_pkgs_5/out.nix | 5 +++-- test/diff/let_in/in.nix | 11 +++++++++++ test/diff/let_in/out.nix | 12 ++++++++++++ test/diff/monsters_5/out.nix | 1 + 12 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 67ff37c8..bd4fd480 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -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) ) ([], []) diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index aec21686..1115ae8b 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -19,8 +19,8 @@ let libAttr = lib.attrsets; inherit (lib) isFunction; -in +in rec { ## -- HELPER FUNCTIONS & DEFAULTS -- @@ -529,8 +529,8 @@ rec { ) x ) ); - in + in '' diff --git a/test/diff/idioms_lib_4/out.nix b/test/diff/idioms_lib_4/out.nix index 2ed68217..8ff78dae 100644 --- a/test/diff/idioms_lib_4/out.nix +++ b/test/diff/idioms_lib_4/out.nix @@ -31,6 +31,7 @@ let assert type.check value; setType type.name ({ inherit name; } // value) ); + in rec { @@ -887,8 +888,8 @@ rec { else abis.unknown; }; - in + in mkSystem parsed; mkSystemFromString = diff --git a/test/diff/idioms_lib_5/out.nix b/test/diff/idioms_lib_5/out.nix index af81eea0..ee898df8 100644 --- a/test/diff/idioms_lib_5/out.nix +++ b/test/diff/idioms_lib_5/out.nix @@ -606,8 +606,8 @@ let .${validity.valid}; }; -in +in { inherit assertValidity commonMeta; } diff --git a/test/diff/idioms_nixos_1/out.nix b/test/diff/idioms_nixos_1/out.nix index 8fc22d24..f2bcf7ff 100644 --- a/test/diff/idioms_nixos_1/out.nix +++ b/test/diff/idioms_nixos_1/out.nix @@ -16,6 +16,7 @@ let kernelModulesConf = pkgs.writeText "nixos.conf" '' ${concatStringsSep "\n" config.boot.kernelModules} ''; + in { diff --git a/test/diff/idioms_nixos_2/out.nix b/test/diff/idioms_nixos_2/out.nix index 6553191d..e7d3e5d2 100644 --- a/test/diff/idioms_nixos_2/out.nix +++ b/test/diff/idioms_nixos_2/out.nix @@ -67,8 +67,8 @@ let ''; inherit (config.system) stateVersion; -in +in { imports = [ @@ -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. @@ -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" ]; diff --git a/test/diff/idioms_pkgs_3/out.nix b/test/diff/idioms_pkgs_3/out.nix index 235289f5..066bc56e 100644 --- a/test/diff/idioms_pkgs_3/out.nix +++ b/test/diff/idioms_pkgs_3/out.nix @@ -228,6 +228,7 @@ let '') defaultPrefs ) ); + in buildStdenv.mkDerivation ({ diff --git a/test/diff/idioms_pkgs_4/out.nix b/test/diff/idioms_pkgs_4/out.nix index 7e0b9772..a4ed06e4 100644 --- a/test/diff/idioms_pkgs_4/out.nix +++ b/test/diff/idioms_pkgs_4/out.nix @@ -145,6 +145,7 @@ let config ; }; + in [ diff --git a/test/diff/idioms_pkgs_5/out.nix b/test/diff/idioms_pkgs_5/out.nix index 2bdedb35..fc02b21a 100644 --- a/test/diff/idioms_pkgs_5/out.nix +++ b/test/diff/idioms_pkgs_5/out.nix @@ -90,8 +90,8 @@ let ); finalPackage = mkDerivationSimple overrideAttrs args; - in + in finalPackage; #makeDerivationExtensibleConst = attrs: makeDerivationExtensible (_: attrs); @@ -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 ( @@ -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 diff --git a/test/diff/let_in/in.nix b/test/diff/let_in/in.nix index 6f80d6f7..41951216 100644 --- a/test/diff/let_in/in.nix +++ b/test/diff/let_in/in.nix @@ -31,6 +31,17 @@ let 2 ]; + a = + let + b = 0; + + + # foo + # bar + in # baz + # qux + null; + in diff --git a/test/diff/let_in/out.nix b/test/diff/let_in/out.nix index 2ad06c03..6870e5b3 100644 --- a/test/diff/let_in/out.nix +++ b/test/diff/let_in/out.nix @@ -60,6 +60,18 @@ let 1 2 ]; + + a = + let + b = 0; + + in + # foo + # bar + # baz + # qux + null; + in a diff --git a/test/diff/monsters_5/out.nix b/test/diff/monsters_5/out.nix index d93c025e..f7a798a2 100644 --- a/test/diff/monsters_5/out.nix +++ b/test/diff/monsters_5/out.nix @@ -50,6 +50,7 @@ let '' ${concatStringsSep "\n" config.boot.kernelModules} ''; + in {