Class: Rink::LineProcessor::PureRuby

Inherits:
Base show all
Defined in:
lib/rink/line_processor/pure_ruby.rb

Overview

Performs autocompletion based on method names of objects used. This algorithm is identical to that of IRB.

Constant Summary collapse

OPERATORS =
["%", "&", "*", "**", "+",  "-",  "/", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
             "[]", "[]=", "^"
]
RESERVED_WORDS =
[ "BEGIN", "END", "alias", "and", "begin", "break", "case", "class", "def", "defined", "do",
                   "else", "elsif", "end", "ensure", "false", "for", "if", "in", "module", "next", "nil", "not",
                   "or", "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless",
                   "until", "when", "while", "yield"
]

Instance Attribute Summary

Attributes inherited from Base

#source

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Rink::LineProcessor::Base

Instance Method Details

#autocomplete(line, namespace) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rink/line_processor/pure_ruby.rb', line 120

def autocomplete(line, namespace)
  # Borrowed from irb/completion.rb
  result = case line
    when /^(\/[^\/]*\/)\.([^.]*)$/
      autocomplete_for_regexp($1, Regexp.quote($2))
      
    when /^([^\]]*\])\.([^.]*)$/
      autocomplete_for_array($1, Regexp.quote($2))

    when /^([^\}]*\})\.([^.]*)$/
      autocomplete_for_proc_or_hash($1, Regexp.quote($2))
	
    when /^(:[^:.]*)$/
      autocomplete_for_symbol($1)

    when /^::([A-Z][^:\.\(]*)$/
      autocomplete_for_absolute_constant_or_class_methods($1)

    when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
      autocomplete_for_constant_or_class_methods($1, Regexp.quote($4))

    when /^(:[^:.]+)\.([^.]*)$/
      autocomplete_for_symbol_method($1, Regexp.quote($2))

    when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
      autocomplete_for_numeric($1, Regexp.quote($5))

    when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
      autocomplete_for_hex_numeric($1, Regexp.quote($2))
    
    when /^(\$[^.]*)$/
      autocomplete_for_global_variable(Regexp.quote($1))
      
    #      when /^(\$?(\.?[^.]+)+)\.([^.]*)$/
    when /^((\.?[^.]+)+)\.([^.]*)$/
      autocomplete_for_variable($1, Regexp.quote($3))

    when /^\.([^.]*)$/
      # unknown(maybe String)
      autocomplete_for_string(Regexp.quote($1))
    else
      candidates = namespace.instance_eval { methods | private_methods | local_variables | self.class.constants }
      (candidates|RESERVED_WORDS).grep(/^#{Regexp.quote(line)}/)
  end
  result.kind_of?(Array) ? 
    result.collect { |r| r.kind_of?(String) ? r : r.to_s } :
    result.kind_of?(String) ? result : result.to_s
end

#autocomplete_for_absolute_constant_or_class_methods(receiver) ⇒ Object



39
40
41
42
# File 'lib/rink/line_processor/pure_ruby.rb', line 39

def autocomplete_for_absolute_constant_or_class_methods(receiver)
  candidates = Object.constants
  candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
end

#autocomplete_for_array(receiver, message) ⇒ Object



20
21
22
23
# File 'lib/rink/line_processor/pure_ruby.rb', line 20

def autocomplete_for_array(receiver, message)
  candidates = Array.instance_methods(true)
  select_message(receiver, message, candidates)
end

#autocomplete_for_constant_or_class_methods(receiver, message) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/rink/line_processor/pure_ruby.rb', line 44

def autocomplete_for_constant_or_class_methods(receiver, message)
  begin
    candidates = eval("#{receiver}.constants | #{receiver}.methods", bind)
  rescue Exception
    candidates = []
  end
  candidates.grep(/^#{message}/).collect{|e| receiver + "::" + e}
end

#autocomplete_for_global_variable(obj) ⇒ Object



76
77
78
# File 'lib/rink/line_processor/pure_ruby.rb', line 76

def autocomplete_for_global_variable(obj)
  global_variables.grep(Regexp.new(obj))
end

#autocomplete_for_hex_numeric(receiver, message) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/rink/line_processor/pure_ruby.rb', line 67

def autocomplete_for_hex_numeric(receiver, message)
  begin
    candidates = eval(receiver, bind).methods
  rescue Exception
    candidates = []
  end
  select_message(receiver, message, candidates)
end

#autocomplete_for_numeric(receiver, message) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/rink/line_processor/pure_ruby.rb', line 58

def autocomplete_for_numeric(receiver, message)
  begin
    candidates = eval(receiver, bind).methods
  rescue Exception
    candidates = []
  end
  select_message(receiver, message, candidates)
end

#autocomplete_for_proc_or_hash(receiver, message) ⇒ Object



25
26
27
28
# File 'lib/rink/line_processor/pure_ruby.rb', line 25

def autocomplete_for_proc_or_hash(receiver, message)
  candidates = Proc.instance_methods(true) | Hash.instance_methods(true)
  select_message(receiver, message, candidates)
end

#autocomplete_for_regexp(receiver, message) ⇒ Object



15
16
17
18
# File 'lib/rink/line_processor/pure_ruby.rb', line 15

def autocomplete_for_regexp(receiver, message)
  candidates = Regexp.instance_methods(true)
  select_message(receiver, message, candidates)
end

#autocomplete_for_string(message) ⇒ Object



114
115
116
117
118
# File 'lib/rink/line_processor/pure_ruby.rb', line 114

def autocomplete_for_string(message)
  receiver = ""
  candidates = String.instance_methods(true)
  select_message(receiver, message, candidates)
end

#autocomplete_for_symbol(sym) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rink/line_processor/pure_ruby.rb', line 30

def autocomplete_for_symbol(sym)
  if Symbol.respond_to?(:all_symbols)
    candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
    candidates.grep(/^#{sym}/)
  else
    []
  end
end

#autocomplete_for_symbol_method(receiver, message) ⇒ Object



53
54
55
56
# File 'lib/rink/line_processor/pure_ruby.rb', line 53

def autocomplete_for_symbol_method(receiver, message)
  candidates = Symbol.instance_methods(true)
  select_message(receiver, message, candidates)
end

#autocomplete_for_variable(receiver, message) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rink/line_processor/pure_ruby.rb', line 80

def autocomplete_for_variable(receiver, message)
  gv = eval("global_variables", bind)
  lv = eval("local_variables", bind)
  cv = eval("self.class.constants", bind)
  
  if (gv | lv | cv).include?(receiver)
    # foo.func and foo is local var.
    candidates = eval("#{receiver}.methods", bind)
  elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
    # Foo::Bar.func
    begin
      candidates = eval("#{receiver}.methods", bind)
    rescue Exception
      candidates = []
    end
  else
    # func1.func2
    candidates = []
    ObjectSpace.each_object(Module){|m|
      begin
        name = m.name
      rescue Exception
        name = ""
      end
      next if name != "IRB::Context" and 
        /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
      candidates.concat m.instance_methods(false)
    }
    candidates.sort!
    candidates.uniq!
  end
  select_message(receiver, message, candidates)
end