-
Notifications
You must be signed in to change notification settings - Fork 119
Move REPL support into a package extension #978
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
JamesWrigley
wants to merge
3
commits into
timholy:master
Choose a base branch
from
JamesWrigley:repl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−107
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module DistributedExt | ||
| module ReviseDistributedExt | ||
|
|
||
| import Distributed: myid, workers, remotecall | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| module ReviseREPLExt | ||
|
|
||
| using REPL: REPL | ||
| using Base: PkgId | ||
| using Base.Meta: isexpr | ||
| using Revise: @warnpcfail, FileInfo, LoweredCodeUtils, ModuleExprsInfos, Revise, | ||
| instantiate_sigs!, is_quotenode_egal, parse_source!, pkgdatas, revise, revise_first, | ||
| revision_queue, unwrap | ||
|
|
||
| const original_repl_prefix = Ref{Union{String, Function, Nothing}}(nothing) | ||
|
|
||
| Revise.revise(::REPL.REPLBackend) = revise() | ||
|
|
||
| # Check if the active REPL backend is available | ||
| active_repl_backend_available() = isdefined(Base, :active_repl_backend) && Base.active_repl_backend !== nothing | ||
|
|
||
| function maybe_set_prompt_color_impl(color::Symbol) | ||
| if isdefined(Base, :active_repl) | ||
| repl = Base.active_repl | ||
| if isa(repl, REPL.LineEditREPL) | ||
| if color === :warn | ||
| # First save the original setting | ||
| if original_repl_prefix[] === nothing | ||
| original_repl_prefix[] = repl.mistate.current_mode.prompt_prefix | ||
| end | ||
| repl.mistate.current_mode.prompt_prefix = "\e[33m" # yellow | ||
| else | ||
| color = original_repl_prefix[] | ||
| color === nothing && return nothing | ||
| repl.mistate.current_mode.prompt_prefix = color | ||
| original_repl_prefix[] = nothing | ||
| end | ||
| end | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| function add_definitions_from_repl_impl(filename::String) | ||
| hist_idx = parse(Int, filename[6:end-1]) | ||
| hp = (Base.active_repl::REPL.LineEditREPL).interface.modes[1].hist::REPL.REPLHistoryProvider | ||
| src = hp.history[hp.start_idx+hist_idx] | ||
| id = PkgId(nothing, "@REPL") | ||
| pkgdata = pkgdatas[id] | ||
| mod_exs_infos = ModuleExprsInfos(Main::Module) | ||
| parse_source!(mod_exs_infos, src, filename, Main::Module) | ||
| instantiate_sigs!(mod_exs_infos) | ||
| fi = FileInfo(mod_exs_infos) | ||
| push!(pkgdata, filename=>fi) | ||
| return fi | ||
| end | ||
| add_definitions_from_repl_impl(filename::AbstractString) = add_definitions_from_repl_impl(convert(String, filename)::String) | ||
|
|
||
| # `revise_first` gets called by the REPL prior to executing the next command (by having been pushed | ||
| # onto the `ast_transform` list). | ||
| # This uses invokelatest not for reasons of world age but to ensure that the call is made at runtime. | ||
| # This allows `revise_first` to be compiled without compiling `revise` itself, and greatly | ||
| # reduces the overhead of using Revise. | ||
| function Revise.revise_first(ex) | ||
| # Special-case `exit()` (issue #562) | ||
| if isa(ex, Expr) | ||
| exu = unwrap(ex) | ||
| if isexpr(exu, :block, 2) | ||
| arg1 = exu.args[1] | ||
| if isexpr(arg1, :softscope) | ||
| exu = exu.args[2] | ||
| end | ||
| end | ||
| if isa(exu, Expr) | ||
| exu.head === :call && length(exu.args) == 1 && exu.args[1] === :exit && return ex | ||
| lhsrhs = LoweredCodeUtils.get_lhs_rhs(exu) | ||
| if lhsrhs !== nothing | ||
| lhs, _ = lhsrhs | ||
| if isexpr(lhs, :ref) && length(lhs.args) == 1 | ||
| arg1 = lhs.args[1] | ||
| isexpr(arg1, :(.), 2) && arg1.args[1] === :Revise && is_quotenode_egal(arg1.args[2], :active) && return ex | ||
| end | ||
| end | ||
| end | ||
| end | ||
| # Check for queued revisions, and if so call `revise` first before executing the expression | ||
| return Expr(:toplevel, :($isempty($revision_queue) || $(Base.invokelatest)($revise)), ex) | ||
| end | ||
|
|
||
| function __init__() | ||
| # Set REPL functions in Revise | ||
| Revise.maybe_set_prompt_color = maybe_set_prompt_color_impl | ||
| Revise.add_definitions_from_repl = add_definitions_from_repl_impl | ||
|
|
||
| if Revise.should_enable_revise() | ||
| pushfirst!(REPL.repl_ast_transforms, revise_first) | ||
| # #664: once a REPL is started, it no longer interacts with REPL.repl_ast_transforms | ||
| if active_repl_backend_available() | ||
| push!(Base.active_repl_backend.ast_transforms, revise_first) | ||
| else | ||
| # wait for active_repl_backend to exist | ||
| # #719: do this async in case Revise is being loaded from startup.jl | ||
| t = @async begin | ||
| iter = 0 | ||
| while !active_repl_backend_available() && iter < 20 | ||
| sleep(0.05) | ||
| iter += 1 | ||
| end | ||
| if active_repl_backend_available() | ||
| push!(Base.active_repl_backend.ast_transforms, revise_first) | ||
| end | ||
| end | ||
| errormonitor(t) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @warnpcfail precompile(active_repl_backend_available, ()) | ||
| @warnpcfail precompile(maybe_set_prompt_color_impl, (Symbol,)) | ||
| @warnpcfail precompile(add_definitions_from_repl_impl, (String,)) | ||
| @warnpcfail precompile(revise_first, (Expr,)) | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.