Skip to content

Commit 5cd1a1a

Browse files
committed
Vendor the old annotatedstring_optimize! from Base
It's about to be removed in an effort to make less assumptions about the structuring of annotations, but it still makes sense to apply it here, so we vendor the old version.
1 parent bfdb4c3 commit 5cd1a1a

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/styledmarkup.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,36 @@ function run_state_machine!(state::State)
641641
end
642642
end
643643

644+
"""
645+
annotatedstring_optimize!(str::AnnotatedString)
646+
647+
Merge contiguous identical annotations in `str`.
648+
"""
649+
function annotatedstring_optimize!(s::AnnotatedString)
650+
last_seen = Dict{Pair{Symbol, Any}, Int}()
651+
i = 1
652+
while i <= length(s.annotations)
653+
region, keyval = s.annotations[i]
654+
prev = get(last_seen, keyval, 0)
655+
if prev > 0
656+
lregion, _ = s.annotations[prev]
657+
if last(lregion) + 1 == first(region)
658+
s.annotations[prev] =
659+
setindex(s.annotations[prev],
660+
first(lregion):last(region),
661+
1)
662+
deleteat!(s.annotations, i)
663+
else
664+
delete!(last_seen, keyval)
665+
end
666+
else
667+
last_seen[keyval] = i
668+
i += 1
669+
end
670+
end
671+
s
672+
end
673+
644674
"""
645675
@styled_str -> AnnotatedString
646676
@@ -740,7 +770,7 @@ macro styled_str(raw_content::String)
740770
elseif state.interpolated[]
741771
:(annotatedstring($(state.parts...)))
742772
else
743-
annotatedstring(map(Base.Fix1(hygienic_eval, state), state.parts)...) |> Base.annotatedstring_optimize!
773+
annotatedstring(map(Base.Fix1(hygienic_eval, state), state.parts)...) |> annotatedstring_optimize!
744774
end
745775
end
746776

@@ -762,7 +792,7 @@ function styled(content::AbstractString)
762792
if !isempty(state.errors)
763793
throw(MalformedStylingMacro(state.content, state.errors))
764794
else
765-
annotatedstring(state.parts...) |> Base.annotatedstring_optimize!
795+
annotatedstring(state.parts...) |> annotatedstring_optimize!
766796
end
767797
end
768798

0 commit comments

Comments
 (0)