mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
pull more show-source/show-doc methods into their superclass
This commit is contained in:
parent
9f4ef13129
commit
c4dc8cffcd
3 changed files with 72 additions and 93 deletions
|
@ -18,33 +18,13 @@ class Pry
|
|||
e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
|
||||
BANNER
|
||||
|
||||
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),
|
||||
:text).
|
||||
# The docs for code_object prepared for display.
|
||||
def content_for(code_object)
|
||||
Code.new(render_doc_markup_for(code_object),
|
||||
start_line_for(code_object), :text).
|
||||
with_line_numbers(use_line_numbers?).to_s
|
||||
end
|
||||
|
||||
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)
|
||||
begin
|
||||
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
|
||||
doc = Code.new(render_doc_markup_for(candidate),
|
||||
opts.present?(:b) ? 1 : candidate.source_line,
|
||||
:text).with_line_numbers(use_line_numbers?).to_s
|
||||
result << "Number of lines: #{doc.lines.count}\n\n" << doc
|
||||
rescue Pry::RescuableException
|
||||
result << "\nNo code found.\n"
|
||||
|
||||
next
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# process the markup (if necessary) and apply colors
|
||||
def render_doc_markup_for(code_object)
|
||||
docs = docs_for(code_object)
|
||||
|
@ -72,42 +52,17 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# takes into account possible yard docs, and returns yard_file / yard_line
|
||||
# Also adjusts for start line of comments (using start_line_for), which it has to infer
|
||||
# by subtracting number of lines of comment from start line of code_object
|
||||
def file_and_line_for(code_object)
|
||||
if code_object.module_with_yard_docs?
|
||||
[code_object.yard_file, code_object.yard_line]
|
||||
else
|
||||
[code_object.source_file, start_line_for(code_object)]
|
||||
end
|
||||
end
|
||||
|
||||
# Generate a header (meta-data information) for all the code
|
||||
# object types: methods, modules, commands, procs...
|
||||
def header(code_object)
|
||||
file_name, line_num = file_and_line_for(code_object)
|
||||
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
||||
if code_object.c_method?
|
||||
h << "(C Method):"
|
||||
else
|
||||
h << "@ line #{line_num}:"
|
||||
end
|
||||
|
||||
if code_object.real_method_object?
|
||||
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
||||
h << "#{text.bold("Visibility:")} #{code_object.visibility}\n"
|
||||
h << "#{text.bold("Signature:")} #{code_object.signature}"
|
||||
end
|
||||
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
|
||||
"#{docs_for(code_object).lines.count}\n\n"
|
||||
# Which sections to include in the 'header', can toggle: :owner,
|
||||
# :signature and visibility.
|
||||
def header_options
|
||||
super.merge :signature => true
|
||||
end
|
||||
|
||||
# figure out start line of docs by back-calculating based on
|
||||
# number of lines in the comment and the start line of the code_object
|
||||
# @return [Fixnum] start line of docs
|
||||
def start_line_for(code_object)
|
||||
if code_object.command?
|
||||
if code_object.command? || opts.present?(:'base-one')
|
||||
1
|
||||
else
|
||||
code_object.source_line.nil? ? 1 :
|
||||
|
|
|
@ -18,7 +18,6 @@ class Pry
|
|||
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
|
||||
|
||||
|
@ -34,6 +33,55 @@ class Pry
|
|||
stagger_output result
|
||||
end
|
||||
|
||||
def content_and_header_for_code_object(code_object)
|
||||
header(code_object) << content_for(code_object)
|
||||
end
|
||||
|
||||
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)
|
||||
begin
|
||||
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
|
||||
content = content_for(candidate)
|
||||
|
||||
result << "Number of lines: #{content.lines.count}\n\n" << content
|
||||
rescue Pry::RescuableException
|
||||
result << "\nNo content found.\n"
|
||||
next
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# Generate a header (meta-data information) for all the code
|
||||
# object types: methods, modules, commands, procs...
|
||||
def header(code_object)
|
||||
file_name, line_num = file_and_line_for(code_object)
|
||||
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
||||
if code_object.c_method?
|
||||
h << "(C Method):"
|
||||
else
|
||||
h << "@ line #{line_num}:"
|
||||
end
|
||||
|
||||
if code_object.real_method_object?
|
||||
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n" if header_options[:owner]
|
||||
h << "#{text.bold("Visibility:")} #{code_object.visibility}\n" if header_options[:visibility]
|
||||
h << "#{text.bold("Signature:")} #{code_object.signature}" if header_options[:signature]
|
||||
end
|
||||
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
|
||||
"#{content_for(code_object).lines.count}\n\n"
|
||||
end
|
||||
|
||||
def header_options
|
||||
{
|
||||
:owner => true,
|
||||
:visibility => true,
|
||||
:signature => false
|
||||
}
|
||||
end
|
||||
|
||||
def show_all_modules?(code_object)
|
||||
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
||||
end
|
||||
|
@ -54,6 +102,17 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# takes into account possible yard docs, and returns yard_file / yard_line
|
||||
# Also adjusts for start line of comments (using start_line_for), which it has to infer
|
||||
# by subtracting number of lines of comment from start line of code_object
|
||||
def file_and_line_for(code_object)
|
||||
if code_object.module_with_yard_docs?
|
||||
[code_object.yard_file, code_object.yard_line]
|
||||
else
|
||||
[code_object.source_file, start_line_for(code_object)]
|
||||
end
|
||||
end
|
||||
|
||||
def complete(input)
|
||||
if input =~ /([^ ]*)#([a-z0-9_]*)\z/
|
||||
prefix, search = [$1, $2]
|
||||
|
|
|
@ -22,46 +22,11 @@ class Pry
|
|||
https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
|
||||
BANNER
|
||||
|
||||
def content_and_header_for_code_object(code_object)
|
||||
result = header(code_object)
|
||||
result << Code.new(code_object.source, start_line_for(code_object)).
|
||||
# The source for code_object prepared for display.
|
||||
def content_for(code_object)
|
||||
Code.new(code_object.source, start_line_for(code_object)).
|
||||
with_line_numbers(use_line_numbers?).to_s
|
||||
end
|
||||
|
||||
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)
|
||||
begin
|
||||
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.file} @ line #{candidate.line}:\n"
|
||||
code = Code.from_module(mod, start_line_for(candidate), v).with_line_numbers(use_line_numbers?).to_s
|
||||
result << "Number of lines: #{code.lines.count}\n\n" << code
|
||||
rescue Pry::RescuableException
|
||||
result << "\nNo code found.\n"
|
||||
next
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# Generate a header (meta-data information) for all the code
|
||||
# object types: methods, modules, commands, procs...
|
||||
def header(code_object)
|
||||
file_name, line_num = code_object.source_file, code_object.source_line
|
||||
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
||||
if code_object.c_method?
|
||||
h << "(C Method):"
|
||||
else
|
||||
h << "@ line #{line_num}:"
|
||||
end
|
||||
|
||||
if code_object.real_method_object?
|
||||
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
||||
h << "#{text.bold("Visibility:")} #{code_object.visibility}"
|
||||
end
|
||||
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
|
||||
"#{code_object.source.lines.count}\n\n"
|
||||
end
|
||||
end
|
||||
|
||||
Pry::Commands.add_command(Pry::Command::ShowSource)
|
||||
|
|
Loading…
Add table
Reference in a new issue