2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Base class for the RDoc code tree.
|
|
|
|
#
|
|
|
|
# We contain the common stuff for contexts (which are containers) and other
|
|
|
|
# elements (methods, attributes and so on)
|
2010-04-19 01:08:28 -04:00
|
|
|
#
|
|
|
|
# Here's the tree of the CodeObject subclasses:
|
|
|
|
#
|
|
|
|
# * RDoc::Context
|
|
|
|
# * RDoc::TopLevel
|
|
|
|
# * RDoc::ClassModule
|
2010-12-19 22:22:49 -05:00
|
|
|
# * RDoc::AnonClass (never used so far)
|
2010-04-19 01:08:28 -04:00
|
|
|
# * RDoc::NormalClass
|
|
|
|
# * RDoc::NormalModule
|
|
|
|
# * RDoc::SingleClass
|
2010-12-19 22:22:49 -05:00
|
|
|
# * RDoc::MethodAttr
|
|
|
|
# * RDoc::Attr
|
|
|
|
# * RDoc::AnyMethod
|
|
|
|
# * RDoc::GhostMethod
|
|
|
|
# * RDoc::MetaMethod
|
2010-04-19 01:08:28 -04:00
|
|
|
# * RDoc::Alias
|
|
|
|
# * RDoc::Constant
|
2013-09-18 19:33:36 -04:00
|
|
|
# * RDoc::Mixin
|
|
|
|
# * RDoc::Require
|
|
|
|
# * RDoc::Include
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
class RDoc::CodeObject
|
|
|
|
|
|
|
|
include RDoc::Text
|
|
|
|
|
|
|
|
##
|
|
|
|
# Our comment
|
|
|
|
|
|
|
|
attr_reader :comment
|
|
|
|
|
|
|
|
##
|
|
|
|
# Do we document our children?
|
|
|
|
|
|
|
|
attr_reader :document_children
|
|
|
|
|
|
|
|
##
|
|
|
|
# Do we document ourselves?
|
|
|
|
|
|
|
|
attr_reader :document_self
|
|
|
|
|
|
|
|
##
|
|
|
|
# Are we done documenting (ie, did we come across a :enddoc:)?
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
attr_reader :done_documenting
|
|
|
|
|
|
|
|
##
|
|
|
|
# Which file this code object was defined in
|
|
|
|
|
|
|
|
attr_reader :file
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Force documentation of this CodeObject
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
attr_reader :force_documentation
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# Line in #file where this CodeObject was defined
|
|
|
|
|
|
|
|
attr_accessor :line
|
|
|
|
|
2010-04-22 22:32:20 -04:00
|
|
|
##
|
|
|
|
# Hash of arbitrary metadata for this CodeObject
|
|
|
|
|
|
|
|
attr_reader :metadata
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
##
|
|
|
|
# Offset in #file where this CodeObject was defined
|
|
|
|
#--
|
|
|
|
# TODO character or byte?
|
|
|
|
|
|
|
|
attr_accessor :offset
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Sets the parent CodeObject
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
attr_writer :parent
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Did we ever receive a +:nodoc:+ directive?
|
|
|
|
|
|
|
|
attr_reader :received_nodoc
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Set the section this CodeObject is in
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
attr_writer :section
|
|
|
|
|
|
|
|
##
|
|
|
|
# The RDoc::Store for this object.
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
attr_reader :store
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# We are the model of the code, but we know that at some point we will be
|
|
|
|
# worked on by viewers. By implementing the Viewable protocol, viewers can
|
|
|
|
# associated themselves with these objects.
|
|
|
|
|
|
|
|
attr_accessor :viewer
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new CodeObject that will document itself and its children
|
|
|
|
|
|
|
|
def initialize
|
2013-09-18 19:33:36 -04:00
|
|
|
@metadata = {}
|
|
|
|
@comment = ''
|
|
|
|
@parent = nil
|
|
|
|
@parent_name = nil # for loading
|
|
|
|
@parent_class = nil # for loading
|
|
|
|
@section = nil
|
|
|
|
@section_title = nil # for loading
|
|
|
|
@file = nil
|
|
|
|
@full_name = nil
|
|
|
|
@store = nil
|
|
|
|
@track_visibility = true
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-01-22 20:02:24 -05:00
|
|
|
initialize_visibility
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Initializes state for visibility of this CodeObject and its children.
|
|
|
|
|
|
|
|
def initialize_visibility # :nodoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
@document_children = true
|
|
|
|
@document_self = true
|
|
|
|
@done_documenting = false
|
|
|
|
@force_documentation = false
|
2010-12-19 22:22:49 -05:00
|
|
|
@received_nodoc = false
|
2011-07-30 20:19:00 -04:00
|
|
|
@ignored = false
|
2013-09-18 19:33:36 -04:00
|
|
|
@suppressed = false
|
|
|
|
@track_visibility = true
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Replaces our comment with +comment+, unless it is empty.
|
|
|
|
|
|
|
|
def comment=(comment)
|
|
|
|
@comment = case comment
|
|
|
|
when NilClass then ''
|
|
|
|
when RDoc::Markup::Document then comment
|
2012-11-26 23:28:14 -05:00
|
|
|
when RDoc::Comment then comment.normalize
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
if comment and not comment.empty? then
|
|
|
|
normalize_comment comment
|
|
|
|
else
|
2011-02-01 19:32:30 -05:00
|
|
|
# HACK correct fix is to have #initialize create @comment
|
|
|
|
# with the correct encoding
|
2011-06-16 00:59:24 -04:00
|
|
|
if String === @comment and
|
|
|
|
Object.const_defined? :Encoding and @comment.empty? then
|
2011-02-01 19:32:30 -05:00
|
|
|
@comment.force_encoding comment.encoding
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
@comment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
2013-09-18 19:33:36 -04:00
|
|
|
# Should this CodeObject be displayed in output?
|
|
|
|
#
|
|
|
|
# A code object should be displayed if:
|
|
|
|
#
|
|
|
|
# * The item didn't have a nodoc or wasn't in a container that had nodoc
|
|
|
|
# * The item wasn't ignored
|
|
|
|
# * The item has documentation and was not suppressed
|
2011-07-30 20:19:00 -04:00
|
|
|
|
|
|
|
def display?
|
2013-09-18 19:33:36 -04:00
|
|
|
@document_self and not @ignored and
|
|
|
|
(documented? or not @suppressed)
|
2011-07-30 20:19:00 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Enables or disables documentation of this CodeObject's children unless it
|
|
|
|
# has been turned off by :enddoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def document_children=(document_children)
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless @track_visibility
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@document_children = document_children unless @done_documenting
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Enables or disables documentation of this CodeObject unless it has been
|
|
|
|
# turned off by :enddoc:. If the argument is +nil+ it means the
|
|
|
|
# documentation is turned off by +:nodoc:+.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def document_self=(document_self)
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless @track_visibility
|
2010-12-19 22:22:49 -05:00
|
|
|
return if @done_documenting
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@document_self = document_self
|
2010-12-19 22:22:49 -05:00
|
|
|
@received_nodoc = true if document_self.nil?
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Does this object have a comment with content or is #received_nodoc true?
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def documented?
|
2010-12-19 22:22:49 -05:00
|
|
|
@received_nodoc or !@comment.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Turns documentation on/off, and turns on/off #document_self
|
|
|
|
# and #document_children.
|
|
|
|
#
|
|
|
|
# Once documentation has been turned off (by +:enddoc:+),
|
|
|
|
# the object will refuse to turn #document_self or
|
|
|
|
# #document_children on, so +:doc:+ and +:start_doc:+ directives
|
|
|
|
# will have no effect in the current file.
|
|
|
|
|
|
|
|
def done_documenting=(value)
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless @track_visibility
|
|
|
|
@done_documenting = value
|
|
|
|
@document_self = !value
|
2010-12-19 22:22:49 -05:00
|
|
|
@document_children = @document_self
|
|
|
|
end
|
|
|
|
|
2011-05-13 20:39:16 -04:00
|
|
|
##
|
|
|
|
# Yields each parent of this CodeObject. See also
|
|
|
|
# RDoc::ClassModule#each_ancestor
|
|
|
|
|
|
|
|
def each_parent
|
|
|
|
code_object = self
|
|
|
|
|
|
|
|
while code_object = code_object.parent do
|
|
|
|
yield code_object
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
|
|
|
# File name where this CodeObject was found.
|
|
|
|
#
|
|
|
|
# See also RDoc::Context#in_files
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def file_name
|
|
|
|
return unless @file
|
|
|
|
|
|
|
|
@file.absolute_name
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Force the documentation of this object unless documentation
|
2012-11-26 23:28:14 -05:00
|
|
|
# has been turned off by :enddoc:
|
2010-12-19 22:22:49 -05:00
|
|
|
#--
|
|
|
|
# HACK untested, was assigning to an ivar
|
|
|
|
|
|
|
|
def force_documentation=(value)
|
|
|
|
@force_documentation = value unless @done_documenting
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sets the full_name overriding any computed full name.
|
|
|
|
#
|
|
|
|
# Set to +nil+ to clear RDoc's cached value
|
|
|
|
|
|
|
|
def full_name= full_name
|
|
|
|
@full_name = full_name
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
|
|
|
# Use this to ignore a CodeObject and all its children until found again
|
2013-09-18 19:33:36 -04:00
|
|
|
# (#record_location is called). An ignored item will not be displayed in
|
2011-07-30 20:19:00 -04:00
|
|
|
# documentation.
|
|
|
|
#
|
|
|
|
# See github issue #55
|
|
|
|
#
|
|
|
|
# The ignored status is temporary in order to allow implementation details
|
|
|
|
# to be hidden. At the end of processing a file RDoc allows all classes
|
|
|
|
# and modules to add new documentation to previously created classes.
|
|
|
|
#
|
|
|
|
# If a class was ignored (via stopdoc) then reopened later with additional
|
2013-09-18 19:33:36 -04:00
|
|
|
# documentation it should be displayed. If a class was ignored and never
|
|
|
|
# reopened it should not be displayed. The ignore flag allows this to
|
|
|
|
# occur.
|
2011-07-30 20:19:00 -04:00
|
|
|
|
|
|
|
def ignore
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless @track_visibility
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
@ignored = true
|
|
|
|
|
|
|
|
stop_doc
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Has this class been ignored?
|
2013-09-18 19:33:36 -04:00
|
|
|
#
|
|
|
|
# See also #ignore
|
2011-07-30 20:19:00 -04:00
|
|
|
|
|
|
|
def ignored?
|
|
|
|
@ignored
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# The options instance from the store this CodeObject is attached to, or a
|
|
|
|
# default options instance if the CodeObject is not attached.
|
|
|
|
#
|
|
|
|
# This is used by Text#snippet
|
|
|
|
|
|
|
|
def options
|
|
|
|
if @store and @store.rdoc then
|
|
|
|
@store.rdoc.options
|
|
|
|
else
|
|
|
|
RDoc::Options.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Our parent CodeObject. The parent may be missing for classes loaded from
|
|
|
|
# legacy RI data stores.
|
|
|
|
|
|
|
|
def parent
|
|
|
|
return @parent if @parent
|
|
|
|
return nil unless @parent_name
|
|
|
|
|
|
|
|
if @parent_class == RDoc::TopLevel then
|
|
|
|
@parent = @store.add_file @parent_name
|
|
|
|
else
|
|
|
|
@parent = @store.find_class_or_module @parent_name
|
|
|
|
|
|
|
|
return @parent if @parent
|
|
|
|
|
|
|
|
begin
|
|
|
|
@parent = @store.load_class @parent_name
|
|
|
|
rescue RDoc::Store::MissingFileError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# File name of our parent
|
|
|
|
|
|
|
|
def parent_file_name
|
|
|
|
@parent ? @parent.base_name : '(unknown)'
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Name of our parent
|
|
|
|
|
|
|
|
def parent_name
|
|
|
|
@parent ? @parent.full_name : '(unknown)'
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Records the RDoc::TopLevel (file) where this code object was defined
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def record_location top_level
|
2013-09-18 19:33:36 -04:00
|
|
|
@ignored = false
|
|
|
|
@suppressed = false
|
|
|
|
@file = top_level
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The section this CodeObject is in. Sections allow grouping of constants,
|
|
|
|
# attributes and methods inside a class or module.
|
|
|
|
|
|
|
|
def section
|
|
|
|
return @section if @section
|
|
|
|
|
|
|
|
@section = parent.add_section @section_title if parent
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Enable capture of documentation unless documentation has been
|
2012-11-26 23:28:14 -05:00
|
|
|
# turned off by :enddoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def start_doc
|
2010-12-19 22:22:49 -05:00
|
|
|
return if @done_documenting
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@document_self = true
|
|
|
|
@document_children = true
|
2013-09-18 19:33:36 -04:00
|
|
|
@ignored = false
|
|
|
|
@suppressed = false
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Disable capture of documentation
|
|
|
|
|
|
|
|
def stop_doc
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless @track_visibility
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@document_self = false
|
|
|
|
@document_children = false
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Sets the +store+ that contains this CodeObject
|
|
|
|
|
|
|
|
def store= store
|
|
|
|
@store = store
|
|
|
|
|
|
|
|
return unless @track_visibility
|
|
|
|
|
|
|
|
if :nodoc == options.visibility then
|
|
|
|
initialize_visibility
|
|
|
|
@track_visibility = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Use this to suppress a CodeObject and all its children until the next file
|
|
|
|
# it is seen in or documentation is discovered. A suppressed item with
|
|
|
|
# documentation will be displayed while an ignored item with documentation
|
|
|
|
# may not be displayed.
|
|
|
|
|
|
|
|
def suppress
|
|
|
|
return unless @track_visibility
|
|
|
|
|
|
|
|
@suppressed = true
|
|
|
|
|
|
|
|
stop_doc
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Has this class been suppressed?
|
|
|
|
#
|
|
|
|
# See also #suppress
|
|
|
|
|
|
|
|
def suppressed?
|
|
|
|
@suppressed
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|