diff options
Diffstat (limited to 'lib/irb/workspace.rb')
-rw-r--r-- | lib/irb/workspace.rb | 21 |
1 files changed, 21 insertions, 0 deletions
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 |