From 56e7d6b5b9d19760dcc356c03d73f203fcc25b9d Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Thu, 20 Apr 2023 17:05:57 +0000 Subject: [PATCH] Simplify/Optimize Decl:layoutPatternBindFinal --- .../Brittany/Internal/ToBriDoc/Decl.hs | 71 +++++++------------ 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs index 884262c..7f177f0 100644 --- a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs +++ b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs @@ -412,6 +412,8 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe pure [(Nothing, [], e)] Right grhss -> layoutGrhs `mapM` grhss + let multipleClauses = not $ null clauseDocs + runFilteredAlternative $ do case clauseDocs of @@ -492,12 +494,11 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe _ -> return () -- no alternatives exclusively when `length clauseDocs /= 1` case mPatDoc of - Nothing -> return () - Just patDoc -> + Just patDoc | multipleClauses, indentPolicy == IndentPolicyFree -> -- multiple clauses added in-paragraph, each in a single line -- example: foo | bar = baz -- | lll = asd - addAlternativeCond (indentPolicy == IndentPolicyFree) + addAlternative $ docLines $ [ docSeq [ appSep $ docForceSingleline $ return patDoc @@ -522,6 +523,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe ] ] ++ wherePartMultiLine + _ -> return () -- multiple clauses, each in a separate, single line addAlternative $ docLines @@ -558,29 +560,16 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe $ map docSetBaseY $ clauseDocs <&> \(grhsEpAnn, guardDocs, bodyDoc) -> -- TODO92 - docHandleComms grhsEpAnn $ docSeq - $ (case guardDocs of - [] -> [] - [g] -> - [ docForceSingleline $ docSeq - [appSep $ docLit $ Text.pack "|", return g] - ] - gs -> - [ docForceSingleline - $ docSeq - $ [appSep $ docLit $ Text.pack "|"] - ++ List.intersperse docCommaSep (return <$> gs) - ] - ) - ++ [ docSeparator - , docCols - ColOpPrefix - [ appSep $ return binderDoc - , docAddBaseY BrIndentRegular - $ docForceParSpacing - $ return bodyDoc - ] - ] + docHandleComms grhsEpAnn $ docSeq + [ singleLineGuardsDoc guardDocs + , docCols + ColOpPrefix + [ appSep $ return binderDoc + , docAddBaseY BrIndentRegular + $ docForceParSpacing + $ return bodyDoc + ] + ] ] ++ wherePartMultiLine -- multiple clauses, each with the guard(s) in a single line, body @@ -593,27 +582,15 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe $ map docSetBaseY $ clauseDocs >>= \(grhsEpAnn, guardDocs, bodyDoc) -> -- TODO92 - (case guardDocs of - [] -> [docHandleComms grhsEpAnn docEmpty] - [g] -> - [ docHandleComms grhsEpAnn $ docForceSingleline - $ docSeq [appSep $ docLit $ Text.pack "|", return g] - ] - gs -> - [ docHandleComms grhsEpAnn $ docForceSingleline - $ docSeq - $ [appSep $ docLit $ Text.pack "|"] - ++ List.intersperse docCommaSep (return <$> gs) - ] - ) - ++ [ docCols - ColOpPrefix - [ appSep $ return binderDoc - , docAddBaseY BrIndentRegular - $ docForceParSpacing - $ return bodyDoc - ] - ] + [ docHandleComms grhsEpAnn $ singleLineGuardsDoc guardDocs + , docCols + ColOpPrefix + [ appSep $ return binderDoc + , docAddBaseY BrIndentRegular + $ docForceParSpacing + $ return bodyDoc + ] + ] ] ++ wherePartMultiLine -- conservative approach: everything starts on the left.