Skip to content

Commit 4256165

Browse files
authored
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`
2 parents aa20959 + 504f3f0 commit 4256165

File tree

3 files changed

+172
-82
lines changed

3 files changed

+172
-82
lines changed

src/librustdoc/html/render/mod.rs

+156-64
Original file line numberDiff line numberDiff line change
@@ -2402,78 +2402,170 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
24022402
}
24032403
}
24042404

2405-
fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
2405+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2406+
enum ItemSection {
2407+
Reexports,
2408+
PrimitiveTypes,
2409+
Modules,
2410+
Macros,
2411+
Structs,
2412+
Enums,
2413+
Constants,
2414+
Statics,
2415+
Traits,
2416+
Functions,
2417+
TypeDefinitions,
2418+
Unions,
2419+
Implementations,
2420+
TypeMethods,
2421+
Methods,
2422+
StructFields,
2423+
Variants,
2424+
AssociatedTypes,
2425+
AssociatedConstants,
2426+
ForeignTypes,
2427+
Keywords,
2428+
OpaqueTypes,
2429+
AttributeMacros,
2430+
DeriveMacros,
2431+
TraitAliases,
2432+
}
2433+
2434+
impl ItemSection {
2435+
const ALL: &'static [Self] = {
2436+
use ItemSection::*;
2437+
// NOTE: The order here affects the order in the UI.
2438+
&[
2439+
Reexports,
2440+
PrimitiveTypes,
2441+
Modules,
2442+
Macros,
2443+
Structs,
2444+
Enums,
2445+
Constants,
2446+
Statics,
2447+
Traits,
2448+
Functions,
2449+
TypeDefinitions,
2450+
Unions,
2451+
Implementations,
2452+
TypeMethods,
2453+
Methods,
2454+
StructFields,
2455+
Variants,
2456+
AssociatedTypes,
2457+
AssociatedConstants,
2458+
ForeignTypes,
2459+
Keywords,
2460+
OpaqueTypes,
2461+
AttributeMacros,
2462+
DeriveMacros,
2463+
TraitAliases,
2464+
]
2465+
};
2466+
2467+
fn id(self) -> &'static str {
2468+
match self {
2469+
Self::Reexports => "reexports",
2470+
Self::Modules => "modules",
2471+
Self::Structs => "structs",
2472+
Self::Unions => "unions",
2473+
Self::Enums => "enums",
2474+
Self::Functions => "functions",
2475+
Self::TypeDefinitions => "types",
2476+
Self::Statics => "statics",
2477+
Self::Constants => "constants",
2478+
Self::Traits => "traits",
2479+
Self::Implementations => "impls",
2480+
Self::TypeMethods => "tymethods",
2481+
Self::Methods => "methods",
2482+
Self::StructFields => "fields",
2483+
Self::Variants => "variants",
2484+
Self::Macros => "macros",
2485+
Self::PrimitiveTypes => "primitives",
2486+
Self::AssociatedTypes => "associated-types",
2487+
Self::AssociatedConstants => "associated-consts",
2488+
Self::ForeignTypes => "foreign-types",
2489+
Self::Keywords => "keywords",
2490+
Self::OpaqueTypes => "opaque-types",
2491+
Self::AttributeMacros => "attributes",
2492+
Self::DeriveMacros => "derives",
2493+
Self::TraitAliases => "trait-aliases",
2494+
}
2495+
}
2496+
2497+
fn name(self) -> &'static str {
2498+
match self {
2499+
Self::Reexports => "Re-exports",
2500+
Self::Modules => "Modules",
2501+
Self::Structs => "Structs",
2502+
Self::Unions => "Unions",
2503+
Self::Enums => "Enums",
2504+
Self::Functions => "Functions",
2505+
Self::TypeDefinitions => "Type Definitions",
2506+
Self::Statics => "Statics",
2507+
Self::Constants => "Constants",
2508+
Self::Traits => "Traits",
2509+
Self::Implementations => "Implementations",
2510+
Self::TypeMethods => "Type Methods",
2511+
Self::Methods => "Methods",
2512+
Self::StructFields => "Struct Fields",
2513+
Self::Variants => "Variants",
2514+
Self::Macros => "Macros",
2515+
Self::PrimitiveTypes => "Primitive Types",
2516+
Self::AssociatedTypes => "Associated Types",
2517+
Self::AssociatedConstants => "Associated Constants",
2518+
Self::ForeignTypes => "Foreign Types",
2519+
Self::Keywords => "Keywords",
2520+
Self::OpaqueTypes => "Opaque Types",
2521+
Self::AttributeMacros => "Attribute Macros",
2522+
Self::DeriveMacros => "Derive Macros",
2523+
Self::TraitAliases => "Trait Aliases",
2524+
}
2525+
}
2526+
}
2527+
2528+
fn item_ty_to_section(ty: ItemType) -> ItemSection {
24062529
match ty {
2407-
ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
2408-
ItemType::Module => ("modules", "Modules"),
2409-
ItemType::Struct => ("structs", "Structs"),
2410-
ItemType::Union => ("unions", "Unions"),
2411-
ItemType::Enum => ("enums", "Enums"),
2412-
ItemType::Function => ("functions", "Functions"),
2413-
ItemType::Typedef => ("types", "Type Definitions"),
2414-
ItemType::Static => ("statics", "Statics"),
2415-
ItemType::Constant => ("constants", "Constants"),
2416-
ItemType::Trait => ("traits", "Traits"),
2417-
ItemType::Impl => ("impls", "Implementations"),
2418-
ItemType::TyMethod => ("tymethods", "Type Methods"),
2419-
ItemType::Method => ("methods", "Methods"),
2420-
ItemType::StructField => ("fields", "Struct Fields"),
2421-
ItemType::Variant => ("variants", "Variants"),
2422-
ItemType::Macro => ("macros", "Macros"),
2423-
ItemType::Primitive => ("primitives", "Primitive Types"),
2424-
ItemType::AssocType => ("associated-types", "Associated Types"),
2425-
ItemType::AssocConst => ("associated-consts", "Associated Constants"),
2426-
ItemType::ForeignType => ("foreign-types", "Foreign Types"),
2427-
ItemType::Keyword => ("keywords", "Keywords"),
2428-
ItemType::OpaqueTy => ("opaque-types", "Opaque Types"),
2429-
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
2430-
ItemType::ProcDerive => ("derives", "Derive Macros"),
2431-
ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
2530+
ItemType::ExternCrate | ItemType::Import => ItemSection::Reexports,
2531+
ItemType::Module => ItemSection::Modules,
2532+
ItemType::Struct => ItemSection::Structs,
2533+
ItemType::Union => ItemSection::Unions,
2534+
ItemType::Enum => ItemSection::Enums,
2535+
ItemType::Function => ItemSection::Functions,
2536+
ItemType::Typedef => ItemSection::TypeDefinitions,
2537+
ItemType::Static => ItemSection::Statics,
2538+
ItemType::Constant => ItemSection::Constants,
2539+
ItemType::Trait => ItemSection::Traits,
2540+
ItemType::Impl => ItemSection::Implementations,
2541+
ItemType::TyMethod => ItemSection::TypeMethods,
2542+
ItemType::Method => ItemSection::Methods,
2543+
ItemType::StructField => ItemSection::StructFields,
2544+
ItemType::Variant => ItemSection::Variants,
2545+
ItemType::Macro => ItemSection::Macros,
2546+
ItemType::Primitive => ItemSection::PrimitiveTypes,
2547+
ItemType::AssocType => ItemSection::AssociatedTypes,
2548+
ItemType::AssocConst => ItemSection::AssociatedConstants,
2549+
ItemType::ForeignType => ItemSection::ForeignTypes,
2550+
ItemType::Keyword => ItemSection::Keywords,
2551+
ItemType::OpaqueTy => ItemSection::OpaqueTypes,
2552+
ItemType::ProcAttribute => ItemSection::AttributeMacros,
2553+
ItemType::ProcDerive => ItemSection::DeriveMacros,
2554+
ItemType::TraitAlias => ItemSection::TraitAliases,
24322555
ItemType::Generic => unreachable!(),
24332556
}
24342557
}
24352558

24362559
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
24372560
let mut sidebar = String::new();
24382561

2439-
// Re-exports are handled a bit differently because they can be extern crates or imports.
2440-
if items.iter().any(|it| {
2441-
it.name.is_some()
2442-
&& (it.type_() == ItemType::ExternCrate
2443-
|| (it.type_() == ItemType::Import && !it.is_stripped()))
2444-
}) {
2445-
let (id, name) = item_ty_to_strs(ItemType::Import);
2446-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
2447-
}
2448-
2449-
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
2450-
// to print its headings
2451-
for &myty in &[
2452-
ItemType::Primitive,
2453-
ItemType::Module,
2454-
ItemType::Macro,
2455-
ItemType::Struct,
2456-
ItemType::Enum,
2457-
ItemType::Constant,
2458-
ItemType::Static,
2459-
ItemType::Trait,
2460-
ItemType::Function,
2461-
ItemType::Typedef,
2462-
ItemType::Union,
2463-
ItemType::Impl,
2464-
ItemType::TyMethod,
2465-
ItemType::Method,
2466-
ItemType::StructField,
2467-
ItemType::Variant,
2468-
ItemType::AssocType,
2469-
ItemType::AssocConst,
2470-
ItemType::ForeignType,
2471-
ItemType::Keyword,
2472-
] {
2473-
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
2474-
let (id, name) = item_ty_to_strs(myty);
2475-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
2476-
}
2562+
let item_sections_in_use: FxHashSet<_> = items
2563+
.iter()
2564+
.filter(|it| !it.is_stripped() && it.name.is_some())
2565+
.map(|it| item_ty_to_section(it.type_()))
2566+
.collect();
2567+
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
2568+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
24772569
}
24782570

24792571
if !sidebar.is_empty() {

src/librustdoc/html/render/print_item.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use rustc_span::symbol::{kw, sym, Symbol};
1616
use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
1717

1818
use super::{
19-
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
20-
render_assoc_item, render_assoc_items, render_attributes_in_code, render_attributes_in_pre,
21-
render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context,
22-
ImplRenderingParameters,
19+
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
20+
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
21+
render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
22+
AssocItemLink, Context, ImplRenderingParameters,
2323
};
2424
use crate::clean;
2525
use crate::formats::item_type::ItemType;
@@ -221,7 +221,9 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
221221
) -> Ordering {
222222
let ty1 = i1.type_();
223223
let ty2 = i2.type_();
224-
if ty1 != ty2 {
224+
if item_ty_to_section(ty1) != item_ty_to_section(ty2)
225+
|| (ty1 != ty2 && (ty1 == ItemType::ExternCrate || ty2 == ItemType::ExternCrate))
226+
{
225227
return (reorder(ty1), idx1).cmp(&(reorder(ty2), idx2));
226228
}
227229
let s1 = i1.stability(tcx).as_ref().map(|s| s.level);
@@ -270,32 +272,28 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
270272
});
271273

272274
debug!("{:?}", indices);
273-
let mut curty = None;
275+
let mut last_section = None;
274276

275277
for &idx in &indices {
276278
let myitem = &items[idx];
277279
if myitem.is_stripped() {
278280
continue;
279281
}
280282

281-
let myty = Some(myitem.type_());
282-
if curty == Some(ItemType::ExternCrate) && myty == Some(ItemType::Import) {
283-
// Put `extern crate` and `use` re-exports in the same section.
284-
curty = myty;
285-
} else if myty != curty {
286-
if curty.is_some() {
283+
let my_section = item_ty_to_section(myitem.type_());
284+
if Some(my_section) != last_section {
285+
if last_section.is_some() {
287286
w.write_str(ITEM_TABLE_CLOSE);
288287
}
289-
curty = myty;
290-
let (short, name) = item_ty_to_strs(myty.unwrap());
288+
last_section = Some(my_section);
291289
write!(
292290
w,
293291
"<h2 id=\"{id}\" class=\"small-section-header\">\
294292
<a href=\"#{id}\">{name}</a>\
295293
</h2>\n{}",
296294
ITEM_TABLE_OPEN,
297-
id = cx.derive_id(short.to_owned()),
298-
name = name
295+
id = cx.derive_id(my_section.id().to_owned()),
296+
name = my_section.name(),
299297
);
300298
}
301299

@@ -407,7 +405,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
407405
}
408406
}
409407

410-
if curty.is_some() {
408+
if last_section.is_some() {
411409
w.write_str(ITEM_TABLE_CLOSE);
412410
}
413411
}

src/test/rustdoc/trait_alias.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt::Debug;
88
// @has foo/all.html '//a[@href="traitalias.Alias2.html"]' 'Alias2'
99
// @has foo/all.html '//a[@href="traitalias.Foo.html"]' 'Foo'
1010

11-
// @has foo/index.html '//h2[@id="trait-aliases"]' 'Trait aliases'
11+
// @has foo/index.html '//h2[@id="trait-aliases"]' 'Trait Aliases'
1212
// @has foo/index.html '//a[@class="traitalias"]' 'CopyAlias'
1313
// @has foo/index.html '//a[@class="traitalias"]' 'Alias2'
1414
// @has foo/index.html '//a[@class="traitalias"]' 'Foo'

0 commit comments

Comments
 (0)