diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index d9e79cf8..36898c8e 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -150,23 +150,6 @@ instance Pretty Binder where -- while we already pretty eagerly expand sets with more than one element, -- in some situations even that is not sufficient. The wide parameter will -- be even more eager at expanding, except for empty sets and inherit statements. -prettySet :: Bool -> (Maybe Leaf, Leaf, Items Binder, Leaf) -> Doc --- Empty attribute set -prettySet _ (krec, Ann [] paropen Nothing, Items [], parclose@(Ann [] _ _)) = - pretty (fmap (,hardspace) krec) <> pretty paropen <> hardspace <> pretty parclose --- Singleton sets are allowed to fit onto one line, --- but apart from that always expand. -prettySet wide (krec, Ann pre paropen post, binders, parclose) = - pretty (fmap (,hardspace) krec) - <> pretty (Ann pre paropen Nothing) - <> surroundWith sep (nest $ pretty post <> prettyItems binders) - <> pretty parclose - where - sep = if wide && not (null (unItems binders)) then hardline else line - -prettyTermWide :: Term -> Doc -prettyTermWide (Set krec paropen items parclose) = prettySet True (krec, paropen, items, parclose) -prettyTermWide t = prettyTerm t -- | Pretty print a term without wrapping it in a group. prettyTerm :: Term -> Doc @@ -198,7 +181,19 @@ prettyTerm (List (Ann pre paropen post) items parclose) = pretty (Ann pre paropen Nothing) <> surroundWith line (nest $ pretty post <> prettyItems items) <> pretty parclose -prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose) +-- Empty attribute set +prettyTerm (Set krec (Ann [] paropen Nothing) (Items []) parclose@(Ann [] _ _)) = + pretty (fmap (,hardspace) krec) <> pretty paropen <> hardspace <> pretty parclose +-- Singleton sets are allowed to fit onto one line, +-- but apart from that always expand. +prettyTerm (Set krec (Ann pre paropen post) binders parclose) = + pretty (fmap (,hardspace) krec) + <> pretty (Ann pre paropen Nothing) + <> surroundWith sep (nest $ pretty post <> prettyItems binders) + <> pretty parclose + where + sep = if not (null (unItems binders)) then hardline else line + -- Parentheses prettyTerm (Parenthesized paropen expr (Ann closePre parclose closePost)) = group $ @@ -209,7 +204,7 @@ prettyTerm (Parenthesized paropen expr (Ann closePre parclose closePost)) = inner = case expr of -- Start on the same line for these - _ | isAbsorbableExpr expr -> group $ absorbExpr False expr + _ | isAbsorbableExpr expr -> group $ absorbExpr expr -- Parenthesized application (Application f a) -> prettyApp True mempty True f a -- Same thing for selections @@ -371,7 +366,7 @@ prettyApp indentFunction pre hasPost f a = <> pretty name <> pretty colon <> hardspace - <> prettyTermWide body + <> prettyTerm body <> pretty close -- Special case: Absorb parenthesized function application with absorbable body absorbLast @@ -388,7 +383,7 @@ prettyApp indentFunction pre hasPost f a = pretty open <> pretty fn <> hardspace - <> prettyTermWide body + <> prettyTerm body <> pretty close absorbLast (Term (Parenthesized open expr close)) = absorbParen open expr close @@ -422,7 +417,7 @@ prettyWith True (With with expr0 semicolon (Term expr1)) = <> pretty semicolon -- Force-expand attrsets <> hardspace - <> group' Priority (prettyTermWide expr1) + <> group' Priority (prettyTerm expr1) -- Normal case prettyWith _ (With with expr0 semicolon expr1) = group @@ -480,18 +475,17 @@ absorbParen (Ann pre' open post') expr (Ann pre'' close post'') = -- Note that unlike for absorbable terms which can be force-absorbed, some expressions -- may turn out to not be absorbable. In that case, they should start with a line' so that -- they properly start on the next line if necessary. -absorbExpr :: Bool -> Expression -> Doc -absorbExpr True (Term t) | isAbsorbableTerm t = prettyTermWide t -absorbExpr False (Term t) | isAbsorbableTerm t = prettyTerm t +absorbExpr :: Expression -> Doc +absorbExpr (Term t) | isAbsorbableTerm t = prettyTerm t -- With expression with absorbable body: Treat as absorbable term -absorbExpr _ expr@(With _ _ _ (Term t)) | isAbsorbableTerm t = prettyWith True expr -absorbExpr _ expr = pretty expr +absorbExpr expr@(With _ _ _ (Term t)) | isAbsorbableTerm t = prettyWith True expr +absorbExpr expr = pretty expr -- Render the RHS value of an assignment or function parameter default value absorbRHS :: Expression -> Doc absorbRHS expr = case expr of -- Absorbable expression. Always start on the same line - _ | isAbsorbableExpr expr -> hardspace <> group (absorbExpr True expr) + _ | isAbsorbableExpr expr -> hardspace <> group (absorbExpr expr) -- Parenthesized expression. Same thing as the special case for parenthesized last argument in function calls. (Term (Parenthesized open expr' close)) -> hardspace <> absorbParen open expr' close -- Not all strings are absorbable, but in this case we always want to keep them attached. @@ -511,11 +505,11 @@ absorbRHS expr = case expr of -- Case 1: two arguments, LHS is absorbable term, RHS fits onto the last line (Operation (Term t) (Ann [] op Nothing) b) | isAbsorbable t && isUpdateOrConcat op -> - group' RegularG $ line <> group' Priority (prettyTermWide t) <> line <> pretty op <> hardspace <> pretty b + group' RegularG $ line <> group' Priority (prettyTerm t) <> line <> pretty op <> hardspace <> pretty b -- Case 2a: LHS fits onto first line, RHS is an absorbable term (Operation l (Ann [] op Nothing) (Term t)) | isAbsorbable t && isUpdateOrConcat op -> - group' RegularG $ line <> pretty l <> line <> group' Transparent (pretty op <> hardspace <> group' Priority (prettyTermWide t)) + group' RegularG $ line <> pretty l <> line <> group' Transparent (pretty op <> hardspace <> group' Priority (prettyTerm t)) -- Case 2b: LHS fits onto first line, RHS is a function application (Operation l (Ann [] op Nothing) (Application f a)) | isUpdateOrConcat op -> @@ -609,7 +603,7 @@ instance Pretty Expression where -- If there are multiple ID parameters to that function, treat them all at once absorbAbs depth (Abstraction (IDParameter param0) colon0 body0) = hardspace <> pretty param0 <> pretty colon0 <> absorbAbs (depth + 1) body0 - absorbAbs _ expr | isAbsorbableExpr expr = hardspace <> group' Priority (absorbExpr False expr) + absorbAbs _ expr | isAbsorbableExpr expr = hardspace <> group' Priority (absorbExpr expr) -- Force the content onto a new line when it is not absorbable and there are more than two arguments absorbAbs depth x = (if depth <= 2 then line else hardline) <> pretty x @@ -617,7 +611,7 @@ instance Pretty Expression where -- Attrset parameter pretty (Abstraction param colon (Term t)) | isAbsorbable t = - pretty param <> pretty colon <> line <> group (prettyTermWide t) + pretty param <> pretty colon <> line <> group (prettyTerm t) pretty (Abstraction param colon body) = pretty param <> pretty colon <> line <> pretty body pretty (Application f a) = @@ -725,7 +719,7 @@ instance Pretty [StringPart] where -- Code copied over from parentheses. Could be factored out into a common function one day inner = case expr of -- Start on the same line for these - _ | isAbsorbableExpr expr -> group $ absorbExpr False expr + _ | isAbsorbableExpr expr -> group $ absorbExpr expr -- Parenthesized application (Application f a) -> prettyApp True mempty True f a -- Same thing for selections diff --git a/test/correct/float-below-one.nix b/test/correct/float-below-one.nix index e5fdbde0..4ca733e6 100644 --- a/test/correct/float-below-one.nix +++ b/test/correct/float-below-one.nix @@ -1 +1,3 @@ -{ foo = 0.3; } +{ + foo = 0.3; +} diff --git a/test/correct/quotes-in-inherit.nix b/test/correct/quotes-in-inherit.nix index a36f90d9..4d8f018c 100644 --- a/test/correct/quotes-in-inherit.nix +++ b/test/correct/quotes-in-inherit.nix @@ -1 +1,8 @@ -{ inherit ({ "in" = 1; }) "in"; } +{ + inherit + ({ + "in" = 1; + }) + "in" + ; +} diff --git a/test/diff/apply/out.nix b/test/diff/apply/out.nix index 42497b7f..e53a8d06 100644 --- a/test/diff/apply/out.nix +++ b/test/diff/apply/out.nix @@ -106,46 +106,90 @@ } '' { - name1 = function arg { asdf = 1; }; + name1 = function arg { + asdf = 1; + }; - name2 = function arg { asdf = 1; } argument; + name2 = function arg { + asdf = 1; + } argument; - name3 = function arg { asdf = 1; } { qwer = 12345; } argument; + name3 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + } + argument; } { - name1 = function arg { asdf = 1; }; + name1 = function arg { + asdf = 1; + }; name2 = function arg { asdf = 1; # multiline } argument; - name3 = function arg { - asdf = 1; - # multiline - } { qwer = 12345; } argument; + name3 = + function arg + { + asdf = 1; + # multiline + } + { + qwer = 12345; + } + argument; } { - name4 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } argument; + name4 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + argument; } { - option1 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option1 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option2 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option2 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option3 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option3 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; } # https://github.com/kamadorueda/alejandra/issues/372#issuecomment-1435083516 { diff --git a/test/diff/attr_set/out.nix b/test/diff/attr_set/out.nix index 7e5ec04b..bcbe8bd7 100644 --- a/test/diff/attr_set/out.nix +++ b/test/diff/attr_set/out.nix @@ -3,8 +3,12 @@ { # a } - { a = 1; } - { a = 1; } + { + a = 1; + } + { + a = 1; + } { @@ -16,7 +20,9 @@ }; } - { b = 1; } + { + b = 1; + } { b = 1; # c } @@ -29,7 +35,9 @@ b = 1; # c } - rec { c = 1; } + rec { + c = 1; + } rec { c = 1; # d } diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index 1115ae8b..603278a7 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -153,7 +153,9 @@ rec { '' [${mkSectionName sectName}] '' - + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues; + + toKeyValue { + inherit mkKeyValue listsAsDuplicateKeys; + } sectValues; in # map input to ini sections mapAttrsToStringsSep "\n" mkSection attrsOfAttrs; @@ -208,9 +210,14 @@ rec { if globalSection == { } then "" else - (toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection) + "\n" + (toKeyValue { + inherit mkKeyValue listsAsDuplicateKeys; + } globalSection) + + "\n" ) - + (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections); + + (toINI { + inherit mkSectionName mkKeyValue listsAsDuplicateKeys; + } sections); # Generate a git-config file from an attrset. # @@ -263,13 +270,19 @@ rec { if isAttrs value && !lib.isDerivation value then lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value else if length path > 1 then - { ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; } + { + ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; + } else - { ${head path} = value; }; + { + ${head path} = value; + }; in attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs)); - toINI_ = toINI { inherit mkKeyValue mkSectionName; }; + toINI_ = toINI { + inherit mkKeyValue mkSectionName; + }; in toINI_ (gitFlattenAttrs attrs); diff --git a/test/diff/idioms_lib_4/out.nix b/test/diff/idioms_lib_4/out.nix index 8ff78dae..36201958 100644 --- a/test/diff/idioms_lib_4/out.nix +++ b/test/diff/idioms_lib_4/out.nix @@ -19,7 +19,9 @@ with lib.lists; with lib.types; with lib.attrsets; with lib.strings; -with (import ./inspect.nix { inherit lib; }).predicates; +with (import ./inspect.nix { + inherit lib; +}).predicates; let inherit (lib.options) mergeOneOption; @@ -29,7 +31,12 @@ let mapAttrs ( name: value: assert type.check value; - setType type.name ({ inherit name; } // value) + setType type.name ( + { + inherit name; + } + // value + ) ); in diff --git a/test/diff/idioms_lib_5/out.nix b/test/diff/idioms_lib_5/out.nix index ee898df8..506991cf 100644 --- a/test/diff/idioms_lib_5/out.nix +++ b/test/diff/idioms_lib_5/out.nix @@ -382,7 +382,9 @@ let null else "key 'meta.${k}' has invalid value; expected ${metaTypes.${k}.description}, got\n ${ - lib.generators.toPretty { indent = " "; } v + lib.generators.toPretty { + indent = " "; + } v }" else "key 'meta.${k}' is unrecognized; expected one of: \n [${ @@ -509,7 +511,9 @@ let } # ----- else - { valid = "yes"; } + { + valid = "yes"; + } ); # The meta attribute is passed in the resulting attribute set, @@ -595,12 +599,22 @@ let # or, alternatively, just output a warning message. handled = { - no = handleEvalIssue { inherit meta attrs; } { - inherit (validity) reason errormsg; - }; - warn = handleEvalWarning { inherit meta attrs; } { - inherit (validity) reason errormsg; - }; + no = + handleEvalIssue + { + inherit meta attrs; + } + { + inherit (validity) reason errormsg; + }; + warn = + handleEvalWarning + { + inherit meta attrs; + } + { + inherit (validity) reason errormsg; + }; yes = true; } .${validity.valid}; diff --git a/test/diff/idioms_nixos_2/out.nix b/test/diff/idioms_nixos_2/out.nix index e7d3e5d2..53a85026 100644 --- a/test/diff/idioms_nixos_2/out.nix +++ b/test/diff/idioms_nixos_2/out.nix @@ -51,7 +51,10 @@ let post_max_size = cfg.maxUploadSize; memory_limit = cfg.maxUploadSize; } - // cfg.phpOptions // optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; }; + // cfg.phpOptions + // optionalAttrs cfg.caching.apcu { + "apc.enable_cli" = "1"; + }; occ = pkgs.writeScriptBin "nextcloud-occ" '' #! ${pkgs.runtimeShell} @@ -1051,7 +1054,9 @@ in ln -sfT \ ${ pkgs.linkFarm "nix-apps" ( - mapAttrsToList (name: path: { inherit name path; }) cfg.extraApps + mapAttrsToList (name: path: { + inherit name path; + }) cfg.extraApps ) } \ ${cfg.home}/nix-apps diff --git a/test/diff/idioms_pkgs_1/out.nix b/test/diff/idioms_pkgs_1/out.nix index afdbed26..12c0e5c3 100644 --- a/test/diff/idioms_pkgs_1/out.nix +++ b/test/diff/idioms_pkgs_1/out.nix @@ -8,7 +8,9 @@ stdenv.mkDerivation rec { pname = "test"; version = "0.0"; - src = fetchFrom { url = "example/${version}"; }; + src = fetchFrom { + url = "example/${version}"; + }; meta = with lib; { maintainers = with maintainers; [ someone ]; description = "something"; diff --git a/test/diff/idioms_pkgs_2/out.nix b/test/diff/idioms_pkgs_2/out.nix index 84f50d54..69619cc8 100644 --- a/test/diff/idioms_pkgs_2/out.nix +++ b/test/diff/idioms_pkgs_2/out.nix @@ -20,12 +20,16 @@ stdenv.mkDerivation rec { doCheck = true; passthru.tests = { - version = testVersion { package = hello; }; + version = testVersion { + package = hello; + }; invariant-under-noXlibs = testEqualDerivation "hello must not be rebuilt when environment.noXlibs is set." hello - (nixos { environment.noXlibs = true; }).pkgs.hello; + (nixos { + environment.noXlibs = true; + }).pkgs.hello; }; meta = with lib; { diff --git a/test/diff/idioms_pkgs_4/out.nix b/test/diff/idioms_pkgs_4/out.nix index a4ed06e4..0372247f 100644 --- a/test/diff/idioms_pkgs_4/out.nix +++ b/test/diff/idioms_pkgs_4/out.nix @@ -197,9 +197,13 @@ in # First build a stdenv based only on tools outside the store. (prevStage: { inherit config overlays; - stdenv = makeStdenv { inherit (prevStage) cc fetchurl; } // { - inherit (prevStage) fetchurl; - }; + stdenv = + makeStdenv { + inherit (prevStage) cc fetchurl; + } + // { + inherit (prevStage) fetchurl; + }; }) # Using that, build a stdenv that adds the ‘xz’ command (which most systems @@ -209,7 +213,9 @@ in stdenv = makeStdenv { inherit (prevStage.stdenv) cc fetchurl; extraPath = [ prevStage.xz ]; - overrides = self: super: { inherit (prevStage) xz; }; + overrides = self: super: { + inherit (prevStage) xz; + }; extraNativeBuildInputs = if localSystem.isLinux then [ prevStage.patchelf ] else [ ]; }; diff --git a/test/diff/idioms_pkgs_5/out.nix b/test/diff/idioms_pkgs_5/out.nix index fc02b21a..92448cf2 100644 --- a/test/diff/idioms_pkgs_5/out.nix +++ b/test/diff/idioms_pkgs_5/out.nix @@ -56,7 +56,12 @@ let # separate lines, because Nix would only show the last line of the comment. # An infinite recursion here can be caused by having the attribute names of expression `e` in `.overrideAttrs(finalAttrs: previousAttrs: e)` depend on `finalAttrs`. Only the attribute values of `e` can depend on `finalAttrs`. - args = rattrs (args // { inherit finalPackage overrideAttrs; }); + args = rattrs ( + args + // { + inherit finalPackage overrideAttrs; + } + ); # ^^^^ overrideAttrs = @@ -500,7 +505,9 @@ let "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}" ); }) - // optionalAttrs __structuredAttrs { env = checkedEnv; } + // optionalAttrs __structuredAttrs { + env = checkedEnv; + } // { builder = attrs.realBuilder or stdenv.shell; args = @@ -628,9 +635,14 @@ let enableParallelChecking = attrs.enableParallelChecking or true; enableParallelInstalling = attrs.enableParallelInstalling or true; } - // optionalAttrs ( - hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl - ) { NIX_HARDENING_ENABLE = enabledHardeningOptions; } + // + optionalAttrs + ( + hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl + ) + { + NIX_HARDENING_ENABLE = enabledHardeningOptions; + } // optionalAttrs (stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform ? gcc.arch) { @@ -712,7 +724,9 @@ let references ; }; - validity = checkMeta.assertValidity { inherit meta attrs; }; + validity = checkMeta.assertValidity { + inherit meta attrs; + }; checkedEnv = let diff --git a/test/diff/if_else/out.nix b/test/diff/if_else/out.nix index 479cd610..15848854 100644 --- a/test/diff/if_else/out.nix +++ b/test/diff/if_else/out.nix @@ -1,5 +1,14 @@ [ - (if true then { version = "1.2.3"; } else { version = "3.2.1"; }) + ( + if true then + { + version = "1.2.3"; + } + else + { + version = "3.2.1"; + } + ) ( if true then '' diff --git a/test/diff/inherit/out.nix b/test/diff/inherit/out.nix index 93a31869..30594acd 100644 --- a/test/diff/inherit/out.nix +++ b/test/diff/inherit/out.nix @@ -18,8 +18,12 @@ j ; } - { inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; } - { inherit b d; } + { + inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; + } + { + inherit b d; + } { inherit b diff --git a/test/diff/inherit_from/out.nix b/test/diff/inherit_from/out.nix index 126b8f8b..aaaf6847 100644 --- a/test/diff/inherit_from/out.nix +++ b/test/diff/inherit_from/out.nix @@ -40,7 +40,9 @@ k ; } - { inherit (c) f h; } + { + inherit (c) f h; + } { inherit (c) f diff --git a/test/diff/key_value/out.nix b/test/diff/key_value/out.nix index 1d93de5b..0470f894 100644 --- a/test/diff/key_value/out.nix +++ b/test/diff/key_value/out.nix @@ -41,14 +41,18 @@ rec { 1 # d ; }; - j = a: { b = 1; }; + j = a: { + b = 1; + }; k = a: { b = 1; c = 2; }; l = a: # b - { b = 1; }; + { + b = 1; + }; m = a: # b { diff --git a/test/diff/with/out.nix b/test/diff/with/out.nix index 9c71920b..bcb43cef 100644 --- a/test/diff/with/out.nix +++ b/test/diff/with/out.nix @@ -16,8 +16,12 @@ ) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) - { a = with b; 1; } - { a = with b; 1 + 1; } + { + a = with b; 1; + } + { + a = with b; 1 + 1; + } { a = with b; { c = 1; @@ -62,7 +66,14 @@ 3 ]) (with a; with b; with c; [ 1 ]) - (with a; with b; with c; { a = 1; }) + ( + with a; + with b; + with c; + { + a = 1; + } + ) ( with a; # comment with b; @@ -89,7 +100,9 @@ b = 2; } ) - { a = with b; with b; with b; 1; } + { + a = with b; with b; with b; 1; + } { binPath = with pkgs;