on master help sign in
Elm.Compiler.Type.toString now takes a format argument
56f76acEvan Czaplicki
src/Elm/Compiler/Type.hs+31−15
@@ -1,7 +1,9 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
3module Elm.Compiler.Type
4+ ( Type(..)
5+ , Format(..)
4− ( Type(..), toString
6+ , toString
5 , Program(..)
7 , Program(..)
6 , encode
8 , encode
7 , decoder
9 , decoder
@@ -49,12 +51,23 @@ data Program =
49-- TO STRING
51-- TO STRING
50
52
51
53
52−data Context = None | ADT | Function
54+data Format = OneLine | MultiLine
53
55
54
56
55−toString :: Type -> String
57+toString :: Format -> Type -> String
56−toString tipe =
58+toString format tipe =
59+ let
60+ mode =
61+ case format of
62+ OneLine ->
63+ P.OneLineMode
64+ MultiLine ->
65+ P.PageMode
66+ in
57− P.render (toDoc None tipe)
67+ P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
68+
69+
70+data Context = None | InType | InFunction
58
71
59
72
60toDoc :: Context -> Type -> P.Doc
73toDoc :: Context -> Type -> P.Doc
@@ -62,7 +75,7 @@ toDoc context tipe =
62 case tipe of
75 case tipe of
63 Lambda _ _ ->
76 Lambda _ _ ->
64 let t:ts =
77 let t:ts =
65− map (toDoc Function) (collectLambdas tipe)
78+ map (toDoc InFunction) (collectLambdas tipe)
66
79
67 lambda =
80 lambda =
68 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
81 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
@@ -89,15 +102,18 @@ toDoc context tipe =
89
102
90 else
103 else
91 let
104 let
92− application =
105+ application =
93− P.hang
106+ P.hang
94− (P.text (Text.unpack name))
107+ (P.text (Text.unpack name))
95− 2
108+ 2
96− (P.sep $ map (toDoc ADT) args)
109+ (P.sep $ map (toDoc InType) args)
97 in
110 in
98− case (context, args) of
111+ case (context, args) of
112+ (InType, _ : _) ->
99− (ADT, _ : _) -> P.parens application
113+ P.parens application
114+
115+ _ ->
100− _ -> application
116+ application
101
117
102 App _ _ ->
118 App _ _ ->
103 error $
119 error $
@@ -160,7 +176,7 @@ flattenRecord tipe =
160
176
161encode :: Type -> Encode.Value
177encode :: Type -> Encode.Value
162encode tipe =
178encode tipe =
163− Encode.text (Text.unwords (Text.words (Text.pack (toString tipe))))
179+ Encode.text (Text.pack (toString OneLine tipe))
164
180
165
181
166decoder :: Decode.Decoder Type
182decoder :: Decode.Decoder Type