Skip to content

Commit

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

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

An example of this is inheriting from `lib`, which usually starts out
with just a single value, but usually gets more later. This would've been
contracted onto a single line before this change:

    inherit (lib)
      foo
      ;

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 authored and piegamesde committed Aug 7, 2024
1 parent f060a9d commit 47f1a74
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ instance Pretty Binder where
pretty inherit
<> ( if null ids
then pretty semicolon
else line <> nest (sepBy (if length ids < 4 then line else hardline) ids <> line' <> pretty semicolon)
else sep <> nest (sepBy sep ids <> nosep <> pretty semicolon)
)
where
-- Only allow a single line if it's already on a single line and has few enough elements
(sep, nosep) = if sourceLine inherit == sourceLine semicolon && length ids < 4 then (line, line') else (hardline, hardline)
-- `inherit (foo) bar` statement
pretty (Inherit inherit (Just source) ids semicolon) =
group $
Expand All @@ -140,11 +143,14 @@ instance Pretty Binder where
<> if null ids
then pretty semicolon
else
line
<> sepBy (if length ids < 4 then line else hardline) ids
<> line'
sep
<> sepBy sep ids
<> nosep
<> pretty semicolon
)
where
-- Only allow a single line if it's already on a single line and has few enough elements
(sep, nosep) = if sourceLine inherit == sourceLine semicolon && length ids < 4 then (line, line') else (hardline, hardline)
-- `foo = bar`
pretty (Assignment selectors assign expr semicolon) =
group $
Expand Down
9 changes: 9 additions & 0 deletions test/diff/inherit/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
h
;
}

{
inherit
;
inherit
a;
inherit a
;
}
]
6 changes: 6 additions & 0 deletions test/diff/inherit/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@
h
;
}

{
inherit;
inherit a;
inherit a;
}
]
14 changes: 13 additions & 1 deletion test/diff/inherit/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
;
}
{
inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
inherit
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
;
}
{ inherit b d; }
{
Expand Down Expand Up @@ -79,4 +81,14 @@
h
;
}

{
inherit;
inherit
a
;
inherit
a
;
}
]
13 changes: 13 additions & 0 deletions test/diff/inherit_from/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@
{ inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f h /*i*/; }
{ inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h ; }
{ inherit /*a*/ (/*b*/ c /*d*/) /*e*/ f /*g*/ h /*i*/; }
{
inherit ({}) ;
inherit ({})
;
inherit
({});

inherit ({}) a;
inherit ({}) a
;
inherit ({})
a;
}
]
9 changes: 9 additions & 0 deletions test/diff/inherit_from/out-pure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,13 @@
h # i
;
}
{
inherit ({ });
inherit ({ });
inherit ({ });

inherit ({ }) a;
inherit ({ }) a;
inherit ({ }) a;
}
]
13 changes: 13 additions & 0 deletions test/diff/inherit_from/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,17 @@
h # i
;
}
{
inherit ({ });
inherit ({ });
inherit ({ });

inherit ({ }) a;
inherit ({ })
a
;
inherit ({ })
a
;
}
]

0 comments on commit 47f1a74

Please sign in to comment.