on master help sign in
Make multi-line multi-name patterns prettier
commit592e4695c9061b891b439f0ae4d4dc27e99058d6 authorUtku Demir <me@utdemir.com> dateWed 28 Aug 2019, 19:05 (7 years ago) parent8662581
5 files changed+40−8
data/examples/declaration/signature/pattern/multiline-out.hs+13 data/examples/declaration/signature/pattern/multiline.hs+10 data/examples/declaration/signature/pattern/single-line-out.hs+2 data/examples/declaration/signature/pattern/single-line.hs+2 src/Ormolu/Printer/Meat/Declaration/Signature.hs+13−8
data/examples/declaration/signature/pattern/multiline-out.hs+13
@@ -4,3 +4,16 @@ pattern Arrow
44 :: Type
55 -> Type
66 -> Type
7+
8+pattern
9+ Foo,
10+ Bar
11+ :: Type -> Type -> Type
12+
13+pattern
14+ TypeSignature,
15+ FunctionBody,
16+ PatternSignature,
17+ WarningPragma
18+ :: [RdrName]
19+ -> HsDecl GhcPs
data/examples/declaration/signature/pattern/multiline.hs+10
@@ -1,3 +1,13 @@
11{-# LANGUAGE PatternSynonyms #-}
2+
23pattern Arrow :: Type
34 -> Type -> Type
5+
6+pattern Foo, Bar ::
7+ Type -> Type -> Type
8+
9+pattern TypeSignature
10+ , FunctionBody
11+ , PatternSignature
12+ , WarningPragma :: [RdrName]
13+ -> HsDecl GhcPs
data/examples/declaration/signature/pattern/single-line-out.hs+2
@@ -1,3 +1,5 @@
11{-# LANGUAGE PatternSynonyms #-}
22
33pattern Arrow :: Type -> Type -> Type
4+
5+pattern Foo, Bar :: Type -> Type -> Type
data/examples/declaration/signature/pattern/single-line.hs+2
@@ -1,3 +1,5 @@
11{-# LANGUAGE PatternSynonyms #-}
22
33pattern Arrow :: Type -> Type -> Type
4+
5+pattern Foo, Bar :: Type -> Type -> Type
src/Ormolu/Printer/Meat/Declaration/Signature.hs+13−8
@@ -14,6 +14,7 @@ where
1414import BasicTypes
1515import BooleanFormula
1616import Control.Monad
17+import Data.Bool (bool)
1718import GHC
1819import Ormolu.Printer.Combinators
1920import Ormolu.Printer.Meat.Common
@@ -22,7 +23,7 @@ import Ormolu.Utils
2223
2324p_sigDecl :: Sig GhcPs -> R ()
2425p_sigDecl = \case
25− TypeSig NoExt names hswc -> p_typeSig names hswc
26+ TypeSig NoExt names hswc -> p_typeSig True names hswc
2627 PatSynSig NoExt names hsib -> p_patSynSig names hsib
2728 ClassOpSig NoExt def names hsib -> p_classOpSig def names hsib
2829 FixSig NoExt sig -> p_fixSig sig
@@ -35,15 +36,16 @@ p_sigDecl = \case
3536 _ -> notImplemented "certain types of signature declarations"
3637
3738p_typeSig
38− :: [Located RdrName] -- ^ Names (before @::@)
39+ :: Bool -- ^ Should the tail of the names be indented
40+ -> [Located RdrName] -- ^ Names (before @::@)
3941 -> LHsSigWcType GhcPs -- ^ Type
4042 -> R ()
41−p_typeSig [] _ = return () -- should not happen though
42−p_typeSig (n:ns) hswc = do
43+p_typeSig _ [] _ = return () -- should not happen though
44+p_typeSig indentTail (n:ns) hswc = do
4345 p_rdrName n
4446 if null ns
4547 then p_typeAscription hswc
46− else inci $ do
48+ else bool id inci indentTail $ do
4749 comma
4850 breakpoint
4951 sep (comma >> breakpoint) p_rdrName ns
@@ -65,8 +67,11 @@ p_patSynSig
6567 -> HsImplicitBndrs GhcPs (LHsType GhcPs)
6668 -> R ()
6769p_patSynSig names hsib = do
68− txt "pattern "
69− p_typeSig names HsWC {hswc_ext = NoExt, hswc_body = hsib}
70+ txt "pattern"
71+ let body = p_typeSig False names HsWC {hswc_ext = NoExt, hswc_body = hsib}
72+ if length names > 1
73+ then breakpoint >> inci body
74+ else space >> body
7075
7176p_classOpSig
7277 :: Bool -- ^ Whether this is a \"default\" signature
@@ -75,7 +80,7 @@ p_classOpSig
7580 -> R ()
7681p_classOpSig def names hsib = do
7782 when def (txt "default" >> space)
78− p_typeSig names HsWC {hswc_ext = NoExt, hswc_body = hsib}
83+ p_typeSig True names HsWC {hswc_ext = NoExt, hswc_body = hsib}
7984
8085p_fixSig
8186 :: FixitySig GhcPs