Skip to content

Commit 237d2c7

Browse files
committed
Adjust to change of annotations type in Base
By popular demand, the type of an annotation is changing from a Tuple{UnitRange{Int}, Pair{Symbol, Any}} to a NamedTuple{(:region, :label, :value), Tuple{UnitRange{Int}, Symbol, Any}}. This necessitates some adjustments in the StyledStrings codebase.
1 parent da41b6a commit 237d2c7

File tree

6 files changed

+154
-149
lines changed

6 files changed

+154
-149
lines changed

docs/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ them to the properties list afterwards, or use the convenient [Styled String
153153
literals](@ref stdlib-styledstring-literals).
154154

155155
```@repl demo
156-
str1 = Base.AnnotatedString("blue text", [(1:9, :face => :blue)])
156+
str1 = Base.AnnotatedString("blue text", [(1:9, :face, :blue)])
157157
str2 = styled"{blue:blue text}"
158158
str1 == str2
159159
sprint(print, str1, context = :color => true)

src/faces.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function Base.show(io::IO, ::MIME"text/plain", color::SimpleColor)
187187
skiptype || show(io, SimpleColor)
188188
skiptype || print(io, '(')
189189
if get(io, :color, false)::Bool
190-
print(io, AnnotatedString("", [(1:1, :face => Face(foreground=color))]), ' ')
190+
print(io, AnnotatedString("", [(region=1:1, label=:face, value=Face(foreground=color))]), ' ')
191191
end
192192
if color.value isa RGBTuple
193193
(; r, g, b) = color.value
@@ -222,7 +222,7 @@ function Base.show(io::IO, ::MIME"text/plain", face::Face)
222222
show(io, Face)
223223
if get(io, :color, false)::Bool
224224
# Could do styled"({$face:sample})", but S_str isn't defined yet
225-
print(io, AnnotatedString("(sample)", [(2:7, :face => face)]))
225+
print(io, AnnotatedString("(sample)", [(region=2:7, label=:face, value=face)]))
226226
# elseif get(io, :limit, false)::Bool
227227
# print(io, "(…)")
228228
else
@@ -244,7 +244,7 @@ function Base.show(io::IO, ::MIME"text/plain", face::Face)
244244
end
245245
else
246246
show(io, Face)
247-
print(io, AnnotatedString(" (sample)", [(3:8, :face => face)]))
247+
print(io, AnnotatedString(" (sample)", [(region=3:8, label=:face, value=face)]))
248248
showcolor(io, color) = show(IOContext(io, :typeinfo => SimpleColor),
249249
MIME("text/plain"), color)
250250
setfields = Pair{Symbol, Any}[]
@@ -286,7 +286,7 @@ function Base.show(io::IO, ::MIME"text/plain", face::Face)
286286
isfirst = true
287287
for iface in face.inherit
288288
if isfirst; isfirst = false else print(io, ", ") end
289-
print(io, iface, '(', AnnotatedString("*", [(1:1, :face => iface)]), ')')
289+
print(io, iface, '(', AnnotatedString("*", [(region=1:1, label=:face, value=iface)]), ')')
290290
end
291291
end
292292
end
@@ -555,12 +555,12 @@ function getface(faces)
555555
end
556556

557557
"""
558-
getface(annotations::Vector{Pair{Symbol, Any}})
558+
getface(annotations::Vector{@NamedTuple{label::Symbol, value::Any}})
559559
560560
Combine all of the `:face` annotations with `getfaces`.
561561
"""
562-
function getface(annotations::Vector{Pair{Symbol, Any}})
563-
faces = (last(annot) for annot in annotations if first(annot) === :face)
562+
function getface(annotations::Vector{@NamedTuple{label::Symbol, value::Any}})
563+
faces = (ann.value for ann in annotations if ann.label === :face)
564564
getface(faces)
565565
end
566566

@@ -599,11 +599,11 @@ Apply `face` to `str`, along `range` if specified or the whole of `str`.
599599
"""
600600
face!(s::Union{<:AnnotatedString, <:SubString{<:AnnotatedString}},
601601
range::UnitRange{Int}, face::Union{Symbol, Face, <:Vector{<:Union{Symbol, Face}}}) =
602-
annotate!(s, range, :face => face)
602+
annotate!(s, range, :face, face)
603603

604604
face!(s::Union{<:AnnotatedString, <:SubString{<:AnnotatedString}},
605605
face::Union{Symbol, Face, <:Vector{<:Union{Symbol, Face}}}) =
606-
annotate!(s, firstindex(s):lastindex(s), :face => face)
606+
annotate!(s, firstindex(s):lastindex(s), :face, face)
607607

608608
## Reading face definitions from a dictionary ##
609609

src/io.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ function Base.show(io::IO, c::AnnotatedChar)
287287
if get(io, :color, false) == true
288288
out = IOBuffer()
289289
show(out, c.char)
290-
print(io, ''', AnnotatedString(String(take!(out)[2:end-1]), map(a -> (1:ncodeunits(c), a), c.annotations)), ''')
290+
cstr = AnnotatedString(
291+
String(take!(out)[2:end-1]),
292+
[(1:ncodeunits(c), a...) for a in c.annotations])
293+
print(io, ''', cstr, ''')
291294
else
292295
show(io, c.char)
293296
end

src/regioniterator.jl

+16-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
struct 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}}}
77
end
88

99
Base.length(si::RegionIterator) = length(si.regions)
@@ -15,35 +15,36 @@ Base.@propagate_inbounds function Base.iterate(si::RegionIterator, i::Integer=1)
1515
end
1616

1717
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}}}
1919

2020
"""
2121
eachregion(s::AnnotatedString{S})
2222
eachregion(s::SubString{AnnotatedString{S}})
2323
2424
Identify the contiguous substrings of `s` with a constant annotations, and return
2525
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}}}`.
2727
2828
# Examples
2929
3030
```jldoctest
3131
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))])
3535
(" ", [])
36-
("there", [:face => :italic])
36+
("there", [@NamedTuple{label::Symbol, value}((:face, :italic))])
3737
```
3838
"""
3939
function 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))
@@ -71,14 +72,14 @@ end
7172

7273
function 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
7879
end
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
8485
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
8990
indicating whether the annotation is being activated or deactivated, and `index`
9091
is 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

Comments
 (0)