git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix prettyprinter for types
commit8051524f708302de2034d6f28beba6a237b5cfa0 authorGary Burgess <gary.burgess@gmail.com> dateSun 17 Apr 2016, 02:42 (11 years ago) parent8bab509
9 files changed+44−31
src/Language/PureScript/Docs/AsMarkdown.hs+1−1 src/Language/PureScript/Docs/Convert.hs+2−2 src/Language/PureScript/Docs/Render.hs+2−2 src/Language/PureScript/Docs/RenderedCode/Render.hs+12−1 src/Language/PureScript/Docs/RenderedCode/Types.hs+10−5 src/Language/PureScript/Pretty/Types.hs+10 src/Language/PureScript/Sugar/Operators.hs+1−1 tests/Main.hs−13 tests/TestDocs.hs+6−6
src/Language/PureScript/Docs/AsMarkdown.hs+1−1
@@ -80,7 +80,7 @@ codeToString :: RenderedCode -> String
8080codeToString = outputWith elemAsMarkdown
8181 where
8282 elemAsMarkdown (Syntax x) = x
83− elemAsMarkdown (Ident x) = x
83+ elemAsMarkdown (Ident x _) = x
8484 elemAsMarkdown (Ctor x _) = x
8585 elemAsMarkdown (Kind x) = x
8686 elemAsMarkdown (Keyword x) = x
src/Language/PureScript/Docs/Convert.hs+2−2
@@ -26,9 +26,9 @@ import Text.Parsec (eof)
2626import qualified Language.PureScript as P
2727import qualified Language.PureScript.Constants as C
2828
29−import Language.PureScript.Docs.Types
30−import Language.PureScript.Docs.Convert.Single (convertSingleModule, collectBookmarks)
3129import Language.PureScript.Docs.Convert.ReExports (updateReExports)
30+import Language.PureScript.Docs.Convert.Single (convertSingleModule, collectBookmarks)
31+import Language.PureScript.Docs.Types
3232
3333-- |
3434-- 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{..} =
7070 renderType' = renderTypeWithOptions opts
7171 renderAlias (P.Qualified mn alias)
7272 | mn == currentModule opts =
73− P.foldFixityAlias P.runIdent P.runProperName P.runProperName alias
73+ P.foldFixityAlias P.runIdent P.runProperName (("type " ++) . P.runProperName) alias
7474 | otherwise =
7575 P.foldFixityAlias
7676 (P.showQualified P.runIdent . P.Qualified mn)
7777 (P.showQualified P.runProperName . P.Qualified mn)
78− (P.showQualified P.runProperName . P.Qualified mn)
78+ (("type " ++) . P.showQualified P.runProperName . P.Qualified mn)
7979 alias
8080
8181renderChildDeclaration :: ChildDeclaration -> RenderedCode
src/Language/PureScript/Docs/RenderedCode/Render.hs+12−1
@@ -48,6 +48,10 @@ typeLiterals = mkPattern match
4848 Just (syntax "()")
4949 match row@RCons{} =
5050 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))
5155 match _ =
5256 Nothing
5357
@@ -115,6 +119,12 @@ constrained = mkPattern match
115119 match (ConstrainedType deps ty) = Just (deps, ty)
116120 match _ = Nothing
117121
122+explicitParens :: Pattern () Type ((), Type)
123+explicitParens = mkPattern match
124+ where
125+ match (ParensInType ty) = Just ((), ty)
126+ match _ = Nothing
127+
118128matchTypeAtom :: Pattern () Type RenderedCode
119129matchTypeAtom = typeLiterals <+> fmap parens matchType
120130 where
@@ -130,6 +140,7 @@ matchType = buildPrettyPrinter operators matchTypeAtom
130140 , [ Wrap constrained $ \deps ty -> renderConstraints deps ty ]
131141 , [ Wrap forall_ $ \idents ty -> mconcat [syntax "forall", sp, mintersperse sp (map ident idents), syntax ".", sp, ty] ]
132142 , [ Wrap kinded $ \k ty -> mintersperse sp [ty, syntax "::", renderKind k] ]
143+ , [ Wrap explicitParens $ \_ ty -> ty ]
133144 ]
134145
135146forall_ :: Pattern () Type ([String], Type)
@@ -156,7 +167,7 @@ convert _ other = other
156167convertForAlls :: Type -> Type
157168convertForAlls (ForAll i ty _) = go [i] ty
158169 where
159− go idents (ForAll ident' ty' _) = go (ident' : idents) ty'
170+ go idents (ForAll i' ty' _) = go (i' : idents) ty'
160171 go idents other = PrettyPrintForAll idents other
161172convertForAlls other = other
162173
src/Language/PureScript/Docs/RenderedCode/Types.hs+10−5
@@ -18,6 +18,7 @@ module Language.PureScript.Docs.RenderedCode.Types
1818 , sp
1919 , syntax
2020 , ident
21+ , ident'
2122 , ctor
2223 , kind
2324 , keyword
@@ -46,7 +47,7 @@ import qualified Language.PureScript as P
4647--
4748data RenderedCodeElement
4849 = Syntax String
49− | Ident String
50+ | Ident String ContainingModule
5051 | Ctor String ContainingModule
5152 | Kind String
5253 | Keyword String
@@ -56,8 +57,8 @@ data RenderedCodeElement
5657instance A.ToJSON RenderedCodeElement where
5758 toJSON (Syntax str) =
5859 A.toJSON ["syntax", str]
59− toJSON (Ident str) =
60− A.toJSON ["ident", str]
60+ toJSON (Ident str mn) =
61+ A.toJSON ["ident", A.toJSON str, A.toJSON mn]
6162 toJSON (Ctor str mn) =
6263 A.toJSON ["ctor", A.toJSON str, A.toJSON mn ]
6364 toJSON (Kind str) =
@@ -70,7 +71,7 @@ instance A.ToJSON RenderedCodeElement where
7071asRenderedCodeElement :: Parse String RenderedCodeElement
7172asRenderedCodeElement =
7273 a Syntax "syntax" <|>
73− a Ident "ident" <|>
74+ asIdent <|>
7475 asCtor <|>
7576 a Kind "kind" <|>
7677 a Keyword "keyword" <|>
@@ -80,6 +81,7 @@ asRenderedCodeElement =
8081 p <|> q = catchError p (const q)
8182
8283 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)
8385 asCtor = nth 0 (withString (eq "ctor")) *> (Ctor <$> nth 1 asString <*> nth 2 asContainingModule)
8486 asSpace = nth 0 (withString (eq "space")) *> pure Space
8587
@@ -159,7 +161,10 @@ syntax :: String -> RenderedCode
159161syntax x = RC [Syntax x]
160162
161163ident :: 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]
163168
164169ctor :: String -> ContainingModule -> RenderedCode
165170ctor x m = RC [Ctor x m]
src/Language/PureScript/Pretty/Types.hs+10
@@ -36,6 +36,9 @@ typeLiterals = mkPattern match
3636 match (Skolem name s _ _) = Just $ text $ name ++ show s
3737 match REmpty = Just $ text "()"
3838 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
3942 match _ = Nothing
4043
4144constraintsAsBox :: [Constraint] -> Box -> Box
@@ -108,6 +111,12 @@ constrained = mkPattern match
108111 match (ConstrainedType deps ty) = Just (deps, ty)
109112 match _ = Nothing
110113
114+explicitParens :: Pattern () Type ((), Type)
115+explicitParens = mkPattern match
116+ where
117+ match (ParensInType ty) = Just ((), ty)
118+ match _ = Nothing
119+
111120matchTypeAtom :: Pattern () Type Box
112121matchTypeAtom = typeLiterals <+> fmap ((`before` (text ")")) . (text "(" <>)) matchType
113122
@@ -121,6 +130,7 @@ matchType = buildPrettyPrinter operators matchTypeAtom
121130 , [ Wrap constrained $ \deps ty -> constraintsAsBox deps ty ]
122131 , [ Wrap forall_ $ \idents ty -> keepSingleLinesOr (moveRight 2) (text ("forall " ++ unwords idents ++ ".")) ty ]
123132 , [ Wrap kinded $ \k ty -> keepSingleLinesOr (moveRight 2) ty (text (":: " ++ prettyPrintKind k)) ]
133+ , [ Wrap explicitParens $ \_ ty -> ty ]
124134 ]
125135
126136 -- 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
5555--
5656rebracket
5757 :: forall m
58− . (MonadError MultipleErrors m)
58+ . MonadError MultipleErrors m
5959 => [ExternsFile]
6060 -> [Module]
6161 -> 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−
141{-# LANGUAGE DataKinds #-}
152{-# LANGUAGE DoAndIfThenElse #-}
163{-# LANGUAGE TupleSections #-}
tests/TestDocs.hs+6−6
@@ -285,12 +285,12 @@ testCases =
285285 , ValueShouldHaveTypeSignature (n "ExplicitTypeSignatures") "aNumber" (ShowFn (P.tyNumber ==))
286286 ])
287287
288− --, ("ConstrainedArgument",
289− -- [ TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithoutArgs" "forall a. (Partial => a) -> a"
290− -- , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithArgs" "forall a. (Foo a => 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"
293− -- ])
288+ , ("ConstrainedArgument",
289+ [ TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithoutArgs" "forall a. (Partial => a) -> a"
290+ , TypeSynonymShouldRenderAs (n "ConstrainedArgument") "WithArgs" "forall a. (Foo a => 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"
293+ ])
294294 ]
295295
296296 where