Skip to content

Commit 5343dc7

Browse files
Generate sidebar elements for the "All items" page
1 parent 8fd6d03 commit 5343dc7

File tree

2 files changed

+86
-26
lines changed

2 files changed

+86
-26
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item};
1717
use super::search_index::build_index;
1818
use super::write_shared::write_shared;
1919
use super::{
20-
collect_spans_and_sources, print_sidebar, scrape_examples_help, AllTypes, LinkFromSrc, NameDoc,
21-
StylePath, BASIC_KEYWORDS,
20+
collect_spans_and_sources, print_sidebar, scrape_examples_help, sidebar_module_like, AllTypes,
21+
LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS,
2222
};
2323

2424
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
@@ -597,16 +597,24 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
597597
keywords: BASIC_KEYWORDS,
598598
resource_suffix: &shared.resource_suffix,
599599
};
600-
let sidebar = if shared.cache.crate_version.is_some() {
601-
format!("<h2 class=\"location\">Crate {}</h2>", crate_name)
602-
} else {
603-
String::new()
604-
};
605600
let all = shared.all.replace(AllTypes::new());
601+
let mut sidebar = Buffer::html();
602+
if shared.cache.crate_version.is_some() {
603+
write!(sidebar, "<h2 class=\"location\">Crate {}</h2>", crate_name)
604+
};
605+
606+
let mut items = Buffer::html();
607+
sidebar_module_like(&mut items, all.item_sections());
608+
if !items.is_empty() {
609+
sidebar.push_str("<div class=\"sidebar-elems\">");
610+
sidebar.push_buffer(items);
611+
sidebar.push_str("</div>");
612+
}
613+
606614
let v = layout::render(
607615
&shared.layout,
608616
&page,
609-
sidebar,
617+
sidebar.into_inner(),
610618
|buf: &mut Buffer| all.print(buf),
611619
&shared.style_files,
612620
);

src/librustdoc/html/render/mod.rs

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,56 @@ impl AllTypes {
290290
};
291291
}
292292
}
293-
}
294293

295-
impl AllTypes {
294+
fn item_sections(&self) -> FxHashSet<ItemSection> {
295+
let mut sections = FxHashSet::default();
296+
297+
if !self.structs.is_empty() {
298+
sections.insert(ItemSection::Structs);
299+
}
300+
if !self.enums.is_empty() {
301+
sections.insert(ItemSection::Enums);
302+
}
303+
if !self.unions.is_empty() {
304+
sections.insert(ItemSection::Unions);
305+
}
306+
if !self.primitives.is_empty() {
307+
sections.insert(ItemSection::PrimitiveTypes);
308+
}
309+
if !self.traits.is_empty() {
310+
sections.insert(ItemSection::Traits);
311+
}
312+
if !self.macros.is_empty() {
313+
sections.insert(ItemSection::Macros);
314+
}
315+
if !self.functions.is_empty() {
316+
sections.insert(ItemSection::Functions);
317+
}
318+
if !self.typedefs.is_empty() {
319+
sections.insert(ItemSection::TypeDefinitions);
320+
}
321+
if !self.opaque_tys.is_empty() {
322+
sections.insert(ItemSection::OpaqueTypes);
323+
}
324+
if !self.statics.is_empty() {
325+
sections.insert(ItemSection::Statics);
326+
}
327+
if !self.constants.is_empty() {
328+
sections.insert(ItemSection::Constants);
329+
}
330+
if !self.attributes.is_empty() {
331+
sections.insert(ItemSection::AttributeMacros);
332+
}
333+
if !self.derives.is_empty() {
334+
sections.insert(ItemSection::DeriveMacros);
335+
}
336+
if !self.trait_aliases.is_empty() {
337+
sections.insert(ItemSection::TraitAliases);
338+
}
339+
340+
sections
341+
}
342+
296343
fn print(self, f: &mut Buffer) {
297344
fn print_entries(f: &mut Buffer, e: &FxHashSet<ItemEntry>, title: &str) {
298345
if !e.is_empty() {
@@ -2468,7 +2515,7 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
24682515
}
24692516

24702517
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2471-
enum ItemSection {
2518+
pub(crate) enum ItemSection {
24722519
Reexports,
24732520
PrimitiveTypes,
24742521
Modules,
@@ -2620,25 +2667,11 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
26202667
}
26212668
}
26222669

2623-
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
2670+
pub(crate) fn sidebar_module_like(buf: &mut Buffer, item_sections_in_use: FxHashSet<ItemSection>) {
26242671
use std::fmt::Write as _;
26252672

26262673
let mut sidebar = String::new();
26272674

2628-
let item_sections_in_use: FxHashSet<_> = items
2629-
.iter()
2630-
.filter(|it| {
2631-
!it.is_stripped()
2632-
&& it
2633-
.name
2634-
.or_else(|| {
2635-
if let clean::ImportItem(ref i) = *it.kind &&
2636-
let clean::ImportKind::Simple(s) = i.kind { Some(s) } else { None }
2637-
})
2638-
.is_some()
2639-
})
2640-
.map(|it| item_ty_to_section(it.type_()))
2641-
.collect();
26422675
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
26432676
let _ = write!(sidebar, "<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name());
26442677
}
@@ -2656,6 +2689,25 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
26562689
}
26572690
}
26582691

2692+
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
2693+
let item_sections_in_use: FxHashSet<_> = items
2694+
.iter()
2695+
.filter(|it| {
2696+
!it.is_stripped()
2697+
&& it
2698+
.name
2699+
.or_else(|| {
2700+
if let clean::ImportItem(ref i) = *it.kind &&
2701+
let clean::ImportKind::Simple(s) = i.kind { Some(s) } else { None }
2702+
})
2703+
.is_some()
2704+
})
2705+
.map(|it| item_ty_to_section(it.type_()))
2706+
.collect();
2707+
2708+
sidebar_module_like(buf, item_sections_in_use);
2709+
}
2710+
26592711
fn sidebar_foreign_type(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) {
26602712
let mut sidebar = Buffer::new();
26612713
sidebar_assoc_items(cx, &mut sidebar, it);

0 commit comments

Comments
 (0)