on master help sign in
Add newlines between most declarations in a type class instance
f9842f5yumiova
data/examples/declaration/instance/associated-data-out.hs+4
@@ -1,15 +1,19 @@
1{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE TypeFamilies #-}
2instance Foo Int where
2instance Foo Int where
3+
3 data Bar Int = IntBar Int Int
4 data Bar Int = IntBar Int Int
4
5
5instance Foo Double where
6instance Foo Double where
7+
6 newtype Bar Double
8 newtype Bar Double
7 = DoubleBar
9 = DoubleBar
8 Double
10 Double
9 Double
11 Double
10
12
11instance Foo [a] where
13instance Foo [a] where
14+
12 data Bar [a]
15 data Bar [a]
13 = ListBar [Bar a]
16 = ListBar [Bar a]
17+
14 data Baz [a]
18 data Baz [a]
15 = ListBaz
19 = ListBaz
data/examples/declaration/instance/associated-types-out.hs+3
@@ -1,12 +1,15 @@
1{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE TypeFamilies #-}
2instance Foo Int where
2instance Foo Int where
3+
3 type Bar Int = Double
4 type Bar Int = Double
4
5
5instance Foo Double where
6instance Foo Double where
7+
6 type
8 type
7 Bar
9 Bar
8 Double =
10 Double =
9 [Double]
11 [Double]
12+
10 type
13 type
11 Baz Double =
14 Baz Double =
12 [Double]
15 [Double]
data/examples/declaration/instance/contexts-out.hs+2
@@ -1,4 +1,5 @@
1instance Eq a => Eq [a] where
1instance Eq a => Eq [a] where
2+
2 (==) _ _ = False
3 (==) _ _ = False
3
4
4instance ( Ord a
5instance ( Ord a
@@ -6,4 +7,5 @@ instance ( Ord a
6 )
7 )
7 => Ord
8 => Ord
8 (a, b) where
9 (a, b) where
10+
9 compare _ _ = GT
11 compare _ _ = GT
data/examples/declaration/instance/instance-sigs-out.hs+4
@@ -1,9 +1,11 @@
1{-# LANGUAGE InstanceSigs #-}
1{-# LANGUAGE InstanceSigs #-}
2instance Eq Int where
2instance Eq Int where
3+
3 (==) :: Int -> Int -> Bool
4 (==) :: Int -> Int -> Bool
4 (==) _ _ = False
5 (==) _ _ = False
5
6
6instance Ord Int where
7instance Ord Int where
8+
7 compare
9 compare
8 :: Int
10 :: Int
9 -> Int
11 -> Int
@@ -14,10 +16,12 @@ instance Ord Int where
14 GT
16 GT
15
17
16instance Applicative [] where
18instance Applicative [] where
19+
17 pure
20 pure
18 :: a
21 :: a
19 -> [a]
22 -> [a]
20 pure a = [a]
23 pure a = [a]
24+
21 (<*>)
25 (<*>)
22 :: [a] -> [a] -> [a]
26 :: [a] -> [a] -> [a]
23 (<*>) _ _ = []
27 (<*>) _ _ = []
data/examples/declaration/instance/multi-parameter-out.hs+3
@@ -1,6 +1,9 @@
1instance MonadReader a ((->) a) where
1instance MonadReader a ((->) a) where
2+
2 ask = id
3 ask = id
3
4
4instance MonadState s (State s) where
5instance MonadState s (State s) where
6+
5 get = State.get
7 get = State.get
8+
6 put = State.put
9 put = State.put
data/examples/declaration/instance/overlappable-instances-out.hs+4
@@ -1,11 +1,15 @@
1instance {-# OVERLAPPABLE #-} Eq Int where
1instance {-# OVERLAPPABLE #-} Eq Int where
2+
2 (==) _ _ = False
3 (==) _ _ = False
3
4
4instance {-# OVERLAPPING #-} Ord Int where
5instance {-# OVERLAPPING #-} Ord Int where
6+
5 compare _ _ = GT
7 compare _ _ = GT
6
8
7instance {-# OVERLAPS #-} Eq Double where
9instance {-# OVERLAPS #-} Eq Double where
10+
8 (==) _ _ = False
11 (==) _ _ = False
9
12
10instance {-# INCOHERENT #-} Ord Double where
13instance {-# INCOHERENT #-} Ord Double where
14+
11 compare _ _ = GT
15 compare _ _ = GT
data/examples/declaration/instance/single-parameter-out.hs+3
@@ -1,7 +1,10 @@
1instance Monoid Int where
1instance Monoid Int where
2+
2 (<>) x y = x + y
3 (<>) x y = x + y
3
4
4instance Enum Int where
5instance Enum Int where
6+
5 fromEnum x = x
7 fromEnum x = x
8+
6 toEnum = \x ->
9 toEnum = \x ->
7 x
10 x
data/examples/declaration/signature/specialize/specialize-instance-out.hs+4
@@ -3,12 +3,16 @@ data Foo a = Foo a
3data VT v m r = VT v m r
3data VT v m r = VT v m r
4
4
5instance (Eq a) => Eq (Foo a) where
5instance (Eq a) => Eq (Foo a) where
6+
6 {-# SPECIALIZE instance Eq (Foo [(Int, Bar)]) #-}
7 {-# SPECIALIZE instance Eq (Foo [(Int, Bar)]) #-}
8+
7 (==) (Foo a) (Foo b) = (==) a b
9 (==) (Foo a) (Foo b) = (==) a b
8
10
9instance (Num r, V.Vector v r, Factored m r) => Num (VT v m r) where
11instance (Num r, V.Vector v r, Factored m r) => Num (VT v m r) where
12+
10 {-# SPECIALIZE instance
13 {-# SPECIALIZE instance
11 ( Factored m Int => Num (VT U.Vector m Int)
14 ( Factored m Int => Num (VT U.Vector m Int)
12 )
15 )
13 #-}
16 #-}
17+
14 VT x + VT y = VT $ V.zipWith (+) x y
18 VT x + VT y = VT $ V.zipWith (+) x y
src/Ormolu/Printer/Meat/Declaration/Instance.hs+19−22
@@ -15,17 +15,16 @@ where
15import BasicTypes
15import BasicTypes
16import Control.Arrow
16import Control.Arrow
17import Data.Foldable
17import Data.Foldable
18−import Data.Function
19import Data.List (sortBy)
18import Data.List (sortBy)
19+import Data.Ord (comparing)
20import GHC
20import GHC
21import Ormolu.Printer.Combinators
21import Ormolu.Printer.Combinators
22import Ormolu.Printer.Meat.Common
22import Ormolu.Printer.Meat.Common
23import Ormolu.Printer.Meat.Declaration.Data
23import Ormolu.Printer.Meat.Declaration.Data
24−import Ormolu.Printer.Meat.Declaration.Signature
25import Ormolu.Printer.Meat.Declaration.TypeFamily
24import Ormolu.Printer.Meat.Declaration.TypeFamily
26−import Ormolu.Printer.Meat.Declaration.Value
27import Ormolu.Printer.Meat.Type
25import Ormolu.Printer.Meat.Type
28import Ormolu.Utils
26import Ormolu.Utils
27+import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
29
28
30p_standaloneDerivDecl :: DerivDecl GhcPs -> R ()
29p_standaloneDerivDecl :: DerivDecl GhcPs -> R ()
31p_standaloneDerivDecl DerivDecl {..} = do
30p_standaloneDerivDecl DerivDecl {..} = do
@@ -75,26 +74,24 @@ p_clsInstDecl = \case
75 -- location order. This happens because different declarations are stored in
74 -- location order. This happens because different declarations are stored in
76 -- different lists. Consequently, to get all the declarations in proper
75 -- different lists. Consequently, to get all the declarations in proper
77 -- order, they need to be manually sorted.
76 -- order, they need to be manually sorted.
78− let binds = (getLoc &&& located' p_valDecl) <$> cid_binds
79− sigs = (getLoc &&& located' p_sigDecl) <$> cid_sigs
77+ let sigs = (getLoc &&& fmap (SigD NoExt)) <$> cid_sigs
80− tyfam_insts =
78+ vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cid_binds
81− (getLoc &&& located' (p_tyFamInstDecl Associated)) <$>
79+ tyFamInsts =
80+ ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt)
82− cid_tyfam_insts
81+ ) <$> cid_tyfam_insts
83− datafam_insts =
82+ dataFamInsts =
84− (getLoc &&& located' (p_dataFamInstDecl Associated)) <$>
83+ ( getLoc &&& fmap (InstD NoExt . DataFamInstD NoExt)
85− cid_datafam_insts
84+ ) <$> cid_datafam_insts
86− decls =
85+ allDecls =
87 snd <$>
86 snd <$>
88− sortBy
87+ sortBy (comparing fst) (sigs <> vals <> tyFamInsts <> dataFamInsts)
89− (compare `on` fst)
90− (toList binds <> sigs <> tyfam_insts <> datafam_insts)
91− if not (null decls)
88+ if not (null allDecls)
92− then do
89+ then do
93− txt " where"
90+ txt " where"
94− newline
91+ newline -- Ensure line is added after where clause.
95− inci (sequence_ decls)
92+ newline -- Add newline before first declaration.
96− else do
93+ inci (p_hsDecls Associated allDecls)
97− newline
94+ else newline
98 XClsInstDecl NoExt -> notImplemented "XClsInstDecl"
95 XClsInstDecl NoExt -> notImplemented "XClsInstDecl"
99
96
100p_tyFamInstDecl :: FamilyStyle -> TyFamInstDecl GhcPs -> R ()
97p_tyFamInstDecl :: FamilyStyle -> TyFamInstDecl GhcPs -> R ()