on master help sign in
Use new operator style in types & declarations

``` a + b ``` instead of ``` a + b

commit159e5e7761dd0289a8f1eda173deb4c50435f4ad authorUtku Demir <me@utdemir.com> dateFri 16 Aug 2019, 11:59 (7 years ago) parentf86155a
6 files changed+54−24
data/examples/declaration/class/type-operators-out.hs+16−8 data/examples/declaration/class/type-operators.hs+7 data/examples/declaration/data/multiline-names-out.hs+4−4 data/examples/declaration/value/function/infix-out.hs+2−2 src/Ormolu/Printer/Meat/Common.hs+4−3 src/Ormolu/Printer/Meat/Type.hs+21−7
data/examples/declaration/class/type-operators-out.hs+16−8
@@ -11,25 +11,33 @@ class
1111class a :* b
1212
1313class
14− a :+ -- Before operator
15− b -- After operator
14+ a -- Before operator
15+ :+ b -- After operator
1616
1717class
18− ( f :.
19− g
18+ ( f
19+ :. g
2020 )
2121 a
2222
2323class a `Pair` b
2424
2525class
26− a `Sum`
27− b
26+ a
27+ `Sum` b
2828
2929class (f `Product` g) a
3030
3131class
32− ( f `Sum`
33− g
32+ ( f
33+ `Sum` g
3434 )
3535 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
3030class (
3131 f `Sum` g
3232 ) 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)
99data (f :* g) a = f a :* g a
1010
1111data
12− ( f :+
13− g
12+ ( f
13+ :+ g
1414 )
1515 a
1616 = L (f a)
@@ -21,8 +21,8 @@ data a `Arrow` b = Arrow' (a -> b)
2121data (f `Product` g) a = f a `Product` g a
2222
2323data
24− ( f `Sum`
25− g
24+ ( f
25+ `Sum` g
2626 )
2727 a
2828 = L' (f a)
data/examples/declaration/value/function/infix-out.hs+2−2
@@ -8,5 +8,5 @@ bar :: Int -> Int -> Int -> Int
88(x `bar` y) z = z
99
1010multiline :: Int -> Int -> Int
11−x `multiline`
12− y = z
11+x
12+ `multiline` y = z
src/Ormolu/Printer/Meat/Common.hs+4−3
@@ -124,10 +124,11 @@ p_infixDefHelper isInfix inci' name args =
124124 else parens
125125 parens' $ do
126126 p0
127− space
128− name
129127 breakpoint
130− inci' p1
128+ inci $ sitcc $ do
129+ name
130+ space
131+ p1
131132 unless (null ps) . inci' $ do
132133 breakpoint
133134 sitcc (sep breakpoint sitcc ps)
src/Ormolu/Printer/Meat/Type.hs+21−7
@@ -17,6 +17,7 @@ import GHC
1717import Ormolu.Printer.Combinators
1818import Ormolu.Printer.Meat.Common
1919import Ormolu.Utils
20+import SrcLoc (combineSrcSpans)
2021import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration.Value (p_hsSplice)
2122
2223p_hsType :: HsType GhcPs -> R ()
@@ -66,13 +67,26 @@ p_hsType = \case
6667 HsSumTy NoExt xs ->
6768 parensHash . sitcc $
6869 sep (txt "| " >> breakpoint') (sitcc . located' p_hsType) xs
69− HsOpTy NoExt x op y -> do
70− located x p_hsType
71− breakpoint
72− inci $ do
73− p_rdrName op
74− space
75− located y p_hsType
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
84+ located x p_hsType
85+ breakpoint
86+ inci $ do
87+ p_rdrName op
88+ space
89+ located y p_hsType
7690 HsParTy NoExt (L _ t@HsKindSig {}) ->
7791 -- NOTE Kind signatures already put parentheses around in all cases, so
7892 -- skip this layer of parentheses. The reason for this behavior is that