From f00c6d6d277513d5bdf4c8f3b24d574666032058 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Mon, 25 Sep 2023 15:59:26 -0500 Subject: [PATCH] Fix breadcrumbs for flattened class declarations When declaring a class with a flattened name like `class Foo::Bar` rather than `module Foo; class Bar`, RDoc does not return a parent object for `Bar`. Note this only applies to classes. When `Bar` is merely a module, RDoc _does_ return a parent object. That behavior caused `module_breadcrumbs` to return only the class's name in such cases. This commit fixes `module_breadcrumbs` to work around that behavior and always return all breadcrumbs. --- lib/sdoc/helpers.rb | 10 +++++----- spec/helpers_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/sdoc/helpers.rb b/lib/sdoc/helpers.rb index 44b0f797..42c95693 100644 --- a/lib/sdoc/helpers.rb +++ b/lib/sdoc/helpers.rb @@ -120,14 +120,14 @@ def more_less_ul(items, limit) end def module_breadcrumbs(rdoc_module) - crumbs = [h(rdoc_module.name)] + parent_names = rdoc_module.full_name.split("::")[0...-1] - rdoc_module.each_parent do |parent| - break if parent.is_a?(RDoc::TopLevel) - crumbs.unshift(link_to(h(parent.name), parent)) + crumbs = parent_names.each_with_index.map do |name, i| + parent = rdoc_module.store.find_module_named(parent_names[0..i].join("::")) + parent ? link_to(h(name), parent) : h(name) end - "#{crumbs.join("::")}" + "#{[*crumbs, h(rdoc_module.name)].join("::")}" end def module_ancestors(rdoc_module) diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 0f590880..7539f0f0 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -547,6 +547,19 @@ module Foo; module Bar; module Qux; end; end; end _(@helpers.module_breadcrumbs(qux)). must_equal "#{@helpers.link_to "Foo", foo}::#{@helpers.link_to "Bar", bar}::Qux" end + + it "handles flattened class declarations" do + top_level = rdoc_top_level_for <<~RUBY + class Foo::Bar::Qux; end + RUBY + + foo = top_level.find_module_named("Foo") + bar = top_level.find_module_named("Foo::Bar") + qux = top_level.find_module_named("Foo::Bar::Qux") + + _(@helpers.module_breadcrumbs(qux)). + must_equal "#{@helpers.link_to "Foo", foo}::#{@helpers.link_to "Bar", bar}::Qux" + end end describe "#module_ancestors" do