Skip to content

Commit

Permalink
Change to explicit method call in completion (#369)
Browse files Browse the repository at this point in the history
Ensure that methods are called even when local variables are defined.
see: #368
  • Loading branch information
osyo-manga authored Oct 2, 2022
1 parent a768b3b commit c34d54b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
36 changes: 29 additions & 7 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,29 @@

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
Expand Down Expand Up @@ -303,10 +325,10 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
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
Expand Down Expand Up @@ -356,17 +378,17 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace

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
Expand Down
47 changes: 46 additions & 1 deletion test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,56 @@ def test_complete_require_relative
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
Expand Down

0 comments on commit c34d54b

Please sign in to comment.