@@ -154,33 +154,31 @@ struct HighlightContext{S <: AbstractString}
154
154
end
155
155
156
156
"""
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}}
158
159
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`.
160
161
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 .
163
164
164
165
This is a small wrapper around [`_hl_annotations!`](@ref) for convenience.
165
166
"""
166
167
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}} ()
168
169
ctx = HighlightContext (content, zero (UInt), ast, ParenDepthCounter ())
169
170
_hl_annotations! (highlights, GreenLineage (ast, nothing ), ctx; syntax_errors)
170
171
highlights
171
172
end
172
173
173
174
"""
174
- _hl_annotations!(highlights::Vector{Tuple{ UnitRange{Int}, Pair{ Symbol, Any} }},
175
+ _hl_annotations!(highlights::Vector{@NamedTuple{region:: UnitRange{Int}, label:: Symbol, value:: Any}},
175
176
lineage::GreenLineage, ctx::HighlightContext)
176
177
177
- Populate `highlights` with `(range, annot)` pairs for the given `lineage` and `ctx`,
178
+ Populate `highlights` with annotations for the given `lineage` and `ctx`,
178
179
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>`.
182
180
"""
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}} ,
184
182
lineage:: GreenLineage , ctx:: HighlightContext ; syntax_errors:: Bool = false )
185
183
(; node, parent) = lineage
186
184
(; content, offset, lnode, pdepths) = ctx
@@ -240,7 +238,7 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
240
238
elseif nkind == K " `" || nkind == K " ```" ; :julia_cmdstring
241
239
elseif nkind == K " Char"
242
240
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 ))
244
242
:julia_char
245
243
elseif nkind == K " '" && kind (lnode) == K " Char" ; :julia_char_delim
246
244
elseif nkind == K " true" || nkind == K " false" ; :julia_bool
@@ -249,8 +247,8 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
249
247
if nkind == K " ="
250
248
ifelse (ppkind == K " for" , :julia_keyword , :julia_assignment )
251
249
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 ))
254
252
nothing
255
253
end
256
254
elseif nkind == K " ;" && pkind == K " parameters" && pnode == lnode
@@ -323,19 +321,19 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
323
321
end
324
322
end
325
323
! isnothing (face) &&
326
- push! (highlights, (region, :face => face))
324
+ push! (highlights, (region, :face , face))
327
325
if nkind == K " Comment"
328
326
for match in eachmatch (
329
327
r" (?:^|[(\[ {[:space:]-])`([^[:space:]](?:.*?[^[:space:]])?)`(?:$|[!,\- .:;?\[\] [:space:]])" ,
330
328
regionstr)
331
329
code = first (match. captures)
332
330
push! (highlights, (firstindex (content)+ offset+ code. offset: firstindex (content)+ offset+ code. offset+ code. ncodeunits- 1 ,
333
- :face => :code ))
331
+ :face , :code ))
334
332
end
335
333
elseif nkind == K " String"
336
334
for match in eachmatch (r" \\ ." , regionstr)
337
335
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 ))
339
337
end
340
338
end
341
339
isempty (node. args) && return
@@ -372,13 +370,13 @@ julia> JuliaSyntaxHighlighting.highlight("sum(1:8)")
372
370
"sum(1:8)"
373
371
374
372
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) )
382
380
```
383
381
"""
384
382
function highlight end
@@ -422,26 +420,26 @@ julia> JuliaSyntaxHighlighting.highlight!(str)
422
420
"sum(1:8)"
423
421
424
422
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) )
432
430
```
433
431
"""
434
432
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 )
437
435
end
438
436
str
439
437
end
440
438
441
439
function highlight! (str:: SubString{AnnotatedString{S}} ; syntax_errors:: Bool = false ) where {S}
442
440
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 )
445
443
end
446
444
str
447
445
end
0 commit comments