Skip to content

Commit 3f2a7c3

Browse files
topolaritytecosaur
authored andcommitted
Resolve Face eagerly when constructing StyledString
This is step 1 to eliminating our type-piracy problems StyledStrings needs to put some type that it owns into its AnnotatedStrings so that we have a right to hook into the display logic (and the display logic can know which copy of StyledStrings to delegate to). It is also a semantic change to how constructing StyledStrings behaves, but overall I think it's a lot more intuitive to have a StyledString actually compute its style information at construction Resolves #87
1 parent 729f56c commit 3f2a7c3

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/styledmarkup.jl

+20-17
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Of course, as usual, the devil is in the details.
4646
module StyledMarkup
4747

4848
using Base: AnnotatedString, annotations, annotatedstring
49-
using ..StyledStrings: Face, SimpleColor
49+
using ..StyledStrings: Face, SimpleColor, getface
5050

5151
export @styled_str, styled
5252

@@ -325,7 +325,7 @@ function readexpr!(state::State, pos::Int = first(popfirst!(state.s)) + 1)
325325
if isempty(state.s)
326326
styerr!(state,
327327
AnnotatedString("Identifier or parenthesised expression expected after \$ in string",
328-
[(55:55, :face, :warning)]),
328+
[(55:55, :face, getface(:warning))]),
329329
-1, "right here")
330330
return "", pos
331331
end
@@ -401,7 +401,7 @@ and register it in the active styles list.
401401
"""
402402
function begin_style!(state::State, i::Int, char::Char)
403403
hasvalue = false
404-
newstyles = Vector{Tuple{Int, Int, Union{Symbol, Expr, Tuple{Symbol, Any}}}}()
404+
newstyles = Vector{Tuple{Int, Int, Union{Expr, Tuple{Symbol, Any}}}}()
405405
while read_annotation!(state, i, char, newstyles) end
406406
push!(state.active_styles, reverse!(newstyles))
407407
# Adjust bytes/offset based on how much the index
@@ -535,9 +535,9 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
535535
valid_options = join(VALID_UNDERLINE_STYLES, ", ", ", or ")
536536
styerr!(state,
537537
AnnotatedString("Invalid underline style '$ustyle_word' (should be $valid_options)",
538-
[(26:25+ncodeunits(ustyle_word), :face, :warning)
538+
[(26:25+ncodeunits(ustyle_word), :face, getface(:warning))
539539
(28+ncodeunits(ustyle_word):39+ncodeunits(ustyle_word)+ncodeunits(valid_options),
540-
:face, :light)]),
540+
:face, getface(:light))]),
541541
-length(ustyle_word) - 3)
542542
end
543543
ustyle = Symbol(ustyle_word)
@@ -658,24 +658,24 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
658658
else
659659
invalid, lastchar = readsymbol!(state, lastchar)
660660
styerr!(state, AnnotatedString("Invalid height '$invalid', should be a natural number or positive float",
661-
[(17:16+ncodeunits(string(invalid)), :face, :warning)]),
661+
[(17:16+ncodeunits(string(invalid)), :face, getface(:warning))]),
662662
-3)
663663
end
664664
elseif key (:weight, :slant)
665665
v, lastchar = readalph!(state, lastchar)
666666
if key == :weight && v VALID_WEIGHTS
667667
valid_options = join(VALID_WEIGHTS, ", ", ", or ")
668668
styerr!(state, AnnotatedString("Invalid weight '$v' (should be $valid_options)",
669-
[(17:16+ncodeunits(v), :face, :warning),
669+
[(17:16+ncodeunits(v), :face, getface(:warning)),
670670
(19+ncodeunits(v):30+ncodeunits(v)+ncodeunits(valid_options),
671-
:face, :light)]),
671+
:face, getface(:light))]),
672672
-3)
673673
elseif key == :slant && v VALID_SLANTS
674674
valid_options = join(VALID_SLANTS, ", ", ", or ")
675675
styerr!(state, AnnotatedString("Invalid slant '$v' (should be $valid_options)",
676-
[(16:15+ncodeunits(v), :face, :warning),
676+
[(16:15+ncodeunits(v), :face, getface(:warning)),
677677
(18+ncodeunits(v):29+ncodeunits(v)+ncodeunits(valid_options),
678-
:face, :light)]),
678+
:face, getface(:light))]),
679679
-3)
680680
end
681681
Symbol(v) |> if ismacro(state) QuoteNode else identity end
@@ -702,7 +702,7 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
702702
else
703703
styerr!(state, AnnotatedString(
704704
"Uses unrecognised face key '$key'. Recognised keys are: $(join(VALID_FACE_ATTRS, ", ", ", and "))",
705-
[(29:28+ncodeunits(String(key)), :face, :warning)]),
705+
[(29:28+ncodeunits(String(key)), :face, getface(:warning))]),
706706
-length(str_key) - 2)
707707
end
708708
if ismacro(state) && !any(k -> first(k.args) == key, kwargs)
@@ -711,7 +711,7 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
711711
push!(kwargs, key => val)
712712
else
713713
styerr!(state, AnnotatedString("Contains repeated face key '$key'",
714-
[(29:28+ncodeunits(String(key)), :face, :warning)]),
714+
[(29:28+ncodeunits(String(key)), :face, getface(:warning))]),
715715
-length(str_key) - 2)
716716
end
717717
isempty(state.s) && styerr!(state, "Incomplete inline face declaration", -1)
@@ -727,11 +727,11 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
727727
push!(newstyles,
728728
(i, i + state.offset + 1,
729729
if !ismacro(state)
730-
:face, Face(; NamedTuple(kwargs)...)
730+
(:face, Face(; NamedTuple(kwargs)...))
731731
elseif needseval
732732
:((:face, $face))
733733
else
734-
:face, hygienic_eval(state, face)
734+
(:face, hygienic_eval(state, face))
735735
end))
736736
end
737737

@@ -803,6 +803,7 @@ function read_face_or_keyval!(state::State, i::Int, char::Char, newstyles)
803803
push!(newstyles,
804804
(i, i + state.offset + ncodeunits('{'),
805805
if key isa String && !(value isa Symbol || value isa Expr)
806+
# TODO
806807
Symbol(key), value
807808
elseif key isa Expr || key isa Symbol
808809
:(($key, $value))
@@ -813,9 +814,11 @@ function read_face_or_keyval!(state::State, i::Int, char::Char, newstyles)
813814
push!(newstyles,
814815
(i, i + state.offset + ncodeunits('{'),
815816
if key isa Symbol || key isa Expr
816-
:((:face, $key))
817+
:((:face, $getface($key)))
818+
elseif ismacro(state) # Face symbol
819+
:((:face, $getface($(QuoteNode(Symbol(key))))))
817820
else # Face symbol
818-
:face, Symbol(key)
821+
(:face, getface(Symbol(key)))
819822
end))
820823
end
821824
if isempty(state.s) || last(peek(state.s)) (' ', '\t', '\n', '\r', ',', ':')
@@ -857,7 +860,7 @@ function run_state_machine!(state::State)
857860
end
858861
for incomplete in Iterators.flatten(state.active_styles)
859862
styerr!(state, AnnotatedString("Unterminated annotation (missing closing '}')",
860-
[(43:43, :face, :warning)]),
863+
[(43:43, :face, getface(:warning))]),
861864
prevind(state.content, first(incomplete)), "starts here")
862865
end
863866
end

0 commit comments

Comments
 (0)