Skip to content

Commit

Permalink
Expand attrset function parameters less
Browse files Browse the repository at this point in the history
Keep up to two arguments compact, which benefits a lot of small functions
operating on key-value pairs.
  • Loading branch information
piegamesde committed Jul 17, 2023
1 parent cc5b426 commit 4468e9b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 125 deletions.
71 changes: 47 additions & 24 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -262,41 +262,46 @@ instance Pretty Term where
pretty l@List{} = group $ prettyTerm l
pretty x = prettyTerm x

-- Does not move around comments, nor does it inject a trailing comma
instance Pretty ParamAttr where
-- Simple parameter, move comment around
-- Move comments around when switching from leading comma to trailing comma style:
-- `, name # foo` → `name, #foo`
pretty (ParamAttr (Ann trivia name (Just comment')) Nothing (Just (Ann trivia' comma Nothing)))
= pretty (ParamAttr (Ann trivia name Nothing) Nothing (Just (Ann trivia' comma (Just comment'))))

-- Simple parameter, move comment around and add trailing comma
-- Same as above, but also add trailing comma
pretty (ParamAttr (Ann trivia name (Just comment')) Nothing Nothing)
= pretty (ParamAttr (Ann trivia name Nothing) Nothing (Just (Ann [] TComma (Just comment'))))

-- Simple parameter
-- Still need to handle missing trailing comma here, because the special cases above are not exhaustive
-- Simple parameter (no default)
pretty (ParamAttr name Nothing maybeComma)
= pretty name <> (fromMaybe (text ",") (fmap pretty maybeComma))
= pretty name <> pretty maybeComma

-- With ? default
pretty (ParamAttr name (Just (qmark, def)) maybeComma)
= group (pretty name <> hardspace <> pretty qmark
<> absorb softline mempty (Just 2) def)
<> (fromMaybe (text ",") (fmap pretty maybeComma))
<> pretty maybeComma

-- `...`
pretty (ParamEllipsis ellipsis)
= pretty ellipsis

-- Move comments around and inject trailing commas everywhere
moveParamAttrComment :: ParamAttr -> ParamAttr
-- Simple parameter, move comment around
-- Move comments around when switching from leading comma to trailing comma style:
-- `, name # foo` → `name, #foo`
moveParamAttrComment (ParamAttr (Ann trivia name (Just comment')) Nothing (Just (Ann trivia' comma Nothing)))
= ParamAttr (Ann trivia name Nothing) Nothing (Just (Ann trivia' comma (Just comment')))
-- Simple parameter, move comment around and add trailing comma
-- Same as above, but also add trailing comma
moveParamAttrComment (ParamAttr (Ann trivia name (Just comment')) Nothing Nothing)
= ParamAttr (Ann trivia name Nothing) Nothing (Just (Ann [] TComma (Just comment')))
-- Other cases, just inject a trailing comma
moveParamAttrComment (ParamAttr name def Nothing)
= ParamAttr name def (Just (Ann [] TComma Nothing))
moveParamAttrComment x = x

-- When a `, name` entry has some line comments before it, they are actually attached to the comment
-- of the preceding item. Move them to the next one
moveParamComments :: [ParamAttr] -> [ParamAttr]
moveParamComments
moveParamsComments :: [ParamAttr] -> [ParamAttr]
moveParamsComments
((ParamAttr name maybeDefault (Just (Ann trivia comma Nothing))) : (ParamAttr (Ann [] name' Nothing) maybeDefault' maybeComma') : xs)
= (ParamAttr name maybeDefault (Just (Ann [] comma Nothing))) : moveParamComments ((ParamAttr (Ann trivia name' Nothing) maybeDefault' maybeComma') : xs)
moveParamComments (x : xs) = x : moveParamComments xs
moveParamComments [] = []
= (ParamAttr name maybeDefault (Just (Ann [] comma Nothing))) : moveParamsComments ((ParamAttr (Ann trivia name' Nothing) maybeDefault' maybeComma') : xs)
moveParamsComments (x : xs) = x : moveParamsComments xs
moveParamsComments [] = []

instance Pretty Parameter where
-- param:
Expand All @@ -307,10 +312,28 @@ instance Pretty Parameter where
= group $ pretty bopen <> hardspace <> pretty bclose

-- { stuff }:
pretty (SetParameter bopen attrs bclose)
= groupWithStart bopen $ hardline
<> nest 2 (((sepBy hardline) . moveParamComments) attrs) <> hardline
<> pretty bclose
pretty (SetParameter bopen attrs bclose) =
groupWithStart bopen $
(surroundWith sep $ nest 2 (sepBy sep $ handleTrailingComma $ map moveParamAttrComment $ moveParamsComments $ attrs))
<> pretty bclose
where
-- pretty all ParamAttrs, but make the trailing comma of the last element specially
handleTrailingComma :: [ParamAttr] -> [Doc]
handleTrailingComma [] = []
-- That's the case we're interested in
handleTrailingComma [(ParamAttr name maybeDefault (Just (Ann [] TComma Nothing)))]
= [pretty (ParamAttr name maybeDefault Nothing) <> trailing ","]
handleTrailingComma (x:xs) = pretty x : handleTrailingComma xs

sep = case attrs of
[] -> line
[ParamEllipsis _] -> line
-- Attributes must be without default
[ParamAttr _ Nothing _] -> line
[ParamAttr _ Nothing _, ParamEllipsis _] -> line
[ParamAttr _ Nothing _, ParamAttr _ Nothing _] -> line
[ParamAttr _ Nothing _, ParamAttr _ Nothing _, ParamEllipsis _] -> line
_ -> hardline

pretty (ContextParameter param1 at param2)
= pretty param1 <> pretty at <> pretty param2
Expand Down
4 changes: 1 addition & 3 deletions test/diff/apply/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@
# https://github.com/kamadorueda/alejandra/issues/372#issuecomment-1435083516
{
outputs =
{
utils,
}:
{ utils }:
# For each supported platform,
utils.lib.eachDefaultSystem (system: { })
;
Expand Down
4 changes: 1 addition & 3 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
lib,
}:
{ lib }:

rec {

Expand Down
9 changes: 2 additions & 7 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
#
# 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 @@ -216,10 +214,7 @@ rec {
# allow lists as values for duplicate keys
listsAsDuplicateKeys ? false,
}:
{
globalSection,
sections,
}:
{ globalSection, sections }:
(
if globalSection == { } then
""
Expand Down
4 changes: 1 addition & 3 deletions test/diff/idioms_lib_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
# e.g. exhaustive cases. Its more a sanity check to make sure nobody defines
# systems that overlap with existing ones and won't notice something amiss.
#
{
lib,
}:
{ lib }:
with lib.lists;
with lib.types;
with lib.attrsets;
Expand Down
15 changes: 3 additions & 12 deletions test/diff/idioms_lib_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ let
;

handleEvalIssue =
{
meta,
attrs,
}:
{ meta, attrs }:
{
reason,
errormsg ? "",
Expand All @@ -303,10 +300,7 @@ let
;

handleEvalWarning =
{
meta,
attrs,
}:
{ meta, attrs }:
{
reason,
errormsg ? "",
Expand Down Expand Up @@ -626,10 +620,7 @@ let
;

assertValidity =
{
meta,
attrs,
}:
{ meta, attrs }:
let
validity = checkValidity attrs;
in
Expand Down
13 changes: 2 additions & 11 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ let

phpPackage = cfg.phpPackage.buildEnv {
extensions =
{
enabled,
all,
}:
{ enabled, all }:
(
with all;
# disable default openssl extension
Expand Down Expand Up @@ -995,13 +992,7 @@ in
'';
occInstallCmd =
let
mkExport =
{
arg,
value,
}:
"export ${arg}=${value}"
;
mkExport = { arg, value }: "export ${arg}=${value}";
dbpass = {
arg = "DBPASS";
value =
Expand Down
8 changes: 1 addition & 7 deletions test/diff/key_value/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ rec {
c = 2;
};
n = pkgs: { };
o =
{
pkgs,
...
}:
{ }
;
o = { pkgs, ... }: { };

a
# b
Expand Down
21 changes: 4 additions & 17 deletions test/diff/lambda/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,30 @@
)

(
{
pkgs,
...
}:
{ pkgs, ... }:
{
# Stuff
}
)

(
{
pkgs,
...
}:
{ pkgs, ... }:
let
in
pkgs
)

(
a:
{
b,
...
}:
{ b, ... }:
c: {
# Stuff
}
)

(
a:
{
b,
c,
...
}:
{ b, c, ... }:
d: {
# Stuff
}
Expand Down
44 changes: 6 additions & 38 deletions test/diff/pattern/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,14 @@
_
)
({ }: _)
(
{
a,
}:
_
)
({ a }: _)
({ }: _)
(
{
...
}:
_
)
(
{
...
}:
_
)
(
{
...
}:
_
)
(
{
...
}:
_
)
({ ... }: _)
({ ... }: _)
({ ... }: _)
({ ... }: _)

(
{
b,
e,
...
}:
_
)
({ b, e, ... }: _)
(
{
b,
Expand Down

0 comments on commit 4468e9b

Please sign in to comment.