Update DataDecl to latest master

This update includes necessary code for handling deriving strategies.
remotes/ChickenProp/datadecl
Evan Rutledge Borden 2018-07-10 00:50:41 -04:00
parent 33fb0ef0ec
commit b02077e010
4 changed files with 44 additions and 17 deletions

View File

@ -350,6 +350,15 @@ data Foo = Bar
} }
deriving (Show, Eq, Monad, Functor, Traversable, Foldable) deriving (Show, Eq, Monad, Functor, Traversable, Foldable)
#test record multiple deriving strategies
data Foo = Bar
{ foo :: Baz
, bars :: Bizzz
}
deriving Show
deriving anyclass (Show, Eq, Monad, Functor)
deriving newtype (Traversable, Foldable)
############################################################################### ###############################################################################
############################################################################### ###############################################################################

View File

@ -40,7 +40,6 @@ import Language.Haskell.Brittany.Internal.LayouterBasics
import Language.Haskell.Brittany.Internal.Layouters.Type import Language.Haskell.Brittany.Internal.Layouters.Type
import Language.Haskell.Brittany.Internal.Layouters.Decl import Language.Haskell.Brittany.Internal.Layouters.Decl
import Language.Haskell.Brittany.Internal.Layouters.Module import Language.Haskell.Brittany.Internal.Layouters.Module
import Language.Haskell.Brittany.Internal.Layouters.DataDecl
import Language.Haskell.Brittany.Internal.Utils import Language.Haskell.Brittany.Internal.Utils
import Language.Haskell.Brittany.Internal.Backend import Language.Haskell.Brittany.Internal.Backend
import Language.Haskell.Brittany.Internal.BackendUtils import Language.Haskell.Brittany.Internal.BackendUtils

View File

@ -17,6 +17,7 @@ import Language.Haskell.Brittany.Internal.Config.Types
import RdrName ( RdrName(..) ) import RdrName ( RdrName(..) )
import GHC ( Located, runGhc, GenLocated(L), moduleNameString ) import GHC ( Located, runGhc, GenLocated(L), moduleNameString )
import BasicTypes (DerivStrategy(..))
import qualified GHC import qualified GHC
import HsSyn import HsSyn
import Name import Name
@ -138,24 +139,39 @@ createBndrDoc bs = do
] ]
createDerivingPar createDerivingPar
:: HsDeriving RdrName :: HsDeriving RdrName -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered
createDerivingPar mDerivs mainDoc = do createDerivingPar mDerivs mainDoc = do
case mDerivs of case mDerivs of
Nothing -> docLines [mainDoc] (L _ []) -> docLines [mainDoc]
Just (L _ [(HsIB _ t)]) -> do (L _ types) ->
docPar mainDoc $ docEnsureIndent BrIndentRegular $ docSeq docPar mainDoc
[docDeriving, docSeparator, layoutType t] $ docEnsureIndent BrIndentRegular
Just (L _ ts ) -> do $ docLines
docPar mainDoc $ docEnsureIndent BrIndentRegular $ docSeq $ derivingClause
[ docDeriving <$> types
, docSeparator where
, docLit $ Text.pack "(" handleStrategy = \case
, docSeq $ List.intersperse docCommaSep $ ts <&> \(HsIB _ t) -> (L _ StockStrategy ) -> docLit $ Text.pack ""
layoutType t (L _ AnyclassStrategy) -> docLit $ Text.pack "anyclass"
, docLit $ Text.pack ")" (L _ NewtypeStrategy ) -> docLit $ Text.pack "newtype"
] derivingClause (L _ (HsDerivingClause mStrategy types)) = case types of
(L _ []) -> docSeq []
(L _ ts) ->
let
tsLength = length ts
whenMoreThan1Type val =
if tsLength > 1 then docLit (Text.pack val) else docLit (Text.pack "")
in
docSeq
[ docDeriving
, docSeq
$ maybe [] ((docSeparator :) . pure . handleStrategy) mStrategy
, docSeparator
, whenMoreThan1Type "("
, docSeq $ List.intersperse docCommaSep $ ts <&> \(HsIB _ t _) ->
layoutType t
, whenMoreThan1Type ")"
]
docDeriving :: ToBriDocM BriDocNumbered docDeriving :: ToBriDocM BriDocNumbered
docDeriving = docLit $ Text.pack "deriving" docDeriving = docLit $ Text.pack "deriving"

View File

@ -41,6 +41,7 @@ import Language.Haskell.Brittany.Internal.Layouters.Type
import {-# SOURCE #-} Language.Haskell.Brittany.Internal.Layouters.Expr import {-# SOURCE #-} Language.Haskell.Brittany.Internal.Layouters.Expr
import {-# SOURCE #-} Language.Haskell.Brittany.Internal.Layouters.Stmt import {-# SOURCE #-} Language.Haskell.Brittany.Internal.Layouters.Stmt
import Language.Haskell.Brittany.Internal.Layouters.Pattern import Language.Haskell.Brittany.Internal.Layouters.Pattern
import Language.Haskell.Brittany.Internal.Layouters.DataDecl
import Bag ( mapBagM ) import Bag ( mapBagM )
@ -52,6 +53,8 @@ layoutDecl d@(L loc decl) = case decl of
ValD bind -> withTransformedAnns d $ layoutBind (L loc bind) >>= \case ValD bind -> withTransformedAnns d $ layoutBind (L loc bind) >>= \case
Left ns -> docLines $ return <$> ns Left ns -> docLines $ return <$> ns
Right n -> return n Right n -> return n
TyClD (DataDecl name tyVars _ dataDefn _ _) ->
withTransformedAnns d $ layoutDataDecl d name tyVars dataDefn
InstD (TyFamInstD{}) -> do InstD (TyFamInstD{}) -> do
-- this is a (temporary (..)) workaround for "type instance" decls -- this is a (temporary (..)) workaround for "type instance" decls
-- that do not round-trip through exactprint properly. -- that do not round-trip through exactprint properly.