Various layouting fixes

pull/1/head
Lennart Spitzner 2016-08-04 15:12:05 +02:00
parent a82d38fa70
commit 87e81189c9
4 changed files with 58 additions and 29 deletions

View File

@ -18,6 +18,7 @@ where
import Language.Haskell.Brittany.Types import Language.Haskell.Brittany.Types
import Language.Haskell.Brittany.LayoutBasics import Language.Haskell.Brittany.LayoutBasics
import Language.Haskell.Brittany.Config.Types
import RdrName ( RdrName(..) ) import RdrName ( RdrName(..) )
import GHC ( runGhc, GenLocated(L), moduleNameString ) import GHC ( runGhc, GenLocated(L), moduleNameString )
@ -148,6 +149,11 @@ layoutPatternBindFinal binderDoc mPatDoc clauseDocs mWhereDocs = do
patPartParWrap = case mPatDoc of patPartParWrap = case mPatDoc of
Nothing -> id Nothing -> id
Just patDoc -> docPar (return patDoc) Just patDoc -> docPar (return patDoc)
_whereIndent <- mAsk
<&> _conf_layout
.> _lconfig_indentWhereSpecial
.> runIdentity
.> Bool.bool BrIndentRegular (BrIndentSpecial 1)
docAlt $ docAlt $
-- one-line solution -- one-line solution
[ docCols ColBindingLine [ docCols ColBindingLine
@ -171,14 +177,29 @@ layoutPatternBindFinal binderDoc mPatDoc clauseDocs mWhereDocs = do
$ [appSep $ docLit $ Text.pack "|"] $ [appSep $ docLit $ Text.pack "|"]
++ List.intersperse docCommaSep (return <$> gs) ++ List.intersperse docCommaSep (return <$> gs)
++ [docSeparator] ++ [docSeparator]
, wherePart <- case mWhereDocs of , let
Nothing -> pure docEmpty wherePart = case mWhereDocs of
Just [w] -> pure $ docSeq Nothing -> docEmpty
[ docSeparator Just [w] -> docAlt
, appSep $ docLit $ Text.pack "where" [ docSeq
, docSetBaseY $ docSetIndentLevel $ return w [ docSeparator
] , appSep $ docLit $ Text.pack "where"
_ -> [] , docSetIndentLevel $ docForceSingleline $ return w
]
, docAddBaseY BrIndentRegular
$ docPar docEmpty
$ docAddBaseY BrIndentRegular
$ docPar
(docLit $ Text.pack "where")
(docSetIndentLevel $ return w)
]
Just ws ->
docAddBaseY BrIndentRegular
$ docPar docEmpty
$ docAddBaseY BrIndentRegular
$ docPar
(docLit $ Text.pack "where")
(docSetIndentLevel $ docLines $ return <$> ws)
] ++ ] ++
-- two-line solution -- two-line solution
[ docLines [ docLines
@ -238,7 +259,10 @@ layoutPatternBindFinal binderDoc mPatDoc clauseDocs mWhereDocs = do
[ docAddBaseY BrIndentRegular [ docAddBaseY BrIndentRegular
$ docPar $ docPar
(docSeq (patPartInline ++ [appSep $ guardPart, return binderDoc])) (docSeq (patPartInline ++ [appSep $ guardPart, return binderDoc]))
(docLines $ [ return body ] ++ wherePart) ( docNonBottomSpacing
$ docLines
$ [ docAddBaseY BrIndentRegular $ return body ] ++ wherePart
)
| [(guards, body, _)] <- [clauseDocs] | [(guards, body, _)] <- [clauseDocs]
, let guardPart = case guards of , let guardPart = case guards of
[] -> docEmpty [] -> docEmpty

View File

@ -57,12 +57,15 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
let funcPatternPartLine = let funcPatternPartLine =
docCols ColCasePattern docCols ColCasePattern
$ (patDocs <&> (\p -> docSeq [docForceSingleline p, docSeparator])) $ (patDocs <&> (\p -> docSeq [docForceSingleline p, docSeparator]))
let lineMod = if isExpressionTypeHeadPar body
then id
else docForceSingleline
docAlt docAlt
[ docSeq [ docSeq
[ docLit $ Text.pack "\\" [ docLit $ Text.pack "\\"
, docWrapNode lmatch $ docForceSingleline funcPatternPartLine , docWrapNode lmatch $ docForceSingleline funcPatternPartLine
, appSep $ docLit $ Text.pack "->" , appSep $ docLit $ Text.pack "->"
, docWrapNode lgrhs $ docForceSingleline bodyDoc , docWrapNode lgrhs $ lineMod bodyDoc
] ]
, docAddBaseY BrIndentRegular , docAddBaseY BrIndentRegular
$ docPar $ docPar
@ -327,11 +330,11 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
, docLines , docLines
[ docSeq [ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docSetBaseY $ docSetIndentLevel $ return bindDoc , docSetIndentLevel $ return bindDoc
] ]
, docSeq , docSeq
[ appSep $ docLit $ Text.pack "in " [ appSep $ docLit $ Text.pack "in "
, docSetBaseY $ docSetIndentLevel $ expDoc1 , docSetIndentLevel $ expDoc1
] ]
] ]
, docLines , docLines
@ -349,11 +352,11 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
[ docLines [ docLines
[ docSeq [ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docSetBaseY $ docSetIndentLevel $ docLines $ return <$> bindDocs , docSetIndentLevel $ docLines $ return <$> bindDocs
] ]
, docSeq , docSeq
[ appSep $ docLit $ Text.pack "in " [ appSep $ docLit $ Text.pack "in "
, docSetBaseY $ docSetIndentLevel $ expDoc1 , docSetIndentLevel $ expDoc1
] ]
] ]
, docLines , docLines
@ -386,7 +389,7 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
, appSep $ docLit $ Text.pack "|" , appSep $ docLit $ Text.pack "|"
, docSeq $ List.intersperse docCommaSep , docSeq $ List.intersperse docCommaSep
$ fmap docForceSingleline $ List.init stmtDocs $ fmap docForceSingleline $ List.init stmtDocs
, docLit $ Text.pack "]" , docLit $ Text.pack " ]"
] ]
, let , let
start = docCols ColListComp start = docCols ColListComp
@ -396,7 +399,7 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
[appSep $ docLit $ Text.pack "|", s1] [appSep $ docLit $ Text.pack "|", s1]
lineM = sM <&> \d -> lineM = sM <&> \d ->
docCols ColListComp [docCommaSep, d] docCols ColListComp [docCommaSep, d]
end = docLit $ Text.pack " ]" end = docLit $ Text.pack "]"
in docSetBaseY $ docLines $ [start, line1] ++ lineM ++ [end] in docSetBaseY $ docLines $ [start, line1] ++ lineM ++ [end]
] ]
HsDo{} -> do HsDo{} -> do
@ -427,9 +430,9 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
docLit $ t <> Text.pack "{}" docLit $ t <> Text.pack "{}"
RecordCon lname _ _ (HsRecFields fs@(_:_) Nothing) -> do RecordCon lname _ _ (HsRecFields fs@(_:_) Nothing) -> do
let t = lrdrNameToText lname let t = lrdrNameToText lname
(fd1:fdr) <- fs `forM` \(L _ (HsRecField (L _ (FieldOcc lnameF _)) fExpr _)) -> do ((fd1l, fd1n, fd1e):fdr) <- fs `forM` \fieldl@(L _ (HsRecField (L _ (FieldOcc lnameF _)) fExpr _)) -> do
fExpDoc <- docSharedWrapper layoutExpr fExpr fExpDoc <- docSharedWrapper layoutExpr fExpr
return $ (lrdrNameToText lnameF, fExpDoc) return $ (fieldl, lrdrNameToText lnameF, fExpDoc)
docAlt docAlt
[ docAddBaseY BrIndentRegular [ docAddBaseY BrIndentRegular
$ docPar $ docPar
@ -437,16 +440,16 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
(docLines $ let (docLines $ let
line1 = docCols ColRecUpdate line1 = docCols ColRecUpdate
[ appSep $ docLit $ Text.pack "{" [ appSep $ docLit $ Text.pack "{"
, appSep $ docLit $ fst fd1 , appSep $ docLit $ fd1n
, docSeq [ appSep $ docLit $ Text.pack "=" , docSeq [ appSep $ docLit $ Text.pack "="
, docAddBaseY BrIndentRegular $ snd fd1 , docWrapNode fd1l $ docAddBaseY BrIndentRegular $ fd1e
] ]
] ]
lineR = fdr <&> \(fText, fDoc) -> docCols ColRecUpdate lineR = fdr <&> \(lfield, fText, fDoc) -> docCols ColRecUpdate
[ appSep $ docLit $ Text.pack "," [ appSep $ docLit $ Text.pack ","
, appSep $ docLit $ fText , appSep $ docLit $ fText
, docSeq [ appSep $ docLit $ Text.pack "=" , docSeq [ appSep $ docLit $ Text.pack "="
, docAddBaseY BrIndentRegular fDoc , docWrapNode lfield $ docAddBaseY BrIndentRegular fDoc
] ]
] ]
lineN = docLit $ Text.pack "}" lineN = docLit $ Text.pack "}"

View File

@ -46,28 +46,30 @@ layoutStmt lstmt@(L _ stmt) = docWrapNode lstmt $ case stmt of
Just [bindDoc] -> docAlt Just [bindDoc] -> docAlt
[ docCols ColDoLet [ docCols ColDoLet
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docSetBaseY $ docAddBaseY BrIndentRegular (return bindDoc) , docSetIndentLevel $ docAddBaseY BrIndentRegular (return bindDoc)
] ]
, docAddBaseY BrIndentRegular $ docPar , docAddBaseY BrIndentRegular $ docPar
(docLit $ Text.pack "let") (docLit $ Text.pack "let")
(return bindDoc) (docSetIndentLevel $ return bindDoc)
] ]
Just bindDocs@(bindDoc1:bindDocr) -> do Just bindDocs@(bindDoc1:bindDocr) -> do
-- TODO: the indentation here is screwed up. needs docSetIndentLevel and
-- SetBaseY based layouting, not cols.
docAlt docAlt
[ docLines [ docLines
$ (docCols ColDoLet $ (docCols ColDoLet
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docAddBaseY (BrIndentSpecial 6) (return bindDoc1) , docSetIndentLevel $ docAddBaseY BrIndentRegular (return bindDoc1)
]) ])
: (bindDocr <&> \bindDoc -> : (bindDocr <&> \bindDoc ->
docCols ColDoLet docCols ColDoLet
[ appSep $ docEmpty [ docEnsureIndent (BrIndentSpecial 4) docEmpty
, docAddBaseY (BrIndentSpecial 6) (return bindDoc) , docSetIndentLevel $ docAddBaseY BrIndentRegular (return bindDoc)
]) ])
, docAddBaseY BrIndentRegular , docAddBaseY BrIndentRegular
$ docPar $ docPar
(docLit $ Text.pack "let") (docLit $ Text.pack "let")
(docLines $ return <$> bindDocs) (docSetIndentLevel $ docAddBaseY BrIndentRegular $ docLines $ return <$> bindDocs)
] ]
BodyStmt expr _ _ _ -> do BodyStmt expr _ _ _ -> do
expDoc <- docSharedWrapper layoutExpr expr expDoc <- docSharedWrapper layoutExpr expr

View File

@ -217,7 +217,7 @@ layoutType ltype@(L _ typ) = docWrapNode ltype $ case typ of
[ docSeq [ docSeq
[ docForceSingleline contextDoc [ docForceSingleline contextDoc
, docLit $ Text.pack " => " , docLit $ Text.pack " => "
, typeDoc , docForceSingleline typeDoc
] ]
-- (Foo a b c) -- (Foo a b c)
-- => a b -- => a b