Skip to content

Commit

Permalink
Fix highlighting of ranges which start with non-ascii chars (#387)
Browse files Browse the repository at this point in the history
This could cause the parser hook to crash on certain inputs.
  • Loading branch information
c42f committed Nov 9, 2023
1 parent 0df34c9 commit 5757535
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ function Base.thisind(source::SourceFile, i::Int)
thisind(source.code, i - source.byte_offset) + source.byte_offset
end

function Base.nextind(source::SourceFile, i::Integer)
nextind(source.code, i - source.byte_offset) + source.byte_offset
end

Base.firstindex(source::SourceFile) = firstindex(source.code) + source.byte_offset
Base.lastindex(source::SourceFile) = lastindex(source.code) + source.byte_offset

Expand Down Expand Up @@ -218,7 +222,8 @@ function highlight(io::IO, source::SourceFile, range::UnitRange;
hitext = source[p:q]
print(io, source[x:p-1])
_printstyled(io, hitext; bgcolor=color)
print(io, source[q+1:d])
#print(io, source[q+1:d])
print(io, source[nextind(source,q):d])
if d >= firstindex(source) && source[thisind(source, d)] != '\n'
print(io, "\n")
end
Expand Down Expand Up @@ -249,7 +254,7 @@ function highlight(io::IO, source::SourceFile, range::UnitRange;
print(io, "\n")
_printstyled(io, source[z:q]; bgcolor=color)
end
print(io, source[q+1:d])
print(io, source[nextind(source, q):d])
source[thisind(source, d)] == '\n' || print(io, "\n")
qline = source[c:q]
_print_marker_line(io, "", qline, true, false, marker_line_color, note, notecolor)
Expand Down
15 changes: 15 additions & 0 deletions test/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ end
@test sprint(highlight, src, 3:4) == "abcd\n# └┘\nαβγδ\n+-*/"
@test sprint(highlight, src, 4:4) == "abcd\n# ╙\nαβγδ\n+-*/"
@test sprint(highlight, src, 5:5) == "abcd\n# └\nαβγδ\n+-*/"
@test sprint(highlight, src, 6:6) == "abcd\nαβγδ\n\n+-*/"
@test sprint(highlight, src, 6:9) == "abcd\nαβγδ\n└┘\n+-*/"
@test sprint(highlight, src, 8:8) == "abcd\nαβγδ\n#╙\n+-*/"

# multi-byte chars
@test sprint(highlight, src, 8:13) == """
Expand Down Expand Up @@ -149,6 +152,18 @@ end
αβγδ
#┘
+-*/"""
@test sprint(highlight, src, 6:15) == """
abcd
┌───
αβγδ
+-*/
"""
@test sprint(highlight, src, 8:15) == """
abcd
#┌──
αβγδ
+-*/
"""
@test sprint(highlight, src, 1:18) == """
┌───
abcd
Expand Down

0 comments on commit 5757535

Please sign in to comment.