Skip to content

Commit 5c01d70

Browse files
authored
Merge pull request #25 from timholy/teh/def_string
Change the API on `definition(::Method, String)`
2 parents 4d0622b + 1f07255 commit 5c01d70

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/CodeTracking.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ function signatures_at(id::PkgId, relpath::AbstractString, line::Integer)
175175
end
176176

177177
"""
178-
src = definition(method::Method, String)
178+
src, line1 = definition(method::Method, String)
179179
180-
Return a string with the code that defines `method`.
180+
Return a string with the code that defines `method`. Also return the first line of the
181+
definition, including the signature.
181182
182183
Note this may not be terribly useful for methods that are defined inside `@eval` statements;
183184
see [`definition(method::Method, Expr)`](@ref) instead.
@@ -194,15 +195,17 @@ function definition(method::Method, ::Type{String})
194195
end
195196
ex, iend = Meta.parse(src, istart)
196197
if isfuncexpr(ex)
197-
return src[istart+1:iend-1]
198+
return src[istart+1:iend-1], line
198199
end
199200
# The function declaration was presumably on a previous line
200201
lineindex = lastindex(linestarts)
201-
while !isfuncexpr(ex)
202+
while !isfuncexpr(ex) && lineindex > 0
202203
istart = linestarts[lineindex]
203204
ex, iend = Meta.parse(src, istart)
205+
lineindex -= 1
206+
line -= 1
204207
end
205-
return src[istart:iend-1]
208+
return src[istart:iend-1], line
206209
end
207210

208211
"""

test/runtests.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,30 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
1111
file, line = whereis(m)
1212
scriptpath = normpath(joinpath(@__DIR__, "script.jl"))
1313
@test file == scriptpath
14-
@test line == 3
14+
@test line == 4
1515
trace = try
1616
call_throws()
1717
catch
1818
stacktrace(catch_backtrace())
1919
end
20-
@test whereis(trace[2]) == (scriptpath, 10)
20+
@test whereis(trace[2]) == (scriptpath, 11)
2121
@test whereis(trace[3]) === nothing
2222

23-
src = definition(m, String)
23+
src, line = definition(m, String)
2424
@test src == """
2525
function f1(x, y)
26+
# A comment
2627
return x + y
2728
end
2829
"""
30+
@test line == 2
2931

3032
m = first(methods(f2))
31-
src = definition(m, String)
33+
src, line = definition(m, String)
3234
@test src == """
3335
f2(x, y) = x + y
3436
"""
37+
@test line == 7
3538

3639
info = CodeTracking.PkgFiles(Base.PkgId(CodeTracking))
3740
@test Base.PkgId(info) === info.id

test/script.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: tests are sensitive to the line number at which statements appear
22
function f1(x, y)
3+
# A comment
34
return x + y
45
end
56

0 commit comments

Comments
 (0)