mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Slightly refactor REPL initialization
This commit is contained in:
parent
2cfbd49f52
commit
3c6ffaf69c
5 changed files with 30 additions and 17 deletions
|
@ -137,7 +137,8 @@ class Pry
|
||||||
end
|
end
|
||||||
alias current_context current_binding # support previous API
|
alias current_context current_binding # support previous API
|
||||||
|
|
||||||
# Push a binding for the given object onto the stack.
|
# Push a binding for the given object onto the stack. If this instance is
|
||||||
|
# currently stopped, mark it as usable again.
|
||||||
def push_binding(object)
|
def push_binding(object)
|
||||||
@stopped = false
|
@stopped = false
|
||||||
binding_stack << Pry.binding_for(object)
|
binding_stack << Pry.binding_for(object)
|
||||||
|
@ -304,7 +305,7 @@ class Pry
|
||||||
complete_expr = Pry::Code.complete_expression?(@eval_string)
|
complete_expr = Pry::Code.complete_expression?(@eval_string)
|
||||||
rescue SyntaxError => e
|
rescue SyntaxError => e
|
||||||
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
|
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
|
||||||
@eval_string = ""
|
reset_line
|
||||||
end
|
end
|
||||||
|
|
||||||
if complete_expr
|
if complete_expr
|
||||||
|
@ -332,10 +333,20 @@ class Pry
|
||||||
throw(:breakout) if current_binding.nil?
|
throw(:breakout) if current_binding.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
# @deprecated - Please use Pry::REPL.start(:pry => pry, :target => target) instead.
|
# @deprecated Use `Pry::REPL.new(pry, :target => target).start` instead.
|
||||||
def repl(target=binding_stack.last)
|
def repl(target = nil)
|
||||||
@@repl_warning ||= (warn "(deprecation) Pry#repl has been replaced by Pry::REPL.start(:pry => pry)"; true)
|
@@repl_warning ||= (warn Pry::Helpers::CommandHelpers.unindent(<<-S); true)
|
||||||
Pry::REPL.start(:pry => self, :target => target)
|
DEPRECATION: Pry#repl is deprecated. Instead, use
|
||||||
|
|
||||||
|
Pry::REPL.new(pry, :target => target).start
|
||||||
|
|
||||||
|
where pry is the Pry instance you called #repl on and target is the
|
||||||
|
optional target parameter of #repl.
|
||||||
|
|
||||||
|
Call stack:
|
||||||
|
#{caller.join("\n" + (' ' * 8))}
|
||||||
|
S
|
||||||
|
Pry::REPL.new(self, :target => target).start
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate_ruby(code)
|
def evaluate_ruby(code)
|
||||||
|
@ -551,7 +562,7 @@ class Pry
|
||||||
|
|
||||||
Pry.critical_section do
|
Pry.critical_section do
|
||||||
# If input buffer is empty then use normal prompt
|
# If input buffer is empty then use normal prompt
|
||||||
if @eval_string.empty?
|
if eval_string.empty?
|
||||||
generate_prompt(Array(prompt).first, c)
|
generate_prompt(Array(prompt).first, c)
|
||||||
|
|
||||||
# Otherwise use the wait prompt (indicating multi-line expression)
|
# Otherwise use the wait prompt (indicating multi-line expression)
|
||||||
|
|
|
@ -8,15 +8,16 @@ class Pry
|
||||||
def_delegators :pry, :input, :output, :input_stack
|
def_delegators :pry, :input, :output, :input_stack
|
||||||
|
|
||||||
def self.start(options)
|
def self.start(options)
|
||||||
new(options).start
|
new(Pry.new(options)).start
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(pry, options = {})
|
||||||
@pry = options[:pry] || Pry.new(options)
|
@pry = pry
|
||||||
if options[:pry] && options[:target]
|
@indent = Pry::Indent.new
|
||||||
|
|
||||||
|
if options[:target]
|
||||||
@pry.push_binding options[:target]
|
@pry.push_binding options[:target]
|
||||||
end
|
end
|
||||||
@indent = Pry::Indent.new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ReplTester
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@pry = Pry.new
|
@pry = Pry.new
|
||||||
@repl = Pry::REPL.new(:pry => @pry)
|
@repl = Pry::REPL.new(@pry)
|
||||||
|
|
||||||
@fiber = Fiber.new do
|
@fiber = Fiber.new do
|
||||||
@repl.start
|
@repl.start
|
||||||
|
|
|
@ -30,9 +30,10 @@ describe "Pry#input_stack" do
|
||||||
|
|
||||||
it 'input objects should be popped off stack as they are used up' do
|
it 'input objects should be popped off stack as they are used up' do
|
||||||
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
|
stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
|
||||||
repl = Pry::REPL.new :input => StringIO.new(":alex\n"),
|
pry = Pry.new :input => StringIO.new(":alex\n"),
|
||||||
:output => @str_output,
|
:output => @str_output,
|
||||||
:input_stack => stack
|
:input_stack => stack
|
||||||
|
repl = Pry::REPL.new pry
|
||||||
stack.size.should == 2
|
stack.size.should == 2
|
||||||
repl.send(:retrieve_line).should == ":alex\n"
|
repl.send(:retrieve_line).should == ":alex\n"
|
||||||
stack.size.should == 2
|
stack.size.should == 2
|
||||||
|
|
|
@ -15,7 +15,7 @@ describe "The REPL" do
|
||||||
|
|
||||||
it "shouldn't break if we start a nested session" do
|
it "shouldn't break if we start a nested session" do
|
||||||
ReplTester.start do |t|
|
ReplTester.start do |t|
|
||||||
t.in 'Pry::REPL.start(:pry => _pry_, :target => 10)'
|
t.in 'Pry::REPL.new(_pry_, :target => 10).start'
|
||||||
t.out ''
|
t.out ''
|
||||||
t.prompt /10.*> $/
|
t.prompt /10.*> $/
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue