2012-08-12 00:22:29 +00:00
|
|
|
class Pry
|
|
|
|
Pry::Commands.create_command "show-source" do
|
|
|
|
extend Pry::Helpers::BaseHelpers
|
|
|
|
|
2012-08-12 01:26:59 +00:00
|
|
|
group 'Introspection'
|
2012-08-12 00:22:29 +00:00
|
|
|
description "Show the source for a method or class. Aliases: $, show-method"
|
|
|
|
|
|
|
|
banner <<-BANNER
|
|
|
|
Usage: show-source [OPTIONS] [METH|CLASS]
|
|
|
|
Aliases: $, show-method
|
|
|
|
|
|
|
|
Show the source for a method or class. Tries instance methods first and then methods by default.
|
|
|
|
|
|
|
|
e.g: `show-source hello_method`
|
|
|
|
e.g: `show-source -m hello_method`
|
|
|
|
e.g: `show-source Pry#rep` # source for Pry#rep method
|
|
|
|
e.g: `show-source Pry` # source for Pry class
|
|
|
|
e.g: `show-source Pry -a` # source for all Pry class definitions (all monkey patches)
|
|
|
|
e.g: `show-source Pry --super # source for superclass of Pry (Object class)
|
|
|
|
|
|
|
|
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
options :shellwords => false
|
|
|
|
options :requires_gem => "ruby18_source_location" if mri_18?
|
|
|
|
|
|
|
|
def setup
|
|
|
|
require 'ruby18_source_location' if mri_18?
|
|
|
|
end
|
|
|
|
|
|
|
|
def options(opt)
|
2012-12-20 16:44:43 +00:00
|
|
|
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
|
2012-08-12 00:22:29 +00:00
|
|
|
opt.on :l, "line-numbers", "Show line numbers."
|
|
|
|
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
|
|
|
|
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
|
|
|
|
opt.on :a, :all, "Show source for all definitions and monkeypatches of the module/class"
|
|
|
|
end
|
|
|
|
|
2012-12-20 16:44:43 +00:00
|
|
|
def process
|
2012-12-25 12:47:33 +00:00
|
|
|
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
|
|
|
|
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
|
2012-12-20 16:44:43 +00:00
|
|
|
|
2012-12-25 12:47:33 +00:00
|
|
|
if show_source_for_all_modules?(code_object)
|
2012-12-20 19:46:33 +00:00
|
|
|
# show all monkey patches for a module
|
2012-12-25 12:47:33 +00:00
|
|
|
result = source_and_headers_for_all_module_candidates(code_object)
|
2012-12-20 19:15:00 +00:00
|
|
|
else
|
2012-12-20 19:46:33 +00:00
|
|
|
# show the source for a specific code object
|
2012-12-25 12:47:33 +00:00
|
|
|
result = source_and_header_for_code_object(code_object)
|
2012-12-20 16:44:43 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 12:47:33 +00:00
|
|
|
set_file_and_dir_locals(code_object.source_file)
|
2012-12-20 19:15:00 +00:00
|
|
|
stagger_output result
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 12:47:33 +00:00
|
|
|
def source_and_header_for_code_object(code_object)
|
|
|
|
result = header(code_object)
|
|
|
|
result << Code.new(code_object.source, start_line_for(code_object)).
|
|
|
|
with_line_numbers(use_line_numbers?).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_and_headers_for_all_module_candidates(mod)
|
|
|
|
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
|
|
|
|
mod.number_of_candidates.times do |v|
|
|
|
|
candidate = mod.candidate(v)
|
|
|
|
begin
|
|
|
|
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.file} @ line #{candidate.line}:\n"
|
|
|
|
code = Code.from_module(mod, start_line_for(candidate), v).with_line_numbers(use_line_numbers?).to_s
|
|
|
|
result << "Number of lines: #{code.lines.count}\n\n" << code
|
|
|
|
rescue Pry::RescuableException
|
|
|
|
result << "\nNo code found.\n"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
2012-12-20 19:46:33 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 12:47:33 +00:00
|
|
|
def show_source_for_all_modules?(code_object)
|
|
|
|
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
|
|
|
end
|
|
|
|
|
|
|
|
def obj_name
|
|
|
|
@obj_name ||= args.empty? ? nil : args.join(" ")
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 19:15:00 +00:00
|
|
|
# Generate a header (meta-data information) for all the code
|
|
|
|
# object types: methods, modules, commands, procs...
|
|
|
|
def header(code_object)
|
|
|
|
file_name, line_num = code_object.source_file, code_object.source_line
|
|
|
|
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
2012-12-25 12:47:33 +00:00
|
|
|
if code_object.c_method?
|
2012-12-20 19:15:00 +00:00
|
|
|
h << "(C Method):"
|
2012-08-12 00:22:29 +00:00
|
|
|
else
|
2012-12-20 19:15:00 +00:00
|
|
|
h << "@ line #{line_num}:"
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 12:47:33 +00:00
|
|
|
if code_object.real_method_object?
|
2012-12-20 19:15:00 +00:00
|
|
|
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
|
|
|
h << "#{text.bold("Visibility:")} #{code_object.visibility}"
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
2012-12-25 12:47:33 +00:00
|
|
|
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
|
|
|
|
"#{code_object.source.lines.count}\n\n"
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 19:15:00 +00:00
|
|
|
def start_line_for(code_object)
|
2012-08-12 00:22:29 +00:00
|
|
|
if opts.present?(:'base-one')
|
|
|
|
1
|
|
|
|
else
|
2012-12-20 19:15:00 +00:00
|
|
|
code_object.source_line || 1
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-20 19:15:00 +00:00
|
|
|
|
|
|
|
def use_line_numbers?
|
|
|
|
opts.present?(:b) || opts.present?(:l)
|
|
|
|
end
|
2012-12-21 15:57:19 +00:00
|
|
|
|
|
|
|
def complete(input)
|
|
|
|
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
|
|
|
|
prefix, search = [$1, $2]
|
|
|
|
methods = begin
|
|
|
|
Pry::Method.all_from_class(binding.eval(prefix))
|
|
|
|
rescue RescuableException => e
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
methods.map do |method|
|
|
|
|
[prefix, method.name].join('#') if method.name.start_with?(search)
|
|
|
|
end.compact
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Pry::Commands.alias_command "show-method", "show-source"
|
|
|
|
Pry::Commands.alias_command "$", "show-source"
|
|
|
|
end
|