summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2022-08-19 10:53:42 +0300
committerGitHub <noreply@github.com>2022-08-19 10:53:42 +0300
commit43c2d3c79419dd79babb0f9c5f3920eb9cd4677f (patch)
tree88126158a288763226034257367864e46d4f0a78 /lib
parent0633835d5de65f8099b0905e5aef34be4cae4238 (diff)
parente0ecbb3b28e4183ec0546762144704b086feb468 (diff)
downloadpry-43c2d3c79419dd79babb0f9c5f3920eb9cd4677f.tar.gz
Merge pull request #2253 from dduugg/lazy-eval-regex
Short circuit eval regexes in finding module definition
Diffstat (limited to 'lib')
-rw-r--r--lib/pry/wrapped_module/candidate.rb11
1 files changed, 6 insertions, 5 deletions
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