Skip to content

Commit dfef96d

Browse files
committed
Adjust prioritisation of composed styling in parse
It's more intuitive, and consistent, if inner-nested and smaller (more specific) annotations take priority.
1 parent 9a23e9e commit dfef96d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/styledmarkup.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ function addpart!(state::State, stop::Int)
8888
styles = sty_type[]
8989
relevant_styles = Iterators.filter(
9090
(_, start, _)::Tuple -> start <= stop + state.offset[] + 1,
91-
Iterators.flatten(state.active_styles))
91+
Iterators.flatten(map(reverse, state.active_styles)))
9292
for (_, start, annot) in relevant_styles
9393
range = (start - state.point[]):(stop - state.point[] + state.offset[] + 1)
9494
push!(styles, tupl(range, annot))
9595
end
96-
sort!(state.pending_styles, by = first)
96+
sort!(state.pending_styles, by = (r -> (first(r), -last(r))) first) # see `Base._annot_sortkey`
9797
for (range, annot) in state.pending_styles
9898
if !isempty(range)
9999
push!(styles, tupl(range .- state.point[], annot))
@@ -122,7 +122,10 @@ function addpart!(state::State, start::Int, expr, stop::Int)
122122
len = gensym("len")
123123
annots = Expr(:vect, [
124124
Expr(:tuple, Expr(:call, UnitRange, 1, len), annot)
125-
for annot in last.(Iterators.flatten(state.active_styles))]...)
125+
for annot in
126+
map(last,
127+
(Iterators.flatten(
128+
map(reverse, state.active_styles))))]...)
126129
if isempty(annots.args)
127130
push!(state.parts, :(AnnotatedString(string($expr))))
128131
else
@@ -195,7 +198,7 @@ function begin_style!(state::State, i::Int, char::Char)
195198
hasvalue = false
196199
newstyles = Vector{Tuple{Int, Int, Union{Symbol, Expr, Pair{Symbol, Any}}}}()
197200
while read_annotation!(state, i, char, newstyles) end
198-
push!(state.active_styles, newstyles)
201+
push!(state.active_styles, reverse!(newstyles))
199202
# Adjust bytes/offset based on how much the index
200203
# has been incremented in the processing of the
201204
# style declaration(s).
@@ -209,7 +212,7 @@ end
209212
function end_style!(state::State, i::Int, char::Char)
210213
# Close off most recent active style
211214
for (_, start, annot) in pop!(state.active_styles)
212-
push!(state.pending_styles, (start:i+state.offset[], annot))
215+
pushfirst!(state.pending_styles, (start:i+state.offset[], annot))
213216
end
214217
deleteat!(state.bytes, i + state.offset[])
215218
state.offset[] -= ncodeunits('}')

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,15 @@ end
268268
@test styled"some {a=1:s}trin{b=2:g}" == AnnotatedString("some string", [(6:6, :a => "1"), (11:11, :b => "2")])
269269
@test styled"{thing=val with spaces:some} string" == AnnotatedString("some string", [(1:4, :thing => "val with spaces")])
270270
@test styled"{aface:some} string" == AnnotatedString("some string", [(1:4, :face => :aface)])
271+
# Annotation prioritisation
271272
@test styled"{aface,bface:some} string" ==
272273
AnnotatedString("some string", [(1:4, :face => :aface), (1:4, :face => :bface)])
274+
@test styled"{aface:{bface:some}} string" ==
275+
AnnotatedString("some string", [(1:4, :face => :aface), (1:4, :face => :bface)])
276+
@test styled"{aface,bface:$(1)} string" ==
277+
AnnotatedString("1 string", [(1:1, :face => :aface), (1:1, :face => :bface)])
278+
@test styled"{aface:{bface:$(1)}} string" ==
279+
AnnotatedString("1 string", [(1:1, :face => :aface), (1:1, :face => :bface)])
273280
# Inline face attributes
274281
@test styled"{(slant=italic):some} string" ==
275282
AnnotatedString("some string", [(1:4, :face => Face(slant=:italic))])

0 commit comments

Comments
 (0)