Skip to content

Commit 0ae7550

Browse files
committed
Add finish command
1 parent 9f1a010 commit 0ae7550

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ pry-debugger
33

44
_Fast execution control in Pry_
55

6-
Adds **step**, **next**, and **continue** commands and **breakpoints** to
7-
[Pry][pry] using [debugger][debugger].
6+
Adds **step**, **next**, **finish**, and **continue** commands and
7+
**breakpoints** to [Pry][pry] using [debugger][debugger].
88

99
To use, invoke pry normally. No need to start your script or app differently.
1010

@@ -15,9 +15,8 @@ def some_method
1515
end
1616
```
1717

18-
To create a fully flexible debugging environment,
19-
[pry-stack_explorer][pry-stack_explorer] is recommended as a companion Pry
20-
plugin to add call-stack frame navigation.
18+
For a complete debugging environment, add
19+
[pry-stack_explorer][pry-stack_explorer] for call-stack frame navigation.
2120

2221

2322
## Execution Commands
@@ -28,6 +27,8 @@ argument to step multiple times.
2827
**next:** Step over to the next line within the same frame. Also takes an
2928
optional numeric argument to step multiple lines.
3029

30+
**finish:** Execute until current stack frame returns.
31+
3132
**continue:** Continue program execution and end the Pry session.
3233

3334

@@ -36,9 +37,9 @@ optional numeric argument to step multiple lines.
3637
You can set and adjust breakpoints directly from a Pry session using the
3738
following commands:
3839

39-
**break:** Set a new breakpoint. Accepts a line number in the current file, a
40-
file and line number, or a method, and an optional condition. By passing various
41-
flags, existing breakpoints can be changed.
40+
**break:** Set a new breakpoint from a line number in the current file, a file
41+
and line number, or a method. Pass an optional expression to create a
42+
conditional breakpoint. Edit existing breakpoints via various flags.
4243

4344
Examples:
4445

@@ -118,6 +119,7 @@ Stepping through code often? Add the following shortcuts to `~/.pryrc`:
118119
Pry.commands.alias_command 'c', 'continue'
119120
Pry.commands.alias_command 's', 'step'
120121
Pry.commands.alias_command 'n', 'next'
122+
Pry.commands.alias_command 'f', 'finish'
121123
```
122124

123125

lib/pry-debugger/commands.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def process
2525

2626

2727
create_command 'next' do
28-
description 'Execute the next line within the same stack frame.'
28+
description 'Execute the next line within the current stack frame.'
2929

3030
banner <<-BANNER
3131
Usage: next [LINES]
@@ -46,6 +46,16 @@ def process
4646
end
4747

4848

49+
create_command 'finish' do
50+
description 'Execute until current stack frame returns.'
51+
52+
def process
53+
check_file_context
54+
breakout_navigation :finish
55+
end
56+
end
57+
58+
4959
create_command 'continue' do
5060
description 'Continue program execution and end the Pry session.'
5161

@@ -194,12 +204,12 @@ def process
194204

195205

196206
helpers do
197-
def breakout_navigation(action, times)
207+
def breakout_navigation(action, times = nil)
198208
_pry_.binding_stack.clear # Clear the binding stack.
199209
throw :breakout_nav, { # Break out of the REPL loop and
200210
:action => action, # signal the tracer.
201-
:times => times,
202-
:pry => _pry_
211+
:times => times,
212+
:pry => _pry_
203213
}
204214
end
205215

lib/pry-debugger/processor.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def run(initial = true, &block)
2222
times = (command[:times] || 1).to_i # Command argument
2323
times = 1 if times <= 0
2424

25-
if [:step, :next].include? command[:action]
25+
if [:step, :next, :finish].include? command[:action]
2626
@pry = command[:pry] # Pry instance to resume after stepping
2727
Debugger.start unless Debugger.started?
2828

@@ -37,8 +37,11 @@ def run(initial = true, &block)
3737
elsif :next == command[:action]
3838
step_over times
3939

40-
else # :step == command[:action]
40+
elsif :step == command[:action]
4141
step times
42+
43+
elsif :finish == command[:action]
44+
finish
4245
end
4346
else
4447
stop
@@ -79,6 +82,10 @@ def at_line(context, file, line)
7982
step @delayed[:step] - 1
8083
@delayed = Hash.new(0)
8184

85+
elsif @delayed[:finish] > 0
86+
finish
87+
@delayed = Hash.new(0)
88+
8289
else # Otherwise, resume the pry session at the stopped line.
8390
resume_pry context
8491
end
@@ -126,6 +133,11 @@ def step_over(lines)
126133
Debugger.current_context.step_over(lines, 0)
127134
end
128135

136+
# Execute until current frame returns.
137+
def finish
138+
Debugger.current_context.stop_frame = 0
139+
end
140+
129141
# Cleanup when debugging is stopped and execution continues.
130142
def stop
131143
Debugger.stop if !@always_enabled && Debugger.started?

0 commit comments

Comments
 (0)