Skip to content

Commit 19bd57b

Browse files
committed
Adjust to change of annotations type in Base
By popular demand, the type of an annotation is changing from a Tuple{UnitRange{Int}, Pair{Symbol, Any}} to a NamedTuple{(:region, :label, :value), Tuple{UnitRange{Int}, Symbol, Any}}. This necessitates some adjustments in the StyledStrings codebase.
1 parent b89dd99 commit 19bd57b

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

Diff for: src/JuliaSyntaxHighlighting.jl

+33-35
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,31 @@ struct HighlightContext{S <: AbstractString}
154154
end
155155

156156
"""
157-
_hl_annotations(content::AbstractString, ast::GreenNode) -> Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
157+
_hl_annotations(content::AbstractString, ast::GreenNode)
158+
-> Vector{@NamedTuple{region::UnitRange{Int}, label::Symbol, value::Any}}
158159
159-
Generate a list of `(range, annot)` pairs for the given `content` and `ast`.
160+
Generate a list of annotations for the given `content` and `ast`.
160161
161-
The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
162-
`annot` is a `Pair` of the form `:face => <face>`.
162+
Each annotation takes the form of a `@NamedTuple{region::UnitRange{Int}, label::Symbol, value::Any}`,
163+
where the region indexes into `content` and the value is a `julia_*` face name.
163164
164165
This is a small wrapper around [`_hl_annotations!`](@ref) for convenience.
165166
"""
166167
function _hl_annotations(content::AbstractString, ast::GreenNode; syntax_errors::Bool = false)
167-
highlights = Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}()
168+
highlights = Vector{@NamedTuple{region::UnitRange{Int}, label::Symbol, value::Any}}()
168169
ctx = HighlightContext(content, zero(UInt), ast, ParenDepthCounter())
169170
_hl_annotations!(highlights, GreenLineage(ast, nothing), ctx; syntax_errors)
170171
highlights
171172
end
172173

173174
"""
174-
_hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}},
175+
_hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int}, label::Symbol, value::Any}},
175176
lineage::GreenLineage, ctx::HighlightContext)
176177
177-
Populate `highlights` with `(range, annot)` pairs for the given `lineage` and `ctx`,
178+
Populate `highlights` with annotations for the given `lineage` and `ctx`,
178179
where `lineage` is expected to be consistent with `ctx.offset` and `ctx.lnode`.
179-
180-
The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
181-
`annot` is a `Pair` of the form `:face => <face>`.
182180
"""
183-
function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}},
181+
function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int}, label::Symbol, value::Any}},
184182
lineage::GreenLineage, ctx::HighlightContext; syntax_errors::Bool = false)
185183
(; node, parent) = lineage
186184
(; content, offset, lnode, pdepths) = ctx
@@ -240,7 +238,7 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
240238
elseif nkind == K"`" || nkind == K"```"; :julia_cmdstring
241239
elseif nkind == K"Char"
242240
kind(lnode) == K"'" && !isempty(highlights) &&
243-
(highlights[end] = (highlights[end][1], :face => :julia_char_delim))
241+
(highlights[end] = (highlights[end][1], :face, :julia_char_delim))
244242
:julia_char
245243
elseif nkind == K"'" && kind(lnode) == K"Char"; :julia_char_delim
246244
elseif nkind == K"true" || nkind == K"false"; :julia_bool
@@ -249,8 +247,8 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
249247
if nkind == K"="
250248
ifelse(ppkind == K"for", :julia_keyword, :julia_assignment)
251249
else # updating for <op>=
252-
push!(highlights, (firstindex(content)+offset:node.span+offset-1, :face => :julia_operator))
253-
push!(highlights, (node.span+offset:node.span+offset, :face => :julia_assignment))
250+
push!(highlights, (firstindex(content)+offset:node.span+offset-1, :face, :julia_operator))
251+
push!(highlights, (node.span+offset:node.span+offset, :face, :julia_assignment))
254252
nothing
255253
end
256254
elseif nkind == K";" && pkind == K"parameters" && pnode == lnode
@@ -323,19 +321,19 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
323321
end
324322
end
325323
!isnothing(face) &&
326-
push!(highlights, (region, :face => face))
324+
push!(highlights, (region, :face, face))
327325
if nkind == K"Comment"
328326
for match in eachmatch(
329327
r"(?:^|[(\[{[:space:]-])`([^[:space:]](?:.*?[^[:space:]])?)`(?:$|[!,\-.:;?\[\][:space:]])",
330328
regionstr)
331329
code = first(match.captures)
332330
push!(highlights, (firstindex(content)+offset+code.offset:firstindex(content)+offset+code.offset+code.ncodeunits-1,
333-
:face => :code))
331+
:face, :code))
334332
end
335333
elseif nkind == K"String"
336334
for match in eachmatch(r"\\.", regionstr)
337335
push!(highlights, (firstindex(content)+offset+match.offset-1:firstindex(content)+offset+match.offset+ncodeunits(match.match)-2,
338-
:face => :julia_backslash_literal))
336+
:face, :julia_backslash_literal))
339337
end
340338
end
341339
isempty(node.args) && return
@@ -372,13 +370,13 @@ julia> JuliaSyntaxHighlighting.highlight("sum(1:8)")
372370
"sum(1:8)"
373371
374372
julia> JuliaSyntaxHighlighting.highlight("sum(1:8)") |> Base.annotations
375-
6-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
376-
(1:3, :face => :julia_funcall)
377-
(4:4, :face => :julia_rainbow_paren_1)
378-
(5:5, :face => :julia_number)
379-
(6:6, :face => :julia_operator)
380-
(7:7, :face => :julia_number)
381-
(8:8, :face => :julia_rainbow_paren_1)
373+
6-element Vector{@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}}:
374+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((1:3, :face, :julia_funcall))
375+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((4:4, :face, :julia_rainbow_paren_1))
376+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((5:5, :face, :julia_number))
377+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((6:6, :face, :julia_operator))
378+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((7:7, :face, :julia_number))
379+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((8:8, :face, :julia_rainbow_paren_1))
382380
```
383381
"""
384382
function highlight end
@@ -422,26 +420,26 @@ julia> JuliaSyntaxHighlighting.highlight!(str)
422420
"sum(1:8)"
423421
424422
julia> Base.annotations(str)
425-
6-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
426-
(1:3, :face => :julia_funcall)
427-
(4:4, :face => :julia_rainbow_paren_1)
428-
(5:5, :face => :julia_number)
429-
(6:6, :face => :julia_operator)
430-
(7:7, :face => :julia_number)
431-
(8:8, :face => :julia_rainbow_paren_1)
423+
6-element Vector{@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}}:
424+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((1:3, :face, :julia_funcall))
425+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((4:4, :face, :julia_rainbow_paren_1))
426+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((5:5, :face, :julia_number))
427+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((6:6, :face, :julia_operator))
428+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((7:7, :face, :julia_number))
429+
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((8:8, :face, :julia_rainbow_paren_1))
432430
```
433431
"""
434432
function highlight!(str::AnnotatedString; syntax_errors::Bool = false)
435-
for (range, annot) in _hl_annotations(str.string, parseall(GreenNode, str.string, ignore_errors=true); syntax_errors)
436-
annotate!(str, range, annot)
433+
for ann in _hl_annotations(str.string, parseall(GreenNode, str.string, ignore_errors=true); syntax_errors)
434+
annotate!(str, ann.region, ann.label, ann.value)
437435
end
438436
str
439437
end
440438

441439
function highlight!(str::SubString{AnnotatedString{S}}; syntax_errors::Bool = false) where {S}
442440
plainstr = SubString{S}(str.string.string, str.offset, str.ncodeunits, Val(:noshift))
443-
for (range, annot) in _hl_annotations(plainstr, parseall(GreenNode, plainstr, ignore_errors=true); syntax_errors)
444-
annotate!(str, range, annot)
441+
for ann in _hl_annotations(plainstr, parseall(GreenNode, plainstr, ignore_errors=true); syntax_errors)
442+
annotate!(str, ann.region, ann.label, ann.value)
445443
end
446444
str
447445
end

Diff for: test/runtests.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ using Test
99
# This also avoids testing as much of the particulars of JuliaSyntax.
1010

1111
sum1to8_highlighted = Base.AnnotatedString("sum(1:8)", [
12-
(1:3, :face => :julia_funcall),
13-
(4:4, :face => :julia_rainbow_paren_1),
14-
(5:5, :face => :julia_number),
15-
(6:6, :face => :julia_operator),
16-
(7:7, :face => :julia_number),
17-
(8:8, :face => :julia_rainbow_paren_1)
12+
(1:3, :face, :julia_funcall),
13+
(4:4, :face, :julia_rainbow_paren_1),
14+
(5:5, :face, :julia_number),
15+
(6:6, :face, :julia_operator),
16+
(7:7, :face, :julia_number),
17+
(8:8, :face, :julia_rainbow_paren_1)
1818
])
1919

2020
@test highlight("sum(1:8)") == sum1to8_highlighted

0 commit comments

Comments
 (0)