From 54d45fccb2fbd420b2597fbf8aff6ec1b109ad89 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 19 Jul 2024 07:13:04 +0200 Subject: [PATCH] Make empty inherit's more consistent and convenient This might happen if you temporarily remove all inherits, in which case the formatter shouldn't remove the spacing that was there. E.g. I might have this initially inherit a b; inherit c d ; removing all the bindings gives me inherit ; # I already have the space that I'll need inherit # I can just press enter here to add one ; which shouldn't be changed, because it's easier like this to insert new entries again, compared to inherit; This also makes it more consistent with the parent commit's change --- src/Nixfmt/Pretty.hs | 17 ++++++----------- test/diff/inherit/out.nix | 5 +++-- test/diff/inherit_from/out.nix | 10 ++++++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 88892ea2..120ef92b 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -127,10 +127,8 @@ instance Pretty Binder where pretty (Inherit inherit Nothing ids semicolon) = group $ pretty inherit - <> ( if null ids - then pretty semicolon - else sep <> nest (sepBy sep ids <> nosep <> pretty semicolon) - ) + <> sep + <> nest (sepBy sep ids <> nosep <> pretty semicolon) where -- Only allow a single line if it's already on a single line and has few enough elements (sep, nosep) = if sourceLine inherit == sourceLine semicolon && length ids < 4 then (line, line') else (hardline, hardline) @@ -140,13 +138,10 @@ instance Pretty Binder where pretty inherit <> nest ( group' RegularG (line <> pretty source) - <> if null ids - then pretty semicolon - else - sep - <> sepBy sep ids - <> nosep - <> pretty semicolon + <> sep + <> sepBy sep ids + <> nosep + <> pretty semicolon ) where -- Only allow a single line if it's already on a single line and has few enough elements diff --git a/test/diff/inherit/out.nix b/test/diff/inherit/out.nix index a5b01067..a15a258d 100644 --- a/test/diff/inherit/out.nix +++ b/test/diff/inherit/out.nix @@ -1,7 +1,7 @@ [ { # empty inherit o.O - inherit; + inherit ; inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; } { @@ -83,7 +83,8 @@ } { - inherit; + inherit + ; inherit a ; diff --git a/test/diff/inherit_from/out.nix b/test/diff/inherit_from/out.nix index 62225bbc..f53312f6 100644 --- a/test/diff/inherit_from/out.nix +++ b/test/diff/inherit_from/out.nix @@ -1,7 +1,7 @@ [ { # empty inherit o.O - inherit (geany.meta); + inherit (geany.meta) ; inherit (builtins) pathExists readFile @@ -572,9 +572,11 @@ ; } { - inherit ({ }); - inherit ({ }); - inherit ({ }); + inherit ({ }) ; + inherit ({ }) + ; + inherit ({ }) + ; inherit ({ }) a; inherit ({ })