Skip to content

Commit f0beb7f

Browse files
authored
Optics show improvements: merge pull request #186 from aplavin/show
2 parents 2675bd3 + 1df7bc9 commit f0beb7f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/sugar.jl

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,17 @@ IndexLens(::Tuple{Properties}) = Properties()
497497
### nice show() for optics
498498
_shortstring(prev, o::PropertyLens{field}) where {field} = "$prev.$field"
499499
_shortstring(prev, o::IndexLens) ="$prev[$(join(repr.(o.indices), ", "))]"
500-
_shortstring(prev, o::Function) = _isoperator(o) ? "$o$prev" : "$o($prev)"
501-
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(o.f) $prev" : "$(o.f)($(o.x), $prev)"
502-
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(o.f) $(o.x)" : "$(o.f)($prev, $(o.x))"
500+
_shortstring(prev, o::Union{Function,Type}) = _isoperator(o) ? "$(_fT_repr(o))$prev" : "$(_fT_repr(o))($prev)"
501+
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(_fT_repr(o.f)) $prev" : "$(_fT_repr(o.f))($(o.x), $prev)"
502+
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(_fT_repr(o.f)) $(o.x)" : "$(_fT_repr(o.f))($prev, $(o.x))"
503503
_shortstring(prev, o::Elements) = "$prev[∗]"
504504
_shortstring(prev, o::Properties) = "$prev[∗ₚ]"
505505

506+
# compact representation of functions and types
507+
# most notably, it deals with the module name in a consistent way: doesn't show it
508+
# by default, it's not shown for functions but shown for types, see https://github.com/JuliaLang/julia/issues/56790
509+
_fT_repr(o) = repr(o; context=:compact => true)
510+
506511
# can f be stringfied using the operator (infix) syntax?
507512
# otherwise uses regular function call syntax
508513
_isoperator(f::Function) = Base.isoperator(nameof(f))
@@ -520,19 +525,23 @@ function show_optic(io, optic)
520525
outer = Iterators.dropwhile(x -> applicable(_shortstring, "", x), opts)
521526
if !isempty(outer)
522527
show(io, opcompose(outer...))
528+
end
529+
if !isempty(inner) && !isempty(outer)
523530
print(io, "")
524531
end
525-
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
526-
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
527-
if need_parens_prev && _need_parens(o)
528-
prev = "($prev)"
532+
if !isempty(inner)
533+
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
534+
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
535+
if need_parens_prev && _need_parens(o)
536+
prev = "($prev)"
537+
end
538+
_shortstring(prev, o), _need_parens(o)
539+
end |> first
540+
if get(io, :compact, false)
541+
print(io, shortstr)
542+
else
543+
print(io, "(@o ", shortstr, ")")
529544
end
530-
_shortstring(prev, o), _need_parens(o)
531-
end |> first
532-
if get(io, :compact, false)
533-
print(io, shortstr)
534-
else
535-
print(io, "(@o ", shortstr, ")")
536545
end
537546
end
538547

test/test_core.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,16 @@ end
469469
@test sprint(show, (@optic (_.a + 1) * 2)) == "(@o (_.a + 1) * 2)"
470470
@test sprint(show, (@optic (_.a * 2) + 1)) == "(@o (_.a * 2) + 1)"
471471
@test sprint(show, (@optic log(_.a[2]))) == "(@o log(_.a[2]))"
472+
@test sprint(show, (@optic Tuple(_.a[2]))) == "(@o Tuple(_.a[2]))"
472473
@test sprint(show, (@optic log(_).a[2])) == "(@o _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
473474
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
475+
@test sprint(show, (@optic Base.tail(_.a[2])); context=:compact => true) == "tail(_.a[2])" # non-exported function
476+
@test sprint(show, (@optic Base.Fix2(_.a[2])); context=:compact => true) == "Fix2(_.a[2])" # non-exported type
477+
478+
# show_optic is reasonable even for types without special show_optic handling:
479+
o = Recursive(x->true, Properties())
480+
@test sprint(Accessors.show_optic, o) == "$o"
481+
@test sprint(Accessors.show_optic, (@o _.a) o) == "(@o _.a) ∘ $o"
474482
end
475483

476484
@testset "text/plain show" begin

0 commit comments

Comments
 (0)