Skip to content

Commit 163a800

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 1115f69 commit 163a800

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,22 +2412,22 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
24122412
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
24132413
enum ItemSection {
24142414
Reexports,
2415+
PrimitiveTypes,
24152416
Modules,
2417+
Macros,
24162418
Structs,
2417-
Unions,
24182419
Enums,
2419-
Functions,
2420-
TypeDefinitions,
2421-
Statics,
24222420
Constants,
2421+
Statics,
24232422
Traits,
2423+
Functions,
2424+
TypeDefinitions,
2425+
Unions,
24242426
Implementations,
24252427
TypeMethods,
24262428
Methods,
24272429
StructFields,
24282430
Variants,
2429-
Macros,
2430-
PrimitiveTypes,
24312431
AssociatedTypes,
24322432
AssociatedConstants,
24332433
ForeignTypes,
@@ -2439,6 +2439,38 @@ enum ItemSection {
24392439
}
24402440

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

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

25722578
if !sidebar.is_empty() {

src/librustdoc/html/render/print_item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
222222
) -> Ordering {
223223
let ty1 = i1.type_();
224224
let ty2 = i2.type_();
225-
if ty1 != ty2 {
225+
if item_ty_to_section(ty1) != item_ty_to_section(ty2)
226+
|| (ty1 != ty2 && (ty1 == ItemType::ExternCrate || ty2 == ItemType::ExternCrate))
227+
{
226228
return (reorder(ty1), idx1).cmp(&(reorder(ty2), idx2));
227229
}
228230
let s1 = i1.stability(tcx).as_ref().map(|s| s.level);

0 commit comments

Comments
 (0)