Skip to content

add Revise support #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[weakdeps]
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

[extensions]
ReviseExt = "Revise"

[compat]
MacroTools = "0.5"
Pkg = "1"
Revise = "3"
TOML = "1"
julia = "1.9"
100 changes: 100 additions & 0 deletions ext/ReviseExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
module ReviseExt
using PlutoDevMacros.FromPackage: FromPackage, get_target_module, get_temp_module, should_log
using PlutoDevMacros.FromPackage.ReviseHelpers
using Revise: Revise, PkgData, pkgdatas, FileInfo, parse_source, wplock, srcfiles, has_writable_paths, CodeTracking, init_watching

# Helper function taken from code of `Revise.queue_includes!`
function track_file(file, mod; pkgdata)
modexsigs = parse_source(file, mod)
if modexsigs !== nothing
fname = relpath(file, pkgdata)
push!(pkgdata, fname => FileInfo(modexsigs))
end
end

# This will modify target extension data in Revise to allow correct revision. This is because the modexsigs will be evaluated in Main.ExtName, while we want them to evaluate in the specific extension module
function ReviseHelpers._watch_package_revise(pkgid::Base.PkgId)
pd = get(pkgdatas, pkgid, nothing)
if pd === nothing
@warn "Something is wrong. Could not find package data for extension $pkgid"
return
end
m = Base.maybe_root_module(pkgid)
is_dummy(_m) = parentmodule(_m) === Main && nameof(_m) === nameof(m)
for fileinfo in pd.fileinfos
modexsigs = fileinfo.modexsigs
# The first key should be associated to Main, we delete that
haskey(modexsigs, Main) && delete!(modexsigs, Main)
# Then we change the module associated to the other expressions
for (k, v) in modexsigs
is_dummy(k) || continue
# Delete the dummy
delete!(modexsigs, k)
# Add the exps to the correct module
modexsigs[m] = v
end
end
if has_writable_paths(pd)
init_watching(pd, srcfiles(pd))
end
return
end

# Add pkgdata to Revise for the specific package
function create_pkgdata(package_dict::Dict)
parent_module = get_temp_module()
m = get_target_module(package_dict)
id = Base.PkgId(m)
pkgdata = PkgData(id)
# We add the entrypoint
entry_point = package_dict["file"]
track_file(entry_point, parent_module; pkgdata)
# We also track the various included files
included_files = get(package_dict, "Included Files", String[])
for file in included_files
track_file(file, m; pkgdata)
end
return pkgdata
end

function ReviseHelpers._add_revise_data(d::Dict)
@lock wplock begin
# Create the PkgData
pkgdata = create_pkgdata(d)
id = pkgdata.info.id
# update CodeTracking, as in queue_includes!
CodeTracking._pkgfiles[id] = pkgdata.info
# We now start watching and put the pkgdata inside the Revise.pkgdatas dict
if has_writable_paths(pkgdata)
init_watching(pkgdata, srcfiles(pkgdata))
end
pkgdatas[id] = pkgdata
end
end

# This check if Revise is enough
function ReviseHelpers._should_reload_module(d::Dict)
should_reload = if !isempty(Revise.queue_errors)
# If we already have errors in the queue we just return true
true
else
redirect_f = should_log() ? (f, args...) -> f() : Base.redirect_stderr
# Otherwise we try a Revise.revise
redirect_f(Base.DevNull()) do
Revise.revise()
end
!isempty(Revise.queue_errors)
end
if should_reload
# We empty the queue and call revise
empty!(Revise.queue_errors)
Revise.revise()
end
return should_reload
end

# Enable Revise
function __init__()
ReviseHelpers.REVISE_LOADED[] = true
end
end
1 change: 1 addition & 0 deletions src/frompackage/FromPackage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module FromPackage
include("types.jl")
include("envcachegroup.jl")
include("helpers.jl")
include("revise_helpers.jl")
include("code_parsing.jl")
include("loading.jl")
include("input_parsing.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/frompackage/code_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function should_skip(loc, lines_to_skip)
skip = any(lines_to_skip) do lr
_inrange(loc, lr)
end
# skip && @info "Skipping $loc"
skip && should_log() && @info "Skipping $loc"
skip
end

Expand Down
Loading