-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
WIP: allow changing context module in REPL #33102
WIP: allow changing context module in REPL #33102
Conversation
1f1e0b2
to
bb6c268
Compare
This is already implemented by calling REPLCompletions.completions(string, pos, context_module=Main) julia/stdlib/REPL/src/REPLCompletions.jl Line 573 in 8093a7c
See it in use in IJulia: But you mean that you want to be able to set that module globally for a REPL? |
Thanks for the quick reply. |
It would generally be useful to be able to set the REPL module (which Juno already allows you to do), both for execution and completions. |
It would make more sense, and I'm willing to implement that if it sounds good to the other maintainers as well. AFAIU the current Juno's approach to change module context for REPL execution couldn't be applied to REPL completions (or could be done in a dirty way with lots of copy and paste in a same way as https://github.com/JunoLab/Atom.jl/blob/master/src/Atom.jl#L22), thus I think it would be better to be implemented here. |
AFAICT you only need to change this line plus some display logic below to use |
Thanks for the pointers. Piping modules through @KristofferC |
bb6c268
to
705a4f2
Compare
Before proceeding to write tests, I would really like to hear your comments on those updates: #33102 (comment) |
203abc1
to
01419e1
Compare
@pfitzseb @KristofferC @StefanKarpinski |
stdlib/REPL/src/docview.jl
Outdated
@@ -229,7 +229,8 @@ repl_search(s) = repl_search(stdout, s) | |||
|
|||
function repl_corrections(io::IO, s) | |||
print(io, "Couldn't find ") | |||
printstyled(io, s, '\n', color=:cyan) | |||
pre = context_module == Main ? [] : [context_module, '.'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I tried to mimic the behaviour introduced in #23806. It's not exactly same, but kind of consistent IMO.
@@ -1032,3 +1032,50 @@ fake_repl() do stdin_write, stdout_read, repl | |||
wait(repltask) | |||
@test istaskdone(repltask) | |||
end | |||
|
|||
# context_module changing #33102 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm feeling like I want to add more tests here, and would really appreciate if you give me your ideas to make this test more effective.
4d1d9aa
to
57fe646
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this looks good in general, but a couple of things to think about:
- The whole
context_module
global is a bit messy as is -- I think it would be a bit cleaner (and much more complex) to only use the global in the frontend and put the current module intoREPLDisplay
andREPLCompletionProvider
, so that those don't rely on any globals. Haven't thought this through in detail, so it might a) not be much better and b) infeasible ;) - It would be cool to have a minimal REPL mode that allows changing the current module without weird
Main.Base.set_context_module
invocations (which would be necessary in abaremodule
). That mode could also display the current module. - Should we change the
julia>
prompt to show the current module? Or change its color when you're not inMain
? I feel like people are going to be confused without some indication that you're not in the default module.
stdlib/REPL/src/REPL.jl
Outdated
@@ -59,6 +59,11 @@ answer_color(::AbstractREPL) = "" | |||
|
|||
const JULIA_PROMPT = "julia> " | |||
|
|||
context_module = Main |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const context_module = Ref{Module}(Main)
seems better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely, I just pushed the commit to use Ref
for context_module
:)
Note that we have this global object to hold options: Lines 263 to 311 in a9d4eac
Perhaps the |
So the reason why it's dangerous to have |
57fe646
to
0cccfcf
Compare
@pfitzseb, thanks for your thoughtful review !
Really agree with this idea, but
So "only using the global in the frontend" would mean to handle
Yeah, sounds nice to me.
Would be cool as well. |
If we are okay with disabling the context module changing within |
I think this is superseded by #33872. |
Sorry for having started #33872 without checking (or finding) that there was already another attempt... |
UPDATED
After a discussion, I decided to add a functionality to change the REPL context module for execution, completion, and docs, in this PR
How works
The context module can be set via calling
REPL.set_context_module(mod)
, and then execution/completion/show docs/display are all done in the context ofmod
TODO
eval_user_input
has been changed and it appears in some docs like stack trace, and profiling.[The original description]
Purpose
Allow REPL [tab] completions in a module context other than
Main
.Description
Currently REPL's [tab] completions work only in the context of the
Main
module, but there are cases when we want to get completions in a context of the other specific module:E.g.: Juno can change REPL's module so that an entered code gets evaluated in a context of that module, and in this case we may want to get completions in the context of the module as well.
With this PR, each REPL provider could call
REPL.set_context_module(mod)
hooking on its module change, and then can get the corresponding completions.If this idea is acceptable, I would really like to add tests for that, of course.
/cc @pfitzseb