[macruby-changes] [4637] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:52:16 PDT 2010


Revision: 4637
          http://trac.macosforge.org/projects/ruby/changeset/4637
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:52:15 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Match local methods, variables and constants.

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/lib/irb/ext/completion.rb
    DietRB/trunk/spec/completion_spec.rb

Modified: DietRB/trunk/lib/irb/ext/completion.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/completion.rb	2010-10-08 10:52:05 UTC (rev 4636)
+++ DietRB/trunk/lib/irb/ext/completion.rb	2010-10-08 10:52:15 UTC (rev 4637)
@@ -24,10 +24,14 @@
       evaluate('local_variables').map(&:to_s)
     end
     
+    def instance_methods
+      @context.object.methods.map(&:to_s)
+    end
+    
     # TODO: test and or fix the fact that we need to get constants from the
     # singleton class.
     def constants
-      evaluate('self.class.constants + (class << self; constants; end)').map(&:to_s)
+      evaluate('Object.constants + self.class.constants + (class << self; constants; end)').map(&:to_s)
     end
     
     def results
@@ -54,7 +58,9 @@
         if call
           methods = methods_of_object(sexp)
           format(receiver, methods, filter)
-        end
+        else
+          match_methods_vars_or_consts_in_scope(sexp)
+        end.sort
       end
     end
     
@@ -62,6 +68,23 @@
       (filter ? methods.grep(/^#{filter}/) : methods).map { |m| "#{receiver}.#{m}" }
     end
     
+    def match_methods_vars_or_consts_in_scope(sexp)
+      filter = sexp[1][1]
+      case sexp[1][0]
+      when :@ident
+        local_variables + instance_methods
+      when :@gvar
+        global_variables.map(&:to_s)
+      when :@const
+        if sexp[0] == :top_const_ref
+          filter = "::#{filter}"
+          Object.constants.map { |c| "::#{c}" }
+        else
+          constants
+        end
+      end.grep(/^#{Regexp.quote(filter)}/)
+    end
+    
     def methods_of_object(sexp)
       result = case sexp[0]
       # [:unary, :-@, [x, …]]

Modified: DietRB/trunk/spec/completion_spec.rb
===================================================================
--- DietRB/trunk/spec/completion_spec.rb	2010-10-08 10:52:05 UTC (rev 4636)
+++ DietRB/trunk/spec/completion_spec.rb	2010-10-08 10:52:15 UTC (rev 4637)
@@ -7,21 +7,15 @@
   end
   
   def imethods(klass, receiver = nil)
-    klass.instance_methods.map { |m| [receiver, m.to_s].compact.join('.') }
+    klass.instance_methods.map { |m| [receiver, m.to_s].compact.join('.') }.sort
   end
   
   def methods(object, receiver = nil)
-    object.methods.map { |m| [receiver, m.to_s].compact.join('.') }
+    object.methods.map { |m| [receiver, m.to_s].compact.join('.') }.sort
   end
 end
 
-describe "IRB::Completion" do
-  extend CompletionHelper
-  
-  it "quacks like a Proc" do
-    IRB::Completion.call('//.').should == imethods(Regexp, '//')
-  end
-end
+Bacon::Context.send(:include, CompletionHelper)
 
 class CompletionStub
   def self.a_cmethod
@@ -40,160 +34,188 @@
 
 $a_completion_stub = CompletionStub.new
 
-describe "IRB::Completion, when the source ends with a period, " do
-  describe "returns all public methods of the object that" do
-    extend CompletionHelper
-    
-    before do
-      @context = IRB::Context.new(Playground.new)
+describe "IRB::Completion" do
+  before do
+    @context = IRB::Context.new(Playground.new)
+  end
+  
+  it "quacks like a Proc" do
+    IRB::Completion.call('//.').should == imethods(Regexp, '//')
+  end
+  
+  describe "when doing a method call on an explicit receiver," do
+    describe "and the source ends with a period," do
+      describe "returns *all* public methods of the receiver that" do
+        it "matches as a local variable" do
+          @context.__evaluate__('foo = ::CompletionStub.new')
+          complete('foo.').should == imethods(::CompletionStub, 'foo')
+          
+          @context.__evaluate__('def foo.singleton_method; end')
+          complete('foo.').should.include('foo.singleton_method')
+        end
+        
+        it "matches as a global variable" do
+          complete('$a_completion_stub.').should == imethods(::CompletionStub, '$a_completion_stub')
+        end
+        
+        # TODO: fix
+        # it "matches as a local constant" do
+        #   complete('CompletionStub.').should == methods(Playground::CompletionStub)
+        # end
+        
+        it "matches as a top level constant" do
+          complete('::CompletionStub.').should == methods(::CompletionStub, '::CompletionStub')
+        end
+      end
+      
+      describe "returns *all* public instance methods of the class (the receiver) that" do
+        it "matches as a Regexp literal" do
+          complete('//.').should == imethods(Regexp, '//')
+          complete('/^(:[^:.]+)\.([^.]*)$/.').should == imethods(Regexp, '/^(:[^:.]+)\.([^.]*)$/')
+          complete('/^(#{oops})\.([^.]*)$/.').should == imethods(Regexp, '/^(#{oops})\.([^.]*)$/')
+          complete('%r{/foo/.*/bar}.').should == imethods(Regexp, '%r{/foo/.*/bar}')
+        end
+        
+        it "matches as an Array literal" do
+          complete('[].').should == imethods(Array, '[]')
+          complete('[:ok, {}, "foo",].').should == imethods(Array, '[:ok, {}, "foo",]')
+          complete('[*foo].').should == imethods(Array, '[*foo]')
+          complete('%w{foo}.').should == imethods(Array, '%w{foo}')
+          complete('%W{#{:foo}}.').should == imethods(Array, '%W{#{:foo}}')
+        end
+        
+        # fails on MacRuby
+        it "matches as a lambda literal" do
+          complete('->{}.').should == imethods(Proc, '->{}')
+          complete('->{x=:ok}.').should == imethods(Proc, '->{x=:ok}')
+          complete('->(x){x=:ok}.').should == imethods(Proc, '->(x){x=:ok}')
+        end
+        
+        it "matches as a Hash literal" do
+          complete('{}.').should == imethods(Hash, '{}')
+          complete('{:foo=>:bar,}.').should == imethods(Hash, '{:foo=>:bar,}')
+          complete('{foo:"bar"}.').should == imethods(Hash, '{foo:"bar"}')
+        end
+        
+        it "matches as a Symbol literal" do
+          complete(':foo.').should == imethods(Symbol, ':foo')
+          complete(':"foo.bar".').should == imethods(Symbol, ':"foo.bar"')
+          complete(':"foo.#{"bar"}".').should == imethods(Symbol, ':"foo.#{"bar"}"')
+          complete(':\'foo.#{"bar"}\'.').should == imethods(Symbol, ':\'foo.#{"bar"}\'')
+          complete('%s{foo.bar}.').should == imethods(Symbol, '%s{foo.bar}')
+        end
+        
+        it "matches as a String literal" do
+          complete("'foo\\'bar'.").should == imethods(String, "'foo\\'bar'")
+          complete('"foo\"bar".').should == imethods(String, '"foo\"bar"')
+          complete('"foo#{"bar"}".').should == imethods(String, '"foo#{"bar"}"')
+          complete('%{foobar}.').should == imethods(String, '%{foobar}')
+          complete('%q{foo#{:bar}}.').should == imethods(String, '%q{foo#{:bar}}')
+          complete('%Q{foo#{:bar}}.').should == imethods(String, '%Q{foo#{:bar}}')
+        end
+        
+        it "matches as a Range literal" do
+          complete('1..10.').should == imethods(Range, '1..10')
+          complete('1...10.').should == imethods(Range, '1...10')
+          complete('"a".."z".').should == imethods(Range, '"a".."z"')
+          complete('"a"..."z".').should == imethods(Range, '"a"..."z"')
+        end
+        
+        it "matches as a Fixnum literal" do
+          complete('42.').should == imethods(Fixnum, '42')
+          complete('+42.').should == imethods(Fixnum, '+42')
+          complete('-42.').should == imethods(Fixnum, '-42')
+          complete('42_000.').should == imethods(Fixnum, '42_000')
+        end
+        
+        it "matches as a Bignum literal as a Fixnum" do
+          complete('100_000_000_000_000_000_000.').should == imethods(Fixnum, '100_000_000_000_000_000_000')
+          complete('-100_000_000_000_000_000_000.').should == imethods(Fixnum, '-100_000_000_000_000_000_000')
+          complete('+100_000_000_000_000_000_000.').should == imethods(Fixnum, '+100_000_000_000_000_000_000')
+        end
+        
+        it "matches as a Float with exponential literal" do
+          complete('1.2e-3.').should == imethods(Float, '1.2e-3')
+          complete('+1.2e-3.').should == imethods(Float, '+1.2e-3')
+          complete('-1.2e-3.').should == imethods(Float, '-1.2e-3')
+        end
+        
+        it "matches as a hex literal as a Fixnum" do
+          complete('0xffff.').should == imethods(Fixnum, '0xffff')
+          complete('+0xffff.').should == imethods(Fixnum, '+0xffff')
+          complete('-0xffff.').should == imethods(Fixnum, '-0xffff')
+        end
+        
+        it "matches as a binary literal as a Fixnum" do
+          complete('0b01011.').should == imethods(Fixnum, '0b01011')
+          complete('-0b01011.').should == imethods(Fixnum, '-0b01011')
+          complete('+0b01011.').should == imethods(Fixnum, '+0b01011')
+        end
+        
+        it "matches as an octal literal as a Fixnum" do
+          complete('0377.').should == imethods(Fixnum, '0377')
+          complete('-0377.').should == imethods(Fixnum, '-0377')
+          complete('+0377.').should == imethods(Fixnum, '+0377')
+        end
+        
+        it "matches as a Float literal" do
+          complete('42.0.').should == imethods(Float, '42.0')
+          complete('-42.0.').should == imethods(Float, '-42.0')
+          complete('+42.0.').should == imethods(Float, '+42.0')
+          complete('42_000.0.').should == imethods(Float, '42_000.0')
+        end
+        
+        it "matches as a Bignum float literal as a Float" do
+          complete('100_000_000_000_000_000_000.0.').should == imethods(Float, '100_000_000_000_000_000_000.0')
+          complete('+100_000_000_000_000_000_000.0.').should == imethods(Float, '+100_000_000_000_000_000_000.0')
+          complete('-100_000_000_000_000_000_000.0.').should == imethods(Float, '-100_000_000_000_000_000_000.0')
+        end
+      end
     end
     
-    it "matches as a local variable" do
-      @context.__evaluate__('foo = ::CompletionStub.new')
-      complete('foo.').should == imethods(::CompletionStub, 'foo')
+    describe "and the source does *not* end with a period," do
+      it "filters the methods, of the literal receiver, by the given method name" do
+        complete('//.nam').should == %w{ //.named_captures //.names }
+        complete('//.named').should == %w{ //.named_captures }
+      end
       
-      @context.__evaluate__('def foo.singleton_method; end')
-      complete('foo.').should.include('foo.singleton_method')
+      it "filters the methods, of the variable receiver, by the given method name" do
+        @context.__evaluate__('foo = ::CompletionStub.new')
+        complete('foo.an_im').should == %w{ foo.an_imethod }
+        complete('$a_completion_stub.an_im').should == %w{ $a_completion_stub.an_imethod }
+        # TODO: fix
+        # complete('CompletionStub.a_sing').should == %w{ CompletionStub.a_singleton_method }
+      end
     end
-    
-    it "matches as a global variable" do
-      complete('$a_completion_stub.').should == imethods(::CompletionStub, '$a_completion_stub')
-    end
-    
-    # TODO: fix
-    # it "matches as a local constant" do
-    #   complete('CompletionStub.').should == methods(Playground::CompletionStub)
-    # end
-    
-    it "matches as a top level constant" do
-      complete('::CompletionStub.').should == methods(::CompletionStub, '::CompletionStub')
-    end
   end
   
-  describe "returns all public instance methods of the class that" do
-    extend CompletionHelper
-    
-    it "matches as a Regexp literal" do
-      complete('//.').should == imethods(Regexp, '//')
-      complete('/^(:[^:.]+)\.([^.]*)$/.').should == imethods(Regexp, '/^(:[^:.]+)\.([^.]*)$/')
-      complete('/^(#{oops})\.([^.]*)$/.').should == imethods(Regexp, '/^(#{oops})\.([^.]*)$/')
-      complete('%r{/foo/.*/bar}.').should == imethods(Regexp, '%r{/foo/.*/bar}')
+  describe "when *not* doing a method call on an explicit receiver" do
+    before do
+      @context.__evaluate__("a_local_variable = :ok")
     end
     
-    it "matches as an Array literal" do
-      complete('[].').should == imethods(Array, '[]')
-      complete('[:ok, {}, "foo",].').should == imethods(Array, '[:ok, {}, "foo",]')
-      complete('[*foo].').should == imethods(Array, '[*foo]')
-      complete('%w{foo}.').should == imethods(Array, '%w{foo}')
-      complete('%W{#{:foo}}.').should == imethods(Array, '%W{#{:foo}}')
+    it "matches local variables" do
+      complete("a_local_v").should == %w{ a_local_variable }
     end
     
-    # fails on MacRuby
-    it "matches as a lambda literal" do
-      complete('->{}.').should == imethods(Proc, '->{}')
-      complete('->{x=:ok}.').should == imethods(Proc, '->{x=:ok}')
-      complete('->(x){x=:ok}.').should == imethods(Proc, '->(x){x=:ok}')
+    it "matches instance methods of the context object" do
+      complete("a_local_m").should == %w{ a_local_method }
     end
     
-    it "matches as a Hash literal" do
-      complete('{}.').should == imethods(Hash, '{}')
-      complete('{:foo=>:bar,}.').should == imethods(Hash, '{:foo=>:bar,}')
-      complete('{foo:"bar"}.').should == imethods(Hash, '{foo:"bar"}')
+    it "matches local variables and instance method of the context object" do
+      complete("a_loc").should == %w{ a_local_method a_local_variable }
     end
     
-    it "matches as a Symbol literal" do
-      complete(':foo.').should == imethods(Symbol, ':foo')
-      complete(':"foo.bar".').should == imethods(Symbol, ':"foo.bar"')
-      complete(':"foo.#{"bar"}".').should == imethods(Symbol, ':"foo.#{"bar"}"')
-      complete(':\'foo.#{"bar"}\'.').should == imethods(Symbol, ':\'foo.#{"bar"}\'')
-      complete('%s{foo.bar}.').should == imethods(Symbol, '%s{foo.bar}')
+    it "matches global variables" do
+      complete("$a_completion_s").should == %w{ $a_completion_stub }
     end
     
-    it "matches as a String literal" do
-      complete("'foo\\'bar'.").should == imethods(String, "'foo\\'bar'")
-      complete('"foo\"bar".').should == imethods(String, '"foo\"bar"')
-      complete('"foo#{"bar"}".').should == imethods(String, '"foo#{"bar"}"')
-      complete('%{foobar}.').should == imethods(String, '%{foobar}')
-      complete('%q{foo#{:bar}}.').should == imethods(String, '%q{foo#{:bar}}')
-      complete('%Q{foo#{:bar}}.').should == imethods(String, '%Q{foo#{:bar}}')
+    it "matches constants" do
+      complete("Playgr").should == %w{ Playground }
     end
     
-    it "matches as a Range literal" do
-      complete('1..10.').should == imethods(Range, '1..10')
-      complete('1...10.').should == imethods(Range, '1...10')
-      complete('"a".."z".').should == imethods(Range, '"a".."z"')
-      complete('"a"..."z".').should == imethods(Range, '"a"..."z"')
+    it "matches top level constants" do
+      complete("::CompletionSt").should == %w{ ::CompletionStub }
     end
-    
-    it "matches as a Fixnum literal" do
-      complete('42.').should == imethods(Fixnum, '42')
-      complete('+42.').should == imethods(Fixnum, '+42')
-      complete('-42.').should == imethods(Fixnum, '-42')
-      complete('42_000.').should == imethods(Fixnum, '42_000')
-    end
-    
-    it "matches as a Bignum literal as a Fixnum" do
-      complete('100_000_000_000_000_000_000.').should == imethods(Fixnum, '100_000_000_000_000_000_000')
-      complete('-100_000_000_000_000_000_000.').should == imethods(Fixnum, '-100_000_000_000_000_000_000')
-      complete('+100_000_000_000_000_000_000.').should == imethods(Fixnum, '+100_000_000_000_000_000_000')
-    end
-    
-    it "matches as a Float with exponential literal" do
-      complete('1.2e-3.').should == imethods(Float, '1.2e-3')
-      complete('+1.2e-3.').should == imethods(Float, '+1.2e-3')
-      complete('-1.2e-3.').should == imethods(Float, '-1.2e-3')
-    end
-    
-    it "matches as a hex literal as a Fixnum" do
-      complete('0xffff.').should == imethods(Fixnum, '0xffff')
-      complete('+0xffff.').should == imethods(Fixnum, '+0xffff')
-      complete('-0xffff.').should == imethods(Fixnum, '-0xffff')
-    end
-    
-    it "matches as a binary literal as a Fixnum" do
-      complete('0b01011.').should == imethods(Fixnum, '0b01011')
-      complete('-0b01011.').should == imethods(Fixnum, '-0b01011')
-      complete('+0b01011.').should == imethods(Fixnum, '+0b01011')
-    end
-    
-    it "matches as an octal literal as a Fixnum" do
-      complete('0377.').should == imethods(Fixnum, '0377')
-      complete('-0377.').should == imethods(Fixnum, '-0377')
-      complete('+0377.').should == imethods(Fixnum, '+0377')
-    end
-    
-    it "matches as a Float literal" do
-      complete('42.0.').should == imethods(Float, '42.0')
-      complete('-42.0.').should == imethods(Float, '-42.0')
-      complete('+42.0.').should == imethods(Float, '+42.0')
-      complete('42_000.0.').should == imethods(Float, '42_000.0')
-    end
-    
-    it "matches as a Bignum float literal as a Float" do
-      complete('100_000_000_000_000_000_000.0.').should == imethods(Float, '100_000_000_000_000_000_000.0')
-      complete('+100_000_000_000_000_000_000.0.').should == imethods(Float, '+100_000_000_000_000_000_000.0')
-      complete('-100_000_000_000_000_000_000.0.').should == imethods(Float, '-100_000_000_000_000_000_000.0')
-    end
   end
-end
-
-describe "IRB::Completion, when the source does not end with a period" do
-  extend CompletionHelper
-  
-  before do
-    @context = IRB::Context.new(Playground.new)
-  end
-  
-  it "filters the methods of a literal by the called method name" do
-    complete('//.nam').should == %w{ //.names //.named_captures }
-    complete('//.named').should == %w{ //.named_captures }
-  end
-  
-  it "filters the methods of an object, reference by variable, by the called method name" do
-    @context.__evaluate__('foo = ::CompletionStub.new')
-    complete('foo.an_im').should == %w{ foo.an_imethod }
-    complete('$a_completion_stub.an_im').should == %w{ $a_completion_stub.an_imethod }
-    # TODO: fix
-    # complete('CompletionStub.a_sing').should == %w{ CompletionStub.a_singleton_method }
-  end
 end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/aad9bb03/attachment-0001.html>


More information about the macruby-changes mailing list