Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Pry::Command::ShowInfo is an abstract base class.
  • Loading branch information...
commit a5a4218660cd8c22cc7f6bcd0915263ada27f65e 1 parent 0aae15c
@banister banister authored
View
69 lib/pry/commands/show_doc.rb
@@ -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 Command::ShowDoc < Pry::ClassCommand
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 @@ def docs_and_header_for_code_object(code_object)
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 @@ def docs_and_headers_for_all_module_candidates(mod)
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 @@ def start_line_for(code_object)
(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
73 lib/pry/commands/show_info.rb
@@ -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
79 lib/pry/commands/show_source.rb
@@ -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 Command::ShowSource < Pry::ClassCommand
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 Command::ShowSource < Pry::ClassCommand
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 @@ def source_and_headers_for_all_module_candidates(mod)
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 @@ def header(code_object)
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)
Please sign in to comment.
Something went wrong with that request. Please try again.