From e8dd22b236d56518bdf8da119c8f642a91b9dca7 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 3 Oct 2024 11:22:56 +0200 Subject: [PATCH] application: Special case list arguments 2/2 --- src/Nixfmt/Pretty.hs | 16 +++++-- test/diff/apply/out-pure.nix | 5 +-- test/diff/apply_with_lists/out-pure.nix | 57 ++++++++++--------------- test/diff/apply_with_lists/out.nix | 57 ++++++++++--------------- test/diff/idioms_lib_3/out-pure.nix | 5 +-- test/diff/idioms_lib_3/out.nix | 5 +-- 6 files changed, 61 insertions(+), 84 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index f7cfc353..8dc68a35 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -469,10 +469,18 @@ prettyApp indentFunction pre hasPost f a = post = if hasPost then line' else mempty in pretty comment' - <> renderSimple - (absorbApp fWithoutComment) - (\fRendered -> group' RegularG $ fRendered <> hardspace <> absorbLast a) - (\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post) + <> case (f, a) of + -- When the two last arguments are lists, don't absorb the last one (absorbLast) + (Application _ (Term List{}), Term List{}) -> + renderSimple + (absorbApp (Application fWithoutComment a)) + (\fRendered -> group' RegularG $ fRendered) + (\fRendered -> group' RegularG $ fRendered <> post) + _ -> + renderSimple + (absorbApp fWithoutComment) + (\fRendered -> group' RegularG $ fRendered <> hardspace <> absorbLast a) + (\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post) <> (if hasPost && not (null comment') then hardline else mempty) prettyWith :: Bool -> Expression -> Doc diff --git a/test/diff/apply/out-pure.nix b/test/diff/apply/out-pure.nix index 99114616..9db9ebca 100644 --- a/test/diff/apply/out-pure.nix +++ b/test/diff/apply/out-pure.nix @@ -160,10 +160,7 @@ ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; test = foo [ diff --git a/test/diff/apply_with_lists/out-pure.nix b/test/diff/apply_with_lists/out-pure.nix index 176b4587..c7f142aa 100644 --- a/test/diff/apply_with_lists/out-pure.nix +++ b/test/diff/apply_with_lists/out-pure.nix @@ -1,20 +1,15 @@ # This file contains an assortment of test cases involving list-heavy function calls [ - (f [ ] [ - rhs - lhs - ]) + (f [ ] [ rhs lhs ]) (lib.mkMerge [ false false ]) - (replaceStrings [ "\${" "''" ] + (replaceStrings + [ "\${" "''" ] #force multiline - [ - "''\${" - "'''" - ] + [ "''\${" "'''" ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) (replaceStrings @@ -39,14 +34,20 @@ "" ] ) - (lists.removePrefix [ - 1 - 2 - ] [ ]) - (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ - 1 - 2 - ] [ ]) + (lists.removePrefix + [ + 1 + 2 + ] + [ ] + ) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [ + 1 + 2 + ] + [ ] + ) (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] (builtins.readFile ./assert_uki_repart_match.py) ) @@ -59,15 +60,10 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) # This line is engineered to exactly hit the line length limit - (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ - "hardware" - "graphics" - ]) + (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ "hardware" "graphics" ] + ) (mkRenamedOptionModule [ "services" @@ -76,12 +72,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ @@ -111,9 +102,7 @@ [ "Blue" "Flamingo" "Green" ] [ accent ] lib.checkListOfEnum - "${pname}: color variant" - [ "Latte" "Frappe" "Macchiato" "Mocha" ] - [ variant ] + "${pname}: color variant" [ "Latte" "Frappe" "Macchiato" "Mocha" ] [ variant ] ) (lib.switch [ coq.coq-version ssreflect.version ] diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix index 851779ed..2deef890 100644 --- a/test/diff/apply_with_lists/out.nix +++ b/test/diff/apply_with_lists/out.nix @@ -1,20 +1,15 @@ # This file contains an assortment of test cases involving list-heavy function calls [ - (f [ ] [ - rhs - lhs - ]) + (f [ ] [ rhs lhs ]) (lib.mkMerge [ false false ]) - (replaceStrings [ "\${" "''" ] + (replaceStrings + [ "\${" "''" ] #force multiline - [ - "''\${" - "'''" - ] + [ "''\${" "'''" ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) (replaceStrings @@ -39,14 +34,20 @@ "" ] ) - (lists.removePrefix [ - 1 - 2 - ] [ ]) - (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ - 1 - 2 - ] [ ]) + (lists.removePrefix + [ + 1 + 2 + ] + [ ] + ) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [ + 1 + 2 + ] + [ ] + ) (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] (builtins.readFile ./assert_uki_repart_match.py) ) @@ -59,15 +60,10 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) # This line is engineered to exactly hit the line length limit - (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ - "hardware" - "graphics" - ]) + (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ "hardware" "graphics" ] + ) (mkRenamedOptionModule [ "services" @@ -76,12 +72,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ @@ -115,9 +106,7 @@ ] [ accent ] lib.checkListOfEnum - "${pname}: color variant" - [ "Latte" "Frappe" "Macchiato" "Mocha" ] - [ variant ] + "${pname}: color variant" [ "Latte" "Frappe" "Macchiato" "Mocha" ] [ variant ] ) (lib.switch [ coq.coq-version ssreflect.version ] diff --git a/test/diff/idioms_lib_3/out-pure.nix b/test/diff/idioms_lib_3/out-pure.nix index 387e9df3..2c9c796e 100644 --- a/test/diff/idioms_lib_3/out-pure.nix +++ b/test/diff/idioms_lib_3/out-pure.nix @@ -366,10 +366,7 @@ rec { ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; singlelineResult = ''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"''; multilineResult = diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index 469184e6..53474a65 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -379,10 +379,7 @@ rec { ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; singlelineResult = ''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"''; multilineResult =