diff --git a/src/tangent_types/structural_tangent.jl b/src/tangent_types/structural_tangent.jl index 7730a6215..a469d9f1b 100644 --- a/src/tangent_types/structural_tangent.jl +++ b/src/tangent_types/structural_tangent.jl @@ -140,6 +140,33 @@ function Base.map(f, tangent::StructuralTangent{P}) where {P} end end + +function Base.show(io::IO, tangent::StructuralTangent{P}) where {P} + if tangent isa MutableTangent + print(io, "Mutable") + end + print(io, "Tangent{") + str = sprint(show, P, context = io) + i = findfirst('{', str) + if isnothing(i) + print(io, str) + else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line: + print(io, str[1:prevind(str, i)]) + if length(str) < 80 + printstyled(io, str[i:end], color=:light_black) + else + printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black) + end + end + print(io, "}") + if isempty(backing(tangent)) + print(io, "()") # so it doesn't show `NamedTuple()` + else + # allow Tuple or NamedTuple `show` to do the rendering of brackets etc + show(io, backing(tangent)) + end +end + """ backing(x) @@ -334,32 +361,6 @@ Base.:(==)(a::Tangent{P}, b::Tangent{Q}) where {P,Q} = false Base.hash(a::Tangent, h::UInt) = Base.hash(backing(canonicalize(a)), h) -function Base.show(io::IO, tangent::StructuralTangent{P}) where {P} - if tangent isa MutableTangent - print(io, "Mutable") - end - print(io, "Tangent{") - str = sprint(show, P, context = io) - i = findfirst('{', str) - if isnothing(i) - print(io, str) - else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line: - print(io, str[1:prevind(str, i)]) - if length(str) < 80 - printstyled(io, str[i:end], color=:light_black) - else - printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black) - end - end - print(io, "}") - if isempty(backing(tangent)) - print(io, "()") # so it doesn't show `NamedTuple()` - else - # allow Tuple or NamedTuple `show` to do the rendering of brackets etc - show(io, backing(tangent)) - end -end - Base.iszero(::Tangent{<:,NamedTuple{}}) = true Base.iszero(::Tangent{<:,Tuple{}}) = true