Skip to content

Commit

Permalink
[wip] application: Special case list arguments 2/2
Browse files Browse the repository at this point in the history
Commit still needs to be rewritten in a cleaner form
  • Loading branch information
piegamesde committed Oct 3, 2024
1 parent 5d52f90 commit 395e0ff
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 75 deletions.
32 changes: 23 additions & 9 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
47 changes: 20 additions & 27 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,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"
Expand All @@ -71,12 +69,7 @@
"sddm"
"enable"
]
[
"services"
"displayManager"
"sddm"
"enable"
]
[ "services" "displayManager" "sddm" "enable" ]
)
(map (
buildAllowCommand "allow" [
Expand Down
47 changes: 20 additions & 27 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,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"
Expand All @@ -71,12 +69,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 395e0ff

Please sign in to comment.