Skip to content

Commit 1df7bc9

Browse files
committed
don't append " ∘ (@o _)" in show_optic
show_optic is not a public API, but convenient to use sometimes before, it appended " ∘ (@o _)" unnecessarily for objects that don't have explicit show_optic handling
1 parent a98868d commit 1df7bc9

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/sugar.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,19 +521,23 @@ function show_optic(io, optic)
521521
outer = Iterators.dropwhile(x -> applicable(_shortstring, "", x), opts)
522522
if !isempty(outer)
523523
show(io, opcompose(outer...))
524+
end
525+
if !isempty(inner) && !isempty(outer)
524526
print(io, "")
525527
end
526-
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
527-
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
528-
if need_parens_prev && _need_parens(o)
529-
prev = "($prev)"
528+
if !isempty(inner)
529+
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
530+
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
531+
if need_parens_prev && _need_parens(o)
532+
prev = "($prev)"
533+
end
534+
_shortstring(prev, o), _need_parens(o)
535+
end |> first
536+
if get(io, :compact, false)
537+
print(io, shortstr)
538+
else
539+
print(io, "(@o ", shortstr, ")")
530540
end
531-
_shortstring(prev, o), _need_parens(o)
532-
end |> first
533-
if get(io, :compact, false)
534-
print(io, shortstr)
535-
else
536-
print(io, "(@o ", shortstr, ")")
537541
end
538542
end
539543

test/test_core.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ end
470470
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
471471
@test sprint(show, (@optic Base.tail(_.a[2])); context=:compact => true) == "tail(_.a[2])" # non-exported function
472472
@test sprint(show, (@optic Base.Fix2(_.a[2])); context=:compact => true) == "Fix2(_.a[2])" # non-exported type
473+
474+
# show_optic is reasonable even for types without special show_optic handling:
475+
o = Recursive(x->true, Properties())
476+
@test sprint(Accessors.show_optic, o) == "$o"
477+
@test sprint(Accessors.show_optic, (@o _.a) o) == "(@o _.a) ∘ $o"
473478
end
474479

475480
@testset "text/plain show" begin

0 commit comments

Comments
 (0)