Skip to content

Commit c221b4e

Browse files
inkydragontecosaur
authored andcommitted
Use the more idiomatic JuliaSyntax API
Instead of just reaching straight into the private fields of the relevant structs, (import and) use the methods provided by JuliaSyntax. Specifically: 1. length(node.args) -> numchildren(node) 2. isempty(children(node)) -> numchildren(node) == 0 3. node.args -> children(node) 4. node.args[x] -> node[x] 5. node.span -> span(node)
1 parent 67e8d6c commit c221b4e

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/JuliaSyntaxHighlighting.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module JuliaSyntaxHighlighting
22

33
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
55
using StyledStrings: Face, addface!
66

77
public highlight, highlight!
@@ -182,8 +182,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
182182
lineage::GreenLineage, ctx::HighlightContext; syntax_errors::Bool = false)
183183
(; node, parent) = lineage
184184
(; 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))
187187
nkind = node.head.kind
188188
pnode = if !isnothing(parent) parent.node end
189189
pkind = if !isnothing(parent) kind(parent.node) end
@@ -209,9 +209,9 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
209209
:julia_type
210210
end
211211
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])
215215
:julia_macro
216216
elseif nkind == K"StringMacroName"; :julia_macro
217217
elseif nkind == K"CmdMacroName"; :julia_macro
@@ -221,15 +221,15 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
221221
else
222222
literal_typedecl = findfirst(
223223
c ->kind(c) == K"::" && JuliaSyntax.is_trivia(c),
224-
node.args)
224+
children(node))
225225
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])
227227
region = first(region)+shift:last(region)
228228
:julia_type
229229
end
230230
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"
233233
:julia_symbol
234234
elseif nkind == K"Comment"; :julia_comment
235235
elseif nkind == K"String"; :julia_string
@@ -247,8 +247,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
247247
if nkind == K"="
248248
ifelse(ppkind == K"for", :julia_keyword, :julia_assignment)
249249
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))
252252
nothing
253253
end
254254
elseif nkind == K";" && pkind == K"parameters" && pnode == lnode
@@ -261,9 +261,9 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
261261
else
262262
literal_where = findfirst(
263263
c ->kind(c) == K"where" && JuliaSyntax.is_trivia(c),
264-
node.args)
264+
children(node))
265265
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])
267267
region = first(region)+shift:last(region)
268268
:julia_type
269269
end
@@ -281,8 +281,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
281281
:julia_broadcast
282282
elseif nkind in (K"call", K"dotcall") && JuliaSyntax.is_prefix_call(node)
283283
argoffset, arg1 = 0, nothing
284-
for arg in node.args
285-
argoffset += arg.span
284+
for arg in children(node)
285+
argoffset += span(arg)
286286
if !JuliaSyntax.is_trivia(arg)
287287
arg1 = arg
288288
break
@@ -293,11 +293,11 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
293293
region = first(region):first(region)+argoffset-1
294294
name = Symbol(regionstr)
295295
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
301301
name = Symbol(regionstr)
302302
ifelse(name in BUILTIN_FUNCTIONS, :julia_builtin, :julia_funcall)
303303
end
@@ -336,13 +336,13 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
336336
:face, :julia_backslash_literal))
337337
end
338338
end
339-
isempty(node.args) && return
339+
numchildren(node) == 0 && return
340340
lnode = node
341-
for child in node.args
341+
for child in children(node)
342342
cctx = HighlightContext(content, offset, lnode, pdepths)
343343
_hl_annotations!(highlights, GreenLineage(child, lineage), cctx)
344344
lnode = child
345-
offset += child.span
345+
offset += span(child)
346346
end
347347
end
348348

0 commit comments

Comments
 (0)