Skip to content

Commit

Permalink
Make empty inherit's more consistent and convenient
Browse files Browse the repository at this point in the history
This might happen if you temporarily remove all inherits, in which case
the formatter shouldn't remove the spacing that was there.

E.g. I might have this initially

    inherit a b;

    inherit
      c
      d
      ;

removing all the bindings gives me

    inherit ; # I already have the space that I'll need

    inherit # I can just press enter here to add one
      ;

which shouldn't be changed, because it's easier like this to insert new
entries again, compared to

    inherit;

This also makes it more consistent with the parent commit's change
  • Loading branch information
infinisil authored and piegamesde committed Aug 7, 2024
1 parent c10c061 commit 4fc4f8f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
17 changes: 6 additions & 11 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ instance Pretty Binder where
pretty (Inherit inherit Nothing ids semicolon) =
group $
pretty inherit
<> ( if null ids
then pretty semicolon
else sep <> nest (sepBy sep ids <> nosep <> pretty semicolon)
)
<> 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)
Expand All @@ -140,13 +138,10 @@ instance Pretty Binder where
pretty inherit
<> nest
( group' RegularG (line <> pretty source)
<> if null ids
then pretty semicolon
else
sep
<> sepBy sep ids
<> nosep
<> pretty semicolon
<> 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
Expand Down
4 changes: 2 additions & 2 deletions test/diff/inherit/out-pure.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
# empty inherit o.O
inherit;
inherit ;
inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
}
{
Expand Down Expand Up @@ -79,7 +79,7 @@
}

{
inherit;
inherit ;
inherit a;
inherit a;
}
Expand Down
5 changes: 3 additions & 2 deletions test/diff/inherit/out.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
# empty inherit o.O
inherit;
inherit ;
inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
}
{
Expand Down Expand Up @@ -83,7 +83,8 @@
}

{
inherit;
inherit
;
inherit
a
;
Expand Down
8 changes: 4 additions & 4 deletions test/diff/inherit_from/out-pure.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
# empty inherit o.O
inherit (geany.meta);
inherit (geany.meta) ;
inherit (builtins)
pathExists
readFile
Expand Down Expand Up @@ -572,9 +572,9 @@
;
}
{
inherit ({ });
inherit ({ });
inherit ({ });
inherit ({ }) ;
inherit ({ }) ;
inherit ({ }) ;

inherit ({ }) a;
inherit ({ }) a;
Expand Down
10 changes: 6 additions & 4 deletions test/diff/inherit_from/out.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
# empty inherit o.O
inherit (geany.meta);
inherit (geany.meta) ;
inherit (builtins)
pathExists
readFile
Expand Down Expand Up @@ -572,9 +572,11 @@
;
}
{
inherit ({ });
inherit ({ });
inherit ({ });
inherit ({ }) ;
inherit ({ })
;
inherit ({ })
;

inherit ({ }) a;
inherit ({ })
Expand Down

0 comments on commit 4fc4f8f

Please sign in to comment.