on master help sign in
Use new operator style in types & declarations
159e5e7Utku Demir
data/examples/declaration/class/type-operators-out.hs+16−8
@@ -11,25 +11,33 @@ class
11class a :* b
11class a :* b
12
12
13class
13class
14− a :+ -- Before operator
14+ a -- Before operator
15− b -- After operator
15+ :+ b -- After operator
16
16
17class
17class
18− ( f :.
18+ ( f
19− g
19+ :. g
20 )
20 )
21 a
21 a
22
22
23class a `Pair` b
23class a `Pair` b
24
24
25class
25class
26− a `Sum`
26+ a
27− b
27+ `Sum` b
28
28
29class (f `Product` g) a
29class (f `Product` g) a
30
30
31class
31class
32− ( f `Sum`
32+ ( f
33− g
33+ `Sum` g
34 )
34 )
35 a
35 a
36+
37+type API
38+ = "route1" :> ApiRoute1
39+ :<|> "route2"
40+ :> ApiRoute2 -- comment here
41+ :<|> OmitDocs
42+ :> "i"
43+ :> ASomething API
data/examples/declaration/class/type-operators.hs+7
@@ -30,3 +30,10 @@ class (f`Product`g)a
30class (
30class (
31 f `Sum` g
31 f `Sum` g
32 ) a
32 ) a
33+
34+type API
35+ = "route1" :> ApiRoute1
36+ :<|> "route2" :> ApiRoute2 -- comment here
37+ :<|> OmitDocs :> "i" :> ASomething API
38+
39+
data/examples/declaration/data/multiline-names-out.hs+4−4
@@ -9,8 +9,8 @@ data a :-> b = Arrow (a -> b)
9data (f :* g) a = f a :* g a
9data (f :* g) a = f a :* g a
10
10
11data
11data
12− ( f :+
12+ ( f
13− g
13+ :+ g
14 )
14 )
15 a
15 a
16 = L (f a)
16 = L (f a)
@@ -21,8 +21,8 @@ data a `Arrow` b = Arrow' (a -> b)
21data (f `Product` g) a = f a `Product` g a
21data (f `Product` g) a = f a `Product` g a
22
22
23data
23data
24− ( f `Sum`
24+ ( f
25− g
25+ `Sum` g
26 )
26 )
27 a
27 a
28 = L' (f a)
28 = L' (f a)
data/examples/declaration/value/function/infix-out.hs+2−2
@@ -8,5 +8,5 @@ bar :: Int -> Int -> Int -> Int
8(x `bar` y) z = z
8(x `bar` y) z = z
9
9
10multiline :: Int -> Int -> Int
10multiline :: Int -> Int -> Int
11−x `multiline`
11+x
12− y = z
12+ `multiline` y = z
src/Ormolu/Printer/Meat/Common.hs+4−3
@@ -124,10 +124,11 @@ p_infixDefHelper isInfix inci' name args =
124 else parens
124 else parens
125 parens' $ do
125 parens' $ do
126 p0
126 p0
127− space
128− name
129 breakpoint
127 breakpoint
128+ inci $ sitcc $ do
129+ name
130+ space
130− inci' p1
131+ p1
131 unless (null ps) . inci' $ do
132 unless (null ps) . inci' $ do
132 breakpoint
133 breakpoint
133 sitcc (sep breakpoint sitcc ps)
134 sitcc (sep breakpoint sitcc ps)
src/Ormolu/Printer/Meat/Type.hs+21−7
@@ -17,6 +17,7 @@ import GHC
17import Ormolu.Printer.Combinators
17import Ormolu.Printer.Combinators
18import Ormolu.Printer.Meat.Common
18import Ormolu.Printer.Meat.Common
19import Ormolu.Utils
19import Ormolu.Utils
20+import SrcLoc (combineSrcSpans)
20import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration.Value (p_hsSplice)
21import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration.Value (p_hsSplice)
21
22
22p_hsType :: HsType GhcPs -> R ()
23p_hsType :: HsType GhcPs -> R ()
@@ -66,13 +67,26 @@ p_hsType = \case
66 HsSumTy NoExt xs ->
67 HsSumTy NoExt xs ->
67 parensHash . sitcc $
68 parensHash . sitcc $
68 sep (txt "| " >> breakpoint') (sitcc . located' p_hsType) xs
69 sep (txt "| " >> breakpoint') (sitcc . located' p_hsType) xs
69− HsOpTy NoExt x op y -> do
70+ HsOpTy NoExt x op y -> sitcc $ do
71+ -- In the AST, type operators are right-associative instead of left-associative
72+ -- like value level operators. This makes similar constructs look inconsistent.
73+ -- Here, we shake the AST to convert right-associative tree to a left-associative
74+ -- one.
75+ case unLoc y of
76+ HsOpTy NoExt x' op' y' ->
77+ p_hsType $
78+ HsOpTy
79+ NoExt
80+ (L (combineSrcSpans (getLoc x) (getLoc x')) (HsOpTy NoExt x op x'))
81+ op'
82+ y'
83+ _ -> do
70− located x p_hsType
84+ located x p_hsType
71− breakpoint
85+ breakpoint
72− inci $ do
86+ inci $ do
73− p_rdrName op
87+ p_rdrName op
74− space
88+ space
75− located y p_hsType
89+ located y p_hsType
76 HsParTy NoExt (L _ t@HsKindSig {}) ->
90 HsParTy NoExt (L _ t@HsKindSig {}) ->
77 -- NOTE Kind signatures already put parentheses around in all cases, so
91 -- NOTE Kind signatures already put parentheses around in all cases, so
78 -- skip this layer of parentheses. The reason for this behavior is that
92 -- skip this layer of parentheses. The reason for this behavior is that