Skip to content

Commit

Permalink
application: Special case list arguments 2/2
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Oct 14, 2024
1 parent e541895 commit e8dd22b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 84 deletions.
16 changes: 12 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions test/diff/apply/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@
''"''
"\${"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
"''\${"
"'''"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
test =
foo
[
Expand Down
57 changes: 23 additions & 34 deletions test/diff/apply_with_lists/out-pure.nix
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
)
Expand All @@ -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"
Expand All @@ -76,12 +72,7 @@
"sddm"
"enable"
]
[
"services"
"displayManager"
"sddm"
"enable"
]
[ "services" "displayManager" "sddm" "enable" ]
)
(map (
buildAllowCommand "allow" [
Expand Down Expand Up @@ -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 ]
Expand Down
57 changes: 23 additions & 34 deletions test/diff/apply_with_lists/out.nix
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
)
Expand All @@ -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"
Expand All @@ -76,12 +72,7 @@
"sddm"
"enable"
]
[
"services"
"displayManager"
"sddm"
"enable"
]
[ "services" "displayManager" "sddm" "enable" ]
)
(map (
buildAllowCommand "allow" [
Expand Down Expand Up @@ -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 ]
Expand Down
5 changes: 1 addition & 4 deletions test/diff/idioms_lib_3/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,7 @@ rec {
''"''
"\${"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
"''\${"
"'''"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
singlelineResult =
''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"'';
multilineResult =
Expand Down
5 changes: 1 addition & 4 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,7 @@ rec {
''"''
"\${"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
"''\${"
"'''"
];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
singlelineResult =
''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"'';
multilineResult =
Expand Down

0 comments on commit e8dd22b

Please sign in to comment.