Skip to content

Commit

Permalink
Support begin/end indexing of SourceFile (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy authored Feb 18, 2023
1 parent c58ea42 commit a941919
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ function Base.getindex(source::SourceFile, i::Int)
source.code[i]
end

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

"""
sourcetext(source::SourceFile)
Expand Down
10 changes: 10 additions & 0 deletions test/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@
@test source_location(SourceFile(; filename=path), 1) == (1,1)
@test source_location(SourceFile(; filename=path, first_line=7), 1) == (7,1)
end

@test SourceFile("a\nb\n")[1:2] == "a\n"
@test SourceFile("a\nb\n")[3:end] == "b\n"
if Base.VERSION >= v"1.4"
# Protect the `[begin` from being viewed by the parser on older Julia versions
@test eval(Meta.parse("""SourceFile("a\nb\n")[begin:end]""")) == "a\nb\n"
end

# unicode
@test SourceFile("αβ")[1:2] == "α"
end

0 comments on commit a941919

Please sign in to comment.