2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# AnyMethod is the base class for objects representing methods
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
class RDoc::AnyMethod < RDoc::MethodAttr
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# 2::
|
|
|
|
# RDoc 4
|
|
|
|
# Added calls_super
|
|
|
|
# Added parent name and class
|
|
|
|
# Added section title
|
2013-09-18 19:33:36 -04:00
|
|
|
# 3::
|
|
|
|
# RDoc 4.1
|
|
|
|
# Added is_alias_for
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
MARSHAL_VERSION = 3 # :nodoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Don't rename \#initialize to \::new
|
|
|
|
|
|
|
|
attr_accessor :dont_rename_initialize
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# The C function that implements this method (if it was defined in a C file)
|
|
|
|
|
|
|
|
attr_accessor :c_function
|
|
|
|
|
2021-10-11 13:44:37 -04:00
|
|
|
# The section title of the method (if defined in a C file via +:category:+)
|
|
|
|
attr_accessor :section_title
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# Parameters for this method
|
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
attr_accessor :params
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# If true this method uses +super+ to call a superclass version
|
|
|
|
|
|
|
|
attr_accessor :calls_super
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
include RDoc::TokenStream
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Creates a new AnyMethod with a token stream +text+ and +name+
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def initialize text, name
|
|
|
|
super
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
@c_function = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
@dont_rename_initialize = false
|
2011-02-01 19:32:30 -05:00
|
|
|
@token_stream = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@calls_super = false
|
|
|
|
@superclass_method = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Adds +an_alias+ as an alias for this method in +context+.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def add_alias an_alias, context = nil
|
2010-12-19 22:22:49 -05:00
|
|
|
method = self.class.new an_alias.text, an_alias.new_name
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
method.record_location an_alias.file
|
|
|
|
method.singleton = self.singleton
|
|
|
|
method.params = self.params
|
|
|
|
method.visibility = self.visibility
|
|
|
|
method.comment = an_alias.comment
|
|
|
|
method.is_alias_for = self
|
2010-04-01 03:45:16 -04:00
|
|
|
@aliases << method
|
2011-06-16 00:59:24 -04:00
|
|
|
context.add_method method if context
|
2010-12-19 22:22:49 -05:00
|
|
|
method
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-19 01:08:28 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Prefix for +aref+ is 'method'.
|
2010-04-19 01:08:28 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def aref_prefix
|
|
|
|
'method'
|
2010-04-19 01:08:28 -04:00
|
|
|
end
|
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
##
|
|
|
|
# The call_seq or the param_seq with method name, if there is no call_seq.
|
|
|
|
#
|
|
|
|
# Use this for displaying a method's argument lists.
|
|
|
|
|
|
|
|
def arglists
|
|
|
|
if @call_seq then
|
|
|
|
@call_seq
|
|
|
|
elsif @params then
|
|
|
|
"#{name}#{param_seq}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-10 19:17:09 -04:00
|
|
|
##
|
|
|
|
# Different ways to call this method
|
|
|
|
|
|
|
|
def call_seq
|
|
|
|
unless call_seq = _call_seq
|
|
|
|
call_seq = is_alias_for._call_seq if is_alias_for
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless call_seq
|
|
|
|
|
|
|
|
deduplicate_call_seq(call_seq)
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Sets the different ways you can call this method. If an empty +call_seq+
|
|
|
|
# is given nil is assumed.
|
|
|
|
#
|
|
|
|
# See also #param_seq
|
|
|
|
|
|
|
|
def call_seq= call_seq
|
|
|
|
return if call_seq.empty?
|
|
|
|
|
|
|
|
@call_seq = call_seq
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Loads is_alias_for from the internal name. Returns nil if the alias
|
|
|
|
# cannot be found.
|
|
|
|
|
|
|
|
def is_alias_for # :nodoc:
|
|
|
|
case @is_alias_for
|
|
|
|
when RDoc::MethodAttr then
|
|
|
|
@is_alias_for
|
|
|
|
when Array then
|
|
|
|
return nil unless @store
|
|
|
|
|
|
|
|
klass_name, singleton, method_name = @is_alias_for
|
|
|
|
|
|
|
|
return nil unless klass = @store.find_class_or_module(klass_name)
|
|
|
|
|
|
|
|
@is_alias_for = klass.find_method method_name, singleton
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Dumps this AnyMethod for use by ri. See also #marshal_load
|
|
|
|
|
|
|
|
def marshal_dump
|
|
|
|
aliases = @aliases.map do |a|
|
2011-06-16 00:59:24 -04:00
|
|
|
[a.name, parse(a.comment)]
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
is_alias_for = [
|
|
|
|
@is_alias_for.parent.full_name,
|
|
|
|
@is_alias_for.singleton,
|
|
|
|
@is_alias_for.name
|
|
|
|
] if @is_alias_for
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
[ MARSHAL_VERSION,
|
|
|
|
@name,
|
|
|
|
full_name,
|
|
|
|
@singleton,
|
|
|
|
@visibility,
|
|
|
|
parse(@comment),
|
|
|
|
@call_seq,
|
|
|
|
@block_params,
|
|
|
|
aliases,
|
2010-04-10 02:36:13 -04:00
|
|
|
@params,
|
2012-11-27 03:54:03 -05:00
|
|
|
@file.relative_name,
|
2012-11-26 23:28:14 -05:00
|
|
|
@calls_super,
|
|
|
|
@parent.name,
|
|
|
|
@parent.class,
|
|
|
|
@section.title,
|
2013-09-18 19:33:36 -04:00
|
|
|
is_alias_for,
|
2010-04-01 03:45:16 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Loads this AnyMethod from +array+. For a loaded AnyMethod the following
|
|
|
|
# methods will return cached values:
|
|
|
|
#
|
|
|
|
# * #full_name
|
|
|
|
# * #parent_name
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def marshal_load array
|
2013-01-22 20:02:24 -05:00
|
|
|
initialize_visibility
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@dont_rename_initialize = nil
|
|
|
|
@token_stream = nil
|
2010-04-20 23:10:03 -04:00
|
|
|
@aliases = []
|
2012-11-26 23:28:14 -05:00
|
|
|
@parent = nil
|
|
|
|
@parent_name = nil
|
|
|
|
@parent_class = nil
|
|
|
|
@section = nil
|
|
|
|
@file = nil
|
|
|
|
|
|
|
|
version = array[0]
|
|
|
|
@name = array[1]
|
|
|
|
@full_name = array[2]
|
|
|
|
@singleton = array[3]
|
|
|
|
@visibility = array[4]
|
|
|
|
@comment = array[5]
|
|
|
|
@call_seq = array[6]
|
|
|
|
@block_params = array[7]
|
|
|
|
# 8 handled below
|
|
|
|
@params = array[9]
|
|
|
|
# 10 handled below
|
|
|
|
@calls_super = array[11]
|
|
|
|
@parent_name = array[12]
|
|
|
|
@parent_title = array[13]
|
|
|
|
@section_title = array[14]
|
2013-09-18 19:33:36 -04:00
|
|
|
@is_alias_for = array[15]
|
2011-06-16 00:59:24 -04:00
|
|
|
|
|
|
|
array[8].each do |new_name, comment|
|
|
|
|
add_alias RDoc::Alias.new(nil, @name, new_name, comment, @singleton)
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@parent_name ||= if @full_name =~ /#/ then
|
|
|
|
$`
|
|
|
|
else
|
|
|
|
name = @full_name.split('::')
|
|
|
|
name.pop
|
|
|
|
name.join '::'
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
@file = RDoc::TopLevel.new array[10] if version > 0
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Method name
|
2010-12-19 22:22:49 -05:00
|
|
|
#
|
|
|
|
# If the method has no assigned name, it extracts it from #call_seq.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def name
|
|
|
|
return @name if @name
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@name =
|
|
|
|
@call_seq[/^.*?\.(\w+)/, 1] ||
|
|
|
|
@call_seq[/^.*?(\w+)/, 1] ||
|
|
|
|
@call_seq if @call_seq
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-02-01 19:32:30 -05:00
|
|
|
# A list of this method's method and yield parameters. +call-seq+ params
|
|
|
|
# are preferred over parsed method and block params.
|
|
|
|
|
|
|
|
def param_list
|
|
|
|
if @call_seq then
|
|
|
|
params = @call_seq.split("\n").last
|
|
|
|
params = params.sub(/.*?\((.*)\)/, '\1')
|
|
|
|
params = params.sub(/(\{|do)\s*\|([^|]*)\|.*/, ',\2')
|
|
|
|
elsif @params then
|
|
|
|
params = @params.sub(/\((.*)\)/, '\1')
|
|
|
|
|
|
|
|
params << ",#{@block_params}" if @block_params
|
|
|
|
elsif @block_params then
|
|
|
|
params = @block_params
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
2014-09-04 21:41:25 -04:00
|
|
|
if @block_params then
|
|
|
|
# If this method has explicit block parameters, remove any explicit
|
|
|
|
# &block
|
2017-11-27 05:45:24 -05:00
|
|
|
params = params.sub(/,?\s*&\w+/, '')
|
2014-09-04 21:41:25 -04:00
|
|
|
else
|
2017-11-27 05:45:24 -05:00
|
|
|
params = params.sub(/\&(\w+)/, '\1')
|
2014-09-04 21:41:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
params = params.gsub(/\s+/, '').split(',').reject(&:empty?)
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
params.map { |param| param.sub(/=.*/, '') }
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Pretty parameter list for this method. If the method's parameters were
|
|
|
|
# given by +call-seq+ it is preferred over the parsed values.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def param_seq
|
2011-02-01 19:32:30 -05:00
|
|
|
if @call_seq then
|
|
|
|
params = @call_seq.split("\n").last
|
|
|
|
params = params.sub(/[^( ]+/, '')
|
|
|
|
params = params.sub(/(\|[^|]+\|)\s*\.\.\.\s*(end|\})/, '\1 \2')
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif @params then
|
2011-02-01 19:32:30 -05:00
|
|
|
params = @params.gsub(/\s*\#.*/, '')
|
2017-11-27 05:45:24 -05:00
|
|
|
params = params.tr_s("\n ", " ")
|
2011-02-01 19:32:30 -05:00
|
|
|
params = "(#{params})" unless params[0] == ?(
|
2012-11-26 23:28:14 -05:00
|
|
|
else
|
|
|
|
params = ''
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
if @block_params then
|
2010-04-01 03:45:16 -04:00
|
|
|
# If this method has explicit block parameters, remove any explicit
|
|
|
|
# &block
|
2017-11-27 05:45:24 -05:00
|
|
|
params = params.sub(/,?\s*&\w+/, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
block = @block_params.tr_s("\n ", " ")
|
2010-04-01 03:45:16 -04:00
|
|
|
if block[0] == ?(
|
2017-11-27 05:45:24 -05:00
|
|
|
block = block.sub(/^\(/, '').sub(/\)/, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
params << " { |#{block}| ... }"
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Sets the store for this method and its referenced code objects.
|
|
|
|
|
|
|
|
def store= store
|
|
|
|
super
|
|
|
|
|
|
|
|
@file = @store.add_file @file.full_name if @file
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# For methods that +super+, find the superclass method that would be called.
|
|
|
|
|
|
|
|
def superclass_method
|
|
|
|
return unless @calls_super
|
|
|
|
return @superclass_method if @superclass_method
|
|
|
|
|
|
|
|
parent.each_ancestor do |ancestor|
|
|
|
|
if method = ancestor.method_list.find { |m| m.name == @name } then
|
|
|
|
@superclass_method = method
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@superclass_method
|
|
|
|
end
|
|
|
|
|
2019-09-10 19:17:09 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
##
|
|
|
|
# call_seq without deduplication and alias lookup.
|
|
|
|
|
|
|
|
def _call_seq
|
|
|
|
@call_seq if defined?(@call_seq) && @call_seq
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
##
|
|
|
|
# call_seq with alias examples information removed, if this
|
|
|
|
# method is an alias method.
|
|
|
|
|
|
|
|
def deduplicate_call_seq(call_seq)
|
|
|
|
return call_seq unless is_alias_for || !aliases.empty?
|
|
|
|
|
|
|
|
method_name = self.name
|
|
|
|
method_name = method_name[0, 1] if method_name =~ /\A\[/
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2019-09-10 19:17:09 -04:00
|
|
|
entries = call_seq.split "\n"
|
|
|
|
|
|
|
|
ignore = aliases.map(&:name)
|
|
|
|
if is_alias_for
|
|
|
|
ignore << is_alias_for.name
|
|
|
|
ignore.concat is_alias_for.aliases.map(&:name)
|
|
|
|
end
|
|
|
|
ignore.map! { |n| n =~ /\A\[/ ? n[0, 1] : n}
|
|
|
|
ignore.delete(method_name)
|
|
|
|
ignore = Regexp.union(ignore)
|
|
|
|
|
|
|
|
matching = entries.reject do |entry|
|
|
|
|
entry =~ /^\w*\.?#{ignore}/ or
|
|
|
|
entry =~ /\s#{ignore}\s/
|
|
|
|
end
|
|
|
|
|
2021-09-20 10:09:14 -04:00
|
|
|
matching.empty? ? nil : matching.join("\n")
|
2019-09-10 19:17:09 -04:00
|
|
|
end
|
|
|
|
end
|