Skip to content

Commit

Permalink
Force-expand lists with more than one item
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed May 5, 2023
1 parent ec4fea5 commit 1142022
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 32 deletions.
14 changes: 9 additions & 5 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,25 @@ prettyTerm (String s) = pretty s
prettyTerm (Path p) = pretty p
prettyTerm (Selection term selectors) = pretty term <> hcat selectors

-- Empty list
prettyTerm (List (Ann paropen Nothing []) [] parclose)
= pretty paropen <> hardspace <> pretty parclose

-- Singleton list
prettyTerm (List (Ann paropen Nothing []) [item] parclose)
| isAbsorbable item
= pretty paropen <> pretty item <> pretty parclose
= pretty paropen <> hardspace <> pretty item <> hardspace <> pretty parclose

-- General list
prettyTerm (List (Ann paropen trailing leading) items parclose)
= base $ pretty paropen <> pretty trailing <> line
<> nest 2 (pretty leading <> sepBy line (map group items)) <> line
= base $ pretty paropen <> pretty trailing <> hardline
<> nest 2 (pretty leading <> sepBy hardline (map group items)) <> hardline
<> pretty parclose

-- Empty, non-recursive attribute set
prettyTerm (Set Nothing (Ann paropen Nothing []) [] parclose)
= pretty paropen <> hardspace <> pretty parclose

-- General set
prettyTerm (Set krec (Ann paropen trailing leading) binders parclose)
= base $ pretty (fmap ((<>hardspace) . pretty) krec)
<> pretty paropen <> pretty trailing <> line
Expand Down Expand Up @@ -160,7 +164,7 @@ isAbsorbable :: Term -> Bool
isAbsorbable (String (Ann parts@(_:_:_) _ _))
= not $ isSimpleString parts
isAbsorbable (Set _ _ (_:_) _) = True
isAbsorbable (List (Ann _ Nothing []) [item] _) = isAbsorbable item
isAbsorbable (List (Ann _ Nothing []) [_item] _) = True
isAbsorbable (Parenthesized (Ann _ Nothing []) (Term t) _) = isAbsorbable t
isAbsorbable (List _ (_:_:_) _) = True
isAbsorbable _ = False
Expand Down
21 changes: 11 additions & 10 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,16 @@ rec {
Type: string -> a -> a
*/
warn =
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.")
else
msg: builtins.trace "warning: ${msg}";
warn = 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.")
else
msg: builtins.trace "warning: ${msg}";

/* Like warn, but only warn when the first argument is `true`.
Expand Down Expand Up @@ -433,9 +436,7 @@ rec {
toBaseDigits = base: i:
let
go = i:
if i < base then
[ i ]
else
if i < base then [ i ] else
let
r = i - ((i / base) * base);
q = (i - r) / base;
Expand Down
34 changes: 28 additions & 6 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ in rec {
# For more examples see the test cases in ./tests/misc.nix.
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 @@ -173,7 +177,11 @@ in rec {
# the part in `sections`.
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 @@ -265,7 +273,12 @@ in 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 = depth:
Expand Down Expand Up @@ -328,9 +341,18 @@ in 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 = let
Expand Down
15 changes: 12 additions & 3 deletions test/diff/idioms_nixos_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ in {
boot.initrd.availableKernelModules = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "sata_nv" "ext3" ];
example = [
"sata_nv"
"ext3"
];
description = ''
The set of kernel modules in the initial ramdisk used during the
boot process. This set must include all modules necessary for
Expand Down Expand Up @@ -276,12 +279,18 @@ 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" ];
++ optionals config.boot.vesa [
"vga=0x317"
"nomodeset"
];

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

boot.kernelModules = [ "loop" "atkbd" ];
boot.kernelModules = [
"loop"
"atkbd"
];

# The Linux kernel >= 2.6.27 provides firmware.
hardware.firmware = [ kernel ];
Expand Down
15 changes: 12 additions & 3 deletions test/diff/idioms_pkgs_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ in {
boot.initrd.availableKernelModules = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "sata_nv" "ext3" ];
example = [
"sata_nv"
"ext3"
];
description = ''
The set of kernel modules in the initial ramdisk used during the
boot process. This set must include all modules necessary for
Expand Down Expand Up @@ -276,12 +279,18 @@ 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" ];
++ optionals config.boot.vesa [
"vga=0x317"
"nomodeset"
];

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

boot.kernelModules = [ "loop" "atkbd" ];
boot.kernelModules = [
"loop"
"atkbd"
];

# The Linux kernel >= 2.6.27 provides firmware.
hardware.firmware = [ kernel ];
Expand Down
6 changes: 5 additions & 1 deletion test/diff/let_in/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ let
# e
in f;

a = let in [ 1 2 ];
a = let
in [
1
2
];

in a
5 changes: 4 additions & 1 deletion test/diff/lists/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

[ 1 ]

[ b d ]
[
b
d
]
[
b
d # e
Expand Down
8 changes: 7 additions & 1 deletion test/diff/monsters_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ stdenv.mkDerivation rec {
wrapGAppsHook4
glib # for glib-compile-resources
];
buildInputs = [ cairo glib gtk4 libadwaita pango ];
buildInputs = [
cairo
glib
gtk4
libadwaita
pango
];
postPatch = ''
patchShebangs build-aux/meson_post_install.py
# https://gitlab.gnome.org/World/design/contrast/-/merge_requests/23
Expand Down
6 changes: 5 additions & 1 deletion test/diff/pattern/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
}@inp:
_)
({
a ? [ 1 2 3 ],
a ? [
1
2
3
],
b ? {
# ...
}
Expand Down
8 changes: 7 additions & 1 deletion test/diff/with/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
b = 2;
})
{ a = with b; with b; with b; 1; }
{ binPath = with pkgs; makeBinPath ([ rsync util-linux ]); }
{
binPath = with pkgs;
makeBinPath ([
rsync
util-linux
]);
}
]

0 comments on commit 1142022

Please sign in to comment.