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.
This commit is contained in:
Noah Lev 2022-01-07 16:30:01 -08:00
parent 1115f69bf4
commit 163a8004ae
2 changed files with 48 additions and 40 deletions

View File

@ -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!("<li><a href=\"#{}\">{}</a></li>", 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!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
}
if !sidebar.is_empty() {

View File

@ -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);