@@ -8,6 +8,7 @@ module Ormolu.Printer.Meat.Declaration.Instance
88 ( p_clsInstDecl
99 , p_tyFamInstDecl
1010 , p_dataFamInstDecl
11+ , p_standaloneDerivDecl
1112 )
1213where
1314
@@ -26,16 +27,47 @@ import Ormolu.Printer.Meat.Declaration.Value
2627import Ormolu.Printer.Meat.Type
2728import Ormolu.Utils
2829
30+p_standaloneDerivDecl :: DerivDecl GhcPs -> R ()
31+p_standaloneDerivDecl DerivDecl {..} = do
32+ let typesAfterInstance = located (hsib_body (hswc_body deriv_type)) p_hsType
33+ instTypes toIndent = inci $ do
34+ txt "instance"
35+ breakpoint
36+ match_overlap_mode deriv_overlap_mode breakpoint
37+ if toIndent
38+ then inci typesAfterInstance
39+ else typesAfterInstance
40+ txt "deriving"
41+ case deriv_strategy of
42+ Nothing -> do
43+ space
44+ instTypes False
45+ Just l -> locatedVia Nothing l $ \case
46+ StockStrategy -> do
47+ txt " stock "
48+ instTypes False
49+ AnyclassStrategy -> do
50+ txt " anyclass "
51+ instTypes False
52+ NewtypeStrategy -> do
53+ txt " newtype "
54+ instTypes False
55+ ViaStrategy HsIB {..} -> do
56+ txt " via"
57+ breakpoint
58+ inci (located hsib_body p_hsType)
59+ breakpoint
60+ instTypes True
61+ ViaStrategy (XHsImplicitBndrs NoExt) ->
62+ notImplemented "XHsImplicitBndrs"
63+ newline
64+p_standaloneDerivDecl (XDerivDecl _) = notImplemented "XDerivDecl"
65+
2966p_clsInstDecl :: ClsInstDecl GhcPs -> R ()
3067p_clsInstDecl = \case
3168 ClsInstDecl {..} -> do
3269 txt "instance "
33− case unLoc <$> cid_overlap_mode of
34− Just Overlappable {} -> txt "{-# OVERLAPPABLE #-} "
35− Just Overlapping {} -> txt "{-# OVERLAPPING #-} "
36− Just Overlaps {} -> txt "{-# OVERLAPS #-} "
37− Just Incoherent {} -> txt "{-# INCOHERENT #-} "
38− _ -> pure ()
70+ match_overlap_mode cid_overlap_mode space
3971 case cid_poly_ty of
4072 HsIB {..} -> sitcc (located hsib_body p_hsType)
4173 XHsImplicitBndrs NoExt -> notImplemented "XHsImplicitBndrs"
@@ -81,3 +113,20 @@ p_dataFamInstDecl style = \case
81113 let HsIB {..} = dfid_eqn
82114 FamEqn {..} = hsib_body
83115 p_dataDecl style feqn_tycon feqn_pats feqn_rhs
116+
117+match_overlap_mode :: Maybe (Located OverlapMode) -> R () -> R ()
118+match_overlap_mode overlap_mode layoutStrategy =
119+ case unLoc <$> overlap_mode of
120+ Just Overlappable {} -> do
121+ txt "{-# OVERLAPPABLE #-}"
122+ layoutStrategy
123+ Just Overlapping {} -> do
124+ txt "{-# OVERLAPPING #-}"
125+ layoutStrategy
126+ Just Overlaps {} -> do
127+ txt "{-# OVERLAPS #-}"
128+ layoutStrategy
129+ Just Incoherent {} -> do
130+ txt "{-# INCOHERENT #-}"
131+ layoutStrategy
132+ _ -> pure ()