Skip to content

Commit 948e3a3

Browse files
committed
Auto merge of #48755 - GuillaumeGomez:rustdoc-fixes, r=QuietMisdreavus
Multiple rustdoc fixes Fixes #48733. r? @QuietMisdreavus
2 parents 3edb3cc + 89f4f1b commit 948e3a3

File tree

4 files changed

+97
-64
lines changed

4 files changed

+97
-64
lines changed

src/librustdoc/html/render.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -3521,11 +3521,9 @@ impl<'a> fmt::Display for Sidebar<'a> {
35213521
let cx = self.cx;
35223522
let it = self.item;
35233523
let parentlen = cx.current.len() - if it.is_mod() {1} else {0};
3524-
let mut should_close = false;
35253524

35263525
if it.is_struct() || it.is_trait() || it.is_primitive() || it.is_union()
3527-
|| it.is_enum() || it.is_mod() || it.is_typedef()
3528-
{
3526+
|| it.is_enum() || it.is_mod() || it.is_typedef() {
35293527
write!(fmt, "<p class='location'>")?;
35303528
match it.inner {
35313529
clean::StructItem(..) => write!(fmt, "Struct ")?,
@@ -3544,30 +3542,29 @@ impl<'a> fmt::Display for Sidebar<'a> {
35443542
}
35453543
write!(fmt, "{}", it.name.as_ref().unwrap())?;
35463544
write!(fmt, "</p>")?;
3545+
}
35473546

3548-
if it.is_crate() {
3549-
if let Some(ref version) = cache().crate_version {
3550-
write!(fmt,
3551-
"<div class='block version'>\
3552-
<p>Version {}</p>\
3553-
</div>",
3554-
version)?;
3555-
}
3547+
if it.is_crate() {
3548+
if let Some(ref version) = cache().crate_version {
3549+
write!(fmt,
3550+
"<div class='block version'>\
3551+
<p>Version {}</p>\
3552+
</div>",
3553+
version)?;
35563554
}
3555+
}
35573556

3558-
write!(fmt, "<div class=\"sidebar-elems\">")?;
3559-
should_close = true;
3560-
match it.inner {
3561-
clean::StructItem(ref s) => sidebar_struct(fmt, it, s)?,
3562-
clean::TraitItem(ref t) => sidebar_trait(fmt, it, t)?,
3563-
clean::PrimitiveItem(ref p) => sidebar_primitive(fmt, it, p)?,
3564-
clean::UnionItem(ref u) => sidebar_union(fmt, it, u)?,
3565-
clean::EnumItem(ref e) => sidebar_enum(fmt, it, e)?,
3566-
clean::TypedefItem(ref t, _) => sidebar_typedef(fmt, it, t)?,
3567-
clean::ModuleItem(ref m) => sidebar_module(fmt, it, &m.items)?,
3568-
clean::ForeignTypeItem => sidebar_foreign_type(fmt, it)?,
3569-
_ => (),
3570-
}
3557+
write!(fmt, "<div class=\"sidebar-elems\">")?;
3558+
match it.inner {
3559+
clean::StructItem(ref s) => sidebar_struct(fmt, it, s)?,
3560+
clean::TraitItem(ref t) => sidebar_trait(fmt, it, t)?,
3561+
clean::PrimitiveItem(ref p) => sidebar_primitive(fmt, it, p)?,
3562+
clean::UnionItem(ref u) => sidebar_union(fmt, it, u)?,
3563+
clean::EnumItem(ref e) => sidebar_enum(fmt, it, e)?,
3564+
clean::TypedefItem(ref t, _) => sidebar_typedef(fmt, it, t)?,
3565+
clean::ModuleItem(ref m) => sidebar_module(fmt, it, &m.items)?,
3566+
clean::ForeignTypeItem => sidebar_foreign_type(fmt, it)?,
3567+
_ => (),
35713568
}
35723569

35733570
// The sidebar is designed to display sibling functions, modules and
@@ -3607,10 +3604,8 @@ impl<'a> fmt::Display for Sidebar<'a> {
36073604
write!(fmt, "<script defer src=\"{path}sidebar-items.js\"></script>",
36083605
path = relpath)?;
36093606
}
3610-
if should_close {
3611-
// Closes sidebar-elems div.
3612-
write!(fmt, "</div>")?;
3613-
}
3607+
// Closes sidebar-elems div.
3608+
write!(fmt, "</div>")?;
36143609

36153610
Ok(())
36163611
}

src/librustdoc/html/static/main.js

+52-36
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,15 @@
240240
}
241241

242242
function handleShortcut(ev) {
243-
if (document.activeElement.tagName === "INPUT")
243+
if (document.activeElement.tagName === "INPUT" &&
244+
hasClass(document.getElementById('main'), "hidden")) {
244245
return;
246+
}
245247

246248
// Don't interfere with browser shortcuts
247-
if (ev.ctrlKey || ev.altKey || ev.metaKey)
249+
if (ev.ctrlKey || ev.altKey || ev.metaKey) {
248250
return;
251+
}
249252

250253
var help = document.getElementById("help");
251254
switch (getVirtualKey(ev)) {
@@ -1457,36 +1460,38 @@
14571460
// Draw a convenient sidebar of known crates if we have a listing
14581461
if (rootPath === '../') {
14591462
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
1460-
var div = document.createElement('div');
1461-
div.className = 'block crate';
1462-
div.innerHTML = '<h3>Crates</h3>';
1463-
var ul = document.createElement('ul');
1464-
div.appendChild(ul);
1465-
1466-
var crates = [];
1467-
for (var crate in rawSearchIndex) {
1468-
if (!rawSearchIndex.hasOwnProperty(crate)) {
1469-
continue;
1463+
if (sidebar) {
1464+
var div = document.createElement('div');
1465+
div.className = 'block crate';
1466+
div.innerHTML = '<h3>Crates</h3>';
1467+
var ul = document.createElement('ul');
1468+
div.appendChild(ul);
1469+
1470+
var crates = [];
1471+
for (var crate in rawSearchIndex) {
1472+
if (!rawSearchIndex.hasOwnProperty(crate)) {
1473+
continue;
1474+
}
1475+
crates.push(crate);
14701476
}
1471-
crates.push(crate);
1472-
}
1473-
crates.sort();
1474-
for (var i = 0; i < crates.length; ++i) {
1475-
var klass = 'crate';
1476-
if (crates[i] === window.currentCrate) {
1477-
klass += ' current';
1477+
crates.sort();
1478+
for (var i = 0; i < crates.length; ++i) {
1479+
var klass = 'crate';
1480+
if (crates[i] === window.currentCrate) {
1481+
klass += ' current';
1482+
}
1483+
var link = document.createElement('a');
1484+
link.href = '../' + crates[i] + '/index.html';
1485+
link.title = rawSearchIndex[crates[i]].doc;
1486+
link.className = klass;
1487+
link.textContent = crates[i];
1488+
1489+
var li = document.createElement('li');
1490+
li.appendChild(link);
1491+
ul.appendChild(li);
14781492
}
1479-
var link = document.createElement('a');
1480-
link.href = '../' + crates[i] + '/index.html';
1481-
link.title = rawSearchIndex[crates[i]].doc;
1482-
link.className = klass;
1483-
link.textContent = crates[i];
1484-
1485-
var li = document.createElement('li');
1486-
li.appendChild(link);
1487-
ul.appendChild(li);
1493+
sidebar.appendChild(div);
14881494
}
1489-
sidebar.appendChild(div);
14901495
}
14911496
}
14921497

@@ -1776,22 +1781,33 @@
17761781
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
17771782
}
17781783

1784+
function checkIfThereAreMethods(elems) {
1785+
var areThereMethods = false;
1786+
1787+
onEach(elems, function(e) {
1788+
if (hasClass(e, "method")) {
1789+
areThereMethods = true;
1790+
return true;
1791+
}
1792+
});
1793+
return areThereMethods;
1794+
}
1795+
17791796
var toggle = document.createElement('a');
17801797
toggle.href = 'javascript:void(0)';
17811798
toggle.className = 'collapse-toggle';
1782-
toggle.innerHTML = "[<span class='inner'>"+labelForToggleButton(false)+"</span>]";
1799+
toggle.innerHTML = "[<span class='inner'>" + labelForToggleButton(false) + "</span>]";
17831800

17841801
var func = function(e) {
17851802
var next = e.nextElementSibling;
17861803
if (!next) {
17871804
return;
17881805
}
1789-
if (hasClass(next, 'docblock') ||
1790-
(hasClass(next, 'stability') &&
1791-
hasClass(next.nextElementSibling, 'docblock'))) {
1792-
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
1793-
}
1794-
if (hasClass(e, 'impl')) {
1806+
if ((checkIfThereAreMethods(next.childNodes) || hasClass(e, 'method')) &&
1807+
(hasClass(next, 'docblock') ||
1808+
hasClass(e, 'impl') ||
1809+
(hasClass(next, 'stability') &&
1810+
hasClass(next.nextElementSibling, 'docblock')))) {
17951811
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
17961812
}
17971813
}

src/librustdoc/html/static/rustdoc.css

+3
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ a {
528528
.anchor.field {
529529
left: -5px;
530530
}
531+
.small-section-header > .anchor {
532+
left: -28px;
533+
}
531534
.anchor:before {
532535
content: '\2002\00a7\2002';
533536
}

src/test/rustdoc/fn-sidebar.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_name = "foo"]
12+
13+
// @has foo/fn.bar.html
14+
// @has - '//*[@class="sidebar-elems"]' ''
15+
pub fn bar() {}
16+
17+
// @has foo/constant.BAR.html
18+
// @has - '//*[@class="sidebar-elems"]' ''
19+
pub const BAR: u32 = 0;

0 commit comments

Comments
 (0)