diff --git a/Project.toml b/Project.toml index 038f5c71..76fb1dfd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Accessors" uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" authors = ["Takafumi Arakaki ", "Jan Weidner and contributors"] -version = "0.1.31" +version = "0.1.32" [deps] CompositionsBase = "a33af91c-f02d-484b-be07-31d278c5ca2b" diff --git a/src/sugar.jl b/src/sugar.jl index 089a8205..5d5de77f 100644 --- a/src/sugar.jl +++ b/src/sugar.jl @@ -444,18 +444,22 @@ function show_optic(io, optic) show(io, opcompose(outer...)) print(io, " ∘ ") end - print(io, "(@optic ", reduce(_shortstring, inner; init="_"), ")") + shortstr = reduce(_shortstring, inner; init="_") + if get(io, :compact, false) + print(io, shortstr) + else + print(io, "(@optic ", shortstr, ")") + end end -Base.show(io::IO, optic::Union{IndexLens, PropertyLens}) = print(io, "(@optic $(_shortstring("_", optic)))") +Base.show(io::IO, optic::Union{IndexLens, PropertyLens}) = show_optic(io, optic) +Base.show(io::IO, ::MIME"text/plain", optic::Union{IndexLens, PropertyLens}) = show(io, optic) +# only need show(io, optic) here because Base defines show(io::IO, ::MIME"text/plain", c::ComposedFunction) = show(io, c) Base.show(io::IO, optic::ComposedFunction{<:Any, <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic) -# resolve method ambiguity: +# resolve method ambiguity with Base: Base.show(io::IO, optic::ComposedFunction{typeof(!), <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic) -Base.show(io::IO, ::MIME"text/plain", optic::Union{IndexLens, PropertyLens}) = show(io, optic) -Base.show(io::IO, ::MIME"text/plain", optic::ComposedFunction{<:Any, <:Union{IndexLens, PropertyLens}}) = show(io, optic) - # debugging show_composition_order(optic) = (show_composition_order(stdout, optic); println()) show_composition_order(io::IO, optic) = show(io, optic) diff --git a/test/test_core.jl b/test/test_core.jl index 5e324682..ba2246eb 100644 --- a/test/test_core.jl +++ b/test/test_core.jl @@ -443,6 +443,13 @@ end @test (@optic _ |> _[1] |> _[2] |> _[3]) === @optic _[1][2][3] end +@testset "full and compact show" begin + @test sprint(show, (@optic _.a)) == "(@optic _.a)" + @test sprint(show, (@optic log(_.a[2]))) == "(@optic log(_.a[2]))" + @test sprint(show, (@optic log(_).a[2])) == "(@optic _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy + @test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])" +end + @testset "text/plain show" begin @testset for lens in [ LensIfTextPlain()