2012-12-19 06:16:05 -05:00
|
|
|
class Pry
|
2013-05-17 20:23:01 -04:00
|
|
|
|
|
|
|
# This class is responsible for taking a string (identifying a
|
|
|
|
# command/class/method/etc) and returning the relevant type of object.
|
|
|
|
# For example, if the user looks up "show-source" then a
|
|
|
|
# `Pry::Command` will be returned. Alternatively, if the user passes in "Pry#repl" then
|
|
|
|
# a `Pry::Method` object will be returned.
|
|
|
|
#
|
|
|
|
# The `CodeObject.lookup` method is responsible for 1. figuring out what kind of
|
|
|
|
# object the user wants (applying precedence rules in doing so -- i.e methods
|
|
|
|
# get precedence over commands with the same name) and 2. Returning
|
|
|
|
# the appropriate object. If the user fails to provide a string
|
|
|
|
# identifer for the object (i.e they pass in `nil` or "") then the
|
|
|
|
# object looked up will be the 'current method' or 'current class'
|
|
|
|
# associated with the Binding.
|
|
|
|
#
|
|
|
|
# TODO: This class is a clusterfuck. We need a much more robust
|
|
|
|
# concept of what a "Code Object" really is. Currently
|
|
|
|
# commands/classes/candidates/methods and so on just share a very
|
|
|
|
# ill-defined interface.
|
2012-12-19 06:16:05 -05:00
|
|
|
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-05-17 20:23:01 -04:00
|
|
|
# when no paramter is given (i.e CodeObject.lookup(nil)), then we
|
|
|
|
# lookup the 'current object' from the binding.
|
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?
|
|
|
|
|
2013-05-17 20:23:01 -04:00
|
|
|
obj = 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
|
|
|
|
|
|
|
|
# respect the super level (i.e user might have specified a
|
|
|
|
# --super flag to show-source)
|
|
|
|
lookup_super(obj, super_level)
|
2012-12-19 16:48:14 -05:00
|
|
|
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
|
2013-05-03 17:33:38 -04:00
|
|
|
obj = case str
|
2013-09-06 17:58:12 -04:00
|
|
|
when /\S+\(\)\z/
|
|
|
|
Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
|
2013-05-17 20:23:01 -04:00
|
|
|
else
|
2013-09-06 17:58:12 -04:00
|
|
|
Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
|
2013-05-17 20:23:01 -04:00
|
|
|
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
|