Skip to content

Commit e912e46

Browse files
committed
move show code to Common area
1 parent 7e9e778 commit e912e46

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/tangent_types/structural_tangent.jl

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ function Base.map(f, tangent::StructuralTangent{P}) where {P}
140140
end
141141
end
142142

143+
144+
function Base.show(io::IO, tangent::StructuralTangent{P}) where {P}
145+
if tangent isa MutableTangent
146+
print(io, "Mutable")
147+
end
148+
print(io, "Tangent{")
149+
str = sprint(show, P, context = io)
150+
i = findfirst('{', str)
151+
if isnothing(i)
152+
print(io, str)
153+
else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line:
154+
print(io, str[1:prevind(str, i)])
155+
if length(str) < 80
156+
printstyled(io, str[i:end], color=:light_black)
157+
else
158+
printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black)
159+
end
160+
end
161+
print(io, "}")
162+
if isempty(backing(tangent))
163+
print(io, "()") # so it doesn't show `NamedTuple()`
164+
else
165+
# allow Tuple or NamedTuple `show` to do the rendering of brackets etc
166+
show(io, backing(tangent))
167+
end
168+
end
169+
143170
"""
144171
backing(x)
145172
@@ -334,32 +361,6 @@ Base.:(==)(a::Tangent{P}, b::Tangent{Q}) where {P,Q} = false
334361

335362
Base.hash(a::Tangent, h::UInt) = Base.hash(backing(canonicalize(a)), h)
336363

337-
function Base.show(io::IO, tangent::StructuralTangent{P}) where {P}
338-
if tangent isa MutableTangent
339-
print(io, "Mutable")
340-
end
341-
print(io, "Tangent{")
342-
str = sprint(show, P, context = io)
343-
i = findfirst('{', str)
344-
if isnothing(i)
345-
print(io, str)
346-
else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line:
347-
print(io, str[1:prevind(str, i)])
348-
if length(str) < 80
349-
printstyled(io, str[i:end], color=:light_black)
350-
else
351-
printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black)
352-
end
353-
end
354-
print(io, "}")
355-
if isempty(backing(tangent))
356-
print(io, "()") # so it doesn't show `NamedTuple()`
357-
else
358-
# allow Tuple or NamedTuple `show` to do the rendering of brackets etc
359-
show(io, backing(tangent))
360-
end
361-
end
362-
363364
Base.iszero(::Tangent{<:,NamedTuple{}}) = true
364365
Base.iszero(::Tangent{<:,Tuple{}}) = true
365366

0 commit comments

Comments
 (0)