|
1 | 1 | module CodeTracking |
2 | 2 |
|
3 | | -export whereis |
| 3 | +using Base: PkgId |
| 4 | +using Core: LineInfoNode |
| 5 | +using UUIDs |
4 | 6 |
|
5 | | -# This is just a stub implementation for now |
| 7 | +export PkgFiles |
| 8 | +export whereis, definition, pkgfiles |
| 9 | + |
| 10 | +include("data.jl") |
| 11 | +include("utils.jl") |
| 12 | + |
| 13 | +""" |
| 14 | + filepath, line = whereis(method::Method) |
| 15 | +
|
| 16 | +Return the file and line of the definition of `method`. `line` |
| 17 | +is the first line of the method's body. |
| 18 | +""" |
6 | 19 | function whereis(method::Method) |
7 | | - file, line = String(method.file), method.line |
| 20 | + lin = get(method_locations, method.sig, nothing) |
| 21 | + if lin === nothing |
| 22 | + file, line = String(method.file), method.line |
| 23 | + else |
| 24 | + file, line = fileline(lin) |
| 25 | + end |
8 | 26 | if !isabspath(file) |
9 | | - # This is a Base method |
| 27 | + # This is a Base or Core method |
10 | 28 | file = Base.find_source_file(file) |
11 | 29 | end |
12 | 30 | return normpath(file), line |
13 | 31 | end |
14 | 32 |
|
| 33 | +""" |
| 34 | + src = definition(method::Method, String) |
| 35 | +
|
| 36 | +Return a string with the code that defines `method`. |
| 37 | +
|
| 38 | +Note this may not be terribly useful for methods that are defined inside `@eval` statements; |
| 39 | +see [`definition(method::Method, Expr)`](@ref) instead. |
| 40 | +""" |
| 41 | +function definition(method::Method, ::Type{String}) |
| 42 | + file, line = whereis(method) |
| 43 | + src = read(file, String) |
| 44 | + eol = isequal('\n') |
| 45 | + linestarts = Int[] |
| 46 | + istart = 0 |
| 47 | + for i = 1:line-1 |
| 48 | + push!(linestarts, istart+1) |
| 49 | + istart = findnext(eol, src, istart+1) |
| 50 | + end |
| 51 | + ex, iend = Meta.parse(src, istart) |
| 52 | + if isfuncexpr(ex) |
| 53 | + return src[istart+1:iend-1] |
| 54 | + end |
| 55 | + # The function declaration was presumably on a previous line |
| 56 | + lineindex = lastindex(linestarts) |
| 57 | + while !isfuncexpr(ex) |
| 58 | + istart = linestarts[lineindex] |
| 59 | + ex, iend = Meta.parse(src, istart) |
| 60 | + end |
| 61 | + return src[istart:iend-1] |
| 62 | +end |
| 63 | + |
| 64 | +""" |
| 65 | + ex = definition(method::Method, Expr) |
| 66 | + ex = definition(method::Method) |
| 67 | +
|
| 68 | +Return an expression that defines `method`. |
| 69 | +""" |
| 70 | +function definition(method::Method, ::Type{Expr}) |
| 71 | + def = get(method_definitions, method.sig, nothing) |
| 72 | + if def === nothing |
| 73 | + f = method_lookup_callback[] |
| 74 | + if f !== nothing |
| 75 | + Base.invokelatest(f, method) |
| 76 | + end |
| 77 | + def = get(method_definitions, method.sig, nothing) |
| 78 | + end |
| 79 | + return def === nothing ? nothing : copy(def) |
| 80 | +end |
| 81 | + |
| 82 | +definition(method::Method) = definition(method, Expr) |
| 83 | + |
| 84 | +""" |
| 85 | + info = pkgfiles(name::AbstractString) |
| 86 | + info = pkgfiles(name::AbstractString, uuid::UUID) |
| 87 | +
|
| 88 | +Return a [`PkgFiles`](@ref) structure with information about the files that define the package |
| 89 | +specified by `name` and `uuid`. |
| 90 | +Returns `nothing` if this package has not been loaded. |
| 91 | +""" |
| 92 | +pkgfiles(name::AbstractString, uuid::UUID) = pkgfiles(PkgId(uuid, name)) |
| 93 | +function pkgfiles(name::AbstractString) |
| 94 | + project = Base.active_project() |
| 95 | + uuid = Base.project_deps_get(project, name) |
| 96 | + uuid == false && error("no package ", name, " recognized") |
| 97 | + return pkgfiles(name, uuid) |
| 98 | +end |
| 99 | +pkgfiles(id::PkgId) = get(_pkgfiles, id, nothing) |
| 100 | + |
| 101 | +""" |
| 102 | + info = pkgfiles(mod::Module) |
| 103 | +
|
| 104 | +Return a [`PkgFiles`](@ref) structure with information about the files that were loaded to |
| 105 | +define the package that defined `mod`. |
| 106 | +""" |
| 107 | +pkgfiles(mod::Module) = pkgfiles(PkgId(mod)) |
| 108 | + |
15 | 109 | end # module |
0 commit comments