From e6bed2a55312bb242e5853bb75a248598cbc18f0 Mon Sep 17 00:00:00 2001 From: piegames Date: Wed, 17 May 2023 09:55:21 +0200 Subject: [PATCH] Don't absort `in` body anymore --- src/Nixfmt/Pretty.hs | 12 +----------- test/diff/idioms_lib_2/out.nix | 6 ++++-- test/diff/idioms_lib_3/out.nix | 30 ++++++++++++++++++++---------- test/diff/idioms_nixos_1/out.nix | 3 ++- test/diff/idioms_nixos_2/out.nix | 12 ++++++++---- test/diff/let_in/out.nix | 3 ++- test/diff/monsters_5/out.nix | 3 ++- 7 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 9b4c8e65..48d97a0f 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -208,16 +208,6 @@ absorb left right (Just level) x absorbSet :: Expression -> Doc absorbSet = absorb line mempty Nothing --- What is allowed to come on the same line as `in`? --- Absorbable terms like sets --- if, with, let -absorbIn :: Expression -> Doc -absorbIn (Term t) | isAbsorbable t = hardspace <> prettyTerm t <> hardspace -absorbIn x@(If _ _ _ _ _ _) = group x -absorbIn x@(With _ _ _ _) = group x -absorbIn x@(Let _ _ _ _) = group x -absorbIn x = line <> group x <> line - -- Only absorb "else if" absorbElse :: Expression -> Doc absorbElse (If if_ cond then_ expr0 else_ expr1) @@ -245,7 +235,7 @@ instance Pretty Expression where (Ann in_ inTrailing inLeading) expr) = base $ group letPart <> line <> inPart where letPart = pretty let_ <> pretty letTrailing <> hardline <> letBody - inPart = pretty in_ <> hardspace <> absorbIn expr + inPart = pretty in_ <> line <> group expr <> line letBody = nest 2 $ pretty letLeading <> sepBy hardline binders diff --git a/test/diff/idioms_lib_2/out.nix b/test/diff/idioms_lib_2/out.nix index 7b42e87a..d891e38c 100644 --- a/test/diff/idioms_lib_2/out.nix +++ b/test/diff/idioms_lib_2/out.nix @@ -226,7 +226,8 @@ rec { versionSuffix = let suffixFile = ../.version-suffix; - in if pathExists suffixFile then + in + if pathExists suffixFile then lib.strings.fileContents suffixFile else "pre-git" @@ -243,7 +244,8 @@ rec { let revisionFile = "${toString ./..}/.git-revision"; gitRepo = "${toString ./..}/.git"; - in if lib.pathIsGitRepo gitRepo then + in + if lib.pathIsGitRepo gitRepo then lib.commitIdFromGitRepo gitRepo else if lib.pathExists revisionFile then lib.fileContents revisionFile diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index e7844203..1467102a 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -22,7 +22,8 @@ let inherit (lib) isFunction; -in rec { +in +rec { ## -- HELPER FUNCTIONS & DEFAULTS -- @@ -39,7 +40,8 @@ in rec { abort ("generators.mkValueStringDefault: " + "${t} not supported: ${toPretty { } v}") ; - in if isInt v then + in + if isInt v then toString v # convert derivations to store paths else if lib.isDerivation v then @@ -252,7 +254,8 @@ in rec { section = head sections; subsections = tail sections; subsection = concatStringsSep "." subsections; - in if containsQuote || subsections == [ ] then + in + if containsQuote || subsections == [ ] then name else ''${section} "${subsection}"'' @@ -346,12 +349,14 @@ in rec { depth: v: let evalNext = x: mapAny (depth + 1) (transform (depth + 1) x); - in if isAttrs v then + in + if isAttrs v then mapAttrs (stepIntoAttr evalNext) v else if isList v then map evalNext v else - transform (depth + 1) v; + transform (depth + 1) v + ; in mapAny 0 ; @@ -395,7 +400,8 @@ in rec { else " " ; - in if isInt v then + in + if isInt v then toString v # toString loses precision on floats, so we use toJSON instead. This isn't perfect # as the resulting string may not parse back as a float (e.g. 42, 1e-06), but for @@ -433,7 +439,8 @@ in rec { else introSpace + lastLine) + "''" ; - in if multiline && length lines > 1 then + in + if multiline && length lines > 1 then multilineResult else singlelineResult @@ -461,7 +468,8 @@ in rec { name + "?" else name) fna); - in if fna == { } then + in + if fna == { } then "" else "" @@ -566,7 +574,8 @@ in rec { ]) x)) ; - in '' + in + '' @@ -583,7 +592,8 @@ in rec { with builtins; let concatItems = lib.strings.concatStringsSep ", "; - in if isAttrs v then + in + if isAttrs v then "{ ${ concatItems (lib.attrsets.mapAttrsToList (key: value: "${key} = ${toDhall args value}") v) diff --git a/test/diff/idioms_nixos_1/out.nix b/test/diff/idioms_nixos_1/out.nix index f64b3013..04d4920c 100644 --- a/test/diff/idioms_nixos_1/out.nix +++ b/test/diff/idioms_nixos_1/out.nix @@ -17,7 +17,8 @@ let ${concatStringsSep "\n" config.boot.kernelModules} ''; -in { +in +{ ###### interface diff --git a/test/diff/idioms_nixos_2/out.nix b/test/diff/idioms_nixos_2/out.nix index ddedc74b..af05e497 100644 --- a/test/diff/idioms_nixos_2/out.nix +++ b/test/diff/idioms_nixos_2/out.nix @@ -66,7 +66,8 @@ let inherit (config.system) stateVersion; -in { +in +{ imports = [ (mkRemovedOptionModule [ @@ -878,7 +879,8 @@ in { renderedAppStoreSetting = let x = cfg.appstoreEnable; - in if x == null then + in + if x == null then "false" else boolToString x @@ -1052,7 +1054,8 @@ in { "--admin-pass" = ''"''$${adminpass.arg}"''; "--data-dir" = ''"${datadir}/data"''; }); - in '' + in + '' ${mkExport dbpass} ${mkExport adminpass} ${occ}/bin/nextcloud-occ maintenance:install \ @@ -1064,7 +1067,8 @@ in { ${toString i} --value="${toString v}" '') ([ cfg.hostName ] ++ cfg.config.extraTrustedDomains)); - in { + in + { wantedBy = [ "multi-user.target" ]; before = [ "phpfpm-nextcloud.service" ]; path = [ occ ]; diff --git a/test/diff/let_in/out.nix b/test/diff/let_in/out.nix index c5f94f3b..638a97f2 100644 --- a/test/diff/let_in/out.nix +++ b/test/diff/let_in/out.nix @@ -64,7 +64,8 @@ let a = let - in [ + in + [ 1 2 ] diff --git a/test/diff/monsters_5/out.nix b/test/diff/monsters_5/out.nix index 10b8a24b..24378b34 100644 --- a/test/diff/monsters_5/out.nix +++ b/test/diff/monsters_5/out.nix @@ -51,7 +51,8 @@ let ${concatStringsSep "\n" config.boot.kernelModules} ''; -in { +in +{ ###### interface