diff options
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | lib/pry.rb | 1 | ||||
| -rw-r--r-- | lib/pry/config.rb | 4 | ||||
| -rw-r--r-- | lib/pry/control_d_handler.rb | 25 | ||||
| -rw-r--r-- | lib/pry/pry_instance.rb | 21 | ||||
| -rw-r--r-- | spec/control_d_handler_spec.rb | 49 | ||||
| -rw-r--r-- | spec/pry_spec.rb | 53 |
7 files changed, 76 insertions, 79 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f459e1f1..479ac334 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,8 @@ ([#2003](https://github.com/pry/pry/pull/2003)) * Deleted `Pry.lazy` (use `Pry::Config::LazyValue` instead) ([#2024](https://github.com/pry/pry/pull/2024)) +* Deleted `control_d_handler` configuration option + ([#2024](https://github.com/pry/pry/pull/2024)) #### Bug fixes @@ -23,7 +23,6 @@ require 'pry/history' require 'pry/color_printer' require 'pry/exception_handler' require 'pry/system_command_handler' -require 'pry/control_d_handler' require 'pry/command_state' Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 55718200..f79405be 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -106,9 +106,6 @@ class Pry # @return [Integer] how many input/output lines to keep in memory attribute :memory_size - # @return [Proc] - attribute :control_d_handler - # @return [Proc] The proc that runs system commands attribute :system @@ -199,7 +196,6 @@ class Pry should_load_requires: true, should_load_plugins: true, windows_console_warning: true, - control_d_handler: Pry::ControlDHandler.method(:default), memory_size: 100, extra_sticky_locals: {}, command_completions: proc { commands.keys }, diff --git a/lib/pry/control_d_handler.rb b/lib/pry/control_d_handler.rb deleted file mode 100644 index 1021c767..00000000 --- a/lib/pry/control_d_handler.rb +++ /dev/null @@ -1,25 +0,0 @@ -class Pry - # @api private - # @since ?.?.? - module ControlDHandler - # Deal with the ^D key being pressed. Different behaviour in different - # cases: - # 1. In an expression behave like `!` command. - # 2. At top-level session behave like `exit` command. - # 3. In a nested session behave like `cd ..`. - def self.default(eval_string, pry_instance) - if !eval_string.empty? - eval_string.replace('') # Clear input buffer. - elsif pry_instance.binding_stack.one? - pry_instance.binding_stack.clear - throw(:breakout) - else - # Otherwise, saves current binding stack as old stack and pops last - # binding out of binding stack (the old stack still has that binding). - cd_state = Pry::CommandState.default.state_for('cd') - cd_state.old_stack = pry_instance.binding_stack.dup - pry_instance.binding_stack.pop - end - end - end -end diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 2aebedc2..8fec7ff8 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -695,5 +695,26 @@ class Pry def prompt_stack @prompt_stack ||= [] end + + # Deal with the ^D key being pressed. Different behaviour in different + # cases: + # 1. In an expression behave like `!` command. + # 2. At top-level session behave like `exit` command. + # 3. In a nested session behave like `cd ..`. + def handle_control_d_press + if !@eval_string.empty? + # Clear input buffer. + @eval_string = '' + elsif binding_stack.one? + binding_stack.clear + throw(:breakout) + else + # Otherwise, saves current binding stack as old stack and pops last + # binding out of binding stack (the old stack still has that binding). + cd_state = Pry::CommandState.default.state_for('cd') + cd_state.old_stack = binding_stack.dup + binding_stack.pop + end + end end # rubocop:enable Metrics/ClassLength diff --git a/spec/control_d_handler_spec.rb b/spec/control_d_handler_spec.rb deleted file mode 100644 index 5e21ccbe..00000000 --- a/spec/control_d_handler_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -RSpec.describe Pry::ControlDHandler do - context "when given eval string is non-empty" do - let(:eval_string) { 'hello' } - let(:pry_instance) { Pry.new } - - it "clears input buffer" do - described_class.default(eval_string, pry_instance) - expect(eval_string).to be_empty - end - end - - context "when given eval string is empty & pry instance has one binding" do - let(:eval_string) { '' } - let(:pry_instance) { Pry.new.tap { |p| p.binding_stack = [binding] } } - - it "throws :breakout" do - expect { described_class.default(eval_string, pry_instance) } - .to throw_symbol(:breakout) - end - - it "clears binding stack" do - expect { described_class.default(eval_string, pry_instance) } - .to throw_symbol - expect(pry_instance.binding_stack).to be_empty - end - end - - context "when given eval string is empty & pry instance has 2+ bindings" do - let(:eval_string) { '' } - let(:binding1) { binding } - let(:binding2) { binding } - let(:binding_stack) { [binding1, binding2] } - - let(:pry_instance) do - Pry.new.tap { |p| p.binding_stack = binding_stack } - end - - it "saves a dup of the current binding stack in the 'cd' command" do - described_class.default(eval_string, pry_instance) - cd_state = pry_instance.commands['cd'].state - expect(cd_state.old_stack).to eq([binding1, binding2]) - end - - it "pops the binding off the stack" do - described_class.default(eval_string, pry_instance) - expect(pry_instance.binding_stack).to eq([binding1]) - end - end -end diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index da73c275..93e55b9c 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -466,4 +466,57 @@ describe Pry do expect(backtrace.any? { |l| l.include?(location) }).to equal true end end + + describe "#eval" do + context "when line is nil" do + let(:line) { nil } + + context "and when eval string is non-empty" do + before { subject.eval_string = 'hello' } + + it "clears input buffer" do + subject.eval(line) + expect(subject.eval_string).to be_empty + end + end + + context "and when eval string is empty & pry instance has one binding" do + before do + subject.eval_string = '' + subject.binding_stack = [binding] + end + + it "catches :breakout" do + expect { subject.eval(line) }.not_to throw_symbol(:breakout) + end + + it "clears binding stack" do + subject.eval(line) + expect(subject.binding_stack).to be_empty + end + end + + context "and when given eval string is empty & pry instance has 2+ bindings" do + let(:binding1) { binding } + let(:binding2) { binding } + let(:binding_stack) { [binding1, binding2] } + + before do + subject.eval_string = '' + subject.binding_stack = binding_stack + end + + it "saves a dup of the current binding stack in the 'cd' command" do + subject.eval(line) + cd_state = subject.commands['cd'].state + expect(cd_state.old_stack).to eq([binding1, binding2]) + end + + it "pops the binding off the stack" do + subject.eval(line) + expect(subject.binding_stack).to eq([binding1]) + end + end + end + end end |
