Skip to content

Commit

Permalink
Don't expand final list arguments of functions
Browse files Browse the repository at this point in the history
Applies 331e0aa to last arguments too
  • Loading branch information
infinisil committed Sep 17, 2024
1 parent 5ff2b12 commit 228f526
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 153 deletions.
4 changes: 4 additions & 0 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ prettyApp indentFunction pre hasPost f a =

-- Render the last argument of a function call
absorbLast :: Expression -> Doc
-- If the list is simple, try to render it single-line instead of expanding
absorbLast (Term (List paropen items parclose))
| isSimpleItems items =
group' Priority $ nest $ prettyList line paropen items parclose
absorbLast (Term t)
| isAbsorbable t =
group' Priority $ nest $ prettyTerm t
Expand Down
11 changes: 2 additions & 9 deletions test/diff/apply/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,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/attr_set/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@
]
++ (if foo then [ bar ] else [ baz ])
++ [ ]
++ (optionals condition [
more
items
]);
++ (optionals condition [ more items ]);
b = with pkgs; [
a
lot
Expand Down
5 changes: 1 addition & 4 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
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
11 changes: 2 additions & 9 deletions test/diff/idioms_lib_3/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,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
11 changes: 2 additions & 9 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,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
8 changes: 1 addition & 7 deletions test/diff/idioms_lib_4/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,7 @@ rec {
"3" =
# cpu-kernel-environment
if
elemAt l 1 == "linux"
|| elem (elemAt l 2) [
"eabi"
"eabihf"
"elf"
"gnu"
]
elemAt l 1 == "linux" || elem (elemAt l 2) [ "eabi" "eabihf" "elf" "gnu" ]
then
{
cpu = elemAt l 0;
Expand Down
8 changes: 1 addition & 7 deletions test/diff/idioms_lib_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,7 @@ rec {
"3" =
# cpu-kernel-environment
if
elemAt l 1 == "linux"
|| elem (elemAt l 2) [
"eabi"
"eabihf"
"elf"
"gnu"
]
elemAt l 1 == "linux" || elem (elemAt l 2) [ "eabi" "eabihf" "elf" "gnu" ]
then
{
cpu = elemAt l 0;
Expand Down
9 changes: 3 additions & 6 deletions test/diff/idioms_nixos_1/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,9 @@ in

# Implement consoleLogLevel both in early boot and using sysctl
# (so you don't need to reboot to have changes take effect).
boot.kernelParams =
[ "loglevel=${toString config.boot.consoleLogLevel}" ]
++ optionals config.boot.vesa [
"vga=0x317"
"nomodeset"
];
boot.kernelParams = [
"loglevel=${toString config.boot.consoleLogLevel}"
] ++ optionals config.boot.vesa [ "vga=0x317" "nomodeset" ];

boot.kernel.sysctl."kernel.printk" = mkDefault config.boot.consoleLogLevel;

Expand Down
9 changes: 3 additions & 6 deletions test/diff/idioms_nixos_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,9 @@ in

# Implement consoleLogLevel both in early boot and using sysctl
# (so you don't need to reboot to have changes take effect).
boot.kernelParams =
[ "loglevel=${toString config.boot.consoleLogLevel}" ]
++ optionals config.boot.vesa [
"vga=0x317"
"nomodeset"
];
boot.kernelParams = [
"loglevel=${toString config.boot.consoleLogLevel}"
] ++ optionals config.boot.vesa [ "vga=0x317" "nomodeset" ];

boot.kernel.sysctl."kernel.printk" = mkDefault config.boot.consoleLogLevel;

Expand Down
20 changes: 3 additions & 17 deletions test/diff/idioms_nixos_2/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,7 @@ in
description = lib.mdDoc "Log level value between 0 (DEBUG) and 4 (FATAL).";
};
logType = mkOption {
type = types.enum [
"errorlog"
"file"
"syslog"
"systemd"
];
type = types.enum [ "errorlog" "file" "syslog" "systemd" ];
default = "syslog";
description = lib.mdDoc ''
Logging backend to use.
Expand Down Expand Up @@ -358,11 +353,7 @@ in

config = {
dbtype = mkOption {
type = types.enum [
"sqlite"
"pgsql"
"mysql"
];
type = types.enum [ "sqlite" "pgsql" "mysql" ];
default = "sqlite";
description = lib.mdDoc "Database type.";
};
Expand Down Expand Up @@ -436,12 +427,7 @@ in
};

overwriteProtocol = mkOption {
type = types.nullOr (
types.enum [
"http"
"https"
]
);
type = types.nullOr (types.enum [ "http" "https" ]);
default = null;
example = "https";

Expand Down
20 changes: 3 additions & 17 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ in
description = lib.mdDoc "Log level value between 0 (DEBUG) and 4 (FATAL).";
};
logType = mkOption {
type = types.enum [
"errorlog"
"file"
"syslog"
"systemd"
];
type = types.enum [ "errorlog" "file" "syslog" "systemd" ];
default = "syslog";
description = lib.mdDoc ''
Logging backend to use.
Expand Down Expand Up @@ -361,11 +356,7 @@ in

config = {
dbtype = mkOption {
type = types.enum [
"sqlite"
"pgsql"
"mysql"
];
type = types.enum [ "sqlite" "pgsql" "mysql" ];
default = "sqlite";
description = lib.mdDoc "Database type.";
};
Expand Down Expand Up @@ -439,12 +430,7 @@ in
};

overwriteProtocol = mkOption {
type = types.nullOr (
types.enum [
"http"
"https"
]
);
type = types.nullOr (types.enum [ "http" "https" ]);
default = null;
example = "https";

Expand Down
10 changes: 2 additions & 8 deletions test/diff/idioms_pkgs_3/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ buildStdenv.mkDerivation ({
which
wrapGAppsHook
]
++ lib.optionals crashreporterSupport [
dump_syms
patchelf
]
++ lib.optionals crashreporterSupport [ dump_syms patchelf ]
++ lib.optionals pgoSupport [ xvfb-run ]
++ extraNativeBuildInputs;

Expand Down Expand Up @@ -484,10 +481,7 @@ buildStdenv.mkDerivation ({
++ lib.optional pulseaudioSupport libpulseaudio # only headers are needed
++ lib.optional sndioSupport sndio
++ lib.optional gssSupport libkrb5
++ lib.optionals waylandSupport [
libxkbcommon
libdrm
]
++ lib.optionals waylandSupport [ libxkbcommon libdrm ]
++ lib.optional jemallocSupport jemalloc
++ extraBuildInputs;

Expand Down
10 changes: 2 additions & 8 deletions test/diff/idioms_pkgs_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ buildStdenv.mkDerivation ({
which
wrapGAppsHook
]
++ lib.optionals crashreporterSupport [
dump_syms
patchelf
]
++ lib.optionals crashreporterSupport [ dump_syms patchelf ]
++ lib.optionals pgoSupport [ xvfb-run ]
++ extraNativeBuildInputs;

Expand Down Expand Up @@ -486,10 +483,7 @@ buildStdenv.mkDerivation ({
++ lib.optional pulseaudioSupport libpulseaudio # only headers are needed
++ lib.optional sndioSupport sndio
++ lib.optional gssSupport libkrb5
++ lib.optionals waylandSupport [
libxkbcommon
libdrm
]
++ lib.optionals waylandSupport [ libxkbcommon libdrm ]
++ lib.optional jemallocSupport jemalloc
++ extraBuildInputs;

Expand Down
14 changes: 4 additions & 10 deletions test/diff/idioms_pkgs_5/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,10 @@ let
# Including it then would cause needless mass rebuilds.
#
# TODO(@Ericson2314): Make [ "build" "host" ] always the default / resolve #87909
configurePlatforms ?
optionals
(
stdenv.hostPlatform != stdenv.buildPlatform
|| config.configurePlatformsByDefault
)
[
"build"
"host"
],
configurePlatforms ? optionals (
stdenv.hostPlatform != stdenv.buildPlatform
|| config.configurePlatformsByDefault
) [ "build" "host" ],

# TODO(@Ericson2314): Make unconditional / resolve #33599
# Check phase
Expand Down
14 changes: 4 additions & 10 deletions test/diff/idioms_pkgs_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,10 @@ let
# Including it then would cause needless mass rebuilds.
#
# TODO(@Ericson2314): Make [ "build" "host" ] always the default / resolve #87909
configurePlatforms ?
optionals
(
stdenv.hostPlatform != stdenv.buildPlatform
|| config.configurePlatformsByDefault
)
[
"build"
"host"
],
configurePlatforms ? optionals (
stdenv.hostPlatform != stdenv.buildPlatform
|| config.configurePlatformsByDefault
) [ "build" "host" ],

# TODO(@Ericson2314): Make unconditional / resolve #33599
# Check phase
Expand Down
5 changes: 1 addition & 4 deletions test/diff/operation/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@
]
++ (if foo then [ bar ] else [ baz ])
++ [ ]
++ (optionals condition [
more
items
])
++ (optionals condition [ more items ])
)

# Test precedence
Expand Down
5 changes: 1 addition & 4 deletions test/diff/operation/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@
]
++ (if foo then [ bar ] else [ baz ])
++ [ ]
++ (optionals condition [
more
items
])
++ (optionals condition [ more items ])
)

# Test precedence
Expand Down

0 comments on commit 228f526

Please sign in to comment.