Skip to content

Commit dab01a0

Browse files
* Put crates list at all levels
* Fix bug in module sidebar: the list of items was from the parent module
1 parent dc08641 commit dab01a0

File tree

5 files changed

+81
-90
lines changed

5 files changed

+81
-90
lines changed

src/librustdoc/clean/types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ impl Item {
526526
crate fn is_crate(&self) -> bool {
527527
self.is_mod() && self.def_id.as_real().map_or(false, |did| did.index == CRATE_DEF_INDEX)
528528
}
529-
530529
crate fn is_mod(&self) -> bool {
531530
self.type_() == ItemType::Module
532531
}

src/librustdoc/html/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ crate fn render<T: Print, S: Print>(
119119
{after_content}\
120120
<div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\" \
121121
data-search-index-js=\"{root_path}search-index{suffix}.js\" \
122-
data-search-js=\"{static_root_path}search{suffix}.js\"></div>
122+
data-search-js=\"{static_root_path}search{suffix}.js\"></div>\
123123
<script src=\"{static_root_path}main{suffix}.js\"></script>\
124124
{extra_scripts}\
125125
</body>\

src/librustdoc/html/render/mod.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
17031703
"<div class=\"block version\">\
17041704
<p>Version {}</p>\
17051705
</div>",
1706-
Escape(version)
1706+
Escape(version),
17071707
);
17081708
}
17091709
}
@@ -1713,9 +1713,10 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
17131713
write!(
17141714
buffer,
17151715
"<a id=\"all-types\" href=\"all.html\"><p>See all {}'s items</p></a>",
1716-
it.name.as_ref().expect("crates always have a name")
1716+
it.name.as_ref().expect("crates always have a name"),
17171717
);
17181718
}
1719+
17191720
match *it.kind {
17201721
clean::StructItem(ref s) => sidebar_struct(cx, buffer, it, s),
17211722
clean::TraitItem(ref t) => sidebar_trait(cx, buffer, it, t),
@@ -1725,7 +1726,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
17251726
clean::TypedefItem(_, _) => sidebar_typedef(cx, buffer, it),
17261727
clean::ModuleItem(ref m) => sidebar_module(buffer, &m.items),
17271728
clean::ForeignTypeItem => sidebar_foreign_type(cx, buffer, it),
1728-
_ => (),
1729+
_ => {}
17291730
}
17301731

17311732
// The sidebar is designed to display sibling functions, modules and
@@ -1736,22 +1737,24 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
17361737
// as much HTML as possible in order to allow non-JS-enabled browsers
17371738
// to navigate the documentation (though slightly inefficiently).
17381739

1739-
buffer.write_str("<p class=\"location\">");
1740-
for (i, name) in cx.current.iter().take(parentlen).enumerate() {
1741-
if i > 0 {
1742-
buffer.write_str("::<wbr>");
1740+
if !it.is_mod() {
1741+
buffer.write_str("<p class=\"location\">");
1742+
for (i, name) in cx.current.iter().take(parentlen).enumerate() {
1743+
if i > 0 {
1744+
buffer.write_str("::<wbr>");
1745+
}
1746+
write!(
1747+
buffer,
1748+
"<a href=\"{}index.html\">{}</a>",
1749+
&cx.root_path()[..(cx.current.len() - i - 1) * 3],
1750+
*name
1751+
);
17431752
}
1744-
write!(
1745-
buffer,
1746-
"<a href=\"{}index.html\">{}</a>",
1747-
&cx.root_path()[..(cx.current.len() - i - 1) * 3],
1748-
*name
1749-
);
1753+
buffer.write_str("</p>");
17501754
}
1751-
buffer.write_str("</p>");
17521755

17531756
// Sidebar refers to the enclosing module, not this module.
1754-
let relpath = if it.is_mod() { "../" } else { "" };
1757+
let relpath = if it.is_mod() && parentlen != 0 { "./" } else { "" };
17551758
write!(
17561759
buffer,
17571760
"<div id=\"sidebar-vars\" data-name=\"{name}\" data-ty=\"{ty}\" data-relpath=\"{path}\">\
@@ -1760,17 +1763,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
17601763
ty = it.type_(),
17611764
path = relpath
17621765
);
1763-
1764-
if parentlen == 0 {
1765-
write!(
1766-
buffer,
1767-
"<script defer src=\"{}sidebar-items{}.js\"></script>",
1768-
relpath, cx.shared.resource_suffix
1769-
);
1770-
} else {
1771-
write!(buffer, "<script defer src=\"{}sidebar-items.js\"></script>", relpath);
1772-
}
1773-
1766+
write!(buffer, "<script defer src=\"{}sidebar-items.js\"></script>", relpath);
17741767
// Closes sidebar-elems div.
17751768
buffer.write_str("</div>");
17761769
}
@@ -2278,8 +2271,8 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
22782271
}
22792272
}
22802273

2281-
fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
2282-
match *ty {
2274+
fn item_ty_to_strs(ty: ItemType) -> (&'static str, &'static str) {
2275+
match ty {
22832276
ItemType::ExternCrate | ItemType::Import => ("reexports", "Re-exports"),
22842277
ItemType::Module => ("modules", "Modules"),
22852278
ItemType::Struct => ("structs", "Structs"),
@@ -2311,10 +2304,14 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
23112304
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
23122305
let mut sidebar = String::new();
23132306

2307+
// Re-exports are handled a bit differently because they can be extern crates or imports.
23142308
if items.iter().any(|it| {
2315-
it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped())
2309+
it.name.is_some()
2310+
&& (it.type_() == ItemType::ExternCrate
2311+
|| (it.type_() == ItemType::Import && !it.is_stripped()))
23162312
}) {
2317-
sidebar.push_str("<li><a href=\"#reexports\">Re-exports</a></li>");
2313+
let (id, name) = item_ty_to_strs(ItemType::Import);
2314+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
23182315
}
23192316

23202317
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
@@ -2341,13 +2338,9 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
23412338
ItemType::ForeignType,
23422339
ItemType::Keyword,
23432340
] {
2344-
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty) {
2345-
let (short, name) = item_ty_to_strs(&myty);
2346-
sidebar.push_str(&format!(
2347-
"<li><a href=\"#{id}\">{name}</a></li>",
2348-
id = short,
2349-
name = name
2350-
));
2341+
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
2342+
let (id, name) = item_ty_to_strs(myty);
2343+
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", id, name));
23512344
}
23522345
}
23532346

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
262262
w.write_str("</table>");
263263
}
264264
curty = myty;
265-
let (short, name) = item_ty_to_strs(&myty.unwrap());
265+
let (short, name) = item_ty_to_strs(myty.unwrap());
266266
write!(
267267
w,
268268
"<h2 id=\"{id}\" class=\"section-header\">\

src/librustdoc/html/static/main.js

+49-50
Original file line numberDiff line numberDiff line change
@@ -561,40 +561,36 @@ function hideThemeButtonState() {
561561
}
562562
}());
563563

564-
function addSidebarCrates(crates) {
565-
// Draw a convenient sidebar of known crates if we have a listing
566-
if (window.rootPath === "../" || window.rootPath === "./") {
567-
var sidebar = document.getElementsByClassName("sidebar-elems")[0];
568-
if (sidebar) {
569-
var div = document.createElement("div");
570-
div.className = "block crate";
571-
div.innerHTML = "<h3>Crates</h3>";
572-
var ul = document.createElement("ul");
573-
div.appendChild(ul);
574-
575-
for (var i = 0; i < crates.length; ++i) {
576-
var klass = "crate";
577-
if (window.rootPath !== "./" && crates[i] === window.currentCrate) {
578-
klass += " current";
579-
}
580-
var link = document.createElement("a");
581-
link.href = window.rootPath + crates[i] + "/index.html";
582-
link.className = klass;
583-
link.textContent = crates[i];
584-
585-
var li = document.createElement("li");
586-
li.appendChild(link);
587-
ul.appendChild(li);
588-
}
589-
sidebar.appendChild(div);
590-
}
591-
}
592-
}
593-
594564
// delayed sidebar rendering.
595565
window.initSidebarItems = function(items) {
596566
var sidebar = document.getElementsByClassName("sidebar-elems")[0];
597567
var current = window.sidebarCurrent;
568+
var isModule = hasClass(document.body, "mod");
569+
570+
function addSidebarCrates(crates) {
571+
// Draw a convenient sidebar of known crates if we have a listing
572+
var div = document.createElement("div");
573+
div.className = "block crate";
574+
div.innerHTML = "<h3>Crates</h3>";
575+
var ul = document.createElement("ul");
576+
div.appendChild(ul);
577+
578+
for (var i = 0; i < crates.length; ++i) {
579+
var klass = "crate";
580+
if (window.rootPath !== "./" && crates[i] === window.currentCrate) {
581+
klass += " current";
582+
}
583+
var link = document.createElement("a");
584+
link.href = window.rootPath + crates[i] + "/index.html";
585+
link.className = klass;
586+
link.textContent = crates[i];
587+
588+
var li = document.createElement("li");
589+
li.appendChild(link);
590+
ul.appendChild(li);
591+
}
592+
sidebar.appendChild(div);
593+
}
598594

599595
function block(shortty, longty) {
600596
var filtered = items[shortty];
@@ -634,28 +630,31 @@ function hideThemeButtonState() {
634630
ul.appendChild(li);
635631
}
636632
div.appendChild(ul);
637-
if (sidebar) {
638-
sidebar.appendChild(div);
639-
}
633+
sidebar.appendChild(div);
640634
}
641635

642-
block("primitive", "Primitive Types");
643-
block("mod", "Modules");
644-
block("macro", "Macros");
645-
block("struct", "Structs");
646-
block("enum", "Enums");
647-
block("union", "Unions");
648-
block("constant", "Constants");
649-
block("static", "Statics");
650-
block("trait", "Traits");
651-
block("fn", "Functions");
652-
block("type", "Type Definitions");
653-
block("foreigntype", "Foreign Types");
654-
block("keyword", "Keywords");
655-
block("traitalias", "Trait Aliases");
656-
657-
// `crates{version}.js` should always be loaded before this script, so we can use it safely.
658-
addSidebarCrates(window.ALL_CRATES);
636+
if (sidebar) {
637+
if (!isModule) {
638+
block("primitive", "Primitive Types");
639+
block("mod", "Modules");
640+
block("macro", "Macros");
641+
block("struct", "Structs");
642+
block("enum", "Enums");
643+
block("union", "Unions");
644+
block("constant", "Constants");
645+
block("static", "Statics");
646+
block("trait", "Traits");
647+
block("fn", "Functions");
648+
block("type", "Type Definitions");
649+
block("foreigntype", "Foreign Types");
650+
block("keyword", "Keywords");
651+
block("traitalias", "Trait Aliases");
652+
}
653+
654+
// `crates{version}.js` should always be loaded before this script, so we can use
655+
// it safely.
656+
addSidebarCrates(window.ALL_CRATES);
657+
}
659658
};
660659

661660
window.register_implementors = function(imp) {

0 commit comments

Comments
 (0)