2012-03-31 22:24:28 -04:00
|
|
|
class Pry
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::FindMethod < Pry::ClassCommand
|
2012-08-11 20:22:29 -04:00
|
|
|
extend Pry::Helpers::BaseHelpers
|
2012-04-02 10:22:06 -04:00
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
match 'find-method'
|
|
|
|
group 'Context'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'Recursively search for a method within a Class/Module or the current namespace.'
|
|
|
|
command_options :shellwords => false
|
|
|
|
command_options :requires_gem => 'ruby18_source_location' if mri_18?
|
2012-04-02 10:22:06 -04:00
|
|
|
|
2012-04-12 22:55:30 -04:00
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
banner <<-'BANNER'
|
|
|
|
Usage: find-method [-n|-c] METHOD [NAMESPACE]
|
2012-04-02 10:22:06 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
Recursively search for a method within a Class/Module or the current namespace.
|
2013-01-09 15:23:19 -05:00
|
|
|
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.
|
2012-04-17 08:20:36 -04:00
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
# Find all methods whose name match /re/ inside
|
|
|
|
# the Pry namespace. Matches Pry#repl, etc.
|
|
|
|
find-method re Pry
|
|
|
|
|
|
|
|
# Find all methods that contain the code:
|
|
|
|
# output.puts inside the Pry namepsace.
|
|
|
|
find-method -c 'output.puts' Pry
|
2012-08-11 20:22:29 -04:00
|
|
|
BANNER
|
2012-04-17 08:20:36 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def setup
|
|
|
|
require 'ruby18_source_location' if mri_18?
|
|
|
|
end
|
2012-04-12 22:55:30 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def options(opti)
|
2013-01-09 15:23:19 -05:00
|
|
|
opti.on :n, :name, "Search for a method by name"
|
2012-08-11 20:22:29 -04:00
|
|
|
opti.on :c, :content, "Search for a method based on content in Regex form"
|
|
|
|
end
|
2012-04-12 22:55:30 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def process
|
|
|
|
return if args.size < 1
|
2013-01-03 10:48:46 -05:00
|
|
|
klass = search_class
|
|
|
|
|
|
|
|
matches = if opts.content?
|
2013-01-03 15:59:13 -05:00
|
|
|
content_search(klass)
|
2012-08-11 20:22:29 -04:00
|
|
|
else
|
2013-01-03 15:59:13 -05:00
|
|
|
name_search(klass)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-04-02 10:22:06 -04:00
|
|
|
|
2013-01-03 15:59:13 -05:00
|
|
|
show_search_results(matches)
|
2013-01-03 10:48:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-03-31 22:24:28 -04:00
|
|
|
|
2013-01-03 15:59:13 -05:00
|
|
|
# @return [Regexp] The pattern to search for.
|
|
|
|
def pattern
|
|
|
|
@pattern ||= ::Regexp.new args[0]
|
|
|
|
end
|
|
|
|
|
2013-01-03 10:48:46 -05:00
|
|
|
# Output the result of the search.
|
|
|
|
#
|
|
|
|
# @param [Array] matches
|
2013-01-03 15:59:13 -05:00
|
|
|
def show_search_results(matches)
|
2012-08-11 20:22:29 -04:00
|
|
|
if matches.empty?
|
|
|
|
output.puts text.bold("No Methods Matched")
|
|
|
|
else
|
2013-01-03 15:59:13 -05:00
|
|
|
print_matches(matches)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-03 10:48:46 -05:00
|
|
|
# The class to search for methods.
|
|
|
|
# We only search classes, so if the search object is an
|
|
|
|
# instance, return its class. If no search object is given
|
|
|
|
# search `target_self`.
|
|
|
|
def search_class
|
|
|
|
klass = if args[1]
|
|
|
|
target.eval(args[1])
|
|
|
|
else
|
|
|
|
target_self
|
|
|
|
end
|
|
|
|
|
|
|
|
klass.is_a?(Module) ? klass : klass.class
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
# pretty-print a list of matching methods.
|
|
|
|
#
|
|
|
|
# @param Array[Method]
|
2013-01-03 15:59:13 -05:00
|
|
|
def print_matches(matches)
|
2012-08-11 20:22:29 -04:00
|
|
|
grouped = matches.group_by(&:owner)
|
|
|
|
order = grouped.keys.sort_by{ |x| x.name || x.to_s }
|
|
|
|
|
|
|
|
order.each do |klass|
|
2013-01-03 15:59:13 -05:00
|
|
|
print_matches_for_class(klass, grouped)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-02 10:22:06 -04:00
|
|
|
|
2013-01-03 10:48:46 -05:00
|
|
|
# Print matched methods for a class
|
2013-01-03 15:59:13 -05:00
|
|
|
def print_matches_for_class(klass, grouped)
|
2013-01-03 10:48:46 -05:00
|
|
|
output.puts text.bold(klass.name)
|
|
|
|
grouped[klass].each do |method|
|
|
|
|
header = method.name_with_owner
|
2013-01-03 15:59:13 -05:00
|
|
|
output.puts header + additional_info(header, method)
|
2013-01-03 10:48:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the matched lines of method source if `-c` is given or ""
|
|
|
|
# if `-c` was not given
|
2013-01-03 15:59:13 -05:00
|
|
|
def additional_info(header, method)
|
2013-01-03 10:48:46 -05:00
|
|
|
if opts.content?
|
2013-01-03 15:59:13 -05:00
|
|
|
": " + colorize_code(matched_method_lines(header, method))
|
2013-01-03 10:48:46 -05:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-03 15:59:13 -05:00
|
|
|
def matched_method_lines(header, method)
|
2013-01-03 10:48:46 -05:00
|
|
|
method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}")
|
|
|
|
end
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
# Run the given block against every constant in the provided namespace.
|
|
|
|
#
|
|
|
|
# @param Module The namespace in which to start the search.
|
|
|
|
# @param Hash[Module,Boolean] The namespaces we've already visited (private)
|
|
|
|
# @yieldparam klazz Each class/module in the namespace.
|
|
|
|
#
|
|
|
|
def recurse_namespace(klass, done={}, &block)
|
|
|
|
return if !(Module === klass) || done[klass]
|
|
|
|
|
|
|
|
done[klass] = true
|
|
|
|
|
|
|
|
yield klass
|
|
|
|
|
|
|
|
klass.constants.each do |name|
|
|
|
|
next if klass.autoload?(name)
|
|
|
|
begin
|
|
|
|
const = klass.const_get(name)
|
|
|
|
rescue RescuableException
|
|
|
|
# constant loading is an inexact science at the best of times,
|
|
|
|
# this often happens when a constant was .autoload? but someone
|
|
|
|
# tried to load it. It's now not .autoload? but will still raise
|
|
|
|
# a NameError when you access it.
|
|
|
|
else
|
|
|
|
recurse_namespace(const, done, &block)
|
2012-04-17 04:05:44 -04:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-17 04:05:44 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
# Gather all the methods in a namespace that pass the given block.
|
|
|
|
#
|
|
|
|
# @param Module The namespace in which to search.
|
|
|
|
# @yieldparam Method The method to test
|
|
|
|
# @yieldreturn Boolean
|
|
|
|
# @return Array[Method]
|
|
|
|
#
|
|
|
|
def search_all_methods(namespace)
|
|
|
|
done = Hash.new{ |h,k| h[k] = {} }
|
|
|
|
matches = []
|
|
|
|
|
|
|
|
recurse_namespace(namespace) do |klass|
|
|
|
|
(Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).each do |method|
|
|
|
|
next if done[method.owner][method.name]
|
|
|
|
done[method.owner][method.name] = true
|
|
|
|
|
|
|
|
matches << method if yield method
|
2012-04-17 04:05:44 -04:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
matches
|
|
|
|
end
|
|
|
|
|
|
|
|
# Search for all methods with a name that matches the given regex
|
|
|
|
# within a namespace.
|
|
|
|
#
|
|
|
|
# @param Module The namespace to search
|
|
|
|
# @return Array[Method]
|
|
|
|
#
|
2013-01-03 15:59:13 -05:00
|
|
|
def name_search(namespace)
|
2012-08-11 20:22:29 -04:00
|
|
|
search_all_methods(namespace) do |meth|
|
2013-01-03 15:59:13 -05:00
|
|
|
meth.name =~ pattern
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-04-17 04:05:44 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
# Search for all methods who's implementation matches the given regex
|
|
|
|
# within a namespace.
|
|
|
|
#
|
|
|
|
# @param Module The namespace to search
|
|
|
|
# @return Array[Method]
|
|
|
|
#
|
2013-01-03 15:59:13 -05:00
|
|
|
def content_search(namespace)
|
2012-08-11 20:22:29 -04:00
|
|
|
search_all_methods(namespace) do |meth|
|
|
|
|
begin
|
2013-01-03 15:59:13 -05:00
|
|
|
meth.source =~ pattern
|
2012-08-11 20:22:29 -04:00
|
|
|
rescue RescuableException
|
|
|
|
false
|
2012-03-31 22:24:28 -04:00
|
|
|
end
|
2012-04-01 04:09:02 -04:00
|
|
|
end
|
2012-03-31 22:24:28 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-25 16:35:17 -05:00
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::FindMethod)
|
2012-04-01 04:09:02 -04:00
|
|
|
end
|