mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/irb] Change to explicit method call in completion (https://github.com/ruby/irb/pull/369)
Ensure that methods are called even when local variables are defined. see: https://github.com/ruby/irb/issues/368 https://github.com/ruby/irb/commit/c34d54b8bb
This commit is contained in:
parent
5cab9d0ee2
commit
7b88ffb34f
2 changed files with 75 additions and 8 deletions
|
@ -11,7 +11,29 @@ require_relative 'ruby-lex'
|
|||
|
||||
module IRB
|
||||
module InputCompletor # :nodoc:
|
||||
using Module.new {
|
||||
refine ::Binding do
|
||||
def eval_methods
|
||||
::Kernel.instance_method(:methods).bind(eval("self")).call
|
||||
end
|
||||
|
||||
def eval_private_methods
|
||||
::Kernel.instance_method(:private_methods).bind(eval("self")).call
|
||||
end
|
||||
|
||||
def eval_instance_variables
|
||||
::Kernel.instance_method(:instance_variables).bind(eval("self")).call
|
||||
end
|
||||
|
||||
def eval_global_variables
|
||||
::Kernel.instance_method(:global_variables).bind(eval("self")).call
|
||||
end
|
||||
|
||||
def eval_class_constants
|
||||
::Module.instance_method(:constants).bind(eval("self.class")).call
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
# Set of reserved words used by Ruby, you should not use these for
|
||||
# constants or variables
|
||||
|
@ -303,10 +325,10 @@ module IRB
|
|||
sep = $2
|
||||
message = $3
|
||||
|
||||
gv = eval("global_variables", bind).collect{|m| m.to_s}.push("true", "false", "nil")
|
||||
lv = eval("local_variables", bind).collect{|m| m.to_s}
|
||||
iv = eval("instance_variables", bind).collect{|m| m.to_s}
|
||||
cv = eval("self.class.constants", bind).collect{|m| m.to_s}
|
||||
gv = bind.eval_global_variables.collect{|m| m.to_s}.push("true", "false", "nil")
|
||||
lv = bind.local_variables.collect{|m| m.to_s}
|
||||
iv = bind.eval_instance_variables.collect{|m| m.to_s}
|
||||
cv = bind.eval_class_constants.collect{|m| m.to_s}
|
||||
|
||||
if (gv | lv | iv | cv).include?(receiver) or /^[A-Z]/ =~ receiver && /\./ !~ receiver
|
||||
# foo.func and foo is var. OR
|
||||
|
@ -356,17 +378,17 @@ module IRB
|
|||
|
||||
else
|
||||
if doc_namespace
|
||||
vars = eval("local_variables | instance_variables", bind).collect{|m| m.to_s}
|
||||
vars = (bind.local_variables | bind.eval_instance_variables).collect{|m| m.to_s}
|
||||
perfect_match_var = vars.find{|m| m.to_s == input}
|
||||
if perfect_match_var
|
||||
eval("#{perfect_match_var}.class.name", bind)
|
||||
else
|
||||
candidates = eval("methods | private_methods | local_variables | instance_variables | self.class.constants", bind).collect{|m| m.to_s}
|
||||
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.find{ |i| i == input }
|
||||
end
|
||||
else
|
||||
candidates = eval("methods | private_methods | local_variables | instance_variables | self.class.constants", bind).collect{|m| m.to_s}
|
||||
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.grep(/^#{Regexp.quote(input)}/)
|
||||
end
|
||||
|
|
|
@ -152,11 +152,56 @@ module TestIRB
|
|||
end
|
||||
|
||||
def test_complete_variable
|
||||
# Bug fix issues https://github.com/ruby/irb/issues/368
|
||||
# Variables other than `str_example` and `@str_example` are defined to ensure that irb completion does not cause unintended behavior
|
||||
str_example = ''
|
||||
str_example.clear # suppress "assigned but unused variable" warning
|
||||
@str_example = ''
|
||||
private_methods = ''
|
||||
methods = ''
|
||||
global_variables = ''
|
||||
local_variables = ''
|
||||
instance_variables = ''
|
||||
|
||||
# suppress "assigned but unused variable" warning
|
||||
str_example.clear
|
||||
@str_example.clear
|
||||
private_methods.clear
|
||||
methods.clear
|
||||
global_variables.clear
|
||||
local_variables.clear
|
||||
instance_variables.clear
|
||||
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("str_examp", bind: binding), "str_example")
|
||||
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example", bind: binding, doc_namespace: true), "String")
|
||||
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
|
||||
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("@str_examp", bind: binding), "@str_example")
|
||||
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example", bind: binding, doc_namespace: true), "String")
|
||||
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
|
||||
end
|
||||
|
||||
def test_complete_methods
|
||||
obj = Object.new
|
||||
obj.singleton_class.class_eval {
|
||||
def public_hoge; end
|
||||
private def private_hoge; end
|
||||
|
||||
# Support for overriding #methods etc.
|
||||
def methods; end
|
||||
def private_methods; end
|
||||
def global_variables; end
|
||||
def local_variables; end
|
||||
def instance_variables; end
|
||||
}
|
||||
bind = obj.instance_exec { binding }
|
||||
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("public_hog", bind: bind), "public_hoge")
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("public_hoge.to_s", bind: bind), "public_hoge.to_s")
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("public_hoge", bind: bind, doc_namespace: true), "public_hoge")
|
||||
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("private_hog", bind: bind), "private_hoge")
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("private_hoge.to_s", bind: bind), "private_hoge.to_s")
|
||||
assert_include(IRB::InputCompletor.retrieve_completion_data("private_hoge", bind: bind, doc_namespace: true), "private_hoge")
|
||||
end
|
||||
|
||||
def test_complete_class_method
|
||||
|
|
Loading…
Reference in a new issue