Browse Source

[julia] Make REPL AST transform more robust.

master
Fredrik Ekre 3 years ago
parent
commit
0762083763
  1. 45
      .julia/config/startup.jl

45
.julia/config/startup.jl

@ -1,17 +1,15 @@
if Base.isinteractive() && if Base.isinteractive() &&
(local REPL = get(Base.loaded_modules, Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"), nothing); REPL !== nothing) (local REPL = get(Base.loaded_modules, Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"), nothing); REPL !== nothing)
# Exit Julia with :q # Exit Julia with :q, restart with :r
pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing}) pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing})
if Meta.isexpr(ast, :toplevel, 2) && ast.args[2] === QuoteNode(:q) function toplevel_quotenode(ast, s)
exit() return (Meta.isexpr(ast, :toplevel, 2) && ast.args[2] === QuoteNode(s)) ||
(Meta.isexpr(ast, :toplevel) && any(x -> toplevel_quotenode(x, s), ast.args))
end end
return ast if toplevel_quotenode(ast, :q)
end) exit()
elseif toplevel_quotenode(ast, :r)
# Restart Julia with :r
pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing})
if Meta.isexpr(ast, :toplevel, 2) && ast.args[2] === QuoteNode(:r)
argv = Base.julia_cmd().exec argv = Base.julia_cmd().exec
opts = Base.JLOptions() opts = Base.JLOptions()
if opts.project != C_NULL if opts.project != C_NULL
@ -25,25 +23,27 @@ if Base.isinteractive() &&
return ast return ast
end) end)
# Automatically load Debugger.jl when encountering @enter or @run and BenchmarkTools.jl # Automatically load tooling on demand:
# when encountering @btime or @benchmark. # - Debugger.jl when encountering @enter or @run
# - BenchmarkTools.jl when encountering @btime or @benchmark
pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing}) pushfirst!(REPL.repl_ast_transforms, function(ast::Union{Expr,Nothing})
contains_macro(_, _) = false function contains_macro(ast, m)
function contains_macro(x::Expr, s::Symbol) return ast isa Expr && (
(Meta.isexpr(x, :macrocall) && x.args[1] === s) || (Meta.isexpr(ast, :macrocall) && ast.args[1] === m) ||
any(y -> contains_macro(y, s), x.args) any(x -> contains_macro(x, m), ast.args)
end )
if Meta.isexpr(ast, :toplevel, 2) && end
!isdefined(Main, :Debugger) && if !isdefined(Main, :Debugger) && (
(contains_macro(ast, Symbol("@enter")) || contains_macro(ast, Symbol("@run"))) contains_macro(ast, Symbol("@enter")) || contains_macro(ast, Symbol("@run"))
)
@info "Loading Debugger ..." @info "Loading Debugger ..."
try try
Core.eval(Main, :(using Debugger)) Core.eval(Main, :(using Debugger))
catch catch
end end
elseif Meta.isexpr(ast, :toplevel, 2) && elseif !isdefined(Main, :BenchmarkTools) && (
!isdefined(Main, :BenchmarkTools) && contains_macro(ast, Symbol("@btime")) || contains_macro(ast, Symbol("@benchmark"))
(contains_macro(ast, Symbol("@btime")) || contains_macro(ast, Symbol("@benchmark"))) )
@info "Loading BenchmarkTools ..." @info "Loading BenchmarkTools ..."
try try
Core.eval(Main, :(using BenchmarkTools)) Core.eval(Main, :(using BenchmarkTools))
@ -52,4 +52,5 @@ if Base.isinteractive() &&
end end
return ast return ast
end) end)
end end

Loading…
Cancel
Save