Permalink
Browse files

all special locals are now injected into all bindings for a Pry insta…

…nce - e.g _ex_ is consistent no matter if you cd into a new context. New 'special local' system, and added tests for the new system
  • Loading branch information...
banister committed Aug 31, 2011
1 parent d16e396 commit 84b0601f4a468072bfd938d1a16a39e4d95b1ef4
View
@@ -12,6 +12,7 @@
* changed `exit` command so that it now called Kernel#exit (after saving history)
* 'quit' now behaves like 'exit-all' (and aliased to exit-all) - it breaks out of the repl loop and sets empties the binding_stack
* 'cd ..' just pops a binding off the binding_stack with special behaviour when only one binding in stack - it breaks out of the repl loop
+* added switch-to command (like jump-to but doesnt unwind the stack)
* show-method and show-doc now accept multiple method names
* control_d hook added (Pry.config.control_d_handler)
* behaviour of ^d is now to break out of current expr if in multi-line expr, or break out of current context if nested, or break out of pry repl loop if at top-level
@@ -131,12 +131,10 @@ module DefaultCommands
should_reload = opts[:r]
if opts.ex?
- last_exception = target.eval("_ex_")
+ next output.puts "No Exception found." if _pry_.last_exception.nil?
- next output.puts "No Exception found." if last_exception.nil?
-
- file_name = last_exception.file
- line = last_exception.line
+ file_name = _pry_.last_exception.file
+ line = _pry_.last_exception.line
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
elsif opts.t?
@@ -38,7 +38,6 @@ module DefaultCommands
start_line = 0
end_line = -1
file_name = nil
- last_exception = target.eval("_ex_")
opts = Slop.parse!(args) do |opt|
opt.on :s, :start, "Start line (defaults to start of file)Line 1 is the first line.", true, :as => Integer do |line|
@@ -51,7 +50,7 @@ module DefaultCommands
opt.on :ex, "Show a window of N lines either side of the last exception (defaults to 5).", :optional => true, :as => Integer do |window_size|
window_size ||= 5
- ex = last_exception
+ ex = _pry_.last_exception
next if !ex
start_line = (ex.line - 1) - window_size
start_line = start_line < 0 ? 0 : start_line
@@ -94,16 +93,16 @@ module DefaultCommands
contents = contents.lines.each_with_index.map do |line, idx|
l = idx + start_line
- if l == (last_exception.line - 1)
+ if l == (_pry_.last_exception.line - 1)
" =>#{line}"
else
" #{line}"
end
end.join
# header for exceptions
- output.puts "\n#{Pry::Helpers::Text.bold('Exception:')}: #{last_exception.class}: #{last_exception.message}"
- output.puts "#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{last_exception.line}\n\n"
+ output.puts "\n#{Pry::Helpers::Text.bold('Exception:')}: #{_pry_.last_exception.class}: #{_pry_.last_exception.message}"
+ output.puts "#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{_pry_.last_exception.line}\n\n"
end
set_file_and_dir_locals(file_name)
@@ -36,8 +36,11 @@ def command_dependencies_met?(options)
def set_file_and_dir_locals(file_name)
return if !target
- _pry_.inject_local("_file_", File.expand_path(file_name), target)
- _pry_.inject_local("_dir_", File.dirname(File.expand_path(file_name)), target)
+ _pry_.last_file = File.expand_path(file_name)
+ _pry_.inject_local("_file_", _pry_.last_file, target)
+
+ _pry_.last_dir = File.dirname(_pry_.last_file)
+ _pry_.inject_local("_dir_", _pry_.last_dir, target)
end
def stub_proc(name, options)
View
@@ -12,6 +12,11 @@ class Pry
attr_accessor :binding_stack
+ attr_accessor :last_result
+ attr_accessor :last_exception
+ attr_accessor :last_file
+ attr_accessor :last_dir
+
# Create a new `Pry` object.
# @param [Hash] options The optional configuration parameters.
# @option options [#readline] :input The object to use for input.
@@ -101,19 +106,36 @@ 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
-
- # Make sure special locals exist
+ # Make sure special locals exist at start of session
+ def initialize_special_locals(target)
inject_local("inp", @input_array, target)
inject_local("out", @output_array, target)
inject_local("_pry_", self, target)
inject_local("_ex_", nil, target)
- inject_local("file_", nil, target)
+ inject_local("_file_", nil, target)
inject_local("_dir_", nil, target)
+
+ # without this line we get 1 test failure, ask Mon_Ouie
set_last_result(nil, target)
+ inject_local("_", nil, target)
+ end
+ private :initialize_special_locals
+
+ def inject_special_locals(target)
+ inject_local("inp", @input_array, target)
+ inject_local("out", @output_array, target)
+ inject_local("_pry_", self, target)
+ inject_local("_ex_", self.last_exception, target)
+ inject_local("_file_", self.last_file, target)
+ inject_local("_dir_", self.last_dir, target)
+ inject_local("_", self.last_result, target)
+ end
+
+ # Initialize the repl session.
+ # @param [Binding] target The target binding for the session.
+ def repl_prologue(target)
+ exec_hook :before_session, output, target
+ initialize_special_locals(target)
@input_array << nil # add empty input so inp and out match
@@ -185,14 +207,14 @@ def re(target=TOPLEVEL_BINDING)
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target, instance_eval(&custom_completions)
end
- inject_local("inp", @input_array, target)
- inject_local("out", @output_array, target)
- inject_local("_pry_", self, target)
+ # It's not actually redundant to inject them continually as we may have
+ # moved into the scope of a new Binding (e.g the user typed `cd`)
+ inject_special_locals(target)
code = r(target)
- res = set_last_result(target.eval(code, Pry.eval_path, Pry.current_line), target)
- res
+ result = set_last_result(target.eval(code, Pry.eval_path, Pry.current_line), target)
+ result
rescue RescuableException => e
set_last_exception(e, target)
ensure
@@ -299,7 +321,7 @@ def set_last_result(result, target)
@last_result_is_exception = false
@output_array << result
- inject_local("_", result, target)
+ self.last_result = result
end
# Set the last exception for a session.
@@ -317,7 +339,7 @@ class << ex
@last_result_is_exception = true
@output_array << ex
- inject_local("_ex_", ex, target)
+ self.last_exception = ex
end
# Update Pry's internal state after evalling code.
View
@@ -27,6 +27,11 @@ def reset_defaults
Pry.reset_defaults
+# this is to test exception code (cat --ex)
+def broken_method
+ this method is broken
+end
+
# sample doc
def sample_method
:sample

0 comments on commit 84b0601

Please sign in to comment.