diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index f3bfbe05..9edff2cd 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -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) diff --git a/src/print.jl b/src/print.jl index 450dc9d6..02a550fd 100644 --- a/src/print.jl +++ b/src/print.jl @@ -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; maxtypedepth::Union{Nothing,Int}=2) where {T} = type_depth_limit(T; maxtypedepth) + +function type_depth_limit(::Type{T}; maxtypedepth::Union{Nothing,Int}=2) where {T} + buf = IOBuffer() + io = IOContext(buf, :limit => true) + type_depth_limit(io, string(T); maxtypedepth=maxtypedepth) +end diff --git a/test/test_depth_limited_type_printing.jl b/test/test_depth_limited_type_printing.jl index ab12fb4a..7c26814f 100644 --- a/test/test_depth_limited_type_printing.jl +++ b/test/test_depth_limited_type_printing.jl @@ -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