Skip to content

rustdoc: Convert sub-variant toggle to HTML #84321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum

use crate::clean::Variant;
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
toggle_open(w, "fields");
let variant_id = cx.derive_id(format!(
"{}.{}.fields",
ItemType::Variant,
Expand Down Expand Up @@ -967,6 +968,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
}
}
w.write_str("</div></div>");
toggle_close(w);
}
render_stability_since(w, variant, it, cx.tcx());
}
Expand Down
16 changes: 12 additions & 4 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ function hideThemeButtonState() {
handleHashes(ev);
}

function openParentDetails(elem) {
while (elem) {
if (elem.tagName === "DETAILS") {
elem.open = true;
}
elem = elem.parentNode;
}
}

function expandSection(id) {
var elem = document.getElementById(id);
if (elem && isHidden(elem)) {
Expand All @@ -442,6 +451,8 @@ function hideThemeButtonState() {
// The element is not visible, we need to make it appear!
collapseDocs(collapses[0], "show");
}
// Open all ancestor <details> to make this element visible.
openParentDetails(h3.parentNode);
}
}
}
Expand Down Expand Up @@ -2418,7 +2429,7 @@ function hideThemeButtonState() {
if (hasClass(relatedDoc, "item-info")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
if (hasClass(relatedDoc, "docblock")) {
if (mode === "toggle") {
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
action = "show";
Expand Down Expand Up @@ -2727,8 +2738,6 @@ function hideThemeButtonState() {
if (hasClass(e, "type-decl")) {
// We do something special for these
return;
} else if (hasClass(e, "sub-variant")) {
otherMessage = "&nbsp;Show&nbsp;fields";
} else if (hasClass(e, "non-exhaustive")) {
otherMessage = "&nbsp;This&nbsp;";
if (hasClass(e, "non-exhaustive-struct")) {
Expand Down Expand Up @@ -2760,7 +2769,6 @@ function hideThemeButtonState() {
}

onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);

autoCollapse(getSettingValue("collapse") === "true");

Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,11 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
}

.sub-variant, .sub-variant > h3 {
margin-top: 1px !important;
margin-top: 0px !important;
padding-top: 1px;
}

#main > .sub-variant > h3 {
#main > details > .sub-variant > h3 {
font-size: 15px;
margin-left: 25px;
margin-bottom: 5px;
Expand Down
8 changes: 7 additions & 1 deletion src/test/rustdoc-gui/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub struct Foo;

impl Foo {
#[must_use]
pub fn must_use(&self) -> bool { true }
pub fn must_use(&self) -> bool {
true
}
}

/// Just a normal enum.
Expand Down Expand Up @@ -85,3 +87,7 @@ pub trait AnotherOne {
/// let x = 12;
/// ```
pub fn check_list_code_block() {}

pub enum AnEnum {
WithVariants { and: usize, sub: usize, variants: usize },
}
3 changes: 2 additions & 1 deletion src/test/rustdoc/item-hide-threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ pub struct PrivStruct {
}

// @has 'item_hide_threshold/enum.Enum.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 0
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
pub enum Enum {
A, B, C,
D {
Expand Down