From df422ffa5f9cbfad2d35652b3aca890e7ec7b617 Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Mon, 6 Mar 2017 15:55:12 +0100 Subject: [PATCH] Refactor/Cleanup layoutPatternBindFinal --- .../Haskell/Brittany/Layouters/Decl.hs | 61 +++++-------------- 1 file changed, 14 insertions(+), 47 deletions(-) diff --git a/src/Language/Haskell/Brittany/Layouters/Decl.hs b/src/Language/Haskell/Brittany/Layouters/Decl.hs index 66da00a..2acccd0 100644 --- a/src/Language/Haskell/Brittany/Layouters/Decl.hs +++ b/src/Language/Haskell/Brittany/Layouters/Decl.hs @@ -209,9 +209,15 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = $ return <$> ws ] + let singleLineGuardsDoc guards = appSep $ case guards of + [] -> docEmpty + [g] -> docSeq [appSep $ docLit $ Text.pack "|", return g] + gs -> docSeq + $ [appSep $ docLit $ Text.pack "|"] + ++ List.intersperse docCommaSep (return <$> gs) docAlt $ -- one-line solution - [ docCols + [ docCols (ColBindingLine alignmentToken) [ docSeq (patPartInline ++ [guardPart]) , docSeq @@ -221,16 +227,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = ] ] | [(guards, body, _bodyRaw)] <- [clauseDocs] - , let - guardPart = case guards of - [] -> docEmpty - [g] -> - docSeq [appSep $ docLit $ Text.pack "|", return g, docSeparator] - gs -> - docSeq - $ [appSep $ docLit $ Text.pack "|"] - ++ List.intersperse docCommaSep (return <$> gs) - ++ [docSeparator] + , let guardPart = singleLineGuardsDoc guards , wherePart <- case mWhereDocs of Nothing -> return @[] $ docEmpty Just [w] -> return @[] $ docSeq @@ -251,16 +248,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = ] ++ wherePartMultiLine | [(guards, body, _bodyRaw)] <- [clauseDocs] - , let - guardPart = case guards of - [] -> docEmpty - [g] -> - docSeq [appSep $ docLit $ Text.pack "|", return g, docSeparator] - gs -> - docSeq - $ [appSep $ docLit $ Text.pack "|"] - ++ List.intersperse docCommaSep (return <$> gs) - ++ [docSeparator] + , let guardPart = singleLineGuardsDoc guards , Data.Maybe.isJust mWhereDocs ] ++ -- two-line solution + where in next line(s) @@ -271,23 +259,14 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = ] ++ wherePartMultiLine | [(guards, body, _bodyRaw)] <- [clauseDocs] - , let - guardPart = case guards of - [] -> docEmpty - [g] -> - docSeq [appSep $ docLit $ Text.pack "|", return g, docSeparator] - gs -> - docSeq - $ [appSep $ docLit $ Text.pack "|"] - ++ List.intersperse docCommaSep (return <$> gs) - ++ [docSeparator] + , let guardPart = singleLineGuardsDoc guards ] ++ -- pattern and exactly one clause in single line, body as par; -- where in following lines [ docLines $ [ docCols (ColBindingLine alignmentToken) - [ docSeq (patPartInline ++ [appSep guardPart]) + [ docSeq (patPartInline ++ [guardPart]) , docSeq [ appSep $ return binderDoc , docForceParSpacing $ docAddBaseY BrIndentRegular $ return body @@ -300,13 +279,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = -- ] ++ wherePartMultiLine | [(guards, body, _bodyRaw)] <- [clauseDocs] - , let - guardPart = case guards of - [] -> docEmpty - [g] -> docSeq [appSep $ docLit $ Text.pack "|", return g] - gs -> docSeq $ [appSep $ docLit $ Text.pack "|"] ++ List.intersperse - docCommaSep - (return <$> gs) + , let guardPart = singleLineGuardsDoc guards ] ++ -- pattern and exactly one clause in single line, body in new line. [ docLines @@ -317,19 +290,13 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs = ] ++ wherePartMultiLine | [(guards, body, _)] <- [clauseDocs] - , let - guardPart = case guards of - [] -> docEmpty - [g] -> docSeq [appSep $ docLit $ Text.pack "|", return g] - gs -> docSeq $ [appSep $ docLit $ Text.pack "|"] ++ List.intersperse - docCommaSep - (return <$> gs) + , let guardPart = singleLineGuardsDoc guards ] ++ -- conservative approach: everything starts on the left. [ docLines $ [ patPartParWrap + $ docEnsureIndent BrIndentRegular $ docLines - $ fmap (docEnsureIndent BrIndentRegular) $ clauseDocs >>= \(guardDocs, bodyDoc, _) -> ( case guardDocs of