Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lineno to SourceFile #191

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 19 additions & 15 deletions src/source_files.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
SourceFile(code [; filename=nothing])
SourceFile(code [; filename=nothing, lineno=1])

A UTF-8 source code string with associated file name and indexing structures.
A UTF-8 source code string with associated file name and line number.

`SourceFile` stores the character positions of line starts to facilitate indexing.
"""
struct SourceFile
# We use `code::String` for now but it could be some other UTF-8 based
Expand All @@ -11,11 +13,12 @@ struct SourceFile
# https://en.wikipedia.org/wiki/Rope_(data_structure)
code::String
filename::Union{Nothing,String}
lineno::Int
# String index of start of every line
line_starts::Vector{Int}
end

function SourceFile(code::AbstractString; filename=nothing)
function SourceFile(code::AbstractString; filename=nothing, lineno=1)
line_starts = Int[1]
for i in eachindex(code)
# The line is considered to start after the `\n`
Expand All @@ -25,31 +28,32 @@ function SourceFile(code::AbstractString; filename=nothing)
if isempty(code) || last(code) != '\n'
push!(line_starts, ncodeunits(code)+1)
end
SourceFile(code, filename, line_starts)
SourceFile(code, filename, lineno, line_starts)
end

function SourceFile(; filename)
SourceFile(read(filename, String); filename=filename)
function SourceFile(; filename, kwargs...)
SourceFile(read(filename, String); filename=filename, kwargs...)
end

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

"""
Get line number and character within the line at the given byte index.
"""
function source_location(source::SourceFile, byte_index)
line = source_line(source, byte_index)
i = source.line_starts[line]
lineidx = source_line_index(source, byte_index)
i = source.line_starts[lineidx]
column = 1
while i < byte_index
i = nextind(source.code, i)
column += 1
end
line, column
source_line(source, byte_index, lineidx), column
end

"""
Expand All @@ -58,9 +62,9 @@ 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 = 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
lineidx = source_line_index(source, byte_index)
fbyte = source.line_starts[max(lineidx-context_lines_before, 1)]
lbyte = source.line_starts[min(lineidx+1+context_lines_after, end)] - 1
fbyte,lbyte
end

Expand Down
17 changes: 17 additions & 0 deletions test/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@
@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)

@test source_location(SourceFile("a"; lineno=7), 1) == (7,1)
@test source_location(SourceFile("a"; lineno=7), 2) == (7,2)

@test source_location(SourceFile("a\n"; lineno=7), 2) == (7,2)
@test source_location(SourceFile("a\n"; lineno=7), 3) == (7,3)

@test source_location(SourceFile("a\nb\n"; lineno=7), 2) == (7,2)
@test source_location(SourceFile("a\nb\n"; lineno=7), 3) == (8,1)
@test source_location(SourceFile("a\nb\n"; lineno=7), 4) == (8,2)
@test source_location(SourceFile("a\nb\n"; lineno=7), 5) == (8,3)

mktemp() do path, io
write(io, "a\n")
@test source_location(SourceFile(; filename=path), 1) == (1,1)
@test source_location(SourceFile(; filename=path, lineno=7), 1) == (7,1)
end
end