Skip to content

Add AnnotatedString display interface (eliminate StyledStrings type-piracy) #56194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

topolarity
Copy link
Member

@topolarity topolarity commented Oct 16, 2024

This implements a "rendering interface" for AnnotatedString, allowing StyledStrings to influence printing without type-piracy.

The idea here is to provide an iterate-like display protocol that passes style information into the implementing library, which takes that information (and a "state" from the last display update), and decides how it needs to update the output to render the string.

Since AnnotatedStrings don't have a distinguished "owner" or "type" (only their annotations have types), this means we have to handle cases where the "laststate" you receive might be from another library. In that case, it's the responsibility of the downstream renderer to discard the laststate information and "reset" the display state as needed.

Dependent on #56192, and see JuliaLang/StyledStrings.jl#100 for the StyledStrings-side changes.

This should eliminate 100% of the StyledStrings type-piracy, once implemented.

@topolarity topolarity requested a review from tecosaur October 16, 2024 14:05
@topolarity topolarity force-pushed the ct/styled-display-interface branch 2 times, most recently from d485bb7 to ccd6ea1 Compare October 16, 2024 14:25
@tecosaur tecosaur added the display and printing Aesthetics and correctness of printed representations of objects. label Oct 16, 2024
topolarity added a commit that referenced this pull request Mar 29, 2025
Excising part of #56194 on the
way to reviving that PR.
KristofferC pushed a commit that referenced this pull request Mar 31, 2025
Excising part of #56194 on the
way to reviving that PR.

(cherry picked from commit a5e0eab)
KristofferC pushed a commit that referenced this pull request Mar 31, 2025
Excising part of #56194 on the
way to reviving that PR.

(cherry picked from commit a5e0eab)
This is required for downstream libraries (esp. StyledStrings)
to be able to influence the display of an AnnotatedString without
type-piracy.

The idea here is to provide an `iterate`-like display protocol that
passes style information into the implementing library, which takes
that information (and a "state" from the last display update), and
decides how it needs to update the output to render the string.

Since AnnotatedStrings don't have a distinguished "owner" or "type"
(only their annotations have types), this leads to corner cases where
the "laststate" you receive might be from another library. In that
case, it's the responsibility of the downstream renderer to discard
the laststate information and "reset" the display state as needed.
end

function show(io::IO, ::MIME"text/html", s::Union{<:AnnotatedString, SubString{<:AnnotatedString}})
htmlescape(str) = replace(str, '&' => "&amp;", '<' => "&lt;", '>' => "&gt;")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topolarity added a commit that referenced this pull request Apr 22, 2025
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 <[email protected]>
KristofferC pushed a commit that referenced this pull request Apr 24, 2025
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 <[email protected]>
(cherry picked from commit 6d78a4a)
LebedevRI pushed a commit to LebedevRI/julia that referenced this pull request May 2, 2025
After discussion with Cody, this is an attempt at a more minimal version
of JuliaLang#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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants