Skip to content

Commit

Permalink
fixup! Rework if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed May 5, 2023
1 parent 6ea9841 commit 818f63c
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 194 deletions.
5 changes: 3 additions & 2 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ absorbIn x = line <> group x <> line
-- Only absorb "else if"
absorbElse :: Expression -> Doc
absorbElse (If if_ cond then_ expr0 else_ expr1)
= hardspace <> pretty if_ <> hardspace <> group cond <> hardspace
-- `if cond then` if it fits on one line, otherwise `if\n cond\nthen` (with cond indented)
= hardspace <> (group (pretty if_ <> hardspace <> line' <> nest 2 (group cond) <> hardspace <> line'))
<> pretty then_ <> absorbThen expr0
<> pretty else_ <> absorbElse expr1
-- XXX Same as for Then
Expand Down Expand Up @@ -245,7 +246,7 @@ instance Pretty Expression where
pretty (If if_ cond then_ expr0 else_ expr1)
= base $ group $
-- `if cond then` if it fits on one line, otherwise `if\n cond\nthen` (with cond indented)
pretty if_ <> hardspace <> line' <> nest 2 (group cond) <> hardspace <> line'
(group (pretty if_ <> hardspace <> line' <> nest 2 (group cond) <> hardspace <> line'))
<> pretty then_ <> absorbThen expr0
<> pretty else_ <> absorbElse expr1

Expand Down
3 changes: 1 addition & 2 deletions test/diff/idioms_lib_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
msg:
# Value to return
x:
if
pred
if pred
then
trace msg x
else
Expand Down
60 changes: 23 additions & 37 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,23 @@ rec {

# bitwise “and”
bitAnd = builtins.bitAnd or (import ./zip-int-bits.nix (a: b:
if
a == 1 && b == 1
if a == 1 && b == 1
then
1
else
0));

# bitwise “or”
bitOr = builtins.bitOr or (import ./zip-int-bits.nix (a: b:
if
a == 1 || b == 1
if a == 1 || b == 1
then
1
else
0));

# bitwise “xor”
bitXor = builtins.bitXor or (import ./zip-int-bits.nix (a: b:
if
a != b
if a != b
then
1
else
Expand All @@ -131,8 +128,7 @@ rec {
Type: boolToString :: bool -> string
*/
boolToString = b:
if
b
if b
then
"true"
else
Expand Down Expand Up @@ -176,8 +172,7 @@ rec {
f:
# Argument to check for null before passing it to `f`
a:
if
a == null
if a == null
then
a
else
Expand Down Expand Up @@ -217,8 +212,7 @@ rec {
# Returns the current nixpkgs version suffix as string.
versionSuffix = let
suffixFile = ../.version-suffix;
in if
pathExists suffixFile
in if pathExists suffixFile
then
lib.strings.fileContents suffixFile
else
Expand All @@ -235,11 +229,11 @@ rec {
let
revisionFile = "${toString ./..}/.git-revision";
gitRepo = "${toString ./..}/.git";
in if
lib.pathIsGitRepo gitRepo
in if lib.pathIsGitRepo gitRepo
then
lib.commitIdFromGitRepo gitRepo
else if lib.pathExists revisionFile then
else if lib.pathExists revisionFile
then
lib.fileContents revisionFile
else
default;
Expand All @@ -258,17 +252,15 @@ rec {

# Return minimum of two numbers.
min = x: y:
if
x < y
if x < y
then
x
else
y;

# Return maximum of two numbers.
max = x: y:
if
x > y
if x > y
then
x
else
Expand All @@ -293,11 +285,11 @@ rec {
a > b, compare a b => 1
*/
compare = a: b:
if
a < b
if a < b
then
-1
else if a > b then
else if a > b
then
1
else
0;
Expand Down Expand Up @@ -331,16 +323,15 @@ rec {
a:
# Second value to compare
b:
if
p a
if p a
then
if
p b
if p b
then
yes a b
else
-1
else if p b then
else if p b
then
1
else
no a b;
Expand Down Expand Up @@ -399,8 +390,7 @@ rec {
Type: bool -> string -> a -> a
*/
warnIf = cond: msg:
if
cond
if cond
then
warn msg
else
Expand All @@ -425,8 +415,7 @@ rec {
pkgs
*/
throwIfNot = cond: msg:
if
cond
if cond
then
x: x
else
Expand Down Expand Up @@ -482,8 +471,7 @@ rec {
setFunctionArgs : (a → b) → Map String Bool.
*/
functionArgs = f:
if
f ? __functor
if f ? __functor
then
f.__functionArgs or (lib.functionArgs (f.__functor f))
else
Expand All @@ -507,8 +495,7 @@ rec {
toHexString = i:
let
toHexDigit = d:
if
d < 10
if d < 10
then
toString d
else
Expand Down Expand Up @@ -536,8 +523,7 @@ rec {
toBaseDigits = base: i:
let
go = i:
if
i < base
if i < base
then [ i ] else
let
r = i - ((i / base) * base);
Expand Down
Loading

0 comments on commit 818f63c

Please sign in to comment.