Permalink
Browse files

Complete instance variables and make sure that if some code wasn't co…

…mpleted it will never try to filter them.
  • Loading branch information...
alloy committed Jul 25, 2010
1 parent ee35b77 commit b9f196c93b8458c2fd3acf705934f8203a6d669b
Showing with 31 additions and 5 deletions.
  1. +19 −3 lib/irb/ext/completion.rb
  2. +12 −2 spec/ext/completion_spec.rb
View
@@ -59,6 +59,14 @@ def local_variables
evaluate('local_variables').map(&:to_s)
end
+ def instance_variables
+ context.object.instance_variables.map(&:to_s)
+ end
+
+ def global_variables
+ super.map(&:to_s)
+ end
+
def instance_methods
context.object.methods.map(&:to_s)
end
@@ -106,7 +114,11 @@ def results
end
if call
- format_methods(receiver, methods || methods_of_object(root), filter)
+ if results = (methods || methods_of_object(root))
+ format_methods(receiver, results, filter)
+ else
+ []
+ end
else
match_methods_vars_or_consts_in_scope(root)
end.sort.uniq
@@ -129,8 +141,10 @@ def match_methods_vars_or_consts_in_scope(symbol)
case var[TYPE]
when :@ident
local_variables + instance_methods + RESERVED_DOWNCASE_WORDS
+ when :@ivar
+ instance_variables
when :@gvar
- global_variables.map(&:to_s)
+ global_variables
when :@const
if symbol[TYPE] == :top_const_ref
filter = "::#{filter}"
@@ -174,8 +188,10 @@ def methods_of_object_in_variable(path)
case type
when :@ident
evaluate(name).methods if local_variables.include?(name)
+ when :@ivar
+ evaluate(name).methods if instance_variables.include?(name)
when :@gvar
- eval(name).methods if global_variables.include?(name.to_sym)
+ eval(name).methods if global_variables.include?(name)
when :@const
evaluate(name).methods if constants.include?(name)
end
@@ -58,6 +58,11 @@ def a_local_method; end
complete('foo.').should include('foo.singleton_method')
end
+ it "matches as an instance variable" do
+ @context.__evaluate__('@an_instance_variable = ::CompletionStub.new')
+ complete('@an_instance_variable.').should == imethods(::CompletionStub, '@an_instance_variable')
+ end
+
it "matches as a global variable" do
complete('$a_completion_stub.').should == imethods(::CompletionStub, '$a_completion_stub')
end
@@ -193,9 +198,10 @@ def a_local_method; end
end
it "filters the methods, of the variable receiver, by the given method name" do
- @context.__evaluate__('foo = ::CompletionStub.new')
+ @context.__evaluate__('foo = @an_instance_variable = ::CompletionStub.new')
complete('foo.an_im').should == %w{ foo.an_imethod }
complete('$a_completion_stub.an_im').should == %w{ $a_completion_stub.an_imethod }
+ complete('@an_instance_variable.an_im').should == %w{ @an_instance_variable.an_imethod }
# TODO: fix
# complete('CompletionStub.a_sing').should == %w{ CompletionStub.a_singleton_method }
end
@@ -204,7 +210,7 @@ def a_local_method; end
describe "when *not* doing a method call on an explicit receiver" do
before do
- @context.__evaluate__("a_local_variable = :ok")
+ @context.__evaluate__("a_local_variable = @an_instance_variable = :ok")
end
it "matches local variables" do
@@ -219,6 +225,10 @@ def a_local_method; end
complete("a_loc").should == %w{ a_local_method a_local_variable }
end
+ it "matches instance variables" do
+ complete("@an_inst").should == %w{ @an_instance_variable }
+ end
+
it "matches global variables" do
complete("$a_completion_s").should == %w{ $a_completion_stub }
end

0 comments on commit b9f196c

Please sign in to comment.