From ec4fea595835a4dbe41ba35579f3f5a39553e5e1 Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 18 Apr 2023 23:33:39 +0200 Subject: [PATCH] Rework function declarations --- src/Nixfmt/Pretty.hs | 24 +- test/diff/apply/out.nix | 4 +- test/diff/comment/out.nix | 5 +- test/diff/idioms_lib_2/out.nix | 4 +- test/diff/idioms_lib_3/out.nix | 60 +-- test/diff/idioms_nixos_1/out.nix | 7 +- test/diff/idioms_pkgs_1/out.nix | 7 +- test/diff/idioms_pkgs_2/out.nix | 10 +- test/diff/idioms_pkgs_3/out.nix | 7 +- test/diff/key_value/out.nix | 6 +- test/diff/lambda/out.nix | 29 +- test/diff/monsters_1/out.nix | 125 +++--- test/diff/monsters_3/out.nix | 21 +- test/diff/monsters_4/out.nix | 63 +-- test/diff/monsters_5/out.nix | 8 +- test/diff/pattern/out.nix | 649 +++++++++++++++++++------------ 16 files changed, 642 insertions(+), 387 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index eb4d7200..707f49a4 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -124,27 +124,33 @@ toLeading :: Maybe TrailingComment -> Trivia toLeading Nothing = [] toLeading (Just (TrailingComment c)) = [LineComment (" " <> c)] -prettyComma :: Maybe Leaf -> Doc -prettyComma Nothing = mempty -prettyComma (Just comma) = softline' <> pretty comma <> hardspace - instance Pretty ParamAttr where - pretty (ParamAttr name Nothing comma) - = pretty name <> prettyComma comma + -- Simple parameter + pretty (ParamAttr name Nothing maybeComma) + = pretty name <> (fromMaybe (text ",") (fmap pretty maybeComma)) <> softline + -- With ? default pretty (ParamAttr name (Just (qmark, def)) comma) = group (pretty name <> hardspace <> pretty qmark <> absorb softline mempty (Just 2) def) - <> prettyComma comma + <> pretty comma <> softline + -- ... pretty (ParamEllipsis ellipsis) = pretty ellipsis instance Pretty Parameter where + -- param: pretty (IDParameter i) = pretty i + + -- {}: + pretty (SetParameter bopen [] bclose) + = group $ pretty bopen <> hardspace <> pretty bclose + + -- { stuff }: pretty (SetParameter bopen attrs bclose) - = group $ pretty bopen <> hardspace - <> hcat attrs <> softline + = group $ pretty bopen <> hardline + <> nest 2 (sepBy hardline attrs) <> hardline <> pretty bclose pretty (ContextParameter param1 at param2) diff --git a/test/diff/apply/out.nix b/test/diff/apply/out.nix index c14df116..d43eb23d 100644 --- a/test/diff/apply/out.nix +++ b/test/diff/apply/out.nix @@ -56,7 +56,9 @@ } # https://github.com/kamadorueda/alejandra/issues/372#issuecomment-1435083516 { - outputs = { utils }: + outputs = { + utils, + }: # For each supported platform, utils.lib.eachDefaultSystem (system: { }); } diff --git a/test/diff/comment/out.nix b/test/diff/comment/out.nix index 4ce451b9..fe313652 100644 --- a/test/diff/comment/out.nix +++ b/test/diff/comment/out.nix @@ -73,8 +73,9 @@ #7 in d) - ({ a, # comment - b ? 2, # comment + ({ + a, # comment + b ? 2, # comment }: _) ] diff --git a/test/diff/idioms_lib_2/out.nix b/test/diff/idioms_lib_2/out.nix index 50127d5b..93264ab6 100644 --- a/test/diff/idioms_lib_2/out.nix +++ b/test/diff/idioms_lib_2/out.nix @@ -1,4 +1,6 @@ -{ lib }: +{ + lib, +}: rec { diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index bcfd80b8..eb1a8c24 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -12,7 +12,9 @@ # # Tests can be found in ./tests/misc.nix # Documentation in the manual, #sec-generators -{ lib, }: +{ + lib, +}: with (lib).trivial; let libStr = lib.strings; @@ -75,7 +77,9 @@ in rec { # # mkKeyValueDefault {} ":" "f:oo" "bar" # > "f\:oo:bar" - mkKeyValueDefault = { mkValueString ? mkValueStringDefault { } }: + mkKeyValueDefault = { + mkValueString ? mkValueStringDefault { } + }: sep: k: v: "${libStr.escape [ sep ] k}${sep}${mkValueString v}"; @@ -84,8 +88,10 @@ in rec { # Generate a key-value-style config file from an attrset. # # mkKeyValue is the same as in toINI. - toKeyValue = - { mkKeyValue ? mkKeyValueDefault { } "=", listsAsDuplicateKeys ? false }: + toKeyValue = { + mkKeyValue ? mkKeyValueDefault { } "=", + listsAsDuplicateKeys ? false + }: let mkLine = k: v: mkKeyValue k v + "\n"; mkLines = if listsAsDuplicateKeys then @@ -117,11 +123,12 @@ 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), - # format a setting line from key and value - mkKeyValue ? mkKeyValueDefault { } "=", - # allow lists as values for duplicate keys - listsAsDuplicateKeys ? false }: + mkSectionName ? (name: libStr.escape [ "[" "]" ] name), + # format a setting line from key and value + mkKeyValue ? mkKeyValueDefault { } "=", + # allow lists as values for duplicate keys + listsAsDuplicateKeys ? false + }: attrsOfAttrs: let # map function to string for each key val @@ -166,12 +173,16 @@ in rec { # the part in `sections`. toINIWithGlobalSection = { # apply transformations (e.g. escapes) to section names - mkSectionName ? (name: libStr.escape [ "[" "]" ] name), - # format a setting line from key and value - mkKeyValue ? mkKeyValueDefault { } "=", - # allow lists as values for duplicate keys - listsAsDuplicateKeys ? false }: - { globalSection, sections, }: + mkSectionName ? (name: libStr.escape [ "[" "]" ] name), + # format a setting line from key and value + mkKeyValue ? mkKeyValueDefault { } "=", + # allow lists as values for duplicate keys + listsAsDuplicateKeys ? false + }: + { + globalSection, + sections, + }: (if globalSection == { } then "" else @@ -247,9 +258,11 @@ in rec { withRecursion = { # If this option is not null, the given value will stop evaluating at a certain depth - depthLimit - # If this option is true, an error will be thrown, if a certain given depth is exceeded - , throwOnDepthLimit ? true }: + depthLimit + # If this option is true, an error will be thrown, if a certain given depth is exceeded + , + throwOnDepthLimit ? true + }: assert builtins.isInt depthLimit; let specialAttrs = [ "__functor" "__functionArgs" "__toString" "__pretty" ]; @@ -286,11 +299,12 @@ in rec { will use fn to convert val to a pretty printed representation. (This means fn is type Val -> String.) */ - allowPrettyValues ? false, - # If this option is true, the output is indented with newlines for attribute sets and lists - multiline ? true, - # Initial indentation level - indent ? "" }: + allowPrettyValues ? false, + # If this option is true, the output is indented with newlines for attribute sets and lists + multiline ? true, + # Initial indentation level + indent ? "" + }: let go = indent: v: with builtins; diff --git a/test/diff/idioms_nixos_1/out.nix b/test/diff/idioms_nixos_1/out.nix index fa81fe0f..1abee780 100644 --- a/test/diff/idioms_nixos_1/out.nix +++ b/test/diff/idioms_nixos_1/out.nix @@ -1,4 +1,9 @@ -{ config, lib, pkgs, ... }: +{ + config, + lib, + pkgs, + ... +}: with lib; diff --git a/test/diff/idioms_pkgs_1/out.nix b/test/diff/idioms_pkgs_1/out.nix index fc9d729b..afdbed26 100644 --- a/test/diff/idioms_pkgs_1/out.nix +++ b/test/diff/idioms_pkgs_1/out.nix @@ -1,4 +1,9 @@ -{ stdenv, lib, fetchFrom, ... }: +{ + stdenv, + lib, + fetchFrom, + ... +}: stdenv.mkDerivation rec { pname = "test"; diff --git a/test/diff/idioms_pkgs_2/out.nix b/test/diff/idioms_pkgs_2/out.nix index e72b643c..4ff5fbf9 100644 --- a/test/diff/idioms_pkgs_2/out.nix +++ b/test/diff/idioms_pkgs_2/out.nix @@ -1,4 +1,12 @@ -{ lib, stdenv, fetchurl, nixos, testVersion, testEqualDerivation, hello }: +{ + lib, + stdenv, + fetchurl, + nixos, + testVersion, + testEqualDerivation, + hello, +}: stdenv.mkDerivation rec { pname = "hello"; diff --git a/test/diff/idioms_pkgs_3/out.nix b/test/diff/idioms_pkgs_3/out.nix index fa81fe0f..1abee780 100644 --- a/test/diff/idioms_pkgs_3/out.nix +++ b/test/diff/idioms_pkgs_3/out.nix @@ -1,4 +1,9 @@ -{ config, lib, pkgs, ... }: +{ + config, + lib, + pkgs, + ... +}: with lib; diff --git a/test/diff/key_value/out.nix b/test/diff/key_value/out.nix index 81fa1d75..b99a70e7 100644 --- a/test/diff/key_value/out.nix +++ b/test/diff/key_value/out.nix @@ -54,7 +54,11 @@ rec { c = 2; }; n = pkgs: { }; - o = { pkgs, ... }: { }; + o = { + pkgs, + ... + }: + { }; a # b diff --git a/test/diff/lambda/out.nix b/test/diff/lambda/out.nix index 7ba1d670..c8fb3d58 100644 --- a/test/diff/lambda/out.nix +++ b/test/diff/lambda/out.nix @@ -19,25 +19,44 @@ (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) - ({ pkgs ? import ./.. { }, locationsXml }: null) + ({ + pkgs ? import ./.. { }, + locationsXml, + }: + null) (a: b: c: { }: a: b: c: a) - ({ pkgs, ... }: + ({ + pkgs, + ... + }: { # Stuff }) - ({ pkgs, ... }: let in pkgs) + ({ + pkgs, + ... + }: + let + in pkgs) (a: - { b, ... }: + { + b, + ... + }: c: { # Stuff }) (a: - { b, c, ... }: + { + b, + c, + ... + }: d: { # Stuff diff --git a/test/diff/monsters_1/out.nix b/test/diff/monsters_1/out.nix index 65b0817a..02d7ee2b 100644 --- a/test/diff/monsters_1/out.nix +++ b/test/diff/monsters_1/out.nix @@ -1,67 +1,68 @@ { # foo -stdenv -# foo -, -# foo -lib -# foo -, -# foo -fetchFromGitLab -# foo -, -# foo -cairo -# foo -, -# foo -desktop-file-utils -# foo -, -# foo -gettext -# foo -, -# foo -glib -# foo -, -# foo -gtk4 -# foo -, -# foo -libadwaita -# foo -, -# foo -meson -# foo -, -# foo -ninja -# foo -, -# foo -pango -# foo -, -# foo -pkg-config -# foo -, -# foo -python3 -# foo -, -# foo -rustPlatform -# foo -, -# foo -wrapGAppsHook4 -# foo + stdenv + # foo + , + # foo + lib + # foo + , + # foo + fetchFromGitLab + # foo + , + # foo + cairo + # foo + , + # foo + desktop-file-utils + # foo + , + # foo + gettext + # foo + , + # foo + glib + # foo + , + # foo + gtk4 + # foo + , + # foo + libadwaita + # foo + , + # foo + meson + # foo + , + # foo + ninja + # foo + , + # foo + pango + # foo + , + # foo + pkg-config + # foo + , + # foo + python3 + # foo + , + # foo + rustPlatform + # foo + , + # foo + wrapGAppsHook4 + # foo + , }: # foo stdenv.mkDerivation diff --git a/test/diff/monsters_3/out.nix b/test/diff/monsters_3/out.nix index daf77bf8..0500dce3 100644 --- a/test/diff/monsters_3/out.nix +++ b/test/diff/monsters_3/out.nix @@ -1,6 +1,21 @@ -{ stdenv, lib, fetchFromGitLab, cairo, desktop-file-utils, gettext, glib, gtk4 -, libadwaita, meson, ninja, pango, pkg-config, python3, rustPlatform -, wrapGAppsHook4 }: +{ + stdenv, + lib, + fetchFromGitLab, + cairo, + desktop-file-utils, + gettext, + glib, + gtk4, + libadwaita, + meson, + ninja, + pango, + pkg-config, + python3, + rustPlatform, + wrapGAppsHook4, +}: stdenv.mkDerivation rec { pname = "contrast"; version = "0.0.5"; diff --git a/test/diff/monsters_4/out.nix b/test/diff/monsters_4/out.nix index e1d1c452..9360bc69 100644 --- a/test/diff/monsters_4/out.nix +++ b/test/diff/monsters_4/out.nix @@ -1,35 +1,36 @@ { # Foo -stdenv # Foo -, # Foo -lib # Foo -, # Foo -fetchFromGitLab # Foo -, # Foo -cairo # Foo -, # Foo -desktop-file-utils # Foo -, # Foo -gettext # Foo -, # Foo -glib # Foo -, # Foo -gtk4 # Foo -, # Foo -libadwaita # Foo -, # Foo -meson # Foo -, # Foo -ninja # Foo -, # Foo -pango # Foo -, # Foo -pkg-config # Foo -, # Foo -python3 # Foo -, # Foo -rustPlatform # Foo -, # Foo -wrapGAppsHook4 # Foo + stdenv # Foo + , # Foo + lib # Foo + , # Foo + fetchFromGitLab # Foo + , # Foo + cairo # Foo + , # Foo + desktop-file-utils # Foo + , # Foo + gettext # Foo + , # Foo + glib # Foo + , # Foo + gtk4 # Foo + , # Foo + libadwaita # Foo + , # Foo + meson # Foo + , # Foo + ninja # Foo + , # Foo + pango # Foo + , # Foo + pkg-config # Foo + , # Foo + python3 # Foo + , # Foo + rustPlatform # Foo + , # Foo + wrapGAppsHook4 # Foo + , }: # Foo stdenv.mkDerivation # Foo rec # Foo diff --git a/test/diff/monsters_5/out.nix b/test/diff/monsters_5/out.nix index a63065f0..af37fdaa 100644 --- a/test/diff/monsters_5/out.nix +++ b/test/diff/monsters_5/out.nix @@ -1,12 +1,12 @@ { -config, + config, -lib, + lib, -pkgs, + pkgs, -... + ... }: diff --git a/test/diff/pattern/out.nix b/test/diff/pattern/out.nix index 23f5ebef..39326a69 100644 --- a/test/diff/pattern/out.nix +++ b/test/diff/pattern/out.nix @@ -1,389 +1,556 @@ [ - ({ foo, bar - # Some comment - , baz }: + ({ + foo, + bar + # Some comment + , + baz, + }: { }) - ({ foo, bar # Some comment + ({ + foo, + bar # Some comment + , }: { }) - (a@{ self, gomod2nix, mach-nix, }: _) - ({ self, gomod2nix, mach-nix, }@inp: _) - ({ a ? [ 1 2 3 ], b ? { - # ... - } }: + (a@{ + self, + gomod2nix, + mach-nix, + }: + _) + ({ + self, + gomod2nix, + mach-nix, + }@inp: + _) + ({ + a ? [ 1 2 3 ], + b ? { + # ... + } + }: _) ({ }: _) - ({ a }: _) + ({ + a, + }: + _) ({ }: _) - ({ ... }: _) - ({ ... }: _) - ({ ... }: _) - ({ ... }: _) + ({ + ... + }: + _) + ({ + ... + }: + _) + ({ + ... + }: + _) + ({ + ... + }: + _) - ({ b, e, ... }: _) - ({ b, e, ... # h + ({ + b, + e, + ... + }: + _) + ({ + b, + e, + ... # h }: _) - ({ b, e, # g - ... }: + ({ + b, + e, # g + ... + }: _) - ({ b, e, # g - ... # h + ({ + b, + e, # g + ... # h }: _) - ({ b, e # f - , ... }: + ({ + b, + e # f + , + ... + }: _) - ({ b, e # f - , ... # h + ({ + b, + e # f + , + ... # h }: _) - ({ b, e # f - , # g - ... }: + ({ + b, + e # f + , # g + ... + }: _) - ({ b, e # f - , # g - ... # h + ({ + b, + e # f + , # g + ... # h }: _) - ({ b, # d - e, ... }: + ({ + b, # d + e, + ... + }: _) - ({ b, # d - e, ... # h + ({ + b, # d + e, + ... # h }: _) - ({ b, # d - e, # g - ... }: + ({ + b, # d + e, # g + ... + }: _) - ({ b, # d - e, # g - ... # h + ({ + b, # d + e, # g + ... # h }: _) - ({ b, # d - e # f - , ... }: + ({ + b, # d + e # f + , + ... + }: _) - ({ b, # d - e # f - , ... # h + ({ + b, # d + e # f + , + ... # h }: _) - ({ b, # d - e # f - , # g - ... }: + ({ + b, # d + e # f + , # g + ... + }: _) - ({ b, # d - e # f - , # g - ... # h + ({ + b, # d + e # f + , # g + ... # h }: _) - ({ b # c - , e, ... }: + ({ + b # c + , + e, + ... + }: _) - ({ b # c - , e, ... # h + ({ + b # c + , + e, + ... # h }: _) - ({ b # c - , e, # g - ... }: + ({ + b # c + , + e, # g + ... + }: _) - ({ b # c - , e, # g - ... # h + ({ + b # c + , + e, # g + ... # h }: _) - ({ b # c - , e # f - , ... }: + ({ + b # c + , + e # f + , + ... + }: _) - ({ b # c - , e # f - , ... # h + ({ + b # c + , + e # f + , + ... # h }: _) - ({ b # c - , e # f - , # g - ... }: + ({ + b # c + , + e # f + , # g + ... + }: _) - ({ b # c - , e # f - , # g - ... # h + ({ + b # c + , + e # f + , # g + ... # h }: _) - ({ b # c - , # d - e, ... }: + ({ + b # c + , # d + e, + ... + }: _) - ({ b # c - , # d - e, ... # h + ({ + b # c + , # d + e, + ... # h }: _) - ({ b # c - , # d - e, # g - ... }: + ({ + b # c + , # d + e, # g + ... + }: _) - ({ b # c - , # d - e, # g - ... # h + ({ + b # c + , # d + e, # g + ... # h }: _) - ({ b # c - , # d - e # f - , ... }: + ({ + b # c + , # d + e # f + , + ... + }: _) - ({ b # c - , # d - e # f - , ... # h + ({ + b # c + , # d + e # f + , + ... # h }: _) - ({ b # c - , # d - e # f - , # g - ... }: + ({ + b # c + , # d + e # f + , # g + ... + }: _) - ({ b # c - , # d - e # f - , # g - ... # h + ({ + b # c + , # d + e # f + , # g + ... # h }: _) ({ # a - b, e, ... }: + b, + e, + ... + }: _) ({ # a - b, e, ... # h + b, + e, + ... # h }: _) ({ # a - b, e, # g - ... }: + b, + e, # g + ... + }: _) ({ # a - b, e, # g - ... # h + b, + e, # g + ... # h }: _) ({ # a - b, e # f - , ... }: + b, + e # f + , + ... + }: _) ({ # a - b, e # f - , ... # h + b, + e # f + , + ... # h }: _) ({ # a - b, e # f - , # g - ... }: + b, + e # f + , # g + ... + }: _) ({ # a - b, e # f - , # g - ... # h + b, + e # f + , # g + ... # h }: _) ({ # a - b, # d - e, ... }: + b, # d + e, + ... + }: _) ({ # a - b, # d - e, ... # h + b, # d + e, + ... # h }: _) ({ # a - b, # d - e, # g - ... }: + b, # d + e, # g + ... + }: _) ({ # a - b, # d - e, # g - ... # h + b, # d + e, # g + ... # h }: _) ({ # a - b, # d - e # f - , ... }: + b, # d + e # f + , + ... + }: _) ({ # a - b, # d - e # f - , ... # h + b, # d + e # f + , + ... # h }: _) ({ # a - b, # d - e # f - , # g - ... }: + b, # d + e # f + , # g + ... + }: _) ({ # a - b, # d - e # f - , # g - ... # h + b, # d + e # f + , # g + ... # h }: _) ({ # a - b # c - , e, ... }: + b # c + , + e, + ... + }: _) ({ # a - b # c - , e, ... # h + b # c + , + e, + ... # h }: _) ({ # a - b # c - , e, # g - ... }: + b # c + , + e, # g + ... + }: _) ({ # a - b # c - , e, # g - ... # h + b # c + , + e, # g + ... # h }: _) ({ # a - b # c - , e # f - , ... }: + b # c + , + e # f + , + ... + }: _) ({ # a - b # c - , e # f - , ... # h + b # c + , + e # f + , + ... # h }: _) ({ # a - b # c - , e # f - , # g - ... }: + b # c + , + e # f + , # g + ... + }: _) ({ # a - b # c - , e # f - , # g - ... # h + b # c + , + e # f + , # g + ... # h }: _) ({ # a - b # c - , # d - e, ... }: + b # c + , # d + e, + ... + }: _) ({ # a - b # c - , # d - e, ... # h + b # c + , # d + e, + ... # h }: _) ({ # a - b # c - , # d - e, # g - ... }: + b # c + , # d + e, # g + ... + }: _) ({ # a - b # c - , # d - e, # g - ... # h + b # c + , # d + e, # g + ... # h }: _) ({ # a - b # c - , # d - e # f - , ... }: + b # c + , # d + e # f + , + ... + }: _) ({ # a - b # c - , # d - e # f - , ... # h + b # c + , # d + e # f + , + ... # h }: _) ({ # a - b # c - , # d - e # f - , # g - ... }: + b # c + , # d + e # f + , # g + ... + }: _) ({ # a - b # c - , # d - e # f - , # g - ... # h + b # c + , # d + e # f + , # g + ... # h }: _) - ({ a ? null }: _) + ({ + a ? null + }: + _) ({ # a - b # a - ? # a - null # c - , # d - e # a - ? # a - null # f - , # g - ... # h + b # a + ? # a + null # c + , # d + e # a + ? # a + null # f + , # g + ... # h }: _) ({ # a # - b - # a - # - ? - # a - # - null - # c + b + # a # - , - # d - # - e - # a - # - ? - # a - # - null - # f + ? + # a + # + null + # c + # + , + # d + # + e + # a + # + ? + # a + # + null + # f + # + , + # g + # + ... + # h # - , - # g - # - ... - # h - # } # i #