CodeTracking.CodeTrackingModule

CodeTracking can be thought of as an extension of InteractiveUtils, and pairs well with Revise.jl.

  • code_string, @code_string: fetch the source code (as a string) for a method definition
  • code_expr, @code_expr: fetch the expression for a method definition
  • definition: a lower-level variant of the above
  • pkgfiles: return information about the source files that define a package
  • whereis: Return location information about methods (with Revise, it updates as you edit files)
  • signatures_at: return the signatures of all methods whose definition spans the specified location
CodeTracking.PkgFilesType

PkgFiles encodes information about the current location of a package. Fields:

  • id: the PkgId of the package
  • basedir: the current base directory of the package
  • files: a list of files (relative path to basedir) that define the package.

Note that basedir may be subsequently updated by Pkg operations such as add and dev.

CodeTracking.code_exprMethod
code_expr(f, types)

Returns the expression for the method definition for f with the specified types.

May return nothing if Revise isn't loaded. In such cases, calling Meta.parse(code_string(f, types)) can sometimes be an alternative.

CodeTracking.code_stringMethod
code_string(f, types)

Returns the code-string for the method definition for f with the specified types.

CodeTracking.definitionMethod
ex = definition(Expr, method::Method)
ex = definition(method::Method)

Return an expression that defines method. If the definition can't be found, returns nothing.

See also code_expr.

CodeTracking.definitionMethod
src, line1 = definition(String, method::Method)

Return a string with the code that defines method. Also return the first line of the definition, including the signature (which may not be the same line number returned by whereis). If the method can't be located (line number 0), then definition instead returns nothing.

Note this may not be terribly useful for methods that are defined inside @eval statements; see definition(Expr, method::Method) instead.

See also code_string.

CodeTracking.maybe_fixup_stdlib_pathMethod
path = maybe_fixup_stdlib_path(path::String)

Return path corrected for julia issue #26314 if applicable. Otherwise, return the input path unchanged.

Due to the issue mentioned above, location info for methods defined one of Julia's standard libraries are, for non source Julia builds, given as absolute paths on the worker that built the julia executable. This function corrects such a path to instead refer to the local path on the users drive.

CodeTracking.pkgfilesMethod
info = pkgfiles(name::AbstractString)
info = pkgfiles(name::AbstractString, uuid::UUID)

Return a CodeTracking.PkgFiles structure with information about the files that define the package specified by name and uuid. Returns nothing if this package has not been loaded.

CodeTracking.signatures_atMethod
sigs = signatures_at(filename, line)

Return the signatures of all methods whose definition spans the specified location. 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.

CodeTracking.signatures_atMethod
sigs = signatures_at(mod::Module, relativepath, line)

For a package that defines module mod, return the signatures of all methods whose definition spans the specified location. relativepath indicates the path of the file relative to the packages top-level directory, e.g., "src/utils.jl". 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.

CodeTracking.src_from_file_or_REPLMethod
src = src_from_file_or_REPL(origin::AbstractString, repl = Base.active_repl)

Read the source for a function from origin, which is either the name of a file or "REPL[i]", where i is an integer specifying the particular history entry. Methods defined at the REPL use strings of this form in their file field.

If you happen to have a file where the name matches REPL[$i], first pass it through abspath.

CodeTracking.whereisMethod
filepath, line = whereis(lineinfo, method::Method)

Return the file and line number associated with a specific statement in method. lineinfo.line should contain the line number of the statement at the time method was compiled. The current location is returned.

CodeTracking.whereisMethod
loc = whereis(sf::StackFrame)

Return location information for a single frame of a stack trace. If sf corresponds to a frame that was inlined, loc will be nothing. Otherwise loc will be (filepath, line).

CodeTracking.whereisMethod
filepath, line = whereis(method::Method)

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.