From 395e0ff6a0518a4ce9d24443e69b207922c00737 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 3 Oct 2024 10:52:05 +0200 Subject: [PATCH] [wip] application: Special case list arguments 2/2 Commit still needs to be rewritten in a cleaner form --- src/Nixfmt/Pretty.hs | 32 ++++++++++++----- test/diff/apply/out-pure.nix | 5 +-- test/diff/apply_with_lists/out-pure.nix | 47 +++++++++++-------------- test/diff/apply_with_lists/out.nix | 47 +++++++++++-------------- test/diff/idioms_lib_3/out-pure.nix | 5 +-- test/diff/idioms_lib_3/out.nix | 5 +-- 6 files changed, 66 insertions(+), 75 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index e96aee84..f97c3584 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -457,16 +457,30 @@ prettyApp indentFunction pre hasPost f a = ((\a'@Ann{preTrivia} -> (a'{preTrivia = []}, preTrivia)) . moveTrailingCommentUp) f - renderedF = pre <> group' Transparent (absorbApp fWithoutComment) - renderedFUnexpanded = unexpandSpacing' Nothing renderedF - post = if hasPost then line' else mempty - in pretty comment' - <> ( if isSimple (Application f a) && isJust renderedFUnexpanded - then group' RegularG $ fromJust renderedFUnexpanded <> hardspace <> absorbLast a - else group' RegularG $ renderedF <> line <> absorbLast a <> post - ) - <> (if hasPost && not (null comment') then hardline else mempty) + in + case (f, a) of + (Application _ (Term List{}), Term List{}) -> + let + app = Application fWithoutComment a + renderedF = pre <> group' Transparent (absorbApp app) + renderedFUnexpanded = unexpandSpacing' Nothing renderedF + in pretty comment' + <> ( if isSimple app && isJust renderedFUnexpanded + then group' RegularG $ fromJust renderedFUnexpanded + else group' RegularG $ renderedF <> post + ) + <> (if hasPost && not (null comment') then hardline else mempty) + _ -> + let + renderedF = pre <> group' Transparent (absorbApp fWithoutComment) + renderedFUnexpanded = unexpandSpacing' Nothing renderedF + in pretty comment' + <> ( if isSimple (Application f a) && isJust renderedFUnexpanded + then group' RegularG $ fromJust renderedFUnexpanded <> hardspace <> absorbLast a + else group' RegularG $ renderedF <> line <> absorbLast a <> post + ) + <> (if hasPost && not (null comment') then hardline else mempty) prettyWith :: Bool -> Expression -> Doc -- absorb the body 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 1397a49f..b9d32211 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,10 +60,7 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) (mkRenamedOptionModule [ "services" @@ -71,12 +69,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix index 1397a49f..b9d32211 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,10 +60,7 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) (mkRenamedOptionModule [ "services" @@ -71,12 +69,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ 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 =