From c10c061713782067e6ae6f90210f10d0aa6366ba Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 19 Jul 2024 06:53:22 +0200 Subject: [PATCH] Always preserve expanded inherits This changes the formatting to take into account whether inherits are expanded onto multiple lines already, and if so, preserves that. This is desirable because if a user writes an inherit over multiple lines, there's most likely an intention to add more, which would become harder when formatted. An example of this is inheriting from `lib`, which usually starts out with just a single value, but usually gets more later. This would've been contracted onto a single line before this change: inherit (lib) foo ; This change conforms to the standard due to this line: > The formatter may take the input formatting into account in some cases in order to preserve multi-line syntax elements (which would otherwise have been contracted by the rules). --- src/Nixfmt/Pretty.hs | 14 ++++++++++---- test/diff/inherit/in.nix | 9 +++++++++ test/diff/inherit/out-pure.nix | 6 ++++++ test/diff/inherit/out.nix | 14 +++++++++++++- test/diff/inherit_from/in.nix | 13 +++++++++++++ test/diff/inherit_from/out-pure.nix | 9 +++++++++ test/diff/inherit_from/out.nix | 13 +++++++++++++ 7 files changed, 73 insertions(+), 5 deletions(-) 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 + ; + } ]