Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tompng
Copy link
Member

@tompng tompng commented Feb 13, 2025

Local variable can be a keyword. Fix a bug that IRB doesn't work in this case.

irb(main):001> {if:1} => {if:}
=> nil
irb(main):002> 1+2
irb(main):003> 
irb(main):004> (Can't submit input)

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.

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.
@tompng tompng force-pushed the ignore_keyword_local_vars branch from 7a901a0 to d6be3b9 Compare February 13, 2025 11:14
@@ -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)
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the above.

@tompng tompng added the bug Something isn't working label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

2 participants