Simplify/Optimize Decl:layoutPatternBindFinal

ghc92
Lennart Spitzner 2023-04-20 17:05:57 +00:00
parent 156b5cf407
commit 56e7d6b5b9
1 changed files with 24 additions and 47 deletions

View File

@ -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.