Fix comments in instance/type instances (#282)

pull/286/head
Lennart Spitzner 2020-02-23 23:09:03 +01:00
parent 7b5c0dc4e3
commit df2ee177b2
2 changed files with 37 additions and 30 deletions

View File

@ -869,3 +869,11 @@ createRedirectedProcess processConfig = do
, std_err = CreatePipe , std_err = CreatePipe
} }
foo foo
#test issue 282
instance HasDependencies SomeDataModel where
-- N.B. Here is a bunch of explanatory context about the relationship
-- between these data models or whatever.
type Dependencies SomeDataModel
= (SomeOtherDataModelId, SomeOtherOtherDataModelId)

View File

@ -72,7 +72,7 @@ layoutDecl d@(L loc decl) = case decl of
Right n -> return n Right n -> return n
TyClD _ tycl -> withTransformedAnns d $ layoutTyCl (L loc tycl) TyClD _ tycl -> withTransformedAnns d $ layoutTyCl (L loc tycl)
InstD _ (TyFamInstD _ tfid) -> InstD _ (TyFamInstD _ tfid) ->
withTransformedAnns d $ layoutTyFamInstDecl False (L loc tfid) withTransformedAnns d $ layoutTyFamInstDecl False d tfid
InstD _ (ClsInstD _ inst) -> InstD _ (ClsInstD _ inst) ->
withTransformedAnns d $ layoutClsInst (L loc inst) withTransformedAnns d $ layoutClsInst (L loc inst)
_ -> briDocByExactNoComment d _ -> briDocByExactNoComment d
@ -84,7 +84,7 @@ layoutDecl d@(L loc decl) = case decl of
Right n -> return n Right n -> return n
TyClD tycl -> withTransformedAnns d $ layoutTyCl (L loc tycl) TyClD tycl -> withTransformedAnns d $ layoutTyCl (L loc tycl)
InstD (TyFamInstD tfid) -> InstD (TyFamInstD tfid) ->
withTransformedAnns d $ layoutTyFamInstDecl False (L loc tfid) withTransformedAnns d $ layoutTyFamInstDecl False d tfid
InstD (ClsInstD inst) -> withTransformedAnns d $ layoutClsInst (L loc inst) InstD (ClsInstD inst) -> withTransformedAnns d $ layoutClsInst (L loc inst)
_ -> briDocByExactNoComment d _ -> briDocByExactNoComment d
#endif #endif
@ -941,39 +941,39 @@ layoutTyVarBndr needsSep lbndr@(L _ bndr) = do
layoutTyFamInstDecl :: Bool -> ToBriDoc TyFamInstDecl layoutTyFamInstDecl
layoutTyFamInstDecl inClass (L loc tfid) = do :: Data.Data.Data a
=> Bool
-> Located a
-> TyFamInstDecl GhcPs
-> ToBriDocM BriDocNumbered
layoutTyFamInstDecl inClass outerNode tfid = do
let let
#if MIN_VERSION_ghc(8,8,0) #if MIN_VERSION_ghc(8,8,0)
linst = L loc (TyFamInstD NoExt tfid) FamEqn _ name bndrsMay pats _fixity typ = hsib_body $ tfid_eqn tfid
feqn@(FamEqn _ name bndrsMay pats _fixity typ) = hsib_body $ tfid_eqn tfid
-- bndrsMay isJust e.g. with -- bndrsMay isJust e.g. with
-- type instance forall a . MyType (Maybe a) = Either () a -- type instance forall a . MyType (Maybe a) = Either () a
lfeqn = L loc feqn innerNode = outerNode
#elif MIN_VERSION_ghc(8,6,0) #elif MIN_VERSION_ghc(8,6,0)
linst = L loc (TyFamInstD NoExt tfid) FamEqn _ name pats _fixity typ = hsib_body $ tfid_eqn tfid
feqn@(FamEqn _ name pats _fixity typ) = hsib_body $ tfid_eqn tfid
bndrsMay = Nothing bndrsMay = Nothing
lfeqn = L loc feqn innerNode = outerNode
#elif MIN_VERSION_ghc(8,4,0) #elif MIN_VERSION_ghc(8,4,0)
linst = L loc (TyFamInstD tfid) FamEqn name pats _fixity typ = hsib_body $ tfid_eqn tfid
feqn@(FamEqn name pats _fixity typ) = hsib_body $ tfid_eqn tfid
bndrsMay = Nothing bndrsMay = Nothing
lfeqn = L loc feqn innerNode = outerNode
#elif MIN_VERSION_ghc(8,2,0) #elif MIN_VERSION_ghc(8,2,0)
linst = L loc (TyFamInstD tfid) innerNode@(L _ (TyFamEqn name boundPats _fixity typ)) = tfid_eqn tfid
lfeqn@(L _ (TyFamEqn name boundPats _fixity typ)) = tfid_eqn tfid
bndrsMay = Nothing bndrsMay = Nothing
pats = hsib_body boundPats pats = hsib_body boundPats
#else #else
linst = L loc (TyFamInstD tfid) innerNode@(L _ (TyFamEqn name boundPats typ)) = tfid_eqn tfid
lfeqn@(L _ (TyFamEqn name boundPats typ)) = tfid_eqn tfid
bndrsMay = Nothing bndrsMay = Nothing
pats = hsib_body boundPats pats = hsib_body boundPats
#endif #endif
docWrapNodePrior linst $ do docWrapNodePrior outerNode $ do
nameStr <- lrdrNameToTextAnn name nameStr <- lrdrNameToTextAnn name
needsParens <- hasAnnKeyword lfeqn AnnOpenP needsParens <- hasAnnKeyword outerNode AnnOpenP
let let
instanceDoc = if inClass instanceDoc = if inClass
then docLit $ Text.pack "type" then docLit $ Text.pack "type"
@ -987,9 +987,7 @@ layoutTyFamInstDecl inClass (L loc tfid) = do
++ processTyVarBndrsSingleline bndrDocs ++ processTyVarBndrsSingleline bndrDocs
) )
lhs = lhs =
docWrapNode lfeqn docWrapNode innerNode
. appSep
. docWrapNodeRest linst
. docSeq . docSeq
$ [appSep instanceDoc] $ [appSep instanceDoc]
++ [ makeForallDoc foralls | Just foralls <- [bndrsMay] ] ++ [ makeForallDoc foralls | Just foralls <- [bndrsMay] ]
@ -998,8 +996,8 @@ layoutTyFamInstDecl inClass (L loc tfid) = do
++ intersperse docSeparator (layoutHsTyPats pats) ++ intersperse docSeparator (layoutHsTyPats pats)
++ [ docParenR | needsParens ] ++ [ docParenR | needsParens ]
hasComments <- (||) hasComments <- (||)
<$> hasAnyRegularCommentsConnected lfeqn <$> hasAnyRegularCommentsConnected outerNode
<*> hasAnyRegularCommentsRest linst <*> hasAnyRegularCommentsRest innerNode
typeDoc <- docSharedWrapper layoutType typ typeDoc <- docSharedWrapper layoutType typ
layoutLhsAndType hasComments lhs "=" typeDoc layoutLhsAndType hasComments lhs "=" typeDoc
@ -1085,8 +1083,8 @@ layoutClsInst lcid@(L _ cid) = docLines
layoutAndLocateTyFamInsts layoutAndLocateTyFamInsts
:: ToBriDocC (TyFamInstDecl GhcPs) (Located BriDocNumbered) :: ToBriDocC (TyFamInstDecl GhcPs) (Located BriDocNumbered)
layoutAndLocateTyFamInsts ltfid@(L loc _) = layoutAndLocateTyFamInsts ltfid@(L loc tfid) =
L loc <$> layoutTyFamInstDecl True ltfid L loc <$> layoutTyFamInstDecl True ltfid tfid
layoutAndLocateDataFamInsts layoutAndLocateDataFamInsts
:: ToBriDocC (DataFamInstDecl GhcPs) (Located BriDocNumbered) :: ToBriDocC (DataFamInstDecl GhcPs) (Located BriDocNumbered)
@ -1168,13 +1166,12 @@ layoutLhsAndType
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
layoutLhsAndType hasComments lhs sep typeDoc = do layoutLhsAndType hasComments lhs sep typeDoc = do
let sepDoc = appSep . docLit $ Text.pack sep
runFilteredAlternative $ do runFilteredAlternative $ do
-- (separators probably are "=" or "::") -- (separators probably are "=" or "::")
-- lhs = type -- lhs = type
-- lhs :: type -- lhs :: type
addAlternativeCond (not hasComments) addAlternativeCond (not hasComments) $ docSeq
$ docSeq [lhs, sepDoc, docForceSingleline typeDoc] [lhs, docSeparator, docLitS sep, docSeparator, docForceSingleline typeDoc]
-- lhs -- lhs
-- :: typeA -- :: typeA
-- -> typeB -- -> typeB
@ -1183,4 +1180,6 @@ layoutLhsAndType hasComments lhs sep typeDoc = do
-- -> typeB -- -> typeB
addAlternative $ docAddBaseY BrIndentRegular $ docPar lhs $ docCols addAlternative $ docAddBaseY BrIndentRegular $ docPar lhs $ docCols
ColTyOpPrefix ColTyOpPrefix
[sepDoc, docAddBaseY (BrIndentSpecial (length sep + 1)) typeDoc] [ appSep $ docLitS sep
, docAddBaseY (BrIndentSpecial (length sep + 1)) typeDoc
]