2011-07-27 11:21:53 +00:00
|
|
|
require 'tempfile'
|
|
|
|
|
2011-05-01 10:25:37 +00:00
|
|
|
class Pry
|
|
|
|
module DefaultCommands
|
|
|
|
|
2011-05-07 05:32:05 +00:00
|
|
|
Introspection = Pry::CommandSet.new do
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
command "show-method", "Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source" do |*args|
|
|
|
|
target = target()
|
|
|
|
|
2011-05-19 15:53:44 +00:00
|
|
|
opts = Slop.parse!(args) do |opt|
|
2011-09-10 20:22:47 +00:00
|
|
|
opt.banner unindent <<-USAGE
|
|
|
|
Usage: show-method [OPTIONS] [METH 1] [METH 2] [METH N]
|
|
|
|
Show the source for method METH. Tries instance methods first and then methods by default.
|
|
|
|
e.g: show-method hello_method
|
|
|
|
USAGE
|
2011-05-27 16:20:00 +00:00
|
|
|
|
2011-05-19 15:53:44 +00:00
|
|
|
opt.on :l, "line-numbers", "Show line numbers."
|
2011-06-16 13:49:31 +00:00
|
|
|
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
|
|
|
|
|
2011-05-19 15:53:44 +00:00
|
|
|
opt.on :M, "instance-methods", "Operate on instance methods."
|
|
|
|
opt.on :m, :methods, "Operate on methods."
|
|
|
|
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
|
|
|
|
opt.on :c, :context, "Select object context to run under.", true do |context|
|
2011-05-01 10:25:37 +00:00
|
|
|
target = Pry.binding_for(target.eval(context))
|
|
|
|
end
|
2011-05-19 15:53:44 +00:00
|
|
|
opt.on :h, :help, "This message." do
|
|
|
|
output.puts opt
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
next if opts.help?
|
2011-09-19 08:18:57 +00:00
|
|
|
opts[:instance] = opts['instance-methods'] if opts.m?
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-08-27 14:13:15 +00:00
|
|
|
args = [nil] if args.empty?
|
|
|
|
args.each do |method_name|
|
2011-10-01 23:57:39 +00:00
|
|
|
begin
|
|
|
|
meth = get_method_or_raise(method_name, target, opts.to_hash(true))
|
|
|
|
rescue CommandError => e
|
|
|
|
puts "\nError: #{e.message}"
|
2011-08-27 14:13:15 +00:00
|
|
|
next
|
|
|
|
end
|
2011-10-01 23:57:39 +00:00
|
|
|
next unless meth.source
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-09-25 05:13:01 +00:00
|
|
|
output.puts make_header(meth)
|
2011-08-27 14:13:15 +00:00
|
|
|
if Pry.color
|
2011-09-25 05:13:01 +00:00
|
|
|
code = CodeRay.scan(meth.source, meth.source_type).term
|
2011-09-19 08:18:57 +00:00
|
|
|
else
|
2011-09-25 05:13:01 +00:00
|
|
|
code = meth.source
|
2011-08-27 14:13:15 +00:00
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-08-27 14:13:15 +00:00
|
|
|
start_line = false
|
2011-09-19 08:18:57 +00:00
|
|
|
if opts.b?
|
|
|
|
start_line = 1
|
|
|
|
elsif opts.l?
|
2011-09-25 05:13:01 +00:00
|
|
|
start_line = meth.source_line || 1
|
2011-08-27 14:13:15 +00:00
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-08-27 14:13:15 +00:00
|
|
|
render_output(opts.flood?, start_line, code)
|
|
|
|
code
|
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2011-09-18 06:36:39 +00:00
|
|
|
alias_command "show-source", "show-method"
|
|
|
|
alias_command "$", "show-method"
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
command "show-command", "Show the source for CMD. Type `show-command --help` for more info." do |*args|
|
|
|
|
target = target()
|
|
|
|
|
2011-05-19 15:53:44 +00:00
|
|
|
opts = Slop.parse!(args) do |opt|
|
2011-09-10 20:22:47 +00:00
|
|
|
opt.banner unindent <<-USAGE
|
|
|
|
Usage: show-command [OPTIONS] [CMD]
|
|
|
|
Show the source for command CMD.
|
|
|
|
e.g: show-command show-method
|
|
|
|
USAGE
|
2011-05-19 15:53:44 +00:00
|
|
|
|
|
|
|
opt.on :l, "line-numbers", "Show line numbers."
|
|
|
|
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
|
|
|
|
opt.on :h, :help, "This message." do
|
|
|
|
output.puts opt
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-16 10:10:30 +00:00
|
|
|
next if opts.help?
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-05-16 10:10:30 +00:00
|
|
|
command_name = args.shift
|
2011-05-01 10:25:37 +00:00
|
|
|
if !command_name
|
2011-10-01 23:57:39 +00:00
|
|
|
raise CommandError, "You must provide a command name."
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 16:20:00 +00:00
|
|
|
if find_command(command_name)
|
2011-09-19 08:18:57 +00:00
|
|
|
block = Pry::Method.new(find_command(command_name).block)
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-09-19 08:18:57 +00:00
|
|
|
next unless block.source
|
|
|
|
set_file_and_dir_locals(block.source_file)
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-09-19 08:18:57 +00:00
|
|
|
output.puts make_header(block)
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
if Pry.color
|
2011-09-19 08:18:57 +00:00
|
|
|
code = CodeRay.scan(block.source, :ruby).term
|
|
|
|
else
|
|
|
|
code = block.source
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 16:20:00 +00:00
|
|
|
start_line = false
|
|
|
|
if opts.l?
|
2011-09-19 08:18:57 +00:00
|
|
|
start_line = block.source_line || 1
|
2011-05-27 16:20:00 +00:00
|
|
|
end
|
|
|
|
|
2011-09-19 08:18:57 +00:00
|
|
|
render_output(opts.flood?, opts.l? ? block.source_line : false, code)
|
2011-05-01 10:25:37 +00:00
|
|
|
code
|
|
|
|
else
|
2011-10-01 23:57:39 +00:00
|
|
|
raise CommandError, "No such command: #{command_name}."
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-05 04:14:24 +00:00
|
|
|
command "edit", "Invoke the default editor on a file. Type `edit --help` for more info" do |*args|
|
2011-06-15 13:50:09 +00:00
|
|
|
opts = Slop.parse!(args) do |opt|
|
2011-09-10 20:22:47 +00:00
|
|
|
opt.banner unindent <<-USAGE
|
2011-09-21 18:07:57 +00:00
|
|
|
Usage: edit [--no-reload|--reload] [--line LINE] [--temp|--ex|FILE[:LINE]|--in N]
|
2011-09-10 20:22:47 +00:00
|
|
|
Open a text editor. When no FILE is given, edits the pry input buffer.
|
|
|
|
Ensure #{text.bold("Pry.config.editor")} is set to your editor of choice.
|
|
|
|
e.g: edit sample.rb
|
|
|
|
USAGE
|
2011-09-10 19:16:25 +00:00
|
|
|
|
2011-09-12 17:07:05 +00:00
|
|
|
opt.on :e, :ex, "Open the file that raised the most recent exception (_ex_.file)", :optional => true, :as => Integer
|
2011-09-26 07:39:16 +00:00
|
|
|
opt.on :i, :in, "Open a temporary file containing the Nth line of _in_. N may be a range.", :optional => true, :as => Range, :default => -1..-1
|
2011-09-10 19:16:25 +00:00
|
|
|
opt.on :t, :temp, "Open an empty temporary file"
|
|
|
|
opt.on :l, :line, "Jump to this line in the opened file", true, :as => Integer
|
|
|
|
opt.on :n, :"no-reload", "Don't automatically reload the edited code"
|
|
|
|
opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
|
2011-06-15 13:50:09 +00:00
|
|
|
opt.on :h, :help, "This message." do
|
|
|
|
output.puts opt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next if opts.h?
|
|
|
|
|
2011-09-26 07:39:16 +00:00
|
|
|
if [opts.ex?, opts.t?, opts.in?, !args.empty?].count(true) > 1
|
2011-10-02 00:06:11 +00:00
|
|
|
raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
|
2011-09-10 19:16:25 +00:00
|
|
|
end
|
2011-07-26 17:08:27 +00:00
|
|
|
|
2011-09-10 19:16:25 +00:00
|
|
|
# edit of local code, eval'd within pry.
|
|
|
|
if !opts.ex? && args.empty?
|
2011-07-26 17:08:27 +00:00
|
|
|
|
2011-09-14 01:56:10 +00:00
|
|
|
content = if opts.t?
|
|
|
|
""
|
2011-09-21 18:07:57 +00:00
|
|
|
elsif opts.i?
|
2011-09-26 07:39:16 +00:00
|
|
|
case opts[:i]
|
|
|
|
when Range
|
|
|
|
(_pry_.input_array[opts[:i]] || []).join
|
|
|
|
when Fixnum
|
|
|
|
_pry_.input_array[opts[:i]] || ""
|
|
|
|
else
|
|
|
|
next output.puts "Not a valid range: #{opts[:i]}"
|
|
|
|
end
|
2011-09-14 01:56:10 +00:00
|
|
|
elsif eval_string.strip != ""
|
2011-09-10 19:16:25 +00:00
|
|
|
eval_string
|
2011-09-14 01:56:10 +00:00
|
|
|
else
|
|
|
|
_pry_.input_array.reverse_each.find{ |x| x && x.strip != "" } || ""
|
|
|
|
end
|
2011-09-01 03:47:29 +00:00
|
|
|
|
2011-09-10 19:16:25 +00:00
|
|
|
line = content.lines.count
|
2011-09-06 04:32:30 +00:00
|
|
|
|
2011-09-12 07:24:08 +00:00
|
|
|
temp_file do |f|
|
|
|
|
f.puts(content)
|
|
|
|
f.flush
|
|
|
|
invoke_editor(f.path, line)
|
|
|
|
if !opts.n?
|
|
|
|
silence_warnings do
|
|
|
|
eval_string.replace(File.read(f.path))
|
|
|
|
end
|
2011-09-10 06:43:06 +00:00
|
|
|
end
|
2011-09-08 02:17:41 +00:00
|
|
|
end
|
2011-09-10 19:16:25 +00:00
|
|
|
|
|
|
|
# edit of remote code, eval'd at top-level
|
2011-07-26 17:08:27 +00:00
|
|
|
else
|
2011-09-10 19:16:25 +00:00
|
|
|
if opts.ex?
|
2011-10-01 23:57:39 +00:00
|
|
|
if _pry_.last_exception.nil?
|
|
|
|
raise CommandError, "No exception found."
|
|
|
|
end
|
|
|
|
|
2011-09-12 17:07:05 +00:00
|
|
|
ex = _pry_.last_exception
|
|
|
|
bt_index = opts[:ex].to_i
|
2011-09-07 08:58:58 +00:00
|
|
|
|
2011-09-12 17:07:05 +00:00
|
|
|
ex_file, ex_line = ex.bt_source_location_for(bt_index)
|
2011-09-19 08:18:57 +00:00
|
|
|
if ex_file && RbxPath.is_core_path?(ex_file)
|
|
|
|
file_name = RbxPath.convert_path_to_full(ex_file)
|
2011-09-10 19:16:25 +00:00
|
|
|
else
|
2011-09-12 17:07:05 +00:00
|
|
|
file_name = ex_file
|
2011-09-10 19:16:25 +00:00
|
|
|
end
|
2011-06-15 13:50:09 +00:00
|
|
|
|
2011-09-12 17:07:05 +00:00
|
|
|
line = ex_line
|
2011-06-15 13:50:09 +00:00
|
|
|
|
2011-10-01 23:57:39 +00:00
|
|
|
if file_name.nil?
|
|
|
|
raise CommandError, "Exception has no associated file."
|
|
|
|
end
|
|
|
|
|
|
|
|
if Pry.eval_path == file_name
|
|
|
|
raise CommandError, "Cannot edit exceptions raised in REPL."
|
|
|
|
end
|
2011-09-10 19:16:25 +00:00
|
|
|
else
|
|
|
|
# break up into file:line
|
|
|
|
file_name = File.expand_path(args.first)
|
|
|
|
|
|
|
|
line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
|
2011-06-15 13:50:09 +00:00
|
|
|
end
|
2011-09-10 19:16:25 +00:00
|
|
|
|
|
|
|
line = opts[:l].to_i if opts.l?
|
|
|
|
|
|
|
|
invoke_editor(file_name, line)
|
|
|
|
set_file_and_dir_locals(file_name)
|
|
|
|
|
|
|
|
if opts.r? || ((opts.ex? || file_name.end_with?(".rb")) && !opts.n?)
|
|
|
|
silence_warnings do
|
|
|
|
TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
|
|
|
|
end
|
2011-09-06 04:32:30 +00:00
|
|
|
end
|
2011-06-15 13:50:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-01 10:25:37 +00:00
|
|
|
command "edit-method", "Edit a method. Type `edit-method --help` for more info." do |*args|
|
|
|
|
target = target()
|
|
|
|
|
2011-05-19 15:53:44 +00:00
|
|
|
opts = Slop.parse!(args) do |opt|
|
2011-09-10 20:22:47 +00:00
|
|
|
opt.banner unindent <<-USAGE
|
|
|
|
Usage: edit-method [OPTIONS] [METH]
|
|
|
|
Edit the method METH in an editor.
|
|
|
|
Ensure #{text.bold("Pry.config.editor")} is set to your editor of choice.
|
|
|
|
e.g: edit-method hello_method
|
|
|
|
USAGE
|
2011-05-19 15:53:44 +00:00
|
|
|
|
|
|
|
opt.on :M, "instance-methods", "Operate on instance methods."
|
|
|
|
opt.on :m, :methods, "Operate on methods."
|
2011-07-25 05:57:40 +00:00
|
|
|
opt.on :n, "no-reload", "Do not automatically reload the method's file after editing."
|
2011-06-04 16:01:12 +00:00
|
|
|
opt.on "no-jump", "Do not fast forward editor to first line of method."
|
2011-09-09 07:31:56 +00:00
|
|
|
opt.on :p, :patch, "Instead of editing the method's file, try to edit in a tempfile and apply as a monkey patch."
|
2011-05-19 15:53:44 +00:00
|
|
|
opt.on :c, :context, "Select object context to run under.", true do |context|
|
2011-05-01 10:25:37 +00:00
|
|
|
target = Pry.binding_for(target.eval(context))
|
|
|
|
end
|
2011-05-19 15:53:44 +00:00
|
|
|
opt.on :h, :help, "This message." do
|
|
|
|
output.puts opt
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
next if opts.help?
|
|
|
|
|
2011-09-09 07:31:56 +00:00
|
|
|
if !Pry.config.editor
|
2011-10-01 23:57:39 +00:00
|
|
|
raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
|
2011-09-09 07:31:56 +00:00
|
|
|
end
|
|
|
|
|
2011-10-01 23:57:39 +00:00
|
|
|
meth = get_method_or_raise(args.shift, target, opts.to_hash(true))
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-09-25 05:13:01 +00:00
|
|
|
if opts.p? || meth.dynamically_defined?
|
|
|
|
lines = meth.source.lines.to_a
|
2011-09-09 07:31:56 +00:00
|
|
|
|
|
|
|
if lines[0] =~ /^def [^( \n]+/
|
2011-09-25 05:13:01 +00:00
|
|
|
lines[0] = "def #{meth.name}#{$'}"
|
2011-09-09 07:31:56 +00:00
|
|
|
else
|
2011-10-01 23:57:39 +00:00
|
|
|
raise CommandError, "Pry can only patch methods created with the `def` keyword."
|
2011-09-09 07:31:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
temp_file do |f|
|
|
|
|
f.puts lines.join
|
|
|
|
f.flush
|
|
|
|
invoke_editor(f.path, 0)
|
|
|
|
Pry.new(:input => StringIO.new(File.read(f.path))).rep(meth.owner)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-09-25 05:13:01 +00:00
|
|
|
if meth.source_type == :c
|
2011-10-01 23:57:39 +00:00
|
|
|
raise CommandError, "Can't edit a C method."
|
2011-05-01 10:25:37 +00:00
|
|
|
else
|
2011-09-25 05:13:01 +00:00
|
|
|
file, line = meth.source_file, meth.source_line
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2011-06-15 13:49:26 +00:00
|
|
|
invoke_editor(file, opts["no-jump"] ? 0 : line)
|
2011-05-04 12:56:32 +00:00
|
|
|
silence_warnings do
|
2011-07-25 05:57:40 +00:00
|
|
|
load file if !opts.n? && !Pry.config.disable_auto_reload
|
2011-05-04 12:56:32 +00:00
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-04 12:22:26 +00:00
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|