From 00d2e9fb3e8cec05f4511b28ff133592f8fad6d1 Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Thu, 19 Sep 2024 13:59:44 -0700 Subject: [PATCH] Fix #1400: Group global constants and functions by file name --- lib/jazzy/doc_index.rb | 18 ++++++++++++++++++ lib/jazzy/grouper.rb | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/jazzy/doc_index.rb b/lib/jazzy/doc_index.rb index 844a578e..75c61b42 100644 --- a/lib/jazzy/doc_index.rb +++ b/lib/jazzy/doc_index.rb @@ -69,6 +69,18 @@ def lookup_path(parts) [self] + (children[parts.first]&.lookup_path(parts[1...]) || []) end + + include Enumerable + + def each(&block) + return to_enum(:each) unless block_given? + + block.call(decl) if decl + + children.each do |index_name, scope| + scope.each(&block) unless index_name.include?('...') + end + end end attr_reader :root_scope @@ -77,6 +89,12 @@ def initialize(all_decls) @root_scope = Scope.new_root(all_decls.group_by(&:module_name)) end + include Enumerable + + def each(&block) + root_scope.each(&block) + end + # Look up a name and return the matching SourceDeclaration or nil. # # `context` is an optional SourceDeclaration indicating where the text diff --git a/lib/jazzy/grouper.rb b/lib/jazzy/grouper.rb index e950b24e..49eeb187 100644 --- a/lib/jazzy/grouper.rb +++ b/lib/jazzy/grouper.rb @@ -69,7 +69,39 @@ def self.group_custom_categories(docs, doc_index) children.each.with_index { |child, i| child.nav_order = i } make_group(children, category['name'], '') end - [group.compact, docs] + + globals_grouped_by_file = self.group_globals_by_file(docs, doc_index) + [group.compact, docs + globals_grouped_by_file] + end + + def self.group_globals_by_file(docs, doc_index) + types_to_files_to_symbols = {} + doc_index.each do |index_item| + if index_item.parent_in_code.nil? && (index_item.type.name == "Constant" || index_item.type.name == "Function") + file_name = File.basename(index_item.file, ".*") + types_to_files_to_symbols[index_item.type.plural_name] ||= {} + types_to_files_to_symbols[index_item.type.plural_name][file_name] ||= [] + + types_to_files_to_symbols[index_item.type.plural_name][file_name] << docs.delete(index_item) + + end + end + + types_to_file_groups = {} + types_to_files_to_symbols.each do |type_name, files| + types_to_file_groups[type_name] ||= [] + files.each do |file, declarations| + file_group = make_group(declarations, file, 'Globally available these types, are.') + file_group.url_name = "#{file}+#{type_name}" + types_to_file_groups[type_name] << file_group + end + end + + type_groups = types_to_file_groups.map do |type_name, files_level_declarations| + make_group(files_level_declarations, type_name, 'These types are available globally.') + end.compact + + type_groups end def self.group_guides(docs, prefix)