Skip to content

Commit

Permalink
Use depth-limited type printing
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Apr 25, 2024
1 parent fccf284 commit 5b42185
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
3 changes: 2 additions & 1 deletion TypedSyntax/src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Boo
inlay_hints = get(io, :inlay_hints, nothing)

print(io, post)
T_str = string(T)
T_str = type_depth_limit(T)
# T_str = string(T)
if iswarn && is_type_unstable(T)
color = is_small_union_or_tunion(T) ? :yellow : :red
printstyled(io, "::", T_str; color)
Expand Down
16 changes: 9 additions & 7 deletions src/print.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function type_depth_limit(data; maxtypedepth::Union{Nothing,Int}=2)
buf = IOBuffer()
type_depth_limit(buf, string(data); maxtypedepth)
return String(take!(buf))
end

function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int}=2)
function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int})
sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
return Base.type_depth_limit(s, max(sz[2], 120); maxdepth=maxtypedepth)
end

type_depth_limit(::T) where {T} = type_depth_limit(T)

function type_depth_limit(::Type{T}) where {T}
buf = IOBuffer()
io = IOContext(buf, :limit => true)
type_depth_limit(io, string(T); maxtypedepth=CONFIG.type_depth_limit)
end
47 changes: 46 additions & 1 deletion test/test_depth_limited_type_printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,57 @@
using Revise; include(joinpath("test", "test_depth_limited_type_printing.jl"))
=#
import Cthulhu

Base.@kwdef struct Nested{A,B}
num::Int = 1
end
struct F49231{a,b,c,d,e,f,g}
num::g
end;
f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);
bar(x) = rand() > 0.5 ? x : Any[0][1]
mysum(x) = sum(y-> bar(x.num), 1:5; init=0)
nest_val(na, nb, ::Val{1}) = Nested{na, nb}()
nest_val(na, nb, ::Val{n}) where {n} = nest_val(Nested{na, nb}, Nested{na, nb}, Val(n-1))
nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
nest_val(n) = nest_val(1, 1, n)

# type_depth_limit(f; maxtypedepth=2) # works
# type_depth_limit(typeof(f); maxtypedepth=2) # works
f = nest_val(5)
a = Any[f];
mysum(a[1]) # make sure it runs
Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and F49231 will be there

# f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);
# a = Any[f];
# mysum(a[1]) # make sure it runs
# Cthulhu.@descend mysum(a[1]) # navigate to sum -> sum, and F49231 will be there


# struct F49231{a,b,c,d,e,f,g}
# num::g
# end;
# struct Nested{A,B} end
# nest_val(na, nb, ::Val{1}) = Nested{na, nb}
# nest_val(na, nb, ::Val{n}) where {n} = nest_val(Nested{na, nb}, Nested{na, nb}, Val(n-1))
# nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
# nest_val(n) = nest_val(1, 1, n)
# nested = nest_val(5)()
# function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int})
# sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
# return Base.type_depth_limit(s, max(sz[2], 120); maxdepth=maxtypedepth)
# end
# function type_depth_limit(data; maxtypedepth::Union{Nothing,Int}=2)
# buf = IOBuffer()
# io = IOContext(buf, :limit => true)
# type_depth_limit(io, string(typeof(data)); maxtypedepth=maxtypedepth)
# end
# type_depth_limit(nested;maxtypedepth=2)

# f = F49231{Float64,Float32,Int,String,AbstractString,6,Float64}(1);

# buf = IOBuffer()
# io = IOContext(buf, :limit => true)
# write(io, type_depth_limit(io, string(typeof(f)); maxtypedepth=2))
# s = String(take!(buf))
# @show s

0 comments on commit 5b42185

Please sign in to comment.