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 Feb 9, 2022
1 parent 1115f69 commit 163a800
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
84 changes: 45 additions & 39 deletions src/librustdoc/html/render/mod.rs
Expand Up @@ -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,
Functions,
TypeDefinitions,
Statics,
Constants,
Statics,
Traits,
Functions,
TypeDefinitions,
Unions,
Implementations,
TypeMethods,
Methods,
StructFields,
Variants,
Macros,
PrimitiveTypes,
AssociatedTypes,
AssociatedConstants,
ForeignTypes,
Expand All @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/render/print_item.rs
Expand Up @@ -222,7 +222,9 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
) -> 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);
Expand Down

0 comments on commit 163a800

Please sign in to comment.