@@ -2,19 +2,23 @@ defmodule Gradient.ElixirType do
22 @ moduledoc """
33 Module to format types.
44
5- TODO records
6- FIXME add tests
5+ Seems that:
6+ - record type
7+ - constrained function type
8+ are not used by Elixir so the pp support has not been added.
9+
10+ TODO add tests simply basing simply on passed type terms
711 """
812
913 @ doc """
1014 Take type and prepare a pretty string representation.
1115 """
1216 @ spec pretty_print ( tuple ( ) ) :: String . t ( )
13- def pretty_print ( { :remote_type , _ , [ { :atom , _ , mod } , { :atom , _ , type } , args ] } ) do
17+ def pretty_print ( { :remote_type , _ , [ { :atom , _ , mod } , { :atom , _ , name } , args ] } ) do
1418 args_str = Enum . map ( args , & pretty_print ( & 1 ) ) |> Enum . join ( ", " )
15- type_str = Atom . to_string ( type )
19+ name_str = Atom . to_string ( name )
1620 mod_str = parse_module ( mod )
17- mod_str <> type_str <> "(#{ args_str } )"
21+ mod_str <> name_str <> "(#{ args_str } )"
1822 end
1923
2024 def pretty_print ( { :user_type , _ , type , args } ) do
@@ -37,14 +41,18 @@ defmodule Gradient.ElixirType do
3741 end
3842
3943 def pretty_print ( { :op , _ , op , type } ) do
40- Atom . to_string ( op ) <> pretty_print ( type )
44+ Atom . to_string ( op ) <> " " <> pretty_print ( type )
4145 end
4246
4347 def pretty_print ( { :op , _ , op , left_type , right_type } ) do
4448 operator = " " <> Atom . to_string ( op ) <> " "
4549 pretty_print ( left_type ) <> operator <> pretty_print ( right_type )
4650 end
4751
52+ def pretty_print ( { :type , _ , :fun , [ ] } ) do
53+ "fun()"
54+ end
55+
4856 def pretty_print ( { :type , _ , :fun , [ { :type , _ , :product , arg_types } , res_type ] } ) do
4957 args = Enum . map ( arg_types , & pretty_print ( & 1 ) ) |> Enum . join ( ", " )
5058 res = pretty_print ( res_type )
@@ -56,6 +64,10 @@ defmodule Gradient.ElixirType do
5664 "(... -> " <> res <> ")"
5765 end
5866
67+ def pretty_print ( { :type , _ , :range , [ low , high ] } ) do
68+ pretty_print ( low ) <> ".." <> pretty_print ( high )
69+ end
70+
5971 def pretty_print ( { :type , _ , :tuple , :any } ) do
6072 "tuple()"
6173 end
@@ -65,11 +77,7 @@ defmodule Gradient.ElixirType do
6577 "{" <> elements_str <> "}"
6678 end
6779
68- def pretty_print ( { :atom , _ , nil } ) do
69- "nil"
70- end
71-
72- def pretty_print ( { :atom , _ , val } ) when val in [ true , false ] do
80+ def pretty_print ( { :atom , _ , val } ) when val in [ nil , true , false ] do
7381 Atom . to_string ( val )
7482 end
7583
@@ -86,6 +94,7 @@ defmodule Gradient.ElixirType do
8694 end
8795
8896 def pretty_print ( { :type , _ , nil , [ ] } ) do
97+ # The empty list type [] cannot be distinguished from the predefined type nil()
8998 "[]"
9099 end
91100
@@ -102,6 +111,10 @@ defmodule Gradient.ElixirType do
102111 Atom . to_string ( type ) <> "(#{ args_str } )"
103112 end
104113
114+ def pretty_print ( { :var , _ , t } ) do
115+ Atom . to_string ( t )
116+ end
117+
105118 def pretty_print ( type ) do
106119 "#{ inspect ( type ) } "
107120 end
0 commit comments