Skip to content

Commit d9bf678

Browse files
authored
Rollup merge of rust-lang#64696 - GuillaumeGomez:rustdoc-sub-settings, r=kinnison
[rustdoc] add sub settings This PR is to give a finer control over what types are automatically expanded or not as well as the possibility to add sub-settings in the settings page. ![Screenshot from 2019-09-23 00-46-14](https://user-images.githubusercontent.com/3050060/65395521-15aff300-dd9c-11e9-9437-429ca347d455.png) r? @Mark-Simulacrum
2 parents 50f8aad + 8784b07 commit d9bf678

File tree

5 files changed

+127
-24
lines changed

5 files changed

+127
-24
lines changed

src/librustdoc/html/render.rs

+79-20
Original file line numberDiff line numberDiff line change
@@ -1230,36 +1230,95 @@ impl AllTypes {
12301230
}
12311231
}
12321232

1233+
#[derive(Debug)]
1234+
enum Setting {
1235+
Section {
1236+
description: &'static str,
1237+
sub_settings: Vec<Setting>,
1238+
},
1239+
Entry {
1240+
js_data_name: &'static str,
1241+
description: &'static str,
1242+
default_value: bool,
1243+
}
1244+
}
1245+
1246+
impl Setting {
1247+
fn display(&self) -> String {
1248+
match *self {
1249+
Setting::Section { ref description, ref sub_settings } => {
1250+
format!(
1251+
"<div class='setting-line'>\
1252+
<div class='title'>{}</div>\
1253+
<div class='sub-settings'>{}</div>
1254+
</div>",
1255+
description,
1256+
sub_settings.iter().map(|s| s.display()).collect::<String>()
1257+
)
1258+
}
1259+
Setting::Entry { ref js_data_name, ref description, ref default_value } => {
1260+
format!(
1261+
"<div class='setting-line'>\
1262+
<label class='toggle'>\
1263+
<input type='checkbox' id='{}' {}>\
1264+
<span class='slider'></span>\
1265+
</label>\
1266+
<div>{}</div>\
1267+
</div>",
1268+
js_data_name,
1269+
if *default_value { " checked" } else { "" },
1270+
description,
1271+
)
1272+
}
1273+
}
1274+
}
1275+
}
1276+
1277+
impl From<(&'static str, &'static str, bool)> for Setting {
1278+
fn from(values: (&'static str, &'static str, bool)) -> Setting {
1279+
Setting::Entry {
1280+
js_data_name: values.0,
1281+
description: values.1,
1282+
default_value: values.2,
1283+
}
1284+
}
1285+
}
1286+
1287+
impl<T: Into<Setting>> From<(&'static str, Vec<T>)> for Setting {
1288+
fn from(values: (&'static str, Vec<T>)) -> Setting {
1289+
Setting::Section {
1290+
description: values.0,
1291+
sub_settings: values.1.into_iter().map(|v| v.into()).collect::<Vec<_>>(),
1292+
}
1293+
}
1294+
}
1295+
12331296
fn settings(root_path: &str, suffix: &str) -> String {
12341297
// (id, explanation, default value)
1235-
let settings = [
1236-
("item-declarations", "Auto-hide item declarations.", true),
1237-
("item-attributes", "Auto-hide item attributes.", true),
1238-
("trait-implementations", "Auto-hide trait implementations documentation",
1239-
true),
1240-
("method-docs", "Auto-hide item methods' documentation", false),
1298+
let settings: &[Setting] = &[
1299+
("Auto-hide item declarations", vec![
1300+
("auto-hide-struct", "Auto-hide structs declaration", true),
1301+
("auto-hide-enum", "Auto-hide enums declaration", false),
1302+
("auto-hide-union", "Auto-hide unions declaration", true),
1303+
("auto-hide-trait", "Auto-hide traits declaration", true),
1304+
("auto-hide-macro", "Auto-hide macros declaration", false),
1305+
]).into(),
1306+
("auto-hide-attributes", "Auto-hide item attributes.", true).into(),
1307+
("auto-hide-method-docs", "Auto-hide item methods' documentation", false).into(),
1308+
("auto-hide-trait-implementations", "Auto-hide trait implementations documentation",
1309+
true).into(),
12411310
("go-to-only-result", "Directly go to item in search if there is only one result",
1242-
false),
1243-
("line-numbers", "Show line numbers on code examples", false),
1244-
("disable-shortcuts", "Disable keyboard shortcuts", false),
1311+
false).into(),
1312+
("line-numbers", "Show line numbers on code examples", false).into(),
1313+
("disable-shortcuts", "Disable keyboard shortcuts", false).into(),
12451314
];
12461315
format!(
12471316
"<h1 class='fqn'>\
12481317
<span class='in-band'>Rustdoc settings</span>\
12491318
</h1>\
12501319
<div class='settings'>{}</div>\
12511320
<script src='{}settings{}.js'></script>",
1252-
settings.iter()
1253-
.map(|(id, text, enabled)| {
1254-
format!("<div class='setting-line'>\
1255-
<label class='toggle'>\
1256-
<input type='checkbox' id='{}' {}>\
1257-
<span class='slider'></span>\
1258-
</label>\
1259-
<div>{}</div>\
1260-
</div>", id, if *enabled { " checked" } else { "" }, text)
1261-
})
1262-
.collect::<String>(),
1321+
settings.iter().map(|s| s.display()).collect::<String>(),
12631322
root_path,
12641323
suffix)
12651324
}

src/librustdoc/html/static/main.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ function getSearchElement() {
21182118
function autoCollapse(pageId, collapse) {
21192119
if (collapse) {
21202120
toggleAllDocs(pageId, true);
2121-
} else if (getCurrentValue("rustdoc-trait-implementations") !== "false") {
2121+
} else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
21222122
var impl_list = document.getElementById("implementations-list");
21232123

21242124
if (impl_list !== null) {
@@ -2156,7 +2156,7 @@ function getSearchElement() {
21562156
}
21572157

21582158
var toggle = createSimpleToggle(false);
2159-
var hideMethodDocs = getCurrentValue("rustdoc-method-docs") === "true";
2159+
var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
21602160
var pageId = getPageId();
21612161

21622162
var func = function(e) {
@@ -2286,7 +2286,31 @@ function getSearchElement() {
22862286
return wrapper;
22872287
}
22882288

2289-
var showItemDeclarations = getCurrentValue("rustdoc-item-declarations") === "false";
2289+
var currentType = document.getElementsByClassName("type-decl")[0];
2290+
var className = null;
2291+
if (currentType) {
2292+
currentType = currentType.getElementsByClassName("rust")[0];
2293+
if (currentType) {
2294+
currentType.classList.forEach(function(item) {
2295+
if (item !== "main") {
2296+
className = item;
2297+
return true;
2298+
}
2299+
});
2300+
}
2301+
}
2302+
var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
2303+
if (showItemDeclarations === null) {
2304+
if (className === "enum" || className === "macro") {
2305+
showItemDeclarations = "false";
2306+
} else if (className === "struct" || className === "union" || className === "trait") {
2307+
showItemDeclarations = "true";
2308+
} else {
2309+
// In case we found an unknown type, we just use the "parent" value.
2310+
showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
2311+
}
2312+
}
2313+
showItemDeclarations = showItemDeclarations === "false";
22902314
function buildToggleWrapper(e) {
22912315
if (hasClass(e, "autohide")) {
22922316
var wrap = e.previousElementSibling;
@@ -2369,7 +2393,7 @@ function getSearchElement() {
23692393

23702394
// To avoid checking on "rustdoc-item-attributes" value on every loop...
23712395
var itemAttributesFunc = function() {};
2372-
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
2396+
if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
23732397
itemAttributesFunc = function(x) {
23742398
collapseDocs(x.previousSibling.childNodes[0], "toggle");
23752399
};

src/librustdoc/html/static/settings.css

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.setting-line {
22
padding: 5px;
3+
position: relative;
34
}
45

56
.setting-line > div {
@@ -10,6 +11,13 @@
1011
padding-top: 2px;
1112
}
1213

14+
.setting-line > .title {
15+
font-size: 19px;
16+
width: 100%;
17+
max-width: none;
18+
border-bottom: 1px solid;
19+
}
20+
1321
.toggle {
1422
position: relative;
1523
display: inline-block;
@@ -59,3 +67,9 @@ input:checked + .slider:before {
5967
-ms-transform: translateX(19px);
6068
transform: translateX(19px);
6169
}
70+
71+
.setting-line > .sub-settings {
72+
padding-left: 42px;
73+
width: 100%;
74+
display: block;
75+
}

src/librustdoc/html/static/themes/dark.css

+3
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,6 @@ div.files > a:hover, div.name:hover {
425425
div.files > .selected {
426426
background-color: #333;
427427
}
428+
.setting-line > .title {
429+
border-bottom-color: #ddd;
430+
}

src/librustdoc/html/static/themes/light.css

+3
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,6 @@ div.files > a:hover, div.name:hover {
419419
div.files > .selected {
420420
background-color: #fff;
421421
}
422+
.setting-line > .title {
423+
border-bottom-color: #D5D5D5;
424+
}

0 commit comments

Comments
 (0)