Skip to content

Commit 51a20b7

Browse files
committed
Add support for single-line if expressions
Fixes #209
1 parent 3b2a0fd commit 51a20b7

File tree

7 files changed

+68
-66
lines changed

7 files changed

+68
-66
lines changed

parser/src/AST/Expression.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ data Expr'
5959
| AccessFunction LowercaseIdentifier
6060

6161
| Lambda [(Comments, Pattern.Pattern)] Comments Expr Bool
62-
| If IfClause [(Comments, IfClause)] (Comments, Expr)
62+
| If IfClause [(Comments, IfClause)] (Comments, Expr) Bool
6363
| Let [LetDeclaration] Comments Expr
6464
| Case (Commented Expr, Bool) [(Commented Pattern.Pattern, (Comments, Expr))]
6565

parser/src/AST/Json.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ instance ToJSON Expr where
373373
, ("body", showJSON body)
374374
]
375375

376-
If (Commented _ cond' _, Commented _ thenBody' _) rest' (_, elseBody) ->
376+
If (Commented _ cond' _, Commented _ thenBody' _) rest' (_, elseBody) _ ->
377377
let
378378
ifThenElse :: Expr -> Expr -> [(Comments, IfClause)] -> JSValue
379379
ifThenElse cond thenBody rest =

parser/src/Parse/Expression.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,18 @@ ifExpr elmVersion =
191191
>> whitespace
192192
in
193193
do
194-
first <- ifClause elmVersion
195-
rest <- many (try $ (,) <$> elseKeyword <*> ifClause elmVersion)
196-
final <- (,) <$> elseKeyword <*> expr elmVersion
194+
(first, firstMultiline) <- trackNewline $ ifClause elmVersion
195+
(rest, restMultiline) <- trackNewline $ many (try $ (,) <$> elseKeyword <*> ifClause elmVersion)
196+
(final, finalMultiline) <- trackNewline $ (,) <$> elseKeyword <*> expr elmVersion
197197

198-
return $ E.If first rest final
198+
199+
return $ E.If first rest final $
200+
case (firstMultiline, restMultiline, finalMultiline) of
201+
(JoinAll, JoinAll, JoinAll) ->
202+
multilineToBool JoinAll
203+
204+
_ ->
205+
multilineToBool SplitAll
199206

200207

201208
ifClause :: ElmVersion -> IParser E.IfClause

src/AST/MapExpr.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ instance MapExpr Expr' where
8181
AccessFunction _ -> expr
8282
Lambda params pre body multi ->
8383
Lambda params pre (mapExpr f body) multi
84-
If c1 elseIfs els ->
85-
If (mapExpr f c1) (mapExpr f elseIfs) (mapExpr f els)
84+
If c1 elseIfs els multiline ->
85+
If (mapExpr f c1) (mapExpr f elseIfs) (mapExpr f els) multiline
8686
Let decls pre body ->
8787
Let (mapExpr f decls) pre body
8888
Case cond branches ->

src/ElmFormat/Render/Box.hs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,57 +1407,59 @@ formatExpression' elmVersion importInfo context aexpr =
14071407
(fmap (formatPreCommentedExpression elmVersion importInfo SpaceSeparated) args)
14081408
|> expressionParens SpaceSeparated context
14091409

1410-
AST.Expression.If if' elseifs (elsComments, els) ->
1411-
let
1412-
opening key cond =
1413-
case (key, cond) of
1414-
(SingleLine key', SingleLine cond') ->
1415-
line $ row
1416-
[ key'
1417-
, space
1418-
, cond'
1419-
, space
1420-
, keyword "then"
1421-
]
1422-
_ ->
1410+
AST.Expression.If (cond, body) elseifs (elsComments, els) multiline ->
1411+
case
1412+
( multiline
1413+
, formatCommentedExpression elmVersion importInfo SyntaxSeparated cond
1414+
, formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) body
1415+
, elseifs
1416+
, formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) (AST.Commented elsComments els [])
1417+
)
1418+
of
1419+
(False, SingleLine cond', SingleLine body', [], SingleLine els') ->
1420+
line $ row
1421+
[ keyword "if" , space , cond' , space , keyword "then" , space , body' , space , keyword "else" , space , els' ]
1422+
(_, ifCond, thenBody, _, elseBody) ->
1423+
let
1424+
opening (SingleLine key) (SingleLine cond') =
1425+
line $ row [ key , space , cond' , space , keyword "then" ]
1426+
opening key cond' =
14231427
stack1
14241428
[ key
1425-
, cond |> indent
1429+
, cond' |> indent
14261430
, line $ keyword "then"
14271431
]
14281432

1429-
formatIf (cond, body) =
1433+
formatElseIf (ifComments, (cond', body')) =
1434+
let
1435+
key =
1436+
case (formatHeadCommented id (ifComments, line $ keyword "if")) of
1437+
SingleLine key' ->
1438+
line $ row [ keyword "else", space, key' ]
1439+
key' ->
1440+
stack1
1441+
[ line $ keyword "else"
1442+
, key'
1443+
]
1444+
in
1445+
stack1
1446+
[ blankLine
1447+
, opening key $ formatCommentedExpression elmVersion importInfo SyntaxSeparated cond'
1448+
, indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) body'
1449+
]
1450+
in
14301451
stack1
1431-
[ opening (line $ keyword "if") $ formatCommentedExpression elmVersion importInfo SyntaxSeparated cond
1432-
, indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) body
1452+
[ opening (line $ keyword "if") ifCond
1453+
, indent thenBody
14331454
]
1434-
1435-
formatElseIf (ifComments, (cond, body)) =
1436-
let
1437-
key =
1438-
case (formatHeadCommented id (ifComments, line $ keyword "if")) of
1439-
SingleLine key' ->
1440-
line $ row [ keyword "else", space, key' ]
1441-
key' ->
1442-
stack1
1443-
[ line $ keyword "else"
1444-
, key'
1455+
|> andThen (map formatElseIf elseifs)
1456+
|> andThen
1457+
[ blankLine
1458+
, line $ keyword "else"
1459+
, indent $ elseBody
14451460
]
1446-
in
1447-
stack1
1448-
[ blankLine
1449-
, opening key $ formatCommentedExpression elmVersion importInfo SyntaxSeparated cond
1450-
, indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) body
1451-
]
1452-
in
1453-
formatIf if'
1454-
|> andThen (map formatElseIf elseifs)
1455-
|> andThen
1456-
[ blankLine
1457-
, line $ keyword "else"
1458-
, indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated) (AST.Commented elsComments els [])
1459-
]
1460-
|> expressionParens AmbiguousEnd context
1461+
|> expressionParens AmbiguousEnd context
1462+
14611463

14621464
AST.Expression.Let defs bodyComments expr ->
14631465
let

tests/Parse/ExpressionTest.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ tests =
259259
]
260260

261261
, testGroup "if statement"
262-
[ example "" "if x then y else z" $ at 1 1 1 19 (If (Commented [] (at 1 4 1 5 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [],Commented [] (at 1 11 1 12 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) []) [] ([],at 1 18 1 19 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))))
263-
, example "comments" "if{-A-}x{-B-}then{-C-}y{-D-}else{-E-}if{-F-}x_{-G-}then{-H-}y_{-I-}else{-J-}z" $ at 1 1 1 78 (If (Commented [BlockComment ["A"]] (at 1 8 1 9 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [BlockComment ["B"]],Commented [BlockComment ["C"]] (at 1 23 1 24 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) [BlockComment ["D"]]) [([BlockComment ["E"]],(Commented [BlockComment ["F"]] (at 1 45 1 47 (VarExpr (VarRef [] $ LowercaseIdentifier "x_"))) [BlockComment ["G"]],Commented [BlockComment ["H"]] (at 1 61 1 63 (VarExpr (VarRef [] $ LowercaseIdentifier "y_"))) [BlockComment ["I"]]))] ([BlockComment ["J"]],at 1 77 1 78 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))))
264-
, example "else if" "if x1 then y1 else if x2 then y2 else if x3 then y3 else z" $ at 1 1 1 59 (If (Commented [] (at 1 4 1 6 (VarExpr (VarRef [] $ LowercaseIdentifier "x1"))) [],Commented [] (at 1 12 1 14 (VarExpr (VarRef [] $ LowercaseIdentifier "y1"))) []) [([],(Commented [] (at 1 23 1 25 (VarExpr (VarRef [] $ LowercaseIdentifier "x2"))) [],Commented [] (at 1 31 1 33 (VarExpr (VarRef [] $ LowercaseIdentifier "y2"))) [])),([],(Commented [] (at 1 42 1 44 (VarExpr (VarRef [] $ LowercaseIdentifier "x3"))) [],Commented [] (at 1 50 1 52 (VarExpr (VarRef [] $ LowercaseIdentifier "y3"))) []))] ([],at 1 58 1 59 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))))
265-
, example "newlines" "if\n x\n then\n y\n else\n z" $ at 1 1 6 3 (If (Commented [] (at 2 2 2 3 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [],Commented [] (at 4 2 4 3 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) []) [] ([],at 6 2 6 3 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))))
262+
[ example' "single line" "if x then y else z" "if x then y else z\n"
263+
, example' "else if" "if x then y else if x_ then y_ else z" "if x then\n y\n\nelse if x_ then\n y_\n\nelse\n z\n"
264+
, example' "new lines" "if\nx\nthen\ny\nelse\nz" "if x then\n y\n\nelse\n z\n"
265+
, example "" "if x then y else z" $ at 1 1 1 19 (If (Commented [] (at 1 4 1 5 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [],Commented [] (at 1 11 1 12 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) []) [] ([],at 1 18 1 19 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))) False)
266+
, example "comments" "if{-A-}x{-B-}then{-C-}y{-D-}else{-E-}if{-F-}x_{-G-}then{-H-}y_{-I-}else{-J-}z" $ at 1 1 1 78 (If (Commented [BlockComment ["A"]] (at 1 8 1 9 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [BlockComment ["B"]],Commented [BlockComment ["C"]] (at 1 23 1 24 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) [BlockComment ["D"]]) [([BlockComment ["E"]],(Commented [BlockComment ["F"]] (at 1 45 1 47 (VarExpr (VarRef [] $ LowercaseIdentifier "x_"))) [BlockComment ["G"]],Commented [BlockComment ["H"]] (at 1 61 1 63 (VarExpr (VarRef [] $ LowercaseIdentifier "y_"))) [BlockComment ["I"]]))] ([BlockComment ["J"]],at 1 77 1 78 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))) False)
267+
, example "else if" "if x1 then y1 else if x2 then y2 else if x3 then y3 else z" $ at 1 1 1 59 (If (Commented [] (at 1 4 1 6 (VarExpr (VarRef [] $ LowercaseIdentifier "x1"))) [],Commented [] (at 1 12 1 14 (VarExpr (VarRef [] $ LowercaseIdentifier "y1"))) []) [([],(Commented [] (at 1 23 1 25 (VarExpr (VarRef [] $ LowercaseIdentifier "x2"))) [],Commented [] (at 1 31 1 33 (VarExpr (VarRef [] $ LowercaseIdentifier "y2"))) [])),([],(Commented [] (at 1 42 1 44 (VarExpr (VarRef [] $ LowercaseIdentifier "x3"))) [],Commented [] (at 1 50 1 52 (VarExpr (VarRef [] $ LowercaseIdentifier "y3"))) []))] ([],at 1 58 1 59 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))) False)
268+
, example "newlines" "if\n x\n then\n y\n else\n z" $ at 1 1 6 3 (If (Commented [] (at 2 2 2 3 (VarExpr (VarRef [] $ LowercaseIdentifier "x"))) [],Commented [] (at 4 2 4 3 (VarExpr (VarRef [] $ LowercaseIdentifier "y"))) []) [] ([],at 6 2 6 3 (VarExpr (VarRef [] $ LowercaseIdentifier "z"))) True)
266269
]
267270

268271
, testGroup "let statement"

tests/test-files/transform/Elm-0.18/Examples.formatted.elm

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ ratio =
1616

1717

1818
bar =
19-
if
20-
if a then
21-
True
22-
23-
else
24-
False
25-
then
26-
"a"
27-
28-
else
29-
"b"
19+
if if a then True else False then "a" else "b"
3020

3121

3222
multilineList =

0 commit comments

Comments
 (0)