Skip to content

Commit

Permalink
Rework if statements
Browse files Browse the repository at this point in the history
- Disallow single-line if statements
- Handle multi-line conditions differently (indent instead of absorb)
  • Loading branch information
piegamesde committed Apr 18, 2023
1 parent db79a17 commit bd478c1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ absorb left right (Just level) x
absorbSet :: Expression -> Doc
absorbSet = absorb line mempty Nothing

-- Don't absorb the if body, always force content on new line
absorbThen :: Expression -> Doc
-- XXX this should be removed, but does not appear to work anyways?
absorbThen (Term t) | isAbsorbable t = hardspace <> prettyTerm t <> hardspace
absorbThen x = line <> nest 2 (group x) <> line
absorbThen x = hardline <> nest 2 (group x) <> hardline

-- What is allowed to come on the same line as `in`?
-- Absorbable terms like sets
Expand All @@ -198,12 +200,13 @@ absorbIn x@(With _ _ _ _) = group x
absorbIn x@(Let _ _ _ _) = group x
absorbIn x = line <> nest 2 (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
<> pretty then_ <> absorbThen expr0
<> pretty else_ <> absorbElse expr1

-- XXX Same as for Then
absorbElse (Term t) | isAbsorbable t = hardspace <> prettyTerm t
absorbElse x = line <> nest 2 (group x)

Expand Down Expand Up @@ -238,7 +241,8 @@ instance Pretty Expression where

pretty (If if_ cond then_ expr0 else_ expr1)
= base $ group $
pretty if_ <> hardspace <> group cond <> hardspace
-- `if cond then` if it fits on one line, otherwise `if\n cond\nthen` (with cond indented)
pretty if_ <> line' <> nest 2 (group cond) <> line'
<> pretty then_ <> absorbThen expr0
<> pretty else_ <> absorbElse expr1

Expand Down

0 comments on commit bd478c1

Please sign in to comment.