2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2010-04-01 03:45:16 -04:00
|
|
|
require 'abbrev'
|
2008-01-08 04:07:31 -05:00
|
|
|
require 'optparse'
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'readline'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2022-02-09 21:31:06 -05:00
|
|
|
require_relative '../../rdoc'
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2022-03-13 06:51:50 -04:00
|
|
|
require_relative 'formatter' # For RubyGems backwards compatibility
|
2022-04-24 20:48:55 -04:00
|
|
|
# TODO: Fix weird documentation with `require_relative`
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# The RI driver implements the command-line ri tool.
|
|
|
|
#
|
|
|
|
# The driver supports:
|
|
|
|
# * loading RI data from:
|
|
|
|
# * Ruby's standard library
|
|
|
|
# * RubyGems
|
|
|
|
# * ~/.rdoc
|
|
|
|
# * A user-supplied directory
|
|
|
|
# * Paging output (uses RI_PAGER environment variable, PAGER environment
|
|
|
|
# variable or the less, more and pager programs)
|
|
|
|
# * Interactive mode with tab-completion
|
|
|
|
# * Abbreviated names (ri Zl shows Zlib documentation)
|
|
|
|
# * Colorized output
|
|
|
|
# * Merging output from multiple RI data sources
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
class RDoc::RI::Driver
|
|
|
|
|
|
|
|
##
|
|
|
|
# Base Driver error class
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
class Error < RDoc::RI::Error; end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Raised when a name isn't found in the ri data stores
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
class NotFoundError < Error
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
def initialize(klass, suggestions = nil) # :nodoc:
|
|
|
|
@klass = klass
|
|
|
|
@suggestions = suggestions
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Name that wasn't found
|
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
def name
|
|
|
|
@klass
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def message # :nodoc:
|
2017-10-09 22:01:00 -04:00
|
|
|
str = "Nothing known about #{@klass}"
|
|
|
|
if @suggestions and !@suggestions.empty?
|
|
|
|
str += "\nDid you mean? #{@suggestions.join("\n ")}"
|
|
|
|
end
|
|
|
|
str
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Show all method documentation following a class or module
|
|
|
|
|
|
|
|
attr_accessor :show_all
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# An RDoc::RI::Store for each entry in the RI path
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_accessor :stores
|
|
|
|
|
|
|
|
##
|
|
|
|
# Controls the user of the pager vs $stdout
|
|
|
|
|
|
|
|
attr_accessor :use_stdout
|
|
|
|
|
|
|
|
##
|
|
|
|
# Default options for ri
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
def self.default_options
|
2008-01-08 04:07:31 -05:00
|
|
|
options = {}
|
2008-09-24 22:43:03 -04:00
|
|
|
options[:interactive] = false
|
2012-11-26 23:28:14 -05:00
|
|
|
options[:profile] = false
|
|
|
|
options[:show_all] = false
|
|
|
|
options[:use_stdout] = !$stdout.tty?
|
|
|
|
options[:width] = 72
|
2008-09-24 22:43:03 -04:00
|
|
|
|
|
|
|
# By default all standard paths are used.
|
2012-11-26 23:28:14 -05:00
|
|
|
options[:use_system] = true
|
|
|
|
options[:use_site] = true
|
|
|
|
options[:use_home] = true
|
|
|
|
options[:use_gems] = true
|
2008-09-24 22:43:03 -04:00
|
|
|
options[:extra_doc_dirs] = []
|
|
|
|
|
|
|
|
return options
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Dump +data_path+ using pp
|
|
|
|
|
|
|
|
def self.dump data_path
|
|
|
|
require 'pp'
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
File.open data_path, 'rb' do |io|
|
2010-04-01 03:45:16 -04:00
|
|
|
pp Marshal.load(io.read)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parses +argv+ and returns a Hash of options
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def self.process_args argv
|
2008-09-24 22:43:03 -04:00
|
|
|
options = default_options
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
opts = OptionParser.new do |opt|
|
2010-04-01 03:45:16 -04:00
|
|
|
opt.accept File do |file,|
|
|
|
|
File.readable?(file) and not File.directory?(file) and file
|
|
|
|
end
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
opt.program_name = File.basename $0
|
|
|
|
opt.version = RDoc::VERSION
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.release = nil
|
2008-01-08 04:07:31 -05:00
|
|
|
opt.summary_indent = ' ' * 4
|
|
|
|
|
|
|
|
opt.banner = <<-EOT
|
2017-08-29 07:52:50 -04:00
|
|
|
Usage: #{opt.program_name} [options] [name ...]
|
2008-01-08 04:07:31 -05:00
|
|
|
|
|
|
|
Where name can be:
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
Class | Module | Module::Class
|
|
|
|
|
|
|
|
Class::method | Class#method | Class.method | method
|
|
|
|
|
|
|
|
gem_name: | gem_name:README | gem_name:History
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2021-04-16 07:42:23 -04:00
|
|
|
ruby: | ruby:NEWS | ruby:globals
|
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
All class names may be abbreviated to their minimum unambiguous form.
|
|
|
|
If a name is ambiguous, all valid options will be listed.
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
A '.' matches either class or instance methods, while #method
|
2008-01-11 04:18:53 -05:00
|
|
|
matches only instance and ::method matches only class methods.
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
README and other files may be displayed by prefixing them with the gem name
|
|
|
|
they're contained in. If the gem name is followed by a ':' all files in the
|
|
|
|
gem will be shown. The file name extension may be omitted where it is
|
|
|
|
unambiguous.
|
|
|
|
|
2021-04-16 07:42:23 -04:00
|
|
|
'ruby' can be used as a pseudo gem name to display files from the Ruby
|
|
|
|
core documentation. Use 'ruby:' by itself to get a list of all available
|
|
|
|
core documentation files.
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
For example:
|
|
|
|
|
|
|
|
#{opt.program_name} Fil
|
|
|
|
#{opt.program_name} File
|
|
|
|
#{opt.program_name} File.new
|
|
|
|
#{opt.program_name} zip
|
2012-11-26 23:28:14 -05:00
|
|
|
#{opt.program_name} rdoc:README
|
2021-04-16 07:42:23 -04:00
|
|
|
#{opt.program_name} ruby:comments
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
Note that shell quoting or escaping may be required for method names
|
|
|
|
containing punctuation:
|
2008-01-08 04:07:31 -05:00
|
|
|
|
|
|
|
#{opt.program_name} 'Array.[]'
|
|
|
|
#{opt.program_name} compact\\!
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
To see the default directories #{opt.program_name} will search, run:
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
#{opt.program_name} --list-doc-dirs
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
Specifying the --system, --site, --home, --gems, or --doc-dir options
|
|
|
|
will limit ri to searching only the specified directories.
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
ri options may be set in the RI environment variable.
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
The ri pager can be set with the RI_PAGER environment variable
|
|
|
|
or the PAGER environment variable.
|
2008-01-08 04:07:31 -05:00
|
|
|
EOT
|
2004-08-16 12:41:21 -04:00
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
opt.separator nil
|
|
|
|
opt.separator "Options:"
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.on("--[no-]interactive", "-i",
|
|
|
|
"In interactive mode you can repeatedly",
|
|
|
|
"look up methods with autocomplete.") do |interactive|
|
|
|
|
options[:interactive] = interactive
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.separator nil
|
|
|
|
|
|
|
|
opt.on("--[no-]all", "-a",
|
|
|
|
"Show all documentation for a class or",
|
|
|
|
"module.") do |show_all|
|
|
|
|
options[:show_all] = show_all
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.on("--[no-]list", "-l",
|
|
|
|
"List classes ri knows about.") do |list|
|
|
|
|
options[:list] = list
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
opt.on("--[no-]pager",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Send output to a pager,",
|
|
|
|
"rather than directly to stdout.") do |use_pager|
|
2012-11-26 23:28:14 -05:00
|
|
|
options[:use_stdout] = !use_pager
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
opt.on("-T",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Synonym for --no-pager.") do
|
2013-09-18 19:33:36 -04:00
|
|
|
options[:use_stdout] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.on("--width=WIDTH", "-w", OptionParser::DecimalInteger,
|
|
|
|
"Set the width of the output.") do |width|
|
|
|
|
options[:width] = width
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
opt.on("--server[=PORT]", Integer,
|
2012-11-26 23:28:14 -05:00
|
|
|
"Run RDoc server on the given port.",
|
|
|
|
"The default port is 8214.") do |port|
|
|
|
|
options[:server] = port || 8214
|
2011-05-13 20:39:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
formatters = RDoc::Markup.constants.grep(/^To[A-Z][a-z]+$/).sort
|
|
|
|
formatters = formatters.sort.map do |formatter|
|
|
|
|
formatter.to_s.sub('To', '').downcase
|
|
|
|
end
|
|
|
|
formatters -= %w[html label test] # remove useless output formats
|
|
|
|
|
|
|
|
opt.on("--format=NAME", "-f",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Use the selected formatter. The default",
|
2012-11-26 23:28:14 -05:00
|
|
|
"formatter is bs for paged output and ansi",
|
2017-08-29 07:52:50 -04:00
|
|
|
"otherwise. Valid formatters are:",
|
|
|
|
"#{formatters.join(', ')}.", formatters) do |value|
|
2012-11-26 23:28:14 -05:00
|
|
|
options[:formatter] = RDoc::Markup.const_get "To#{value.capitalize}"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
opt.separator nil
|
|
|
|
|
|
|
|
opt.on("--help", "-h",
|
|
|
|
"Show help and exit.") do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
|
|
|
opt.on("--version", "-v",
|
|
|
|
"Output version information and exit.") do
|
|
|
|
puts "#{opts.program_name} #{opts.version}"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
opt.separator nil
|
|
|
|
opt.separator "Data source options:"
|
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.on("--[no-]list-doc-dirs",
|
2010-04-01 03:45:16 -04:00
|
|
|
"List the directories from which ri will",
|
2012-11-26 23:28:14 -05:00
|
|
|
"source documentation on stdout and exit.") do |list_doc_dirs|
|
|
|
|
options[:list_doc_dirs] = list_doc_dirs
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
|
|
|
opt.on("--doc-dir=DIRNAME", "-d", Array,
|
2008-09-24 22:43:03 -04:00
|
|
|
"List of directories from which to source",
|
|
|
|
"documentation in addition to the standard",
|
|
|
|
"directories. May be repeated.") do |value|
|
2008-01-08 04:07:31 -05:00
|
|
|
value.each do |dir|
|
|
|
|
unless File.directory? dir then
|
|
|
|
raise OptionParser::InvalidArgument, "#{dir} is not a directory"
|
|
|
|
end
|
2008-09-24 22:43:03 -04:00
|
|
|
|
|
|
|
options[:extra_doc_dirs] << File.expand_path(dir)
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.on("--no-standard-docs",
|
|
|
|
"Do not include documentation from",
|
|
|
|
"the Ruby standard library, site_lib,",
|
|
|
|
"installed gems, or ~/.rdoc.",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Use with --doc-dir.") do
|
2008-09-24 22:43:03 -04:00
|
|
|
options[:use_system] = false
|
|
|
|
options[:use_site] = false
|
|
|
|
options[:use_gems] = false
|
|
|
|
options[:use_home] = false
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.on("--[no-]system",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Include documentation from Ruby's",
|
|
|
|
"standard library. Defaults to true.") do |value|
|
2008-09-24 22:43:03 -04:00
|
|
|
options[:use_system] = value
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.on("--[no-]site",
|
|
|
|
"Include documentation from libraries",
|
|
|
|
"installed in site_lib.",
|
|
|
|
"Defaults to true.") do |value|
|
|
|
|
options[:use_site] = value
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.on("--[no-]gems",
|
|
|
|
"Include documentation from RubyGems.",
|
|
|
|
"Defaults to true.") do |value|
|
|
|
|
options[:use_gems] = value
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
opt.on("--[no-]home",
|
|
|
|
"Include documentation stored in ~/.rdoc.",
|
|
|
|
"Defaults to true.") do |value|
|
|
|
|
options[:use_home] = value
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
2010-04-01 03:45:16 -04:00
|
|
|
opt.separator "Debug options:"
|
2008-01-08 04:07:31 -05:00
|
|
|
opt.separator nil
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
opt.on("--[no-]profile",
|
2017-08-29 07:52:50 -04:00
|
|
|
"Run with the ruby profiler.") do |value|
|
2012-11-26 23:28:14 -05:00
|
|
|
options[:profile] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
opt.separator nil
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
opt.on("--dump=CACHE", File,
|
2017-08-29 07:52:50 -04:00
|
|
|
"Dump data from an ri cache or data file.") do |value|
|
2010-04-01 03:45:16 -04:00
|
|
|
options[:dump_path] = value
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2004-08-16 12:41:21 -04:00
|
|
|
end
|
|
|
|
|
2020-03-08 04:38:37 -04:00
|
|
|
argv = ENV['RI'].to_s.split(' ').concat argv
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
opts.parse! argv
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
options[:names] = argv
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
options[:use_stdout] ||= !$stdout.tty?
|
|
|
|
options[:use_stdout] ||= options[:interactive]
|
|
|
|
options[:width] ||= 72
|
2004-08-16 12:41:21 -04:00
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
options
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
|
|
|
|
puts opts
|
|
|
|
puts
|
|
|
|
puts e
|
|
|
|
exit 1
|
2006-06-26 14:10:30 -04:00
|
|
|
end
|
2004-08-30 10:22:26 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Runs the ri command line executable using +argv+
|
|
|
|
|
|
|
|
def self.run argv = ARGV
|
2008-01-08 04:07:31 -05:00
|
|
|
options = process_args argv
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
if options[:dump_path] then
|
|
|
|
dump options[:dump_path]
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
ri = new options
|
|
|
|
ri.run
|
2004-08-30 10:22:26 -04:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates a new driver using +initial_options+ from ::process_args
|
|
|
|
|
|
|
|
def initialize initial_options = {}
|
|
|
|
@paging = false
|
|
|
|
@classes = nil
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
options = self.class.default_options.update(initial_options)
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@formatter_klass = options[:formatter]
|
|
|
|
|
|
|
|
require 'profile' if options[:profile]
|
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
@names = options[:names]
|
2011-05-13 20:39:16 -04:00
|
|
|
@list = options[:list]
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@doc_dirs = []
|
|
|
|
@stores = []
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
RDoc::RI::Paths.each(options[:use_system], options[:use_site],
|
2011-06-27 22:28:25 -04:00
|
|
|
options[:use_home], options[:use_gems],
|
|
|
|
*options[:extra_doc_dirs]) do |path, type|
|
2010-04-01 03:45:16 -04:00
|
|
|
@doc_dirs << path
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
store = RDoc::RI::Store.new path, type
|
|
|
|
store.load_cache
|
|
|
|
@stores << store
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@list_doc_dirs = options[:list_doc_dirs]
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2008-09-24 22:43:03 -04:00
|
|
|
@interactive = options[:interactive]
|
2012-11-26 23:28:14 -05:00
|
|
|
@server = options[:server]
|
2010-04-01 03:45:16 -04:00
|
|
|
@use_stdout = options[:use_stdout]
|
2012-11-26 23:28:14 -05:00
|
|
|
@show_all = options[:show_all]
|
2018-03-26 01:56:26 -04:00
|
|
|
@width = options[:width]
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Adds paths for undocumented classes +also_in+ to +out+
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def add_also_in out, also_in
|
|
|
|
return if also_in.empty?
|
|
|
|
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
|
|
|
out << RDoc::Markup::Paragraph.new("Also found in:")
|
|
|
|
|
|
|
|
paths = RDoc::Markup::Verbatim.new
|
|
|
|
also_in.each do |store|
|
2010-12-19 22:22:49 -05:00
|
|
|
paths.parts.push store.friendly_path, "\n"
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
out << paths
|
|
|
|
end
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Adds a class header to +out+ for class +name+ which is described in
|
|
|
|
# +classes+.
|
|
|
|
|
|
|
|
def add_class out, name, classes
|
|
|
|
heading = if classes.all? { |klass| klass.module? } then
|
|
|
|
name
|
|
|
|
else
|
|
|
|
superclass = classes.map do |klass|
|
|
|
|
klass.superclass unless klass.module?
|
|
|
|
end.compact.shift || 'Object'
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
superclass = superclass.full_name unless String === superclass
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
"#{name} < #{superclass}"
|
|
|
|
end
|
|
|
|
|
|
|
|
out << RDoc::Markup::Heading.new(1, heading)
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Adds "(from ...)" to +out+ for +store+
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def add_from out, store
|
|
|
|
out << RDoc::Markup::Paragraph.new("(from #{store.friendly_path})")
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Adds +extends+ to +out+
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def add_extends out, extends
|
|
|
|
add_extension_modules out, 'Extended by', extends
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Adds a list of +extensions+ to this module of the given +type+ to +out+.
|
|
|
|
# add_includes and add_extends call this, so you should use those directly.
|
|
|
|
|
|
|
|
def add_extension_modules out, type, extensions
|
|
|
|
return if extensions.empty?
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
out << RDoc::Markup::Rule.new(1)
|
2012-11-26 23:28:14 -05:00
|
|
|
out << RDoc::Markup::Heading.new(1, "#{type}:")
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
extensions.each do |modules, store|
|
2010-04-01 03:45:16 -04:00
|
|
|
if modules.length == 1 then
|
2013-09-18 19:33:36 -04:00
|
|
|
add_extension_modules_single out, store, modules.first
|
2008-09-24 22:43:03 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
add_extension_modules_multiple out, store, modules
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Renders multiple included +modules+ from +store+ to +out+.
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def add_extension_modules_multiple out, store, modules # :nodoc:
|
|
|
|
out << RDoc::Markup::Paragraph.new("(from #{store.friendly_path})")
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
wout, with = modules.partition { |incl| incl.comment.empty? }
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
out << RDoc::Markup::BlankLine.new unless with.empty?
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
with.each do |incl|
|
|
|
|
out << RDoc::Markup::Paragraph.new(incl.name)
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << incl.comment
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
unless wout.empty? then
|
|
|
|
verb = RDoc::Markup::Verbatim.new
|
|
|
|
|
|
|
|
wout.each do |incl|
|
|
|
|
verb.push incl.name, "\n"
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
out << verb
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Adds a single extension module +include+ from +store+ to +out+
|
|
|
|
|
|
|
|
def add_extension_modules_single out, store, include # :nodoc:
|
|
|
|
name = include.name
|
|
|
|
path = store.friendly_path
|
|
|
|
out << RDoc::Markup::Paragraph.new("#{name} (from #{path})")
|
|
|
|
|
|
|
|
if include.comment then
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << include.comment
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2004-08-30 10:22:26 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Adds +includes+ to +out+
|
|
|
|
|
|
|
|
def add_includes out, includes
|
|
|
|
add_extension_modules out, 'Includes', includes
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Looks up the method +name+ and adds it to +out+
|
|
|
|
|
|
|
|
def add_method out, name
|
|
|
|
filtered = lookup_method name
|
|
|
|
|
|
|
|
method_out = method_document name, filtered
|
|
|
|
|
|
|
|
out.concat method_out.parts
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Adds documentation for all methods in +klass+ to +out+
|
|
|
|
|
|
|
|
def add_method_documentation out, klass
|
|
|
|
klass.method_list.each do |method|
|
|
|
|
begin
|
|
|
|
add_method out, method.full_name
|
|
|
|
rescue NotFoundError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Adds a list of +methods+ to +out+ with a heading of +name+
|
|
|
|
|
|
|
|
def add_method_list out, methods, name
|
2011-07-30 20:19:00 -04:00
|
|
|
return if methods.empty?
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
out << RDoc::Markup::Heading.new(1, "#{name}:")
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if @use_stdout and !@interactive then
|
|
|
|
out.concat methods.map { |method|
|
2011-06-16 00:59:24 -04:00
|
|
|
RDoc::Markup::Verbatim.new method
|
2012-11-26 23:28:14 -05:00
|
|
|
}
|
2011-06-16 00:59:24 -04:00
|
|
|
else
|
|
|
|
out << RDoc::Markup::IndentedParagraph.new(2, methods.join(', '))
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns ancestor classes of +klass+
|
|
|
|
|
|
|
|
def ancestors_of klass
|
|
|
|
ancestors = []
|
|
|
|
|
|
|
|
unexamined = [klass]
|
|
|
|
seen = []
|
|
|
|
|
|
|
|
loop do
|
|
|
|
break if unexamined.empty?
|
|
|
|
current = unexamined.shift
|
|
|
|
seen << current
|
|
|
|
|
|
|
|
stores = classes[current]
|
|
|
|
|
2021-06-26 18:33:34 -04:00
|
|
|
next unless stores and not stores.empty?
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2021-06-26 18:33:34 -04:00
|
|
|
klasses = stores.flat_map do |store|
|
|
|
|
store.ancestors[current] || []
|
|
|
|
end.uniq
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
klasses = klasses - seen
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
ancestors.concat klasses
|
|
|
|
unexamined.concat klasses
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ancestors.reverse
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# For RubyGems backwards compatibility
|
|
|
|
|
|
|
|
def class_cache # :nodoc:
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2011-06-27 22:28:25 -04:00
|
|
|
##
|
|
|
|
# Builds a RDoc::Markup::Document from +found+, +klasess+ and +includes+
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def class_document name, found, klasses, includes, extends
|
2011-06-27 22:28:25 -04:00
|
|
|
also_in = []
|
|
|
|
|
|
|
|
out = RDoc::Markup::Document.new
|
|
|
|
|
|
|
|
add_class out, name, klasses
|
|
|
|
|
|
|
|
add_includes out, includes
|
2012-11-26 23:28:14 -05:00
|
|
|
add_extends out, extends
|
2011-06-27 22:28:25 -04:00
|
|
|
|
|
|
|
found.each do |store, klass|
|
2013-09-18 19:33:36 -04:00
|
|
|
render_class out, store, klass, also_in
|
|
|
|
end
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
add_also_in out, also_in
|
2011-07-30 20:19:00 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
out
|
|
|
|
end
|
2011-07-30 20:19:00 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Adds the class +comment+ to +out+.
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def class_document_comment out, comment # :nodoc:
|
|
|
|
unless comment.empty? then
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if comment.merged? then
|
|
|
|
parts = comment.parts
|
|
|
|
parts = parts.zip [RDoc::Markup::BlankLine.new] * parts.length
|
|
|
|
parts.flatten!
|
|
|
|
parts.pop
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
out.concat parts
|
|
|
|
else
|
|
|
|
out << comment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Adds the constants from +klass+ to the Document +out+.
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def class_document_constants out, klass # :nodoc:
|
|
|
|
return if klass.constants.empty?
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
out << RDoc::Markup::Heading.new(1, "Constants:")
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
list = RDoc::Markup::List.new :NOTE
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
constants = klass.constants.sort_by { |constant| constant.name }
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
list.items.concat constants.map { |constant|
|
|
|
|
parts = constant.comment.parts if constant.comment
|
|
|
|
parts << RDoc::Markup::Paragraph.new('[not documented]') if
|
|
|
|
parts.empty?
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
RDoc::Markup::ListItem.new(constant.name, *parts)
|
|
|
|
}
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
out << list
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
2011-06-27 22:28:25 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Hash mapping a known class or module to the stores it can be loaded from
|
|
|
|
|
|
|
|
def classes
|
|
|
|
return @classes if @classes
|
|
|
|
|
|
|
|
@classes = {}
|
|
|
|
|
|
|
|
@stores.each do |store|
|
|
|
|
store.cache[:modules].each do |mod|
|
|
|
|
# using default block causes searched-for modules to be added
|
|
|
|
@classes[mod] ||= []
|
|
|
|
@classes[mod] << store
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@classes
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
|
|
|
|
2011-06-27 22:28:25 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Returns the stores wherein +name+ is found along with the classes,
|
|
|
|
# extends and includes that match it
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def classes_and_includes_and_extends_for name
|
2011-06-27 22:28:25 -04:00
|
|
|
klasses = []
|
2012-11-26 23:28:14 -05:00
|
|
|
extends = []
|
2011-06-27 22:28:25 -04:00
|
|
|
includes = []
|
|
|
|
|
|
|
|
found = @stores.map do |store|
|
|
|
|
begin
|
|
|
|
klass = store.load_class name
|
|
|
|
klasses << klass
|
2012-11-26 23:28:14 -05:00
|
|
|
extends << [klass.extends, store] if klass.extends
|
2011-06-27 22:28:25 -04:00
|
|
|
includes << [klass.includes, store] if klass.includes
|
|
|
|
[store, klass]
|
2012-11-26 23:28:14 -05:00
|
|
|
rescue RDoc::Store::MissingFileError
|
2011-06-27 22:28:25 -04:00
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
extends.reject! do |modules,| modules.empty? end
|
2011-06-27 22:28:25 -04:00
|
|
|
includes.reject! do |modules,| modules.empty? end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
[found, klasses, includes, extends]
|
2011-06-27 22:28:25 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Completes +name+ based on the caches. For Readline
|
|
|
|
|
|
|
|
def complete name
|
|
|
|
completions = []
|
|
|
|
|
|
|
|
klass, selector, method = parse_name name
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
complete_klass name, klass, selector, method, completions
|
|
|
|
complete_method name, klass, selector, completions
|
|
|
|
|
|
|
|
completions.sort.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def complete_klass name, klass, selector, method, completions # :nodoc:
|
|
|
|
klasses = classes.keys
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# may need to include Foo when given Foo::
|
|
|
|
klass_name = method ? name : klass
|
|
|
|
|
|
|
|
if name !~ /#|\./ then
|
2013-09-18 19:33:36 -04:00
|
|
|
completions.replace klasses.grep(/^#{Regexp.escape klass_name}[^:]*$/)
|
2011-06-16 00:59:24 -04:00
|
|
|
completions.concat klasses.grep(/^#{Regexp.escape name}[^:]*$/) if
|
|
|
|
name =~ /::$/
|
2011-05-13 20:39:16 -04:00
|
|
|
|
|
|
|
completions << klass if classes.key? klass # to complete a method name
|
2010-04-01 03:45:16 -04:00
|
|
|
elsif selector then
|
|
|
|
completions << klass if classes.key? klass
|
|
|
|
elsif classes.key? klass_name then
|
|
|
|
completions << klass_name
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def complete_method name, klass, selector, completions # :nodoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
if completions.include? klass and name =~ /#|\.|::/ then
|
|
|
|
methods = list_methods_matching name
|
|
|
|
|
|
|
|
if not methods.empty? then
|
|
|
|
# remove Foo if given Foo:: and a method was found
|
|
|
|
completions.delete klass
|
|
|
|
elsif selector then
|
|
|
|
# replace Foo with Foo:: as given
|
|
|
|
completions.delete klass
|
2010-04-02 00:40:47 -04:00
|
|
|
completions << "#{klass}#{selector}"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
completions.concat methods
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Converts +document+ to text and writes it to the pager
|
|
|
|
|
|
|
|
def display document
|
|
|
|
page do |io|
|
2018-03-26 01:56:26 -04:00
|
|
|
f = formatter(io)
|
|
|
|
f.width = @width if @width and f.respond_to?(:width)
|
|
|
|
text = document.accept f
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
io.write text
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Outputs formatted RI data for class +name+. Groups undocumented classes
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def display_class name
|
|
|
|
return if name =~ /#|\./
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
found, klasses, includes, extends =
|
|
|
|
classes_and_includes_and_extends_for name
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
return if found.empty?
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
out = class_document name, found, klasses, includes, extends
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
display out
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Outputs formatted RI data for method +name+
|
|
|
|
|
|
|
|
def display_method name
|
2012-11-26 23:28:14 -05:00
|
|
|
out = RDoc::Markup::Document.new
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
add_method out, name
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
display out
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Outputs formatted RI data for the class or method +name+.
|
|
|
|
#
|
|
|
|
# Returns true if +name+ was found, false if it was not an alternative could
|
|
|
|
# be guessed, raises an error if +name+ couldn't be guessed.
|
|
|
|
|
|
|
|
def display_name name
|
2012-11-26 23:28:14 -05:00
|
|
|
if name =~ /\w:(\w|$)/ then
|
|
|
|
display_page name
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
return true if display_class name
|
|
|
|
|
|
|
|
display_method name if name =~ /::|#|\./
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue NotFoundError
|
|
|
|
matches = list_methods_matching name if name =~ /::|#|\./
|
2013-09-18 19:33:36 -04:00
|
|
|
matches = classes.keys.grep(/^#{Regexp.escape name}/) if matches.empty?
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
raise if matches.empty?
|
|
|
|
|
|
|
|
page do |io|
|
|
|
|
io.puts "#{name} not found, maybe you meant:"
|
|
|
|
io.puts
|
2012-11-26 23:28:14 -05:00
|
|
|
io.puts matches.sort.join("\n")
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2008-07-21 14:35:14 -04:00
|
|
|
##
|
2010-04-01 03:45:16 -04:00
|
|
|
# Displays each name in +name+
|
|
|
|
|
|
|
|
def display_names names
|
|
|
|
names.each do |name|
|
|
|
|
name = expand_name name
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
display_name name
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2011-06-27 22:28:25 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Outputs formatted RI data for page +name+.
|
|
|
|
|
|
|
|
def display_page name
|
|
|
|
store_name, page_name = name.split ':', 2
|
|
|
|
|
|
|
|
store = @stores.find { |s| s.source == store_name }
|
|
|
|
|
|
|
|
return display_page_list store if page_name.empty?
|
|
|
|
|
|
|
|
pages = store.cache[:pages]
|
|
|
|
|
|
|
|
unless pages.include? page_name then
|
|
|
|
found_names = pages.select do |n|
|
2012-11-29 20:39:03 -05:00
|
|
|
n =~ /#{Regexp.escape page_name}\.[^.]+$/
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 20:39:03 -05:00
|
|
|
if found_names.length.zero? then
|
|
|
|
return display_page_list store, pages
|
|
|
|
elsif found_names.length > 1 then
|
2012-11-26 23:28:14 -05:00
|
|
|
return display_page_list store, found_names, page_name
|
|
|
|
end
|
|
|
|
|
|
|
|
page_name = found_names.first
|
|
|
|
end
|
|
|
|
|
|
|
|
page = store.load_page page_name
|
|
|
|
|
|
|
|
display page.comment
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Outputs a formatted RI page list for the pages in +store+.
|
|
|
|
|
|
|
|
def display_page_list store, pages = store.cache[:pages], search = nil
|
|
|
|
out = RDoc::Markup::Document.new
|
|
|
|
|
|
|
|
title = if search then
|
|
|
|
"#{search} pages"
|
|
|
|
else
|
|
|
|
'Pages'
|
|
|
|
end
|
|
|
|
|
|
|
|
out << RDoc::Markup::Heading.new(1, "#{title} in #{store.friendly_path}")
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
|
|
|
|
list = RDoc::Markup::List.new(:BULLET)
|
|
|
|
|
|
|
|
pages.each do |page|
|
|
|
|
list << RDoc::Markup::Paragraph.new(page)
|
|
|
|
end
|
|
|
|
|
|
|
|
out << list
|
|
|
|
|
|
|
|
display out
|
|
|
|
end
|
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
def check_did_you_mean # :nodoc:
|
|
|
|
if defined? DidYouMean::SpellChecker
|
|
|
|
true
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
require 'did_you_mean'
|
|
|
|
if defined? DidYouMean::SpellChecker
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Expands abbreviated klass +klass+ into a fully-qualified class. "Zl::Da"
|
|
|
|
# will be expanded to Zlib::DataError.
|
|
|
|
|
|
|
|
def expand_class klass
|
2017-10-09 22:01:00 -04:00
|
|
|
class_names = classes.keys
|
|
|
|
ary = class_names.grep(Regexp.new("\\A#{klass.gsub(/(?=::|\z)/, '[^:]*')}\\z"))
|
|
|
|
if ary.length != 1 && ary.first != klass
|
|
|
|
if check_did_you_mean
|
|
|
|
suggestions = DidYouMean::SpellChecker.new(dictionary: class_names).correct(klass)
|
|
|
|
raise NotFoundError.new(klass, suggestions)
|
|
|
|
else
|
|
|
|
raise NotFoundError, klass
|
|
|
|
end
|
|
|
|
end
|
2016-09-05 06:35:30 -04:00
|
|
|
ary.first
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Expands the class portion of +name+ into a fully-qualified class. See
|
|
|
|
# #expand_class.
|
|
|
|
|
|
|
|
def expand_name name
|
|
|
|
klass, selector, method = parse_name name
|
|
|
|
|
|
|
|
return [selector, method].join if klass.empty?
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
case selector
|
|
|
|
when ':' then
|
|
|
|
[find_store(klass), selector, method]
|
|
|
|
else
|
|
|
|
[expand_class(klass), selector, method]
|
|
|
|
end.join
|
2008-07-21 14:35:14 -04:00
|
|
|
end
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# Filters the methods in +found+ trying to find a match for +name+.
|
|
|
|
|
|
|
|
def filter_methods found, name
|
|
|
|
regexp = name_regexp name
|
|
|
|
|
|
|
|
filtered = found.find_all do |store, methods|
|
|
|
|
methods.any? { |method| method.full_name =~ regexp }
|
|
|
|
end
|
|
|
|
|
|
|
|
return filtered unless filtered.empty?
|
|
|
|
|
|
|
|
found
|
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2010-04-01 03:45:16 -04:00
|
|
|
# Yields items matching +name+ including the store they were found in, the
|
|
|
|
# class being searched for, the class they were found in (an ancestor) the
|
|
|
|
# types of methods to look up (from #method_type), and the method name being
|
|
|
|
# searched for
|
|
|
|
|
|
|
|
def find_methods name
|
|
|
|
klass, selector, method = parse_name name
|
|
|
|
|
|
|
|
types = method_type selector
|
|
|
|
|
|
|
|
klasses = nil
|
|
|
|
ambiguous = klass.empty?
|
|
|
|
|
|
|
|
if ambiguous then
|
|
|
|
klasses = classes.keys
|
|
|
|
else
|
|
|
|
klasses = ancestors_of klass
|
|
|
|
klasses.unshift klass
|
|
|
|
end
|
|
|
|
|
|
|
|
methods = []
|
|
|
|
|
|
|
|
klasses.each do |ancestor|
|
|
|
|
ancestors = classes[ancestor]
|
|
|
|
|
|
|
|
next unless ancestors
|
|
|
|
|
|
|
|
klass = ancestor if ambiguous
|
|
|
|
|
|
|
|
ancestors.each do |store|
|
|
|
|
methods << [store, klass, ancestor, types, method]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
methods = methods.sort_by do |_, k, a, _, m|
|
|
|
|
[k, a, m].compact
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
methods.each do |item|
|
2010-04-10 02:36:13 -04:00
|
|
|
yield(*item) # :yields: store, klass, ancestor, types, method
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
self
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2004-01-06 23:05:25 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Finds a store that matches +name+ which can be the name of a gem, "ruby",
|
|
|
|
# "home" or "site".
|
|
|
|
#
|
|
|
|
# See also RDoc::Store#source
|
|
|
|
|
|
|
|
def find_store name
|
|
|
|
@stores.each do |store|
|
|
|
|
source = store.source
|
|
|
|
|
|
|
|
return source if source == name
|
|
|
|
|
|
|
|
return source if
|
|
|
|
store.type == :gem and source =~ /^#{Regexp.escape name}-\d/
|
|
|
|
end
|
|
|
|
|
|
|
|
raise RDoc::RI::Driver::NotFoundError, name
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates a new RDoc::Markup::Formatter. If a formatter is given with -f,
|
|
|
|
# use it. If we're outputting to a pager, use bs, otherwise ansi.
|
|
|
|
|
2010-06-27 16:36:12 -04:00
|
|
|
def formatter(io)
|
2010-04-01 03:45:16 -04:00
|
|
|
if @formatter_klass then
|
|
|
|
@formatter_klass.new
|
2010-06-27 16:36:12 -04:00
|
|
|
elsif paging? or !io.tty? then
|
2010-04-01 03:45:16 -04:00
|
|
|
RDoc::Markup::ToBs.new
|
|
|
|
else
|
|
|
|
RDoc::Markup::ToAnsi.new
|
|
|
|
end
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2004-01-06 23:05:25 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2010-04-01 03:45:16 -04:00
|
|
|
# Runs ri interactively using Readline if it is available.
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def interactive
|
|
|
|
puts "\nEnter the method name you want to look up."
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
if defined? Readline then
|
|
|
|
Readline.completion_proc = method :complete
|
|
|
|
puts "You can use tab to autocomplete."
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
puts "Enter a blank line to exit.\n\n"
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
loop do
|
|
|
|
name = if defined? Readline then
|
|
|
|
Readline.readline ">> "
|
|
|
|
else
|
|
|
|
print ">> "
|
|
|
|
$stdin.gets
|
|
|
|
end
|
|
|
|
|
|
|
|
return if name.nil? or name.empty?
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
begin
|
2016-03-23 01:25:29 -04:00
|
|
|
display_name expand_name(name.strip)
|
2010-04-01 03:45:16 -04:00
|
|
|
rescue NotFoundError => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
end
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
rescue Interrupt
|
|
|
|
exit
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
2004-01-06 00:59:31 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2011-05-13 20:39:16 -04:00
|
|
|
# Lists classes known to ri starting with +names+. If +names+ is empty all
|
|
|
|
# known classes are shown.
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2011-05-13 20:39:16 -04:00
|
|
|
def list_known_classes names = []
|
2010-04-01 03:45:16 -04:00
|
|
|
classes = []
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
stores.each do |store|
|
2012-11-26 23:28:14 -05:00
|
|
|
classes << store.module_names
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
classes = classes.flatten.uniq.sort
|
2008-01-08 05:49:19 -05:00
|
|
|
|
2011-05-13 20:39:16 -04:00
|
|
|
unless names.empty? then
|
|
|
|
filter = Regexp.union names.map { |name| /^#{name}/ }
|
|
|
|
|
|
|
|
classes = classes.grep filter
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
page do |io|
|
|
|
|
if paging? or io.tty? then
|
2011-05-13 20:39:16 -04:00
|
|
|
if names.empty? then
|
|
|
|
io.puts "Classes and Modules known to ri:"
|
|
|
|
else
|
|
|
|
io.puts "Classes and Modules starting with #{names.join ', '}:"
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
io.puts
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
io.puts classes.join("\n")
|
|
|
|
end
|
|
|
|
end
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns an Array of methods matching +name+
|
|
|
|
|
|
|
|
def list_methods_matching name
|
|
|
|
found = []
|
|
|
|
|
|
|
|
find_methods name do |store, klass, ancestor, types, method|
|
|
|
|
if types == :instance or types == :both then
|
|
|
|
methods = store.instance_methods[ancestor]
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
if methods then
|
2011-05-13 20:39:16 -04:00
|
|
|
matches = methods.grep(/^#{Regexp.escape method.to_s}/)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
matches = matches.map do |match|
|
|
|
|
"#{klass}##{match}"
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
found.concat matches
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
if types == :class or types == :both then
|
|
|
|
methods = store.class_methods[ancestor]
|
|
|
|
|
|
|
|
next unless methods
|
2011-05-13 20:39:16 -04:00
|
|
|
matches = methods.grep(/^#{Regexp.escape method.to_s}/)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
matches = matches.map do |match|
|
|
|
|
"#{klass}::#{match}"
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
found.concat matches
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
found.uniq
|
2004-01-06 00:59:31 -05:00
|
|
|
end
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Loads RI data for method +name+ on +klass+ from +store+. +type+ and
|
|
|
|
# +cache+ indicate if it is a class or instance method.
|
|
|
|
|
|
|
|
def load_method store, cache, klass, type, name
|
2020-10-27 00:42:52 -04:00
|
|
|
methods = store.public_send(cache)[klass]
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
return unless methods
|
|
|
|
|
|
|
|
method = methods.find do |method_name|
|
|
|
|
method_name == name
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless method
|
|
|
|
|
|
|
|
store.load_method klass, "#{type}#{method}"
|
2013-09-18 19:33:36 -04:00
|
|
|
rescue RDoc::Store::MissingFileError => e
|
|
|
|
comment = RDoc::Comment.new("missing documentation at #{e.file}").parse
|
|
|
|
|
|
|
|
method = RDoc::AnyMethod.new nil, name
|
|
|
|
method.comment = comment
|
|
|
|
method
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns an Array of RI data for methods matching +name+
|
|
|
|
|
|
|
|
def load_methods_matching name
|
|
|
|
found = []
|
|
|
|
|
|
|
|
find_methods name do |store, klass, ancestor, types, method|
|
|
|
|
methods = []
|
|
|
|
|
|
|
|
methods << load_method(store, :class_methods, ancestor, '::', method) if
|
2011-02-01 19:32:30 -05:00
|
|
|
[:class, :both].include? types
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
methods << load_method(store, :instance_methods, ancestor, '#', method) if
|
2011-02-01 19:32:30 -05:00
|
|
|
[:instance, :both].include? types
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
found << [store, methods.compact]
|
|
|
|
end
|
|
|
|
|
|
|
|
found.reject do |path, methods| methods.empty? end
|
|
|
|
end
|
|
|
|
|
2011-06-27 22:28:25 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Returns a filtered list of methods matching +name+
|
|
|
|
|
|
|
|
def lookup_method name
|
|
|
|
found = load_methods_matching name
|
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
if found.empty?
|
|
|
|
if check_did_you_mean
|
|
|
|
methods = []
|
|
|
|
_, _, method_name = parse_name name
|
|
|
|
find_methods name do |store, klass, ancestor, types, method|
|
|
|
|
methods.push(*store.class_methods[klass]) if [:class, :both].include? types
|
|
|
|
methods.push(*store.instance_methods[klass]) if [:instance, :both].include? types
|
|
|
|
end
|
|
|
|
methods = methods.uniq
|
|
|
|
suggestions = DidYouMean::SpellChecker.new(dictionary: methods).correct(method_name)
|
|
|
|
raise NotFoundError.new(name, suggestions)
|
|
|
|
else
|
|
|
|
raise NotFoundError, name
|
|
|
|
end
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
filter_methods found, name
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds a RDoc::Markup::Document from +found+, +klasses+ and +includes+
|
2011-06-27 22:28:25 -04:00
|
|
|
|
|
|
|
def method_document name, filtered
|
|
|
|
out = RDoc::Markup::Document.new
|
|
|
|
|
|
|
|
out << RDoc::Markup::Heading.new(1, name)
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
|
|
|
|
filtered.each do |store, methods|
|
|
|
|
methods.each do |method|
|
2013-09-18 19:33:36 -04:00
|
|
|
render_method out, store, method, name
|
2011-06-27 22:28:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Returns the type of method (:both, :instance, :class) for +selector+
|
|
|
|
|
|
|
|
def method_type selector
|
|
|
|
case selector
|
|
|
|
when '.', nil then :both
|
|
|
|
when '#' then :instance
|
|
|
|
else :class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# Returns a regular expression for +name+ that will match an
|
|
|
|
# RDoc::AnyMethod's name.
|
|
|
|
|
|
|
|
def name_regexp name
|
|
|
|
klass, type, name = parse_name name
|
|
|
|
|
|
|
|
case type
|
|
|
|
when '#', '::' then
|
2011-05-13 20:39:16 -04:00
|
|
|
/^#{klass}#{type}#{Regexp.escape name}$/
|
2011-02-01 19:32:30 -05:00
|
|
|
else
|
2011-05-13 20:39:16 -04:00
|
|
|
/^#{klass}(#|::)#{Regexp.escape name}$/
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Paginates output through a pager program.
|
|
|
|
|
|
|
|
def page
|
|
|
|
if pager = setup_pager then
|
|
|
|
begin
|
|
|
|
yield pager
|
|
|
|
ensure
|
|
|
|
pager.close
|
2012-11-26 23:28:14 -05:00
|
|
|
@jruby_pager_process.wait_for if @jruby_pager_process
|
2008-01-31 00:10:58 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
yield $stdout
|
2008-01-31 00:10:58 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
rescue Errno::EPIPE
|
|
|
|
ensure
|
|
|
|
@paging = false
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Are we using a pager?
|
|
|
|
|
|
|
|
def paging?
|
|
|
|
@paging
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2011-02-01 19:32:30 -05:00
|
|
|
# Extracts the class, selector and method name parts from +name+ like
|
2010-04-01 03:45:16 -04:00
|
|
|
# Foo::Bar#baz.
|
|
|
|
#
|
|
|
|
# NOTE: Given Foo::Bar, Bar is considered a class even though it may be a
|
2012-11-26 23:28:14 -05:00
|
|
|
# method
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
def parse_name name
|
2012-11-26 23:28:14 -05:00
|
|
|
parts = name.split(/(::?|#|\.)/)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
if parts.length == 1 then
|
2011-07-30 20:19:00 -04:00
|
|
|
if parts.first =~ /^[a-z]|^([%&*+\/<>^`|~-]|\+@|-@|<<|<=>?|===?|=>|=~|>>|\[\]=?|~@)$/ then
|
2010-04-01 03:45:16 -04:00
|
|
|
type = '.'
|
|
|
|
meth = parts.pop
|
|
|
|
else
|
|
|
|
type = nil
|
|
|
|
meth = nil
|
2008-09-24 22:43:03 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
elsif parts.length == 2 or parts.last =~ /::|#|\./ then
|
|
|
|
type = parts.pop
|
|
|
|
meth = nil
|
2012-12-03 18:32:39 -05:00
|
|
|
elsif parts[1] == ':' then
|
|
|
|
klass = parts.shift
|
|
|
|
type = parts.shift
|
|
|
|
meth = parts.join
|
2010-04-01 03:45:16 -04:00
|
|
|
elsif parts[-2] != '::' or parts.last !~ /^[A-Z]/ then
|
|
|
|
meth = parts.pop
|
|
|
|
type = parts.pop
|
|
|
|
end
|
|
|
|
|
2012-12-03 18:32:39 -05:00
|
|
|
klass ||= parts.join
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
[klass, type, meth]
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Renders the +klass+ from +store+ to +out+. If the klass has no
|
|
|
|
# documentable items the class is added to +also_in+ instead.
|
|
|
|
|
|
|
|
def render_class out, store, klass, also_in # :nodoc:
|
|
|
|
comment = klass.comment
|
|
|
|
# TODO the store's cache should always return an empty Array
|
|
|
|
class_methods = store.class_methods[klass.full_name] || []
|
|
|
|
instance_methods = store.instance_methods[klass.full_name] || []
|
|
|
|
attributes = store.attributes[klass.full_name] || []
|
|
|
|
|
|
|
|
if comment.empty? and
|
|
|
|
instance_methods.empty? and class_methods.empty? then
|
|
|
|
also_in << store
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
add_from out, store
|
|
|
|
|
|
|
|
class_document_comment out, comment
|
|
|
|
|
|
|
|
if class_methods or instance_methods or not klass.constants.empty? then
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
class_document_constants out, klass
|
|
|
|
|
|
|
|
add_method_list out, class_methods, 'Class methods'
|
|
|
|
add_method_list out, instance_methods, 'Instance methods'
|
|
|
|
add_method_list out, attributes, 'Attributes'
|
|
|
|
|
|
|
|
add_method_documentation out, klass if @show_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_method out, store, method, name # :nodoc:
|
|
|
|
out << RDoc::Markup::Paragraph.new("(from #{store.friendly_path})")
|
|
|
|
|
|
|
|
unless name =~ /^#{Regexp.escape method.parent_name}/ then
|
|
|
|
out << RDoc::Markup::Heading.new(3, "Implementation from #{method.parent_name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
|
|
|
|
|
|
|
render_method_arguments out, method.arglists
|
|
|
|
render_method_superclass out, method
|
2018-03-26 01:56:26 -04:00
|
|
|
if method.is_alias_for
|
|
|
|
al = method.is_alias_for
|
|
|
|
alias_for = store.load_method al.parent_name, "#{al.name_prefix}#{al.name}"
|
|
|
|
render_method_comment out, method, alias_for
|
|
|
|
else
|
|
|
|
render_method_comment out, method
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_method_arguments out, arglists # :nodoc:
|
|
|
|
return unless arglists
|
|
|
|
|
|
|
|
arglists = arglists.chomp.split "\n"
|
|
|
|
arglists = arglists.map { |line| line + "\n" }
|
|
|
|
out << RDoc::Markup::Verbatim.new(*arglists)
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
|
|
|
end
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
def render_method_comment out, method, alias_for = nil# :nodoc:
|
|
|
|
if alias_for
|
|
|
|
unless method.comment.nil? or method.comment.empty?
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << method.comment
|
|
|
|
end
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
2018-11-27 02:02:36 -05:00
|
|
|
out << RDoc::Markup::Paragraph.new("(This method is an alias for #{alias_for.full_name}.)")
|
2018-03-26 01:56:26 -04:00
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << alias_for.comment
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
else
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << method.comment
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_method_superclass out, method # :nodoc:
|
|
|
|
return unless
|
|
|
|
method.respond_to?(:superclass_method) and method.superclass_method
|
|
|
|
|
|
|
|
out << RDoc::Markup::BlankLine.new
|
|
|
|
out << RDoc::Markup::Heading.new(4, "(Uses superclass method #{method.superclass_method})")
|
|
|
|
out << RDoc::Markup::Rule.new(1)
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Looks up and displays ri data according to the options given.
|
|
|
|
|
|
|
|
def run
|
|
|
|
if @list_doc_dirs then
|
|
|
|
puts @doc_dirs
|
2011-05-13 20:39:16 -04:00
|
|
|
elsif @list then
|
|
|
|
list_known_classes @names
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif @server then
|
|
|
|
start_server
|
2011-05-13 20:39:16 -04:00
|
|
|
elsif @interactive or @names.empty? then
|
2010-04-01 03:45:16 -04:00
|
|
|
interactive
|
|
|
|
else
|
|
|
|
display_names @names
|
|
|
|
end
|
|
|
|
rescue NotFoundError => e
|
|
|
|
abort e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sets up a pager program to pass output through. Tries the RI_PAGER and
|
|
|
|
# PAGER environment variables followed by pager, less then more.
|
|
|
|
|
|
|
|
def setup_pager
|
|
|
|
return if @use_stdout
|
|
|
|
|
|
|
|
pagers = [ENV['RI_PAGER'], ENV['PAGER'], 'pager', 'less', 'more']
|
|
|
|
|
2022-06-04 07:32:23 -04:00
|
|
|
require 'shellwords'
|
2010-04-01 03:45:16 -04:00
|
|
|
pagers.compact.uniq.each do |pager|
|
2022-06-04 07:32:23 -04:00
|
|
|
pager = Shellwords.split(pager)
|
|
|
|
next if pager.empty?
|
2010-05-01 01:06:21 -04:00
|
|
|
|
2022-06-04 07:32:23 -04:00
|
|
|
io = IO.popen(pager, 'w') rescue next
|
2012-11-26 23:28:14 -05:00
|
|
|
next if $? and $?.pid == io.pid and $?.exited? # pager didn't work
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
@paging = true
|
|
|
|
|
|
|
|
return io
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@use_stdout = true
|
|
|
|
|
|
|
|
nil
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Starts a WEBrick server for ri.
|
|
|
|
|
|
|
|
def start_server
|
2020-12-10 02:20:21 -05:00
|
|
|
begin
|
|
|
|
require 'webrick'
|
|
|
|
rescue LoadError
|
2020-12-10 07:04:57 -05:00
|
|
|
abort "webrick is not found. You may need to `gem install webrick` to install webrick."
|
2020-12-10 02:20:21 -05:00
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
server = WEBrick::HTTPServer.new :Port => @server
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
extra_doc_dirs = @stores.map {|s| s.type == :extra ? s.path : nil}.compact
|
|
|
|
|
|
|
|
server.mount '/', RDoc::Servlet, nil, extra_doc_dirs
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
trap 'INT' do server.shutdown end
|
|
|
|
trap 'TERM' do server.shutdown end
|
|
|
|
|
|
|
|
server.start
|
|
|
|
end
|
|
|
|
|
2008-01-08 04:07:31 -05:00
|
|
|
end
|