Skip to content

Commit

Permalink
List all class ancestors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisbernard committed Oct 31, 2024
1 parent 7fb0e50 commit a7331c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/rdoc/code_object/class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,20 @@ def superclass=(superclass)
@superclass = superclass
end

##
# Get all super classes of this class in an array. The last element might be
# a string if the name is unknown.

def super_classes
result = []
parent = self
while parent = parent.superclass
result << parent
return result if parent.is_a?(String)
end
result
end

def to_s # :nodoc:
if is_alias_for then
"#{self.class.name} #{self.full_name} -> #{is_alias_for}"
Expand Down
45 changes: 38 additions & 7 deletions lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
<%- if klass.type == 'class' then %>
<%- if klass.type == 'class' && (ancestors = klass.super_classes).any? then %>
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<h3>Ancestors</h3>
<% if ancestors.size > 1 %>
<% parent = ancestors.shift %>
<ul>
<li>
<details>
<summary>
<% if parent.is_a?(RDoc::NormalClass) %>
<a href="<%= klass.aref_to parent.path -%>"><%= parent.full_name -%></a>
<% else %>
<%= parent -%>
<% end %>
</summary>

<%- if klass.superclass and not String === klass.superclass then -%>
<p class="link"><a href="<%= klass.aref_to klass.superclass.path %>"><%= klass.superclass.full_name %></a>
<%- else -%>
<p class="link"><%= klass.superclass %>
<%- end -%>
<% for ancestor in ancestors %>
<ul>
<li>
<% if ancestor.is_a?(RDoc::NormalClass) %>
<a href="<%= klass.aref_to ancestor.path -%>"><%= ancestor.full_name -%></a>
<% else %>
<%= ancestor -%>
<% end %>
<% end %>
<% for ancestor in ancestors # Sorry, templates cannot be recursive %>
</li>
</ul>
<% end %>
</details>
</li>
</ul>
<% else %>
<%- if klass.superclass and not String === klass.superclass then -%>
<p class="link"><a href="<%= klass.aref_to klass.superclass.path %>"><%= klass.superclass.full_name %></a>
<%- else -%>
<p class="link"><%= klass.superclass %>
<%- end -%>
<% end %>
</div>
<%- end -%>
6 changes: 6 additions & 0 deletions test/rdoc/test_rdoc_class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,12 @@ def test_superclass
assert_equal @c3_h1, @c3_h2.superclass
end

def test_super_classes
rdoc_c3_h1 = @xref_data.find_module_named('C3::H1')
rdoc_object = @xref_data.find_module_named('Object')
assert_equal [rdoc_c3_h1, rdoc_object, "BasicObject"], @c3_h2.super_classes
end

def test_update_aliases_class
n1 = @xref_data.add_module RDoc::NormalClass, 'N1'
n1_k2 = n1.add_module RDoc::NormalClass, 'N2'
Expand Down

0 comments on commit a7331c8

Please sign in to comment.