2012-07-06 01:34:05 -04:00
|
|
|
class Pry
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::Whereami < Pry::ClassCommand
|
|
|
|
match 'whereami'
|
|
|
|
description 'Show code surrounding the current context.'
|
2012-08-11 20:22:29 -04:00
|
|
|
group 'Context'
|
2012-12-25 16:35:17 -05:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
banner <<-BANNER
|
|
|
|
Usage: whereami [-q] [N]
|
|
|
|
|
|
|
|
Describe the current location. If you use `binding.pry` inside a
|
|
|
|
method then whereami will print out the source for that method.
|
|
|
|
|
|
|
|
If a number is passed, then N lines before and after the current line
|
|
|
|
will be shown instead of the method itself.
|
|
|
|
|
|
|
|
The `-q` flag can be used to suppress error messages in the case that
|
|
|
|
there's no code to show. This is used by pry in the default
|
|
|
|
before_session hook to show you when you arrive at a `binding.pry`.
|
|
|
|
|
|
|
|
When pry was started on an Object and there is no associated method,
|
|
|
|
whereami will instead output a brief description of the current
|
|
|
|
object.
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
def setup
|
2012-11-18 19:41:56 -05:00
|
|
|
@method = Pry::Method.from_binding(target)
|
2012-11-20 16:44:23 -05:00
|
|
|
@file = target.eval('__FILE__')
|
|
|
|
@line = target.eval('__LINE__')
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def options(opt)
|
|
|
|
opt.on :q, :quiet, "Don't display anything in case of an error"
|
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def code
|
|
|
|
@code ||= if show_method?
|
|
|
|
Pry::Code.from_method(@method)
|
|
|
|
else
|
|
|
|
Pry::Code.from_file(@file).around(@line, window_size)
|
|
|
|
end
|
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def location
|
2012-11-20 16:44:50 -05:00
|
|
|
"#{@file} @ line #{@line} #{@method && @method.name_with_owner}"
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def process
|
2013-01-01 19:12:17 -05:00
|
|
|
if nothing_to_do?
|
2012-08-11 20:22:29 -04:00
|
|
|
return
|
2012-11-18 19:41:56 -05:00
|
|
|
elsif internal_binding?(target)
|
2013-01-01 20:06:51 -05:00
|
|
|
handle_internal_binding
|
2012-11-20 16:44:23 -05:00
|
|
|
return
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
set_file_and_dir_locals(@file)
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
output.puts "\n#{text.bold('From:')} #{location}:\n\n"
|
|
|
|
output.puts code.with_line_numbers.with_marker(@line)
|
|
|
|
output.puts
|
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
private
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2013-01-01 20:06:51 -05:00
|
|
|
def nothing_to_do?
|
|
|
|
opts.quiet? && (internal_binding?(target) || !code?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def top_level?
|
|
|
|
target_self == TOPLEVEL_BINDING.eval("self")
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_internal_binding
|
|
|
|
if top_level?
|
|
|
|
output.puts "At the top level."
|
|
|
|
else
|
|
|
|
output.puts "Inside #{Pry.view_clip(target_self)}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def show_method?
|
|
|
|
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)
|
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def code?
|
|
|
|
!!code
|
|
|
|
rescue MethodSource::SourceNotFoundError
|
|
|
|
false
|
|
|
|
end
|
2012-07-06 01:34:05 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def window_size
|
|
|
|
if args.empty?
|
|
|
|
Pry.config.default_window_size
|
|
|
|
else
|
|
|
|
args.first.to_i
|
2012-07-06 01:34:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-25 16:35:17 -05:00
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::Whereami)
|
2012-07-06 01:34:05 -04:00
|
|
|
end
|