diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index d4a2497c..88892ea2 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -129,8 +129,11 @@ instance Pretty Binder where pretty inherit <> ( if null ids then pretty semicolon - else line <> nest (sepBy (if length ids < 4 then line else hardline) ids <> line' <> pretty semicolon) + else 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) -- `inherit (foo) bar` statement pretty (Inherit inherit (Just source) ids semicolon) = group $ @@ -140,11 +143,14 @@ instance Pretty Binder where <> if null ids then pretty semicolon else - line - <> sepBy (if length ids < 4 then line else hardline) ids - <> line' + 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 + (sep, nosep) = if sourceLine inherit == sourceLine semicolon && length ids < 4 then (line, line') else (hardline, hardline) -- `foo = bar` pretty (Assignment selectors assign expr semicolon) = group $ diff --git a/test/diff/inherit/in.nix b/test/diff/inherit/in.nix index a66fc107..a5088d4c 100644 --- a/test/diff/inherit/in.nix +++ b/test/diff/inherit/in.nix @@ -29,4 +29,13 @@ h ; } + + { + inherit + ; + inherit + a; + inherit a + ; + } ] diff --git a/test/diff/inherit/out-pure.nix b/test/diff/inherit/out-pure.nix index 93a31869..307e32ab 100644 --- a/test/diff/inherit/out-pure.nix +++ b/test/diff/inherit/out-pure.nix @@ -77,4 +77,10 @@ h ; } + + { + inherit; + inherit a; + inherit a; + } ] diff --git a/test/diff/inherit/out.nix b/test/diff/inherit/out.nix index 77e5e873..a5b01067 100644 --- a/test/diff/inherit/out.nix +++ b/test/diff/inherit/out.nix @@ -19,7 +19,9 @@ ; } { - inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; + inherit + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ; } { inherit b d; } { @@ -79,4 +81,14 @@ h ; } + + { + inherit; + inherit + a + ; + inherit + a + ; + } ] diff --git a/test/diff/inherit_from/in.nix b/test/diff/inherit_from/in.nix index 6b375f1c..ea9d0e6e 100644 --- a/test/diff/inherit_from/in.nix +++ b/test/diff/inherit_from/in.nix @@ -75,4 +75,17 @@ { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f h /*i*/; } { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h ; } { inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h /*i*/; } + { + inherit ({}) ; + inherit ({}) + ; + inherit + ({}); + + inherit ({}) a; + inherit ({}) a + ; + inherit ({}) + a; + } ] diff --git a/test/diff/inherit_from/out-pure.nix b/test/diff/inherit_from/out-pure.nix index 126b8f8b..58be059f 100644 --- a/test/diff/inherit_from/out-pure.nix +++ b/test/diff/inherit_from/out-pure.nix @@ -571,4 +571,13 @@ h # i ; } + { + inherit ({ }); + inherit ({ }); + inherit ({ }); + + inherit ({ }) a; + inherit ({ }) a; + inherit ({ }) a; + } ] diff --git a/test/diff/inherit_from/out.nix b/test/diff/inherit_from/out.nix index 126b8f8b..62225bbc 100644 --- a/test/diff/inherit_from/out.nix +++ b/test/diff/inherit_from/out.nix @@ -571,4 +571,17 @@ h # i ; } + { + inherit ({ }); + inherit ({ }); + inherit ({ }); + + inherit ({ }) a; + inherit ({ }) + a + ; + inherit ({ }) + a + ; + } ]