From 622ea8c109d32f90353eb036c78640115e7d85f8 Mon Sep 17 00:00:00 2001 From: piegames Date: Mon, 2 Dec 2024 23:24:01 +0100 Subject: [PATCH] Fix absorbtion of empty attrsets with newline Fixes #253 --- src/Nixfmt/Pretty.hs | 6 ++++++ test/diff/attr_set/in.nix | 17 +++++++++++++++++ test/diff/attr_set/out-pure.nix | 9 +++++++++ test/diff/attr_set/out.nix | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 1f03c822..4f99661c 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -540,6 +540,12 @@ isAbsorbable (Path _) = True -- Non-empty sets and lists isAbsorbable (Set _ _ (Items (_ : _)) _) = True isAbsorbable (List _ (Items (_ : _)) _) = True +-- Empty sets and lists if they have a line break +-- https://github.com/NixOS/nixfmt/issues/253 +isAbsorbable (Set _ (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2})) + | line1 /= line2 = True +isAbsorbable (List (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2})) + | line1 /= line2 = True isAbsorbable (Parenthesized (LoneAnn _) (Term t) _) = isAbsorbable t isAbsorbable _ = False diff --git a/test/diff/attr_set/in.nix b/test/diff/attr_set/in.nix index e9ca7d84..abc8b304 100644 --- a/test/diff/attr_set/in.nix +++ b/test/diff/attr_set/in.nix @@ -274,4 +274,21 @@ pkgs.xorg.fontadobe75dpi ]; } + # Regression https://github.com/NixOS/nixfmt/issues/253 + { + foo1 = { + }; + foo2 = bar { + }; + foo3 = bar { + } { + }; + foo4 = [ + ]; + foo5 = bar [ + ]; + foo6 = bar [ + ] [ + ]; + } ] diff --git a/test/diff/attr_set/out-pure.nix b/test/diff/attr_set/out-pure.nix index f28d2e25..c4b9cf21 100644 --- a/test/diff/attr_set/out-pure.nix +++ b/test/diff/attr_set/out-pure.nix @@ -378,4 +378,13 @@ pkgs.xorg.fontadobe75dpi ]; } + # Regression https://github.com/NixOS/nixfmt/issues/253 + { + foo1 = { }; + foo2 = bar { }; + foo3 = bar { } { }; + foo4 = [ ]; + foo5 = bar [ ]; + foo6 = bar [ ] [ ]; + } ] diff --git a/test/diff/attr_set/out.nix b/test/diff/attr_set/out.nix index 7b52a916..bd47e960 100644 --- a/test/diff/attr_set/out.nix +++ b/test/diff/attr_set/out.nix @@ -381,4 +381,27 @@ pkgs.xorg.fontadobe75dpi ]; } + # Regression https://github.com/NixOS/nixfmt/issues/253 + { + foo1 = { + }; + foo2 = bar { + }; + foo3 = + bar + { + } + { + }; + foo4 = [ + ]; + foo5 = bar [ + ]; + foo6 = + bar + [ + ] + [ + ]; + } ]