@@ -1407,57 +1407,9 @@ 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- _ ->
1423- stack1
1424- [ key
1425- , cond |> indent
1426- , line $ keyword " then"
1427- ]
1428-
1429- formatIf (cond, body) =
1430- stack1
1431- [ opening (line $ keyword " if" ) $ formatCommentedExpression elmVersion importInfo SyntaxSeparated cond
1432- , indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated ) body
1433- ]
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'
1445- ]
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
1410+ AST.Expression. If if' elseifs els multiline ->
1411+ formatIfExpression elmVersion importInfo if' elseifs els multiline
1412+ |> expressionParens AmbiguousEnd context
14611413
14621414 AST.Expression. Let defs bodyComments expr ->
14631415 let
@@ -2120,6 +2072,106 @@ formatComment comment =
21202072 AST. CommentTrickBlock c ->
21212073 mustBreak $ row [ punc " {--" , literal c, punc " -}" ]
21222074
2075+ formatIfExpression ::
2076+ ElmVersion
2077+ -> ImportInfo
2078+ -> AST.Expression. IfClause
2079+ -> [(AST. Comments , AST.Expression. IfClause )]
2080+ -> (AST. Comments , AST.Expression. Expr )
2081+ -> AST. Multiline
2082+ -> Box
2083+ formatIfExpression elmVersion importInfo if' elseifs (elsComments, els) multiline =
2084+ let
2085+ (cond, (AST. Commented preThen thenExpr postThen)) =
2086+ if'
2087+
2088+ then' =
2089+ concat $
2090+ [ Maybe. maybeToList $ formatComments preThen
2091+ , [ formatExpression elmVersion importInfo SyntaxSeparated thenExpr ]
2092+ , Maybe. maybeToList $ formatComments postThen
2093+ ]
2094+
2095+ else' =
2096+ concat $
2097+ [ Maybe. maybeToList $ formatComments elsComments
2098+ , [ formatExpression elmVersion importInfo SyntaxSeparated els ]
2099+ ]
2100+ in
2101+ case
2102+ ( multiline
2103+ , formatCommentedExpression elmVersion importInfo SyntaxSeparated cond
2104+ , allSingles then'
2105+ , elseifs
2106+ , allSingles else'
2107+ )
2108+ of
2109+ (AST. JoinAll , SingleLine cond', Right singleThens, [] , Right singleElses) ->
2110+ line $ row $ concat
2111+ [ [ keyword " if" , space, cond', space, keyword " then" , space ]
2112+ , List. intersperse space singleThens
2113+ , [ space, keyword " else" , space ]
2114+ , List. intersperse space singleElses
2115+ ]
2116+
2117+ (_, ifCond, thenBody, _, elseBody) ->
2118+ formatIfExpressionMultiline elmVersion importInfo ifCond thenBody elseifs elseBody
2119+
2120+
2121+ formatIfExpressionMultiline ::
2122+ ElmVersion
2123+ -> ImportInfo
2124+ -> Box
2125+ -> Either [Box ] [Line ]
2126+ -> [(AST. Comments , AST.Expression. IfClause )]
2127+ -> Either [Box ] [Line ]
2128+ -> Box
2129+ formatIfExpressionMultiline elmVersion importInfo ifCond thenBody elseifs elseBody =
2130+ let
2131+ opening (SingleLine key) (SingleLine cond') =
2132+ line $ row [ key, space, cond', space, keyword " then" ]
2133+ opening key cond' =
2134+ stack1
2135+ [ key
2136+ , cond' |> indent
2137+ , line $ keyword " then"
2138+ ]
2139+
2140+ boxesOrLinesToBox boxesOrLines =
2141+ ElmStructure. forceableSpaceSepOrStack1 True $
2142+ case boxesOrLines of
2143+ Right singleLines -> map line singleLines
2144+ Left boxes -> boxes
2145+
2146+ formatElseIf (ifComments, (cond', body')) =
2147+ let
2148+ key =
2149+ case (formatHeadCommented id (ifComments, line $ keyword " if" )) of
2150+ SingleLine key' ->
2151+ line $ row [ keyword " else" , space, key' ]
2152+ key' ->
2153+ stack1
2154+ [ line $ keyword " else"
2155+ , key'
2156+ ]
2157+ in
2158+ stack1
2159+ [ blankLine
2160+ , opening key $ formatCommentedExpression elmVersion importInfo SyntaxSeparated cond'
2161+ , indent $ formatCommented_ True (formatExpression elmVersion importInfo SyntaxSeparated ) body'
2162+ ]
2163+ in
2164+ stack1
2165+ [ opening (line $ keyword " if" ) ifCond
2166+ , indent $ boxesOrLinesToBox thenBody
2167+ ]
2168+ |> andThen (map formatElseIf elseifs)
2169+ |> andThen
2170+ [ blankLine
2171+ , line $ keyword " else"
2172+ , indent $ boxesOrLinesToBox elseBody
2173+ ]
2174+
21232175
21242176formatLiteral :: ElmVersion -> AST. Literal -> Box
21252177formatLiteral elmVersion lit =
0 commit comments