1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Define a command superclass for show-source and show-doc

Pry::Command::ShowInfo is an abstract base class.
This commit is contained in:
John Mair 2013-01-01 21:25:23 +01:00
parent 0aae15c4d9
commit a5a4218660
3 changed files with 84 additions and 137 deletions

View file

@ -1,13 +1,12 @@
require 'pry/commands/show_info'
class Pry
class Command::ShowDoc < Pry::ClassCommand
class Command::ShowDoc < Command::ShowInfo
include Pry::Helpers::DocumentationHelpers
extend Pry::Helpers::BaseHelpers
match 'show-doc'
group 'Introspection'
description 'Show the documentation for a method or class. Aliases: \?'
command_options :shellwords => false
command_options :requires_gem => 'ruby18_source_location' if mri_18?
banner <<-BANNER
Usage: show-doc [OPTIONS] [METH]
@ -19,35 +18,7 @@ class Pry
e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
BANNER
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
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 :a, :all, "Show docs for all definitions and monkeypatches of the module/class"
end
def process
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
if show_docs_for_all_modules?(code_object)
# show docs for all monkey patches for a module
result = docs_and_headers_for_all_module_candidates(code_object)
else
# show the source for a specific code object
result = docs_and_header_for_code_object(code_object)
end
set_file_and_dir_locals(code_object.source_file)
stagger_output result
end
def docs_and_header_for_code_object(code_object)
def content_and_header_for_code_object(code_object)
result = header(code_object)
result << Code.new(render_doc_markup_for(code_object),
opts.present?(:b) ? 1 : start_line_for(code_object),
@ -55,7 +26,7 @@ class Pry
with_line_numbers(use_line_numbers?).to_s
end
def docs_and_headers_for_all_module_candidates(mod)
def content_and_headers_for_all_module_candidates(mod)
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
candidate = mod.candidate(v)
@ -74,16 +45,6 @@ class Pry
result
end
def show_docs_for_all_modules?(code_object)
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
end
# simple function to piece together the name of the object
# passed in from the arg list
def obj_name
@obj_name ||= args.empty? ? nil : args.join(" ")
end
# process the markup (if necessary) and apply colors
def render_doc_markup_for(code_object)
docs = docs_for(code_object)
@ -153,26 +114,6 @@ class Pry
(code_object.source_line - code_object.doc.lines.count)
end
end
def use_line_numbers?
opts.present?(:b) || opts.present?(:l)
end
def complete(input)
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
prefix, search = [$1, $2]
methods = begin
Pry::Method.all_from_class(binding.eval(prefix))
rescue RescuableException => e
return super
end
methods.map do |method|
[prefix, method.name].join('#') if method.name.start_with?(search)
end.compact
else
super
end
end
end
Pry::Commands.add_command(Pry::Command::ShowDoc)

View file

@ -0,0 +1,73 @@
class Pry
class Command::ShowInfo < Pry::ClassCommand
extend Pry::Helpers::BaseHelpers
options :shellwords => false
options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
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 :a, :all, "Show all definitions and monkeypatches of the module/class"
end
def process
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
if show_all_modules?(code_object)
# show all monkey patches for a module
result = content_and_headers_for_all_module_candidates(code_object)
else
# show a specific code object
result = content_and_header_for_code_object(code_object)
end
set_file_and_dir_locals(code_object.source_file)
stagger_output result
end
def show_all_modules?(code_object)
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
end
def obj_name
@obj_name ||= args.empty? ? nil : args.join(" ")
end
def use_line_numbers?
opts.present?(:b) || opts.present?(:l)
end
def start_line_for(code_object)
if opts.present?(:'base-one')
1
else
code_object.source_line || 1
end
end
def complete(input)
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
prefix, search = [$1, $2]
methods = begin
Pry::Method.all_from_class(binding.eval(prefix))
rescue RescuableException => e
return super
end
methods.map do |method|
[prefix, method.name].join('#') if method.name.start_with?(search)
end.compact
else
super
end
end
end
end

View file

@ -1,7 +1,7 @@
class Pry
class Command::ShowSource < Pry::ClassCommand
extend Pry::Helpers::BaseHelpers
require 'pry/commands/show_info'
class Pry
class Command::ShowSource < Command::ShowInfo
match 'show-source'
group 'Introspection'
description 'Show the source for a method or class. Aliases: $, show-method'
@ -13,7 +13,7 @@ class Pry
Show the source for a method or class. Tries instance methods first and then methods by default.
e.g: `show-source hello_method`
e.g: `show-source -m hello_method`
e.g: `show-source hello_method`
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)
@ -22,44 +22,13 @@ class Pry
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
BANNER
options :shellwords => false
options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end
def options(opt)
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
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 :a, :all, "Show source for all definitions and monkeypatches of the module/class"
end
def process
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
if show_source_for_all_modules?(code_object)
# show all monkey patches for a module
result = source_and_headers_for_all_module_candidates(code_object)
else
# show the source for a specific code object
result = source_and_header_for_code_object(code_object)
end
set_file_and_dir_locals(code_object.source_file)
stagger_output result
end
def source_and_header_for_code_object(code_object)
def content_and_header_for_code_object(code_object)
result = header(code_object)
result << Code.new(code_object.source, start_line_for(code_object)).
with_line_numbers(use_line_numbers?).to_s
end
def source_and_headers_for_all_module_candidates(mod)
def content_and_headers_for_all_module_candidates(mod)
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
candidate = mod.candidate(v)
@ -75,14 +44,6 @@ class Pry
result
end
def show_source_for_all_modules?(code_object)
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
end
def obj_name
@obj_name ||= args.empty? ? nil : args.join(" ")
end
# Generate a header (meta-data information) for all the code
# object types: methods, modules, commands, procs...
def header(code_object)
@ -101,34 +62,6 @@ class Pry
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
"#{code_object.source.lines.count}\n\n"
end
def start_line_for(code_object)
if opts.present?(:'base-one')
1
else
code_object.source_line || 1
end
end
def use_line_numbers?
opts.present?(:b) || opts.present?(:l)
end
def complete(input)
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
prefix, search = [$1, $2]
methods = begin
Pry::Method.all_from_class(binding.eval(prefix))
rescue RescuableException => e
return super
end
methods.map do |method|
[prefix, method.name].join('#') if method.name.start_with?(search)
end.compact
else
super
end
end
end
Pry::Commands.add_command(Pry::Command::ShowSource)