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-08-27 14:13:15 +00:00
opt . banner " Usage: show-method [OPTIONS] [METH 1] [METH 2] [METH N] \n " \
2011-05-19 15:53:44 +00:00
" Show the source for method METH. Tries instance methods first and then methods by default. \n " \
" e.g: show-method hello_method "
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-08-27 14:13:15 +00:00
args = [ nil ] if args . empty?
args . each do | method_name |
meth_name = method_name
if ( meth = get_method_object ( meth_name , target , opts . to_hash ( true ) ) ) . nil?
output . puts " Invalid method name: #{ meth_name } . Type `show-method --help` for help "
next
end
2011-05-01 10:25:37 +00:00
2011-08-27 14:13:15 +00:00
code , code_type = code_and_code_type_for ( meth )
next if ! code
2011-05-01 10:25:37 +00:00
2011-08-27 14:13:15 +00:00
output . puts make_header ( meth , code_type , code )
if Pry . color
code = CodeRay . scan ( code , code_type ) . term
end
2011-05-01 10:25:37 +00:00
2011-08-27 14:13:15 +00:00
start_line = false
if opts . l?
start_line = meth . source_location ? meth . source_location . last : 1
end
2011-05-01 10:25:37 +00:00
2011-08-27 14:13:15 +00:00
start_line = opts . b? ? 1 : start_line
2011-06-16 13:49:31 +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
alias_command " show-source " , " show-method " , " "
alias_command " $ " , " show-method " , " "
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 |
opt . banner = " Usage: show-command [OPTIONS] [CMD] \n " \
" Show the source for command CMD. \n " \
" e.g: show-command show-method "
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
output . puts " You must provide a command name. "
next
end
2011-05-27 16:20:00 +00:00
if find_command ( command_name )
block = find_command ( command_name ) . block
2011-05-01 10:25:37 +00:00
2011-05-27 16:20:00 +00:00
code , _ = code_and_code_type_for ( block )
next if ! code
2011-05-01 10:25:37 +00:00
2011-05-27 16:20:00 +00:00
output . puts make_header ( block , :ruby , code )
2011-05-01 10:25:37 +00:00
if Pry . color
code = CodeRay . scan ( code , :ruby ) . term
end
2011-05-27 16:20:00 +00:00
start_line = false
if opts . l?
start_line = block . source_location ? block . source_location . last : 1
end
render_output ( opts . flood? , opts . l? ? block . source_location . last : false , code )
2011-05-01 10:25:37 +00:00
code
else
output . puts " No such command: #{ command_name } . "
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 |
opt . banner " Usage: edit [OPTIONS] [FILE] \n " \
2011-09-10 06:43:06 +00:00
" Edit the method FILE in an editor. \n When no file given, opens editor with contents of input buffer or previous input and evals after closing. " \
2011-09-05 04:14:24 +00:00
" \n Ensure #{ text . bold ( " Pry.config.editor " ) } is set to your editor of choice. \n " \
2011-06-15 13:50:09 +00:00
" e.g: edit sample.rb "
2011-07-26 17:08:27 +00:00
opt . on :r , " reload " , " Eval file content after editing (evals at top level) "
2011-09-06 04:32:30 +00:00
opt . on :n , " no-reload " , " Do not automatically reload the file after editing (only applies to --ex and -t). "
opt . on :ex , " Open an editor at the line and file that generated the most recent Exception, reloads file after editing. "
2011-09-10 06:43:06 +00:00
opt . on :t , " temp " , " Open a temporary file in an editor with contents of input buffer and eval it in current context after closing (does not default to previous input) "
2011-07-26 17:08:27 +00:00
opt . on :p , " play " , " Use the pry `play` command to eval the file content after editing. "
2011-06-15 13:50:09 +00:00
opt . on :l , " line " , " Specify line number to jump to in file " , true , :as = > Integer
opt . on :h , :help , " This message. " do
output . puts opt
end
end
next if opts . h?
2011-09-06 04:32:30 +00:00
should_reload_at_top_level = opts [ :r ]
should_reload_locally = false
2011-07-26 17:08:27 +00:00
if opts . ex?
2011-08-31 17:05:21 +00:00
next output . puts " No Exception found. " if _pry_ . last_exception . nil?
2011-07-26 17:08:27 +00:00
2011-09-01 03:47:29 +00:00
if is_core_rbx_path? ( _pry_ . last_exception . file )
file_name = rbx_convert_path_to_full ( _pry_ . last_exception . file )
else
file_name = _pry_ . last_exception . file
end
2011-08-31 17:05:21 +00:00
line = _pry_ . last_exception . line
2011-07-26 17:08:27 +00:00
next output . puts " Exception has no associated file. " if file_name . nil?
next output . puts " Cannot edit exceptions raised in REPL. " if Pry . eval_path == file_name
2011-09-06 04:32:30 +00:00
should_reload_at_top_level = opts [ :n ] ? false : true
2011-09-10 06:43:06 +00:00
elsif opts . t? || args . empty?
2011-09-08 02:17:41 +00:00
file_name = temp_file do | f |
2011-09-10 06:43:06 +00:00
if ! eval_string . empty?
f . puts eval_string
elsif ! opts . t? && ! _pry_ . input_array . empty?
f . puts _pry_ . input_array [ - 1 ]
end
2011-09-08 02:17:41 +00:00
end
2011-09-05 04:14:24 +00:00
line = eval_string . lines . count + 1
2011-09-06 04:32:30 +00:00
should_reload_locally = opts [ :n ] ? false : true
2011-07-26 17:08:27 +00:00
else
2011-09-07 08:58:58 +00:00
# break up into file:line
/ (:( \ d+))?$ / =~ File . expand_path ( args . first )
# $` is pre-match
file_name , line = [ $` , $2 ]
2011-09-05 04:14:24 +00:00
line = line ? line . to_i : opts [ :l ] . to_i
2011-07-26 17:08:27 +00:00
end
2011-06-15 13:50:09 +00:00
2011-07-26 17:08:27 +00:00
invoke_editor ( file_name , line )
2011-06-16 13:49:31 +00:00
set_file_and_dir_locals ( file_name )
2011-06-15 13:50:09 +00:00
2011-07-27 11:21:53 +00:00
if opts [ :p ]
2011-06-15 13:50:09 +00:00
silence_warnings do
2011-08-16 01:02:32 +00:00
_pry_ . input = StringIO . new ( File . readlines ( file_name ) . join )
2011-06-15 13:50:09 +00:00
end
2011-09-06 04:32:30 +00:00
elsif should_reload_locally
2011-06-15 13:50:09 +00:00
silence_warnings do
2011-09-05 04:14:24 +00:00
eval_string . replace ( File . read ( file_name ) )
2011-06-15 13:50:09 +00:00
end
2011-09-06 04:32:30 +00:00
elsif should_reload_at_top_level
silence_warnings do
TOPLEVEL_BINDING . eval ( File . read ( file_name ) , file_name )
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 |
opt . banner " Usage: edit-method [OPTIONS] [METH] \n " \
" Edit the method METH in an editor. \n " \
2011-08-29 15:00:13 +00:00
" Ensure #{ text . bold ( " Pry.config.editor " ) } is set to your editor of choice. \n " \
2011-05-19 15:53:44 +00:00
" e.g: edit-method hello_method "
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-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?
meth_name = args . shift
if ( meth = get_method_object ( meth_name , target , opts . to_hash ( true ) ) ) . nil?
output . puts " Invalid method name: #{ meth_name } . "
next
end
2011-08-29 15:00:13 +00:00
next output . puts " Error: No editor set! \n Ensure that #{ text . bold ( " Pry.config.editor " ) } is set to your editor of choice. " if ! Pry . config . editor
2011-05-01 10:25:37 +00:00
if is_a_c_method? ( meth )
output . puts " Error: Can't edit a C method. "
elsif is_a_dynamically_defined_method? ( meth )
output . puts " Error: Can't edit an eval method. "
# editor is invoked here
else
2011-06-03 15:09:24 +00:00
file , line = path_line_for ( meth )
2011-05-04 13:44:54 +00:00
set_file_and_dir_locals ( file )
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