33struct RegionIterator{S <: AbstractString }
44 str:: S
55 regions:: Vector{UnitRange{Int}}
6- annotations:: Vector{Vector{Pair{ Symbol, Any}}}
6+ annotations:: Vector{Vector{@NamedTuple{label:: Symbol, value:: Any}}}
77end
88
99Base. length (si:: RegionIterator ) = length (si. regions)
@@ -15,35 +15,36 @@ Base.@propagate_inbounds function Base.iterate(si::RegionIterator, i::Integer=1)
1515end
1616
1717Base. eltype (:: RegionIterator{S} ) where { S <: AbstractString } =
18- Tuple{SubString{S}, Vector{Pair{ Symbol, Any}}}
18+ Tuple{SubString{S}, Vector{@NamedTuple {label :: Symbol , value :: Any }}}
1919
2020"""
2121 eachregion(s::AnnotatedString{S})
2222 eachregion(s::SubString{AnnotatedString{S}})
2323
2424Identify the contiguous substrings of `s` with a constant annotations, and return
2525an iterator which provides each substring and the applicable annotations as a
26- `Tuple{SubString{S}, Vector{Pair{ Symbol, Any}}}`.
26+ `Tuple{SubString{S}, Vector{@NamedTuple{label:: Symbol, value:: Any}}}`.
2727
2828# Examples
2929
3030```jldoctest
3131julia> collect(StyledStrings.eachregion(AnnotatedString(
32- "hey there", [(1:3, :face => :bold), (5:9, :face => :italic)])))
33- 3-element Vector{Tuple{SubString{String}, Vector{Pair{ Symbol, Any }}}}:
34- ("hey", [: face => :bold])
32+ "hey there", [(1:3, :face, :bold), (5:9, :face, :italic)])))
33+ 3-element Vector{Tuple{SubString{String}, Vector{@NamedTuple{label:: Symbol, value }}}}:
34+ ("hey", [@NamedTuple{label::Symbol, value}((: face, :bold)) ])
3535 (" ", [])
36- ("there", [: face => :italic])
36+ ("there", [@NamedTuple{label::Symbol, value}((: face, :italic)) ])
3737```
3838"""
3939function eachregion (s:: AnnotatedString , subregion:: UnitRange{Int} = firstindex (s): lastindex (s))
4040 isempty (s) || isempty (subregion) &&
41- return RegionIterator (s. string, UnitRange{Int}[], Vector{Pair{ Symbol, Any}}[])
41+ return RegionIterator (s. string, UnitRange{Int}[], Vector{@NamedTuple {label :: Symbol , value :: Any }}[])
4242 events = annotation_events (s, subregion)
43- isempty (events) && return RegionIterator (s. string, [subregion], [Pair{Symbol, Any}[]])
44- annotvals = last .(annotations (s))
43+ isempty (events) && return RegionIterator (s. string, [subregion], [@NamedTuple {label:: Symbol , value:: Any }[]])
44+ annotvals = @NamedTuple {label:: Symbol , value:: Any }[
45+ (; label, value) for (; label, value) in annotations (s)]
4546 regions = Vector {UnitRange{Int}} ()
46- annots = Vector {Vector{Pair{ Symbol, Any}}} ()
47+ annots = Vector {Vector{@NamedTuple{label:: Symbol, value:: Any}}} ()
4748 pos = first (events). pos
4849 if pos > first (subregion)
4950 push! (regions, thisind (s, first (subregion)): prevind (s, pos))
7172
7273function eachregion (s:: SubString{<:AnnotatedString} , pos:: UnitRange{Int} = firstindex (s): lastindex (s))
7374 if isempty (s)
74- RegionIterator (s. string, Vector {UnitRange{Int}} (), Vector {Vector{Pair{ Symbol, Any}}} ())
75+ RegionIterator (s. string, Vector {UnitRange{Int}} (), Vector {Vector{@NamedTuple{label:: Symbol, value:: Any}}} ())
7576 else
7677 eachregion (s. string, first (pos)+ s. offset: last (pos)+ s. offset)
7778 end
7879end
7980
8081"""
81- annotation_events(string::AbstractString, annots::Vector{Tuple{ UnitRange{Int}, Pair{ Symbol, Any} }}, subregion::UnitRange{Int})
82+ annotation_events(string::AbstractString, annots::Vector{@NamedTuple{region:: UnitRange{Int}, label:: Symbol, value:: Any}}, subregion::UnitRange{Int})
8283 annotation_events(string::AnnotatedString, subregion::UnitRange{Int})
8384
8485Find all annotation "change events" that occur within a `subregion` of `annots`,
@@ -89,9 +90,9 @@ index::Int}` where `pos` is the position of the event, `active` is a boolean
8990indicating whether the annotation is being activated or deactivated, and `index`
9091is the index of the annotation in question.
9192"""
92- function annotation_events (s:: AbstractString , annots:: Vector{Tuple{ UnitRange{Int}, Pair{ Symbol, Any} }} , subregion:: UnitRange{Int} )
93+ function annotation_events (s:: AbstractString , annots:: Vector{@NamedTuple{region:: UnitRange{Int}, label:: Symbol, value:: Any}} , subregion:: UnitRange{Int} )
9394 events = Vector {NamedTuple{(:pos, :active, :index), Tuple{Int, Bool, Int}}} () # Position, Active?, Annotation index
94- for (i, (region, _ )) in enumerate (annots)
95+ for (i, (; region )) in enumerate (annots)
9596 if ! isempty (intersect (subregion, region))
9697 start, stop = max (first (subregion), first (region)), min (last (subregion), last (region))
9798 start <= stop || continue # Currently can't handle empty regions
0 commit comments