Class: Rcodetools::XMPCompletionFilter

Inherits:
XMPFilter
  • Object
show all
Includes:
ProcessParticularLine
Defined in:
lib/rcodetools/completion.rb

Overview

Nearly 100% accurate completion for any editors!!

by rubikitch <[email protected]>

Defined Under Namespace

Classes: NoCandidates

Constant Summary

Constants included from ProcessParticularLine

ProcessParticularLine::OPERATOR_CHARS

Constants inherited from XMPFilter

Rcodetools::XMPFilter::INITIALIZE_OPTS, Rcodetools::XMPFilter::INTERPRETER_FORK, Rcodetools::XMPFilter::INTERPRETER_RBTEST, Rcodetools::XMPFilter::INTERPRETER_RUBY, Rcodetools::XMPFilter::MARKER, Rcodetools::XMPFilter::MULTI_LINE_RE, Rcodetools::XMPFilter::SINGLE_LINE_RE, Rcodetools::XMPFilter::VAR, Rcodetools::XMPFilter::VERSION, Rcodetools::XMPFilter::WARNING_RE, Rcodetools::XMPFilter::XMP_RE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProcessParticularLine

#__magic_help_code, #_handle_brackets, #_handle_do_end, #_handle_keywords, #add_BEGIN, #aref_or_aset?, #current_phrase, #fill_literal!, #runtime_data, #runtime_data_with_class, #set_expr_and_postfix!, #set_last_word!

Methods inherited from XMPFilter

#add_markers, #annotate, #annotated_line, #annotated_multi_line, #common_path, #debugprint, detect_rbtest, #execute, #execute_popen, #execute_ruby, #execute_script, #execute_tmpfile, #extract_data, #final_decoration, #get_test_method_from_lineno, #initialize, #initialize_for_test_script, #initialize_rbtest, #initialize_rct_fork, #interpreter_command, #oneline_ize, #prepare_line_annotation, #windows?

Constructor Details

This class inherits a constructor from Rcodetools::XMPFilter

Class Attribute Details

.candidates_with_description_flagObject

Returns the value of attribute candidates_with_description_flag.



198
199
200
# File 'lib/rcodetools/completion.rb', line 198

def candidates_with_description_flag
  @candidates_with_description_flag
end

Instance Attribute Details

#prefixObject (readonly)

String completion begins with this.



203
204
205
# File 'lib/rcodetools/completion.rb', line 203

def prefix
  @prefix
end

Class Method Details

.run(code, opts) ⇒ Object



205
206
207
# File 'lib/rcodetools/completion.rb', line 205

def self.run(code, opts)
  new(opts).completion_code(code, opts[:lineno], opts[:column])
end

Instance Method Details

#___prepare_line(recv, all_completion_expr) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rcodetools/completion.rb', line 273

def ___prepare_line(recv, all_completion_expr)
  v = "#{VAR}"
  rcv = "#{VAR}_recv"
  idx = 1
  oneline_ize(<<EOC)
#{rcv} = (#{recv})
#{v} = (#{all_completion_expr}).map{|x| x.to_s}.grep(/^#{Regexp.quote(@prefix)}/)
#{rcv} = Module === #{rcv} ? #{rcv} : #{rcv}.class
$stderr.puts("#{MARKER}[#{idx}] => " + #{rcv}.to_s  + " " + #{v}.join(" ")) || #{v}
exit
EOC
end

#__prepare_line(recv, all_completion_expr, all_completion_expr_verbose) ⇒ Object



264
265
266
267
268
269
270
271
# File 'lib/rcodetools/completion.rb', line 264

def __prepare_line(recv, all_completion_expr, all_completion_expr_verbose)
  if self.class.candidates_with_description_flag
    ___prepare_line(recv, all_completion_expr_verbose.gsub(/%n/, '('+all_completion_expr+')'))
  else
    ___prepare_line(recv, all_completion_expr) 
  end

end

#candidates(code, lineno, column = nil) ⇒ Object



294
295
296
# File 'lib/rcodetools/completion.rb', line 294

def candidates(code, lineno, column=nil)
  candidates_with_class(code, lineno, column)[1]
end

#candidates_with_class(code, lineno, column = nil) ⇒ Object

Raises:



286
287
288
289
290
# File 'lib/rcodetools/completion.rb', line 286

def candidates_with_class(code, lineno, column=nil)
  klass, methods = runtime_data_with_class(code, lineno, column) rescue ["", ""]
  raise NoCandidates, "No candidates." if methods.nil? or methods.empty?
  [klass, methods.split(/ /).sort]
end

#completion_code(code, lineno, column = nil) ⇒ Object

Completion code for editors.



299
300
301
# File 'lib/rcodetools/completion.rb', line 299

def completion_code(code, lineno, column=nil)
  candidates(code, lineno, column).join("\n") rescue "\n"
end

#magic_help_code(recv, meth) ⇒ Object



209
210
211
# File 'lib/rcodetools/completion.rb', line 209

def magic_help_code(recv, meth)
  oneline_ize __magic_help_code("#{VAR}_result", recv, meth)
end

#methods_map_code(recv) ⇒ Object



213
214
215
216
217
218
# File 'lib/rcodetools/completion.rb', line 213

def methods_map_code(recv)
  # delimiter is \0
  m = "#{VAR}_m"
  mhc = magic_help_code((recv), m)
  %Q[map{|%s| "\#{%s}\\0" + %s}] % [m, m, mhc]
end

#prepare_line(expr, column) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rcodetools/completion.rb', line 224

def prepare_line(expr, column)
  set_expr_and_postfix!(expr, column){|c| /^.{#{c}}/ }
  @prefix = expr
  case expr
  when /^\$\w*$/              # global variable
    __prepare_line 'nil', 'global_variables', '%n'
  when /^@@\w*$/              # class variable
    __prepare_line 'nil', 'Module === self ? class_variables : self.class.class_variables', '%n'
  when /^@\w*$/               # instance variable
    __prepare_line 'nil', 'instance_variables', '%n'
  when /^([A-Z].*)::([^.]*)$/    # nested constants / class methods
    @prefix = $2
    __prepare_line $1, "#$1.constants | #$1.methods(true)",
    %Q[#$1.constants + #$1.methods(true).#{methods_map_code($1)}]
  when /^[A-Z]\w*$/           # normal constants
    __prepare_line 'nil', 'Module.constants', '%n'
  when /^(.*::.+)\.(.*)$/       # toplevel class methods
    @prefix = $2
    __prepare_line $1, "#$1.methods",
    %Q[%n.#{methods_map_code($1)}]
  when /^(::.+)::(.*)$/       # toplevel nested constants
    @prefix = $2
    __prepare_line $1, "#$1.constants | #$1.methods",
    %Q[#$1.constants + #$1.methods.#{methods_map_code($1)}]
  when /^::(.*)/              # toplevel constant
    @prefix = $1
    __prepare_line 'nil', 'Object.constants', '%n'
  when /^(:[^:.]*)$/          # symbol
    __prepare_line 'nil', 'Symbol.all_symbols.map{|s| ":" + s.id2name}', '%n'
  when /\.([^.]*)$/           # method call
    @prefix = $1
    recv = Regexp.last_match.pre_match
    __prepare_line recv, "(#{recv}).methods(true)",
    %Q[%n.#{methods_map_code(recv)}]
  else                        # bare words
    __prepare_line 'self', "methods | private_methods | local_variables | self.class.constants",
    %Q[(methods | private_methods).#{methods_map_code('self')} + local_variables | self.class.constants]
  end
end

#split_method_info(minfo) ⇒ Object



220
221
222
# File 'lib/rcodetools/completion.rb', line 220

def split_method_info(minfo)
  minfo.split(/\0/,2)
end