Skip to content

Commit 527a985

Browse files
committed
Adjust parsing of incomplete active styles
Instead of applying active styles strictly after pending styles, which is in danger of overriding more specific pending regions, convert active regions into appropriate pending regions before specificity-sorting.
1 parent 43e72f8 commit 527a985

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/styledmarkup.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,22 @@ function addpart!(state::State, stop::Int)
207207
str
208208
else
209209
styles = sty_type[]
210+
# Turn active styles into pending styles
211+
relevant_styles = Iterators.filter(
212+
(_, start, _)::Tuple -> start <= stop + state.offset + 1,
213+
Iterators.flatten(state.active_styles))
214+
for (_, start, annot) in relevant_styles
215+
pushfirst!(state.pending_styles, (start:stop+state.offset+1, annot))
216+
end
217+
# Order the pending styles by specificity
210218
sort!(state.pending_styles, by = (r -> (first(r), -last(r))) first) # Prioritise the most specific styles
211219
for (range, annot) in state.pending_styles
212220
if !isempty(range)
213-
push!(styles, tupl(range .- state.point, annot))
221+
adjrange = (first(range) - state.point):(last(range) - state.point)
222+
push!(styles, tupl(adjrange, annot))
214223
end
215224
end
216225
empty!(state.pending_styles)
217-
relevant_styles = Iterators.filter(
218-
(_, start, _)::Tuple -> start <= stop + state.offset + 1,
219-
Iterators.flatten(map(reverse, state.active_styles)))
220-
for (_, start, annot) in relevant_styles
221-
range = (start - state.point):(stop - state.point + state.offset + 1)
222-
push!(styles, tupl(range, annot))
223-
end
224226
if isempty(styles)
225227
str
226228
elseif !ismacro(state)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ end
386386
"val", [$merge((; region=$(1:3)), $NamedTupleLV((:face, $(Face)(foreground = color))))]))) ==
387387
@macroexpand styled"{(foreground=$color):val}"
388388
end
389+
# Partial annotation termination with interpolation
390+
@test styled"{green:a}{red:{blue:b}$('c')}" ==
391+
AnnotatedString{String}("abc", [(1:1, :face, :green),
392+
(2:3, :face, :red),
393+
(2:2, :face, :blue)])
389394

390395
# Trailing (and non-trailing) Backslashes
391396
@test String(styled"\\") == "\\"

0 commit comments

Comments
 (0)