Skip to content

Commit

Permalink
Rework function declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed May 5, 2023
1 parent 37e17e7 commit ec4fea5
Show file tree
Hide file tree
Showing 16 changed files with 642 additions and 387 deletions.
24 changes: 15 additions & 9 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/diff/apply/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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: { });
}
Expand Down
5 changes: 3 additions & 2 deletions test/diff/comment/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@
#7
in d)

({ a, # comment
b ? 2, # comment
({
a, # comment
b ? 2, # comment
}:
_)
]
4 changes: 3 additions & 1 deletion test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{ lib }:
{
lib,
}:

rec {

Expand Down
60 changes: 37 additions & 23 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}";

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" ];
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion test/diff/idioms_nixos_1/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:

with lib;

Expand Down
7 changes: 6 additions & 1 deletion test/diff/idioms_pkgs_1/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ stdenv, lib, fetchFrom, ... }:
{
stdenv,
lib,
fetchFrom,
...
}:

stdenv.mkDerivation rec {
pname = "test";
Expand Down
10 changes: 9 additions & 1 deletion test/diff/idioms_pkgs_2/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{ lib, stdenv, fetchurl, nixos, testVersion, testEqualDerivation, hello }:
{
lib,
stdenv,
fetchurl,
nixos,
testVersion,
testEqualDerivation,
hello,
}:

stdenv.mkDerivation rec {
pname = "hello";
Expand Down
7 changes: 6 additions & 1 deletion test/diff/idioms_pkgs_3/out.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:

with lib;

Expand Down
6 changes: 5 additions & 1 deletion test/diff/key_value/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ rec {
c = 2;
};
n = pkgs: { };
o = { pkgs, ... }: { };
o = {
pkgs,
...
}:
{ };

a
# b
Expand Down
29 changes: 24 additions & 5 deletions test/diff/lambda/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ec4fea5

Please sign in to comment.