diff --git a/src-literatetests/tests-context-free.blt b/src-literatetests/tests-context-free.blt index 11e1bee..be8a8c9 100644 --- a/src-literatetests/tests-context-free.blt +++ b/src-literatetests/tests-context-free.blt @@ -382,8 +382,9 @@ func (x:+:xr) = x func | True = x #test multiple-clauses-1 -func x | x = simple expression - | otherwise = 0 +func x + | x = simple expression + | otherwise = 0 #test multiple-clauses-2 func x @@ -845,8 +846,9 @@ showPackageDetailedInfo pkginfo = ] #test issue 7a -isValidPosition position | validX && validY = Just position - | otherwise = Nothing +isValidPosition position + | validX && validY = Just position + | otherwise = Nothing #test issue-6-pattern-linebreak-validity ## this is ugly, but at least syntactically valid. diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Decl.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Decl.hs index 5073eab..30e26c2 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Decl.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Decl.hs @@ -265,9 +265,15 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha gs -> docSeq $ [appSep $ docLit $ Text.pack "|"] ++ List.intersperse docCommaSep (return <$> gs) - docAlt + + indentPolicy <- mAsk + <&> _conf_layout + .> _lconfig_indentPolicy + .> confUnpack + docAltFilter $ -- one-line solution - [ docCols + [ ( True + , docCols (ColBindingLine alignmentToken) [ docSeq (patPartInline ++ [guardPart]) , docSeq @@ -276,6 +282,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha , wherePart ] ] + ) | not hasComments , [(guards, body, _bodyRaw)] <- [clauseDocs] , let guardPart = singleLineGuardsDoc guards @@ -289,7 +296,8 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha _ -> [] ] ++ -- one-line solution + where in next line(s) - [ docLines + [ ( True + , docLines $ [ docCols (ColBindingLine alignmentToken) [ docSeq (patPartInline ++ [guardPart]) @@ -298,23 +306,27 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) | [(guards, body, _bodyRaw)] <- [clauseDocs] , let guardPart = singleLineGuardsDoc guards , Data.Maybe.isJust mWhereDocs ] ++ -- two-line solution + where in next line(s) - [ docLines + [ ( True + , docLines $ [ docForceSingleline $ docSeq (patPartInline ++ [guardPart, return binderDoc]) , docEnsureIndent BrIndentRegular $ docForceSingleline $ return body ] ++ wherePartMultiLine + ) | [(guards, body, _bodyRaw)] <- [clauseDocs] , let guardPart = singleLineGuardsDoc guards ] ++ -- pattern and exactly one clause in single line, body as par; -- where in following lines - [ docLines + [ ( True + , docLines $ [ docCols (ColBindingLine alignmentToken) [ docSeq (patPartInline ++ [guardPart]) @@ -329,24 +341,28 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha -- , docAddBaseY BrIndentRegular $ return body -- ] ++ wherePartMultiLine + ) | [(guards, body, _bodyRaw)] <- [clauseDocs] , let guardPart = singleLineGuardsDoc guards ] ++ -- pattern and exactly one clause in single line, body in new line. - [ docLines + [ ( True + , docLines $ [ docSeq (patPartInline ++ [guardPart, return binderDoc]) , docEnsureIndent BrIndentRegular $ docNonBottomSpacing $ (docAddBaseY BrIndentRegular $ return body) ] ++ wherePartMultiLine + ) | [(guards, body, _)] <- [clauseDocs] , let guardPart = singleLineGuardsDoc guards ] ++ -- multiple clauses added in-paragraph, each in a single line -- example: foo | bar = baz -- | lll = asd - [ docLines + [ ( indentPolicy /= IndentPolicyLeft + , docLines $ [ docSeq [ appSep $ docForceSingleline $ return patDoc , docSetBaseY @@ -370,10 +386,12 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) | Just patDoc <- [mPatDoc] ] ++ -- multiple clauses, each in a separate, single line - [ docLines + [ ( True + , docLines $ [ docAddBaseY BrIndentRegular $ patPartParWrap $ docLines @@ -396,10 +414,12 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) ] ++ -- multiple clauses, each with the guard(s) in a single line, body -- as a paragraph - [ docLines + [ ( True + , docLines $ [ docAddBaseY BrIndentRegular $ patPartParWrap $ docLines @@ -431,10 +451,12 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) ] ++ -- multiple clauses, each with the guard(s) in a single line, body -- in a new line as a paragraph - [ docLines + [ ( True + , docLines $ [ docAddBaseY BrIndentRegular $ patPartParWrap $ docLines @@ -464,9 +486,11 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) ] ++ -- conservative approach: everything starts on the left. - [ docLines + [ ( True + , docLines $ [ docAddBaseY BrIndentRegular $ patPartParWrap $ docLines @@ -494,4 +518,5 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha ] ] ++ wherePartMultiLine + ) ]