Skip to content

Commit 11e9dc4

Browse files
committed
add AnnotatedChar support
1 parent c0c33ff commit 11e9dc4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/functionlenses.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,15 @@ end
207207

208208
if isdefined(Base, :AnnotatedString)
209209
# 1.11+
210-
using Base: AnnotatedString, annotations
210+
using Base: AnnotatedString, AnnotatedChar, annotations
211+
211212
set(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)
212213
set(s::AnnotatedString, ::typeof(annotations), anns) = AnnotatedString(s.string, anns)
213214
delete(s::AnnotatedString, ::typeof(annotations)) = s.string
214215
insert(s::AbstractString, ::typeof(annotations), anns) = AnnotatedString(s, anns)
216+
217+
set(s::AbstractChar, ::typeof(annotations), anns) = AnnotatedChar(s, anns)
218+
set(s::AnnotatedChar, ::typeof(annotations), anns) = AnnotatedChar(s.char, anns)
219+
delete(s::AnnotatedChar, ::typeof(annotations)) = s.char
220+
insert(s::AbstractChar, ::typeof(annotations), anns) = AnnotatedChar(s, anns)
215221
end

test/test_functionlenses.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,22 +399,39 @@ end
399399
VERSION v"1.11" && @testset "AnnotatedStrings" begin
400400
using Base: AnnotatedChar, AnnotatedString, annotations
401401

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

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

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

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

414414
test_getset_laws((@o annotations(_)[2].region), s, 5:5, 1:3)
415415
test_getset_laws((@o annotations(_)[2].label), s, :abc, :def)
416416
test_getset_laws((@o annotations(_)[2].value), s, "sad", +2)
417417
test_insertdelete_laws((@o annotations(_)[2]), s, (region=2:2, label=:mylabel, value=+1))
418+
419+
420+
c = AnnotatedChar('x', [(label=:level, value="warning"), (label=:alphabet, value=1)])
421+
422+
@test (@delete annotations(c))::Char == 'x'
423+
424+
cnew = @delete annotations(c)[2]
425+
@test Char(cnew) == 'x'
426+
@test annotations(cnew) == [(label=:level, value="warning")]
427+
428+
cnew = (@set annotations(c)[1].label = :severity)
429+
@test Char(cnew) == 'x'
430+
@test annotations(cnew) == [(label=:severity, value="warning"), (label=:alphabet, value=1)]
431+
432+
test_getset_laws((@o annotations(_)[2].label), c, :abc, :def)
433+
test_getset_laws((@o annotations(_)[1].value), c, 5, "bad")
434+
test_insertdelete_laws((@o annotations(_)[2]), c, (label=:mylabel, value=+1))
418435
end
419436

420437
@testset "custom binary function" begin

0 commit comments

Comments
 (0)