Skip to content

Commit

Permalink
Refactor sidebar printing code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Jan 8, 2022
1 parent f561ca4 commit feea50e
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2413,22 +2413,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,
Functions,
TypeDefinitions,
Statics,
Constants,
Statics,
Traits,
Functions,
TypeDefinitions,
Unions,
Implementations,
TypeMethods,
Methods,
StructFields,
Variants,
Macros,
PrimitiveTypes,
AssociatedTypes,
AssociatedConstants,
ForeignTypes,
Expand All @@ -2440,6 +2440,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",
Expand Down Expand Up @@ -2535,39 +2567,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() {
Expand Down

0 comments on commit feea50e

Please sign in to comment.