on master help sign in
Make sure types print out prettier

When trying to max out renderPretty, you cannot just give maxBound because the internal math can cause overflows. I give (maxBound / 2) so that you would need a really long subpart to trigger a problem.

commit69b783ff95a424398c7e24a3d32d72de58d96017 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 2 Mar 2018, 15:23 (9 years ago) parent4a6dd82
3 files changed+9−20
builder/src/Elm/Diff.hs+2−2 compiler/src/Elm/Compiler/Type.hs+5−17 compiler/src/Generate/JavaScript.hs+2−1
builder/src/Elm/Diff.hs+2−2
@@ -252,7 +252,7 @@ aliasToDoc name (Docs.Alias _ tvars tipe) =
252252
253253valueToDoc :: N.Name -> Docs.Value -> P.Doc
254254valueToDoc name (Docs.Value _ tipe) =
255− nameToDoc name <+> P.colon <+> typeDoc tipe
255+ P.hang 4 $ P.sep [ nameToDoc name <+> P.colon, typeDoc tipe ]
256256
257257
258258binopToDoc :: N.Name -> Docs.Binop -> P.Doc
@@ -271,7 +271,7 @@ binopToDoc name (Docs.Binop _ tipe associativity (Docs.Precedence n)) =
271271
272272typeDoc :: Type.Type -> P.Doc
273273typeDoc tipe =
274− P.text (Type.toString Type.OneLine tipe)
274+ Type.toDoc Type.None tipe
275275
276276
277277nameToDoc :: N.Name -> P.Doc
compiler/src/Elm/Compiler/Type.hs+5−17
@@ -2,8 +2,8 @@
22{-# LANGUAGE OverloadedStrings #-}
33module Elm.Compiler.Type
44 ( Type(..)
5− , Format(..)
6− , toString
5+ , Context(..)
6+ , toDoc
77 , DebugMetadata(..)
88 , Alias(..)
99 , Union(..)
@@ -55,20 +55,7 @@ data Union = Union N.Name [N.Name] [(N.Name, [Type])]
5555
5656
5757
58−-- TO STRING
59−
60−
61−data Format = OneLine | MultiLine
62−
63−
64−toString :: Format -> Type -> String
65−toString format tipe =
66− case format of
67− OneLine ->
68− P.displayS (P.renderPretty 1.0 maxBound (toDoc None tipe)) ""
69−
70− MultiLine ->
71− P.displayS (P.renderPretty 1.0 80 (toDoc None tipe)) ""
58+-- TO DOC
7259
7360
7461data Context = None | InType | InFunction
@@ -162,7 +149,8 @@ collectLambdas tipe =
162149
163150encode :: Type -> Encode.Value
164151encode tipe =
165− Encode.text (Text.pack (toString OneLine tipe))
152+ Encode.text $ Text.pack $
153+ P.displayS (P.renderPretty 1.0 (maxBound `div` 2) (toDoc None tipe)) ""
166154
167155
168156decoder :: Decode.Decoder () Type
compiler/src/Generate/JavaScript.hs+2−1
@@ -17,6 +17,7 @@ import qualified Data.Map as Map
1717import qualified Data.Set as Set
1818import qualified Data.Text as Text
1919import qualified Data.Text.Encoding as Text
20+import qualified Text.PrettyPrint.ANSI.Leijen as P
2021
2122import qualified AST.Optimized as Opt
2223import qualified AST.Module.Name as ModuleName
@@ -90,7 +91,7 @@ print interfaces home name =
9091 value = Name.toBuilder (Name.fromGlobal home name)
9192 toString = Name.toBuilder (Name.fromKernel N.debug "toString")
9293 annotation = I._types (interfaces ! home) ! name
93− tipe = Type.toString Type.MultiLine $ Extract.fromAnnotation annotation
94+ tipe = P.displayS (P.renderPretty 1.0 80 (Type.toDoc Type.None (Extract.fromAnnotation annotation))) ""
9495 in
9596 "var _value = " <> toString <> "(" <> value <> ");\n" <>
9697 "var _type = '" <> B.stringUtf8 (show tipe) <> "';\n\