@@ -1,7 +1,9 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings #-}
33module Elm.Compiler.Type
4− ( Type(..), toString
4+ ( Type(..)
5+ , Format(..)
6+ , toString
57 , Program(..)
68 , encode
79 , decoder
@@ -49,12 +51,23 @@ data Program =
4951-- TO STRING
5052
5153
52−data Context = None | ADT | Function
54+data Format = OneLine | MultiLine
5355
5456
55−toString :: Type -> String
56−toString tipe =
57− P.render (toDoc None tipe)
57+toString :: Format -> Type -> String
58+toString format tipe =
59+ let
60+ mode =
61+ case format of
62+ OneLine ->
63+ P.OneLineMode
64+ MultiLine ->
65+ P.PageMode
66+ in
67+ P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
68+
69+
70+data Context = None | InType | InFunction
5871
5972
6073toDoc :: Context -> Type -> P.Doc
@@ -62,7 +75,7 @@ toDoc context tipe =
6275 case tipe of
6376 Lambda _ _ ->
6477 let t:ts =
65− map (toDoc Function) (collectLambdas tipe)
78+ map (toDoc InFunction) (collectLambdas tipe)
6679
6780 lambda =
6881 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
@@ -89,15 +102,18 @@ toDoc context tipe =
89102
90103 else
91104 let
92− application =
93− P.hang
94− (P.text (Text.unpack name))
95− 2
96− (P.sep $ map (toDoc ADT) args)
105+ application =
106+ P.hang
107+ (P.text (Text.unpack name))
108+ 2
109+ (P.sep $ map (toDoc InType) args)
97110 in
98− case (context, args) of
99− (ADT, _ : _) -> P.parens application
100− _ -> application
111+ case (context, args) of
112+ (InType, _ : _) ->
113+ P.parens application
114+
115+ _ ->
116+ application
101117
102118 App _ _ ->
103119 error $
@@ -160,7 +176,7 @@ flattenRecord tipe =
160176
161177encode :: Type -> Encode.Value
162178encode tipe =
163− Encode.text (Text.unwords (Text.words (Text.pack (toString tipe))))
179+ Encode.text (Text.pack (toString OneLine tipe))
164180
165181
166182decoder :: Decode.Decoder Type