From 1142022f46ec713b325d7cb677d4c4ff1e66d1c3 Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 18 Apr 2023 23:46:10 +0200 Subject: [PATCH] Force-expand lists with more than one item --- src/Nixfmt/Pretty.hs | 14 ++++++++----- test/diff/idioms_lib_2/out.nix | 21 ++++++++++---------- test/diff/idioms_lib_3/out.nix | 34 ++++++++++++++++++++++++++------ test/diff/idioms_nixos_1/out.nix | 15 +++++++++++--- test/diff/idioms_pkgs_3/out.nix | 15 +++++++++++--- test/diff/let_in/out.nix | 6 +++++- test/diff/lists/out.nix | 5 ++++- test/diff/monsters_3/out.nix | 8 +++++++- test/diff/pattern/out.nix | 6 +++++- test/diff/with/out.nix | 8 +++++++- 10 files changed, 100 insertions(+), 32 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 707f49a4..16557ff8 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -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 @@ -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 diff --git a/test/diff/idioms_lib_2/out.nix b/test/diff/idioms_lib_2/out.nix index 93264ab6..a265b747 100644 --- a/test/diff/idioms_lib_2/out.nix +++ b/test/diff/idioms_lib_2/out.nix @@ -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`. @@ -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; diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index eb1a8c24..b334af1a 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -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 @@ -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 @@ -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: @@ -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 diff --git a/test/diff/idioms_nixos_1/out.nix b/test/diff/idioms_nixos_1/out.nix index 1abee780..a16b6963 100644 --- a/test/diff/idioms_nixos_1/out.nix +++ b/test/diff/idioms_nixos_1/out.nix @@ -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 @@ -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 ]; diff --git a/test/diff/idioms_pkgs_3/out.nix b/test/diff/idioms_pkgs_3/out.nix index 1abee780..a16b6963 100644 --- a/test/diff/idioms_pkgs_3/out.nix +++ b/test/diff/idioms_pkgs_3/out.nix @@ -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 @@ -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 ]; diff --git a/test/diff/let_in/out.nix b/test/diff/let_in/out.nix index 65cc06c0..46ac00d4 100644 --- a/test/diff/let_in/out.nix +++ b/test/diff/let_in/out.nix @@ -32,6 +32,10 @@ let # e in f; - a = let in [ 1 2 ]; + a = let + in [ + 1 + 2 + ]; in a diff --git a/test/diff/lists/out.nix b/test/diff/lists/out.nix index 173cace2..83a2a53f 100644 --- a/test/diff/lists/out.nix +++ b/test/diff/lists/out.nix @@ -3,7 +3,10 @@ [ 1 ] - [ b d ] + [ + b + d + ] [ b d # e diff --git a/test/diff/monsters_3/out.nix b/test/diff/monsters_3/out.nix index 0500dce3..0f66f531 100644 --- a/test/diff/monsters_3/out.nix +++ b/test/diff/monsters_3/out.nix @@ -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 diff --git a/test/diff/pattern/out.nix b/test/diff/pattern/out.nix index 39326a69..27875c1b 100644 --- a/test/diff/pattern/out.nix +++ b/test/diff/pattern/out.nix @@ -26,7 +26,11 @@ }@inp: _) ({ - a ? [ 1 2 3 ], + a ? [ + 1 + 2 + 3 + ], b ? { # ... } diff --git a/test/diff/with/out.nix b/test/diff/with/out.nix index a94e480c..8eb76a24 100644 --- a/test/diff/with/out.nix +++ b/test/diff/with/out.nix @@ -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 + ]); + } ]