Skip to content

rustdoc: add header map to the table of contents #120736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rustdoc: add separate section for module items
  • Loading branch information
notriddle committed Aug 20, 2024
commit 68773c789a6a84a888b69f7287fd294b6dd3625c
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added the test for the two headings, awesome!


//@ 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