Skip to content

Instantly share code, notes, and snippets.

@aplavin
Last active March 1, 2024 11:51
Show Gist options
  • Save aplavin/cb39121225954e37b6fe8efeebfb6404 to your computer and use it in GitHub Desktop.
Save aplavin/cb39121225954e37b6fe8efeebfb6404 to your computer and use it in GitHub Desktop.
insert!(LOAD_PATH, 2, mktempdir())
if Base.isinteractive() &&
(local REPL = get(Base.loaded_modules, Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"), nothing); REPL !== nothing)
# Automatically load tooling on demand:
local tooling_dict = Dict{Symbol,Vector{Symbol}}(
# dev tools:
:BenchmarkTools => Symbol.(["@btime", "@benchmark"]),
:Chairmarks => Symbol.(["@b", "@be"]),
:Cthulhu => Symbol.(["@descend", "@descend_code_typed", "@descend_code_warntype"]),
# :ProfileCanvas => [Symbol("@profview")], # vscode automatically provides it
# :TestEnv => [:TestEnv], # only macros and functions supported for now
# everything else:
:Accessors => Symbol.(["@optic", "@o", "@set", "@modify"]),
:ConstructionBase => [:constructorof, :getproperties, :getfields, :setproperties],
:DataPipes => Symbol.(["@p"]),
:DataManipulation => [:flatmap, :filtermap, :mapview, :group, :groupview, :groupmap, :findonly, :filteronly, :filterfirst, :uniqueonly],
:Dates => [:Date, :Time, :DateTime],
:DateFormats => [:mjd, :julian_day, :yeardecimal, :*ₜ, :/ₜ],
:Dictionaries => [:Dictionary, :dictionary],
# :DirectionalStatistics => [:Circular],
:FlexiJoins => [:innerjoin, :leftjoin, :rightjoin, :outerjoin],
:IntervalSets => [:.., :Interval, Symbol("@iv_str")],
:LinearAlgebra => [:dot, :norm, :normalize, :Symmetric, :Diagonal, :eigen, :eigvals, :eigvecs],
:StaticArrays => [:SVector, Symbol("@SVector")],
:Statistics => [:mean, :median, :std, :cor, :cov, :quantile],
:StatsBase => [:geomean, :harmmean, :percentile, :weights],
:StructArrays => [:StructArray],
:TraceFuns => [Symbol("@trace")],
:Unitful => [Symbol("@u_str"), :ustrip, :upreferred],
# :VOTables => [:VOTables],
:VirtualObservatory => [:TAPService, :VizierCatalog],
# :VLBIData => [:VLBI, :VLBIData],
)
pushfirst!(REPL.repl_ast_transforms, function (ast::Union{Expr,Nothing})
contains_calls(ast, ms::Vector{Symbol}, res=Symbol[]) = res
function contains_calls(ast::Expr, ms::Vector{Symbol}, res=Symbol[])
if Meta.isexpr(ast, [:macrocall, :call]) && first(ast.args) ∈ ms
push!(res, first(ast.args))
end
for x in ast.args
contains_calls(x, ms, res)
end
return res
end
for (mod, callables) in tooling_dict
isdefined(Main, mod) && continue
calls = contains_calls(ast, callables)
filter!(c -> !isdefined(Main, c), calls)
isempty(calls) && continue
@info "Loading $mod for $(join(calls, ", ")) ..."
try
if isnothing(Base.identify_package(String(mod)))
for f in REPL.install_packages_hooks
Base.invokelatest(f, [mod]) && break
end
end
Core.eval(Main, :(using $mod))
catch err
@info "Failed to automatically install and load $mod" exception = err
end
end
return ast
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment