2013-01-01 21:25:23 +01:00
|
|
|
require 'pry/commands/show_info'
|
2012-08-11 17:22:29 -07:00
|
|
|
|
2013-01-01 21:25:23 +01:00
|
|
|
class Pry
|
|
|
|
class Command::ShowSource < Command::ShowInfo
|
2012-12-25 23:35:17 +02:00
|
|
|
match 'show-source'
|
2012-08-11 18:26:59 -07:00
|
|
|
group 'Introspection'
|
2013-01-09 22:23:19 +02:00
|
|
|
description 'Show the source for a method or class.'
|
2012-08-11 17:22:29 -07:00
|
|
|
|
2013-01-09 22:23:19 +02:00
|
|
|
banner <<-'BANNER'
|
2014-03-29 22:07:33 +09:00
|
|
|
Usage: show-source [OPTIONS] [METH|CLASS]
|
2012-08-11 17:22:29 -07:00
|
|
|
Aliases: $, show-method
|
|
|
|
|
2013-01-09 22:23:19 +02:00
|
|
|
Show the source for a method or class. Tries instance methods first and then
|
|
|
|
methods by default.
|
2012-08-11 17:22:29 -07:00
|
|
|
|
2013-01-09 22:23:19 +02:00
|
|
|
show-source hi_method
|
|
|
|
show-source hi_method
|
|
|
|
show-source Pry#rep # source for Pry#rep method
|
|
|
|
show-source Pry # for Pry class
|
|
|
|
show-source Pry -a # for all Pry class definitions (all monkey patches)
|
2014-03-30 06:40:16 +02:00
|
|
|
show-source Pry.foo -e # for class of the return value of expression `Pry.foo`
|
2013-01-09 22:23:19 +02:00
|
|
|
show-source Pry --super # for superclass of Pry (Object class)
|
2012-08-11 17:22:29 -07:00
|
|
|
|
|
|
|
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
|
|
|
|
BANNER
|
|
|
|
|
2014-03-29 22:07:33 +09:00
|
|
|
def options(opt)
|
2014-03-30 06:55:15 +02:00
|
|
|
opt.on :e, :eval, "evaluate the command's argument as a ruby expression and show the class its return value"
|
2014-03-29 22:07:33 +09:00
|
|
|
super(opt)
|
2014-03-29 08:13:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
|
|
|
if opts.present?(:e)
|
|
|
|
obj = target.eval(args.first)
|
|
|
|
self.args = Array.new(1) { Module === obj ? obj.name : obj.class.name }
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2013-01-01 22:45:23 +01:00
|
|
|
# The source for code_object prepared for display.
|
|
|
|
def content_for(code_object)
|
|
|
|
Code.new(code_object.source, start_line_for(code_object)).
|
2014-07-20 17:31:02 -07:00
|
|
|
with_line_numbers(use_line_numbers?).highlighted
|
2012-12-25 13:47:33 +01:00
|
|
|
end
|
2012-08-11 17:22:29 -07:00
|
|
|
end
|
|
|
|
|
2012-12-25 23:35:17 +02:00
|
|
|
Pry::Commands.add_command(Pry::Command::ShowSource)
|
|
|
|
Pry::Commands.alias_command 'show-method', 'show-source'
|
|
|
|
Pry::Commands.alias_command '$', 'show-source'
|
2012-08-11 17:22:29 -07:00
|
|
|
end
|