From 9e9c7d6b1afef22fbbfcd255f70543291223dea2 Mon Sep 17 00:00:00 2001 From: TEC Date: Mon, 29 Apr 2024 21:44:58 +0800 Subject: [PATCH] Implement eval-able AnnotatedString 2-arg show The generic/fallback AbstractString 2-arg show omits the annotations, meaning that eval(Meta.parse(repr(::AnnotatedString))) doesn't round-trip. The resolution to this is fairly simple, we just need to implement a specialised show method. The implementation is fairly obvious, we're just also able to get away with hiding the vector type annotation since the constructor is fine without it. --- base/strings/annotated.jl | 12 ++++++++++++ test/strings/annotated.jl | 2 ++ 2 files changed, 14 insertions(+) diff --git a/base/strings/annotated.jl b/base/strings/annotated.jl index 2a331c5133f838..5d4c09391a9ef8 100644 --- a/base/strings/annotated.jl +++ b/base/strings/annotated.jl @@ -159,6 +159,18 @@ function getindex(s::AnnotatedString, i::Integer) end end +# To make `AnnotatedString`s repr-evaluable, we need to override +# the generic `AbstractString` 2-arg show method. + +function show(io::IO, s::A) where {A <: AnnotatedString} + show(io, A) + print(io, '(') + show(io, s.string) + print(io, ", ") + show(IOContext(io, :typeinfo => typeof(annotations(s))), annotations(s)) + print(io, ')') +end + ## AbstractChar interface ## ncodeunits(c::AnnotatedChar) = ncodeunits(c.char) diff --git a/test/strings/annotated.jl b/test/strings/annotated.jl index b70a2350757a27..96a494933f648c 100644 --- a/test/strings/annotated.jl +++ b/test/strings/annotated.jl @@ -58,6 +58,8 @@ (3:3, :val => 2)])), Base.AnnotatedString("abc", [(1:2, :val => 1), (2:3, :val => 2)])) + @test chopprefix(sprint(show, str), "Base.") == + "AnnotatedString{String}(\"some string\", , [(1:4, :thing=>0x01), (6:11, :other=>0x02), (1:11, :all=>0x03)])" end @testset "AnnotatedChar" begin