[macruby-changes] [4652] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:54:28 PDT 2010


Revision: 4652
          http://trac.macosforge.org/projects/ruby/changeset/4652
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:54:27 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Complete instance methods when Klass.new. or klass.new. is called

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:54:18 UTC (rev 4651)
+++ DietRB/trunk/lib/irb/ext/completion.rb	2010-10-08 10:54:27 UTC (rev 4652)
@@ -56,6 +56,10 @@
       @context.object.methods.map(&:to_s)
     end
     
+    def instance_methods_of(klass)
+      evaluate(klass).instance_methods
+    end
+    
     # TODO: test and or fix the fact that we need to get constants from the
     # singleton class.
     def constants
@@ -77,20 +81,41 @@
         
         # [:call, [:hash, nil], :".", [:@ident, x, …]]
         if root[TYPE] == :call
-          call     = true
-          filter   = root[CALLEE][VALUE]
-          receiver = source[0..-(filter.length + 2)]
-          root     = root[VALUE]
+          call  = true
+          stack = unwind_callstack(root)
+          # [[:var_ref, [:@const, "Klass", [1, 0]]], [:call, "new"]]
+          # [[:var_ref, [:@ident, "klass", [1, 0]]], [:call, "new"], [:call, "filter"]]
+          if stack[1][VALUE] == 'new'
+            klass    = stack[0][VALUE][VALUE]
+            filter   = stack[2][VALUE] if stack[2]
+            receiver = "#{klass}.new"
+            methods  = instance_methods_of(klass)
+          else
+            filter   = root[CALLEE][VALUE]
+            filter   = stack[1][VALUE]
+            receiver = source[0..-(filter.length + 2)]
+            root     = root[VALUE]
+          end
         end
         
         if call
-          format_methods(receiver, methods_of_object(root), filter)
+          format_methods(receiver, methods || methods_of_object(root), filter)
         else
           match_methods_vars_or_consts_in_scope(root)
         end.sort.uniq
       end
     end
     
+    def unwind_callstack(root, stack = [])
+      if root[TYPE] == :call
+        stack.unshift [:call, root[CALLEE][VALUE]]
+        unwind_callstack(root[VALUE], stack)
+      else
+        stack.unshift root
+      end
+      stack
+    end
+    
     def match_methods_vars_or_consts_in_scope(symbol)
       var    = symbol[VALUE]
       filter = var[VALUE]

Modified: DietRB/trunk/spec/completion_spec.rb
===================================================================
--- DietRB/trunk/spec/completion_spec.rb	2010-10-08 10:54:18 UTC (rev 4651)
+++ DietRB/trunk/spec/completion_spec.rb	2010-10-08 10:54:27 UTC (rev 4652)
@@ -171,6 +171,15 @@
           complete('-100_000_000_000_000_000_000.0.').should == imethods(Float, '-100_000_000_000_000_000_000.0')
         end
       end
+      
+      it "returns *all* public instance methods of the class (the receiver) that ::new is called on" do
+        complete("Playground.new.").should == imethods(Playground, 'Playground.new')
+        complete("Playground.new.a_local_m").should == %w{ Playground.new.a_local_method }
+        
+        @context.__evaluate__("klass = Playground")
+        complete("klass.new.").should == imethods(Playground, 'klass.new')
+        complete("klass.new.a_local_m").should == %w{ klass.new.a_local_method }
+      end
     end
     
     describe "and the source does *not* end with a period," do
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/e671c149/attachment-0001.html>


More information about the macruby-changes mailing list