Skip to content

Commit

Permalink
don't append " ∘ (@o _)" in show_optic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aplavin committed Dec 22, 2024
1 parent a98868d commit 1df7bc9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,19 +521,23 @@ function show_optic(io, optic)
outer = Iterators.dropwhile(x -> applicable(_shortstring, "", x), opts)
if !isempty(outer)
show(io, opcompose(outer...))
end
if !isempty(inner) && !isempty(outer)
print(io, "")
end
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
if need_parens_prev && _need_parens(o)
prev = "($prev)"
if !isempty(inner)
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
if need_parens_prev && _need_parens(o)
prev = "($prev)"
end
_shortstring(prev, o), _need_parens(o)
end |> first
if get(io, :compact, false)
print(io, shortstr)
else
print(io, "(@o ", shortstr, ")")
end
_shortstring(prev, o), _need_parens(o)
end |> first
if get(io, :compact, false)
print(io, shortstr)
else
print(io, "(@o ", shortstr, ")")
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ end
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
@test sprint(show, (@optic Base.tail(_.a[2])); context=:compact => true) == "tail(_.a[2])" # non-exported function
@test sprint(show, (@optic Base.Fix2(_.a[2])); context=:compact => true) == "Fix2(_.a[2])" # non-exported type

# show_optic is reasonable even for types without special show_optic handling:
o = Recursive(x->true, Properties())
@test sprint(Accessors.show_optic, o) == "$o"
@test sprint(Accessors.show_optic, (@o _.a) o) == "(@o _.a) ∘ $o"
end

@testset "text/plain show" begin
Expand Down

0 comments on commit 1df7bc9

Please sign in to comment.