|
1 | 1 | module CodeTracking |
2 | 2 |
|
3 | | -export whereis |
| 3 | +using Core: LineInfoNode |
4 | 4 |
|
5 | | -# This is just a stub implementation for now |
| 5 | +export whereis, definition, pkgfiles |
| 6 | + |
| 7 | +include("data.jl") |
| 8 | +include("utils.jl") |
| 9 | + |
| 10 | +""" |
| 11 | + filepath, line = whereis(method::Method) |
| 12 | +
|
| 13 | +Return the file and line of the definition of `method`. `line` |
| 14 | +is the first line of the method's body. |
| 15 | +""" |
6 | 16 | function whereis(method::Method) |
7 | | - file, line = String(method.file), method.line |
| 17 | + lin = get(method_locations, method.sig, nothing) |
| 18 | + if lin === nothing |
| 19 | + file, line = String(method.file), method.line |
| 20 | + else |
| 21 | + file, line = fileline(lin) |
| 22 | + end |
8 | 23 | if !isabspath(file) |
9 | | - # This is a Base method |
| 24 | + # This is a Base or Core method |
10 | 25 | file = Base.find_source_file(file) |
11 | 26 | end |
12 | 27 | return normpath(file), line |
13 | 28 | end |
14 | 29 |
|
| 30 | +""" |
| 31 | + src = definition(method::Method, String) |
| 32 | +
|
| 33 | +Return a string with the code that defines `method`. |
| 34 | +
|
| 35 | +Note this may not be terribly useful for methods that are defined inside `@eval` statements; |
| 36 | +see [`definition(method::Method, Expr)`](@ref) instead. |
| 37 | +""" |
| 38 | +function definition(method::Method, ::Type{String}) |
| 39 | + file, line = whereis(method) |
| 40 | + src = read(file, String) |
| 41 | + eol = isequal('\n') |
| 42 | + linestarts = Int[] |
| 43 | + istart = 0 |
| 44 | + for i = 1:line-1 |
| 45 | + push!(linestarts, istart+1) |
| 46 | + istart = findnext(eol, src, istart+1) |
| 47 | + end |
| 48 | + ex, iend = Meta.parse(src, istart) |
| 49 | + if isfuncexpr(ex) |
| 50 | + return src[istart+1:iend-1] |
| 51 | + end |
| 52 | + # The function declaration was presumably on a previous line |
| 53 | + lineindex = lastindex(linestarts) |
| 54 | + while !isfuncexpr(ex) |
| 55 | + istart = linestarts[lineindex] |
| 56 | + ex, iend = Meta.parse(src, istart) |
| 57 | + end |
| 58 | + return src[istart:iend-1] |
| 59 | +end |
| 60 | + |
| 61 | +""" |
| 62 | + ex = definition(method::Method, Expr) |
| 63 | + ex = definition(method::Method) |
| 64 | +
|
| 65 | +Return an expression that defines `method`. |
| 66 | +""" |
| 67 | +definition(method::Method, ::Type{Expr}) = get(method_definitions, method.sig, nothing) |
| 68 | + |
| 69 | +definition(method::Method) = definition(method, Expr) |
| 70 | + |
| 71 | +""" |
| 72 | + files = pkgfiles(mod::Module) |
| 73 | +
|
| 74 | +Return a list of the files that were loaded to define `mod`. |
| 75 | +""" |
| 76 | +function pkgfiles(mod::Module) |
| 77 | + error("not implemented") |
| 78 | +end |
| 79 | + |
15 | 80 | end # module |
0 commit comments