Skip to content

Commit 47c4632

Browse files
committed
rustdoc: clean up the In [name] up-pointer
This commit makes three changes for consistency and readability: - It shows the sibling navigation on module pages. It's weird that it didn't work before, and is inconsistent with everything else (even Crates have sibling navigation with other Crates). - It hides the "In [parent]" header if it's the same as the current crate, and if there's no other header between them. We need to keep it on modules and types, since they have their own header and data between them, and we don't want to show siblings under a header implying that they're children. - It adds a margin to deal with the headers butting directly into the branding lockup.
1 parent b0d76a7 commit 47c4632

File tree

9 files changed

+50
-16
lines changed

9 files changed

+50
-16
lines changed

src/librustdoc/html/render/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
662662
let shared = Rc::clone(&self.shared);
663663
let mut page = layout::Page {
664664
title: "List of all items in this crate",
665-
css_class: "mod",
665+
css_class: "mod sys",
666666
root_path: "../",
667667
static_root_path: shared.static_root_path.as_deref(),
668668
description: "List of all items in this crate",
@@ -677,6 +677,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
677677
title_prefix: "",
678678
title: "",
679679
is_crate: false,
680+
is_mod: false,
680681
blocks: vec![blocks],
681682
path: String::new(),
682683
};

src/librustdoc/html/render/sidebar.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(super) struct Sidebar<'a> {
1919
pub(super) title_prefix: &'static str,
2020
pub(super) title: &'a str,
2121
pub(super) is_crate: bool,
22+
pub(super) is_mod: bool,
2223
pub(super) blocks: Vec<LinkBlock<'a>>,
2324
pub(super) path: String,
2425
}
@@ -112,12 +113,24 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buf
112113
} else {
113114
("", "")
114115
};
115-
let path: String = if !it.is_mod() {
116-
cx.current.iter().map(|s| s.as_str()).intersperse("::").collect()
116+
// need to show parent path header if:
117+
// - it's a child module, instead of the crate root
118+
// - there's a sidebar section for the item itself
119+
//
120+
// otherwise, the parent path header is redundant with the big crate
121+
// branding area at the top of the sidebar
122+
let sidebar_path = if it.is_mod() { &cx.current[..cx.current.len() - 1] } else { &cx.current[..] };
123+
let path: String = if sidebar_path.len() > 1 || !title.is_empty() {
124+
let path = sidebar_path.iter().map(|s| s.as_str()).intersperse("::").collect();
125+
if sidebar_path.len() == 1 {
126+
format!("crate {path}")
127+
} else {
128+
path
129+
}
117130
} else {
118131
"".into()
119132
};
120-
let sidebar = Sidebar { title_prefix, title, is_crate: it.is_crate(), blocks, path };
133+
let sidebar = Sidebar { title_prefix, title, is_mod: it.is_mod(), is_crate: it.is_crate(), blocks, path };
121134
sidebar.render_into(buffer).unwrap();
122135
}
123136

src/librustdoc/html/render/write_shared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
336336
let dst = cx.dst.join("index.html");
337337
let page = layout::Page {
338338
title: "Index of crates",
339-
css_class: "mod",
339+
css_class: "mod sys",
340340
root_path: "./",
341341
static_root_path: shared.static_root_path.as_deref(),
342342
description: "List of crates",

src/librustdoc/html/static/css/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ ul.block, .block li {
523523
justify-content: center;
524524
/* there's a 10px padding at the top of <main>, and a 4px margin at the
525525
top of the search form. To line them up, add them. */
526-
margin: 14px 32px 0;
526+
margin: 14px 32px 1rem;
527527
row-gap: 10px;
528528
column-gap: 32px;
529529
flex-wrap: wrap;

src/librustdoc/html/static/js/main.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -457,22 +457,27 @@ function preLoadCss(cssUrl) {
457457
return;
458458
}
459459

460+
const modpath = hasClass(document.body, "mod") ? "../" : "";
461+
460462
const h3 = document.createElement("h3");
461-
h3.innerHTML = `<a href="index.html#${id}">${longty}</a>`;
463+
h3.innerHTML = `<a href="${modpath}index.html#${id}">${longty}</a>`;
462464
const ul = document.createElement("ul");
463465
ul.className = "block " + shortty;
464466

465467
for (const name of filtered) {
466468
let path;
467469
if (shortty === "mod") {
468-
path = name + "/index.html";
470+
path = `${modpath}${name}/index.html`;
469471
} else {
470-
path = shortty + "." + name + ".html";
472+
path = `${modpath}${shortty}.${name}.html`;
473+
}
474+
let current_page = document.location.href.toString();
475+
if (current_page.endsWith("/")) {
476+
current_page += "index.html";
471477
}
472-
const current_page = document.location.href.split("/").pop();
473478
const link = document.createElement("a");
474479
link.href = path;
475-
if (path === current_page) {
480+
if (link.href === current_page) {
476481
link.className = "current";
477482
}
478483
link.textContent = name;

src/librustdoc/html/templates/page.html

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
<script defer src="{{page.root_path|safe}}src-files{{page.resource_suffix}}.js"></script> {# #}
4343
{% else if !page.css_class.contains("mod") %}
4444
<script defer src="sidebar-items{{page.resource_suffix}}.js"></script> {# #}
45+
{% else if !page.css_class.contains("sys") %}
46+
<script defer src="../sidebar-items{{page.resource_suffix}}.js"></script> {# #}
4547
{% endif %}
4648
<script defer src="{{static_root_path|safe}}{{files.main_js}}"></script> {# #}
4749
{% if layout.scrape_examples_extension %}

src/librustdoc/html/templates/sidebar.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ <h3><a href="#{{block.heading.href|safe}}">{{block.heading.name}}</a></h3>
2929
</section>
3030
{% endif %}
3131
{% if !path.is_empty() %}
32-
<h2><a href="index.html">In {{+ path}}</a></h2>
32+
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a></h2>
3333
{% endif %}
3434
</div>

tests/rustdoc-gui/sidebar-mobile.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ assert-css: (".sidebar", {"left": "0px"})
2626
// Make sure the "struct Foo" header is hidden, since the mobile topbar already does it.
2727
assert-css: ("//nav[contains(@class, 'sidebar')]//h2/a[text()='Foo']/parent::h2", {"display": "none"})
2828
// Make sure the global navigation is still here.
29-
assert-css: ("//nav[contains(@class, 'sidebar')]//h2/a[text()='In test_docs']/parent::h2", {"display": "block"})
29+
assert-css: ("//nav[contains(@class, 'sidebar')]//h2/a[text()='In crate test_docs']/parent::h2", {"display": "block"})
3030

3131
// Click elsewhere.
3232
click: "body"

tests/rustdoc-gui/sidebar.goml

+16-3
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ click: "#functions + .item-table .item-name > a"
110110

111111
// PAGE: fn.foobar.html
112112
// In items containing no items (like functions or constants) and in modules, we have no
113-
// "location" elements. Only the parent module h2 and crate.
113+
// "location" elements. Only the crate and optional parent module.
114+
// This page, being directly below the crate, only has its heading.
114115
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
115116
assert-count: (".sidebar .location", 0)
116-
assert-count: (".sidebar h2", 2)
117-
assert-text: (".sidebar .sidebar-elems h2", "In lib2")
117+
assert-count: (".sidebar h2", 1)
118118
// We check that we don't have the crate list.
119119
assert-false: ".sidebar-elems > .crate"
120120

@@ -123,13 +123,26 @@ assert-property: (".sidebar", {"clientWidth": "200"})
123123
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
124124
assert-text: (".sidebar > .location", "Module module")
125125
assert-count: (".sidebar .location", 1)
126+
// Module page requires three headings:
127+
// - Presistent crate branding (name and version)
128+
// - Module name, followed by TOC for module headings
129+
// - "In crate [name]" parent pointer, followed by sibling navigation
130+
assert-count: (".sidebar h2", 3)
131+
assert-text: (".sidebar > .sidebar-elems > h2", "In crate lib2")
132+
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
133+
"href": "/lib2/index.html",
134+
}, ENDS_WITH)
126135
// We check that we don't have the crate list.
127136
assert-false: ".sidebar-elems > .crate"
128137

129138
go-to: "./sub_module/sub_sub_module/index.html"
130139
assert-property: (".sidebar", {"clientWidth": "200"})
131140
assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2")
132141
assert-text: (".sidebar > .location", "Module sub_sub_module")
142+
assert-text: (".sidebar > .sidebar-elems > h2", "In lib2::module::sub_module")
143+
assert-property: (".sidebar > .sidebar-elems > h2 > a", {
144+
"href": "/module/sub_module/index.html",
145+
}, ENDS_WITH)
133146
// We check that we don't have the crate list.
134147
assert-false: ".sidebar-elems .crate"
135148
assert-text: (".sidebar-elems > section ul > li:nth-child(1)", "Functions")

0 commit comments

Comments
 (0)