Skip to content

Commit

Permalink
Don't expand already-contracted lists if they fit on a line
Browse files Browse the repository at this point in the history
This fixes annoying previous expansions of e.g.

    imports = [
      (mkRenamedOptionModule [ "foo" "bar" ] [ "florp" "baz" ])
    ];
  • Loading branch information
infinisil committed Aug 20, 2024
1 parent 14be7e6 commit ac31a9b
Show file tree
Hide file tree
Showing 37 changed files with 187 additions and 956 deletions.
7 changes: 3 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,13 @@ prettyTerm (List paropen@Ann{trailComment = Nothing} (Items []) parclose@Ann{pre
-- If the brackets are on different lines, keep them like that
sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace
-- General list
-- Always expand if len > 1
prettyTerm (List paropen@Ann{trailComment = post} items parclose) =
prettyTerm (List paropen@Ann{trailComment = post} (Items items) parclose) =
pretty (paropen{trailComment = Nothing})
<> surroundWith sur (nest $ pretty post <> prettyItems items)
<> surroundWith sep (nest $ pretty post <> sepBy sep items)
<> pretty parclose
where
-- If the brackets are on different lines, keep them like that
sur = if sourceLine paropen /= sourceLine parclose then hardline else line
sep = if sourceLine paropen /= sourceLine parclose then hardline else line
prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose)
-- Parentheses
prettyTerm (Parenthesized paropen expr parclose@Ann{preTrivia = closePre}) =
Expand Down
10 changes: 1 addition & 9 deletions test/correct/paths-with-interpolations.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,4 @@ let
bar = "bar";

in
[
/${foo}
./${foo}
/foo/${bar}
/foo${bar}
/${foo}/bar
/${foo}bar
/foo${bar}baz
]
[ /${foo} ./${foo} /foo/${bar} /foo${bar} /${foo}/bar /${foo}bar /foo${bar}baz ]
24 changes: 3 additions & 21 deletions test/diff/apply/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@
b = # Function call with comment
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
}
[
(mapAttrsToStringsSep [
force
long
] "\n" mkSection attrsOfAttrs)
]
[ (mapAttrsToStringsSep [ force long ] "\n" mkSection attrsOfAttrs) ]
(a b)
((a b) (a b)
(
Expand Down Expand Up @@ -155,21 +150,8 @@
utils.lib.eachDefaultSystem (system: { });
}
{
escapeSingleline = libStr.escape [
"\\"
''"''
"\${"
];
escapeMultiline =
libStr.replaceStrings
[
"\${"
"''"
]
[
"''\${"
"'''"
];
escapeSingleline = libStr.escape [ "\\" ''"'' "\${" ];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
test =
foo
[
Expand Down
5 changes: 1 addition & 4 deletions test/diff/apply/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
}
[
(mapAttrsToStringsSep [
force
long
] "\n" mkSection attrsOfAttrs)
(mapAttrsToStringsSep [ force long ] "\n" mkSection attrsOfAttrs)
]
(a b)
((a b) (a b)
Expand Down
55 changes: 8 additions & 47 deletions test/diff/attr_set/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,8 @@
]
++ (if foo then [ bar ] else [ baz ])
++ [ ]
++ (optionals condition [
more
items
]);
b = with pkgs; [
a
lot
of
packages
];
++ (optionals condition [ more items ]);
b = with pkgs; [ a lot of packages ];
}
{
systemd.initrdBi = lib.mkIf config.boot.initrd.services.lvm.enable [ pkgs.vdo ];
Expand Down Expand Up @@ -309,56 +301,28 @@
# Parentheses
{
a = ({ });
b = ([
1
2
3
]);
b = ([ 1 2 3 ]);
c = (if null then true else false);
d = (
let
in
[
1
2
3
]
);
e = (
if null then
true
else
[
1
2
3
]
[ 1 2 3 ]
);
e = (if null then true else [ 1 2 3 ]);
# FIXME: This one exposes a really weird bug in the underlying
# pretty printing engine.
# (It's probably the same one that causes weird indentation in
# functions with multiline function)
# f = /* comment */ (if null then true else [ 1 2 3 ]);

a = (with a; { });
b = (
with a;
[
1
2
3
]
);
b = (with a; [ 1 2 3 ]);
c = (with a; if null then true else false);
d = (
with a;
let
in
[
1
2
3
]
[ 1 2 3 ]
);
}

Expand All @@ -373,9 +337,6 @@
# Sans) than that horror. But we do need the Adobe fonts for some
# old non-fontconfig applications. (Possibly this could be done
# better using a fontconfig rule.)
[
pkgs.xorg.fontadobe100dpi
pkgs.xorg.fontadobe75dpi
];
[ pkgs.xorg.fontadobe100dpi pkgs.xorg.fontadobe75dpi ];
}
]
43 changes: 6 additions & 37 deletions test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@
]
++ (if foo then [ bar ] else [ baz ])
++ [ ]
++ (optionals condition [
more
items
]);
++ (optionals condition [ more items ]);
b = with pkgs; [
a
lot
Expand Down Expand Up @@ -312,56 +309,28 @@
# Parentheses
{
a = ({ });
b = ([
1
2
3
]);
b = ([ 1 2 3 ]);
c = (if null then true else false);
d = (
let
in
[
1
2
3
]
);
e = (
if null then
true
else
[
1
2
3
]
[ 1 2 3 ]
);
e = (if null then true else [ 1 2 3 ]);
# FIXME: This one exposes a really weird bug in the underlying
# pretty printing engine.
# (It's probably the same one that causes weird indentation in
# functions with multiline function)
# f = /* comment */ (if null then true else [ 1 2 3 ]);

a = (with a; { });
b = (
with a;
[
1
2
3
]
);
b = (with a; [ 1 2 3 ]);
c = (with a; if null then true else false);
d = (
with a;
let
in
[
1
2
3
]
[ 1 2 3 ]
);
}

Expand Down
8 changes: 1 addition & 7 deletions test/diff/idioms_lib_2/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,7 @@ rec {
Type: string -> a -> a
*/
warn =
if
lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [
"1"
"true"
"yes"
]
then
if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [ "1" "true" "yes" ] then
msg:
builtins.trace "warning: ${msg}" (
abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors."
Expand Down
8 changes: 1 addition & 7 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,7 @@ rec {
Type: string -> a -> a
*/
warn =
if
lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [
"1"
"true"
"yes"
]
then
if lib.elem (builtins.getEnv "NIX_ABORT_ON_WARN") [ "1" "true" "yes" ] then
msg:
builtins.trace "warning: ${msg}" (
abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors."
Expand Down
40 changes: 5 additions & 35 deletions test/diff/idioms_lib_3/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,7 @@ rec {
toINI =
{
# apply transformations (e.g. escapes) to section names
mkSectionName ? (
name:
libStr.escape [
"["
"]"
] name
),
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
# format a setting line from key and value
mkKeyValue ? mkKeyValueDefault { } "=",
# allow lists as values for duplicate keys
Expand Down Expand Up @@ -191,13 +185,7 @@ rec {
toINIWithGlobalSection =
{
# apply transformations (e.g. escapes) to section names
mkSectionName ? (
name:
libStr.escape [
"["
"]"
] name
),
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
# format a setting line from key and value
mkKeyValue ? mkKeyValueDefault { } "=",
# allow lists as values for duplicate keys
Expand Down Expand Up @@ -292,12 +280,7 @@ rec {
}:
assert builtins.isInt depthLimit;
let
specialAttrs = [
"__functor"
"__functionArgs"
"__toString"
"__pretty"
];
specialAttrs = [ "__functor" "__functionArgs" "__toString" "__pretty" ];
stepIntoAttr =
evalNext: name: if builtins.elem name specialAttrs then id else evalNext;
transform =
Expand Down Expand Up @@ -373,21 +356,8 @@ rec {
else if isString v then
let
lines = filter (v: !isList v) (builtins.split "\n" v);
escapeSingleline = libStr.escape [
"\\"
''"''
"\${"
];
escapeMultiline =
libStr.replaceStrings
[
"\${"
"''"
]
[
"''\${"
"'''"
];
escapeSingleline = libStr.escape [ "\\" ''"'' "\${" ];
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
singlelineResult =
''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"'';
multilineResult =
Expand Down
Loading

0 comments on commit ac31a9b

Please sign in to comment.