Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ language: julia
os:
- linux
- osx
- windows

julia:
- 1.0
- 1.1
- 1
- nightly

branches:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CodeTracking"
uuid = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
authors = ["Tim Holy <tim.holy@gmail.com>"]
version = "0.5.8"
version = "0.5.9"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
13 changes: 10 additions & 3 deletions src/CodeTracking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ using InteractiveUtils

export whereis, definition, pkgfiles, signatures_at

# More recent Julia versions assign the line number to the line with the function declaration,
# not the first non-comment line of the body.
const line_is_decl = VERSION >= v"1.5.0-DEV.567"

include("pkgfiles.jl")
include("utils.jl")

Expand Down Expand Up @@ -44,8 +48,9 @@ const juliastdlib = joinpath("julia", "stdlib", "v$(VERSION.major).$(VERSION.min
"""
filepath, line = whereis(method::Method)

Return the file and line of the definition of `method`. `line`
is the first line of the method's body.
Return the file and line of the definition of `method`. The meaning of `line`
depends on the Julia version: on Julia 1.5 and higher it is the line number of
the method declaration, otherwise it is the first line of the method's body.
"""
function whereis(method::Method)
file, line = String(method.file), method.line
Expand Down Expand Up @@ -111,7 +116,8 @@ end
sigs = signatures_at(filename, line)

Return the signatures of all methods whose definition spans the specified location.
`line` must correspond to a line in the method body (not the signature or final `end`).
Prior to Julia 1.5, `line` must correspond to a line in the method body
(not the signature or final `end`).

Returns `nothing` if there are no methods at that location.
"""
Expand Down Expand Up @@ -193,6 +199,7 @@ function definition(::Type{String}, method::Method)
file, line = whereis(method)
line == 0 && return nothing
src = src_from_file_or_REPL(file)
src = replace(src, "\r"=>"")
eol = isequal('\n')
linestarts = Int[]
istart = 1
Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ using CodeTracking
using Test, InteractiveUtils
# Note: ColorTypes needs to be installed, but note the intentional absence of `using ColorTypes`

using CodeTracking: line_is_decl

isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")

@testset "CodeTracking.jl" begin
m = first(methods(f1))
file, line = whereis(m)
scriptpath = normpath(joinpath(@__DIR__, "script.jl"))
@test file == scriptpath
@test line == 4
@test line == (line_is_decl ? 2 : 4)
trace = try
call_throws()
catch
Expand Down