Skip to content

Commit 5757535

Browse files
authored
Fix highlighting of ranges which start with non-ascii chars (#387)
This could cause the parser hook to crash on certain inputs.
1 parent 0df34c9 commit 5757535

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/source_files.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ function Base.thisind(source::SourceFile, i::Int)
124124
thisind(source.code, i - source.byte_offset) + source.byte_offset
125125
end
126126

127+
function Base.nextind(source::SourceFile, i::Integer)
128+
nextind(source.code, i - source.byte_offset) + source.byte_offset
129+
end
130+
127131
Base.firstindex(source::SourceFile) = firstindex(source.code) + source.byte_offset
128132
Base.lastindex(source::SourceFile) = lastindex(source.code) + source.byte_offset
129133

@@ -218,7 +222,8 @@ function highlight(io::IO, source::SourceFile, range::UnitRange;
218222
hitext = source[p:q]
219223
print(io, source[x:p-1])
220224
_printstyled(io, hitext; bgcolor=color)
221-
print(io, source[q+1:d])
225+
#print(io, source[q+1:d])
226+
print(io, source[nextind(source,q):d])
222227
if d >= firstindex(source) && source[thisind(source, d)] != '\n'
223228
print(io, "\n")
224229
end
@@ -249,7 +254,7 @@ function highlight(io::IO, source::SourceFile, range::UnitRange;
249254
print(io, "\n")
250255
_printstyled(io, source[z:q]; bgcolor=color)
251256
end
252-
print(io, source[q+1:d])
257+
print(io, source[nextind(source, q):d])
253258
source[thisind(source, d)] == '\n' || print(io, "\n")
254259
qline = source[c:q]
255260
_print_marker_line(io, "", qline, true, false, marker_line_color, note, notecolor)

test/source_files.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ end
103103
@test sprint(highlight, src, 3:4) == "abcd\n# └┘\nαβγδ\n+-*/"
104104
@test sprint(highlight, src, 4:4) == "abcd\n# ╙\nαβγδ\n+-*/"
105105
@test sprint(highlight, src, 5:5) == "abcd\n# └\nαβγδ\n+-*/"
106+
@test sprint(highlight, src, 6:6) == "abcd\nαβγδ\n\n+-*/"
107+
@test sprint(highlight, src, 6:9) == "abcd\nαβγδ\n└┘\n+-*/"
108+
@test sprint(highlight, src, 8:8) == "abcd\nαβγδ\n#╙\n+-*/"
106109

107110
# multi-byte chars
108111
@test sprint(highlight, src, 8:13) == """
@@ -149,6 +152,18 @@ end
149152
αβγδ
150153
#┘
151154
+-*/"""
155+
@test sprint(highlight, src, 6:15) == """
156+
abcd
157+
┌───
158+
αβγδ
159+
+-*/
160+
"""
161+
@test sprint(highlight, src, 8:15) == """
162+
abcd
163+
#┌──
164+
αβγδ
165+
+-*/
166+
"""
152167
@test sprint(highlight, src, 1:18) == """
153168
┌───
154169
abcd

0 commit comments

Comments
 (0)