@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE PatternSynonyms #-}
33{-# LANGUAGE RecordWildCards #-}
4+{-# LANGUAGE ViewPatterns #-}
45
56-- | Rendering of declarations.
67
@@ -29,23 +30,24 @@ import Ormolu.Printer.Meat.Declaration.Value
2930import Ormolu.Printer.Meat.Type
3031import Ormolu.Utils
3132
32−p_hsDecls :: [LHsDecl GhcPs] -> R ()
33−p_hsDecls decls =
34− forM_ (zip decls ((Just <$> drop 1 decls) ++ [Nothing])) $
35− \(d, md) -> do
36− case md of
37− Nothing -> located d p_hsDecl
38− Just d' ->
39− if separatedDecls (unLoc d) (unLoc d')
40− then line (located d p_hsDecl)
41− else located d p_hsDecl
33+p_hsDecls :: FamilyStyle -> [LHsDecl GhcPs] -> R ()
34+p_hsDecls style decls =
35+ forM_ (zip decls ((Just <$> drop 1 decls) ++ [Nothing])) $ \(d, md) ->
36+ case md of
37+ Nothing -> located d pDecl
38+ Just d' ->
39+ if separatedDecls (unLoc d) (unLoc d')
40+ then line (located d pDecl)
41+ else located d pDecl
42+ where
43+ pDecl = p_hsDecl style
4244
43−p_hsDecl :: HsDecl GhcPs -> R ()
44−p_hsDecl = \case
45− TyClD NoExt x -> p_tyClDecl x
45+p_hsDecl :: FamilyStyle -> HsDecl GhcPs -> R ()
46+p_hsDecl style = \case
47+ TyClD NoExt x -> p_tyClDecl style x
4648 ValD NoExt x -> p_valDecl x
4749 SigD NoExt x -> p_sigDecl x
48− InstD NoExt x -> p_instDecl x
50+ InstD NoExt x -> p_instDecl style x
4951 DerivD NoExt x -> p_derivDecl x
5052 DefD NoExt x -> p_defaultDecl x
5153 ForD NoExt x -> p_foreignDecl x
@@ -57,9 +59,9 @@ p_hsDecl = \case
5759 RoleAnnotD NoExt x -> p_roleAnnot x
5860 XHsDecl _ -> notImplemented "XHsDecl"
5961
60−p_tyClDecl :: TyClDecl GhcPs -> R ()
61−p_tyClDecl = \case
62− FamDecl NoExt x -> p_famDecl Free x
62+p_tyClDecl :: FamilyStyle -> TyClDecl GhcPs -> R ()
63+p_tyClDecl style = \case
64+ FamDecl NoExt x -> p_famDecl style x
6365 SynDecl {..} -> p_synDecl tcdLName tcdTyVars tcdRhs
6466 DataDecl {..} ->
6567 p_dataDecl Associated tcdLName (tyVarsToTypes tcdTyVars) tcdDataDefn
@@ -76,11 +78,11 @@ p_tyClDecl = \case
7678 tcdATDefs
7779 XTyClDecl {} -> notImplemented "XTyClDecl"
7880
79−p_instDecl :: InstDecl GhcPs -> R ()
80−p_instDecl = \case
81+p_instDecl :: FamilyStyle -> InstDecl GhcPs -> R ()
82+p_instDecl style = \case
8183 ClsInstD NoExt x -> p_clsInstDecl x
82− TyFamInstD NoExt x -> p_tyFamInstDecl Free x
83− DataFamInstD NoExt x -> p_dataFamInstDecl Free x
84+ TyFamInstD NoExt x -> p_tyFamInstDecl style x
85+ DataFamInstD NoExt x -> p_dataFamInstDecl style x
8486 XInstDecl _ -> notImplemented "XInstDecl"
8587
8688p_derivDecl :: DerivDecl GhcPs -> R ()
@@ -114,8 +116,13 @@ pattern TypeSignature
114116 , InlinePragma
115117 , SpecializePragma
116118 , SCCPragma :: RdrName -> HsDecl GhcPs
117−pattern TypeSignature n <- SigD NoExt (TypeSig NoExt ((L _ n):_) _)
119+pattern TypeSignature n <- (sigRdrName -> Just n)
118120pattern FunctionBody n <- ValD NoExt (FunBind NoExt (L _ n) _ _ _)
119121pattern InlinePragma n <- SigD NoExt (InlineSig NoExt (L _ n) _)
120122pattern SpecializePragma n <- SigD NoExt (SpecSig NoExt (L _ n) _ _)
121123pattern SCCPragma n <- SigD NoExt (SCCFunSig NoExt _ (L _ n) _)
124+
125+sigRdrName :: HsDecl GhcPs -> Maybe RdrName
126+sigRdrName (SigD NoExt (TypeSig NoExt ((L _ n):_) _)) = Just n
127+sigRdrName (SigD NoExt (ClassOpSig NoExt _ ((L _ n):_) _)) = Just n
128+sigRdrName _ = Nothing