@@ -153,7 +153,7 @@ lastindex(s::AnnotatedString) = lastindex(s.string)
153153function getindex (s:: AnnotatedString , i:: Integer )
154154 @boundscheck checkbounds (s, i)
155155 @inbounds if isvalid (s, i)
156- AnnotatedChar (s. string[i], annotations (s, i))
156+ AnnotatedChar (s. string[i], map (last, annotations (s, i) ))
157157 else
158158 string_index_err (s, i)
159159 end
@@ -356,37 +356,44 @@ annotate!(c::AnnotatedChar, @nospecialize(labelval::Pair{Symbol, <:Any})) =
356356 (push! (c. annotations, labelval); c)
357357
358358"""
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}}}
361362
362363Get all annotations that apply to `str`. Should `position` be provided, only
363364annotations that overlap with `position` will be returned.
364365
366+ Annotations are provided together with the regions they apply to, in the form of
367+ a vector of region–annotation tuples.
368+
365369See also: `annotate!`.
366370"""
367371annotations (s:: AnnotatedString ) = s. annotations
368372
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
371377
372378function annotations (s:: AnnotatedString , pos:: UnitRange{<:Integer} )
373379 # 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))]
377383end
378384
379385annotations (s:: AnnotatedString , pos:: Integer ) = annotations (s, pos: pos)
380386
381387annotations (s:: SubString{<:AnnotatedString} , pos:: Integer ) =
382388 annotations (s. string, s. offset + pos)
389+
383390annotations (s:: SubString{<:AnnotatedString} , pos:: UnitRange{<:Integer} ) =
384391 annotations (s. string, first (pos)+ s. offset: last (pos)+ s. offset)
385392
386393"""
387- annotations(chr::AnnotatedChar)
394+ annotations(chr::AnnotatedChar) -> Vector{Pair{Symbol, Any}}
388395
389- Get all annotations of `chr`.
396+ Get all annotations of `chr`, in the form of a vector of annotation pairs .
390397"""
391398annotations (c:: AnnotatedChar ) = c. annotations
392399
0 commit comments