2012-12-19 06:16:05 -05:00
|
|
|
class Pry
|
|
|
|
class CodeObject
|
2012-12-25 07:47:33 -05:00
|
|
|
module Helpers
|
|
|
|
# we need this helper as some Pry::Method objects can wrap Procs
|
|
|
|
# @return [Boolean]
|
|
|
|
def real_method_object?
|
|
|
|
is_a?(::Method) || is_a?(::UnboundMethod)
|
|
|
|
end
|
|
|
|
|
|
|
|
def c_method?
|
|
|
|
real_method_object? && source_type == :c
|
|
|
|
end
|
|
|
|
|
|
|
|
def module_with_yard_docs?
|
|
|
|
is_a?(WrappedModule) && yard_docs?
|
|
|
|
end
|
|
|
|
|
|
|
|
def command?
|
|
|
|
is_a?(Module) && self <= Pry::Command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Pry::Helpers::CommandHelpers
|
2012-12-19 06:16:05 -05:00
|
|
|
|
|
|
|
class << self
|
2013-01-11 14:34:48 -05:00
|
|
|
def lookup(str, _pry_, options={})
|
|
|
|
co = new(str, _pry_, options)
|
2012-12-19 16:48:14 -05:00
|
|
|
|
|
|
|
co.default_lookup || co.method_or_class_lookup ||
|
2013-01-08 19:37:08 -05:00
|
|
|
co.command_lookup || co.empty_lookup
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :str
|
|
|
|
attr_accessor :target
|
|
|
|
attr_accessor :pry
|
|
|
|
attr_accessor :super_level
|
|
|
|
|
2013-01-11 14:34:48 -05:00
|
|
|
def initialize(str, _pry_, options={})
|
2012-12-19 06:16:05 -05:00
|
|
|
options = {
|
2013-01-08 19:37:08 -05:00
|
|
|
:super => 0,
|
2012-12-19 06:16:05 -05:00
|
|
|
}.merge!(options)
|
|
|
|
|
|
|
|
@str = str
|
|
|
|
@pry = _pry_
|
2013-01-11 14:34:48 -05:00
|
|
|
@target = _pry_.current_context
|
2012-12-19 06:16:05 -05:00
|
|
|
@super_level = options[:super]
|
|
|
|
end
|
|
|
|
|
2012-12-19 16:48:14 -05:00
|
|
|
def command_lookup
|
2012-12-20 11:44:29 -05:00
|
|
|
# TODO: just make it so find_command_by_match_or_listing doesn't
|
|
|
|
# raise?
|
|
|
|
pry.commands.find_command_by_match_or_listing(str) rescue nil
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
2013-01-08 19:37:08 -05:00
|
|
|
def empty_lookup
|
2012-12-19 16:48:14 -05:00
|
|
|
return nil if str && !str.empty?
|
|
|
|
|
|
|
|
if internal_binding?(target)
|
|
|
|
mod = target_self.is_a?(Module) ? target_self : target_self.class
|
|
|
|
Pry::WrappedModule(mod)
|
|
|
|
else
|
|
|
|
Pry::Method.from_binding(target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 15:17:07 -05:00
|
|
|
# lookup variables and constants and `self` that are not modules
|
2012-12-19 16:48:14 -05:00
|
|
|
def default_lookup
|
2013-01-05 18:16:44 -05:00
|
|
|
|
|
|
|
# we skip instance methods as we want those to fall through to method_or_class_lookup()
|
|
|
|
if safe_to_evaluate?(str) && !looks_like_an_instance_method?(str)
|
2012-12-19 06:16:05 -05:00
|
|
|
obj = target.eval(str)
|
|
|
|
|
2012-12-27 07:33:56 -05:00
|
|
|
# restrict to only objects we KNOW for sure support the full API
|
|
|
|
# Do NOT support just any object that responds to source_location
|
2013-01-01 20:06:51 -05:00
|
|
|
if sourcable_object?(obj)
|
2012-12-19 06:16:05 -05:00
|
|
|
Pry::Method(obj)
|
|
|
|
elsif !obj.is_a?(Module)
|
|
|
|
Pry::WrappedModule(obj.class)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue Pry::RescuableException
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-12-19 16:48:14 -05:00
|
|
|
def method_or_class_lookup
|
|
|
|
# we need this here because stupid Pry::Method.from_str() does a
|
|
|
|
# Pry::Method.from_binding when str is nil.
|
|
|
|
# Once we refactor Pry::Method.from_str() so it doesnt lookup
|
|
|
|
# from bindings, we can get rid of this check
|
2013-01-05 18:16:44 -05:00
|
|
|
return nil if str.to_s.empty?
|
2012-12-19 16:48:14 -05:00
|
|
|
|
2012-12-19 06:16:05 -05:00
|
|
|
obj = if str =~ /::(?:\S+)\Z/
|
|
|
|
Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
|
|
|
|
else
|
|
|
|
Pry::Method.from_str(str,target) || Pry::WrappedModule.from_str(str, target)
|
|
|
|
end
|
|
|
|
|
2013-01-01 20:06:51 -05:00
|
|
|
lookup_super(obj, super_level)
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
2012-12-19 16:48:14 -05:00
|
|
|
|
|
|
|
private
|
2013-01-01 20:06:51 -05:00
|
|
|
|
|
|
|
def sourcable_object?(obj)
|
|
|
|
[::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) }
|
|
|
|
end
|
|
|
|
|
2013-01-05 18:16:44 -05:00
|
|
|
|
|
|
|
# Returns true if `str` looks like a method, i.e Klass#method
|
|
|
|
# We need to consider this case because method lookups should fall
|
|
|
|
# through to the `method_or_class_lookup()` method but a
|
|
|
|
# defined?() on a "Klass#method` string will see the `#` as a
|
|
|
|
# comment and only evaluate the `Klass` part.
|
|
|
|
# @param [String] str
|
|
|
|
# @return [Boolean] Whether the string looks like an instance method.
|
|
|
|
def looks_like_an_instance_method?(str)
|
|
|
|
str =~ /\S#\S/
|
|
|
|
end
|
|
|
|
|
|
|
|
# We use this method to decide whether code is safe to eval. Method's are
|
|
|
|
# generally not, but everything else is.
|
|
|
|
# TODO: is just checking != "method" enough??
|
|
|
|
# TODO: see duplication of this method in Pry::WrappedModule
|
2013-01-01 20:06:51 -05:00
|
|
|
# @param [String] str The string to lookup
|
2013-01-05 18:16:44 -05:00
|
|
|
# @return [Boolean]
|
|
|
|
def safe_to_evaluate?(str)
|
|
|
|
return true if str.strip == "self"
|
|
|
|
kind = target.eval("defined?(#{str})")
|
|
|
|
kind =~ /variable|constant/
|
2013-01-01 20:06:51 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 16:48:14 -05:00
|
|
|
def target_self
|
|
|
|
target.eval('self')
|
|
|
|
end
|
2013-01-01 20:06:51 -05:00
|
|
|
|
|
|
|
# grab the nth (`super_level`) super of `obj
|
|
|
|
# @param [Object] obj
|
|
|
|
# @param [Fixnum] super_level How far up the super chain to ascend.
|
|
|
|
def lookup_super(obj, super_level)
|
|
|
|
return nil if !obj
|
|
|
|
|
|
|
|
sup = obj.super(super_level)
|
|
|
|
if !sup
|
|
|
|
raise Pry::CommandError, "No superclass found for #{obj.wrapped}"
|
|
|
|
else
|
|
|
|
sup
|
|
|
|
end
|
|
|
|
end
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
end
|