Skip to content

Commit

Permalink
add AnnotatedChar support
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Dec 5, 2024
1 parent c0c33ff commit 11e9dc4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,15 @@ end

if isdefined(Base, :AnnotatedString)
# 1.11+
using Base: AnnotatedString, annotations
using Base: AnnotatedString, AnnotatedChar, annotations

set(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)
set(s::AnnotatedString, ::typeof(annotations), anns) = AnnotatedString(s.string, anns)
delete(s::AnnotatedString, ::typeof(annotations)) = s.string
insert(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)

set(s::AbstractChar, ::typeof(annotations), anns) = AnnotatedChar(s, anns)
set(s::AnnotatedChar, ::typeof(annotations), anns) = AnnotatedChar(s.char, anns)
delete(s::AnnotatedChar, ::typeof(annotations)) = s.char
insert(s::AbstractChar, ::typeof(annotations), anns) = AnnotatedChar(s, anns)
end
21 changes: 19 additions & 2 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,39 @@ end
VERSION v"1.11" && @testset "AnnotatedStrings" begin
using Base: AnnotatedChar, AnnotatedString, annotations

s = Base.AnnotatedString("good bad", [(region=1:4, label=:sentiment, value=+1), (region=6:8, label=:sentiment, value=-1)])
s = AnnotatedString("good bad", [(region=1:4, label=:sentiment, value=+1), (region=6:8, label=:sentiment, value=-1)])

@test (@delete annotations(s))::String == "good bad"

snew = @delete annotations(s)[2]
@test String(snew) == "good bad"
@test annotations(snew) == [(region=1:4, label=:sentiment, value=+1)]

snew = (@set Base.annotations(s)[1].region = 2:6)
snew = (@set annotations(s)[1].region = 2:6)
@test String(snew) == "good bad"
@test annotations(snew) == [(region=2:6, label=:sentiment, value=+1), (region=6:8, label=:sentiment, value=-1)]

test_getset_laws((@o annotations(_)[2].region), s, 5:5, 1:3)
test_getset_laws((@o annotations(_)[2].label), s, :abc, :def)
test_getset_laws((@o annotations(_)[2].value), s, "sad", +2)
test_insertdelete_laws((@o annotations(_)[2]), s, (region=2:2, label=:mylabel, value=+1))


c = AnnotatedChar('x', [(label=:level, value="warning"), (label=:alphabet, value=1)])

@test (@delete annotations(c))::Char == 'x'

cnew = @delete annotations(c)[2]
@test Char(cnew) == 'x'
@test annotations(cnew) == [(label=:level, value="warning")]

cnew = (@set annotations(c)[1].label = :severity)
@test Char(cnew) == 'x'
@test annotations(cnew) == [(label=:severity, value="warning"), (label=:alphabet, value=1)]

test_getset_laws((@o annotations(_)[2].label), c, :abc, :def)
test_getset_laws((@o annotations(_)[1].value), c, 5, "bad")
test_insertdelete_laws((@o annotations(_)[2]), c, (label=:mylabel, value=+1))
end

@testset "custom binary function" begin
Expand Down

2 comments on commit 11e9dc4

@aplavin
Copy link
Member Author

@aplavin aplavin commented on 11e9dc4 Dec 5, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/120742

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.39 -m "<description of version>" 11e9dc447b7644495d532e59537f30e36b2a0b56
git push origin v0.1.39

Please sign in to comment.