Skip to content

Commit

Permalink
Display class ancestors in the sidebar #1183
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisbernard authored Nov 4, 2024
1 parent d5c6eca commit 50dda13
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 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
18 changes: 18 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,22 @@ def excerpt(content)

extracted_text[0...150].gsub(/\n/, " ").squeeze(" ")
end

def generate_ancestor_list(ancestors, klass)
return '' if ancestors.empty?

ancestor = ancestors.shift
content = +'<ul><li>'

if ancestor.is_a?(RDoc::NormalClass)
content << "<a href=\"#{klass.aref_to ancestor.path}\">#{ancestor.full_name}</a>"
else
content << ancestor.to_s
end

# Recursively call the method for the remaining ancestors
content << generate_ancestor_list(ancestors, klass)

content << '</li></ul>'
end
end
11 changes: 3 additions & 8 deletions lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<%- if klass.type == 'class' then %>
<%- if klass.type == 'class' && (ancestors = klass.super_classes).any? -%>
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>

<%- 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 -%>
<h3>Ancestors</h3>
<%= generate_ancestor_list(ancestors, klass) %>
</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 50dda13

Please sign in to comment.