From 5d871d779a747b54b0d734a4b32ddcb58dd25eed Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 5 Aug 2025 20:28:27 +0200 Subject: [PATCH] Keep trailing comment in { and [ This is a partial revert of 5bb0639 The change introduced there is now redundant because we catch that specific corner case more precisely in the parser nowadays, so it's reasonable to revert --- src/Nixfmt/Pretty.hs | 12 ++++++------ test/diff/attr_set/out-pure.nix | 21 +++++++-------------- test/diff/attr_set/out.nix | 21 +++++++-------------- test/diff/comment/out-pure.nix | 6 ++---- test/diff/comment/out.nix | 6 ++---- test/diff/idioms_lib_4/out-pure.nix | 3 +-- test/diff/idioms_lib_4/out.nix | 3 +-- test/diff/idioms_pkgs_4/out-pure.nix | 3 +-- test/diff/idioms_pkgs_4/out.nix | 3 +-- test/diff/lists/out-pure.nix | 15 +++++---------- test/diff/lists/out.nix | 15 +++++---------- test/diff/monsters_4/out-pure.nix | 21 +++++++-------------- test/diff/monsters_4/out.nix | 21 +++++++-------------- 13 files changed, 52 insertions(+), 98 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index a383446e..aa5c9378 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -177,10 +177,10 @@ prettySet _ (krec, paropen@(LoneAnn _), Items [], parclose@Ann{preTrivia = []}) sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace -- Singleton sets are allowed to fit onto one line, -- but apart from that always expand. -prettySet wide (krec, paropen@Ann{trailComment = post}, binders, parclose) = +prettySet wide (krec, paropen, binders, parclose) = pretty (fmap (,hardspace) krec) - <> pretty (paropen{trailComment = Nothing}) - <> surroundWith sep (nest $ pretty post <> prettyItems binders) + <> pretty paropen + <> surroundWith sep (nest $ prettyItems binders) <> pretty parclose where sep = @@ -223,9 +223,9 @@ prettyTerm (List paropen@Ann{trailComment = Nothing} (Items []) parclose@Ann{pre sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace -- General list -- Always expand if len > 1 -prettyTerm (List paropen@Ann{trailComment = post} items parclose) = - pretty (paropen{trailComment = Nothing}) - <> surroundWith sur (nest $ pretty post <> prettyItems items) +prettyTerm (List paropen items parclose) = + pretty paropen + <> surroundWith sur (nest $ prettyItems items) <> pretty parclose where -- If the brackets are on different lines, keep them like that diff --git a/test/diff/attr_set/out-pure.nix b/test/diff/attr_set/out-pure.nix index 27fb559e..3a51754f 100644 --- a/test/diff/attr_set/out-pure.nix +++ b/test/diff/attr_set/out-pure.nix @@ -1,7 +1,6 @@ [ { } - { - # a + { # a } { a = 1; } { a = 1; } @@ -21,12 +20,10 @@ { b = 1; # c } - { - # a + { # a b = 1; } - { - # a + { # a b = 1; # c } @@ -34,12 +31,10 @@ rec { c = 1; # d } - rec { - # b + rec { # b c = 1; } - rec { - # b + rec { # b c = 1; # d } rec # a @@ -51,13 +46,11 @@ c = 1; # d } rec # a - { - # b + { # b c = 1; } rec # a - { - # b + { # b c = 1; # d } diff --git a/test/diff/attr_set/out.nix b/test/diff/attr_set/out.nix index 403fab2b..ff935444 100644 --- a/test/diff/attr_set/out.nix +++ b/test/diff/attr_set/out.nix @@ -1,7 +1,6 @@ [ { } - { - # a + { # a } { a = 1; } { @@ -24,12 +23,10 @@ { b = 1; # c } - { - # a + { # a b = 1; } - { - # a + { # a b = 1; # c } @@ -37,12 +34,10 @@ rec { c = 1; # d } - rec { - # b + rec { # b c = 1; } - rec { - # b + rec { # b c = 1; # d } rec # a @@ -54,13 +49,11 @@ c = 1; # d } rec # a - { - # b + { # b c = 1; } rec # a - { - # b + { # b c = 1; # d } diff --git a/test/diff/comment/out-pure.nix b/test/diff/comment/out-pure.nix index c8d87b6c..d3dcf730 100644 --- a/test/diff/comment/out-pure.nix +++ b/test/diff/comment/out-pure.nix @@ -103,8 +103,7 @@ ``` */ - [ - # 1 + [ # 1 #2 a # 3 b @@ -121,8 +120,7 @@ a = 123; # comment } - { - # 1 + { # 1 #2 a = 1; # 3 b = 1; diff --git a/test/diff/comment/out.nix b/test/diff/comment/out.nix index c8d87b6c..d3dcf730 100644 --- a/test/diff/comment/out.nix +++ b/test/diff/comment/out.nix @@ -103,8 +103,7 @@ ``` */ - [ - # 1 + [ # 1 #2 a # 3 b @@ -121,8 +120,7 @@ a = 123; # comment } - { - # 1 + { # 1 #2 a = 1; # 3 b = 1; diff --git a/test/diff/idioms_lib_4/out-pure.nix b/test/diff/idioms_lib_4/out-pure.nix index c1ed4245..f1b0f0d6 100644 --- a/test/diff/idioms_lib_4/out-pure.nix +++ b/test/diff/idioms_lib_4/out-pure.nix @@ -588,8 +588,7 @@ rec { families = { }; }; } - // { - # aliases + // { # aliases # 'darwin' is the kernel for all of them. We choose macOS by default. darwin = kernels.macos; watchos = kernels.ios; diff --git a/test/diff/idioms_lib_4/out.nix b/test/diff/idioms_lib_4/out.nix index c1ed4245..f1b0f0d6 100644 --- a/test/diff/idioms_lib_4/out.nix +++ b/test/diff/idioms_lib_4/out.nix @@ -588,8 +588,7 @@ rec { families = { }; }; } - // { - # aliases + // { # aliases # 'darwin' is the kernel for all of them. We choose macOS by default. darwin = kernels.macos; watchos = kernels.ios; diff --git a/test/diff/idioms_pkgs_4/out-pure.nix b/test/diff/idioms_pkgs_4/out-pure.nix index c6d7ea72..d0d54663 100644 --- a/test/diff/idioms_pkgs_4/out-pure.nix +++ b/test/diff/idioms_pkgs_4/out-pure.nix @@ -163,8 +163,7 @@ in cc = let nativePrefix = - { - # switch + { # switch i686-solaris = "/usr/gnu"; x86_64-solaris = "/opt/local/gcc47"; } diff --git a/test/diff/idioms_pkgs_4/out.nix b/test/diff/idioms_pkgs_4/out.nix index 87861127..db9b6de8 100644 --- a/test/diff/idioms_pkgs_4/out.nix +++ b/test/diff/idioms_pkgs_4/out.nix @@ -167,8 +167,7 @@ in cc = let nativePrefix = - { - # switch + { # switch i686-solaris = "/usr/gnu"; x86_64-solaris = "/opt/local/gcc47"; } diff --git a/test/diff/lists/out-pure.nix b/test/diff/lists/out-pure.nix index 6bf6b5ae..287ee538 100644 --- a/test/diff/lists/out-pure.nix +++ b/test/diff/lists/out-pure.nix @@ -23,8 +23,7 @@ foo2 = "barbar"; } ] - { - # List in attrset with comment + { # List in attrset with comment imports0 = [ ]; @@ -66,23 +65,19 @@ b # c d # e ] - [ - # a + [ # a b d ] - [ - # a + [ # a b d # e ] - [ - # a + [ # a b # c d ] - [ - # a + [ # a b # c d # e ] diff --git a/test/diff/lists/out.nix b/test/diff/lists/out.nix index 6798279b..5bd2e9e1 100644 --- a/test/diff/lists/out.nix +++ b/test/diff/lists/out.nix @@ -24,8 +24,7 @@ foo2 = "barbar"; } ] - { - # List in attrset with comment + { # List in attrset with comment imports0 = [ ]; @@ -69,23 +68,19 @@ b # c d # e ] - [ - # a + [ # a b d ] - [ - # a + [ # a b d # e ] - [ - # a + [ # a b # c d ] - [ - # a + [ # a b # c d # e ] diff --git a/test/diff/monsters_4/out-pure.nix b/test/diff/monsters_4/out-pure.nix index ac311ac3..2003c1ad 100644 --- a/test/diff/monsters_4/out-pure.nix +++ b/test/diff/monsters_4/out-pure.nix @@ -34,8 +34,7 @@ }: # Foo stdenv.mkDerivation # Foo rec # Foo - { - # Foo + { # Foo pname # Foo = # Foo "contrast"; # Foo @@ -45,8 +44,7 @@ stdenv.mkDerivation # Foo src # Foo = # Foo # Foo - fetchFromGitLab { - # Foo + fetchFromGitLab { # Foo domain # Foo = # Foo "gitlab.gnome.org"; # Foo @@ -69,8 +67,7 @@ stdenv.mkDerivation # Foo cargoDeps # Foo = # Foo rustPlatform.fetchCargoTarball # Foo - { - # Foo + { # Foo inherit # Foo src ; # Foo @@ -83,8 +80,7 @@ stdenv.mkDerivation # Foo }; # Foo nativeBuildInputs # Foo = # Foo - [ - # Foo + [ # Foo desktop-file-utils # Foo gettext # Foo meson # Foo @@ -100,8 +96,7 @@ stdenv.mkDerivation # Foo ]; # Foo buildInputs # Foo = # Foo - [ - # Foo + [ # Foo cairo # Foo glib # Foo gtk4 # Foo @@ -120,8 +115,7 @@ stdenv.mkDerivation # Foo = # Foo with # Foo lib; # Foo - { - # Foo + { # Foo description # Foo = # Foo "Checks whether the contrast between two colors meet the WCAG requirements"; # Foo @@ -135,8 +129,7 @@ stdenv.mkDerivation # Foo = # Foo with # Foo maintainers; # Foo - [ - # Foo + [ # Foo jtojnar # Foo ]; # Foo platforms # Foo diff --git a/test/diff/monsters_4/out.nix b/test/diff/monsters_4/out.nix index ac311ac3..2003c1ad 100644 --- a/test/diff/monsters_4/out.nix +++ b/test/diff/monsters_4/out.nix @@ -34,8 +34,7 @@ }: # Foo stdenv.mkDerivation # Foo rec # Foo - { - # Foo + { # Foo pname # Foo = # Foo "contrast"; # Foo @@ -45,8 +44,7 @@ stdenv.mkDerivation # Foo src # Foo = # Foo # Foo - fetchFromGitLab { - # Foo + fetchFromGitLab { # Foo domain # Foo = # Foo "gitlab.gnome.org"; # Foo @@ -69,8 +67,7 @@ stdenv.mkDerivation # Foo cargoDeps # Foo = # Foo rustPlatform.fetchCargoTarball # Foo - { - # Foo + { # Foo inherit # Foo src ; # Foo @@ -83,8 +80,7 @@ stdenv.mkDerivation # Foo }; # Foo nativeBuildInputs # Foo = # Foo - [ - # Foo + [ # Foo desktop-file-utils # Foo gettext # Foo meson # Foo @@ -100,8 +96,7 @@ stdenv.mkDerivation # Foo ]; # Foo buildInputs # Foo = # Foo - [ - # Foo + [ # Foo cairo # Foo glib # Foo gtk4 # Foo @@ -120,8 +115,7 @@ stdenv.mkDerivation # Foo = # Foo with # Foo lib; # Foo - { - # Foo + { # Foo description # Foo = # Foo "Checks whether the contrast between two colors meet the WCAG requirements"; # Foo @@ -135,8 +129,7 @@ stdenv.mkDerivation # Foo = # Foo with # Foo maintainers; # Foo - [ - # Foo + [ # Foo jtojnar # Foo ]; # Foo platforms # Foo