From a94191988dde0ddc761e2668d0fc17e76c132429 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sat, 18 Feb 2023 08:20:17 -0600 Subject: [PATCH] Support `begin/end` indexing of `SourceFile` (#201) --- src/source_files.jl | 3 +++ test/source_files.jl | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/source_files.jl b/src/source_files.jl index fe78185c..66e960bb 100644 --- a/src/source_files.jl +++ b/src/source_files.jl @@ -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) diff --git a/test/source_files.jl b/test/source_files.jl index 88a1cad8..b40f2818 100644 --- a/test/source_files.jl +++ b/test/source_files.jl @@ -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