Skip to content

Commit

Permalink
Simplify show_source's super calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Dec 9, 2023
1 parent 23c1d67 commit 8b94fa9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
13 changes: 5 additions & 8 deletions lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ def execute(str = nil)
puts "Error: Expected a string but got #{str.inspect}"
return
end
if str.include? " -s"
str, esses = str.split(" -")
s_count = esses.count("^s").zero? ? esses.size : 1
source = SourceFinder.new(@irb_context).find_source(str, s_count)
else
source = SourceFinder.new(@irb_context).find_source(str)
end

str, esses = str.split(" -")
super_level = esses ? esses.count("s") : 0
source = SourceFinder.new(@irb_context).find_source(str, super_level)

if source
show_source(source)
elsif s_count
elsif super_level
puts "Error: Couldn't locate a super definition for #{str}"
else
puts "Error: Couldn't locate a definition for #{str}"
Expand Down
18 changes: 6 additions & 12 deletions lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(irb_context)
@irb_context = irb_context
end

def find_source(signature, s_count = nil)
def find_source(signature, super_level = 0)
context_binding = @irb_context.workspace.binding
case signature
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
Expand All @@ -27,12 +27,12 @@ def find_source(signature, s_count = nil)
owner = eval(Regexp.last_match[:owner], context_binding)
method = Regexp.last_match[: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
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
method = Regexp.last_match[:method]
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
if file && line && File.exist?(file)
Source.new(file: file, first_line: line, last_line: find_end(file, line))
Expand Down Expand Up @@ -60,20 +60,14 @@ def find_end(file, first_line)
first_line
end

def method_target(owner_receiver, s_count, method, type)
def method_target(owner_receiver, super_level, method, type)
case type
when "owner"
target_method = owner_receiver.instance_method(method)
return target_method.source_location unless s_count
when "receiver"
if s_count
target_method = owner_receiver.class.instance_method(method)
else
target_method = method
return owner_receiver.method(method).source_location
end
target_method = owner_receiver.method(method)
end
s_count.times do |s|
super_level.times do |s|
target_method = target_method.super_method if target_method
end
target_method.nil? ? nil : target_method.source_location
Expand Down

0 comments on commit 8b94fa9

Please sign in to comment.