Skip to content

Commit feea50e

Browse files
committed
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.
1 parent f561ca4 commit feea50e

File tree

1 file changed

+45
-39
lines changed
  • src/librustdoc/html/render

1 file changed

+45
-39
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,22 +2413,22 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
24132413
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
24142414
enum ItemSection {
24152415
Reexports,
2416+
PrimitiveTypes,
24162417
Modules,
2418+
Macros,
24172419
Structs,
2418-
Unions,
24192420
Enums,
2420-
Functions,
2421-
TypeDefinitions,
2422-
Statics,
24232421
Constants,
2422+
Statics,
24242423
Traits,
2424+
Functions,
2425+
TypeDefinitions,
2426+
Unions,
24252427
Implementations,
24262428
TypeMethods,
24272429
Methods,
24282430
StructFields,
24292431
Variants,
2430-
Macros,
2431-
PrimitiveTypes,
24322432
AssociatedTypes,
24332433
AssociatedConstants,
24342434
ForeignTypes,
@@ -2440,6 +2440,38 @@ enum ItemSection {
24402440
}
24412441

24422442
impl ItemSection {
2443+
const ALL: &'static [Self] = {
2444+
use ItemSection::*;
2445+
// NOTE: The order here affects the order in the UI.
2446+
&[
2447+
Reexports,
2448+
PrimitiveTypes,
2449+
Modules,
2450+
Macros,
2451+
Structs,
2452+
Enums,
2453+
Constants,
2454+
Statics,
2455+
Traits,
2456+
Functions,
2457+
TypeDefinitions,
2458+
Unions,
2459+
Implementations,
2460+
TypeMethods,
2461+
Methods,
2462+
StructFields,
2463+
Variants,
2464+
AssociatedTypes,
2465+
AssociatedConstants,
2466+
ForeignTypes,
2467+
Keywords,
2468+
OpaqueTypes,
2469+
AttributeMacros,
2470+
DeriveMacros,
2471+
TraitAliases,
2472+
]
2473+
};
2474+
24432475
fn id(self) -> &'static str {
24442476
match self {
24452477
Self::Reexports => "reexports",
@@ -2535,39 +2567,13 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
25352567
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
25362568
let mut sidebar = String::new();
25372569

2538-
let mut already_emitted_sections = FxHashSet::default();
2539-
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
2540-
// to print its headings
2541-
for &myty in &[
2542-
ItemType::Import,
2543-
ItemType::Primitive,
2544-
ItemType::Module,
2545-
ItemType::Macro,
2546-
ItemType::Struct,
2547-
ItemType::Enum,
2548-
ItemType::Constant,
2549-
ItemType::Static,
2550-
ItemType::Trait,
2551-
ItemType::Function,
2552-
ItemType::Typedef,
2553-
ItemType::Union,
2554-
ItemType::Impl,
2555-
ItemType::TyMethod,
2556-
ItemType::Method,
2557-
ItemType::StructField,
2558-
ItemType::Variant,
2559-
ItemType::AssocType,
2560-
ItemType::AssocConst,
2561-
ItemType::ForeignType,
2562-
ItemType::Keyword,
2563-
] {
2564-
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
2565-
let sec = item_ty_to_section(myty);
2566-
if !already_emitted_sections.insert(sec) {
2567-
continue;
2568-
}
2569-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
2570-
}
2570+
let item_sections_in_use: FxHashSet<_> = items
2571+
.iter()
2572+
.filter(|it| !it.is_stripped() && it.name.is_some())
2573+
.map(|it| item_ty_to_section(it.type_()))
2574+
.collect();
2575+
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
2576+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
25712577
}
25722578

25732579
if !sidebar.is_empty() {

0 commit comments

Comments
 (0)