Skip to content

Commit 49514f6

Browse files
st0012ko1
authored andcommitted
Extract get_src method
1 parent a2c95ef commit 49514f6

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

lib/debug/session.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ def process_event evt
252252
else
253253
tc << [:eval, :display, @displays]
254254
end
255-
256255
when :result
257256
raise "[BUG] not in subsession" if @subsession_stack.empty?
258257

lib/debug/thread_client.rb

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -415,55 +415,64 @@ def frame_eval src, re_raise: false
415415
raise if re_raise
416416
end
417417

418-
def show_src(frame_index: @current_frame_index,
419-
update_line: false,
420-
max_lines: CONFIG[:show_src_lines] || 10,
418+
def get_src(frame:,
419+
max_lines:,
421420
start_line: nil,
422421
end_line: nil,
423422
dir: +1)
424-
if @target_frames && frame = @target_frames[frame_index]
425-
if file_lines = frame.file_lines
426-
frame_line = frame.location.lineno - 1
427-
428-
lines = file_lines.map.with_index do |e, i|
429-
cur = i == frame_line ? '=>' : ' '
430-
line = colorize_dim('%4d|' % (i+1))
431-
"#{cur}#{line} #{e}"
432-
end
423+
if file_lines = frame.file_lines
424+
frame_line = frame.location.lineno - 1
433425

434-
unless start_line
435-
if frame.show_line
436-
if dir > 0
437-
start_line = frame.show_line
438-
else
439-
end_line = frame.show_line - max_lines
440-
start_line = [end_line - max_lines, 0].max
441-
end
426+
lines = file_lines.map.with_index do |e, i|
427+
cur = i == frame_line ? '=>' : ' '
428+
line = colorize_dim('%4d|' % (i+1))
429+
"#{cur}#{line} #{e}"
430+
end
431+
432+
unless start_line
433+
if frame.show_line
434+
if dir > 0
435+
start_line = frame.show_line
442436
else
443-
start_line = [frame_line - max_lines/2, 0].max
437+
end_line = frame.show_line - max_lines
438+
start_line = [end_line - max_lines, 0].max
444439
end
440+
else
441+
start_line = [frame_line - max_lines/2, 0].max
445442
end
443+
end
446444

447-
unless end_line
448-
end_line = [start_line + max_lines, lines.size].min
449-
end
445+
unless end_line
446+
end_line = [start_line + max_lines, lines.size].min
447+
end
450448

449+
if start_line != end_line && max_lines
450+
[start_line, end_line, lines]
451+
end
452+
else # no file lines
453+
nil
454+
end
455+
rescue Exception => e
456+
p e
457+
pp e.backtrace
458+
exit!
459+
end
460+
461+
def show_src(frame_index: @current_frame_index, update_line: false, max_lines: CONFIG[:show_src_lines] || 10, **options)
462+
if frame = get_frame(frame_index)
463+
start_line, end_line, lines = *get_src(frame: frame, max_lines: max_lines, **options)
464+
465+
if start_line
451466
if update_line
452467
frame.show_line = end_line
453468
end
454469

455-
if start_line != end_line && max_lines
456-
puts "[#{start_line+1}, #{end_line}] in #{frame.pretty_path}" if !update_line && max_lines != 1
457-
puts lines[start_line ... end_line]
458-
end
459-
else # no file lines
470+
puts "[#{start_line+1}, #{end_line}] in #{frame.pretty_path}" if !update_line && max_lines != 1
471+
puts lines[start_line...end_line]
472+
else
460473
puts "# No sourcefile available for #{frame.path}"
461474
end
462475
end
463-
rescue Exception => e
464-
p e
465-
pp e.backtrace
466-
exit!
467476
end
468477

469478
def current_frame

0 commit comments

Comments
 (0)