-
Notifications
You must be signed in to change notification settings - Fork 120
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
Handle keyword local variables correctly #1085
base: master
Are you sure you want to change the base?
Conversation
Local variable can be a keyword. Example: `def f(if:0, and:0); binding.irb; end` IRB prepends local variable assignment code `a=_=nil;` to the input code but keyword local variables should be excluded.
7a901a0
to
d6be3b9
Compare
@@ -459,12 +439,12 @@ def retrieve_completion_data(input, bind:, doc_namespace:) | |||
eval("#{perfect_match_var}.class.name", bind) rescue nil | |||
else | |||
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s} | |||
candidates |= ReservedWords | |||
candidates |= RubyLex::RESERVED_WORDS.map(&:to_s) |
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 using name
will prevent recreating string objects every time this is called:
irb(main):002> :foo.name.object_id
=> 11536
irb(main):003> :foo.name.object_id
=> 11536
irb(main):004> :foo.name.object_id
=> 11536
irb(main):005> :foo.to_s.object_id
=> 15608
irb(main):006> :foo.to_s.object_id
=> 16096
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.
Symbol#name
is added in Ruby 3.0 and IRB supports >= 2.7
candidates.find{ |i| i == input } | ||
end | ||
else | ||
candidates = (bind.eval_methods | bind.eval_private_methods | bind.local_variables | bind.eval_instance_variables | bind.eval_class_constants).collect{|m| m.to_s} | ||
candidates |= ReservedWords | ||
candidates |= RubyLex::RESERVED_WORDS.map(&:to_s) |
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.
Same as the above.
Local variable can be a keyword. Fix a bug that IRB doesn't work in this case.
IRB prepends local variable assignment code
a=_=nil;
to the input code. (Because Ruby code is parsed differently depending on local variable existence)Prepending
if=_=nil;
cause bug in this case.