[macruby-changes] [4632] DietRB/trunk/lib/irb/ext/completion.rb

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:51:30 PDT 2010


Revision: 4632
          http://trac.macosforge.org/projects/ruby/changeset/4632
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:51:29 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Cleanup a bit.

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

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

Modified: DietRB/trunk/lib/irb/ext/completion.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/completion.rb	2010-10-08 10:51:20 UTC (rev 4631)
+++ DietRB/trunk/lib/irb/ext/completion.rb	2010-10-08 10:51:29 UTC (rev 4632)
@@ -24,8 +24,10 @@
       evaluate('local_variables').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').map(&:to_s)
+      evaluate('self.class.constants + (class << self; constants; end)').map(&:to_s)
     end
     
     def results
@@ -36,48 +38,54 @@
       src = src[0..-2] if call
       
       # p src, call
-      results = Ripper::SexpBuilder.new(src).parse
-      # p @source, results
+      tree = Ripper::SexpBuilder.new(src).parse
+      # p @source, tree
       
       # [:program, [:stmts_add, [:stmts_new], [x, …]]]
       #                                        ^
-      sub = results[1][2]
-      klass = case sub[0]
-      when :var_ref
-        type = sub[1][0]
-        name = sub[1][1]
-        if type == :@ident && local_variables.include?(name)
-          return evaluate(name).methods.map(&:to_s)
-        elsif type == :@const && constants.include?(name)
-          return evaluate(name).methods.map(&:to_s)
-        elsif type == :@gvar && global_variables.include?(name.to_sym)
-          return eval(name).methods.map(&:to_s)
-        end
-      when :top_const_ref
-        type = sub[1][0]
-        name = sub[1][1]
+      process_any(tree[1][2])
+    end
+    
+    def process_any(tree)
+      result = case tree[0]
+      # [:program, [:stmts_add, [:stmts_new], [:unary, :-@, [x, …]]]]
+      #                                                     ^
+      when :unary                          then process_any(tree[2])
+      when :var_ref, :top_const_ref        then process_variable(tree)
+      when :array, :words_add, :qwords_add then Array
+      when :@int                           then Fixnum
+      when :@float                         then Float
+      when :hash                           then Hash
+      when :lambda                         then Proc
+      when :dot2, :dot3                    then Range
+      when :regexp_literal                 then Regexp
+      when :string_literal                 then String
+      when :symbol_literal, :dyna_symbol   then Symbol
+      end
+      
+      if result
+        result = result.instance_methods if result.is_a?(Class)
+        result.map(&:to_s)
+      end
+    end
+    
+    def process_variable(tree)
+      type, name = tree[1][0..1]
+      
+      if tree[0] == :top_const_ref
         if type == :@const && Object.constants.include?(name.to_sym)
-          return evaluate("::#{name}").methods.map(&:to_s)
+          evaluate("::#{name}").methods
         end
-      when :regexp_literal then Regexp
-      when :array, :words_add, :qwords_add then Array
-      when :lambda then Proc
-      when :hash then Hash
-      when :symbol_literal, :dyna_symbol then Symbol
-      when :string_literal then String
-      when :dot2, :dot3 then Range
-      when :@int then Fixnum
-      when :@float then Float
-      when :unary
-        # [:program, [:stmts_add, [:stmts_new], [:unary, :-@, [x, …]]]]
-        #                                                      ^
-        case sub[2][0]
-        when :@int then Fixnum
-        when :@float then Float
+      else
+        case type
+        when :@ident
+          evaluate(name).methods if local_variables.include?(name)
+        when :@gvar
+          eval(name).methods if global_variables.include?(name.to_sym)
+        when :@const
+          evaluate(name).methods if constants.include?(name)
         end
       end
-      
-      klass.instance_methods.map(&:to_s) if klass
     end
   end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/d3003901/attachment.html>


More information about the macruby-changes mailing list