Skip to content

Commit

Permalink
rustdoc: add separate section for module items
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Aug 20, 2024
1 parent 5a6054b commit 68773c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ impl ModuleLike {
matches!(self, ModuleLike::Crate)
}
}
impl<'a> From<&'a clean::Item> for ModuleLike {
fn from(it: &'a clean::Item) -> ModuleLike {
if it.is_crate() { ModuleLike::Crate } else { ModuleLike::Module }
}
}

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

fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static> {
fn sidebar_module(
items: &[clean::Item],
ids: &mut IdMap,
module_like: ModuleLike,
) -> LinkBlock<'static> {
let item_sections_in_use: FxHashSet<_> = items
.iter()
.filter(|it| {
Expand All @@ -582,7 +593,7 @@ fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static>
.map(|it| item_ty_to_section(it.type_()))
.collect();

sidebar_module_like(item_sections_in_use, ids, ModuleLike::Module)
sidebar_module_like(item_sections_in_use, ids, module_like)
}

fn sidebar_foreign_type<'a>(
Expand Down
16 changes: 16 additions & 0 deletions tests/rustdoc/sidebar/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![crate_name = "foo"]

//@ has 'foo/index.html'
//@ has - '//section[@id="TOC"]/h3' 'Crate Items'

//@ has 'foo/bar/index.html'
//@ has - '//section[@id="TOC"]/h3' 'Module Items'
pub mod bar {
//@ has 'foo/bar/struct.Baz.html'
//@ !has - '//section[@id="TOC"]/h3' 'Module Items'
pub struct Baz;
}

//@ has 'foo/baz/index.html'
//@ !has - '//section[@id="TOC"]/h3' 'Module Items'
pub mod baz {}
1 change: 1 addition & 0 deletions tests/rustdoc/sidebar/top-toc-html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// @has foo/index.html
// User header
// @has - '//section[@id="TOC"]/h3' 'Sections'
// @has - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]' 'Basic link and emphasis'
// @count - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]/em' 0
// @count - '//section[@id="TOC"]/ul[@class="block top-toc"]/li/a[@href="#basic-link-and-emphasis"]/a' 0

0 comments on commit 68773c7

Please sign in to comment.