1
1
module JuliaSyntaxHighlighting
2
2
3
3
import Base: JuliaSyntax, AnnotatedString, annotate!
4
- import Base. JuliaSyntax: @K_str , Kind, GreenNode, parseall, kind, flags
4
+ import Base. JuliaSyntax: @K_str , Kind, GreenNode, parseall, kind, flags, children, numchildren, span
5
5
using StyledStrings: Face, addface!
6
6
7
7
public highlight, highlight!
@@ -182,8 +182,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
182
182
lineage:: GreenLineage , ctx:: HighlightContext ; syntax_errors:: Bool = false )
183
183
(; node, parent) = lineage
184
184
(; content, offset, lnode, pdepths) = ctx
185
- region = firstindex (content)+ offset: node . span+ offset
186
- regionstr = view (content, firstindex (content)+ offset: prevind (content, node . span+ offset+ 1 ))
185
+ region = firstindex (content)+ offset: span (node) + offset
186
+ regionstr = view (content, firstindex (content)+ offset: prevind (content, span (node) + offset+ 1 ))
187
187
nkind = node. head. kind
188
188
pnode = if ! isnothing (parent) parent. node end
189
189
pkind = if ! isnothing (parent) kind (parent. node) end
@@ -209,9 +209,9 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
209
209
:julia_type
210
210
end
211
211
end
212
- elseif nkind == K " macrocall" && length (node. args ) >= 2 &&
213
- kind (node. args [1 ]) == K " @" && kind (node. args [2 ]) == K " MacroName"
214
- region = first (region): first (region)+ node. args [2 ]. span
212
+ elseif nkind == K " macrocall" && numchildren (node) >= 2 &&
213
+ kind (node[1 ]) == K " @" && kind (node[2 ]) == K " MacroName"
214
+ region = first (region): first (region)+ span ( node[2 ])
215
215
:julia_macro
216
216
elseif nkind == K " StringMacroName" ; :julia_macro
217
217
elseif nkind == K " CmdMacroName" ; :julia_macro
@@ -221,15 +221,15 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
221
221
else
222
222
literal_typedecl = findfirst (
223
223
c -> kind (c) == K " ::" && JuliaSyntax. is_trivia (c),
224
- node. args )
224
+ children ( node) )
225
225
if ! isnothing (literal_typedecl)
226
- shift = sum (c -> Int (c . span) , node. args [1 : literal_typedecl])
226
+ shift = sum (c -> Int (span (c)) , node[1 : literal_typedecl])
227
227
region = first (region)+ shift: last (region)
228
228
:julia_type
229
229
end
230
230
end
231
- elseif nkind == K " quote" && length (node. args ) == 2 &&
232
- kind (node. args [1 ]) == K " :" && kind (node. args [2 ]) == K " Identifier"
231
+ elseif nkind == K " quote" && numchildren (node) == 2 &&
232
+ kind (node[1 ]) == K " :" && kind (node[2 ]) == K " Identifier"
233
233
:julia_symbol
234
234
elseif nkind == K " Comment" ; :julia_comment
235
235
elseif nkind == K " String" ; :julia_string
@@ -247,8 +247,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
247
247
if nkind == K " ="
248
248
ifelse (ppkind == K " for" , :julia_keyword , :julia_assignment )
249
249
else # updating for <op>=
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 ))
250
+ push! (highlights, (firstindex (content)+ offset: span (node) + offset- 1 , :face , :julia_operator ))
251
+ push! (highlights, (span (node) + offset: span (node) + offset, :face , :julia_assignment ))
252
252
nothing
253
253
end
254
254
elseif nkind == K " ;" && pkind == K " parameters" && pnode == lnode
@@ -261,9 +261,9 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
261
261
else
262
262
literal_where = findfirst (
263
263
c -> kind (c) == K " where" && JuliaSyntax. is_trivia (c),
264
- node. args )
264
+ children ( node) )
265
265
if ! isnothing (literal_where)
266
- shift = sum (c -> Int (c . span) , node. args [1 : literal_where])
266
+ shift = sum (c -> Int (span (c)) , node[1 : literal_where])
267
267
region = first (region)+ shift: last (region)
268
268
:julia_type
269
269
end
@@ -281,8 +281,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
281
281
:julia_broadcast
282
282
elseif nkind in (K " call" , K " dotcall" ) && JuliaSyntax. is_prefix_call (node)
283
283
argoffset, arg1 = 0 , nothing
284
- for arg in node. args
285
- argoffset += arg . span
284
+ for arg in children ( node)
285
+ argoffset += span (arg)
286
286
if ! JuliaSyntax. is_trivia (arg)
287
287
arg1 = arg
288
288
break
@@ -293,11 +293,11 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
293
293
region = first (region): first (region)+ argoffset- 1
294
294
name = Symbol (regionstr)
295
295
ifelse (name in BUILTIN_FUNCTIONS, :julia_builtin , :julia_funcall )
296
- elseif kind (arg1) == K " ." && length (arg1. args ) == 3 &&
297
- kind (arg1. args [end ]) == K " quote" &&
298
- length (arg1. args [end ]. args ) == 1 &&
299
- kind (arg1. args [end ]. args [1 ]) == K " Identifier"
300
- region = first (region)+ argoffset- arg1. args [end ]. args [1 ]. span : first (region)+ argoffset- 1
296
+ elseif kind (arg1) == K " ." && numchildren (arg1) == 3 &&
297
+ kind (arg1[end ]) == K " quote" &&
298
+ numchildren (arg1[end ]) == 1 &&
299
+ kind (arg1[end ][1 ]) == K " Identifier"
300
+ region = first (region)+ argoffset- span ( arg1[end ][1 ]) : first (region)+ argoffset- 1
301
301
name = Symbol (regionstr)
302
302
ifelse (name in BUILTIN_FUNCTIONS, :julia_builtin , :julia_funcall )
303
303
end
@@ -336,13 +336,13 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
336
336
:face , :julia_backslash_literal ))
337
337
end
338
338
end
339
- isempty (node. args) && return
339
+ numchildren (node) == 0 && return
340
340
lnode = node
341
- for child in node. args
341
+ for child in children ( node)
342
342
cctx = HighlightContext (content, offset, lnode, pdepths)
343
343
_hl_annotations! (highlights, GreenLineage (child, lineage), cctx)
344
344
lnode = child
345
- offset += child . span
345
+ offset += span (child)
346
346
end
347
347
end
348
348
0 commit comments