@@ -493,12 +493,23 @@ IndexLens(::Tuple{Properties}) = Properties()
493
493
# ## nice show() for optics
494
494
_shortstring (prev, o:: PropertyLens{field} ) where {field} = " $prev .$field "
495
495
_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) )"
499
499
_shortstring (prev, o:: Elements ) = " $prev [∗]"
500
500
_shortstring (prev, o:: Properties ) = " $prev [∗ₚ]"
501
501
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
+
502
513
function show_optic (io, optic)
503
514
opts = deopcompose (optic)
504
515
inner = Iterators. takewhile (x -> applicable (_shortstring, " " , x), opts)
@@ -507,7 +518,13 @@ function show_optic(io, optic)
507
518
show (io, opcompose (outer... ))
508
519
print (io, " ∘ " )
509
520
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
511
528
if get (io, :compact , false )
512
529
print (io, shortstr)
513
530
else
0 commit comments