Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,15 @@ def register_default_command

# * `whereami`
# * Show the current frame with source code.
register_command 'whereami', unsafe: false do
request_tc [:show, :whereami]
# * `whereami -n`
# * Show the current frame with source code without line numbers.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main motivation for this comes from the documentation in pry:

The -n flag can be used to hide line numbers so that code can be copy/pasted
effectively.

register_command 'whereami', unsafe: false do |arg|
case arg ? arg.strip : nil
when "-n"
request_tc [:show, :whereami, {no_lineno: true}]
else
request_tc [:show, :whereami]
end
end

# * `edit`
Expand Down
12 changes: 7 additions & 5 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,13 @@ def get_src(frame,
max_lines:,
start_line: nil,
end_line: nil,
dir: +1)
dir: +1,
no_lineno: CONFIG[:no_lineno]
)
if file_lines = frame.file_lines
frame_line = frame.location.lineno - 1

if CONFIG[:no_lineno]
if no_lineno
lines = file_lines
else
lines = file_lines.map.with_index do |e, i|
Expand Down Expand Up @@ -509,15 +511,15 @@ def get_src(frame,
exit!
end

def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_line: false, max_lines: CONFIG[:show_src_lines], **options)
def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_line: false, max_lines: CONFIG[:show_src_lines], no_lineno: CONFIG[:no_lineno], **options)
if frame = get_frame(frame_index)
begin
if ignore_show_line
prev_show_line = frame.show_line
frame.show_line = nil
end

start_line, end_line, lines = *get_src(frame, max_lines: max_lines, **options)
start_line, end_line, lines = *get_src(frame, max_lines: max_lines, no_lineno: no_lineno, **options)

if start_line
if update_line
Expand Down Expand Up @@ -1119,7 +1121,7 @@ def wait_next_action_
show_src(update_line: true, **(args.first || {}))

when :whereami
show_src ignore_show_line: true
show_src(ignore_show_line: true, **(args.first || {}))
show_frames CONFIG[:show_frames]

when :edit
Expand Down
105 changes: 80 additions & 25 deletions test/console/whereami_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ class WhereamiTest < ConsoleTestCase
def program
<<~RUBY
1| a = 1
2| a = 1
3| a = 1
4| a = 1
5| a = 1
6| a = 1
7| a = 1
8| a = 1
9| a = 1
10| a = 1
11| b = 1
12| b = 1
13| b = 1
14| b = 1
15| b = 1
16| b = 1
17| b = 1
18| b = 1
19| b = 1
20| b = 1
21| c = 1
2| b = 1
3| c = 1
4| d = 1
5| e = 1
6| f = 1
7| g = 1
8| h = 1
9| i = 1
10| j = 1
11| k = 1
12| l = 1
13| m = 1
14| n = 1
15| o = 1
16| p = 1
17| q = 1
18| r = 1
19| s = 1
20| t = 1
21| u = 1
RUBY
end

Expand All @@ -36,20 +36,75 @@ def test_whereami_displays_current_frames_code
type "list"

# after 2 list commands, we should advance to the next 10 lines and not able to see the current frame's source
assert_no_line_text(/=> 1\| a = 1/)
assert_line_text(/b = 1/)
assert_no_line_text(/=> 10\| j = 1/)
assert_line_text(/k = 1/)

type "whereami"

# with whereami, we should see the current frame's source but have no visual outside the closest 10 lines
assert_no_line_text(/b = 1/)
assert_no_line_text(/k = 1/)
assert_line_text(/=> 1\| a = 1/)

type "list"

# list command should work as normal after whereami is executed
assert_no_line_text(/b = 1/)
assert_line_text(/c = 1/)
assert_no_line_text(/t = 1/)
assert_line_text(/u = 1/)

type "continue"
end
end

def test_whereami_n_argument_removes_line_numbers
debug_code(program) do
type "whereami"

assert_line_text([
/1\| a = 1/,
/2\| b = 1/,
/3\| c = 1/,
/4\| d = 1/,
/5\| e = 1/,
/6\| f = 1/,
/7\| g = 1/,
/8\| h = 1/,
/9\| i = 1/,
/10\| j = 1/
])

type "whereami -n"

# with -n there should be no line markers at all
assert_no_line_text(/\|/)
assert_line_text([
/a = 1/,
/b = 1/,
/c = 1/,
/d = 1/,
/e = 1/,
/f = 1/,
/g = 1/,
/h = 1/,
/i = 1/,
/j = 1/
])


# whereami should work as normal again
type "whereami"

assert_line_text([
/1\| a = 1/,
/2\| b = 1/,
/3\| c = 1/,
/4\| d = 1/,
/5\| e = 1/,
/6\| f = 1/,
/7\| g = 1/,
/8\| h = 1/,
/9\| i = 1/,
/10\| j = 1/
])

type "continue"
end
Expand Down
Loading