Skip to content

Commit 3592132

Browse files
committed
Fix viewing of regions ending in multi-codepoint
We can't actually use view(content, region) as region spans the codepoints of the string, but Julia expects the end of the range to be the start not the end of the final codepoint.
1 parent 8ffcf53 commit 3592132

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/JuliaSyntaxHighlighting.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
113113
(; node, parent) = lineage
114114
(; content, offset, lnode, pdepths) = ctx
115115
region = firstindex(content)+offset:node.span+offset
116+
regionstr = view(content, firstindex(content)+offset:prevind(content, node.span+offset+1))
116117
nkind = node.head.kind
117118
pnode = if !isnothing(parent) parent.node end
118119
pkind = if !isnothing(parent) kind(parent.node) end
@@ -129,7 +130,7 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
129130
if pkind == K"curly"
130131
:julia_type
131132
else
132-
name = Symbol(view(content, region))
133+
name = Symbol(regionstr)
133134
if name in SINGLETON_IDENTIFIERS
134135
:julia_singleton_identifier
135136
elseif name == :NaN
@@ -220,14 +221,14 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
220221
if isnothing(arg1)
221222
elseif kind(arg1) == K"Identifier"
222223
region = first(region):first(region)+argoffset-1
223-
name = Symbol(view(content, region))
224+
name = Symbol(regionstr)
224225
ifelse(name in BUILTIN_FUNCTIONS, :julia_builtin, :julia_funcall)
225226
elseif kind(arg1) == K"." && length(arg1.args) == 3 &&
226227
kind(arg1.args[end]) == K"quote" &&
227228
length(arg1.args[end].args) == 1 &&
228229
kind(arg1.args[end].args[1]) == K"Identifier"
229230
region = first(region)+argoffset-arg1.args[end].args[1].span:first(region)+argoffset-1
230-
name = Symbol(view(content, region))
231+
name = Symbol(regionstr)
231232
ifelse(name in BUILTIN_FUNCTIONS, :julia_builtin, :julia_funcall)
232233
end
233234
elseif JuliaSyntax.is_error(nkind); :julia_error
@@ -254,13 +255,13 @@ function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol,
254255
if nkind == K"Comment"
255256
for match in eachmatch(
256257
r"(?:^|[(\[{[:space:]-])`([^[:space:]](?:.*?[^[:space:]])?)`(?:$|[!,\-.:;?\[\][:space:]])",
257-
view(content, region))
258+
regionstr)
258259
code = first(match.captures)
259260
push!(highlights, (firstindex(content)+offset+code.offset:firstindex(content)+offset+code.offset+code.ncodeunits-1,
260261
:face => :code))
261262
end
262263
elseif nkind == K"String"
263-
for match in eachmatch(r"\\.", view(content, region))
264+
for match in eachmatch(r"\\.", regionstr)
264265
push!(highlights, (firstindex(content)+offset+match.offset-1:firstindex(content)+offset+match.offset+ncodeunits(match.match)-2,
265266
:face => :julia_backslash_literal))
266267
end

test/runtests.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ sum1to8_highlighted = Base.AnnotatedString("sum(1:8)", [
1717
(8:8, :face => :julia_rainbow_paren_1)
1818
])
1919

20-
@test highlight("sum(1:8)") ==sum1to8_highlighted
21-
@test highlight(IOBuffer("sum(1:8)")) ==sum1to8_highlighted
22-
@test highlight(IOContext(IOBuffer("sum(1:8)"))) ==sum1to8_highlighted
20+
@test highlight("sum(1:8)") == sum1to8_highlighted
21+
@test highlight(IOBuffer("sum(1:8)")) == sum1to8_highlighted
22+
@test highlight(IOContext(IOBuffer("sum(1:8)"))) == sum1to8_highlighted
2323

2424
astr_sum1to8 = Base.AnnotatedString("sum(1:8)")
2525
@test highlight!(astr_sum1to8) == sum1to8_highlighted
2626
@test astr_sum1to8 == sum1to8_highlighted
27+
28+
# Check for string indexing issues
29+
@test Base.annotations(highlight("")) |> first |> first == 1:3

0 commit comments

Comments
 (0)