From 92d187d590a65943a81ffaca8c7fbef0a7420e32 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 27 Jul 2023 12:14:58 +0100 Subject: [PATCH] Refactor SourceFinder --- lib/irb/source_finder.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb index 342dec043..346b53144 100644 --- a/lib/irb/source_finder.rb +++ b/lib/irb/source_finder.rb @@ -18,33 +18,34 @@ def initialize(irb_context) end def find_source(signature) + context_binding = @irb_context.workspace.binding case signature when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name - eval(signature, @irb_context.workspace.binding) # trigger autoload - base = @irb_context.workspace.binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object } + eval(signature, context_binding) # trigger autoload + base = context_binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object } file, line = base.const_source_location(signature) when /\A(?[A-Z]\w*(::[A-Z]\w*)*)#(?[^ :.]+)\z/ # Class#method - owner = eval(Regexp.last_match[:owner], @irb_context.workspace.binding) + owner = eval(Regexp.last_match[:owner], context_binding) method = Regexp.last_match[:method] if owner.respond_to?(:instance_method) methods = owner.instance_methods + owner.private_instance_methods file, line = owner.instance_method(method).source_location if methods.include?(method.to_sym) end when /\A((?.+)(\.|::))?(?[^ :.]+)\z/ # method, receiver.method, receiver::method - receiver = eval(Regexp.last_match[:receiver] || 'self', @irb_context.workspace.binding) + receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding) method = Regexp.last_match[:method] file, line = receiver.method(method).source_location if receiver.respond_to?(method, true) end if file && line && File.exist?(file) - Source.new(file: file, first_line: line, last_line: find_end(file, line, @irb_context)) + Source.new(file: file, first_line: line, last_line: find_end(file, line)) end end private - def find_end(file, first_line, irb_context) + def find_end(file, first_line) return first_line unless File.exist?(file) - lex = RubyLex.new(irb_context) + lex = RubyLex.new(@irb_context) lines = File.read(file).lines[(first_line - 1)..-1] tokens = RubyLex.ripper_lex_without_warning(lines.join) prev_tokens = []