From 86071caf49845c3904c30f01cf8554df64f80fb7 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 17 Feb 2023 16:30:01 -0600 Subject: [PATCH 1/3] Support `begin/end` indexing of `SourceFile` --- src/source_files.jl | 3 +++ test/source_files.jl | 9 +++++++++ 2 files changed, 12 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..34b4c273 100644 --- a/test/source_files.jl +++ b/test/source_files.jl @@ -26,4 +26,13 @@ @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.6" + @test SourceFile("a\nb\n")[begin:end] == "a\nb\n" + end + + # unicode + @test SourceFile("αβ")[1:2] == "α" end From 18c5f1c57e96f44458e8f7de8c64ecb53ab4e228 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 17 Feb 2023 16:38:26 -0600 Subject: [PATCH 2/3] Handle old Julia versions --- test/source_files.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/source_files.jl b/test/source_files.jl index 34b4c273..94db3330 100644 --- a/test/source_files.jl +++ b/test/source_files.jl @@ -29,8 +29,9 @@ @test SourceFile("a\nb\n")[1:2] == "a\n" @test SourceFile("a\nb\n")[3:end] == "b\n" - if Base.VERSION >= v"1.6" - @test SourceFile("a\nb\n")[begin:end] == "a\nb\n" + if Base.VERSION >= v"1.4" + # Protect the `[begin` from being viewed by the parse on older Julia versions + @test eval(Meta.parse("""SourceFile("a\nb\n")[begin:end]""")) == "a\nb\n" end # unicode From 1c712b90bd21b3f8fce978d54343f3c2383bd12c Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sat, 18 Feb 2023 08:04:17 -0600 Subject: [PATCH 3/3] Fix comment --- test/source_files.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/source_files.jl b/test/source_files.jl index 94db3330..b40f2818 100644 --- a/test/source_files.jl +++ b/test/source_files.jl @@ -30,7 +30,7 @@ @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 parse on older Julia versions + # 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