3
3
struct RegionIterator{S <: AbstractString }
4
4
str:: S
5
5
regions:: Vector{UnitRange{Int}}
6
- annotations:: Vector{Vector{Pair{ Symbol, Any}}}
6
+ annotations:: Vector{Vector{@NamedTuple{label:: Symbol, value:: Any}}}
7
7
end
8
8
9
9
Base. length (si:: RegionIterator ) = length (si. regions)
@@ -15,35 +15,36 @@ Base.@propagate_inbounds function Base.iterate(si::RegionIterator, i::Integer=1)
15
15
end
16
16
17
17
Base. eltype (:: RegionIterator{S} ) where { S <: AbstractString } =
18
- Tuple{SubString{S}, Vector{Pair{ Symbol, Any}}}
18
+ Tuple{SubString{S}, Vector{@NamedTuple {label :: Symbol , value :: Any }}}
19
19
20
20
"""
21
21
eachregion(s::AnnotatedString{S})
22
22
eachregion(s::SubString{AnnotatedString{S}})
23
23
24
24
Identify the contiguous substrings of `s` with a constant annotations, and return
25
25
an 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}}}`.
27
27
28
28
# Examples
29
29
30
30
```jldoctest
31
31
julia> collect(StyledStrings.eachregion(Base.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)) ])
35
35
(" ", [])
36
- ("there", [: face => :italic])
36
+ ("there", [@NamedTuple{label::Symbol, value}((: face, :italic)) ])
37
37
```
38
38
"""
39
39
function eachregion (s:: AnnotatedString , subregion:: UnitRange{Int} = firstindex (s): lastindex (s))
40
40
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 }}[])
42
42
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)]
45
46
regions = Vector {UnitRange{Int}} ()
46
- annots = Vector {Vector{Pair{ Symbol, Any}}} ()
47
+ annots = Vector {Vector{@NamedTuple{label:: Symbol, value:: Any}}} ()
47
48
pos = first (events). pos
48
49
if pos > first (subregion)
49
50
push! (regions, thisind (s, first (subregion)): prevind (s, pos))
71
72
72
73
function eachregion (s:: SubString{<:AnnotatedString} , pos:: UnitRange{Int} = firstindex (s): lastindex (s))
73
74
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}}} ())
75
76
else
76
77
eachregion (s. string, first (pos)+ s. offset: last (pos)+ s. offset)
77
78
end
78
79
end
79
80
80
81
"""
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})
82
83
annotation_events(string::AnnotatedString, subregion::UnitRange{Int})
83
84
84
85
Find 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
89
90
indicating whether the annotation is being activated or deactivated, and `index`
90
91
is the index of the annotation in question.
91
92
"""
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} )
93
94
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)
95
96
if ! isempty (intersect (subregion, region))
96
97
start, stop = max (first (subregion), first (region)), min (last (subregion), last (region))
97
98
start <= stop || continue # Currently can't handle empty regions
0 commit comments