pry--pry/lib/pry/pry_instance.rb

424 lines
13 KiB
Ruby
Raw Normal View History

require "pry/command_processor.rb"
# @attr prompt
class Pry
attr_accessor :input
attr_accessor :output
attr_accessor :commands
attr_accessor :print
attr_accessor :exception_handler
attr_accessor :hooks
attr_accessor :custom_completions
# Returns the target binding for the session. Note that altering this
# attribute will not change the target binding.
# @return [Binding] The target object for the session
attr_accessor :session_target
2010-12-29 09:14:12 +00:00
# Create a new `Pry` object.
# @param [Hash] options The optional configuration parameters.
# @option options [#readline] :input The object to use for input.
# @option options [#puts] :output The object to use for output.
# @option options [Pry::CommandBase] :commands The object to use for commands. (see commands.rb)
2010-12-29 11:25:38 +00:00
# @option options [Hash] :hooks The defined hook Procs (see hooks.rb)
# @option options [Array<Proc>] :default_prompt The array of Procs to use for the prompts. (see prompts.rb)
# @option options [Proc] :print The Proc to use for the 'print'
# component of the REPL. (see print.rb)
def initialize(options={})
defaults = {}
attributes = [
:input, :output, :commands, :print,
:exception_handler, :hooks, :custom_completions,
:prompt
]
attributes.each do |attribute|
defaults[attribute] = Pry.send attribute
end
defaults.merge!(options).each_key do |key|
send "#{key}=", defaults[key]
end
@command_processor = CommandProcessor.new(self)
end
# The current prompt.
# This is the prompt at the top of the prompt stack.
#
# @example
# self.prompt = Pry::SIMPLE_PROMPT
# self.prompt # => Pry::SIMPLE_PROMPT
#
# @return [Array<Proc>] Current prompt.
def prompt
prompt_stack.last
end
def prompt=(new_prompt)
if prompt_stack.empty?
push_prompt new_prompt
else
prompt_stack[-1] = new_prompt
end
end
2010-12-29 09:14:12 +00:00
# Get nesting data.
# This method should not need to be accessed directly.
# @return [Array] The unparsed nesting information.
def nesting
self.class.nesting
end
2010-12-29 09:14:12 +00:00
# Set nesting data.
# This method should not need to be accessed directly.
# @param v nesting data.
def nesting=(v)
self.class.nesting = v
end
2010-12-29 09:14:12 +00:00
# Return parent of current Pry session.
# @return [Pry] The parent of the current Pry session.
def parent
idx = Pry.sessions.index(self)
2011-04-24 18:31:19 +00:00
Pry.sessions[idx - 1] if idx && idx > 0
end
2010-12-29 09:14:12 +00:00
# Execute the hook `hook_name`, if it is defined.
# @param [Symbol] hook_name The hook to execute
# @param [Array] args The arguments to pass to the hook.
def exec_hook(hook_name, *args, &block)
hooks[hook_name].call(*args, &block) if hooks[hook_name]
end
# Initialize the repl session.
# @param [Binding] target The target binding for the session.
def repl_prologue(target)
exec_hook :before_session, output, target
Pry.active_instance = self
# Make sure special locals exist
target.eval("_pry_ = ::Pry.active_instance")
target.eval("_ = ::Pry.last_result")
self.session_target = target
end
# Clean-up after the repl session.
# @param [Binding] target The target binding for the session.
# @return [Object] The return value of the repl session (if one exists).
def repl_epilogue(target, nesting_level, break_data)
nesting.pop
exec_hook :after_session, output, target
# If break_data is an array, then the last element is the return value
break_level, return_value = Array(break_data)
# keep throwing until we reach the desired nesting level
if nesting_level != break_level
throw :breakout, break_data
end
return_value
end
2010-12-29 09:14:12 +00:00
# Start a read-eval-print-loop.
2010-12-28 03:56:23 +00:00
# If no parameter is given, default to top-level (main).
2010-12-29 09:14:12 +00:00
# @param [Object, Binding] target The receiver of the Pry session
2011-02-26 01:00:55 +00:00
# @return [Object] The target of the Pry session or an explictly given
# return value. If given return value is `nil` or no return value
# is specified then `target` will be returned.
2010-12-28 03:56:23 +00:00
# @example
# Pry.new.repl(Object.new)
def repl(target=TOPLEVEL_BINDING)
target = Pry.binding_for(target)
target_self = target.eval('self')
repl_prologue(target)
# cannot rely on nesting.level as
# nesting.level changes with new sessions
nesting_level = nesting.size
break_data = catch(:breakout) do
nesting.push [nesting.size, target_self, self]
loop do
rep(target)
end
end
return_value = repl_epilogue(target, nesting_level, break_data)
return_value || target_self
end
2010-12-29 09:14:12 +00:00
# Perform a read-eval-print.
2010-12-28 03:56:23 +00:00
# If no parameter is given, default to top-level (main).
2010-12-29 09:14:12 +00:00
# @param [Object, Binding] target The receiver of the read-eval-print
2010-12-28 03:56:23 +00:00
# @example
# Pry.new.rep(Object.new)
def rep(target=TOPLEVEL_BINDING)
target = Pry.binding_for(target)
result = re(target)
show_result(result) if should_print?
end
2010-12-28 03:56:23 +00:00
# Perform a read-eval
# If no parameter is given, default to top-level (main).
2010-12-29 09:14:12 +00:00
# @param [Object, Binding] target The receiver of the read-eval-print
# @return [Object] The result of the eval or an `Exception` object in case of
2011-04-16 21:27:48 +00:00
# error. In the latter case, you can check whether the exception was raised
# or is just the result of the expression using #last_result_is_exception?
2010-12-28 03:56:23 +00:00
# @example
# Pry.new.re(Object.new)
def re(target=TOPLEVEL_BINDING)
target = Pry.binding_for(target)
if input == Readline
# Readline tab completion
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target, instance_eval(&custom_completions)
end
# save the pry instance to active_instance
set_active_instance(target)
2011-03-14 10:55:22 +00:00
@last_result_is_exception = false
expr = r(target)
Pry.line_buffer.push(*expr.each_line)
set_last_result(target.eval(expr, Pry.eval_path, Pry.current_line), target)
rescue SystemExit => e
exit
rescue Exception => e
@last_result_is_exception = true
2011-03-14 10:55:22 +00:00
set_last_exception(e, target)
ensure
Pry.current_line += expr.each_line.count if expr
end
2010-12-28 03:56:23 +00:00
# Perform a read.
# If no parameter is given, default to top-level (main).
# This is a multi-line read; so the read continues until a valid
# Ruby expression is received.
# Pry commands are also accepted here and operate on the target.
2010-12-29 09:14:12 +00:00
# @param [Object, Binding] target The receiver of the read.
# @param [String] eval_string Optionally Prime `eval_string` with a start value.
2010-12-28 03:56:23 +00:00
# @return [String] The Ruby expression.
# @example
# Pry.new.r(Object.new)
def r(target=TOPLEVEL_BINDING, eval_string="")
target = Pry.binding_for(target)
@suppress_output = false
2011-01-09 11:51:45 +00:00
val = ""
loop do
val = retrieve_line(eval_string, target)
process_line(val, eval_string, target)
break if valid_expression?(eval_string)
end
@suppress_output = true if eval_string =~ /;\Z/ || null_input?(val)
eval_string
2011-04-15 03:58:29 +00:00
end
# FIXME should delete this method? it's exposing an implementation detail!
def show_result(result)
if last_result_is_exception?
exception_handler.call output, result
else
print.call output, result
end
2011-04-15 03:58:29 +00:00
end
# Returns true if input is "" and a command is not returning a
# value.
# @param [String] val The input string.
# @return [Boolean] Whether the input is null.
def null_input?(val)
val.empty? && !Pry.cmd_ret_value
end
# Read a line of input and check for ^d, also determine prompt to use.
# This method should not need to be invoked directly.
# @param [String] eval_string The cumulative lines of input.
# @param [Binding] target The target of the session.
# @return [String] The line received.
def retrieve_line(eval_string, target)
current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
val = readline(current_prompt)
# exit session if we receive EOF character
if !val
output.puts
throw(:breakout, nesting.level)
end
val
end
# Process the line received.
# This method should not need to be invoked directly.
# @param [String] val The line to process.
# @param [String] eval_string The cumulative lines of input.
# @target [Binding] target The target of the Pry session.
def process_line(val, eval_string, target)
val.rstrip!
Pry.cmd_ret_value = @command_processor.process_commands(val, eval_string, target)
if Pry.cmd_ret_value
eval_string << "Pry.cmd_ret_value\n"
else
eval_string << "#{val}\n" if !val.empty?
end
end
2011-03-14 10:55:22 +00:00
# Set the last result of an eval.
# This method should not need to be invoked directly.
2011-03-14 10:55:22 +00:00
# @param [Object] result The result.
# @param [Binding] target The binding to set `_` on.
def set_last_result(result, target)
Pry.last_result = result
target.eval("_ = ::Pry.last_result")
2011-03-14 10:55:22 +00:00
end
# Set the last exception for a session.
# This method should not need to be invoked directly.
2011-03-14 10:55:22 +00:00
# @param [Exception] ex The exception.
# @param [Binding] target The binding to set `_ex_` on.
def set_last_exception(ex, target)
Pry.last_exception = ex
target.eval("_ex_ = ::Pry.last_exception")
2011-03-14 10:55:22 +00:00
end
# Set the active instance for a session.
# This method should not need to be invoked directly.
# @param [Binding] target The binding to set `_ex_` on.
def set_active_instance(target)
Pry.active_instance = self
target.eval("_pry_ = ::Pry.active_instance")
end
# @return [Boolean] True if the last result is an exception that was raised,
# as opposed to simply an instance of Exception (like the result of
# Exception.new)
def last_result_is_exception?
@last_result_is_exception
end
# Returns the next line of input to be used by the pry instance.
# This method should not need to be invoked directly.
# @param [String] current_prompt The prompt to use for input.
# @return [String] The next line of input.
def readline(current_prompt="> ")
if input == Readline
# Readline must be treated differently
# as it has a second parameter.
input.readline(current_prompt, true)
else
begin
if input.method(:readline).arity == 1
input.readline(current_prompt)
else
input.readline
end
2011-04-18 21:31:39 +00:00
rescue EOFError
self.input = Pry.input
""
end
end
end
2011-04-10 23:10:05 +00:00
# Whether the print proc should be invoked.
# Currently only invoked if the output is not suppressed OR the last result
2011-04-10 23:10:05 +00:00
# is an exception regardless of suppression.
# @return [Boolean] Whether the print proc should be invoked.
def should_print?
!@suppress_output || last_result_is_exception?
2011-04-10 23:10:05 +00:00
end
2010-12-28 03:56:23 +00:00
# Returns the appropriate prompt to use.
# This method should not need to be invoked directly.
# @param [Boolean] first_line Whether this is the first line of input
# (and not multi-line input).
# @param [Object] target_self The receiver of the Pry session.
2010-12-28 03:56:23 +00:00
# @return [String] The prompt.
2011-01-09 11:51:45 +00:00
def select_prompt(first_line, target_self)
if first_line
Array(prompt).first.call(target_self, nesting.level)
else
Array(prompt).last.call(target_self, nesting.level)
end
end
# the array that the prompt stack is stored in
def prompt_stack
@prompt_stack ||= Array.new
end
private :prompt_stack
# Pushes the current prompt onto a stack that it can be restored from later.
# Use this if you wish to temporarily change the prompt.
# @param [Array<Proc>] new_prompt
# @return [Array<Proc>] new_prompt
# @example
# new_prompt = [ proc { '>' }, proc { '>>' } ]
# push_prompt(new_prompt) # => new_prompt
def push_prompt(new_prompt)
prompt_stack.push new_prompt
end
# Pops the current prompt off of the prompt stack.
# If the prompt you are popping is the last prompt, it will not be popped.
# Use this to restore the previous prompt.
# @return [Array<Proc>] Prompt being popped.
# @example
# prompt1 = [ proc { '>' }, proc { '>>' } ]
# prompt2 = [ proc { '$' }, proc { '>' } ]
# pry = Pry.new :prompt => prompt1
# pry.push_prompt(prompt2)
# pry.pop_prompt # => prompt2
# pry.pop_prompt # => prompt1
# pry.pop_prompt # => prompt1
def pop_prompt
prompt_stack.size > 1 ? prompt_stack.pop : prompt
end
if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE == "ruby"
require 'ripper'
2010-12-28 03:56:23 +00:00
# Determine if a string of code is a valid Ruby expression.
2010-12-29 09:14:12 +00:00
# Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
2010-12-28 03:56:23 +00:00
# @param [String] code The code to validate.
# @return [Boolean] Whether or not the code is a valid Ruby expression.
# @example
# valid_expression?("class Hello") #=> false
# valid_expression?("class Hello; end") #=> true
def valid_expression?(code)
!!Ripper::SexpBuilder.new(code).parse
end
else
require 'ruby_parser'
2010-12-28 03:56:23 +00:00
# Determine if a string of code is a valid Ruby expression.
2010-12-29 09:14:12 +00:00
# Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
2010-12-28 03:56:23 +00:00
# @param [String] code The code to validate.
# @return [Boolean] Whether or not the code is a valid Ruby expression.
# @example
# valid_expression?("class Hello") #=> false
# valid_expression?("class Hello; end") #=> true
def valid_expression?(code)
!!RubyParser.new.parse(code)
rescue Racc::ParseError, SyntaxError
false
end
end
end