Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display class ancestors in the sidebar #1183

Merged

Conversation

alexisbernard
Copy link
Contributor

The purpose is to list all class ancestors in a new theme (see PR #1182).

@alexisbernard
Copy link
Contributor Author

I believe the right name is super_classes but it is inconsistent with superclass. However Method#super_method exists. Let me know if you have a preference.

@alexisbernard alexisbernard force-pushed the add_method_super_classes_to_class_module branch from c88c36e to a7331c8 Compare October 31, 2024 14:13
@alexisbernard
Copy link
Contributor Author

I added the template part to list all ancestors where only the direct parent is displayed by default.

The triangle would be better right aligned to prevent from miss clicks. That could be done in another PR.

Capture d’écran du 2024-10-31 15-24-58

@st0012 st0012 changed the title Add method RDoc::ClassModule#super_classes Display class ancestors in the sidebar Oct 31, 2024
@st0012 st0012 added this to the v6.8.0 milestone Oct 31, 2024
@st0012
Copy link
Member

st0012 commented Nov 2, 2024

Thanks for the PR. I think this is a great addition to the theme's functionalities.
I'd like to propose a simplification on the implementation though:

diff --git a/lib/rdoc/generator/darkfish.rb b/lib/rdoc/generator/darkfish.rb
index 5709fabf..cdf7fdd2 100644
--- a/lib/rdoc/generator/darkfish.rb
+++ b/lib/rdoc/generator/darkfish.rb
@@ -795,4 +795,23 @@ class RDoc::Generator::Darkfish
 
     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>'
+    content
+  end
 end
diff --git a/lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml b/lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml
index 7bd1a7ca..6808b2bf 100644
--- a/lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml
+++ b/lib/rdoc/generator/template/darkfish/_sidebar_parent.rhtml
@@ -1,42 +1,6 @@
-<%- if klass.type == 'class' && (ancestors = klass.super_classes).any? then %>
+<%- if klass.type == 'class' && (ancestors = klass.super_classes).any? -%>
 <div id="parent-class-section" class="nav-section">
   <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>
-
-          <% 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 %>
+  <%= generate_ancestor_list(ancestors, klass) %>
 </div>
 <%- end -%>

This will remove the expanding toggle, which IMO isn't necessary as expanded list will make the ancestors list instantly understandable. We can make it folded later based on user feedback.

@alexisbernard alexisbernard force-pushed the add_method_super_classes_to_class_module branch from a7331c8 to efb9356 Compare November 4, 2024 16:42
@alexisbernard
Copy link
Contributor Author

I applied the patch and I removed the last line of generate_ancestor_list since content is already implicitly returned.

I also expanded the tree by default when I started building this feature. But after using it, it felt too repetitive because in most cases ancestors are only Object and BasicObject. Sure, let's adjust according to other user's feedback.

@st0012
Copy link
Member

st0012 commented Nov 4, 2024

it felt too repetitive because in most cases ancestors are only Object and BasicObject.

To be honest, I feel the same. But my thinking is that if we only display it when it's patched (in the context of gems), it may be confusing to people new to Ruby as the timing to have parent classes will vary in an implicit way.

Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing, thank you!

@st0012 st0012 merged commit 50dda13 into ruby:master Nov 4, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants