2008-01-08 05:18:41 -05:00
|
|
|
require 'rdoc/ri'
|
|
|
|
|
|
|
|
##
|
|
|
|
# This is a kind of 'flag' module. If you want to write your own 'ri' display
|
|
|
|
# module (perhaps because you'r writing an IDE or somesuch beast), you simply
|
|
|
|
# write a class which implements the various 'display' methods in
|
|
|
|
# 'DefaultDisplay', and include the 'RiDisplay' module in that class.
|
2004-01-06 00:59:31 -05:00
|
|
|
#
|
|
|
|
# To access your class from the command line, you can do
|
|
|
|
#
|
|
|
|
# ruby -r <your source file> ../ri ....
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
module RDoc::RI::Display
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
@@display_class = nil
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
def self.append_features(display_class)
|
2004-01-06 00:59:31 -05:00
|
|
|
@@display_class = display_class
|
|
|
|
end
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
def self.new(*args)
|
2004-01-06 00:59:31 -05:00
|
|
|
@@display_class.new(*args)
|
|
|
|
end
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
##
|
|
|
|
# A paging display module. Uses the RDoc::RI::Formatter class to do the actual
|
2008-01-13 22:34:05 -05:00
|
|
|
# presentation.
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
class RDoc::RI::DefaultDisplay
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
include RDoc::RI::Display
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
def initialize(formatter, width, use_stdout)
|
|
|
|
@use_stdout = use_stdout
|
2008-01-18 19:06:19 -05:00
|
|
|
@formatter = formatter.new $stdout, width, " "
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def display_method_info(method)
|
|
|
|
page do
|
|
|
|
@formatter.draw_line(method.full_name)
|
|
|
|
display_params(method)
|
|
|
|
@formatter.draw_line
|
|
|
|
display_flow(method.comment)
|
|
|
|
if method.aliases && !method.aliases.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
aka = "(also known as "
|
2008-01-08 04:07:31 -05:00
|
|
|
aka << method.aliases.map {|a| a.name }.join(", ")
|
2004-01-06 00:59:31 -05:00
|
|
|
aka << ")"
|
|
|
|
@formatter.wrap(aka)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def display_class_info(klass, ri_reader)
|
2008-01-08 04:07:31 -05:00
|
|
|
page do
|
2004-01-06 00:59:31 -05:00
|
|
|
superclass = klass.superclass_string
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
if superclass
|
|
|
|
superclass = " < " + superclass
|
|
|
|
else
|
|
|
|
superclass = ""
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
@formatter.draw_line(klass.display_name + ": " +
|
|
|
|
klass.full_name + superclass)
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
display_flow(klass.comment)
|
2008-01-08 04:07:31 -05:00
|
|
|
@formatter.draw_line
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
unless klass.includes.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Includes:", 2, "")
|
|
|
|
incs = []
|
|
|
|
klass.includes.each do |inc|
|
|
|
|
inc_desc = ri_reader.find_class_by_name(inc.name)
|
|
|
|
if inc_desc
|
|
|
|
str = inc.name + "("
|
|
|
|
str << inc_desc.instance_methods.map{|m| m.name}.join(", ")
|
|
|
|
str << ")"
|
|
|
|
incs << str
|
|
|
|
else
|
|
|
|
incs << inc.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@formatter.wrap(incs.sort.join(', '))
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
unless klass.constants.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Constants:", 2, "")
|
|
|
|
len = 0
|
|
|
|
klass.constants.each { |c| len = c.name.length if c.name.length > len }
|
|
|
|
len += 2
|
|
|
|
klass.constants.each do |c|
|
2008-01-08 04:07:31 -05:00
|
|
|
@formatter.wrap(c.value,
|
2004-01-06 00:59:31 -05:00
|
|
|
@formatter.indent+((c.name+":").ljust(len)))
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
unless klass.class_methods.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Class methods:", 2, "")
|
|
|
|
@formatter.wrap(klass.class_methods.map{|m| m.name}.sort.join(', '))
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
|
|
|
unless klass.class_method_extensions.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Class Method Extensions:", 2, "")
|
|
|
|
@formatter.wrap(klass.class_method_extensions.map{|m| m.name}.sort.join(', '))
|
|
|
|
end
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
unless klass.instance_methods.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Instance methods:", 2, "")
|
|
|
|
@formatter.wrap(klass.instance_methods.map{|m| m.name}.sort.join(', '))
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
|
|
|
unless klass.instance_method_extensions.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.display_heading("Instance Method Extensions:", 2, "")
|
|
|
|
@formatter.wrap(klass.instance_method_extensions.map{|m| m.name}.sort.join(', '))
|
|
|
|
end
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
unless klass.attributes.empty?
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.wrap("Attributes:", "")
|
|
|
|
@formatter.wrap(klass.attributes.map{|a| a.name}.sort.join(', '))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
##
|
2004-01-06 00:59:31 -05:00
|
|
|
# Display a list of method names
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def display_method_list(methods)
|
|
|
|
page do
|
2008-01-31 00:10:58 -05:00
|
|
|
@formatter.raw_print_line("More than one method matched your request. You can refine")
|
|
|
|
@formatter.raw_print_line("your search by asking for information on one of:\n\n")
|
2004-01-06 00:59:31 -05:00
|
|
|
@formatter.wrap(methods.map {|m| m.full_name} .join(", "))
|
|
|
|
end
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def display_class_list(namespaces)
|
|
|
|
page do
|
2008-01-31 00:10:58 -05:00
|
|
|
@formatter.raw_print_line("More than one class or module matched your request. You can refine")
|
|
|
|
@formatter.raw_print_line("your search by asking for information on one of:\n\n")
|
2004-01-06 00:59:31 -05:00
|
|
|
@formatter.wrap(namespaces.map {|m| m.full_name}.join(", "))
|
|
|
|
end
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def list_known_classes(classes)
|
|
|
|
if classes.empty?
|
2004-03-24 14:17:42 -05:00
|
|
|
warn_no_database
|
2004-01-06 00:59:31 -05:00
|
|
|
else
|
2008-01-08 04:07:31 -05:00
|
|
|
page do
|
2004-01-06 00:59:31 -05:00
|
|
|
@formatter.draw_line("Known classes and modules")
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.wrap(classes.sort.join(", "))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-24 14:17:42 -05:00
|
|
|
def list_known_names(names)
|
|
|
|
if names.empty?
|
|
|
|
warn_no_database
|
|
|
|
else
|
2008-01-08 04:07:31 -05:00
|
|
|
page do
|
2004-03-24 14:17:42 -05:00
|
|
|
names.each {|n| @formatter.raw_print_line(n)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def page
|
2008-01-08 04:07:31 -05:00
|
|
|
if pager = setup_pager then
|
|
|
|
begin
|
2008-01-31 01:48:35 -05:00
|
|
|
orig_output = @formatter.output
|
|
|
|
@formatter.output = pager
|
2008-01-08 04:07:31 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2008-01-31 01:48:35 -05:00
|
|
|
@formatter.output = orig_output
|
2008-01-08 04:07:31 -05:00
|
|
|
pager.close
|
|
|
|
end
|
|
|
|
else
|
2004-01-06 00:59:31 -05:00
|
|
|
yield
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
rescue Errno::EPIPE
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_pager
|
2008-01-08 04:07:31 -05:00
|
|
|
unless @use_stdout then
|
2004-03-02 02:30:35 -05:00
|
|
|
for pager in [ ENV['PAGER'], "less", "more", 'pager' ].compact.uniq
|
2004-03-03 11:17:32 -05:00
|
|
|
return IO.popen(pager, "w") rescue nil
|
2004-03-02 02:30:35 -05:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
@use_stdout = true
|
2004-03-03 04:58:25 -05:00
|
|
|
nil
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
def display_params(method)
|
2004-01-06 00:59:31 -05:00
|
|
|
params = method.params
|
|
|
|
|
|
|
|
if params[0,1] == "("
|
|
|
|
if method.is_singleton
|
|
|
|
params = method.full_name + params
|
|
|
|
else
|
|
|
|
params = method.name + params
|
|
|
|
end
|
|
|
|
end
|
2004-01-11 22:11:25 -05:00
|
|
|
params.split(/\n/).each do |p|
|
2008-01-08 04:07:31 -05:00
|
|
|
@formatter.wrap(p)
|
2004-01-11 22:11:25 -05:00
|
|
|
@formatter.break_to_newline
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
if method.source_path then
|
|
|
|
@formatter.blankline
|
|
|
|
@formatter.wrap("Extension from #{method.source_path}")
|
|
|
|
end
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2004-01-06 00:59:31 -05:00
|
|
|
def display_flow(flow)
|
|
|
|
if !flow || flow.empty?
|
|
|
|
@formatter.wrap("(no description...)")
|
|
|
|
else
|
|
|
|
@formatter.display_flow(flow)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-24 14:17:42 -05:00
|
|
|
def warn_no_database
|
2008-01-08 04:07:31 -05:00
|
|
|
puts "No ri data found"
|
|
|
|
puts
|
|
|
|
puts "If you've installed Ruby yourself, you need to generate documentation using:"
|
|
|
|
puts
|
|
|
|
puts " make install-doc"
|
|
|
|
puts
|
|
|
|
puts "from the same place you ran `make` to build ruby."
|
|
|
|
puts
|
|
|
|
puts "If you installed Ruby from a packaging system, then you may need to"
|
|
|
|
puts "install an additional package, or ask the packager to enable ri generation."
|
2004-03-24 14:17:42 -05:00
|
|
|
end
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|