Class: YARD::Docstring
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::String
|
|
Instance Chain:
self,
::String
|
|
Inherits: |
String
|
Defined in: | lib/yard/docstring.rb |
Overview
A documentation string, or "docstring" for short, encapsulates the
comments and metadata, or "tags", of an object. Meta-data is expressed
in the form +@tag VALUE+, where VALUE can span over multiple lines as
long as they are indented. The following @example
tag shows how tags
can be indented:
# @example My example # a = "hello world" # a.reverse # @version 1.0
Tags
can be nested in a documentation string, though the Tags::Tag
itself is responsible for parsing the inner tags.
Constant Summary
-
META_MATCH =
Deprecated.
Matches a tag at the start of a comment line
DocstringParser::META_MATCH
Creating a Docstring Object
-
#+(other) ⇒ Docstring
Adds another
Docstring
, copying over tags. -
#all=(content, parse = true)
Alias for #replace.
-
#dup ⇒ Docstring
Deep-copies a docstring.
-
.new(content = '', object = nil) ⇒ Docstring
constructor
Creates a new docstring with the raw contents attached to an optional object.
-
.new!(text, tags = [], object = nil, raw_data = nil, ref_object = nil)
Creates a new docstring without performing any parsing through a
DocstringParser
. -
#replace(content, parse = true)
(also: #all=)
Replaces the docstring with new raw content.
- #to_s
Creating and Accessing Meta-data
-
#add_tag(*tags) ⇒ void
Adds a tag or reftag object to the tag list.
-
#blank?(only_visible_tags = true) ⇒ Boolean
Returns true if the docstring has no content that is visible to a template.
-
#delete_tag_if {|tag| ... } ⇒ void
Deletes all tags where the block returns true.
-
#delete_tags(name) ⇒ void
Delete all tags with
name
-
#has_tag?(name) ⇒ Boolean
Returns true if at least one tag by the name
name
was declared. -
#tag(name) ⇒ Tags::Tag
Convenience method to return the first tag object in the list of tag objects of that name.
-
#tags(name = nil) ⇒ Array<Tags::Tag>
Returns a list of tags specified by
name
or all tags ifname
is not specified.
Class Attribute Summary
Class Method Summary
-
.parser(*args) ⇒ DocstringParser
Creates a parser object using the current .default_parser.
Instance Attribute Summary
Instance Method Summary
- #all ⇒ String
- #line ⇒ Fixnum?
-
#resolve_reference ⇒ void
Resolves unresolved other docstring reference if there is unresolved reference.
-
#summary ⇒ String
Gets the first line of a docstring to the period or the first paragraph.
-
#to_raw ⇒ String
Reformats and returns a raw representation of the tag data using the current tag and docstring data, not the original text.
-
#convert_ref_tags ⇒ Array<Tags::RefTag>
private
Maps valid reference tags.
-
#parse_comments(comments) ⇒ String
private
Parses out comments split by newlines into a new code object.
-
#stable_sort_by(list) ⇒ Array
private
A stable sort_by method.
::String
- Inherited
#shell_split | Splits text into tokens the way a shell would, handling quoted text as a single token. |
Constructor Details
.new(content = '', object = nil) ⇒ Docstring
To properly parse directives with proper parser context within
handlers, you should not use this method to create a Docstring
.
Instead, use the .parser, which takes a handler object that
can pass parser state onto directives. If a Docstring
is created
with this method, directives do not have access to any parser
state, and may not function as expected.
Creates a new docstring with the raw contents attached to an optional
object. Parsing will be done by the DocstringParser
class.
Class Attribute Details
.default_parser ⇒ Class
<DocstringParser> (rw)
Plugin developers should make sure to reset this value
after parsing finishes. This can be done via the
Parser::SourceParser.after_parse_list callback. This will
ensure that ::YARD
can properly parse multiple projects in
the same process.
# File 'lib/yard/docstring.rb', line 28
attr_accessor :default_parser
Class Method Details
.new!(text, tags = [], object = nil, raw_data = nil, ref_object = nil)
Creates a new docstring without performing any parsing through
a DocstringParser
. This method is called by DocstringParser
when creating the new docstring object.
# File 'lib/yard/docstring.rb', line 77
def self.new!(text, = [], object = nil, raw_data = nil, ref_object = nil) docstring = allocate docstring.replace(text, false) docstring.object = object docstring.add_tag(* ) docstring.instance_variable_set("@unresolved_reference", ref_object) docstring.instance_variable_set("@all", raw_data) if raw_data docstring end
.parser(*args) ⇒ DocstringParser
Creates a parser object using the current .default_parser. Equivalent to: Docstring.default_parser.new(*args)
# File 'lib/yard/docstring.rb', line 38
def parser(*args) default_parser.new(*args) end
Instance Attribute Details
#hash_flag ⇒ Boolean
(rw)
# File 'lib/yard/docstring.rb', line 56
attr_reader :hash_flag
#hash_flag=(v) (rw)
[ GitHub ]# File 'lib/yard/docstring.rb', line 57
def hash_flag=(v) @hash_flag = v.nil? ? false : v end
#line_range ⇒ Range
(rw)
# File 'lib/yard/docstring.rb', line 50
attr_accessor :line_range
#object ⇒ CodeObjects::Base (rw)
# File 'lib/yard/docstring.rb', line 47
attr_accessor :object
#ref_tags ⇒ Array<Tags::RefTag> (readonly)
# File 'lib/yard/docstring.rb', line 44
attr_reader :
Instance Method Details
#+(other) ⇒ Docstring
Adds another Docstring
, copying over tags.
#add_tag(*tags) ⇒ void
This method returns an undefined value.
Adds a tag or reftag object to the tag list. If you want to parse
tag data based on the Tags::DefaultFactory
tag factory, use
DocstringParser
instead.
#all ⇒ String
# File 'lib/yard/docstring.rb', line 53
attr_reader :all
#all=(content, parse = true)
Alias for #replace.
# File 'lib/yard/docstring.rb', line 144
alias all= replace
#blank?(only_visible_tags = true) ⇒ Boolean
Returns true if the docstring has no content that is visible to a template.
#convert_ref_tags ⇒ Array<Tags::RefTag> (private)
Maps valid reference tags
# File 'lib/yard/docstring.rb', line 344
def list = @ref_tags.reject {|t| CodeObjects::Proxy === t.owner } @ref_tag_recurse_count ||= 0 @ref_tag_recurse_count += 1 if @ref_tag_recurse_count > 2 log.error "#{@object.file}:#{@object.line}: Detected circular reference tag in " \ "`#{@object}', ignoring all reference tags for this object " \ "(#{@ref_tags.map {|t| "@#{t.tag_name}" }.join(", ")})." @ref_tags = [] return @ref_tags end list = list.map(&: ).flatten @ref_tag_recurse_count -= 1 list end
#delete_tag_if {|tag| ... } ⇒ void
This method returns an undefined value.
Deletes all tags where the block returns true
# File 'lib/yard/docstring.rb', line 300
def delete_tag_if(&block) @tags.delete_if(&block) @ref_tags.delete_if(&block) end
#delete_tags(name) ⇒ void
This method returns an undefined value.
Delete all tags with name
# File 'lib/yard/docstring.rb', line 291
def (name) delete_tag_if {|tag| tag.tag_name.to_s == name.to_s } end
#dup ⇒ Docstring
This method creates a new docstring with new tag lists, but does not create new individual tags. Modifying the tag objects will still affect the original tags.
Deep-copies a docstring
# File 'lib/yard/docstring.rb', line 153
def dup resolve_reference obj = super %w(all summary tags ref_tags).each do |name| val = instance_variable_defined?("@#{name}") && instance_variable_get("@#{name}") obj.instance_variable_set("@#{name}", val ? val.dup : nil) end obj end
#has_tag?(name) ⇒ Boolean
Returns true if at least one tag by the name name
was declared
#line ⇒ Fixnum
?
# File 'lib/yard/docstring.rb', line 167
def line line_range ? line_range.first : nil end
#parse_comments(comments) ⇒ String (private)
Parses out comments split by newlines into a new code object
#replace(content, parse = true) Also known as: #all=
Replaces the docstring with new raw content. Called by #all=.
# File 'lib/yard/docstring.rb', line 132
def replace(content, parse = true) content = content.join("\n") if content.is_a?(Array) @tags = [] @ref_tags = [] if parse super(parse_comments(content)) else @all = content @unresolved_reference = nil super(content) end end
#resolve_reference ⇒ void
This method returns an undefined value.
Resolves unresolved other docstring reference if there is unresolved reference. Does nothing if there is no unresolved reference.
Normally, you don't need to call this method explicitly. Resolving unresolved reference is done implicitly.
# File 'lib/yard/docstring.rb', line 328
def resolve_reference loop do return if defined?(@unresolved_reference).nil? || @unresolved_reference.nil? return if CodeObjects::Proxy === @unresolved_reference reference = @unresolved_reference @unresolved_reference = nil self.all = [reference.docstring.all, @all].join("\n") end end
#stable_sort_by(list) ⇒ Array (private)
A stable sort_by method.
#summary ⇒ String
Gets the first line of a docstring to the period or the first paragraph.
# File 'lib/yard/docstring.rb', line 173
def summary resolve_reference return @summary if defined?(@summary) && @summary stripped = gsub(/[\r\n](?![\r\n])/, ' ').strip num_parens = 0 idx = length.times do |index| case stripped[index, 1] when "." next_char = stripped[index + 1, 1].to_s break index - 1 if num_parens <= 0 && next_char =~ /^\s*$/ when "\r", "\n" next_char = stripped[index + 1, 1].to_s if next_char =~ /^\s*$/ break stripped[index - 1, 1] == '.' ? index - 2 : index - 1 end when "{", "(", "[" num_parens += 1 when "}", ")", "]" num_parens -= 1 end end @summary = stripped[0..idx] if !@summary.empty? && @summary !~ /\A\s*\{include:.+\}\s*\Z/ @summary += '.' end @summary end
#tag(name) ⇒ Tags::Tag
Convenience method to return the first tag object in the list of tag objects of that name
#tags(name = nil) ⇒ Array<Tags::Tag>
Returns a list of tags specified by name
or all tags if name
is not specified.
# File 'lib/yard/docstring.rb', line 273
def (name = nil) list = stable_sort_by(@tags + , &:tag_name) return list unless name list.select {|tag| tag.tag_name.to_s == name.to_s } end
#to_raw ⇒ String
Add Tags::Tag#to_raw and refactor
Reformats and returns a raw representation of the tag data using the current tag and docstring data, not the original text.
# File 'lib/yard/docstring.rb', line 207
def to_raw tag_data = .map do |tag| case tag when Tags::OverloadTag tag_text = "@#{tag.tag_name} #{tag.signature}\n" unless tag.docstring.blank? tag_text += "\n " + tag.docstring.all.gsub(/\r?\n/, "\n ") end when Tags::OptionTag tag_text = "@#{tag.tag_name} #{tag.name}" tag_text += ' [' + tag.pair.types.join(', ') + ']' if tag.pair.types tag_text += ' ' + tag.pair.name.to_s if tag.pair.name tag_text += "\n " if tag.name && tag.text tag_text += ' (' + tag.pair.defaults.join(', ') + ')' if tag.pair.defaults tag_text += " " + tag.pair.text.strip.gsub(/\n/, "\n ") if tag.pair.text else tag_text = '@' + tag.tag_name tag_text += ' [' + tag.types.join(', ') + ']' if tag.types tag_text += ' ' + tag.name.to_s if tag.name tag_text += "\n " if tag.name && tag.text tag_text += ' ' + tag.text.strip.gsub(/\n/, "\n ") if tag.text end tag_text end [strip, tag_data.join("\n")].reject(&:empty?).compact.join("\n") end
#to_s
[ GitHub ]# File 'lib/yard/docstring.rb', line 125
def to_s resolve_reference super end