From 6f2e59d64e4ce2b1fdd2ca05c202d3e5ecb70450 Mon Sep 17 00:00:00 2001 From: k0kubun Date: Fri, 24 Nov 2017 03:53:27 +0000 Subject: irb.rb: show source around binding.irb on start [Feature #14124] [ruby-dev:50319] [close GH-1764] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/irb/workspace.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib/irb') diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 9051f96df0..5c1e97a981 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -107,6 +107,27 @@ EOF bt end + def code_around_binding + file = @binding.eval('__FILE__') + pos = @binding.eval('__LINE__') - 1 + return nil unless File.exist?(file) + + if defined?(SCRIPT_LINES__) && SCRIPT_LINES__.key?(file) + lines = SCRIPT_LINES__[file].split(/^/) + else + lines = File.readlines(file) + end + + start_pos = [pos - 5, 0].max + end_pos = [pos + 5, lines.size - 1].min + + body = (start_pos..end_pos).map do |current_pos| + lineno = "%#{end_pos.to_s.length}d" % (current_pos + 1) + " #{pos == current_pos ? '=>' : ' '} #{lineno}: #{lines[current_pos]}" + end.join + "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}\n" + end + def IRB.delete_caller end end -- cgit v1.2.3