-
-
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
Dict completions in REPL #17017
Dict completions in REPL #17017
Conversation
@@ -381,12 +381,44 @@ function bslash_completions(string, pos) | |||
return (false, (String[], 0:-1, false)) | |||
end | |||
|
|||
function dict_identifier_key(str,tag) | |||
if tag == :string |
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.
wrong indent, should also use ===
97e7a94
to
54d5f49
Compare
Looks nice! Any chance this could work for any |
54d5f49
to
17da9f8
Compare
now works with all |
|
Yes, I know |
It seems somewhat unlikely to be a problem, but how long will this hang if somoene have a really large dictionary? Should there be a timeout? You should probably also look into how errors in |
if identifier != nothing | ||
matches = [] | ||
for key in eval(Main,:(collect(keys($identifier)))) | ||
startswith(repr(key),partial_key) && push!(matches,repr(key)) |
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.
Is the call to collect
really needed, and should we use a temporary variable to avoid calling repr(key)
twice?
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 think that we have generally tried to stay away from repl features that
can execute arbitrary code while you are editing. Could we avoid that by
supporting just a reasonable subset of key types?
On 20 Jun 2016 09:36, "Ivar Nesje" notifications@github.com wrote:
In base/REPLCompletions.jl
#17017 (comment):function completions(string, pos)
# First parse everything up to the current position
partial = string[1:pos]
inc_tag = Base.syntax_deprecation_warnings(false) do
Base.incomplete_tag(parse(partial, raise=false))
end
+
if completing a key in a Dict
- identifier, partial_key, loc = dict_identifier_key(partial,inc_tag)
- if identifier != nothing
matches = []
for key in eval(Main,:(collect(keys($identifier))))
startswith(repr(key),partial_key) && push!(matches,repr(key))
Is the call to collect really needed, and should we use a temporary
variable to avoid calling repr(key) twice?—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/JuliaLang/julia/pull/17017/files/17da9f809389e84c028c0404833323031740e7be#r67646639,
or mute the thread
https://github.com/notifications/unsubscribe/ABjTf9KSHjxCXeSCCqlCKunOGuGnIK6Rks5qNkMMgaJpZM4I5Ln-
.
17da9f8
to
99c96ed
Compare
happy to add a timeout. can someone suggest how to go about doing that? what specifically is the problem with using |
Timeout (based on time) should probably be implemented at another level in the REPL. I don't think that should be a requirement for this PR. The simple (partial) solution is to add a threshold (eg only look at the 1000 first keys), to limit the worst case runtime in case of big dictionaries. I would strongly consider restricting the key type to PS: |
i tested REPL responsiveness with Dicts of size up to 1e6. see code below. a bit slow but tolerable at 1e6. 1e5 was no problem at all.
it would be straightforward to have a limit beyond which tab completion was simply disabled. but does anyone actually ever use Dicts this big? |
Did you test various key types/lengths? It'd probably be good performance of huge strings keys as well. I think it'd be good to put some kind of length cutoff. |
I don't know, but if they do, a warning is better than a hang. PS: your code should probably read |
1e6 strings of length 32 is also tolerable. |
99c96ed
to
11928d7
Compare
code now includes a 1e6 limit on Dict completions. |
partial_key = str[begin_of_key:end] | ||
main_id = eval(Main,identifier) | ||
typeof(main_id) <: Associative && length(main_id)<1e6 || return (identifier, nothing, nothing) | ||
identifier, partial_key, begin_of_key |
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.
Should this return main_id
so you only have to call eval
once? (See the call for keys($identifier)
below.)
Just got a vote for this functionality, so I searched and found this PR. Nice work, @bjarthur. Aside from the issue I commented on, this seems merge-worthy to me. Any objections? |
11928d7
to
d3f95ee
Compare
duplicate |
begin_of_key = findnext(x->!in(x,whitespace_chars), str, end_of_indentifier+2) | ||
begin_of_key==0 && return (identifier, nothing, nothing) | ||
partial_key = str[begin_of_key:end] | ||
main_id = eval(Main,identifier) |
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.
could this be getfield?
last |
I think this looks good, and is usefull functionality. I found this minor nit while testing completions of string keys: julia> a = Dict("1234"=>1)
Dict{String,Int64} with 1 entry:
"1234"=>1
julia> a["1{\tab}"]
#turns into
julia> a["1234"]"]
# but I would expect
julia> a["1234"] PS: I'm still somewhat sceptical that this attempts to work for all key types (via |
i don't agree that this should be the expected behavior. if we're using as a counter example, what would you expect in the following case:
|
Yea, I guess it is the Anyway, the osx travis failiure seems unrelated and this can easily be reverted if it causes problems, so I'm going to merge now. |
i have found rapture with this hack. the REPL now detects if you're within the square brackets of a
Dict
, and tab completes based on the keys. it works for all key types, so long as you type therepr
of it.