-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTypePrint.jl
185 lines (166 loc) · 5.47 KB
/
SimpleTypePrint.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module SimpleTypePrint
export show_type, repr_type, config_type_display
"""
# Examples
```jldoctest
julia> using SimpleTypePrint: print_multiple
print_multiple(show, stdout, 1:10, "{", ",", "}")
{1,2,3,4,5,6,7,8,9,10}
```
"""
function print_multiple(f, io::IO, xs, left="{", sep=",", right="}")
n = length(xs)::Int
print(io, left)
for (i, x) in enumerate(xs)
f(io,x)
i < n && print(io, sep)
end
print(io, right)
end
"""
show_type(io, type; kwargs...)
"""
function show_type(io::IO, @nospecialize(ty::Type); max_depth::Int = 3, short_type_name::Bool = true)
t_var_scope::Set{Symbol} = Set{Symbol}()
function rec(x::DataType, d)
# to be consistent with the Julia Compiler.
(x === Tuple) && return Base.show_type_name(io, x.name)
if x.name === Tuple.name
n = length(x.parameters)::Int
if n > 3 && all(i -> (x.parameters[1] === i), x.parameters)
# Print homogeneous tuples with more than 3 elements compactly as NTuple
print(io, "NTuple{", n, ',')
rec(x.parameters[1], d-1)
print(io, "}")
return
end
end
# hanlde function type specially
is_func = startswith(string(nameof(x)), "#")
if short_type_name && !is_func
print(io, nameof(x))
else
Base.show_type_name(io, x.name)
end
if !isempty(x.parameters)
if d ≤ 1
print(io, "{...}")
else
print_multiple((_,p) -> rec(p, d-1), io, x.parameters)
end
end
end
function rec(x::Union, d)
if x.a isa DataType && Core.Compiler.typename(x.a) === Core.Compiler.typename(DenseArray)
T, N = x.a.parameters
if x == StridedArray{T,N}
print(io, "StridedArray")
print_multiple(show, io, (T,N))
return
elseif x == StridedVecOrMat{T}
print(io, "StridedVecOrMat")
print_multiple(show, io, (T,))
return
elseif StridedArray{T,N} <: x
print(io, "Union")
elems = vcat(StridedArray{T,N},
Base.uniontypes(Core.Compiler.typesubtract(x, StridedArray{T,N})))
print_multiple((_, p) -> rec(p, d-1), io, elems)
return
end
end
print(io, "Union")
print_multiple((_, p) -> rec(p, d-1), io, Base.uniontypes(x))
end
function show_tv_def(tv::TypeVar, d)
function show_bound(io::IO, @nospecialize(b))
parens = isa(b,UnionAll) && !Base.print_without_params(b)
parens && print(io, "(")
rec(b, d-1)
parens && print(io, ")")
end
lb, ub = tv.lb, tv.ub
if lb !== Base.Bottom
if ub === Any
Base.show_unquoted(io, tv.name)
print(io, ">:")
show_bound(io, lb)
else
show_bound(io, lb)
print(io, "<:")
Base.show_unquoted(io, tv.name)
end
else
Base.show_unquoted(io, tv.name)
end
if ub !== Any
print(io, "<:")
show_bound(io, ub)
end
nothing
end
function rec(x::UnionAll, d, var_group::Vector{TypeVar}=TypeVar[])
# rename tvar as needed
var_symb = x.var.name
if var_symb === :_ || var_symb ∈ t_var_scope
counter = 1
while true
newname = Symbol(var_symb, counter)
if newname ∉ t_var_scope
newtv = TypeVar(newname, x.var.lb, x.var.ub)
x = UnionAll(newtv, x{newtv})
break
end
counter += 1
end
end
var_symb = x.var.name
push!(var_group, x.var)
push!(t_var_scope, var_symb)
if x.body isa UnionAll
# current var group continues
rec(x.body, d, var_group)
else
# current var group ends
rec(x.body, d)
print(io, " where ")
if length(var_group) == 1
show_tv_def(var_group[1], d)
else
print_multiple((_, v) -> show_tv_def(v, d), io, var_group)
end
end
delete!(t_var_scope, var_symb)
end
function rec(tv::TypeVar, d)
Base.show_unquoted(io, tv.name)
end
function rec(ohter, d)
@nospecialize(other)
show(io, ohter)
end
rec(ty, max_depth)
end
"""
repr_type(ty::Type; kwargs...)::String
"""
repr_type(ty::Type; kwargs...) = sprint((io,t) -> show_type(io,t; kwargs...), ty)
"""
config_type_display(;kwargs...)
Replace `Base.show(io::IO, x::Type)` with the simpler type printing funciton
`show_type`.
See also: `show_type`, `repr_type`.
"""
function config_type_display(;kwargs...)
@eval Base.show(io::IO, x::Type) = show_type(io, x; $(kwargs)...)
end
"""
# Keyword args
- `max_depth=3`: the maximal type AST depth to show. Type arguments deeper than this value
will be printed as `...`.
- `short_type_name=true`: when set to `true`, will print simple type names without their
corresponding module path. e.g. "Name" instead of "ModuleA.ModuleB.Name". Note that the
shorter name will always be used if the type is visible from the current scope.
"""
show_type, repr_type, config_type_display
end # module SimpleTypePrint