diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 65b5ec1e..7057b9ac 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -64,7 +64,10 @@ instance Pretty a => Pretty (Item a) where -- For lists, attribute sets and let bindings prettyItems :: Pretty a => Doc -> Items a -> Doc -prettyItems sep = prettyItems' . unItems +-- Special case: Preserve an empty line with no items +-- usually, trailing newlines after the last element are not preserved +prettyItems _ (Items [DetachedComments []]) = emptyline +prettyItems sep items = prettyItems' $ unItems items where prettyItems' :: Pretty a => [Item a] -> Doc prettyItems' [] = mempty @@ -124,9 +127,9 @@ instance Pretty Binder where -- 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, non-recursive attribute set -prettySet _ (Nothing, Ann [] paropen Nothing, Items [], parclose@(Ann [] _ _)) - = pretty paropen <> hardspace <> pretty parclose +-- 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) diff --git a/test/diff/attr_set/in.nix b/test/diff/attr_set/in.nix index 36ceab67..07c182fe 100644 --- a/test/diff/attr_set/in.nix +++ b/test/diff/attr_set/in.nix @@ -5,6 +5,14 @@ {a=1; } + { + + } + + { a = { + + };} + { b=1; } { b=1; /*c*/ } { /*a*/ b=1; } diff --git a/test/diff/attr_set/out.nix b/test/diff/attr_set/out.nix index 7d38543f..397512e3 100644 --- a/test/diff/attr_set/out.nix +++ b/test/diff/attr_set/out.nix @@ -6,6 +6,16 @@ { a = 1; } { a = 1; } + { + + } + + { + a = { + + }; + } + { b = 1; } { b = 1; # c diff --git a/test/diff/lists/in.nix b/test/diff/lists/in.nix index 56352bc1..0839c920 100644 --- a/test/diff/lists/in.nix +++ b/test/diff/lists/in.nix @@ -1,4 +1,12 @@ [ + [ + + ] + + [ [ + + ] ] + [ { # multiline foo = "bar"; diff --git a/test/diff/lists/out.nix b/test/diff/lists/out.nix index 8cbd273a..b389f22a 100644 --- a/test/diff/lists/out.nix +++ b/test/diff/lists/out.nix @@ -1,4 +1,14 @@ [ + [ + + ] + + [ + [ + + ] + ] + [ { # multiline