Skip to content

Commit

Permalink
Rework inherit statements
Browse files Browse the repository at this point in the history
- The (from) case needs to be improved
- Somehow it indents the comments *after* the statement now, no idea why or how to fix
  • Loading branch information
piegamesde committed May 5, 2023
1 parent cd0acd3 commit 8315ca3
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 183 deletions.
11 changes: 7 additions & 4 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,19 @@ instance Pretty Selector where
= pretty dot <> pretty sel
<> hardspace <> pretty kw <> hardspace <> pretty def

-- in attrsets and let bindings
instance Pretty Binder where
-- `inherit bar` statement
pretty (Inherit inherit Nothing ids semicolon)
= base $ group (pretty inherit <> softline
<> nest 2 (sepBy softline ids)) <> pretty semicolon
= base $ group (pretty inherit <> line
<> nest 2 (sepBy line ids <> line' <> pretty semicolon))

-- `inherit (foo) bar` statement
pretty (Inherit inherit source ids semicolon)
= base $ group (pretty inherit <> hardspace
<> pretty source <> line
<> nest 2 (sepBy softline ids)) <> pretty semicolon
<> nest 2 ((pretty source) <> line <> sepBy line ids <> line' <> pretty semicolon))

-- `foo = bar`
pretty (Assignment selectors assign expr semicolon)
= base $ group (hcat selectors <> hardspace
<> nest 2 (pretty assign <> softline <> pretty expr))
Expand Down
22 changes: 16 additions & 6 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,22 @@ rec {

# Pull in some builtins not included elsewhere.
inherit (builtins)
pathExists readFile isBool isInt isFloat add sub lessThan seq deepSeq
genericClosure;

## nixpkgs version strings

# Returns the current full nixpkgs version number.
pathExists
readFile
isBool
isInt
isFloat
add
sub
lessThan
seq
deepSeq
genericClosure
;

## nixpkgs version strings

# Returns the current full nixpkgs version number.
version = release + versionSuffix;

# Returns the current nixpkgs release number as string.
Expand Down
38 changes: 25 additions & 13 deletions test/diff/inherit/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,46 @@
}
{ inherit b d; }
{
inherit b d # e
;
inherit
b
d # e
;
}
{
inherit b # c
d;
inherit
b # c
d
;
}
{
inherit b # c
inherit
b # c
d # e
;
;
}
{
inherit # a
b d;
b
d
;
}
{
inherit # a
b d # e
;
b
d # e
;
}
{
inherit # a
b # c
d;
d
;
}
{
inherit # a
b # c
d # e
;
;
}
{
inherit # test
Expand All @@ -47,8 +56,11 @@
c # test
d # test

e f
e
f

g h;
g
h
;
}
]
12 changes: 8 additions & 4 deletions test/diff/inherit_blank_trailing/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
c # test
d # test

e f
e
f

g h;
g
h
;
}
{
inherit a # mixed trivialities
inherit
a # mixed trivialities

# comment 1
# comment 2
Expand All @@ -24,6 +28,6 @@
# comment 1
# comment 2
# comment 3
;
;
}
]
8 changes: 5 additions & 3 deletions test/diff/inherit_comment/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
inherit # eeby deeby
a
# b
c;
c
;

# https://github.com/kamadorueda/alejandra/issues/372
# https://github.com/kamadorueda/alejandra/issues/372
inherit (pkgs.haskell.lib)
# doJailbreak - remove package bounds from build-depends of a package
doJailbreak
# dontCheck - skip tests
dontCheck
# override deps of a package
# see what can be overriden - https://github.com/NixOS/nixpkgs/blob/0ba44a03f620806a2558a699dba143e6cf9858db/pkgs/development/haskell-modules/generic-builder.nix#L13
overrideCabal;
overrideCabal
;
}
9 changes: 9 additions & 0 deletions test/diff/inherit_from/in.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
inherit (builtins)
pathExists readFile isBool
isInt isFloat add sub lessThan
seq deepSeq genericClosure;
}
{
inherit ({ foo = "1"; bar = "2"; /* force multiline */}) foo bar;
}
{ inherit ( c ) f h ; }
{ inherit ( c ) f h /*i*/; }
{ inherit ( c ) f /*g*/ h ; }
Expand Down
Loading

0 comments on commit 8315ca3

Please sign in to comment.