on master help sign in
Support infix operator & multi-line class names
commit515251c7a5aaab912af7c7490dbb63ba2a068a05 authoryumiova <yumiova@protonmail.ch> dateWed 19 Jun 2019, 20:35 (7 years ago) parent73cc30c
12 files changed+142−39
data/examples/declaration/class/associated-data-out.hs+5−1 data/examples/declaration/class/associated-data.hs+4−1 data/examples/declaration/class/associated-types-out.hs+6−1 data/examples/declaration/class/associated-types.hs+6−2 data/examples/declaration/class/multi-parameters-out.hs+9−2 data/examples/declaration/class/multi-parameters.hs+8−8 data/examples/declaration/class/poly-kinded-classes-out.hs+4−3 data/examples/declaration/class/poly-kinded-classes.hs+3−2 data/examples/declaration/class/type-operators-out.hs+29 data/examples/declaration/class/type-operators.hs+32 src/Ormolu/Printer/Meat/Declaration.hs+1−3 src/Ormolu/Printer/Meat/Declaration/Class.hs+35−16
data/examples/declaration/class/associated-data-out.hs+5−1
@@ -14,7 +14,11 @@ class Bar a where
1414-- | Something more.
1515class Baz a where
1616 -- | Baz bar
17− data BazBar a b c
17+ data
18+ BazBar
19+ a
20+ b
21+ c
1822 -- | Baz baz
1923 data
2024 BazBaz
data/examples/declaration/class/associated-data.hs+4−1
@@ -14,7 +14,10 @@ class Bar a
1414-- | Something more.
1515class Baz a where
1616 -- | Baz bar
17− data BazBar a b c
17+ data BazBar
18+ a
19+ b
20+ c
1821
1922 -- | Baz baz
2023 data family BazBaz
data/examples/declaration/class/associated-types-out.hs+6−1
@@ -14,9 +14,14 @@ class Bar a where
1414-- | Something more.
1515class Baz a where
1616 -- | Baz bar
17− type BazBar a b c
17+ type
18+ BazBar
19+ a -- Foo
20+ b -- Bar
21+ c
1822 -- | Baz baz
1923 type
24+ -- After type
2025 BazBaz
2126 b
2227 a
data/examples/declaration/class/associated-types.hs+6−2
@@ -14,10 +14,14 @@ class Bar a
1414-- | Something more.
1515class Baz a where
1616 -- | Baz bar
17− type BazBar a b c
17+ type BazBar
18+ a -- Foo
19+ b -- Bar
20+ c
1821
1922 -- | Baz baz
20− type BazBaz
23+ type -- After type
24+ BazBaz
2125 b
2226 a
2327 c
data/examples/declaration/class/multi-parameters-out.hs+9−2
@@ -10,11 +10,18 @@ class Bar a b c d where
1010 -> c
1111 -> d
1212
13−class Baz where
13+class -- Before name
14+ Baz where
1415 baz :: Int
1516
1617-- | Something else.
17−class BarBaz a b c d e f where
18+class BarBaz
19+ a -- Foo
20+ b -- Bar
21+ c -- Baz bar
22+ d -- Baz baz
23+ e -- Rest
24+ f where
1825 barbaz
1926 :: a -> f
2027 bazbar
data/examples/declaration/class/multi-parameters.hs+8−8
@@ -3,8 +3,7 @@
33class Foo a b where foo :: a -> b
44
55-- | Something.
6−class Bar
7− a b c d
6+class Bar a b c d
87 where
98 bar ::
109 a
@@ -12,18 +11,19 @@ class Bar
1211 -> c
1312 -> d
1413
15−class Baz
14+class -- Before name
15+ Baz
1616 where
1717 baz :: Int
1818
1919-- | Something else.
2020class
2121 BarBaz
22− a
23− b
24− c
25− d
26− e
22+ a -- Foo
23+ b -- Bar
24+ c -- Baz bar
25+ d -- Baz baz
26+ e -- Rest
2727 f where
2828 barbaz ::
2929 a -> f
data/examples/declaration/class/poly-kinded-classes-out.hs+4−3
@@ -1,6 +1,7 @@
11{-# LANGUAGE PolyKinds #-}
22class Foo (a :: k)
33
4−class Bar ( a
5− :: *
6− )
4+class Bar
5+ ( a -- Variable
6+ :: * -- Star
7+ )
data/examples/declaration/class/poly-kinded-classes.hs+3−2
@@ -3,5 +3,6 @@
33class Foo (a::k)
44
55class Bar
6− (a
7− :: *)
6+ (a -- Variable
7+ :: * -- Star
8+ )
data/examples/declaration/class/type-operators-out.hsnew file+29
@@ -0,0 +1,29 @@
1+{-# LANGUAGE MultiParamTypeClasses #-}
2+{-# LANGUAGE TypeOperators #-}
3+class (:$) a b
4+
5+class (:&)
6+ a
7+ b
8+
9+class a :* b
10+
11+class a :+ -- Before operator
12+ b -- After operator
13+
14+class ( f :.
15+ g
16+ )
17+ a
18+
19+class a `Pair` b
20+
21+class a `Sum`
22+ b
23+
24+class (f `Product` g) a
25+
26+class ( f `Sum`
27+ g
28+ )
29+ a
data/examples/declaration/class/type-operators.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE MultiParamTypeClasses #-}
2+{-# LANGUAGE TypeOperators #-}
3+
4+class (:$) a b
5+
6+class (:&)
7+ a
8+ b
9+
10+class a:*b
11+
12+class
13+ a -- Before operator
14+ :+
15+ b -- After operator
16+
17+class (
18+ f :. g
19+ ) a
20+
21+class
22+ a`Pair`b
23+
24+class
25+ a
26+ `Sum` b
27+
28+class (f`Product`g)a
29+
30+class (
31+ f `Sum` g
32+ ) a
src/Ormolu/Printer/Meat/Declaration.hs+1−3
@@ -52,6 +52,7 @@ p_tyClDecl = \case
5252 tcdCtxt
5353 tcdLName
5454 tcdTyVars
55+ tcdFixity
5556 tcdFDs
5657 tcdSigs
5758 tcdMeths
@@ -70,6 +71,3 @@ p_derivDecl :: DerivDecl GhcPs -> R ()
7071p_derivDecl = \case
7172 d@DerivDecl {..} -> p_standaloneDerivDecl d
7273 XDerivDecl _ -> notImplemented "XDerivDecl standalone deriving"
73−
74−----------------------------------------------------------------------------
75−-- Helpers
src/Ormolu/Printer/Meat/Declaration/Class.hs+35−16
@@ -30,33 +30,32 @@ p_classDecl
3030 :: LHsContext GhcPs
3131 -> Located RdrName
3232 -> LHsQTyVars GhcPs
33+ -> LexicalFixity
3334 -> [Located (FunDep (Located RdrName))]
3435 -> [LSig GhcPs]
3536 -> LHsBinds GhcPs
3637 -> [LFamilyDecl GhcPs]
3738 -> [LTyFamDefltEqn GhcPs]
3839 -> R ()
39−p_classDecl ctx name tvars fdeps csigs cdefs cats catdefs = do
40+p_classDecl ctx name tvars fixity fdeps csigs cdefs cats catdefs = do
4041 let HsQTvs {..} = tvars
42+ variableSpans = foldr (combineSrcSpans . getLoc) noSrcSpan hsq_explicit
43+ signatureSpans = getLoc name `combineSrcSpans` variableSpans
44+ dependencySpans = foldr (combineSrcSpans . getLoc) noSrcSpan fdeps
4145 combinedSpans =
4246 getLoc ctx `combineSrcSpans`
43− getLoc name `combineSrcSpans`
44− foldr (combineSrcSpans . getLoc) noSrcSpan hsq_explicit `combineSrcSpans`
45− foldr (combineSrcSpans . getLoc) noSrcSpan fdeps
47+ signatureSpans `combineSrcSpans`
48+ dependencySpans
4649 txt "class "
4750 sitcc $ do
48− switchLayout combinedSpans $ do
49− unless (null (unLoc ctx)) $ do
50− located ctx p_hsContext
51− breakpoint
52− txt "=> "
53− p_rdrName name
54− unless (null hsq_explicit) space
55− spaceSep (located' p_hsTyVarBndr) hsq_explicit
56− unless (null fdeps) $ do
57− breakpoint
58− txt "| "
59− velt (withSep comma (located' p_funDep) fdeps)
51+ switchLayout combinedSpans $ p_classContext ctx
52+ switchLayout signatureSpans $ do
53+ p_infixDefHelper
54+ (isInfix fixity)
55+ inci
56+ (p_rdrName name)
57+ (located' p_hsTyVarBndr <$> hsq_explicit)
58+ switchLayout combinedSpans $ p_classFundeps fdeps
6059 -- GHC's AST does not necessarily store each kind of element in source
6160 -- location order. This happens because different declarations are stored in
6261 -- different lists. Consequently, to get all the declarations in proper order,
@@ -77,6 +76,18 @@ p_classDecl ctx name tvars fdeps csigs cdefs cats catdefs = do
7776 inci (sequence_ decls)
7877 else newline
7978
79+p_classContext :: LHsContext GhcPs -> R ()
80+p_classContext ctx = unless (null (unLoc ctx)) $ do
81+ located ctx p_hsContext
82+ breakpoint
83+ txt "=> "
84+
85+p_classFundeps :: [Located (FunDep (Located RdrName))] -> R ()
86+p_classFundeps fdeps = unless (null fdeps) $ do
87+ breakpoint
88+ txt "| "
89+ velt $ withSep comma (located' p_funDep) fdeps
90+
8091p_famDefDecl :: TyFamDefltEqn GhcPs -> R ()
8192p_famDefDecl FamEqn {..} = do
8293 txt "type"
@@ -92,3 +103,11 @@ p_funDep (before, after) = do
92103 spaceSep p_rdrName before
93104 txt " -> "
94105 spaceSep p_rdrName after
106+
107+----------------------------------------------------------------------------
108+-- Helpers
109+
110+isInfix :: LexicalFixity -> Bool
111+isInfix = \case
112+ Infix -> True
113+ Prefix -> False