2013-01-01 15:25:23 -05:00
|
|
|
require 'pry/commands/show_info'
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
class Pry
|
2013-01-01 15:25:23 -05:00
|
|
|
class Command::ShowDoc < Command::ShowInfo
|
2012-08-11 20:22:29 -04:00
|
|
|
include Pry::Helpers::DocumentationHelpers
|
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
match 'show-doc'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Introspection'
|
2013-01-09 15:23:19 -05:00
|
|
|
description 'Show the documentation for a method or class.'
|
2012-08-11 21:26:59 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
banner <<-BANNER
|
2013-01-09 15:23:19 -05:00
|
|
|
Usage: show-doc [OPTIONS] [METH]
|
2012-08-11 20:22:29 -04:00
|
|
|
Aliases: ?
|
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
Show the documentation for a method or class. Tries instance methods first and
|
|
|
|
then methods by default.
|
|
|
|
|
|
|
|
show-doc hi_method # docs for hi_method
|
|
|
|
show-doc Pry # for Pry class
|
|
|
|
show-doc Pry -a # for all definitions of Pry class (all monkey patches)
|
2012-08-11 20:22:29 -04:00
|
|
|
BANNER
|
|
|
|
|
2013-01-01 16:45:23 -05:00
|
|
|
# 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).
|
2012-12-25 07:47:33 -05:00
|
|
|
with_line_numbers(use_line_numbers?).to_s
|
|
|
|
end
|
|
|
|
|
2012-12-22 16:54:17 -05:00
|
|
|
# process the markup (if necessary) and apply colors
|
2012-12-21 10:39:57 -05:00
|
|
|
def render_doc_markup_for(code_object)
|
2012-12-22 16:54:17 -05:00
|
|
|
docs = docs_for(code_object)
|
|
|
|
|
2012-12-25 07:47:33 -05:00
|
|
|
if code_object.command?
|
|
|
|
# command '--help' shouldn't use markup highlighting
|
2012-12-22 16:54:17 -05:00
|
|
|
docs
|
2012-12-21 10:39:57 -05:00
|
|
|
else
|
2013-03-13 07:15:58 -04:00
|
|
|
if docs.empty?
|
|
|
|
raise CommandError, "No docs found for: #{
|
|
|
|
obj_name ? obj_name : 'current context'
|
|
|
|
}"
|
|
|
|
end
|
2012-12-22 16:54:17 -05:00
|
|
|
process_comment_markup(docs)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-22 16:54:17 -05:00
|
|
|
# Return docs for the code_object, adjusting for whether the code_object
|
|
|
|
# has yard docs available, in which case it returns those.
|
|
|
|
# (note we only have to check yard docs for modules since they can
|
|
|
|
# have multiple docs, but methods can only be doc'd once so we
|
|
|
|
# dont need to check them)
|
|
|
|
def docs_for(code_object)
|
2012-12-25 07:47:33 -05:00
|
|
|
if code_object.module_with_yard_docs?
|
2012-12-22 16:54:17 -05:00
|
|
|
# yard docs
|
|
|
|
code_object.yard_doc
|
|
|
|
else
|
|
|
|
# normal docs (i.e comments above method/module/command)
|
|
|
|
code_object.doc
|
|
|
|
end
|
2012-12-21 10:39:57 -05:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2013-01-01 16:45:23 -05:00
|
|
|
# Which sections to include in the 'header', can toggle: :owner,
|
|
|
|
# :signature and visibility.
|
|
|
|
def header_options
|
|
|
|
super.merge :signature => true
|
2012-08-13 23:54:07 -04:00
|
|
|
end
|
|
|
|
|
2012-12-25 07:47:33 -05:00
|
|
|
# figure out start line of docs by back-calculating based on
|
2012-12-22 16:54:17 -05:00
|
|
|
# number of lines in the comment and the start line of the code_object
|
|
|
|
# @return [Fixnum] start line of docs
|
2012-12-21 10:39:57 -05:00
|
|
|
def start_line_for(code_object)
|
2013-01-01 16:45:23 -05:00
|
|
|
if code_object.command? || opts.present?(:'base-one')
|
2012-08-11 20:22:29 -04:00
|
|
|
1
|
|
|
|
else
|
2012-12-21 10:39:57 -05:00
|
|
|
code_object.source_line.nil? ? 1 :
|
|
|
|
(code_object.source_line - code_object.doc.lines.count)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-21 10:39:57 -05:00
|
|
|
end
|
2012-12-25 07:47:33 -05:00
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
Pry::Commands.add_command(Pry::Command::ShowDoc)
|
|
|
|
Pry::Commands.alias_command '?', 'show-doc'
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|