Skip to content

Commit

Permalink
Always preserve expanded attribute parameters
Browse files Browse the repository at this point in the history
This changes the formatting to take into account whether attribute
parameters are expanded onto multiple lines already,
and if so, preserves that.

This is desirable because if a user writes an attribute parameter over
multiple lines, there's most likely an intention to add more attributes,
which would become harder when formatted.

An example of this is package recipes, which usually start out with just a
single attribute argument, but usually get more later. This would've been
contracted onto a single line before this change:

    {
      stdenv,
    }:

This change conforms to the standard due to this line:

> The formatter may take the input formatting into account in some cases in order to preserve multi-line syntax elements (which would otherwise have been contracted by the rules).
  • Loading branch information
infinisil committed Aug 8, 2024
1 parent 51fe84a commit 9be49f0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
25 changes: 16 additions & 9 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ instance Pretty Parameter where
pretty (IDParameter i) = pretty i
-- {}:
pretty (SetParameter bopen [] bclose) =
group $ pretty (moveTrailingCommentUp bopen) <> hardspace <> pretty bclose
group $ pretty (moveTrailingCommentUp bopen) <> sep <> pretty bclose
where
-- If the braces are on different lines, keep them like that
sep = if sourceLine bopen /= sourceLine bclose then hardline else hardspace
-- { stuff }:
pretty (SetParameter bopen attrs bclose) =
group $
Expand All @@ -332,14 +335,18 @@ instance Pretty Parameter where
[pretty (ParamAttr name maybeDefault Nothing) <> trailing ","]
handleTrailingComma (x : xs) = pretty x : handleTrailingComma xs

sep = case attrs of
[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
sep =
-- If the braces are on different lines, keep them like that
if sourceLine bopen /= sourceLine bclose
then hardline
else case attrs of
[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
23 changes: 17 additions & 6 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 All @@ -29,7 +31,8 @@ rec {
# The builtin `toString` function has some strange defaults,
# suitable for bash scripts but not much else.
mkValueStringDefault =
{ }:
{
}:
v:
with builtins;
let
Expand Down Expand Up @@ -203,7 +206,10 @@ rec {
# allow lists as values for duplicate keys
listsAsDuplicateKeys ? false,
}:
{ globalSection, sections }:
{
globalSection,
sections,
}:
(
if globalSection == { } then
""
Expand Down Expand Up @@ -279,7 +285,10 @@ rec {

# Generates JSON from an arbitrary (non-function) value.
# For more information see the documentation of the builtin.
toJSON = { }: builtins.toJSON;
toJSON =
{
}:
builtins.toJSON;

# YAML has been a strict superset of JSON since 1.2, so we
# use toJSON. Before it only had a few differences referring
Expand Down Expand Up @@ -465,7 +474,8 @@ rec {

# PLIST handling
toPlist =
{ }:
{
}:
v:
let
isFloat = builtins.isFloat or (x: false);
Expand Down Expand Up @@ -546,7 +556,8 @@ rec {
# Note that integers are translated to Integer and never
# the Natural type.
toDhall =
{ }@args:
{
}@args:
v:
with builtins;
let
Expand Down
7 changes: 6 additions & 1 deletion test/diff/key_value/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ rec {
c = 2;
};
n = pkgs: { };
o = { pkgs, ... }: { };
o =
{
pkgs,
...
}:
{ };

a
# b
Expand Down
7 changes: 7 additions & 0 deletions test/diff/lambda/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ let
inherit lib;
in
[
({}: null)
({
}: null)
({

}: null)

(
{ lib, }:
let
Expand Down
9 changes: 9 additions & 0 deletions test/diff/lambda/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ let
inherit lib;
in
[
({ }: null)
({ }: null)
(
{

}:
null
)

(
{ lib }:
let
Expand Down
24 changes: 22 additions & 2 deletions test/diff/lambda/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ let
inherit lib;
in
[
({ }: null)
(
{
}:
null
)
(
{

}:
null
)

(
{ lib }:
let
Expand Down Expand Up @@ -81,15 +94,22 @@ in

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

(
a:
{ b, c, ... }:
{
b,
c,
...
}:
d: {
# Stuff
}
Expand Down

0 comments on commit 9be49f0

Please sign in to comment.