Skip to content

Commit 18d8625

Browse files
authored
Merge pull request #155 from aplavin/master
nicer show for optics
2 parents 270e78f + d5520ac commit 18d8625

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/sugar.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,23 @@ IndexLens(::Tuple{Properties}) = Properties()
493493
### nice show() for optics
494494
_shortstring(prev, o::PropertyLens{field}) where {field} = "$prev.$field"
495495
_shortstring(prev, o::IndexLens) ="$prev[$(join(repr.(o.indices), ", "))]"
496-
_shortstring(prev, o::Function) = "$o($prev)"
497-
_shortstring(prev, o::Base.Fix1) = "$(o.f)($(o.x), $prev)"
498-
_shortstring(prev, o::Base.Fix2) = "$(o.f)($prev, $(o.x))"
496+
_shortstring(prev, o::Function) = _isoperator(o) ? "$o$prev" : "$o($prev)"
497+
_shortstring(prev, o::Base.Fix1) = _isoperator(o.f) ? "$(o.x) $(o.f) $prev" : "$(o.f)($(o.x), $prev)"
498+
_shortstring(prev, o::Base.Fix2) = _isoperator(o.f) ? "$prev $(o.f) $(o.x)" : "$(o.f)($prev, $(o.x))"
499499
_shortstring(prev, o::Elements) = "$prev[∗]"
500500
_shortstring(prev, o::Properties) = "$prev[∗ₚ]"
501501

502+
# can f be stringfied using the operator (infix) syntax?
503+
# otherwise uses regular function call syntax
504+
_isoperator(f::Function) = Base.isoperator(nameof(f))
505+
_isoperator(f) = false
506+
507+
# does o need parens when nested in another such o?
508+
# let's be conservative and always put parens around operators-in-operators
509+
_need_parens(o::Base.Fix1) = _isoperator(o.f)
510+
_need_parens(o::Base.Fix2) = _isoperator(o.f)
511+
_need_parens(o) = _isoperator(o)
512+
502513
function show_optic(io, optic)
503514
opts = deopcompose(optic)
504515
inner = Iterators.takewhile(x -> applicable(_shortstring, "", x), opts)
@@ -507,7 +518,13 @@ function show_optic(io, optic)
507518
show(io, opcompose(outer...))
508519
print(io, "")
509520
end
510-
shortstr = reduce(_shortstring, inner; init="_")
521+
shortstr = reduce(inner; init=("_", false)) do (prev, need_parens_prev), o
522+
# if _need_parens is true for this o and the one before, wrap the previous one in parentheses
523+
if need_parens_prev && _need_parens(o)
524+
prev = "($prev)"
525+
end
526+
_shortstring(prev, o), _need_parens(o)
527+
end |> first
511528
if get(io, :compact, false)
512529
print(io, shortstr)
513530
else

test/test_core.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ end
461461

462462
@testset "full and compact show" begin
463463
@test sprint(show, (@optic _.a)) == "(@o _.a)"
464+
@test sprint(show, (@optic _.a + 1)) == "(@o _.a + 1)"
465+
@test sprint(show, (@optic (_.a + 1) * 2)) == "(@o (_.a + 1) * 2)"
466+
@test sprint(show, (@optic (_.a * 2) + 1)) == "(@o (_.a * 2) + 1)"
464467
@test sprint(show, (@optic log(_.a[2]))) == "(@o log(_.a[2]))"
465468
@test sprint(show, (@optic log(_).a[2])) == "(@o _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
466469
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
@@ -512,6 +515,7 @@ end
512515
(@optic _.a) UserDefinedLens() (@optic _.b)
513516
(@optic _.a) LensIfTextPlain() (@optic _.b)
514517
@optic 2 * (abs(_.a.b[2].c) + 1)
518+
@optic 2 * (-(abs(_.a.b[2].c) + 1))
515519
@optic !(_.a) # issue 105
516520
]
517521
buf = IOBuffer()

0 commit comments

Comments
 (0)