Skip to content

Commit

Permalink
config: delete the control_d_handler option
Browse files Browse the repository at this point in the history
This change is a preparational step for #1824 (Enabling `frozen_string_literal:
true` in `~/.pryc` crashes most operations).

The handler was mutating `eval_string` that we pass, and this behaviour will
stop working in Ruby 3.

I was pondering about keeping this option but couldn't come up with an easy
solution. I made a GitHub search to see if anybody uses it, and it seems like
nobody configures it. I am not sure why anybody would want to do that, so I hope
it won't be missed.

The handler's behaviour wasn't deleted, it is still in place. We just moved the
handler's code to `pry_instance.rb`.
  • Loading branch information
kyrylo committed May 3, 2019
1 parent 6f3cd93 commit 6647f4c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 80 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/pry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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)
Expand Down
4 changes: 0 additions & 4 deletions lib/pry/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ class Config
# @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

Expand Down Expand Up @@ -199,7 +196,6 @@ def initialize
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 },
Expand Down
25 changes: 0 additions & 25 deletions lib/pry/control_d_handler.rb

This file was deleted.

24 changes: 23 additions & 1 deletion lib/pry/pry_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def eval(line, options = {})

def handle_line(line, options)
if line.nil?
config.control_d_handler.call(@eval_string, self)
handle_control_d_press
return
end

Expand Down Expand Up @@ -697,5 +697,27 @@ def raise_up!(*args)
def quiet?
config.quiet
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
private :handle_control_d_press
end
# rubocop:enable Metrics/ClassLength
49 changes: 0 additions & 49 deletions spec/control_d_handler_spec.rb

This file was deleted.

53 changes: 53 additions & 0 deletions spec/pry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,57 @@ class Hello
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

0 comments on commit 6647f4c

Please sign in to comment.