Skip to content

Commit 31f33b7

Browse files
committed
Allow single-line if expressions
Resolves #209
1 parent 3b2a0fd commit 31f33b7

File tree

10 files changed

+93
-75
lines changed

10 files changed

+93
-75
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) Multiline
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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,17 @@ 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+
return $ E.If first rest final $
199+
case (firstMultiline, restMultiline, finalMultiline) of
200+
(JoinAll, JoinAll, JoinAll) ->
201+
JoinAll
202+
203+
_ ->
204+
SplitAll
199205

200206

201207
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+
(AST.JoinAll, 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ 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 "" "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"))) JoinAll)
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"))) JoinAll)
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"))) JoinAll)
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"))) SplitAll)
266266
]
267267

268268
, testGroup "let statement"

tests/test-files/good/Elm-0.19/AllSyntax/Expressions.elm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ lambdaWithMultilinePattern =
273273
ifStatement =
274274
let
275275
a =
276+
if True then 1 else 2
277+
278+
b =
279+
if True then
280+
1
281+
282+
else
283+
2
284+
285+
c =
276286
if True then
277287
1
278288

@@ -282,7 +292,10 @@ ifStatement =
282292
else
283293
3
284294

285-
b =
295+
d =
296+
if {- B -} True then 1 else 2
297+
298+
e =
286299
if {- C -} True {- D -} then
287300
{- E -}
288301
1
@@ -297,7 +310,7 @@ ifStatement =
297310
{- L -}
298311
3
299312

300-
c =
313+
f =
301314
if
302315
--C
303316
True

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,15 @@ module Main exposing (bar, multilineList, ratio)
22

33

44
ratio =
5-
graphHeight
6-
/ (if range == 0 then
7-
0.1
8-
9-
else
10-
toFloat range
11-
)
5+
graphHeight / (if range == 0 then 0.1 else toFloat range)
126

137

148

159
-- foo=(case x of {True->1;False->3})
1610

1711

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

3115

3216
multilineList =
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x = if True then 1 else if False then 2 else 3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Main exposing (x)
2+
3+
4+
x =
5+
if True then
6+
1
7+
8+
else if False then
9+
2
10+
11+
else
12+
3

0 commit comments

Comments
 (0)