Deal with parens inside comments on 8.4.3

pull/189/head
Rupert Horlick 2018-10-17 17:01:31 -04:00
parent e1b43531a8
commit b249c10054
No known key found for this signature in database
GPG Key ID: D15A1B9A51513E0A
4 changed files with 56 additions and 19 deletions

View File

@ -19,6 +19,8 @@ import qualified Language.Haskell.GHC.ExactPrint.Annotate as ExactPrint.Annotate
import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint.Types import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint.Types
import Language.Haskell.GHC.ExactPrint.Types ( AnnKey, Annotation ) import Language.Haskell.GHC.ExactPrint.Types ( AnnKey, Annotation )
import GHC ( AnnKeywordId (..) )
import Language.Haskell.Brittany.Internal.LayouterBasics import Language.Haskell.Brittany.Internal.LayouterBasics
import Language.Haskell.Brittany.Internal.BackendUtils import Language.Haskell.Brittany.Internal.BackendUtils
import Language.Haskell.Brittany.Internal.Utils import Language.Haskell.Brittany.Internal.Utils
@ -173,6 +175,8 @@ layoutBriDocM = \case
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
"(" -> pure ()
")" -> layoutMoveToCommentPosX (x - 1)
_ -> layoutMoveToCommentPos y x _ -> layoutMoveToCommentPos y x
-- fixedX <- fixMoveToLineByIsNewline x -- fixedX <- fixMoveToLineByIsNewline x
-- replicateM_ fixedX layoutWriteNewline -- replicateM_ fixedX layoutWriteNewline
@ -244,6 +248,7 @@ layoutBriDocM = \case
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
")" -> layoutMoveToCommentPosX (x - 1)
_ -> layoutMoveToCommentPos y x _ -> layoutMoveToCommentPos y x
-- fixedX <- fixMoveToLineByIsNewline x -- fixedX <- fixMoveToLineByIsNewline x
-- replicateM_ fixedX layoutWriteNewline -- replicateM_ fixedX layoutWriteNewline

View File

@ -26,6 +26,7 @@ module Language.Haskell.Brittany.Internal.BackendUtils
, layoutAddSepSpace , layoutAddSepSpace
, layoutSetCommentCol , layoutSetCommentCol
, layoutMoveToCommentPos , layoutMoveToCommentPos
, layoutMoveToCommentPosX
, layoutIndentRestorePostComment , layoutIndentRestorePostComment
, moveToExactAnn , moveToExactAnn
, ppmMoveToExactLoc , ppmMoveToExactLoc
@ -200,6 +201,17 @@ layoutMoveToCommentPos y x = do
Right{} -> lstate_baseY state Right{} -> lstate_baseY state
} }
layoutMoveToCommentPosX
:: ( MonadMultiWriter Text.Builder.Builder m
, MonadMultiState LayoutState m
, MonadMultiWriter (Seq String) m
)
=> Int
-> m ()
layoutMoveToCommentPosX x = do
traceLocal ("layoutMoveToCommentPosX", x)
state <- mGet
mSet state { _lstate_addSepSpace = Just $ _lstate_indLevelLinger state + x }
-- | does _not_ add spaces to again reach the current base column. -- | does _not_ add spaces to again reach the current base column.
layoutWriteNewline layoutWriteNewline

View File

@ -63,6 +63,7 @@ module Language.Haskell.Brittany.Internal.LayouterBasics
, docSharedWrapper , docSharedWrapper
, hasAnyCommentsBelow , hasAnyCommentsBelow
, hasAnyCommentsConnected , hasAnyCommentsConnected
, hasAnnKeywordComment
, hasAnnKeyword , hasAnnKeyword
) )
where where
@ -296,6 +297,15 @@ hasAnyCommentsConnected ast = do
$ Map.elems $ Map.elems
$ anns $ anns
hasAnnKeywordComment
:: Data ast => GHC.Located ast -> AnnKeywordId -> ToBriDocM Bool
hasAnnKeywordComment ast annKeyword = do
anns <- mAsk
pure $ case Map.lookup (ExactPrint.Types.mkAnnKey ast) anns of
Nothing -> False
Just ann -> any hasK (extractAllComments ann)
where hasK = (== Just annKeyword) . ExactPrint.Types.commentOrigin . fst
hasAnnKeyword hasAnnKeyword
:: (Data a, MonadMultiReader (Map AnnKey Annotation) m) :: (Data a, MonadMultiReader (Map AnnKey Annotation) m)
=> Located a => Located a

View File

@ -633,34 +633,41 @@ layoutTyCl ltycl@(L _loc tycl) = case tycl of
Just (c, _) -> not (c == '(' || isUpper c) Just (c, _) -> not (c == '(' || isUpper c)
isInfix <- (isInfixTypeOp ||) <$> hasAnnKeyword name AnnBackquote isInfix <- (isInfixTypeOp ||) <$> hasAnnKeyword name AnnBackquote
#endif #endif
docWrapNode ltycl $ layoutSynDecl isInfix name (hsq_explicit vars) typ hasTrailingParen <- hasAnnKeywordComment ltycl AnnCloseP
let parenWrapper = if hasTrailingParen
then appSep . docWrapNodeRest ltycl
else id
docWrapNodePrior ltycl
$ layoutSynDecl isInfix parenWrapper name (hsq_explicit vars) typ
_ -> briDocByExactNoComment ltycl _ -> briDocByExactNoComment ltycl
layoutSynDecl layoutSynDecl
:: Bool :: Bool
-> (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> Located (IdP GhcPs) -> Located (IdP GhcPs)
-> [LHsTyVarBndr GhcPs] -> [LHsTyVarBndr GhcPs]
-> LHsType GhcPs -> LHsType GhcPs
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
layoutSynDecl isInfix name vars typ = do layoutSynDecl isInfix parenWrapper name vars typ = do
nameStr <- lrdrNameToTextAnn name nameStr <- lrdrNameToTextAnn name
let let
lhs = if isInfix lhs = if isInfix
then do then do
let let
(a : b : rest) = vars (a : b : rest) = vars
hasOwnParens <- hasAnnKeywordComment a AnnOpenP
-- This isn't quite right, but does give syntactically valid results -- This isn't quite right, but does give syntactically valid results
hasParens = not $ null rest let needsParens = not $ null rest || hasOwnParens
docSeq parenWrapper . docSeq
$ [ appSep $ docLit $ Text.pack "type" $ [ appSep $ docLit $ Text.pack "type"
, appSep , appSep
. docSeq . docSeq
$ [ docParenL | hasParens ] $ [ docParenL | needsParens ]
++ [ appSep $ layoutTyVarBndr a ++ [ appSep $ layoutTyVarBndr a
, appSep $ docLit nameStr , appSep $ docLit nameStr
, layoutTyVarBndr b , layoutTyVarBndr b
] ]
++ [ docParenR | hasParens ] ++ [ docParenR | needsParens ]
] ]
++ fmap (appSep . layoutTyVarBndr) rest ++ fmap (appSep . layoutTyVarBndr) rest
else else
@ -679,19 +686,22 @@ layoutSynDecl isInfix name vars typ = do
] ]
layoutTyVarBndr :: ToBriDoc HsTyVarBndr layoutTyVarBndr :: ToBriDoc HsTyVarBndr
layoutTyVarBndr (L _ bndr) = case bndr of layoutTyVarBndr lbndr@(L _ bndr) = do
UserTyVar name -> do needsPriorSpace <- hasAnnKeywordComment lbndr AnnCloseP
nameStr <- lrdrNameToTextAnn name docWrapNodePrior lbndr $ case bndr of
docLit nameStr UserTyVar name -> do
KindedTyVar name kind -> do nameStr <- lrdrNameToTextAnn name
nameStr <- lrdrNameToTextAnn name docSeq $ [ docSeparator | needsPriorSpace ] ++ [docLit nameStr]
docSeq KindedTyVar name kind -> do
[ docLit $ Text.pack "(" nameStr <- lrdrNameToTextAnn name
, appSep $ docLit nameStr docSeq
, appSep . docLit $ Text.pack "::" $ [ docSeparator | needsPriorSpace ]
, docForceSingleline $ layoutType kind ++ [ docLit $ Text.pack "("
, docLit $ Text.pack ")" , appSep $ docLit nameStr
] , appSep . docLit $ Text.pack "::"
, docForceSingleline $ layoutType kind
, docLit $ Text.pack ")"
]
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------