Skip to content

Commit 0aacc2c

Browse files
Use depth-limited type printing
1 parent fccf284 commit 0aacc2c

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

TypedSyntax/src/show.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Boo
118118
inlay_hints = get(io, :inlay_hints, nothing)
119119

120120
print(io, post)
121-
T_str = string(T)
121+
T_str = type_depth_limit(T)
122+
# T_str = string(T)
122123
if iswarn && is_type_unstable(T)
123124
color = is_small_union_or_tunion(T) ? :yellow : :red
124125
printstyled(io, "::", T_str; color)

src/print.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
function type_depth_limit(data; maxtypedepth::Union{Nothing,Int}=2)
2-
buf = IOBuffer()
3-
type_depth_limit(buf, string(data); maxtypedepth)
4-
return String(take!(buf))
5-
end
6-
7-
function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int}=2)
1+
function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int})
82
sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
93
return Base.type_depth_limit(s, max(sz[2], 120); maxdepth=maxtypedepth)
104
end
5+
6+
type_depth_limit(::T; maxtypedepth::Union{Nothing,Int}=2) where {T} = type_depth_limit(T; maxtypedepth)
7+
8+
function type_depth_limit(::Type{T}; maxtypedepth::Union{Nothing,Int}=2) where {T}
9+
buf = IOBuffer()
10+
io = IOContext(buf, :limit => true)
11+
type_depth_limit(io, string(T); maxtypedepth=maxtypedepth)
12+
end

test/test_depth_limited_type_printing.jl

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,57 @@
22
using Revise; include(joinpath("test", "test_depth_limited_type_printing.jl"))
33
=#
44
import Cthulhu
5+
6+
Base.@kwdef struct Nested{A,B}
7+
num::Int = 1
8+
end
59
struct F49231{a,b,c,d,e,f,g}
610
num::g
711
end;
8-
f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);
912
bar(x) = rand() > 0.5 ? x : Any[0][1]
1013
mysum(x) = sum(y-> bar(x.num), 1:5; init=0)
14+
nest_val(na, nb, ::Val{1}) = Nested{na, nb}()
15+
nest_val(na, nb, ::Val{n}) where {n} = nest_val(Nested{na, nb}, Nested{na, nb}, Val(n-1))
16+
nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
17+
nest_val(n) = nest_val(1, 1, n)
18+
19+
# type_depth_limit(f; maxtypedepth=2) # works
20+
# type_depth_limit(typeof(f); maxtypedepth=2) # works
21+
f = nest_val(5)
1122
a = Any[f];
1223
mysum(a[1]) # make sure it runs
1324
Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and F49231 will be there
25+
26+
# f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);
27+
# a = Any[f];
28+
# mysum(a[1]) # make sure it runs
29+
# Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and F49231 will be there
30+
31+
32+
# struct F49231{a,b,c,d,e,f,g}
33+
# num::g
34+
# end;
35+
# struct Nested{A,B} end
36+
# nest_val(na, nb, ::Val{1}) = Nested{na, nb}
37+
# nest_val(na, nb, ::Val{n}) where {n} = nest_val(Nested{na, nb}, Nested{na, nb}, Val(n-1))
38+
# nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
39+
# nest_val(n) = nest_val(1, 1, n)
40+
# nested = nest_val(5)()
41+
# function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int})
42+
# sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
43+
# return Base.type_depth_limit(s, max(sz[2], 120); maxdepth=maxtypedepth)
44+
# end
45+
# function type_depth_limit(data; maxtypedepth::Union{Nothing,Int}=2)
46+
# buf = IOBuffer()
47+
# io = IOContext(buf, :limit => true)
48+
# type_depth_limit(io, string(typeof(data)); maxtypedepth=maxtypedepth)
49+
# end
50+
# type_depth_limit(nested;maxtypedepth=2)
51+
52+
# f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);
53+
54+
# buf = IOBuffer()
55+
# io = IOContext(buf, :limit => true)
56+
# write(io, type_depth_limit(io, string(typeof(f)); maxtypedepth=2))
57+
# s = String(take!(buf))
58+
# @show s

0 commit comments

Comments
 (0)