Support same-line let decl when indentAmount>=4

pull/141/head
Lennart Spitzner 2018-05-01 23:21:31 +02:00
parent dd53948a23
commit 4973298f30
1 changed files with 50 additions and 39 deletions

View File

@ -13,7 +13,10 @@ import Language.Haskell.Brittany.Internal.Types
import Language.Haskell.Brittany.Internal.LayouterBasics import Language.Haskell.Brittany.Internal.LayouterBasics
import Language.Haskell.Brittany.Internal.Config.Types import Language.Haskell.Brittany.Internal.Config.Types
import GHC ( runGhc, GenLocated(L), moduleNameString ) import GHC ( runGhc
, GenLocated(L)
, moduleNameString
)
import HsSyn import HsSyn
import Name import Name
import qualified FastString import qualified FastString
@ -28,6 +31,8 @@ import {-# SOURCE #-} Language.Haskell.Brittany.Internal.Layouters.Expr
layoutStmt :: ToBriDoc' (StmtLR GhcPs GhcPs (LHsExpr GhcPs)) layoutStmt :: ToBriDoc' (StmtLR GhcPs GhcPs (LHsExpr GhcPs))
layoutStmt lstmt@(L _ stmt) = do layoutStmt lstmt@(L _ stmt) = do
indentPolicy <- mAsk <&> _conf_layout .> _lconfig_indentPolicy .> confUnpack indentPolicy <- mAsk <&> _conf_layout .> _lconfig_indentPolicy .> confUnpack
indentAmount :: Int <-
mAsk <&> _conf_layout .> _lconfig_indentAmount .> confUnpack
docWrapNode lstmt $ case stmt of docWrapNode lstmt $ case stmt of
LastStmt body False _ -> do LastStmt body False _ -> do
layoutExpr body layoutExpr body
@ -47,45 +52,51 @@ layoutStmt lstmt@(L _ stmt) = do
$ docPar (docLit $ Text.pack "<-") (expDoc) $ docPar (docLit $ Text.pack "<-") (expDoc)
] ]
] ]
LetStmt binds -> layoutLocalBinds binds >>= \case LetStmt binds -> do
Nothing -> docLit $ Text.pack "let" -- i just tested let isFree = indentPolicy == IndentPolicyFree
-- it, and it is let indentFourPlus = indentAmount >= 4
-- indeed allowed. layoutLocalBinds binds >>= \case
-- heh. Nothing -> docLit $ Text.pack "let"
Just [] -> docLit $ Text.pack "let" -- this probably never happens -- i just tested the above, and it is indeed allowed. heh.
Just [bindDoc] -> docAlt Just [] -> docLit $ Text.pack "let" -- this probably never happens
[ -- let bind = expr Just [bindDoc] -> docAlt
docCols [ -- let bind = expr
ColDoLet docCols
[ appSep $ docLit $ Text.pack "let" ColDoLet
, let f = case indentPolicy of [ appSep $ docLit $ Text.pack "let"
IndentPolicyFree -> docSetBaseAndIndent , let
IndentPolicyLeft -> docForceSingleline f = case indentPolicy of
IndentPolicyMultiple -> docForceSingleline IndentPolicyFree -> docSetBaseAndIndent
in f $ return bindDoc IndentPolicyLeft -> docForceSingleline
IndentPolicyMultiple | indentFourPlus -> docSetBaseAndIndent
| otherwise -> docForceSingleline
in f $ return bindDoc
]
, -- let
-- bind = expr
docAddBaseY BrIndentRegular $ docPar
(docLit $ Text.pack "let")
(docSetBaseAndIndent $ return bindDoc)
] ]
, -- let Just bindDocs -> runFilteredAlternative $ do
-- bind = expr -- let aaa = expra
docAddBaseY BrIndentRegular $ docPar -- bbb = exprb
(docLit $ Text.pack "let") -- ccc = exprc
(docSetBaseAndIndent $ return bindDoc) addAlternativeCond (isFree || indentFourPlus) $ docSeq
] [ appSep $ docLit $ Text.pack "let"
Just bindDocs -> runFilteredAlternative $ do , let f = if indentFourPlus
-- let aaa = expra then docEnsureIndent BrIndentRegular
-- bbb = exprb else docSetBaseAndIndent
-- ccc = exprc in f $ docLines $ return <$> bindDocs
-- TODO: Allow this for IndentPolicyMultiple when indentAmount = 4 ]
addAlternativeCond (indentPolicy == IndentPolicyFree) $ docSeq -- let
[ appSep $ docLit $ Text.pack "let" -- aaa = expra
, docSetBaseAndIndent $ docLines $ return <$> bindDocs -- bbb = exprb
] -- ccc = exprc
-- let addAlternativeCond (not indentFourPlus)
-- aaa = expra $ docAddBaseY BrIndentRegular
-- bbb = exprb $ docPar (docLit $ Text.pack "let")
-- ccc = exprc (docSetBaseAndIndent $ docLines $ return <$> bindDocs)
addAlternative $ docAddBaseY BrIndentRegular $ docPar
(docLit $ Text.pack "let")
(docSetBaseAndIndent $ docLines $ return <$> bindDocs)
RecStmt stmts _ _ _ _ _ _ _ _ _ -> runFilteredAlternative $ do RecStmt stmts _ _ _ _ _ _ _ _ _ -> runFilteredAlternative $ do
-- rec stmt1 -- rec stmt1
-- stmt2 -- stmt2