3232function 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
5355end
@@ -63,7 +65,7 @@ function _print(io::IO, x, node, position)
6365 end
6466end
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 ))
8486end
@@ -113,12 +115,25 @@ function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hi
113115 return type_annotate, pre, pre2, post
114116end
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)
0 commit comments