Skip to content

Commit 44f35d9

Browse files
tecosaurtopolarity
authored andcommitted
Workaround StyledStrings type-piracy (#58112)
After discussion with Cody, this is an attempt at a more minimal version of #56194. The intent is to work around the invalidation issue introduced by the split design with AnnotatedStrings, resolving the headache for 1.12. Paired with JuliaLang/StyledStrings.jl#115 Many thanks to @topolarity for working out this approach, the discussion on balancing the short/long term fixes for this issue. ------ From my understanding of the problem, this should fix the `write`/`print`/`show` invalidations, but this needs to be checked. --------- Co-authored-by: Cody Tapscott <topolarity@tapscott.me> (cherry picked from commit 6d78a4a)
1 parent d9da421 commit 44f35d9

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

base/strings/annotated_io.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,78 @@ function _insert_annotations!(io::AnnotatedIOBuffer, annotations::Vector{RegionA
199199
push!(io.annotations, setindex(annotations[index], start+offset:stop+offset, :region))
200200
end
201201
end
202+
203+
# NOTE: This is an interim solution to the invalidations caused
204+
# by the split styled display implementation. This should be
205+
# replaced by a more robust solution (such as a consolidation of
206+
# the type and method definitions) in the near future.
207+
module AnnotatedDisplay
208+
209+
using ..Base: IO, SubString, AnnotatedString, AnnotatedChar, AnnotatedIOBuffer
210+
using ..Base: eachregion, invoke_in_world, tls_world_age
211+
212+
# Write
213+
214+
ansi_write(f::Function, io::IO, x::Any) = f(io, String(x))
215+
216+
ansi_write_(f::Function, io::IO, @nospecialize(x::Any)) =
217+
invoke_in_world(tls_world_age(), ansi_write, f, io, x)
218+
219+
Base.write(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
220+
ansi_write_(write, io, s)::Int
221+
222+
Base.write(io::IO, c::AnnotatedChar) =
223+
ansi_write_(write, io, c)::Int
224+
225+
function Base.write(io::IO, aio::AnnotatedIOBuffer)
226+
if get(io, :color, false) == true
227+
# This does introduce an overhead that technically
228+
# could be avoided, but I'm not sure that it's currently
229+
# worth the effort to implement an efficient version of
230+
# writing from a AnnotatedIOBuffer with style.
231+
# In the meantime, by converting to an `AnnotatedString` we can just
232+
# reuse all the work done to make that work.
233+
ansi_write_(write, io, read(aio, AnnotatedString))::Int
234+
else
235+
write(io, aio.io)
236+
end
237+
end
238+
239+
# Print
240+
241+
Base.print(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
242+
(ansi_write_(write, io, s); nothing)
243+
244+
Base.print(io::IO, s::AnnotatedChar) =
245+
(ansi_write_(write, io, s); nothing)
246+
247+
Base.print(io::AnnotatedIOBuffer, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
248+
(write(io, s); nothing)
249+
250+
Base.print(io::AnnotatedIOBuffer, c::AnnotatedChar) =
251+
(write(io, c); nothing)
252+
253+
# Escape
254+
255+
Base.escape_string(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedString}},
256+
esc = ""; keep = (), ascii::Bool=false, fullhex::Bool=false) =
257+
(ansi_write_((io, s) -> escape_string(io, s, esc; keep, ascii, fullhex), io, s); nothing)
258+
259+
# Show
260+
261+
show_annot(io::IO, ::Any) = nothing
262+
show_annot(io::IO, ::MIME, ::Any) = nothing
263+
264+
show_annot_(io::IO, @nospecialize(x::Any)) =
265+
invoke_in_world(tls_world_age(), show_annot, io, x)::Nothing
266+
267+
show_annot_(io::IO, m::MIME, @nospecialize(x::Any)) =
268+
invoke_in_world(tls_world_age(), show_annot, io, m, x)::Nothing
269+
270+
Base.show(io::IO, m::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}}) =
271+
show_annot_(io, m, s)
272+
273+
Base.show(io::IO, m::MIME"text/html", c::AnnotatedChar) =
274+
show_annot_(io, m, c)
275+
276+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1cb6007a66d3f74cbe5b27ee449aa9c8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1fa95646fdf4cc7ea282bd355fded9464e7572792912942ea1c45f6ed126eead2333fdeed92e7db3efbcd6c3a171a04e5c9562dab2685bb39947136284ae1da3

deps/checksums/StyledStrings-8985a37ac054c37d084a03ad2837208244824877.tar.gz/md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

deps/checksums/StyledStrings-8985a37ac054c37d084a03ad2837208244824877.tar.gz/sha512

Lines changed: 0 additions & 1 deletion
This file was deleted.

stdlib/StyledStrings.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
STYLEDSTRINGS_BRANCH = main
2-
STYLEDSTRINGS_SHA1 = 8985a37ac054c37d084a03ad2837208244824877
2+
STYLEDSTRINGS_SHA1 = 3fe829fcf611b5fefaefb64df7e61f2ae82db117
33
STYLEDSTRINGS_GIT_URL := https://github.com/JuliaLang/StyledStrings.jl.git
44
STYLEDSTRINGS_TAR_URL = https://api.github.com/repos/JuliaLang/StyledStrings.jl/tarball/$1

0 commit comments

Comments
 (0)