git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix prettyprinter for types
8051524Gary Burgess
src/Language/PureScript/Docs/AsMarkdown.hs+1−1
@@ -80,7 +80,7 @@ codeToString :: RenderedCode -> String
80codeToString = outputWith elemAsMarkdown
80codeToString = outputWith elemAsMarkdown
81 where
81 where
82 elemAsMarkdown (Syntax x) = x
82 elemAsMarkdown (Syntax x) = x
83− elemAsMarkdown (Ident x) = x
83+ elemAsMarkdown (Ident x _) = x
84 elemAsMarkdown (Ctor x _) = x
84 elemAsMarkdown (Ctor x _) = x
85 elemAsMarkdown (Kind x) = x
85 elemAsMarkdown (Kind x) = x
86 elemAsMarkdown (Keyword x) = x
86 elemAsMarkdown (Keyword x) = x
src/Language/PureScript/Docs/Convert.hs+2−2
@@ -26,9 +26,9 @@ import Text.Parsec (eof)
26import qualified Language.PureScript as P
26import qualified Language.PureScript as P
27import qualified Language.PureScript.Constants as C
27import qualified Language.PureScript.Constants as C
28
28
29−import Language.PureScript.Docs.Types
30−import Language.PureScript.Docs.Convert.Single (convertSingleModule, collectBookmarks)
31import Language.PureScript.Docs.Convert.ReExports (updateReExports)
29import Language.PureScript.Docs.Convert.ReExports (updateReExports)
30+import Language.PureScript.Docs.Convert.Single (convertSingleModule, collectBookmarks)
31+import Language.PureScript.Docs.Types
32
32
33-- |
33-- |
34-- Like convertModules, except that it takes a list of modules, together with
34-- Like convertModules, except that it takes a list of modules, together with
src/Language/PureScript/Docs/Render.hs+2−2
@@ -70,12 +70,12 @@ renderDeclarationWithOptions opts Declaration{..} =
70 renderType' = renderTypeWithOptions opts
70 renderType' = renderTypeWithOptions opts
71 renderAlias (P.Qualified mn alias)
71 renderAlias (P.Qualified mn alias)
72 | mn == currentModule opts =
72 | mn == currentModule opts =
73− P.foldFixityAlias P.runIdent P.runProperName P.runProperName alias
73+ P.foldFixityAlias P.runIdent P.runProperName (("type " ++) . P.runProperName) alias
74 | otherwise =
74 | otherwise =
75 P.foldFixityAlias
75 P.foldFixityAlias
76 (P.showQualified P.runIdent . P.Qualified mn)
76 (P.showQualified P.runIdent . P.Qualified mn)
77 (P.showQualified P.runProperName . P.Qualified mn)
77 (P.showQualified P.runProperName . P.Qualified mn)
78− (P.showQualified P.runProperName . P.Qualified mn)
78+ (("type " ++) . P.showQualified P.runProperName . P.Qualified mn)
79 alias
79 alias
80
80
81renderChildDeclaration :: ChildDeclaration -> RenderedCode
81renderChildDeclaration :: ChildDeclaration -> RenderedCode
src/Language/PureScript/Docs/RenderedCode/Render.hs+12−1
@@ -48,6 +48,10 @@ typeLiterals = mkPattern match
48 Just (syntax "()")
48 Just (syntax "()")
49 match row@RCons{} =
49 match row@RCons{} =
50 Just (syntax "(" <> renderRow row <> syntax ")")
50 Just (syntax "(" <> renderRow row <> syntax ")")
51+ match (BinaryNoParensType op l r) =
52+ Just $ renderTypeAtom l <> sp <> renderTypeAtom op <> sp <> renderTypeAtom r
53+ match (TypeOp (Qualified mn op)) =
54+ Just (ident' (runIdent op) (maybeToContainingModule mn))
51 match _ =
55 match _ =
52 Nothing
56 Nothing
53
57
@@ -115,6 +119,12 @@ constrained = mkPattern match
115 match (ConstrainedType deps ty) = Just (deps, ty)
119 match (ConstrainedType deps ty) = Just (deps, ty)
116 match _ = Nothing
120 match _ = Nothing
117
121
122+explicitParens :: Pattern () Type ((), Type)
123+explicitParens = mkPattern match
124+ where
125+ match (ParensInType ty) = Just ((), ty)
126+ match _ = Nothing
127+
118matchTypeAtom :: Pattern () Type RenderedCode
128matchTypeAtom :: Pattern () Type RenderedCode
119matchTypeAtom = typeLiterals <+> fmap parens matchType
129matchTypeAtom = typeLiterals <+> fmap parens matchType
120 where
130 where
@@ -130,6 +140,7 @@ matchType = buildPrettyPrinter operators matchTypeAtom
130 , [ Wrap constrained $ \deps ty -> renderConstraints deps ty ]
140 , [ Wrap constrained $ \deps ty -> renderConstraints deps ty ]
131 , [ Wrap forall_ $ \idents ty -> mconcat [syntax "forall", sp, mintersperse sp (map ident idents), syntax ".", sp, ty] ]
141 , [ Wrap forall_ $ \idents ty -> mconcat [syntax "forall", sp, mintersperse sp (map ident idents), syntax ".", sp, ty] ]
132 , [ Wrap kinded $ \k ty -> mintersperse sp [ty, syntax "::", renderKind k] ]
142 , [ Wrap kinded $ \k ty -> mintersperse sp [ty, syntax "::", renderKind k] ]
143+ , [ Wrap explicitParens $ \_ ty -> ty ]
133 ]
144 ]
134
145
135forall_ :: Pattern () Type ([String], Type)
146forall_ :: Pattern () Type ([String], Type)
@@ -156,7 +167,7 @@ convert _ other = other
156convertForAlls :: Type -> Type
167convertForAlls :: Type -> Type
157convertForAlls (ForAll i ty _) = go [i] ty
168convertForAlls (ForAll i ty _) = go [i] ty
158 where
169 where
159− go idents (ForAll ident' ty' _) = go (ident' : idents) ty'
170+ go idents (ForAll i' ty' _) = go (i' : idents) ty'
160 go idents other = PrettyPrintForAll idents other
171 go idents other = PrettyPrintForAll idents other
161convertForAlls other = other
172convertForAlls other = other
162
173
src/Language/PureScript/Docs/RenderedCode/Types.hs+10−5
@@ -18,6 +18,7 @@ module Language.PureScript.Docs.RenderedCode.Types
18 , sp
18 , sp
19 , syntax
19 , syntax
20 , ident
20 , ident
21+ , ident'
21 , ctor
22 , ctor
22 , kind
23 , kind
23 , keyword
24 , keyword
@@ -46,7 +47,7 @@ import qualified Language.PureScript as P
46--
47--
47data RenderedCodeElement
48data RenderedCodeElement
48 = Syntax String
49 = Syntax String
49− | Ident String
50+ | Ident String ContainingModule
50 | Ctor String ContainingModule
51 | Ctor String ContainingModule
51 | Kind String
52 | Kind String
52 | Keyword String
53 | Keyword String
@@ -56,8 +57,8 @@ data RenderedCodeElement
56instance A.ToJSON RenderedCodeElement where
57instance A.ToJSON RenderedCodeElement where
57 toJSON (Syntax str) =
58 toJSON (Syntax str) =
58 A.toJSON ["syntax", str]
59 A.toJSON ["syntax", str]
59− toJSON (Ident str) =
60+ toJSON (Ident str mn) =
60− A.toJSON ["ident", str]
61+ A.toJSON ["ident", A.toJSON str, A.toJSON mn]
61 toJSON (Ctor str mn) =
62 toJSON (Ctor str mn) =
62 A.toJSON ["ctor", A.toJSON str, A.toJSON mn ]
63 A.toJSON ["ctor", A.toJSON str, A.toJSON mn ]
63 toJSON (Kind str) =
64 toJSON (Kind str) =
@@ -70,7 +71,7 @@ instance A.ToJSON RenderedCodeElement where
70asRenderedCodeElement :: Parse String RenderedCodeElement
71asRenderedCodeElement :: Parse String RenderedCodeElement
71asRenderedCodeElement =
72asRenderedCodeElement =
72 a Syntax "syntax" <|>
73 a Syntax "syntax" <|>
73− a Ident "ident" <|>
74+ asIdent <|>
74 asCtor <|>
75 asCtor <|>
75 a Kind "kind" <|>
76 a Kind "kind" <|>
76 a Keyword "keyword" <|>
77 a Keyword "keyword" <|>
@@ -80,6 +81,7 @@ asRenderedCodeElement =
80 p <|> q = catchError p (const q)
81 p <|> q = catchError p (const q)
81
82
82 a ctor' ctorStr = ctor' <$> (nth 0 (withString (eq ctorStr)) *> nth 1 asString)
83 a ctor' ctorStr = ctor' <$> (nth 0 (withString (eq ctorStr)) *> nth 1 asString)
84+ asIdent = nth 0 (withString (eq "ident")) *> (Ident <$> nth 1 asString <*> nth 2 asContainingModule)
83 asCtor = nth 0 (withString (eq "ctor")) *> (Ctor <$> nth 1 asString <*> nth 2 asContainingModule)
85 asCtor = nth 0 (withString (eq "ctor")) *> (Ctor <$> nth 1 asString <*> nth 2 asContainingModule)
84 asSpace = nth 0 (withString (eq "space")) *> pure Space
86 asSpace = nth 0 (withString (eq "space")) *> pure Space
85
87
@@ -159,7 +161,10 @@ syntax :: String -> RenderedCode
159syntax x = RC [Syntax x]
161syntax x = RC [Syntax x]
160
162
161ident :: String -> RenderedCode
163ident :: String -> RenderedCode
162−ident x = RC [Ident x]
164+ident x = RC [Ident x ThisModule]
165+
166+ident' :: String -> ContainingModule -> RenderedCode
167+ident' x m = RC [Ident x m]
163
168
164ctor :: String -> ContainingModule -> RenderedCode
169ctor :: String -> ContainingModule -> RenderedCode
165ctor x m = RC [Ctor x m]
170ctor x m = RC [Ctor x m]
src/Language/PureScript/Pretty/Types.hs+10
@@ -36,6 +36,9 @@ typeLiterals = mkPattern match
36 match (Skolem name s _ _) = Just $ text $ name ++ show s
36 match (Skolem name s _ _) = Just $ text $ name ++ show s
37 match REmpty = Just $ text "()"
37 match REmpty = Just $ text "()"
38 match row@RCons{} = Just $ prettyPrintRowWith '(' ')' row
38 match row@RCons{} = Just $ prettyPrintRowWith '(' ')' row
39+ match (BinaryNoParensType op l r) =
40+ Just $ typeAsBox l <> text " " <> typeAsBox op <> text " " <> typeAsBox r
41+ match (TypeOp op) = Just $ text $ showQualified runIdent op
39 match _ = Nothing
42 match _ = Nothing
40
43
41constraintsAsBox :: [Constraint] -> Box -> Box
44constraintsAsBox :: [Constraint] -> Box -> Box
@@ -108,6 +111,12 @@ constrained = mkPattern match
108 match (ConstrainedType deps ty) = Just (deps, ty)
111 match (ConstrainedType deps ty) = Just (deps, ty)
109 match _ = Nothing
112 match _ = Nothing
110
113
114+explicitParens :: Pattern () Type ((), Type)
115+explicitParens = mkPattern match
116+ where
117+ match (ParensInType ty) = Just ((), ty)
118+ match _ = Nothing
119+
111matchTypeAtom :: Pattern () Type Box
120matchTypeAtom :: Pattern () Type Box
112matchTypeAtom = typeLiterals <+> fmap ((`before` (text ")")) . (text "(" <>)) matchType
121matchTypeAtom = typeLiterals <+> fmap ((`before` (text ")")) . (text "(" <>)) matchType
113
122
@@ -121,6 +130,7 @@ matchType = buildPrettyPrinter operators matchTypeAtom
121 , [ Wrap constrained $ \deps ty -> constraintsAsBox deps ty ]
130 , [ Wrap constrained $ \deps ty -> constraintsAsBox deps ty ]
122 , [ Wrap forall_ $ \idents ty -> keepSingleLinesOr (moveRight 2) (text ("forall " ++ unwords idents ++ ".")) ty ]
131 , [ Wrap forall_ $ \idents ty -> keepSingleLinesOr (moveRight 2) (text ("forall " ++ unwords idents ++ ".")) ty ]
123 , [ Wrap kinded $ \k ty -> keepSingleLinesOr (moveRight 2) ty (text (":: " ++ prettyPrintKind k)) ]
132 , [ Wrap kinded $ \k ty -> keepSingleLinesOr (moveRight 2) ty (text (":: " ++ prettyPrintKind k)) ]
133+ , [ Wrap explicitParens $ \_ ty -> ty ]
124 ]
134 ]
125
135
126 -- If both boxes span a single line, keep them on the same line, or else
136 -- If both boxes span a single line, keep them on the same line, or else
src/Language/PureScript/Sugar/Operators.hs+1−1
@@ -55,7 +55,7 @@ type FixityRecord = (Qualified Ident, SourceSpan, Fixity, Maybe (Qualified Fixit
55--
55--
56rebracket
56rebracket
57 :: forall m
57 :: forall m
58− . (MonadError MultipleErrors m)
58+ . MonadError MultipleErrors m
59 => [ExternsFile]
59 => [ExternsFile]
60 -> [Module]
60 -> [Module]
61 -> m [Module]
61 -> m [Module]
tests/Main.hs−13
@@ -1,16 +1,3 @@
1−-----------------------------------------------------------------------------
2−--
3−-- Module : Main
4−-- License : MIT (http://opensource.org/licenses/MIT)
5−--
6−-- Maintainer : Phil Freeman <paf31@cantab.net>
7−-- Stability : experimental
8−-- Portability :
9−--
10−-- |
11−--
12−-----------------------------------------------------------------------------
13−
14{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
15{-# LANGUAGE DoAndIfThenElse #-}
2{-# LANGUAGE DoAndIfThenElse #-}
16{-# LANGUAGE TupleSections #-}
3{-# LANGUAGE TupleSections #-}
tests/TestDocs.hs+6−6
@@ -285,12 +285,12 @@ testCases =
285 , ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "aNumber" (ShowFn (P.tyNumber ==))
285 , ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "aNumber" (ShowFn (P.tyNumber ==))
286 ])
286 ])
287
287
288− --, ("ConstrainedArgument",
288+ , ("ConstrainedArgument",
289− -- [ TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithoutArgs" "forall a. (Partial => a) -> a"
289+ [ TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithoutArgs" "forall a. (Partial => a) -> a"
290− -- , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithArgs" "forall a. (Foo a => a) -> a"
290+ , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithArgs" "forall a. (Foo a => a) -> a"
291− -- , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithoutArgs" "forall a. ((Partial, Partial) => a) -> a"
291+ , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithoutArgs" "forall a. ((Partial, Partial) => a) -> a"
292− -- , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithArgs" "forall a b. ((Foo a, Foo b) => a) -> a"
292+ , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "MultiWithArgs" "forall a b. ((Foo a, Foo b) => a) -> a"
293− -- ])
293+ ])
294 ]
294 ]
295
295
296 where
296 where