@@ -153,7 +153,7 @@ lastindex(s::AnnotatedString) = lastindex(s.string)
153
153
function getindex (s:: AnnotatedString , i:: Integer )
154
154
@boundscheck checkbounds (s, i)
155
155
@inbounds if isvalid (s, i)
156
- AnnotatedChar (s. string[i], annotations (s, i))
156
+ AnnotatedChar (s. string[i], map (last, annotations (s, i) ))
157
157
else
158
158
string_index_err (s, i)
159
159
end
@@ -356,37 +356,44 @@ annotate!(c::AnnotatedChar, @nospecialize(labelval::Pair{Symbol, <:Any})) =
356
356
(push! (c. annotations, labelval); c)
357
357
358
358
"""
359
- annotations(str::AnnotatedString, [position::Union{Integer, UnitRange}])
360
- annotations(str::SubString{AnnotatedString}, [position::Union{Integer, UnitRange}])
359
+ annotations(str::Union{AnnotatedString, SubString{AnnotatedString}},
360
+ [position::Union{Integer, UnitRange}]) ->
361
+ Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
361
362
362
363
Get all annotations that apply to `str`. Should `position` be provided, only
363
364
annotations that overlap with `position` will be returned.
364
365
366
+ Annotations are provided together with the regions they apply to, in the form of
367
+ a vector of region–annotation tuples.
368
+
365
369
See also: `annotate!`.
366
370
"""
367
371
annotations (s:: AnnotatedString ) = s. annotations
368
372
369
- annotations (s:: SubString{<:AnnotatedString} ) =
370
- annotations (s, s. offset+ 1 : s. offset+ s. ncodeunits)
373
+ function annotations (s:: SubString{<:AnnotatedString} )
374
+ map (((region, annot),) -> (first (region)- s. offset: last (region)- s. offset, annot),
375
+ annotations (s. string, s. offset+ 1 : s. offset+ s. ncodeunits))
376
+ end
371
377
372
378
function annotations (s:: AnnotatedString , pos:: UnitRange{<:Integer} )
373
379
# TODO optimise
374
- annots = filter (label -> ! isempty ( intersect (pos, first (label))),
375
- s . annotations )
376
- last .(annots)
380
+ Tuple{UnitRange{Int64}, Pair{Symbol, Any}}[
381
+ ( max ( first (pos), first (region)) : min ( last (pos), last (region)), annot )
382
+ for (region, annot) in s . annotations if ! isempty ( intersect (pos, region))]
377
383
end
378
384
379
385
annotations (s:: AnnotatedString , pos:: Integer ) = annotations (s, pos: pos)
380
386
381
387
annotations (s:: SubString{<:AnnotatedString} , pos:: Integer ) =
382
388
annotations (s. string, s. offset + pos)
389
+
383
390
annotations (s:: SubString{<:AnnotatedString} , pos:: UnitRange{<:Integer} ) =
384
391
annotations (s. string, first (pos)+ s. offset: last (pos)+ s. offset)
385
392
386
393
"""
387
- annotations(chr::AnnotatedChar)
394
+ annotations(chr::AnnotatedChar) -> Vector{Pair{Symbol, Any}}
388
395
389
- Get all annotations of `chr`.
396
+ Get all annotations of `chr`, in the form of a vector of annotation pairs .
390
397
"""
391
398
annotations (c:: AnnotatedChar ) = c. annotations
392
399
0 commit comments