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 1fde293 commit ec04379
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 78 deletions.
21 changes: 17 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,23 @@ 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 (fWithoutComment, a) of
-- When the two last arguments are lists, render these specially (same as above)
-- Also no need to wrap in renderSimple here, because we know that these kinds of arguments
-- are never "simple" by definition.
(Application fWithoutCommandAndWithoutArg l1@(Term List{}), l2@(Term List{})) ->
group' RegularG $
(pre <> group' Transparent (absorbApp fWithoutCommandAndWithoutArg))
<> line
<> nest (group (absorbInner l1))
<> line
<> nest (group (absorbInner l2))
<> 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
55 changes: 24 additions & 31 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,12 @@
(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 +74,7 @@
"sddm"
"enable"
]
[
"services"
"displayManager"
"sddm"
"enable"
]
[ "services" "displayManager" "sddm" "enable" ]
)
(map (
buildAllowCommand "allow" [
Expand Down
55 changes: 24 additions & 31 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,12 @@
(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 +74,7 @@
"sddm"
"enable"
]
[
"services"
"displayManager"
"sddm"
"enable"
]
[ "services" "displayManager" "sddm" "enable" ]
)
(map (
buildAllowCommand "allow" [
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 ec04379

Please sign in to comment.