Skip to content

Commit 85dd300

Browse files
committed
Apply annotatedstring_optimize! to interpolations
Might as well, my gut says it's often worth it.
1 parent 2744928 commit 85dd300

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/styledmarkup.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ macro styled_str(raw_content::String)
958958
if !isempty(state.errors)
959959
throw(MalformedStylingMacro(state.content, state.errors))
960960
elseif state.interpolated[]
961-
:(annotatedstring($(state.parts...)))
961+
:(annotatedstring($(state.parts...)) |> annotatedstring_optimize!)
962962
else
963963
annotatedstring(map(Base.Fix1(hygienic_eval, state), state.parts)...) |> annotatedstring_optimize!
964964
end

test/runtests.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,28 +342,34 @@ end
342342
# Interpolation
343343
let annotatedstring = GlobalRef(StyledMarkup, :annotatedstring)
344344
AnnotatedString = GlobalRef(StyledMarkup, :AnnotatedString)
345+
annotatedstring_optimize! = GlobalRef(StyledMarkup, :annotatedstring_optimize!)
346+
chain = GlobalRef(StyledMarkup, :|>)
345347
Pair = GlobalRef(StyledMarkup, :Pair)
346348
Symbol = GlobalRef(StyledMarkup, :Symbol)
347349
Any = GlobalRef(StyledMarkup, :Any)
348-
@test :($annotatedstring(val)) == @macroexpand styled"$val"
349-
@test :($annotatedstring("a", val)) == @macroexpand styled"a$val"
350-
@test :($annotatedstring("a", val, "b")) == @macroexpand styled"a$(val)b"
350+
@test :($chain($annotatedstring(val), $annotatedstring_optimize!)) == @macroexpand styled"$val"
351+
@test :($chain($annotatedstring("a", val), $annotatedstring_optimize!)) == @macroexpand styled"a$val"
352+
@test :($chain($annotatedstring("a", val, "b"), $annotatedstring_optimize!)) == @macroexpand styled"a$(val)b"
351353
# @test :($annotatedstring(StyledStrings.AnnotatedString(string(val), $(Pair{Symbol, Any}(:face, :style))))) ==
352354
# @macroexpand styled"{style:$val}"
353-
@test :($annotatedstring($AnnotatedString(
354-
"val", [($(1:3), $Pair{$Symbol, $Any}(:face, face))]))) ==
355+
@test :($chain($annotatedstring($AnnotatedString(
356+
"val", [($(1:3), $Pair{$Symbol, $Any}(:face, face))])),
357+
$annotatedstring_optimize!)) ==
355358
@macroexpand styled"{$face:val}"
356-
@test :($annotatedstring($AnnotatedString(
357-
"val", [($(1:3), $Pair{$Symbol, $Any}(key, "val"))]))) ==
359+
@test :($chain($annotatedstring($AnnotatedString(
360+
"val", [($(1:3), $Pair{$Symbol, $Any}(key, "val"))])),
361+
$annotatedstring_optimize!)) ==
358362
@macroexpand styled"{$key=val:val}"
359-
@test :($annotatedstring($AnnotatedString(
360-
"val", [($(1:3), $Pair{$Symbol, $Any}(key, val))]))) ==
363+
@test :($chain($annotatedstring($AnnotatedString(
364+
"val", [($(1:3), $Pair{$Symbol, $Any}(key, val))])),
365+
$annotatedstring_optimize!)) ==
361366
@macroexpand styled"{$key=$val:val}"
362367
# @test :($annotatedstring($AnnotatedString(
363368
# string(val), $Pair{$Symbol, $Any}(key, val)))) ==
364369
# @macroexpand styled"{$key=$val:$val}"
365-
@test :($annotatedstring($AnnotatedString(
366-
"val", [($(1:3), $Pair{$Symbol, $Any}(:face, $(Face)(foreground = color)))]))) ==
370+
@test :($chain($annotatedstring($AnnotatedString(
371+
"val", [($(1:3), $Pair{$Symbol, $Any}(:face, $(Face)(foreground = color)))])),
372+
$annotatedstring_optimize!)) ==
367373
@macroexpand styled"{(foreground=$color):val}"
368374
end
369375

@@ -421,7 +427,6 @@ end
421427
showerror(aio, err)
422428
end
423429
errstr = read(seekstart(aio), AnnotatedString)
424-
sort!(annotations(errstr), by=first) # Remove when julialang/julia/#53800 is merged.
425430
@test errstr ==
426431
styled"MalformedStylingMacro\n\
427432
{error:│} Incomplete annotation declaration:\n\

0 commit comments

Comments
 (0)