Skip to content

Commit 5a6054b

Browse files
committed
rustdoc: add separate section for module items
1 parent a7aea5d commit 5a6054b

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ use rustc_span::edition::Edition;
1414
use rustc_span::{sym, FileName, Symbol};
1515

1616
use super::print_item::{full_path, item_path, print_item};
17-
use super::sidebar::{print_sidebar, sidebar_module_like, Sidebar};
1817
use super::write_shared::write_shared;
19-
use super::{collect_spans_and_sources, scrape_examples_help, AllTypes, LinkFromSrc, StylePath};
18+
use super::{
19+
collect_spans_and_sources, scrape_examples_help,
20+
sidebar::{print_sidebar, sidebar_module_like, ModuleLike, Sidebar},
21+
AllTypes, LinkFromSrc, StylePath,
22+
};
2023
use crate::clean::types::ExternalLocation;
2124
use crate::clean::utils::has_doc_flag;
2225
use crate::clean::{self, ExternalCrate};
@@ -617,7 +620,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
617620
let mut sidebar = Buffer::html();
618621

619622
// all.html is not customizable, so a blank id map is fine
620-
let blocks = sidebar_module_like(all.item_sections(), &mut IdMap::new());
623+
let blocks = sidebar_module_like(all.item_sections(), &mut IdMap::new(), ModuleLike::Crate);
621624
let bar = Sidebar {
622625
title_prefix: "",
623626
title: "",

src/librustdoc/html/render/sidebar.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ use crate::{
1515

1616
use super::{item_ty_to_section, Context, ItemSection};
1717

18+
#[derive(Clone, Copy)]
19+
pub(crate) enum ModuleLike {
20+
Module,
21+
Crate,
22+
}
23+
24+
impl ModuleLike {
25+
pub(crate) fn is_crate(self) -> bool {
26+
matches!(self, ModuleLike::Crate)
27+
}
28+
}
29+
1830
#[derive(Template)]
1931
#[template(path = "sidebar.html")]
2032
pub(super) struct Sidebar<'a> {
@@ -530,14 +542,23 @@ fn sidebar_enum<'a>(
530542
pub(crate) fn sidebar_module_like(
531543
item_sections_in_use: FxHashSet<ItemSection>,
532544
ids: &mut IdMap,
545+
module_like: ModuleLike,
533546
) -> LinkBlock<'static> {
534-
let item_sections = ItemSection::ALL
547+
let item_sections: Vec<Link<'_>> = ItemSection::ALL
535548
.iter()
536549
.copied()
537550
.filter(|sec| item_sections_in_use.contains(sec))
538551
.map(|sec| Link::new(ids.derive(sec.id()), sec.name()))
539552
.collect();
540-
LinkBlock::new(Link::empty(), "", item_sections)
553+
let header = if let Some(first_section) = item_sections.get(0) {
554+
Link::new(
555+
first_section.href.to_owned(),
556+
if module_like.is_crate() { "Crate Items" } else { "Module Items" },
557+
)
558+
} else {
559+
Link::empty()
560+
};
561+
LinkBlock::new(header, "", item_sections)
541562
}
542563

543564
fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static> {
@@ -561,7 +582,7 @@ fn sidebar_module(items: &[clean::Item], ids: &mut IdMap) -> LinkBlock<'static>
561582
.map(|it| item_ty_to_section(it.type_()))
562583
.collect();
563584

564-
sidebar_module_like(item_sections_in_use, ids)
585+
sidebar_module_like(item_sections_in_use, ids, ModuleLike::Module)
565586
}
566587

567588
fn sidebar_foreign_type<'a>(

src/librustdoc/html/templates/sidebar.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ <h2 class="location"> {# #}
1717
{% if !block.heading.name.is_empty() %}
1818
<h3> {# #}
1919
<a href="#{{block.heading.href|safe}}">{{block.heading.name|wrapped|safe}}</a> {# #}
20-
</h3> {# #}
20+
</h3>
2121
{% endif %}
2222
{% if !block.links.is_empty() %}
2323
<ul class="block{% if !block.class.is_empty() +%} {{+block.class}}{% endif %}">
2424
{% for link in block.links %}
2525
<li> {# #}
2626
<a href="#{{link.href|safe}}">{{link.name}}</a> {# #}
2727
{% if !link.children.is_empty() %}
28-
<ul> {# #}
28+
<ul>
2929
{% for child in link.children %}
3030
<li><a href="#{{child.href|safe}}">{{child.name}}</a></li>
3131
{% endfor %}
32-
</ul> {# #}
32+
</ul>
3333
{% endif %}
34-
</li> {# #}
34+
</li>
3535
{% endfor %}
3636
</ul>
3737
{% endif %}
@@ -43,7 +43,7 @@ <h3> {# #}
4343
{% if !path.is_empty() %}
4444
<h2{% if parent_is_crate +%} class="in-crate"{% endif %}> {# #}
4545
<a href="{% if is_mod %}../{% endif %}index.html">In {{+ path|wrapped|safe}}</a> {# #}
46-
</h2> {# #}
46+
</h2>
4747
{% endif %}
4848
</div> {# #}
4949
</div>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![crate_name = "foo"]
2+
3+
//! This test case covers missing top TOC entries.
4+
5+
// @has foo/index.html
6+
// User header
7+
// @!has - '//section[@id="TOC"]/ul[@class="block top-toc"]' 'Basic link and emphasis'

0 commit comments

Comments
 (0)