[ruby/irb] Simplify show_source's super calculation
(https://github.com/ruby/irb/pull/807) https://github.com/ruby/irb/commit/2cccc448de
This commit is contained in:
parent
04eb1b6f26
commit
130268e264
@ -27,17 +27,14 @@ module IRB
|
|||||||
puts "Error: Expected a string but got #{str.inspect}"
|
puts "Error: Expected a string but got #{str.inspect}"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if str.include? " -s"
|
|
||||||
str, esses = str.split(" -")
|
str, esses = str.split(" -")
|
||||||
s_count = esses.count("^s").zero? ? esses.size : 1
|
super_level = esses ? esses.count("s") : 0
|
||||||
source = SourceFinder.new(@irb_context).find_source(str, s_count)
|
source = SourceFinder.new(@irb_context).find_source(str, super_level)
|
||||||
else
|
|
||||||
source = SourceFinder.new(@irb_context).find_source(str)
|
|
||||||
end
|
|
||||||
|
|
||||||
if source
|
if source
|
||||||
show_source(source)
|
show_source(source)
|
||||||
elsif s_count
|
elsif super_level > 0
|
||||||
puts "Error: Couldn't locate a super definition for #{str}"
|
puts "Error: Couldn't locate a super definition for #{str}"
|
||||||
else
|
else
|
||||||
puts "Error: Couldn't locate a definition for #{str}"
|
puts "Error: Couldn't locate a definition for #{str}"
|
||||||
|
@ -16,7 +16,7 @@ module IRB
|
|||||||
@irb_context = irb_context
|
@irb_context = irb_context
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_source(signature, s_count = nil)
|
def find_source(signature, super_level = 0)
|
||||||
context_binding = @irb_context.workspace.binding
|
context_binding = @irb_context.workspace.binding
|
||||||
case signature
|
case signature
|
||||||
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
|
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
|
||||||
@ -27,12 +27,12 @@ module IRB
|
|||||||
owner = eval(Regexp.last_match[:owner], context_binding)
|
owner = eval(Regexp.last_match[:owner], context_binding)
|
||||||
method = Regexp.last_match[:method]
|
method = Regexp.last_match[:method]
|
||||||
return unless owner.respond_to?(:instance_method)
|
return unless owner.respond_to?(:instance_method)
|
||||||
file, line = method_target(owner, s_count, method, "owner")
|
file, line = method_target(owner, super_level, method, "owner")
|
||||||
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
|
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
|
||||||
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
|
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
|
||||||
method = Regexp.last_match[:method]
|
method = Regexp.last_match[:method]
|
||||||
return unless receiver.respond_to?(method, true)
|
return unless receiver.respond_to?(method, true)
|
||||||
file, line = method_target(receiver, s_count, method, "receiver")
|
file, line = method_target(receiver, super_level, method, "receiver")
|
||||||
end
|
end
|
||||||
if file && line && File.exist?(file)
|
if file && line && File.exist?(file)
|
||||||
Source.new(file: file, first_line: line, last_line: find_end(file, line))
|
Source.new(file: file, first_line: line, last_line: find_end(file, line))
|
||||||
@ -60,20 +60,14 @@ module IRB
|
|||||||
first_line
|
first_line
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_target(owner_receiver, s_count, method, type)
|
def method_target(owner_receiver, super_level, method, type)
|
||||||
case type
|
case type
|
||||||
when "owner"
|
when "owner"
|
||||||
target_method = owner_receiver.instance_method(method)
|
target_method = owner_receiver.instance_method(method)
|
||||||
return target_method.source_location unless s_count
|
|
||||||
when "receiver"
|
when "receiver"
|
||||||
if s_count
|
target_method = owner_receiver.method(method)
|
||||||
target_method = owner_receiver.class.instance_method(method)
|
|
||||||
else
|
|
||||||
target_method = method
|
|
||||||
return owner_receiver.method(method).source_location
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
s_count.times do |s|
|
super_level.times do |s|
|
||||||
target_method = target_method.super_method if target_method
|
target_method = target_method.super_method if target_method
|
||||||
end
|
end
|
||||||
target_method.nil? ? nil : target_method.source_location
|
target_method.nil? ? nil : target_method.source_location
|
||||||
|
@ -39,6 +39,19 @@ module TestIRB
|
|||||||
assert_match(%r[/irb\/init\.rb], out)
|
assert_match(%r[/irb\/init\.rb], out)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_show_source_with_missing_signature
|
||||||
|
write_ruby <<~'RUBY'
|
||||||
|
binding.irb
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
out = run_ruby_file do
|
||||||
|
type "show_source foo"
|
||||||
|
type "exit"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match(%r[Couldn't locate a definition for foo], out)
|
||||||
|
end
|
||||||
|
|
||||||
def test_show_source_string
|
def test_show_source_string
|
||||||
write_ruby <<~'RUBY'
|
write_ruby <<~'RUBY'
|
||||||
binding.irb
|
binding.irb
|
||||||
|
Loading…
x
Reference in New Issue
Block a user