Skip to content

Commit 6f91b8d

Browse files
committed
rustdoc: add separate section for module items
1 parent 5bd0812 commit 6f91b8d

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/librustdoc/html/render/sidebar.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ impl ModuleLike {
2626
matches!(self, ModuleLike::Crate)
2727
}
2828
}
29+
impl<'a> From<&'a clean::Item> for ModuleLike {
30+
fn from(it: &'a clean::Item) -> ModuleLike {
31+
if it.is_crate() { ModuleLike::Crate } else { ModuleLike::Module }
32+
}
33+
}
2934

3035
#[derive(Template)]
3136
#[template(path = "sidebar.html")]
@@ -119,7 +124,9 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
119124
clean::UnionItem(ref u) => sidebar_union(cx, it, u, &mut blocks),
120125
clean::EnumItem(ref e) => sidebar_enum(cx, it, e, &mut blocks),
121126
clean::TypeAliasItem(ref t) => sidebar_type_alias(cx, it, t, &mut blocks),
122-
clean::ModuleItem(ref m) => blocks.push(sidebar_module(&m.items, &mut ids)),
127+
clean::ModuleItem(ref m) => {
128+
blocks.push(sidebar_module(&m.items, &mut ids, ModuleLike::from(it)))
129+
}
123130
clean::ForeignTypeItem => sidebar_foreign_type(cx, it, &mut blocks),
124131
_ => {}
125132
}
@@ -561,7 +568,11 @@ pub(crate) fn sidebar_module_like(
561568
LinkBlock::new(header, "", item_sections)
562569
}
563570

564-
fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static> {
571+
fn sidebar_module(
572+
items: &[clean::Item],
573+
ids: &mut IdMap,
574+
module_like: ModuleLike,
575+
) -> LinkBlock<'static> {
565576
let item_sections_in_use: FxHashSet<_> = items
566577
.iter()
567578
.filter(|it| {
@@ -582,7 +593,7 @@ fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static>
582593
.map(|it| item_ty_to_section(it.type_()))
583594
.collect();
584595

585-
sidebar_module_like(item_sections_in_use, ids, ModuleLike::Module)
596+
sidebar_module_like(item_sections_in_use, ids, module_like)
586597
}
587598

588599
fn sidebar_foreign_type<'a>(

tests/rustdoc/sidebar/module.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_name = "foo"]
2+
3+
//@ has 'foo/index.html'
4+
//@ has - '//section[@id="TOC"]/h3' 'Crate Items'
5+
6+
//@ has 'foo/bar/index.html'
7+
//@ has - '//section[@id="TOC"]/h3' 'Module Items'
8+
pub mod bar {
9+
//@ has 'foo/bar/struct.Baz.html'
10+
//@ !has - '//section[@id="TOC"]/h3' 'Module Items'
11+
pub struct Baz;
12+
}
13+
14+
//@ has 'foo/baz/index.html'
15+
//@ !has - '//section[@id="TOC"]/h3' 'Module Items'
16+
pub mod baz {}

tests/rustdoc/sidebar/top-toc-html.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
// @has foo/index.html
1616
// User header
17+
// @has - '//section[@id="TOC"]/h3' 'Sections'
1718
// @has - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]' 'Basic link and emphasis'
1819
// @count - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]/em' 0
1920
// @count - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]/a' 0

0 commit comments

Comments
 (0)