Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function SourceFile(code::AbstractString; filename=nothing)
code[i] == '\n' && push!(line_starts, i+1)
end
if isempty(code) || last(code) != '\n'
push!(line_starts, lastindex(code)+1)
push!(line_starts, ncodeunits(code)+1)
end
SourceFile(code, filename, line_starts)
end
Expand All @@ -34,14 +34,15 @@ end

# Get line number of the given byte within the code
function source_line(source::SourceFile, byte_index)
searchsortedlast(source.line_starts, byte_index)
line = searchsortedlast(source.line_starts, byte_index)
return (line < lastindex(source.line_starts)) ? line : line-1
end

"""
Get line number and character within the line at the given byte index.
"""
function source_location(source::SourceFile, byte_index)
line = searchsortedlast(source.line_starts, byte_index)
line = source_line(source, byte_index)
i = source.line_starts[line]
column = 1
while i < byte_index
Expand All @@ -57,7 +58,7 @@ Get byte range of the source line at byte_index, buffered by
"""
function source_line_range(source::SourceFile, byte_index;
context_lines_before=0, context_lines_after=0)
line = searchsortedlast(source.line_starts, byte_index)
line = source_line(source, byte_index)
fbyte = source.line_starts[max(line-context_lines_before, 1)]
lbyte = source.line_starts[min(line+1+context_lines_after, end)] - 1
fbyte,lbyte
Expand Down
6 changes: 2 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ include("parser.jl")
include("diagnostics.jl")
include("parser_api.jl")
include("expr.jl")
@testset "Parsing values from strings" begin
@testset "Parsing literals from strings" begin
include("value_parsing.jl")
end
include("hooks.jl")
include("parse_packages.jl")
include("source_files.jl")

# Prototypes
#include("syntax_interpolation.jl")
#include("simple_parser.jl")
12 changes: 12 additions & 0 deletions test/source_files.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@testset "SourceFile lines and column indexing" begin
@test source_location(SourceFile("a"), 1) == (1,1)
@test source_location(SourceFile("a"), 2) == (1,2)

@test source_location(SourceFile("a\n"), 2) == (1,2)
@test source_location(SourceFile("a\n"), 3) == (1,3)

@test source_location(SourceFile("a\nb\n"), 2) == (1,2)
@test source_location(SourceFile("a\nb\n"), 3) == (2,1)
@test source_location(SourceFile("a\nb\n"), 4) == (2,2)
@test source_location(SourceFile("a\nb\n"), 5) == (2,3)
end
1 change: 1 addition & 0 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using .JuliaSyntax:
ParseState,
Diagnostic,
SourceFile,
source_location,
parse!,
parse,
parseall,
Expand Down