Skip to content

Commit 5c6e614

Browse files
committed
Docstring preserves order of tags
combines @tags and @ref_tags into one array, @all_tags, from which #tags is built
1 parent a001d36 commit 5c6e614

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

lib/yard/docstring.rb

+27-19
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ def parser(*args) default_parser.new(*args) end
4040

4141
self.default_parser = DocstringParser
4242

43-
# @return [Array<Tags::RefTag>] the list of reference tags
44-
attr_reader :ref_tags
45-
4643
# @return [CodeObjects::Base] the object that owns the docstring.
4744
attr_accessor :object
4845

@@ -131,8 +128,7 @@ def to_s
131128
# @param [String] content the raw comments to be parsed
132129
def replace(content, parse = true)
133130
content = content.join("\n") if content.is_a?(Array)
134-
@tags = []
135-
@ref_tags = []
131+
@all_tags = []
136132
if parse
137133
super(parse_comments(content))
138134
else
@@ -153,7 +149,7 @@ def replace(content, parse = true)
153149
def dup
154150
resolve_reference
155151
obj = super
156-
%w(all summary tags ref_tags).each do |name|
152+
%w(all summary all_tags).each do |name|
157153
val = instance_variable_defined?("@#{name}") && instance_variable_get("@#{name}")
158154
obj.instance_variable_set("@#{name}", val ? val.dup : nil)
159155
end
@@ -244,9 +240,9 @@ def add_tag(*tags)
244240
case tag
245241
when Tags::Tag
246242
tag.object = object
247-
@tags << tag
248-
when Tags::RefTag, Tags::RefTagList
249-
@ref_tags << tag
243+
@all_tags << tag
244+
when Tags::RefTagList
245+
@all_tags << tag
250246
else
251247
raise ArgumentError, "expected Tag or RefTag, got #{tag.class} (at index #{i})"
252248
end
@@ -271,11 +267,17 @@ def tag(name)
271267
# @param [#to_s] name the tag name to return data for, or nil for all tags
272268
# @return [Array<Tags::Tag>] the list of tags by the specified tag name
273269
def tags(name = nil)
274-
list = stable_sort_by(@tags + convert_ref_tags, &:tag_name)
270+
list = @all_tags.map { |tag| convert_ref_tag(tag) }.flatten
271+
list = stable_sort_by(list, &:tag_name)
275272
return list unless name
276273
list.select {|tag| tag.tag_name.to_s == name.to_s }
277274
end
278275

276+
# @return [Array<Tags::RefTag, Tags::RefTagList>] the list of reference tags
277+
def ref_tags
278+
@all_tags.select { |tag| tag.is_a?(Tags::RefTag) || tag.is_a?(Tags::RefTagList) }
279+
end
280+
279281
# Returns true if at least one tag by the name +name+ was declared
280282
#
281283
# @param [String] name the tag name to search for
@@ -298,8 +300,7 @@ def delete_tags(name)
298300
# @return [void]
299301
# @since 0.7.0
300302
def delete_tag_if(&block)
301-
@tags.delete_if(&block)
302-
@ref_tags.delete_if(&block)
303+
@all_tags.delete_if(&block)
303304
end
304305

305306
# Returns true if the docstring has no content that is visible to a template.
@@ -311,7 +312,7 @@ def blank?(only_visible_tags = true)
311312
if only_visible_tags
312313
empty? && !tags.any? {|tag| Tags::Library.visible_tags.include?(tag.tag_name.to_sym) }
313314
else
314-
empty? && @tags.empty? && @ref_tags.empty?
315+
empty? && @all_tags.empty?
315316
end
316317
end
317318

@@ -340,20 +341,27 @@ def resolve_reference
340341

341342
# Maps valid reference tags
342343
#
343-
# @return [Array<Tags::RefTag>] the list of valid reference tags
344-
def convert_ref_tags
345-
list = @ref_tags.reject {|t| CodeObjects::Proxy === t.owner }
346-
344+
# @param tag [Tags::Tag, Tags::RefTagList]
345+
# @return [Array<Tags::Tag>] dereferenced tags
346+
def convert_ref_tag(tag)
347347
@ref_tag_recurse_count ||= 0
348348
@ref_tag_recurse_count += 1
349349
if @ref_tag_recurse_count > 2
350350
log.error "#{@object.file}:#{@object.line}: Detected circular reference tag in " \
351351
"`#{@object}', ignoring all reference tags for this object " \
352-
"(#{@ref_tags.map {|t| "@#{t.tag_name}" }.join(", ")})."
352+
"(@#{tag.tag_name})."
353353
@ref_tags = []
354354
return @ref_tags
355355
end
356-
list = list.map(&:tags).flatten
356+
if tag.is_a?(Tags::RefTagList)
357+
if CodeObjects::Proxy === tag.owner
358+
list = []
359+
else
360+
list = tag.tags
361+
end
362+
else
363+
list = [tag]
364+
end
357365
@ref_tag_recurse_count -= 1
358366
list
359367
end

0 commit comments

Comments
 (0)