Align then/else when formatting if expressions (#398)

Fixes #397
This commit is contained in:
Gabriel Gonzalez 2018-05-21 10:43:17 -07:00 committed by GitHub
parent dddfb1a8ea
commit e177d9bdfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 18 deletions

View File

@ -382,26 +382,49 @@ prettyExprB a0@(Lam _ _ _) = arrows (fmap duplicate (docs a0))
docs (Note _ c) = docs c
docs c = [ prettyExprB c ]
prettyExprB a0@(BoolIf _ _ _) =
enclose' "" " " (space <> keyword "else" <> space) (Pretty.hardline <> keyword "else" <> " ") (fmap duplicate (docs a0))
Pretty.group (Pretty.flatAlt long short)
where
docs (BoolIf a b c) =
Pretty.group (Pretty.flatAlt long short) : docs c
where
long =
Pretty.align
( (keyword "if" <> " ")
<> prettyExprA a
<> Pretty.hardline
<> (keyword "then" <> " ")
<> prettyExprA b
)
prefixesLong =
" "
: cycle
[ Pretty.hardline <> keyword "then" <> " "
, Pretty.hardline <> keyword "else" <> " "
]
prefixesShort =
""
: cycle
[ space <> keyword "then" <> space
, space <> keyword "else" <> space
]
longLines = zipWith (<>) prefixesLong (docsLong a0)
long =
Pretty.align (mconcat (Data.List.intersperse Pretty.hardline longLines))
short = mconcat (zipWith (<>) prefixesShort (docsShort a0))
docsLong (BoolIf a b c) =
docLong ++ docsLong c
where
docLong =
[ keyword "if" <> " " <> prettyExprA a
, prettyExprA b
]
docsLong (Note _ c) = docsLong c
docsLong c = [ prettyExprB c ]
docsShort (BoolIf a b c) =
docShort ++ docsShort c
where
docShort =
[ keyword "if" <> " " <> prettyExprA a
, prettyExprA b
]
docsShort (Note _ c) = docsShort c
docsShort c = [ prettyExprB c ]
short = (keyword "if" <> " ")
<> prettyExprA a
<> (space <> keyword "then" <> space)
<> prettyExprA b
docs (Note _ c) = docs c
docs c = [ prettyExprB c ]
prettyExprB a0@(Pi _ _ _) =
arrows (fmap duplicate (docs a0))
where

View File

@ -36,6 +36,9 @@ formatTests =
, should
"correctly format the empty record literal"
"emptyRecord"
, should
"indent then/else to the same column"
"ifThenElse"
]
opts :: Data.Text.Prettyprint.Doc.LayoutOptions

View File

@ -0,0 +1,2 @@
if True then if True then if True then 1 else 2 else if True then 3 else 4
else if True then if True then 5 else 6 else if True then 7 else 8

View File

@ -0,0 +1,13 @@
if True
then if True then if True then 1 else 2 else if True then 3 else 4
else if True
then if True then 5 else 6
else if True
then 7
else 8