Skip to content

Commit a941919

Browse files
authored
Support begin/end indexing of SourceFile (#201)
1 parent c58ea42 commit a941919

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/source_files.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ function Base.getindex(source::SourceFile, i::Int)
109109
source.code[i]
110110
end
111111

112+
Base.firstindex(source::SourceFile) = firstindex(source.code)
113+
Base.lastindex(source::SourceFile) = lastindex(source.code)
114+
112115
"""
113116
sourcetext(source::SourceFile)
114117

test/source_files.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
@test source_location(SourceFile(; filename=path), 1) == (1,1)
2727
@test source_location(SourceFile(; filename=path, first_line=7), 1) == (7,1)
2828
end
29+
30+
@test SourceFile("a\nb\n")[1:2] == "a\n"
31+
@test SourceFile("a\nb\n")[3:end] == "b\n"
32+
if Base.VERSION >= v"1.4"
33+
# Protect the `[begin` from being viewed by the parser on older Julia versions
34+
@test eval(Meta.parse("""SourceFile("a\nb\n")[begin:end]""")) == "a\nb\n"
35+
end
36+
37+
# unicode
38+
@test SourceFile("αβ")[1:2] == "α"
2939
end

0 commit comments

Comments
 (0)