Skip to content

Commit 6899a17

Browse files
Add type_depth_limit to CthulhuConfig
Add helper Use depth-limited type printing
1 parent 17c53a1 commit 6899a17

File tree

4 files changed

+93
-15
lines changed

4 files changed

+93
-15
lines changed

TypedSyntax/src/show.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ end
3232
function Base.printstyled(io::IO, rootnode::MaybeTypedSyntaxNode;
3333
type_annotations::Bool=true, iswarn::Bool=true, hide_type_stable::Bool=true,
3434
with_linenumber::Bool=true,
35-
idxend = last_byte(rootnode))
35+
idxend = last_byte(rootnode),
36+
maxtypedepth = 2
37+
)
3638
rt = gettyp(rootnode)
3739
nd = with_linenumber ? ndigits_linenumbers(rootnode, idxend) : 0
3840
rootnode = get_function_def(rootnode)
@@ -43,11 +45,11 @@ function Base.printstyled(io::IO, rootnode::MaybeTypedSyntaxNode;
4345
@assert length(children(rootnode)) == 2
4446
sig, body = children(rootnode)
4547
type_annotate, pre, pre2, post = type_annotation_mode(sig, rt; type_annotations, hide_type_stable)
46-
position = show_src_expr(io, sig, position, pre, pre2; type_annotations, iswarn, hide_type_stable, nd)
47-
type_annotate && show_annotation(io, rt, post, rootnode.source, position; iswarn)
48+
position = show_src_expr(io, sig, position, pre, pre2; type_annotations, iswarn, hide_type_stable, nd, maxtypedepth)
49+
type_annotate && show_annotation(io, rt, post, rootnode.source, position; iswarn, maxtypedepth)
4850
rootnode = body
4951
end
50-
position = show_src_expr(io, rootnode, position, "", ""; type_annotations, iswarn, hide_type_stable, nd)
52+
position = show_src_expr(io, rootnode, position, "", ""; type_annotations, iswarn, hide_type_stable, nd, maxtypedepth)
5153
catchup(io, rootnode, position, nd, idxend+1) # finish the node
5254
return nothing
5355
end
@@ -63,7 +65,7 @@ function _print(io::IO, x, node, position)
6365
end
6466
end
6567

66-
function show_src_expr(io::IO, node::MaybeTypedSyntaxNode, position::Int, pre::String, pre2::String; type_annotations::Bool=true, iswarn::Bool=false, hide_type_stable::Bool=false, nd::Int)
68+
function show_src_expr(io::IO, node::MaybeTypedSyntaxNode, position::Int, pre::String, pre2::String; type_annotations::Bool=true, iswarn::Bool=false, hide_type_stable::Bool=false, nd::Int, maxtypedepth)
6769
_lastidx = last_byte(node)
6870
position = catchup(io, node, position, nd)
6971
if haschildren(node)
@@ -77,8 +79,8 @@ function show_src_expr(io::IO, node::MaybeTypedSyntaxNode, position::Int, pre::S
7779
i == 2 && _print(io, pre2, node.source, position)
7880
cT = gettyp(child)
7981
ctype_annotate, cpre, cpre2, cpost = type_annotation_mode(child, cT; type_annotations, hide_type_stable)
80-
position = show_src_expr(io, child, position, cpre, cpre2; type_annotations, iswarn, hide_type_stable, nd)
81-
ctype_annotate && show_annotation(io, cT, cpost, node.source, position; iswarn)
82+
position = show_src_expr(io, child, position, cpre, cpre2; type_annotations, iswarn, hide_type_stable, nd, maxtypedepth)
83+
ctype_annotate && show_annotation(io, cT, cpost, node.source, position; iswarn, maxtypedepth)
8284
end
8385
return Int(catchup(io, node, position, nd, _lastidx+1))
8486
end
@@ -113,12 +115,25 @@ function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hi
113115
return type_annotate, pre, pre2, post
114116
end
115117

116-
function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool)
118+
function type_depth_limit(io::IO, s::String; maxtypedepth::Union{Nothing,Int})
119+
sz = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
120+
return Base.type_depth_limit(s, max(sz[2], 120); maxdepth=maxtypedepth)
121+
end
122+
123+
type_depth_limit(::T; maxtypedepth) where {T} = type_depth_limit(T; maxtypedepth)
124+
125+
function type_depth_limit(::Type{T}; maxtypedepth) where {T}
126+
buf = IOBuffer()
127+
io = IOContext(buf, :limit => true)
128+
type_depth_limit(io, string(T); maxtypedepth)
129+
end
130+
131+
function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool, maxtypedepth)
117132
diagnostics = get(io, :diagnostics, nothing)
118133
inlay_hints = get(io, :inlay_hints, nothing)
119134

120135
print(io, post)
121-
T_str = string(T)
136+
T_str = type_depth_limit(T; maxtypedepth)
122137
if iswarn && is_type_unstable(T)
123138
color = is_small_union_or_tunion(T) ? :yellow : :red
124139
printstyled(io, "::", T_str; color)

src/Cthulhu.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Base.@kwdef mutable struct CthulhuConfig
4747
inlay_types_vscode::Bool = true
4848
diagnostics_vscode::Bool = true
4949
jump_always::Bool = false
50+
type_depth_limit::Union{Nothing, Int} = 2
5051
end
5152

5253
"""
@@ -411,6 +412,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs
411412
inlay_types_vscode::Bool = CONFIG.inlay_types_vscode, # default is true
412413
diagnostics_vscode::Bool = CONFIG.diagnostics_vscode, # default is true
413414
jump_always::Bool = CONFIG.jump_always, # default is false
415+
type_depth_limit::Union{Nothing, Int} = CONFIG.type_depth_limit, # default is 2
414416
)
415417

416418
if isnothing(hide_type_stable)
@@ -651,7 +653,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs
651653
remarks, with_effects, exception_type, inline_cost,
652654
type_annotations, annotate_source,
653655
inlay_types_vscode, diagnostics_vscode,
654-
jump_always)
656+
jump_always, type_depth_limit)
655657

656658
elseif toggle === :warn
657659
iswarn ⊻= true

src/codeview.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
119119
lineprinter = __debuginfo[debuginfo]
120120
rettype = ignorelimited(rt)
121121
lambda_io = IOContext(io, :limit=>true)
122+
maxtypedepth = CONFIG.type_depth_limit
122123

123124
if annotate_source && isa(src, CodeInfo)
124125
tsn, _ = get_typed_sourcetext(mi, src, rt)
@@ -147,9 +148,9 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
147148
)
148149

149150
if istruncated
150-
printstyled(lambda_io, tsn; type_annotations, iswarn, hide_type_stable, idxend)
151+
printstyled(lambda_io, tsn; type_annotations, iswarn, hide_type_stable, idxend, maxtypedepth)
151152
else
152-
printstyled(vscode_io, tsn; type_annotations, iswarn, hide_type_stable, idxend)
153+
printstyled(vscode_io, tsn; type_annotations, iswarn, hide_type_stable, idxend, maxtypedepth)
153154
end
154155

155156
callsite_diagnostics = TypedSyntax.Diagnostic[]
@@ -186,6 +187,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
186187
show_variables(io, src, slotnames)
187188
end
188189
end
190+
maxtypedepth = CONFIG.type_depth_limit
189191

190192
# preprinter configuration
191193
preprinter = if src isa IRCode && inline_cost
@@ -215,7 +217,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
215217
idx == -1 ? lpad(total_cost, nd+1) :
216218
" "^(nd+1)
217219
str = sprint(; context=:color=>true) do @nospecialize io
218-
printstyled(io, str; color=:green)
220+
printstyled(io, str; color=:green, maxtypedepth)
219221
end
220222
if debuginfo === :source
221223
str *= " "
@@ -263,7 +265,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
263265
function (io::IO; idx::Int, @nospecialize(kws...))
264266
_postprinter(io; idx, kws...)
265267
for i = searchsorted(pc2remarks, idx=>"", by=((idx,msg),)->idx)
266-
printstyled(io, ' ', pc2remarks[i].second; color=:light_black)
268+
printstyled(io, ' ', pc2remarks[i].second; color=:light_black, maxtypedepth)
267269
end
268270
end
269271
else
@@ -301,8 +303,9 @@ function descend_into_callsite!(io::IO, tsn::TypedSyntaxNode;
301303
# We empty the body when filling kwargs
302304
istruncated = isempty(children(body))
303305
idxend = istruncated ? JuliaSyntax.last_byte(sig) : lastindex(tsn.source)
306+
maxtypedepth = CONFIG.type_depth_limit
304307
if !istruncated # If method only fills in default arguments
305-
printstyled(io, tsn; type_annotations, iswarn, hide_type_stable, idxend)
308+
printstyled(io, tsn; type_annotations, iswarn, hide_type_stable, idxend, maxtypedepth)
306309
end
307310
end
308311

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#=
2+
using Revise; include(joinpath("test", "test_depth_limited_type_printing.jl"))
3+
=#
4+
import Cthulhu
5+
6+
Base.@kwdef struct Nested{A,B}
7+
num::Int = 1
8+
end
9+
struct F49231{a,b,c,d,e,f,g}
10+
num::g
11+
end;
12+
bar(x) = rand() > 0.5 ? x : Any[0][1]
13+
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)
22+
a = Any[f];
23+
mysum(a[1]) # make sure it runs
24+
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)