From e0ecbb3b28e4183ec0546762144704b086feb468 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 15 Aug 2022 12:38:45 -0700 Subject: Short circuit eval regexes in finding module definition --- lib/pry/wrapped_module/candidate.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/pry/wrapped_module/candidate.rb b/lib/pry/wrapped_module/candidate.rb index f48f8751..7a0c3d66 100644 --- a/lib/pry/wrapped_module/candidate.rb +++ b/lib/pry/wrapped_module/candidate.rb @@ -98,14 +98,15 @@ class Pry # line number is one-indexed. def first_line_of_module_definition(file, line) searchable_lines = lines_for_file(file)[0..(line - 2)] - searchable_lines.rindex { |v| class_regexes.any? { |r| r =~ v } } + 1 + searchable_lines.rindex { |v| module_definition_first_line?(v) } + 1 end - def class_regexes + def module_definition_first_line?(line) mod_type_string = wrapped.class.to_s.downcase - [/(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/, - /^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/, - /^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/] + wrapped_name_last = wrapped.name.split(/::/).last + /(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped_name_last}/ =~ line || + /^\s*(::)?#{wrapped_name_last}\s*?=\s*?#{wrapped.class}/ =~ line || + /^\s*(::)?#{wrapped_name_last}\.(class|instance)_eval/ =~ line end # This method is used by `Candidate#source_location` as a -- cgit v1.2.1