2011-05-01 10:25:37 +00:00
|
|
|
require "pry/default_commands/ls"
|
2012-03-02 01:04:28 +00:00
|
|
|
require "pry/default_commands/cd"
|
2012-04-01 02:24:28 +00:00
|
|
|
require "pry/default_commands/find_method"
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
class Pry
|
|
|
|
module DefaultCommands
|
|
|
|
|
2011-05-07 05:32:05 +00:00
|
|
|
Context = Pry::CommandSet.new do
|
2011-05-01 10:25:37 +00:00
|
|
|
import Ls
|
2012-03-02 01:04:28 +00:00
|
|
|
import Cd
|
2012-04-01 02:24:28 +00:00
|
|
|
import FindMethod
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2012-04-05 05:33:08 +00:00
|
|
|
create_command "whereami" do
|
|
|
|
description "Show code surrounding the current context."
|
|
|
|
banner <<-BANNER
|
|
|
|
Usage: whereami [OPTIONS]
|
|
|
|
BANNER
|
|
|
|
|
2012-04-06 08:31:44 +00:00
|
|
|
def setup
|
|
|
|
@method = Pry::Method.from_binding(target)
|
2012-06-04 03:09:00 +00:00
|
|
|
@file = target.eval('__FILE__')
|
|
|
|
@line = target.eval('__LINE__')
|
2012-04-05 05:33:08 +00:00
|
|
|
end
|
|
|
|
|
2012-06-11 06:37:47 +00:00
|
|
|
def options(opt)
|
|
|
|
opt.on :q, :quiet, "Don't display anything in case of an error"
|
|
|
|
end
|
|
|
|
|
2012-06-04 03:09:00 +00:00
|
|
|
def code
|
2012-06-11 06:37:47 +00:00
|
|
|
@code ||= if show_method?
|
|
|
|
Pry::Code.from_method(@method)
|
|
|
|
else
|
2012-06-15 13:25:07 +00:00
|
|
|
Pry::Code.from_file(@file).around(@line, window_size)
|
2012-06-11 06:37:47 +00:00
|
|
|
end
|
2012-06-04 03:09:00 +00:00
|
|
|
end
|
2012-04-05 05:33:08 +00:00
|
|
|
|
2012-06-04 03:09:00 +00:00
|
|
|
def location
|
|
|
|
"#{@file} @ line #{show_method? ? @method.source_line : @line} #{@method && @method.name_with_owner}"
|
|
|
|
end
|
2012-04-14 05:15:50 +00:00
|
|
|
|
2012-06-04 03:09:00 +00:00
|
|
|
def process
|
2012-06-11 06:37:47 +00:00
|
|
|
if opts.quiet? && (internal_binding? || !code?)
|
|
|
|
return
|
|
|
|
elsif internal_binding?
|
2012-06-15 13:25:07 +00:00
|
|
|
output.puts "Could not find local context, did you use \`binding.pry\`?"
|
2012-06-11 06:37:47 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-06-04 03:09:00 +00:00
|
|
|
set_file_and_dir_locals(@file)
|
2012-05-16 19:47:13 +00:00
|
|
|
|
2012-06-04 03:09:00 +00:00
|
|
|
output.puts "\n#{text.bold('From:')} #{location}:\n\n"
|
|
|
|
output.puts code.with_line_numbers.with_marker(@line)
|
|
|
|
output.puts
|
2012-03-02 08:12:18 +00:00
|
|
|
end
|
|
|
|
|
2012-04-18 06:49:00 +00:00
|
|
|
private
|
2012-04-05 05:33:08 +00:00
|
|
|
|
2012-06-11 06:37:47 +00:00
|
|
|
def internal_binding?
|
|
|
|
@method && ['__binding__', '__binding_impl__'].include?(@method.name)
|
|
|
|
end
|
|
|
|
|
2012-04-05 05:33:08 +00:00
|
|
|
def show_method?
|
2012-06-04 03:09:00 +00:00
|
|
|
args.empty? && @method && @method.source? && @method.source_range.count < 20 &&
|
|
|
|
# These checks are needed in case of an eval with a binding and file/line
|
|
|
|
# numbers set to outside the function. As in rails' use of ERB.
|
|
|
|
@method.source_file == @file && @method.source_range.include?(@line)
|
2012-04-05 05:33:08 +00:00
|
|
|
end
|
2012-03-02 08:12:18 +00:00
|
|
|
|
2012-06-11 06:37:47 +00:00
|
|
|
def code?
|
|
|
|
!!code
|
|
|
|
rescue MethodSource::SourceNotFoundError
|
|
|
|
false
|
2012-04-01 08:02:37 +00:00
|
|
|
end
|
2012-06-15 13:25:07 +00:00
|
|
|
|
|
|
|
def window_size
|
|
|
|
if args.empty?
|
|
|
|
Pry.config.default_window_size
|
|
|
|
else
|
|
|
|
args.first.to_i
|
|
|
|
end
|
|
|
|
end
|
2012-03-02 08:12:18 +00:00
|
|
|
end
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
create_command "pry-backtrace", "Show the backtrace for the Pry session." do
|
2012-01-15 00:21:10 +00:00
|
|
|
banner <<-BANNER
|
2012-02-23 03:28:00 +00:00
|
|
|
Usage: pry-backtrace [OPTIONS] [--help]
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
Show the backtrace for the position in the code where Pry was started. This can be used to
|
|
|
|
infer the behavior of the program immediately before it entered Pry, just like the backtrace
|
|
|
|
property of an exception.
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
(NOTE: if you are looking for the backtrace of the most recent exception raised,
|
|
|
|
just type: `_ex_.backtrace` instead, see https://github.com/pry/pry/wiki/Special-Locals)
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g: pry-backtrace
|
2012-01-15 00:21:10 +00:00
|
|
|
BANNER
|
|
|
|
|
2012-01-14 09:01:51 +00:00
|
|
|
def process
|
2012-02-23 03:28:00 +00:00
|
|
|
output.puts "\n#{text.bold('Backtrace:')}\n--\n"
|
|
|
|
stagger_output _pry_.backtrace.join("\n")
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
command "reset", "Reset the REPL to a clean state." do
|
|
|
|
output.puts "Pry reset."
|
|
|
|
exec "pry"
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2012-05-28 23:37:02 +00:00
|
|
|
create_command(/wtf([?!]*)/, "Show the backtrace of the most recent exception") do
|
2012-02-23 03:28:00 +00:00
|
|
|
options :listing => 'wtf?'
|
2012-01-15 00:21:10 +00:00
|
|
|
|
|
|
|
banner <<-BANNER
|
2012-02-23 03:28:00 +00:00
|
|
|
Show's a few lines of the backtrace of the most recent exception (also available
|
|
|
|
as _ex_.backtrace).
|
|
|
|
|
|
|
|
If you want to see more lines, add more question marks or exclamation marks:
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g.
|
|
|
|
pry(main)> wtf?
|
|
|
|
pry(main)> wtf?!???!?!?
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
To see the entire backtrace, pass the -v/--verbose flag:
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g.
|
|
|
|
pry(main)> wtf -v
|
2012-01-15 00:21:10 +00:00
|
|
|
BANNER
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
def options(opt)
|
|
|
|
opt.on(:v, :verbose, "Show the full backtrace.")
|
|
|
|
end
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-01-14 09:01:51 +00:00
|
|
|
def process
|
2012-02-23 03:28:00 +00:00
|
|
|
raise Pry::CommandError, "No most-recent exception" unless _pry_.last_exception
|
2012-03-02 08:12:18 +00:00
|
|
|
|
|
|
|
output.puts "#{text.bold('Exception:')} #{_pry_.last_exception.class}: #{_pry_.last_exception}\n--"
|
2012-02-23 03:28:00 +00:00
|
|
|
if opts.verbose?
|
2012-03-02 08:12:18 +00:00
|
|
|
output.puts Code.new(_pry_.last_exception.backtrace, 0, :text).with_line_numbers.to_s
|
2011-08-25 09:41:43 +00:00
|
|
|
else
|
2012-03-02 08:12:18 +00:00
|
|
|
output.puts Code.new(_pry_.last_exception.backtrace.first([captures[0].size, 0.5].max * 10), 0, :text).with_line_numbers.to_s
|
2011-08-25 09:41:43 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-29 12:35:45 +00:00
|
|
|
end
|
|
|
|
|
2012-03-08 05:54:19 +00:00
|
|
|
# N.B. using a regular expresion here so that "raise-up 'foo'" does the right thing.
|
2012-05-28 23:37:02 +00:00
|
|
|
create_command(/raise-up(!?\b.*)/, :listing => 'raise-up') do
|
2012-03-24 18:04:03 +00:00
|
|
|
description "Raise an exception out of the current pry instance."
|
2012-03-08 05:54:19 +00:00
|
|
|
banner <<-BANNER
|
|
|
|
Raise up, like exit, allows you to quit pry. Instead of returning a value however, it raises an exception.
|
|
|
|
If you don't provide the exception to be raised, it will use the most recent exception (in pry _ex_).
|
|
|
|
|
|
|
|
e.g. `raise-up "get-me-out-of-here"` is equivalent to:
|
|
|
|
`raise "get-me-out-of-here"
|
|
|
|
raise-up`
|
|
|
|
|
|
|
|
When called as raise-up! (with an exclamation mark), this command raises the exception through
|
|
|
|
any nested prys you have created by "cd"ing into objects.
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
def process
|
2012-03-24 18:04:03 +00:00
|
|
|
return stagger_output help if captures[0] =~ /(-h|--help)\b/
|
|
|
|
# Handle 'raise-up', 'raise-up "foo"', 'raise-up RuntimeError, 'farble' in a rubyesque manner
|
2012-03-08 05:54:19 +00:00
|
|
|
target.eval("_pry_.raise_up#{captures[0]}")
|
|
|
|
end
|
|
|
|
end
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|