on master help sign in
Improve rendering of "where" in class declarations and such
commitac719f3e99c397862118e3fcf1eca03b6dfc788c authorMark Karpov <markkarpov92@gmail.com> dateMon 10 Feb 2020, 10:50 (7 years ago) parent84c2a53
10 files changed+57−29
CHANGELOG.md+5 data/examples/declaration/class/dependency-super-classes-out.hs+2−1 data/examples/declaration/class/functional-dependencies-out.hs+2−1 data/examples/declaration/class/multi-parameters2-out.hs+2−1 data/examples/declaration/data/gadt/multiline-where-out.hs+7 data/examples/declaration/data/gadt/multiline-where.hs+6 data/examples/declaration/type-families/closed-type-family/multi-line-out.hs+6−3 src/Ormolu/Printer/Meat/Declaration/Class.hs+17−16 src/Ormolu/Printer/Meat/Declaration/Data.hs+7−5 src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+3−2
CHANGELOG.md+5
@@ -11,6 +11,11 @@
1111* More compact rendering of type family injectivity constraints. [Issue
1212 482](https://github.com/tweag/ormolu/issues/482).
1313
14+* Improved rendering of the keyword `where` in class, data, and type family
15+ declarations. When headers of these declarations are multi-line `where` is
16+ now put on its own line. [Issue
17+ 509](https://github.com/tweag/ormolu/issues/509).
18+
1419## Ormolu 0.0.3.1
1520
1621* Fixed rendering of record updates with the record dot preprocessor syntax
data/examples/declaration/class/dependency-super-classes-out.hs+2−1
@@ -13,7 +13,8 @@ class
1313 MonadPlus m -- Potential for failure
1414 ) =>
1515 MonadParsec e s m
16− | m -> e s where
16+ | m -> e s
17+ where
1718 -- | 'getState' returns state
1819 getState ::
1920 m s
data/examples/declaration/class/functional-dependencies-out.hs+2−1
@@ -14,5 +14,6 @@ class
1414 b c -> a d, -- Bar
1515 a c -> b d, -- Baz
1616 a c d -> b,
17− a b d -> a b c d where
17+ a b d -> a b c d
18+ where
1819 baz :: a -> b
data/examples/declaration/class/multi-parameters2-out.hs+2−1
@@ -10,7 +10,8 @@ class
1010 c -- Baz bar
1111 d -- Baz baz
1212 e -- Rest
13− f where
13+ f
14+ where
1415 barbaz ::
1516 a -> f
1617 bazbar ::
data/examples/declaration/data/gadt/multiline-where-out.hsnew file+7
@@ -0,0 +1,7 @@
1+data
2+ Foo
3+ a
4+ b
5+ c
6+ where
7+ Foo :: a -> b -> c -> Foo a b c
data/examples/declaration/data/gadt/multiline-where.hsnew file+6
@@ -0,0 +1,6 @@
1+data
2+ Foo
3+ a
4+ b
5+ c where
6+ Foo :: a -> b -> c -> Foo a b c
data/examples/declaration/type-families/closed-type-family/multi-line-out.hs+6−3
@@ -1,6 +1,7 @@
11type family
22 Id a =
3− result | result -> a where
3+ result | result -> a
4+ where
45 Id a =
56 a
67
@@ -9,13 +10,15 @@ type family
910 (a :: k)
1011 b
1112 c =
12− foo | foo -> k b where
13+ foo | foo -> k b
14+ where
1315 G a b c =
1416 (a, b)
1517
1618type family
1719 F a ::
18− * -> * where
20+ * -> *
21+ where
1922 F Int = Double
2023 F Bool =
2124 Char
src/Ormolu/Printer/Meat/Declaration/Class.hs+17−16
@@ -38,6 +38,20 @@ p_classDecl ctx name HsQTvs {..} fixity fdeps csigs cdefs cats catdefs cdocs = d
3838 signatureSpans = getLoc name : variableSpans
3939 dependencySpans = getLoc <$> fdeps
4040 combinedSpans = getLoc ctx : (signatureSpans ++ dependencySpans)
41+ -- GHC's AST does not necessarily store each kind of element in source
42+ -- location order. This happens because different declarations are stored
43+ -- in different lists. Consequently, to get all the declarations in proper
44+ -- order, they need to be manually sorted.
45+ sigs = (getLoc &&& fmap (SigD NoExt)) <$> csigs
46+ vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cdefs
47+ tyFams = (getLoc &&& fmap (TyClD NoExt . FamDecl NoExt)) <$> cats
48+ docs = (getLoc &&& fmap (DocD NoExt)) <$> cdocs
49+ tyFamDefs =
50+ ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt . defltEqnToInstDecl)
51+ )
52+ <$> catdefs
53+ allDecls =
54+ snd <$> sortOn fst (sigs <> vals <> tyFams <> tyFamDefs <> docs)
4155 txt "class"
4256 switchLayout combinedSpans $ do
4357 breakpoint
@@ -50,23 +64,10 @@ p_classDecl ctx name HsQTvs {..} fixity fdeps csigs cdefs cats catdefs cdocs = d
5064 (p_rdrName name)
5165 (located' p_hsTyVarBndr <$> hsq_explicit)
5266 inci (p_classFundeps fdeps)
53− -- GHC's AST does not necessarily store each kind of element in source
54− -- location order. This happens because different declarations are stored
55− -- in different lists. Consequently, to get all the declarations in proper
56− -- order, they need to be manually sorted.
57− let sigs = (getLoc &&& fmap (SigD NoExt)) <$> csigs
58− vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cdefs
59− tyFams = (getLoc &&& fmap (TyClD NoExt . FamDecl NoExt)) <$> cats
60− docs = (getLoc &&& fmap (DocD NoExt)) <$> cdocs
61− tyFamDefs =
62− ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt . defltEqnToInstDecl)
63− )
64− <$> catdefs
65− allDecls =
66− snd <$> sortOn fst (sigs <> vals <> tyFams <> tyFamDefs <> docs)
67+ unless (null allDecls) $ do
68+ breakpoint
69+ txt "where"
6770 unless (null allDecls) $ do
68− space
69− txt "where"
7071 breakpoint -- Ensure whitespace is added after where clause.
7172 inci (p_hsDeclsRespectGrouping Associated allDecls)
7273p_classDecl _ _ XLHsQTyVars {} _ _ _ _ _ _ _ = notImplemented "XLHsQTyVars"
src/Ormolu/Printer/Meat/Declaration/Data.hs+7−5
@@ -37,7 +37,8 @@ p_dataDecl style name tpats fixity HsDataDefn {..} = do
3737 txt $ case style of
3838 Associated -> mempty
3939 Free -> " instance"
40− switchLayout (getLoc name : fmap getLoc tpats) $ do
40+ let constructorSpans = getLoc name : fmap getLoc tpats
41+ switchLayout constructorSpans $ do
4142 breakpoint
4243 inci $
4344 p_infixDefHelper
@@ -55,11 +56,12 @@ p_dataDecl style name tpats fixity HsDataDefn {..} = do
5556 let gadt = isJust dd_kindSig || any (isGadt . unLoc) dd_cons
5657 unless (null dd_cons) $
5758 if gadt
58− then do
59− space
60− txt "where"
59+ then inci $ do
60+ switchLayout constructorSpans $ do
61+ breakpoint
62+ txt "where"
6163 breakpoint
62− inci $ sepSemi (located' p_conDecl) dd_cons
64+ sepSemi (located' p_conDecl) dd_cons
6365 else switchLayout (getLoc name : (getLoc <$> dd_cons))
6466 $ inci
6567 $ do
src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+3−2
@@ -45,8 +45,9 @@ p_famDecl style FamilyDecl {fdTyVars = HsQTvs {..}, ..} = do
4545 case mmeqs of
4646 Nothing -> return ()
4747 Just meqs -> do
48− space
49− txt "where"
48+ inci $ do
49+ breakpoint
50+ txt "where"
5051 case meqs of
5152 Nothing -> do
5253 space