Skip to content

Commit

Permalink
Save irb:debug's history
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Aug 9, 2023
1 parent 402554a commit fca09eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,18 @@ def self.debug_readline(binding:, irb:)
# 2. Any input that's not irb-command, like `foo = 123`
#
# Irb#eval_input will simply return the input, and we need to pass it to the debugger.
input = irb.eval_input
input = if conf[:SAVE_HISTORY] && irb.context.io.support_history_saving?
# Previous IRB session's history has been saved when `Irb#run` is exited
# We need to clear the old history before we start debugging, so it's not written again
irb.context.io.clear_history
begin
irb.eval_input
ensure
irb.context.io.save_history
end
else
irb.eval_input
end

if input&.include?("\n")
irb.scanner.increase_line_no(input.count("\n") - 1)
Expand Down
5 changes: 5 additions & 0 deletions lib/irb/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ def support_history_saving?
true
end

def clear_history
self.class::HISTORY.clear
@loaded_history_lines = 0 if defined? @loaded_history_lines
end

def load_history
history = self.class::HISTORY
if history_file = IRB.conf[:HISTORY_FILE]
Expand Down
35 changes: 34 additions & 1 deletion test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,40 @@ def with_temp_stdio
end
end

class NestedIRBHistoryTest < IntegrationTestCase
class IRBHistoryIntegrationTest < IntegrationTestCase
def test_history_saving_with_debug
write_history ""

write_ruby <<~'RUBY'
def foo
end
binding.irb
foo
RUBY

output = run_ruby_file do
type "'irb session'"
type "next"
type "'irb:debug session'"
type "step"
type "irb_info"
type "q!"
end

assert_include(output, "InputMethod: RelineInputMethod")

assert_equal <<~HISTORY, @history_file.open.read
'irb session'
next
'irb:debug session'
step
irb_info
q!
HISTORY
end

def test_history_saving_with_nested_sessions
write_history ""

Expand Down

0 comments on commit fca09eb

Please sign in to comment.