summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
commit106c9883e11673fa31e3854d7e24b97f4c1ba9fd (patch)
treeec8528ed8bf060ad891770220295422bbd8a62da /lib
parentf8523d35bf3e8ec4a705b1dd45648e1b4352199a (diff)
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')
-rw-r--r--lib/irb.rb4
-rw-r--r--lib/irb/workspace.rb21
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index 3c549d2c6e..dcc160f140 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -706,6 +706,8 @@ class Binding
undef irb if method_defined?(:irb)
def irb
IRB.setup(eval("__FILE__"))
- IRB::Irb.new(IRB::WorkSpace.new(self)).run(IRB.conf)
+ workspace = IRB::WorkSpace.new(self)
+ STDOUT.print(workspace.code_around_binding)
+ IRB::Irb.new(workspace).run(IRB.conf)
end
end
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