Skip to content

Commit

Permalink
improve stringification
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Dec 18, 2024
1 parent 92b7218 commit bbccbe1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 30 deletions.
16 changes: 1 addition & 15 deletions src/AccessorsExtra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ include("regex.jl")
include("replace.jl")
include("moremacros.jl")
include("construct.jl")
include("nicer_show.jl")
include("bystep.jl")
include("testing.jl")

Expand Down Expand Up @@ -71,19 +72,6 @@ function __init__()
end


Accessors._shortstring(prev, o::Returns) = sprint(show, o.value)
Accessors._shortstring(prev, os::OSomething) =
(prev == "_" ? "" : "(") * join(map(barebones_string, os.os), " || ") * (prev == "_" ? "" : ") ∘ $(prev)")

barebones_string(optic::Base.Splat) = sprint(Accessors.show_optic, optic; context=:compact => true)
barebones_string(optic::Union{Base.Fix1,Base.Fix2}) = sprint(Accessors.show_optic, optic; context=:compact => true)
barebones_string(optic::typeof(identity)) = "_"
barebones_string(optic) = @p let
sprint(Accessors.show_optic, optic; context=:compact => true)
replace(__, "_." => "", "_[" => "[")
end


Base.@propagate_inbounds set(obj, lens::Base.Fix2{typeof(view)}, val) = setindex!(obj, val, lens.x)
Base.@propagate_inbounds set(obj, lens::Base.Fix2{typeof(view), <:Integer}, val::AbstractArray{<:Any, 0}) = setindex!(obj, only(val), lens.x)

Expand Down Expand Up @@ -116,8 +104,6 @@ function set(obj, f::Base.Fix1{typeof(getindex)}, val)
return ix
end

Accessors._shortstring(prev, o::Base.Splat) = "$(o.f)($prev...)"

# unambiguous for unitranges, but tension with general array @set first(x)...
# piracy
set(r::AbstractUnitRange, ::typeof(first), x) = x:last(r)
Expand Down
48 changes: 37 additions & 11 deletions src/maybe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,31 @@ end

@inline delete(obj, o::MaybeOptic) = hasoptic(obj, o.o) ? delete(obj, o.o) : obj


Accessors._shortstring(prev, o::MaybeOptic) = Accessors._shortstring(prev, o.o) * "?" * (
isnothing(o.default) || ismissing(o.default) || (o.default isa Number && isnan(o.default)) ?
"" : string(o.default)
)
function Base.show(io::IO, o::MaybeOptic)
if get(io, :compact, false)
print(io, o.o)
if isnothing(o.default)
print(io, "?")
else
print(io, " || ", o.default)
end
else
print(io, "(@maybe ")
Accessors.show_optic(IOContext(io, :compact => true), o.o)
if o.default != nothing
print(io, " ", o.default)
end
print(io, ")")
end
end
function Accessors._shortstring(prev, o::MaybeOptic)
res = Accessors._shortstring(prev, o.o)
if isnothing(o.default) || ismissing(o.default) || (o.default isa Number && isnan(o.default))
res *= "?"
else
res *= " || " * sprint(show, o.default; context=:compact => true)
end
end

struct OSomething{OS}
os::OS
Expand All @@ -109,13 +129,19 @@ osomething(optics...) = OSomething(optics)
@inline set(obj, o::OSomething{Tuple{}}, val) = error("no optic in osomething applicable to $obj")

function Base.show(io::IO, os::OSomething)
compact = get(io, :compact, false)
print(io, compact ? "some(" : "osomething(")
for (i, o) in enumerate(os.os)
i == 1 || print(io, ", ")
Accessors.show_optic(io, o)
if get(io, :compact, false)
for (i, o) in enumerate(os.os)
i == 1 || print(io, " || ")
Accessors.show_optic(IOContext(io, :compact => true), o)
end
else
print(io, "(@osomething ")
for (i, o) in enumerate(os.os)
i == 1 || print(io, " ")
Accessors.show_optic(IOContext(io, :compact => true), o)
end
print(io, ")")
end
print(io, ")")
end
Base.show(io::IO, ::MIME"text/plain", optic::OSomething) = show(io, optic)

Expand Down
40 changes: 40 additions & 0 deletions src/nicer_show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# some piracy, but cannot upstream: relies on @o parsing extensions here
Accessors._shortstring(prev, o::Base.Splat) = "$(o.f)($prev...)"
Accessors._shortstring(prev, o::Returns) = sprint(show, o.value)

barebones_string(optic::Base.Splat) = sprint(Accessors.show_optic, optic; context=:compact => true)
barebones_string(optic::Union{Base.Fix1,Base.Fix2}) = sprint(Accessors.show_optic, optic; context=:compact => true)
barebones_string(optic::typeof(identity)) = "_"
barebones_string(optic) = @p let
sprint(Accessors.show_optic, optic; context=:compact => true)
replace(__, "_." => "", "_[" => "[")
end



# XXX: piracy, should upstream the changes
function Accessors.show_optic(io::IO, optic)
opts = deopcompose(optic)
inner = Iterators.takewhile(x -> applicable(Accessors._shortstring, "", x), opts)
outer = Iterators.dropwhile(x -> applicable(Accessors._shortstring, "", x), opts)
if !isempty(outer)
show(io, opcompose(outer...))
end
if !isempty(inner) && !isempty(outer)
print(io, "")
end
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 && Accessors._need_parens(o)
prev = "($prev)"
end
Accessors._shortstring(prev, o), Accessors._need_parens(o)
end |> first
if get(io, :compact, false)
print(io, shortstr)
else
print(io, "(@o ", shortstr, ")")
end
end
end
17 changes: 13 additions & 4 deletions test/moresyntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,17 @@ end
@test sprint(show, @o(_.a[ₚ] |> selfcontext() |> _.b); context=:compact => true) == "(_.b)ᵢ ∘ _.a[∗ₚ] |> selfcontext(identity)"
@test sprint(show, (@o _.a + _.b)) == "(@o _.a + _.b)"
@test sprint(show, (@o _.a + _.b); context=:compact => true) == "_.a + _.b"
@test sprint(show, (@osomething _.a 123)) == "osomething((@o _.a), (@o 123))"
@test sprint(show, (@osomething _.a 123); context=:compact => true) == "some(_.a, 123)"

@test sprint(show, @maybe _.a) == "(@maybe _.a)"
@test sprint(show, @maybe _.a 0.2) == "(@maybe _.a 0.2)"
@test sprint(show, (@maybe _.a); context=:compact => true) == "_.a?"
@test sprint(show, (@maybe _.a 0.2); context=:compact => true) == "_.a || 0.2"

@test sprint(show, (@osomething _.a 123)) == "(@osomething _.a 123)"
@test sprint(show, (@o _.a) (@osomething _.a 123)) == "(@o _.a) ∘ (@osomething _.a 123)"
@test sprint(show, (@osomething _.a 123) (@o _.a)) == "(@osomething _.a 123) ∘ (@o _.a)"
@test sprint(show, (@osomething _.a 123); context=:compact => true) == "_.a || 123"
@test sprint(show, (@o _.a) (@osomething _.a 123); context=:compact => true) == "_.a ∘ _.a || 123"

@test map(flat_concatoptic((a=1, b=(2, 3)), (@o _.a exp(_.b[]))).optics) do o
sprint(show, o; context=:compact => true)
Expand All @@ -112,9 +121,9 @@ end
@test barebones_string(@o sort(_, by=abs)) == "sort(_, by=abs)"
@test barebones_string(@o sort(_, 1, by=abs)) == "sort(_, 1, by=abs)"
@test barebones_string(@maybe _.a) == "a?"
@test barebones_string(@maybe _.a 0.2) == "a?0.2"
@test barebones_string(@maybe _.a 0.2) == "a || 0.2"
@test barebones_string(@maybe _.a + _.b) == "a + b?"
@test barebones_string(exp (@maybe _.a + _.b 0.2)) == "exp(a + b?0.2)"
@test barebones_string(exp (@maybe _.a + _.b 0.2)) == "exp(a + b || 0.2)"
@test barebones_string(Returns(123)) == "123"
@test barebones_string(@osomething _.a 123) == "a || 123"
end

0 comments on commit bbccbe1

Please sign in to comment.