Merge pull request #95 from eborden/datadecl

Format data declarations
pull/105/head
Lennart Spitzner 2018-01-13 17:10:32 +01:00 committed by GitHub
commit 55f636fcf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 160 additions and 93 deletions

View File

@ -312,6 +312,47 @@ func = f
f = id f = id
###############################################################################
###############################################################################
###############################################################################
#group data type declarations
###############################################################################
###############################################################################
###############################################################################
#test single record
data Foo = Bar { foo :: Baz }
#test record multiple names
data Foo = Bar { foo, bar :: Baz }
#test record multiple types
data Foo = Bar
{ foo :: Baz
, bar :: Bizzz
}
#test record multiple types and names
data Foo = Bar
{ foo, biz :: Baz
, bar :: Bizzz
}
#test record multiple types deriving
data Foo = Bar
{ foo :: Baz
, bar :: Bizzz
}
deriving Show
#test record multiple types deriving
data Foo = Bar
{ foo :: Baz
, bar :: Bizzz
}
deriving (Show, Eq, Monad, Functor, Traversable, Foldable)
############################################################################### ###############################################################################
############################################################################### ###############################################################################
############################################################################### ###############################################################################

View File

@ -103,7 +103,7 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
_ -> briDocByExact ld _ -> briDocByExact ld
_ -> briDocByExactNoComment ld _ -> briDocByExactNoComment ld
where
createContextDoc :: HsContext RdrName -> ToBriDocM BriDocNumbered createContextDoc :: HsContext RdrName -> ToBriDocM BriDocNumbered
createContextDoc [] = docEmpty createContextDoc [] = docEmpty
createContextDoc [t] = createContextDoc [t] =
@ -114,6 +114,7 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
, docLit (Text.pack ") =>") , docLit (Text.pack ") =>")
, docSeparator , docSeparator
] ]
createBndrDoc :: [LHsTyVarBndr RdrName] -> ToBriDocM BriDocNumbered createBndrDoc :: [LHsTyVarBndr RdrName] -> ToBriDocM BriDocNumbered
createBndrDoc bs = do createBndrDoc bs = do
tyVarDocs <- bs `forM` \case tyVarDocs <- bs `forM` \case
@ -135,6 +136,7 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
, kind , kind
, docLit (Text.pack ")") , docLit (Text.pack ")")
] ]
createDerivingPar createDerivingPar
:: HsDeriving RdrName :: HsDeriving RdrName
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
@ -143,17 +145,21 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
case mDerivs of case mDerivs of
Nothing -> docLines [mainDoc] Nothing -> docLines [mainDoc]
Just (L _ [(HsIB _ t)]) -> do Just (L _ [(HsIB _ t)]) -> do
docAddBaseY BrIndentRegular $ docPar mainDoc $ docSeq docPar mainDoc $ docEnsureIndent BrIndentRegular $ docSeq
[docLit $ Text.pack "deriving", docSeparator, layoutType t] [docDeriving, docSeparator, layoutType t]
Just (L _ ts ) -> do Just (L _ ts ) -> do
docAddBaseY BrIndentRegular $ docPar mainDoc $ docSeq docPar mainDoc $ docEnsureIndent BrIndentRegular $ docSeq
[ docLit $ Text.pack "deriving" [ docDeriving
, docSeparator , docSeparator
, docLit $ Text.pack "(" , docLit $ Text.pack "("
, docSeq $ List.intersperse docCommaSep $ ts <&> \(HsIB _ t) -> , docSeq $ List.intersperse docCommaSep $ ts <&> \(HsIB _ t) ->
layoutType t layoutType t
, docLit $ Text.pack ")" , docLit $ Text.pack ")"
] ]
docDeriving :: ToBriDocM BriDocNumbered
docDeriving = docLit $ Text.pack "deriving"
createDetailsDoc createDetailsDoc
:: Text -> HsConDeclDetails RdrName -> (ToBriDocM BriDocNumbered) :: Text -> HsConDeclDetails RdrName -> (ToBriDocM BriDocNumbered)
createDetailsDoc consNameStr details = case details of createDetailsDoc consNameStr details = case details of
@ -162,25 +168,31 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
, docSeparator , docSeparator
, docSeq $ List.intersperse docSeparator $ args <&> layoutType , docSeq $ List.intersperse docSeparator $ args <&> layoutType
] ]
RecCon (L _ fields) -> docSeq RecCon (L _ []) -> docEmpty
[ appSep $ docLit $ Text.pack "{" RecCon (L _ [L _ (ConDeclField names t _)]) -> docSeq
, docSeq [ docLit consNameStr
$ List.intersperse docSeparator
$ fields
<&> \(L _ (ConDeclField names t _)) -> do
docSeq
[ docSeq
$ List.intersperse docCommaSep
$ names
<&> \(L _ (FieldOcc fieldName _)) ->
docLit =<< lrdrNameToTextAnn fieldName
, docSeparator , docSeparator
, docLit $ Text.pack "::" , appSep $ docLit $ Text.pack "{"
, createNamesAndTypeDoc names t
, docSeparator , docSeparator
, layoutType t
]
, docLit $ Text.pack "}" , docLit $ Text.pack "}"
] ]
RecCon (L _ (fstField:fields)) ->
docAddBaseY BrIndentRegular $ docPar
(docLit consNameStr)
(docLines
[ docSeq
[ docLit $ Text.pack "{ "
, let L _ (ConDeclField names t _) = fstField
in createNamesAndTypeDoc names t
]
, docLines
$ (\(L _ (ConDeclField names t _)) ->
docSeq [docCommaSep, createNamesAndTypeDoc names t])
<$> fields
, docLit $ Text.pack "}"
]
)
InfixCon arg1 arg2 -> docSeq InfixCon arg1 arg2 -> docSeq
[ layoutType arg1 [ layoutType arg1
, docSeparator , docSeparator
@ -188,6 +200,7 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
, docSeparator , docSeparator
, layoutType arg2 , layoutType arg2
] ]
createForallDoc :: Maybe (LHsQTyVars RdrName) -> ToBriDocM BriDocNumbered createForallDoc :: Maybe (LHsQTyVars RdrName) -> ToBriDocM BriDocNumbered
createForallDoc Nothing = docEmpty createForallDoc Nothing = docEmpty
createForallDoc (Just (HsQTvs _ bs _)) = do createForallDoc (Just (HsQTvs _ bs _)) = do
@ -199,3 +212,16 @@ layoutDataDecl ld name (HsQTvs _ bndrs _) defn = case defn of
, docSeparator , docSeparator
] ]
createNamesAndTypeDoc
:: [GenLocated t (FieldOcc u)] -> Located (HsType RdrName) -> ToBriDocM BriDocNumbered
createNamesAndTypeDoc names t = docSeq
[ docSeq
$ List.intersperse docCommaSep
$ names
<&> \(L _ (FieldOcc fieldName _)) ->
docLit =<< lrdrNameToTextAnn fieldName
, docSeparator
, docLit $ Text.pack "::"
, docSeparator
, layoutType t
]