From 163a8004ae61e0c1d35f6411d65868bc03556bf9 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 7 Jan 2022 16:30:01 -0800 Subject: [PATCH] Refactor sidebar printing code The new code is much simpler and easier to understand. In fact, the old code actually had a subtle bug where it excluded a few item types, including trait aliases, from the sidebar, even though they are rendered on the page itself! Now, all sections should show up in the sidebar. --- src/librustdoc/html/render/mod.rs | 84 +++++++++++++----------- src/librustdoc/html/render/print_item.rs | 4 +- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 279047a2d1c..6dd730c3f88 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2412,22 +2412,22 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean: #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] enum ItemSection { Reexports, + PrimitiveTypes, Modules, + Macros, Structs, - Unions, Enums, + Constants, + Statics, + Traits, Functions, TypeDefinitions, - Statics, - Constants, - Traits, + Unions, Implementations, TypeMethods, Methods, StructFields, Variants, - Macros, - PrimitiveTypes, AssociatedTypes, AssociatedConstants, ForeignTypes, @@ -2439,6 +2439,38 @@ enum ItemSection { } impl ItemSection { + const ALL: &'static [Self] = { + use ItemSection::*; + // NOTE: The order here affects the order in the UI. + &[ + Reexports, + PrimitiveTypes, + Modules, + Macros, + Structs, + Enums, + Constants, + Statics, + Traits, + Functions, + TypeDefinitions, + Unions, + Implementations, + TypeMethods, + Methods, + StructFields, + Variants, + AssociatedTypes, + AssociatedConstants, + ForeignTypes, + Keywords, + OpaqueTypes, + AttributeMacros, + DeriveMacros, + TraitAliases, + ] + }; + fn id(self) -> &'static str { match self { Self::Reexports => "reexports", @@ -2534,39 +2566,13 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection { fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { let mut sidebar = String::new(); - let mut already_emitted_sections = FxHashSet::default(); - // ordering taken from item_module, reorder, where it prioritized elements in a certain order - // to print its headings - for &myty in &[ - ItemType::Import, - ItemType::Primitive, - ItemType::Module, - ItemType::Macro, - ItemType::Struct, - ItemType::Enum, - ItemType::Constant, - ItemType::Static, - ItemType::Trait, - ItemType::Function, - ItemType::Typedef, - ItemType::Union, - ItemType::Impl, - ItemType::TyMethod, - ItemType::Method, - ItemType::StructField, - ItemType::Variant, - ItemType::AssocType, - ItemType::AssocConst, - ItemType::ForeignType, - ItemType::Keyword, - ] { - if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) { - let sec = item_ty_to_section(myty); - if !already_emitted_sections.insert(sec) { - continue; - } - sidebar.push_str(&format!("
  • {}
  • ", sec.id(), sec.name())); - } + let item_sections_in_use: FxHashSet<_> = items + .iter() + .filter(|it| !it.is_stripped() && it.name.is_some()) + .map(|it| item_ty_to_section(it.type_())) + .collect(); + for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) { + sidebar.push_str(&format!("
  • {}
  • ", sec.id(), sec.name())); } if !sidebar.is_empty() { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index e781e3c1ef4..6e466ae8218 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -222,7 +222,9 @@ fn cmp( ) -> Ordering { let ty1 = i1.type_(); let ty2 = i2.type_(); - if ty1 != ty2 { + if item_ty_to_section(ty1) != item_ty_to_section(ty2) + || (ty1 != ty2 && (ty1 == ItemType::ExternCrate || ty2 == ItemType::ExternCrate)) + { return (reorder(ty1), idx1).cmp(&(reorder(ty2), idx2)); } let s1 = i1.stability(tcx).as_ref().map(|s| s.level);