class Yadriggy::SourceCode

@api private Retrieves source code in the S-expression style.

Public Class Methods

def_at?(line, t, prog) click to toggle source
# File lib/yadriggy/source_code.rb, line 128
def self.def_at?(line, t, prog)
  (t == :def || t == :defs) &&
    prog[1].is_a?(Array) && prog[1][0] == :@ident &&
      prog[1][2][0] == line
end
find_sexp(prog, line) click to toggle source
# File lib/yadriggy/source_code.rb, line 81
def self.find_sexp(prog, line)
  find_sexp2(prog, line, [1, nil])
end
find_sexp2(prog, line, current) click to toggle source

@param [Array] current the current location `[line, block]`

# File lib/yadriggy/source_code.rb, line 86
def self.find_sexp2(prog, line, current)
  if prog.nil? || !prog.is_a?(Array)
    return nil
  else
    t = prog[0]
    if t == :@ident || t == :@tstring_content || t == :@const ||
       t == :@int || t == :@float || t == :@kw || t == :@label ||
       t == :@gvar || t == :@CHAR
      #current[0] = prog[2][0]
      current_line = prog[2][0]
      if line < current_line && !current[1].nil?
        return current[1]
      else
        current[0] = current_line
        return nil
      end
    else
      is_block = (t == :brace_block || t == :do_block ||
                  t == :def || t == :defs || t == :lambda)
      if is_block && line == current[0] || def_at?(line, t, prog)
        return prog
      else
        current[1] = nil
        prog.each do |e|
          r = find_sexp2(e, line, current)
          return r unless r.nil?
        end
        if is_block
          if line <= current[0]
            return prog
          else
            current[1] = prog
            return nil
          end
        else
          nil
        end
      end
    end
  end
end
get_sexp(proc) click to toggle source

Gets an S-expression.

# File lib/yadriggy/source_code.rb, line 42
def self.get_sexp(proc)
  return nil unless proc.is_a?(Proc) || proc.is_a?(Method) ||
                    proc.is_a?(UnboundMethod)

  file_name, line = proc.source_location
  return nil if file_name.nil?
  src = if file_name == "(pry)" then read_pry_history
                                else File.read(file_name) end
  prog = Ripper.sexp(src)
  prog && [file_name, find_sexp(prog, line)]
end
max(a, b) click to toggle source
# File lib/yadriggy/source_code.rb, line 77
def self.max(a, b)
  if a > b then a else b end
end
min(a, b) click to toggle source
# File lib/yadriggy/source_code.rb, line 73
def self.min(a, b)
  if a < b then a else b end
end
read_pry_history() click to toggle source
# File lib/yadriggy/source_code.rb, line 60
def self.read_pry_history
  cmds = Pry.commands
  his = Pry.history.to_a[Pry.history.original_lines + @pry_offset ...
                         Pry.history.history_line_count]
  his.reduce("\n" * @pry_offset) do |source, line|
    if cmds.select {|k,v| v.matches?(line) }.empty?
      source << line << "\n"
    else
      source # << "\n"
    end
  end
end
reset_pry() click to toggle source
# File lib/yadriggy/source_code.rb, line 56
def self.reset_pry
  @pry_offset = Pry.history.history_line_count - Pry.history.original_lines
end