Fix comment handling with let-in

remotes/felixonmars/release
Lennart Spitzner 2019-09-02 17:02:20 +02:00
parent 670b796edb
commit 8861f16624
2 changed files with 72 additions and 36 deletions

View File

@ -612,6 +612,39 @@ func =
_ -> True _ -> True
] ]
###############################################################################
###############################################################################
###############################################################################
#group expression.let
###############################################################################
###############################################################################
###############################################################################
#test single-bind-comment-long
testMethod foo bar baz qux =
let x = undefined foo bar baz qux qux baz bar :: String
-- some comment explaining the in expression
in undefined foo x :: String
#test single-bind-comment-short
testMethod foo bar baz qux =
let x = undefined :: String
-- some comment explaining the in expression
in undefined :: String
#test single-bind-comment-before
testMethod foo bar baz qux =
-- some comment explaining the in expression
let x = undefined :: String in undefined :: String
#test multiple-binds-comment
foo foo bar baz qux =
let a = 1
b = 2
c = 3
-- some comment explaining the in expression
in undefined :: String
############################################################################### ###############################################################################
############################################################################### ###############################################################################

View File

@ -725,10 +725,10 @@ layoutExpr lexpr@(L _ expr) = do
#else #else
HsLet binds exp1 -> do HsLet binds exp1 -> do
#endif #endif
expDoc1 <- docSharedWrapper layoutExpr exp1 expDoc1 <- docSharedWrapper layoutExpr exp1
-- We jump through some ugly hoops here to ensure proper sharing. -- We jump through some ugly hoops here to ensure proper sharing.
mBindDocs <- mapM (fmap (fmap return) . docWrapNodeRest lexpr . return) hasComments <- hasAnyCommentsBelow lexpr
=<< layoutLocalBinds binds mBindDocs <- fmap (fmap (fmap pure)) $ layoutLocalBinds binds
let let
ifIndentFreeElse :: a -> a -> a ifIndentFreeElse :: a -> a -> a
ifIndentFreeElse x y = ifIndentFreeElse x y =
@ -745,37 +745,38 @@ layoutExpr lexpr@(L _ expr) = do
-- if "let" is moved horizontally as part of the transformation, as the -- if "let" is moved horizontally as part of the transformation, as the
-- comments before the first let item are moved horizontally with it. -- comments before the first let item are moved horizontally with it.
docSetBaseAndIndent $ case mBindDocs of docSetBaseAndIndent $ case mBindDocs of
Just [bindDoc] -> docAlt Just [bindDoc] -> runFilteredAlternative $ do
[ docSeq addAlternativeCond (not hasComments) $ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, appSep $ docForceSingleline bindDoc , docNodeAnnKW lexpr (Just AnnLet)
, appSep $ docLit $ Text.pack "in" $ appSep $ docForceSingleline bindDoc
, docForceSingleline expDoc1 , appSep $ docLit $ Text.pack "in"
] , docForceSingleline expDoc1
, docLines ]
[ docAlt addAlternative $ docLines
[ docSeq [ docNodeAnnKW lexpr (Just AnnLet)
[ appSep $ docLit $ Text.pack "let" $ docAlt
, ifIndentFreeElse docSetBaseAndIndent docForceSingleline [ docSeq
$ bindDoc [ appSep $ docLit $ Text.pack "let"
] , ifIndentFreeElse docSetBaseAndIndent docForceSingleline
, docAddBaseY BrIndentRegular $ bindDoc
$ docPar ]
(docLit $ Text.pack "let") , docAddBaseY BrIndentRegular
(docSetBaseAndIndent bindDoc) $ docPar
] (docLit $ Text.pack "let")
, docAlt (docSetBaseAndIndent bindDoc)
[ docSeq ]
[ appSep $ docLit $ Text.pack $ ifIndentFreeElse "in " "in" , docAlt
, ifIndentFreeElse docSetBaseAndIndent docForceSingleline expDoc1 [ docSeq
] [ appSep $ docLit $ Text.pack $ ifIndentFreeElse "in " "in"
, docAddBaseY BrIndentRegular , ifIndentFreeElse docSetBaseAndIndent docForceSingleline expDoc1
$ docPar ]
(docLit $ Text.pack "in") , docAddBaseY BrIndentRegular
(docSetBaseY expDoc1) $ docPar
] (docLit $ Text.pack "in")
] (docSetBaseY expDoc1)
] ]
]
Just bindDocs@(_:_) -> runFilteredAlternative $ do Just bindDocs@(_:_) -> runFilteredAlternative $ do
--either --either
-- let -- let
@ -805,7 +806,8 @@ layoutExpr lexpr@(L _ expr) = do
IndentPolicyLeft -> docLines noHangingBinds IndentPolicyLeft -> docLines noHangingBinds
IndentPolicyMultiple -> docLines noHangingBinds IndentPolicyMultiple -> docLines noHangingBinds
IndentPolicyFree -> docLines IndentPolicyFree -> docLines
[ docSeq [ docNodeAnnKW lexpr (Just AnnLet)
$ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docSetBaseAndIndent $ docLines bindDocs , docSetBaseAndIndent $ docLines bindDocs
] ]
@ -816,7 +818,8 @@ layoutExpr lexpr@(L _ expr) = do
] ]
addAlternative addAlternative
$ docLines $ docLines
[ docAddBaseY BrIndentRegular [ docNodeAnnKW lexpr (Just AnnLet)
$ docAddBaseY BrIndentRegular
$ docPar $ docPar
(docLit $ Text.pack "let") (docLit $ Text.pack "let")
(docSetBaseAndIndent $ docLines $ bindDocs) (docSetBaseAndIndent $ docLines $ bindDocs)