Skip to content

Commit

Permalink
Rollup merge of rust-lang#64696 - GuillaumeGomez:rustdoc-sub-settings…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
Centril authored Nov 7, 2019
2 parents 50f8aad + 8784b07 commit d9bf678
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 24 deletions.
99 changes: 79 additions & 20 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,36 +1230,95 @@ impl AllTypes {
}
}

#[derive(Debug)]
enum Setting {
Section {
description: &'static str,
sub_settings: Vec<Setting>,
},
Entry {
js_data_name: &'static str,
description: &'static str,
default_value: bool,
}
}

impl Setting {
fn display(&self) -> String {
match *self {
Setting::Section { ref description, ref sub_settings } => {
format!(
"<div class='setting-line'>\
<div class='title'>{}</div>\
<div class='sub-settings'>{}</div>
</div>",
description,
sub_settings.iter().map(|s| s.display()).collect::<String>()
)
}
Setting::Entry { ref js_data_name, ref description, ref default_value } => {
format!(
"<div class='setting-line'>\
<label class='toggle'>\
<input type='checkbox' id='{}' {}>\
<span class='slider'></span>\
</label>\
<div>{}</div>\
</div>",
js_data_name,
if *default_value { " checked" } else { "" },
description,
)
}
}
}
}

impl From<(&'static str, &'static str, bool)> for Setting {
fn from(values: (&'static str, &'static str, bool)) -> Setting {
Setting::Entry {
js_data_name: values.0,
description: values.1,
default_value: values.2,
}
}
}

impl<T: Into<Setting>> From<(&'static str, Vec<T>)> for Setting {
fn from(values: (&'static str, Vec<T>)) -> Setting {
Setting::Section {
description: values.0,
sub_settings: values.1.into_iter().map(|v| v.into()).collect::<Vec<_>>(),
}
}
}

fn settings(root_path: &str, suffix: &str) -> String {
// (id, explanation, default value)
let settings = [
("item-declarations", "Auto-hide item declarations.", true),
("item-attributes", "Auto-hide item attributes.", true),
("trait-implementations", "Auto-hide trait implementations documentation",
true),
("method-docs", "Auto-hide item methods' documentation", false),
let settings: &[Setting] = &[
("Auto-hide item declarations", vec![
("auto-hide-struct", "Auto-hide structs declaration", true),
("auto-hide-enum", "Auto-hide enums declaration", false),
("auto-hide-union", "Auto-hide unions declaration", true),
("auto-hide-trait", "Auto-hide traits declaration", true),
("auto-hide-macro", "Auto-hide macros declaration", false),
]).into(),
("auto-hide-attributes", "Auto-hide item attributes.", true).into(),
("auto-hide-method-docs", "Auto-hide item methods' documentation", false).into(),
("auto-hide-trait-implementations", "Auto-hide trait implementations documentation",
true).into(),
("go-to-only-result", "Directly go to item in search if there is only one result",
false),
("line-numbers", "Show line numbers on code examples", false),
("disable-shortcuts", "Disable keyboard shortcuts", false),
false).into(),
("line-numbers", "Show line numbers on code examples", false).into(),
("disable-shortcuts", "Disable keyboard shortcuts", false).into(),
];
format!(
"<h1 class='fqn'>\
<span class='in-band'>Rustdoc settings</span>\
</h1>\
<div class='settings'>{}</div>\
<script src='{}settings{}.js'></script>",
settings.iter()
.map(|(id, text, enabled)| {
format!("<div class='setting-line'>\
<label class='toggle'>\
<input type='checkbox' id='{}' {}>\
<span class='slider'></span>\
</label>\
<div>{}</div>\
</div>", id, if *enabled { " checked" } else { "" }, text)
})
.collect::<String>(),
settings.iter().map(|s| s.display()).collect::<String>(),
root_path,
suffix)
}
Expand Down
32 changes: 28 additions & 4 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ function getSearchElement() {
function autoCollapse(pageId, collapse) {
if (collapse) {
toggleAllDocs(pageId, true);
} else if (getCurrentValue("rustdoc-trait-implementations") !== "false") {
} else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
var impl_list = document.getElementById("implementations-list");

if (impl_list !== null) {
Expand Down Expand Up @@ -2156,7 +2156,7 @@ function getSearchElement() {
}

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

var func = function(e) {
Expand Down Expand Up @@ -2286,7 +2286,31 @@ function getSearchElement() {
return wrapper;
}

var showItemDeclarations = getCurrentValue("rustdoc-item-declarations") === "false";
var currentType = document.getElementsByClassName("type-decl")[0];
var className = null;
if (currentType) {
currentType = currentType.getElementsByClassName("rust")[0];
if (currentType) {
currentType.classList.forEach(function(item) {
if (item !== "main") {
className = item;
return true;
}
});
}
}
var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
if (showItemDeclarations === null) {
if (className === "enum" || className === "macro") {
showItemDeclarations = "false";
} else if (className === "struct" || className === "union" || className === "trait") {
showItemDeclarations = "true";
} else {
// In case we found an unknown type, we just use the "parent" value.
showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
}
}
showItemDeclarations = showItemDeclarations === "false";
function buildToggleWrapper(e) {
if (hasClass(e, "autohide")) {
var wrap = e.previousElementSibling;
Expand Down Expand Up @@ -2369,7 +2393,7 @@ function getSearchElement() {

// To avoid checking on "rustdoc-item-attributes" value on every loop...
var itemAttributesFunc = function() {};
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
itemAttributesFunc = function(x) {
collapseDocs(x.previousSibling.childNodes[0], "toggle");
};
Expand Down
14 changes: 14 additions & 0 deletions src/librustdoc/html/static/settings.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.setting-line {
padding: 5px;
position: relative;
}

.setting-line > div {
Expand All @@ -10,6 +11,13 @@
padding-top: 2px;
}

.setting-line > .title {
font-size: 19px;
width: 100%;
max-width: none;
border-bottom: 1px solid;
}

.toggle {
position: relative;
display: inline-block;
Expand Down Expand Up @@ -59,3 +67,9 @@ input:checked + .slider:before {
-ms-transform: translateX(19px);
transform: translateX(19px);
}

.setting-line > .sub-settings {
padding-left: 42px;
width: 100%;
display: block;
}
3 changes: 3 additions & 0 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,6 @@ div.files > a:hover, div.name:hover {
div.files > .selected {
background-color: #333;
}
.setting-line > .title {
border-bottom-color: #ddd;
}
3 changes: 3 additions & 0 deletions src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,6 @@ div.files > a:hover, div.name:hover {
div.files > .selected {
background-color: #fff;
}
.setting-line > .title {
border-bottom-color: #D5D5D5;
}

0 comments on commit d9bf678

Please sign in to comment.