Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`show-source -v` => `show-source -a`

* switch to show all monkeypatches is now -a (for all)
* improved some method names (top_method_candidates => method_candidates in wrapped_module.rb)
* improved in-session documentation for show-source and show-doc
  • Loading branch information...
commit e6b280d488ed3ace4a8e52b4f1d2967a498570d8 1 parent af5e1f8
@banister banister authored
View
10 lib/pry/default_commands/find_method.rb
@@ -11,6 +11,16 @@ module DefaultCommands
description "Recursively search for a method within a Class/Module or the current namespace. find-method [-n | -c] METHOD [NAMESPACE]"
+ banner <<-BANNER
+ Usage: find-method [-n | -c] METHOD [NAMESPACE]
+
+ Recursively search for a method within a Class/Module or the current namespace.
+ Use the `-n` switch (the default) to search for methods whose name matches the given regex.
+ Use the `-c` switch to search for methods that contain the given code.
+
+ e.g find re Pry # find all methods whose name match /re/. Matches Pry#repl, etc.
+ e.g find -c 'output.puts' Pry # find all methods that contain the code: output.puts
+ BANNER
def setup
require 'ruby18_source_location' if mri_18?
View
30 lib/pry/default_commands/introspection.rb
@@ -50,8 +50,12 @@ def use_line_numbers?
banner <<-BANNER
Usage: show-doc [OPTIONS] [METH]
+ Aliases: ?
+
Show the comments above method METH. Tries instance methods first and then methods by default.
- e.g show-doc hello_method
+ e.g show-doc hello_method # docs for hello_method
+ e.g show-doc Pry # docs for Pry class
+ e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
BANNER
def options(opt)
@@ -59,14 +63,14 @@ def options(opt)
opt.on :l, "line-numbers", "Show line numbers."
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
- opt.on :v, :verbose, "Show docs for all candidates (modules only)"
+ opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module (modules only)"
end
def process_module(name)
mod = Pry::WrappedModule.from_str(name)
- if opts.present?(:verbose)
- verbose_module(mod)
+ if opts.present?(:all)
+ all_modules(mod)
else
normal_module(mod)
end
@@ -97,7 +101,7 @@ def normal_module(mod)
end
end
- def verbose_module(mod)
+ def all_modules(mod)
doc = ""
doc << "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
@@ -190,8 +194,9 @@ def process
e.g: `show-source hello_method`
e.g: `show-source -m hello_method`
- e.g: `show-source Pry#rep`
- e.g: `show-source Pry`
+ e.g: `show-source Pry#rep` # source for Pry#rep method
+ e.g: `show-source Pry` # source for Pry class
+ e.g: `show-source Pry -a` # source for all Pry class definitions (all monkey patches)
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
BANNER
@@ -205,7 +210,7 @@ def options(opt)
opt.on :l, "line-numbers", "Show line numbers."
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
- opt.on :v, :verbose, "Show source for all candidates (modules only)"
+ opt.on :a, :all, "Show source for all definitions and monkeypatches of the module (modules only)"
end
def process_method
@@ -224,12 +229,11 @@ def process_method
def process_module(name)
mod = Pry::WrappedModule.from_str(name)
- if opts.present?(:verbose)
- verbose_module(mod)
+ if opts.present?(:all)
+ all_modules(mod)
else
normal_module(mod)
end
-
end
def normal_module(mod)
@@ -240,12 +244,12 @@ def normal_module(mod)
code << Code.from_module(mod, module_start_line(mod)).with_line_numbers(use_line_numbers?)
end
- def verbose_module(mod)
+ def all_modules(mod)
code = ""
code << "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
begin
- code << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{mod.source_file_for_candidate(v)} @ #{mod.source_line_for_candidate(v)}:\n\n"
+ code << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{mod.source_file_for_candidate(v)} @ line #{mod.source_line_for_candidate(v)}:\n\n"
code << Code.new(mod.source_for_candidate(v), module_start_line(mod, v)).with_line_numbers(use_line_numbers?).to_s
rescue Pry::RescuableException
next
View
15 lib/pry/wrapped_module.rb
@@ -199,20 +199,19 @@ def module_source_location_for_candidate(idx)
return nil if !file.is_a?(String)
- class_regex = /#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/
+ class_regex1 = /#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/
+ class_regex2 = /(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/
host_file_lines = lines_for_file(file)
search_lines = host_file_lines[0..(line - 2)]
- idx = search_lines.rindex { |v| class_regex =~ v }
+ idx = search_lines.rindex { |v| class_regex1 =~ v || class_regex2 =~ v }
source_location = [file, idx + 1]
rescue Pry::RescuableException
nil
end
- #private
-
def extract_doc
extract_doc_for_candidate(0)
end
@@ -275,19 +274,19 @@ def all_source_locations_by_popularity
sort_by { |k, v| -v.size }
end
- def top_method_candidates
- @top_method_candidtates ||= all_source_locations_by_popularity.map do |group|
+ def method_candidates
+ @method_candidtates ||= all_source_locations_by_popularity.map do |group|
sorted_by_lowest_line_number = group.last.sort_by(&:source_line)
best_candidate_for_group = sorted_by_lowest_line_number.first
end
end
def number_of_candidates
- top_method_candidates.count
+ method_candidates.count
end
def method_source_location_for_candidate(idx)
- file, line = top_method_candidates[idx].source_location
+ file, line = method_candidates[idx].source_location
if file && RbxPath.is_core_path?(file)
file = RbxPath.convert_path_to_full(file)
Please sign in to comment.
Something went wrong with that request. Please try again.