Rollup merge of #93830 - camelid:cleanup-section-code, r=GuillaumeGomez
Refactor sidebar printing code This is the refactoring parts of #92660, plus the trait aliases capitalization consistency fix. I think this will be necessary for #92658. r? `@GuillaumeGomez`
This commit is contained in:
commit
4256165411
3 changed files with 172 additions and 82 deletions
|
@ -2402,33 +2402,156 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
|
|||
}
|
||||
}
|
||||
|
||||
fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
enum ItemSection {
|
||||
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,
|
||||
}
|
||||
|
||||
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",
|
||||
Self::Modules => "modules",
|
||||
Self::Structs => "structs",
|
||||
Self::Unions => "unions",
|
||||
Self::Enums => "enums",
|
||||
Self::Functions => "functions",
|
||||
Self::TypeDefinitions => "types",
|
||||
Self::Statics => "statics",
|
||||
Self::Constants => "constants",
|
||||
Self::Traits => "traits",
|
||||
Self::Implementations => "impls",
|
||||
Self::TypeMethods => "tymethods",
|
||||
Self::Methods => "methods",
|
||||
Self::StructFields => "fields",
|
||||
Self::Variants => "variants",
|
||||
Self::Macros => "macros",
|
||||
Self::PrimitiveTypes => "primitives",
|
||||
Self::AssociatedTypes => "associated-types",
|
||||
Self::AssociatedConstants => "associated-consts",
|
||||
Self::ForeignTypes => "foreign-types",
|
||||
Self::Keywords => "keywords",
|
||||
Self::OpaqueTypes => "opaque-types",
|
||||
Self::AttributeMacros => "attributes",
|
||||
Self::DeriveMacros => "derives",
|
||||
Self::TraitAliases => "trait-aliases",
|
||||
}
|
||||
}
|
||||
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Reexports => "Re-exports",
|
||||
Self::Modules => "Modules",
|
||||
Self::Structs => "Structs",
|
||||
Self::Unions => "Unions",
|
||||
Self::Enums => "Enums",
|
||||
Self::Functions => "Functions",
|
||||
Self::TypeDefinitions => "Type Definitions",
|
||||
Self::Statics => "Statics",
|
||||
Self::Constants => "Constants",
|
||||
Self::Traits => "Traits",
|
||||
Self::Implementations => "Implementations",
|
||||
Self::TypeMethods => "Type Methods",
|
||||
Self::Methods => "Methods",
|
||||
Self::StructFields => "Struct Fields",
|
||||
Self::Variants => "Variants",
|
||||
Self::Macros => "Macros",
|
||||
Self::PrimitiveTypes => "Primitive Types",
|
||||
Self::AssociatedTypes => "Associated Types",
|
||||
Self::AssociatedConstants => "Associated Constants",
|
||||
Self::ForeignTypes => "Foreign Types",
|
||||
Self::Keywords => "Keywords",
|
||||
Self::OpaqueTypes => "Opaque Types",
|
||||
Self::AttributeMacros => "Attribute Macros",
|
||||
Self::DeriveMacros => "Derive Macros",
|
||||
Self::TraitAliases => "Trait Aliases",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn item_ty_to_section(ty: ItemType) -> ItemSection {
|
||||
match ty {
|
||||
ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
|
||||
ItemType::Module => ("modules", "Modules"),
|
||||
ItemType::Struct => ("structs", "Structs"),
|
||||
ItemType::Union => ("unions", "Unions"),
|
||||
ItemType::Enum => ("enums", "Enums"),
|
||||
ItemType::Function => ("functions", "Functions"),
|
||||
ItemType::Typedef => ("types", "Type Definitions"),
|
||||
ItemType::Static => ("statics", "Statics"),
|
||||
ItemType::Constant => ("constants", "Constants"),
|
||||
ItemType::Trait => ("traits", "Traits"),
|
||||
ItemType::Impl => ("impls", "Implementations"),
|
||||
ItemType::TyMethod => ("tymethods", "Type Methods"),
|
||||
ItemType::Method => ("methods", "Methods"),
|
||||
ItemType::StructField => ("fields", "Struct Fields"),
|
||||
ItemType::Variant => ("variants", "Variants"),
|
||||
ItemType::Macro => ("macros", "Macros"),
|
||||
ItemType::Primitive => ("primitives", "Primitive Types"),
|
||||
ItemType::AssocType => ("associated-types", "Associated Types"),
|
||||
ItemType::AssocConst => ("associated-consts", "Associated Constants"),
|
||||
ItemType::ForeignType => ("foreign-types", "Foreign Types"),
|
||||
ItemType::Keyword => ("keywords", "Keywords"),
|
||||
ItemType::OpaqueTy => ("opaque-types", "Opaque Types"),
|
||||
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
|
||||
ItemType::ProcDerive => ("derives", "Derive Macros"),
|
||||
ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
|
||||
ItemType::ExternCrate | ItemType::Import => ItemSection::Reexports,
|
||||
ItemType::Module => ItemSection::Modules,
|
||||
ItemType::Struct => ItemSection::Structs,
|
||||
ItemType::Union => ItemSection::Unions,
|
||||
ItemType::Enum => ItemSection::Enums,
|
||||
ItemType::Function => ItemSection::Functions,
|
||||
ItemType::Typedef => ItemSection::TypeDefinitions,
|
||||
ItemType::Static => ItemSection::Statics,
|
||||
ItemType::Constant => ItemSection::Constants,
|
||||
ItemType::Trait => ItemSection::Traits,
|
||||
ItemType::Impl => ItemSection::Implementations,
|
||||
ItemType::TyMethod => ItemSection::TypeMethods,
|
||||
ItemType::Method => ItemSection::Methods,
|
||||
ItemType::StructField => ItemSection::StructFields,
|
||||
ItemType::Variant => ItemSection::Variants,
|
||||
ItemType::Macro => ItemSection::Macros,
|
||||
ItemType::Primitive => ItemSection::PrimitiveTypes,
|
||||
ItemType::AssocType => ItemSection::AssociatedTypes,
|
||||
ItemType::AssocConst => ItemSection::AssociatedConstants,
|
||||
ItemType::ForeignType => ItemSection::ForeignTypes,
|
||||
ItemType::Keyword => ItemSection::Keywords,
|
||||
ItemType::OpaqueTy => ItemSection::OpaqueTypes,
|
||||
ItemType::ProcAttribute => ItemSection::AttributeMacros,
|
||||
ItemType::ProcDerive => ItemSection::DeriveMacros,
|
||||
ItemType::TraitAlias => ItemSection::TraitAliases,
|
||||
ItemType::Generic => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
@ -2436,44 +2559,13 @@ fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
|
|||
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
|
||||
let mut sidebar = String::new();
|
||||
|
||||
// Re-exports are handled a bit differently because they can be extern crates or imports.
|
||||
if items.iter().any(|it| {
|
||||
it.name.is_some()
|
||||
&& (it.type_() == ItemType::ExternCrate
|
||||
|| (it.type_() == ItemType::Import && !it.is_stripped()))
|
||||
}) {
|
||||
let (id, name) = item_ty_to_strs(ItemType::Import);
|
||||
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
|
||||
}
|
||||
|
||||
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
|
||||
// to print its headings
|
||||
for &myty in &[
|
||||
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 (id, name) = item_ty_to_strs(myty);
|
||||
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, 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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue