Module: VER::EvalCompleter

Defined in:
lib/ver/vendor/eval_completer.rb

Overview

Completion based on some code of IRB::InputCompleter

Copyright by Keiju ISHITSUKA <[email protected]>

and Michael Fellinger <[email protected]>

From Original Idea of [email protected]

Constant Summary collapse

ReservedWords =
%w[
  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
]
Operators =
%w[
  % & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ ! != !~
]
WordBreak =
/[\t\n"'`><=;|&{(]/

Class Method Summary collapse

Class Method Details

.complete(input, bind = TOPLEVEL_BINDING) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ver/vendor/eval_completer.rb', line 22

def complete(input, bind = TOPLEVEL_BINDING)
  case input
  when /^(\/[^\/]*\/)\.([^.]*)$/
    complete_regexp_method($1, Regexp.quote($2))
  when /^([^\]]*\])\.([^.]*)$/
    complete_array_method($1, Regexp.quote($2))
  when /^([^\}]*\})\.([^.]*)$/
    complete_proc_or_hash_method($1, Regexp.quote($2))
  when /^(:[^:.]*)$/
    complete_symbol(Regexp.quote($1))
  when /^::([A-Z][^:\.\(]*)$/
    complete_absolute_constant_or_class_method(Regexp.quote($1))
  when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/
    complete_constant_or_class_method(bind, $1, Regexp.quote($4))
  when /^(:[^:.]+)\.([^.]*)$/
    complete_symbol_method($1, Regexp.quote($2))
  when /^(-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE]-?[0-9]+)?)\.([^.]*)$/
    complete_numeric_method(bind, $1, Regexp.quote($5))
  when /^(-?0x[0-9a-fA-F_]+)\.([^.]*)$/
    complete_numeric_hex_method(bind, $1, Regexp.quote($2))
  when /^(\$[^.]*)$/
    complete_global_variable(Regexp.new(Regexp.quote($1)))
  when /^((\.?[^.]+)+)\.([^.]*)$/
    complete_variable(bind, $1, Regexp.quote($3))
  when /^\.([^.]*)$/
    complete_unknown(Regexp.quote($1))
  else
    complete_else(bind, Regexp.quote(input))
  end
end

.complete_absolute_constant_or_class_method(receiver) ⇒ Object



74
75
76
77
# File 'lib/ver/vendor/eval_completer.rb', line 74

def complete_absolute_constant_or_class_method(receiver)
  candidates = Object.constants
  candidates.grep(/^#{receiver}/).map{|name| "::#{name}" }
end

.complete_array_method(receiver, message) ⇒ Object



58
59
60
61
# File 'lib/ver/vendor/eval_completer.rb', line 58

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

.complete_constant_or_class_method(bind, receiver, message) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/ver/vendor/eval_completer.rb', line 79

def complete_constant_or_class_method(bind, receiver, message)
  begin
    candidates =  eval("#{receiver}.constants", bind)
    candidates |= eval("#{receiver}.methods", bind)
  rescue Exception
    candidates = [*candidates]
  end

  candidates.grep(/^#{message}/).map{|name| "#{receiver}::#{name}" }
end

.complete_else(bind, receiver) ⇒ Object



165
166
167
168
169
170
# File 'lib/ver/vendor/eval_completer.rb', line 165

def complete_else(bind, receiver)
  code = "methods | private_methods | local_variables | self.class.constants"
  candidates = eval(code, bind)

  (candidates|ReservedWords).grep(/^#{receiver}/).map(&:to_s)
end

.complete_global_variable(regmessage) ⇒ Object



116
117
118
# File 'lib/ver/vendor/eval_completer.rb', line 116

def complete_global_variable(regmessage)
  global_variables.grep(regmessage).map(&:to_s)
end

.complete_numeric_hex_method(bind, receiver, message) ⇒ Object

Numeric(0xFFFF)



106
107
108
109
110
111
112
113
114
# File 'lib/ver/vendor/eval_completer.rb', line 106

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

  select_message(receiver, message, candidates)
end

.complete_numeric_method(bind, receiver, message) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/ver/vendor/eval_completer.rb', line 95

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

  select_message(receiver, message, candidates)
end

.complete_proc_or_hash_method(receiver, message) ⇒ Object



63
64
65
66
67
# File 'lib/ver/vendor/eval_completer.rb', line 63

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

.complete_regexp_method(receiver, message) ⇒ Object



53
54
55
56
# File 'lib/ver/vendor/eval_completer.rb', line 53

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

.complete_symbol(sym) ⇒ Object



69
70
71
72
# File 'lib/ver/vendor/eval_completer.rb', line 69

def complete_symbol(sym)
  candidates = Symbol.all_symbols.map(&:inspect)
  candidates.grep(/^#{sym}/)
end

.complete_symbol_method(receiver, message) ⇒ Object



90
91
92
93
# File 'lib/ver/vendor/eval_completer.rb', line 90

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

.complete_unknown(message) ⇒ Object

unknown(maybe String)



158
159
160
161
162
163
# File 'lib/ver/vendor/eval_completer.rb', line 158

def complete_unknown(message)
  receiver = ""

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

.complete_variable(bind, receiver, message) ⇒ 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
# File 'lib/ver/vendor/eval_completer.rb', line 120

def complete_variable(bind, receiver, message)
  gv = eval("global_variables",     bind).map(&:to_s)
  lv = eval("local_variables",      bind).map(&:to_s)
  cv = eval("self.class.constants", bind).map(&:to_s)

  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) do |mod|
      begin
        name = mod.name
      rescue Exception
        name = ""
      end

      next if name != "IRB::Context" && /^(IRB|SLex|RubyLex|RubyToken)/ =~ name
      candidates.concat mod.instance_methods(false)
    end

    candidates.sort!
    candidates.uniq!
  end

  select_message(receiver, message, candidates)
end

.select_message(receiver, message, candidates) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ver/vendor/eval_completer.rb', line 172

def select_message(receiver, message, candidates)
  candidates.grep(/^#{message}/).map{|entity|
    case entity
    when /^[a-zA-Z_]/
      "#{receiver}.#{entity}"
    when /^[0-9]/
    when *Operators
      # "#{receiver} #{entity}"
    end
  }.compact
end