Skip to content

Commit c0c33ff

Browse files
committed
AnnotatedString support
1 parent 171d06a commit c0c33ff

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/functionlenses.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,12 @@ function set(s::AbstractString, o::Base.Fix2{typeof(split), <:Union{AbstractChar
204204
any(c -> occursin(o.x, c), v) && throw(ArgumentError("split components cannot contain the delimiter $(repr(o.x))"))
205205
join(v, o.x)
206206
end
207+
208+
if isdefined(Base, :AnnotatedString)
209+
# 1.11+
210+
using Base: AnnotatedString, annotations
211+
set(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)
212+
set(s::AnnotatedString, ::typeof(annotations), anns) = AnnotatedString(s.string, anns)
213+
delete(s::AnnotatedString, ::typeof(annotations)) = s.string
214+
insert(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)
215+
end

test/test_functionlenses.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,27 @@ end
396396
@test_throws ArgumentError set(" abc def ", @optic(split(_, ' ')), [" ", "y"])
397397
end
398398

399+
VERSION v"1.11" && @testset "AnnotatedStrings" begin
400+
using Base: AnnotatedChar, AnnotatedString, annotations
401+
402+
s = Base.AnnotatedString("good bad", [(region=1:4, label=:sentiment, value=+1), (region=6:8, label=:sentiment, value=-1)])
403+
404+
@test (@delete annotations(s))::String == "good bad"
405+
406+
snew = @delete annotations(s)[2]
407+
@test String(snew) == "good bad"
408+
@test annotations(snew) == [(region=1:4, label=:sentiment, value=+1)]
409+
410+
snew = (@set Base.annotations(s)[1].region = 2:6)
411+
@test String(snew) == "good bad"
412+
@test annotations(snew) == [(region=2:6, label=:sentiment, value=+1), (region=6:8, label=:sentiment, value=-1)]
413+
414+
test_getset_laws((@o annotations(_)[2].region), s, 5:5, 1:3)
415+
test_getset_laws((@o annotations(_)[2].label), s, :abc, :def)
416+
test_getset_laws((@o annotations(_)[2].value), s, "sad", +2)
417+
test_insertdelete_laws((@o annotations(_)[2]), s, (region=2:2, label=:mylabel, value=+1))
418+
end
419+
399420
@testset "custom binary function" begin
400421
(x, y) = x - y
401422
Accessors.set(x, f::Base.Fix1{typeof(↑)}, y) = f.x - y

0 commit comments

Comments
 (0)