Skip to content

Commit 1f07255

Browse files
committed
Also return line1 from definition(::Method, String)
The String variant of `definition` has the ability to back up in the source file to also extract the signature. As a consequence, it may be unclear how the first line of the string corresponds to the output of `whereis`. To resolve the uncertainty, return the first source line as a second argument from `definition(::Method, String)`. This is a breaking change.
1 parent 1fbb0f2 commit 1f07255

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/CodeTracking.jl

Lines changed: 5 additions & 4 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,7 +195,7 @@ 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)
@@ -204,7 +205,7 @@ function definition(method::Method, ::Type{String})
204205
lineindex -= 1
205206
line -= 1
206207
end
207-
return src[istart:iend-1]
208+
return src[istart:iend-1], line
208209
end
209210

210211
"""

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
2020
@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)
2626
# A comment
2727
return x + y
2828
end
2929
"""
30+
@test line == 2
3031

3132
m = first(methods(f2))
32-
src = definition(m, String)
33+
src, line = definition(m, String)
3334
@test src == """
3435
f2(x, y) = x + y
3536
"""
37+
@test line == 7
3638

3739
info = CodeTracking.PkgFiles(Base.PkgId(CodeTracking))
3840
@test Base.PkgId(info) === info.id

0 commit comments

Comments
 (0)