2013-01-01 15:25:23 -05:00
|
|
|
require 'pry/commands/show_info'
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2013-01-01 15:25:23 -05:00
|
|
|
class Pry
|
|
|
|
class Command::ShowSource < Command::ShowInfo
|
2012-12-25 16:35:17 -05:00
|
|
|
match 'show-source'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Introspection'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'Show the source for a method or class.'
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
banner <<-'BANNER'
|
2014-03-29 09:07:33 -04:00
|
|
|
Usage: show-source [OPTIONS] [METH|CLASS]
|
2012-08-11 20:22:29 -04:00
|
|
|
Aliases: $, show-method
|
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
Show the source for a method or class. Tries instance methods first and then
|
|
|
|
methods by default.
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2013-01-09 15:23:19 -05: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 00:40:16 -04:00
|
|
|
show-source Pry.foo -e # for class of the return value of expression `Pry.foo`
|
2013-01-09 15:23:19 -05:00
|
|
|
show-source Pry --super # for superclass of Pry (Object class)
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
|
|
|
|
BANNER
|
|
|
|
|
2014-03-29 09:07:33 -04:00
|
|
|
def options(opt)
|
2014-03-30 00:55:15 -04:00
|
|
|
opt.on :e, :eval, "evaluate the command's argument as a ruby expression and show the class its return value"
|
2014-03-29 09:07:33 -04:00
|
|
|
super(opt)
|
2014-03-29 03:13:22 -04: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 16:45:23 -05:00
|
|
|
# The source for code_object prepared for display.
|
|
|
|
def content_for(code_object)
|
2018-10-03 14:51:40 -04:00
|
|
|
code = Code.new(
|
|
|
|
code_object.source || [],
|
|
|
|
start_line_for(code_object)
|
|
|
|
)
|
|
|
|
code.with_line_numbers(use_line_numbers?).highlighted
|
2012-12-25 07:47:33 -05:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
2012-12-25 16:35:17 -05: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 20:22:29 -04:00
|
|
|
end
|