on master help sign in
Implement rendering of type synonyms
e01287amrkkrp
data/examples/declaration/type-families/closed-type-family/multi-line.hs+2−2
@@ -1,7 +1,7 @@
1type family Id a
1type family Id a
2 = result | result -> a where
2 = result | result -> a where
3− Id
3+ Id a
4− a = a
4+ = a
5type family G (a :: k)
5type family G (a :: k)
6 b c = foo | foo -> k b where
6 b c = foo | foo -> k b where
7 G a b c =
7 G a b c =
data/examples/declaration/type-synonyms/multi-line-out.hsnew file+7
@@ -0,0 +1,7 @@
1+type Foo a b c
2+ = Bar c a b
3+
4+type Foo
5+ = Bar
6+ Baz
7+ Quux
data/examples/declaration/type-synonyms/multi-line.hsnew file+7
@@ -0,0 +1,7 @@
1+type Foo a b c
2+ = Bar c a b
3+
4+type Foo =
5+ Bar
6+ Baz
7+ Quux
data/examples/declaration/type-synonyms/simple-out.hsnew file+2
@@ -0,0 +1,2 @@
1+-- | Documentation.
2+type Foo a b c = Bar c a b
data/examples/declaration/type-synonyms/simple.hsnew file+3
@@ -0,0 +1,3 @@
1+-- | Documentation.
2+
3+type Foo a b c = Bar c a b
ormolu.cabal+1
@@ -50,6 +50,7 @@ library
50 , Ormolu.Printer.Internal
50 , Ormolu.Printer.Internal
51 , Ormolu.Printer.Meat.Common
51 , Ormolu.Printer.Meat.Common
52 , Ormolu.Printer.Meat.Declaration
52 , Ormolu.Printer.Meat.Declaration
53+ , Ormolu.Printer.Meat.Declaration.Type
53 , Ormolu.Printer.Meat.Declaration.TypeFamily
54 , Ormolu.Printer.Meat.Declaration.TypeFamily
54 , Ormolu.Printer.Meat.ImportExport
55 , Ormolu.Printer.Meat.ImportExport
55 , Ormolu.Printer.Meat.Module
56 , Ormolu.Printer.Meat.Module
src/Ormolu/Printer/Combinators.hs+20−2
@@ -21,6 +21,8 @@ module Ormolu.Printer.Combinators
21 , velt'
21 , velt'
22 , vlayout
22 , vlayout
23 , withSep
23 , withSep
24+ , spaceSep
25+ , newlineSep
24 -- ** Wrapping
26 -- ** Wrapping
25 , line
27 , line
26 , braces
28 , braces
@@ -80,7 +82,7 @@ locatedVia
80 :: Data a
82 :: Data a
81 => Maybe SrcSpan -- ^ Span that controls layout selection
83 => Maybe SrcSpan -- ^ Span that controls layout selection
82 -> Located a -- ^ Thing to enter
84 -> Located a -- ^ Thing to enter
83− -> (a -> R ()) -- ^ How to renedr inner value
85+ -> (a -> R ()) -- ^ How to render inner value
84 -> R ()
86 -> R ()
85locatedVia ml loc@(L l a) f = do
87locatedVia ml loc@(L l a) f = do
86 mann <- lookupAnn loc
88 mann <- lookupAnn loc
@@ -96,7 +98,7 @@ locatedVia ml loc@(L l a) f = do
96 case mann of
98 case mann of
97 Nothing -> m
99 Nothing -> m
98 Just Ann {..} ->
100 Just Ann {..} ->
99− enterLayout MultiLine . sitcc $ do
101+ sitcc $ do
100 -- There are three things in 'Ann' which contain comments:
102 -- There are three things in 'Ann' which contain comments:
101
103
102 let cmode =
104 let cmode =
@@ -170,6 +172,22 @@ withSep sep f = \case
170 let g a = sep >> f a
172 let g a = sep >> f a
171 in f x : fmap g xs
173 in f x : fmap g xs
172
174
175+-- | Render space-separated elements.
176+
177+spaceSep
178+ :: (a -> R ()) -- ^ How to render list items
179+ -> [a] -- ^ List to render
180+ -> R ()
181+spaceSep f = sequence_ . withSep space f
182+
183+-- | Render newline-separated elements.
184+
185+newlineSep
186+ :: (a -> R ()) -- ^ How to render list items
187+ -> [a] -- ^ List to render
188+ -> R ()
189+newlineSep f = sequence_ . withSep newline f
190+
173----------------------------------------------------------------------------
191----------------------------------------------------------------------------
174-- Wrapping
192-- Wrapping
175
193
src/Ormolu/Printer/Meat/Declaration.hs+4−1
@@ -1,4 +1,5 @@
1−{-# LANGUAGE LambdaCase #-}
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE RecordWildCards #-}
2
3
3-- | Rendering of declarations.
4-- | Rendering of declarations.
4
5
@@ -10,6 +11,7 @@ where
10import HsDecls
11import HsDecls
11import Language.Haskell.GHC.ExactPrint.Types
12import Language.Haskell.GHC.ExactPrint.Types
12import Ormolu.Printer.Combinators
13import Ormolu.Printer.Combinators
14+import Ormolu.Printer.Meat.Declaration.Type
13import Ormolu.Printer.Meat.Declaration.TypeFamily
15import Ormolu.Printer.Meat.Declaration.TypeFamily
14
16
15p_hsDecl :: HsDecl GhcPs -> R ()
17p_hsDecl :: HsDecl GhcPs -> R ()
@@ -20,4 +22,5 @@ p_hsDecl = \case
20p_tyClDecl :: TyClDecl GhcPs -> R ()
22p_tyClDecl :: TyClDecl GhcPs -> R ()
21p_tyClDecl = \case
23p_tyClDecl = \case
22 FamDecl x -> p_famDecl x
24 FamDecl x -> p_famDecl x
25+ SynDecl {..} -> p_synDecl tcdLName tcdTyVars tcdRhs
23 _ -> error "this is not yet supported"
26 _ -> error "this is not yet supported"
src/Ormolu/Printer/Meat/Declaration/Type.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RecordWildCards #-}
3+
4+-- | Rendering of type synonym declarations.
5+
6+module Ormolu.Printer.Meat.Declaration.Type
7+ ( p_synDecl
8+ )
9+where
10+
11+import Control.Monad
12+import HsTypes
13+import Language.Haskell.GHC.ExactPrint.Types
14+import Ormolu.Printer.Combinators
15+import Ormolu.Printer.Meat.Common
16+import Ormolu.Printer.Meat.Type
17+import RdrName (RdrName (..))
18+import SrcLoc (Located)
19+
20+p_synDecl
21+ :: Located RdrName -- ^ Type constructor
22+ -> LHsQTyVars GhcPs -- ^ Type variables
23+ -> LHsType GhcPs -- ^ RHS of type declaration
24+ -> R ()
25+p_synDecl name tvars t = line $ do
26+ txt "type "
27+ located name p_rdrName'
28+ let HsQTvs {..} = tvars
29+ unless (null hsq_explicit) space
30+ spaceSep (located' p_hsTyVarBndr) hsq_explicit
31+ vlayout space newline
32+ inci (txt "= " >> located t p_hsType)
src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+8−11
@@ -10,7 +10,6 @@ module Ormolu.Printer.Meat.Declaration.TypeFamily
10where
10where
11
11
12import Control.Monad
12import Control.Monad
13−import Data.List (intersperse)
14import Data.Maybe (maybeToList, isJust)
13import Data.Maybe (maybeToList, isJust)
15import HsDecls
14import HsDecls
16import HsTypes
15import HsTypes
@@ -23,21 +22,19 @@ import SrcLoc (Located, GenLocated (..))
23p_famDecl :: FamilyDecl GhcPs -> R ()
22p_famDecl :: FamilyDecl GhcPs -> R ()
24p_famDecl FamilyDecl {..} = do
23p_famDecl FamilyDecl {..} = do
25 mmeqs <- case fdInfo of
24 mmeqs <- case fdInfo of
26− DataFamily -> Nothing <$ txt "data family"
25+ DataFamily -> Nothing <$ txt "data family "
27− OpenTypeFamily -> Nothing <$ txt "type family"
26+ OpenTypeFamily -> Nothing <$ txt "type family "
28− ClosedTypeFamily eqs -> Just eqs <$ txt "type family"
27+ ClosedTypeFamily eqs -> Just eqs <$ txt "type family "
29− space
30 located fdLName p_rdrName
28 located fdLName p_rdrName
31 space
29 space
32 let HsQTvs {..} = fdTyVars
30 let HsQTvs {..} = fdTyVars
33 items =
31 items =
34 maybeToList (p_familyResultSigL (isJust fdInjectivityAnn) fdResultSig)
32 maybeToList (p_familyResultSigL (isJust fdInjectivityAnn) fdResultSig)
35 ++ (located' p_injectivityAnn <$> maybeToList fdInjectivityAnn)
33 ++ (located' p_injectivityAnn <$> maybeToList fdInjectivityAnn)
36− unless (null hsq_explicit) $
37− sequence_ $ withSep space (located' p_hsTyVarBndr) hsq_explicit
34+ spaceSep (located' p_hsTyVarBndr) hsq_explicit
38 unless (null items) $
35 unless (null items) $
39 vlayout space newline
36 vlayout space newline
40− inci . inci . sequence_ $ intersperse space items
37+ inci . inci $ spaceSep id items
41 case mmeqs of
38 case mmeqs of
42 Nothing -> newline
39 Nothing -> newline
43 Just meqs -> do
40 Just meqs -> do
@@ -66,14 +63,14 @@ p_injectivityAnn (InjectivityAnn a bs) = do
66 located a p_rdrName
63 located a p_rdrName
67 space
64 space
68 rarrow
65 rarrow
69− sequence_ $ withSep space (located' p_rdrName) bs
66+ spaceSep (located' p_rdrName) bs
70
67
71p_tyFamInstEqn :: TyFamInstEqn GhcPs -> R ()
68p_tyFamInstEqn :: TyFamInstEqn GhcPs -> R ()
72p_tyFamInstEqn HsIB {..} = velt'
69p_tyFamInstEqn HsIB {..} = velt'
73 [ do located feqn_tycon p_rdrName
70 [ do located feqn_tycon p_rdrName
74 space
71 space
75− sequence_ $ withSep space (located' p_hsType) feqn_pats
72+ spaceSep (located' p_hsType) feqn_pats
76− , inci $ txt "= " >> located feqn_rhs p_hsType
73+ , inci $ txt "= " >> inci (located feqn_rhs p_hsType)
77 ]
74 ]
78 where
75 where
79 FamEqn {..} = hsib_body
76 FamEqn {..} = hsib_body
src/Ormolu/Printer/Meat/Module.hs+1−2
@@ -9,7 +9,6 @@ module Ormolu.Printer.Meat.Module
9where
9where
10
10
11import Control.Monad
11import Control.Monad
12−import Data.List (intersperse)
13import GHC hiding (GhcPs, IE)
12import GHC hiding (GhcPs, IE)
14import Ormolu.Imports
13import Ormolu.Imports
15import Ormolu.Printer.Combinators
14import Ormolu.Printer.Combinators
@@ -43,4 +42,4 @@ p_hsModule loc@(L moduleSpan hsModule) = do
43 unless (null hsmodImports) newline
42 unless (null hsmodImports) newline
44 forM_ (sortImports hsmodImports) (located' p_hsmodImport)
43 forM_ (sortImports hsmodImports) (located' p_hsmodImport)
45 when (not (null hsmodImports) && not (null hsmodDecls)) newline
44 when (not (null hsmodImports) && not (null hsmodDecls)) newline
46− sequence_ . intersperse newline $ located' p_hsDecl <$> hsmodDecls
45+ newlineSep (located' p_hsDecl) hsmodDecls
src/Ormolu/Printer/Meat/Type.hs+4−2
@@ -34,8 +34,10 @@ p_hsType = \case
34 Promoted -> txt "'"
34 Promoted -> txt "'"
35 NotPromoted -> return ()
35 NotPromoted -> return ()
36 located n p_rdrName
36 located n p_rdrName
37− HsAppsTy apps ->
37+ HsAppsTy apps -> do
38+ velt' $ case apps of
39+ [] -> []
38− velt' (located' p_hsAppType <$> apps)
40+ (x:xs) -> located' p_hsAppType x : (located' (inci . p_hsAppType) <$> xs)
39 HsAppTy f x -> velt'
41 HsAppTy f x -> velt'
40 [ located f p_hsType
42 [ located f p_hsType
41 , inci $ located x p_hsType
43 , inci $ located x p_hsType