diff options
author | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-24 03:53:27 +0000 |
---|---|---|
committer | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-24 03:53:27 +0000 |
commit | 106c9883e11673fa31e3854d7e24b97f4c1ba9fd (patch) | |
tree | ec8528ed8bf060ad891770220295422bbd8a62da /lib/irb/workspace.rb | |
parent | f8523d35bf3e8ec4a705b1dd45648e1b4352199a (diff) | |
download | ruby-106c9883e11673fa31e3854d7e24b97f4c1ba9fd.tar.gz |
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
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 |