Skip to content

Commit d05e28d

Browse files
authored
Rollup merge of rust-lang#84321 - Swatinem:subvariant-details, r=GuillaumeGomez
rustdoc: Convert sub-variant toggle to HTML Instead of creating a JS toggle, this injects details/summary for sub-variants of enums. This also fixes the CSS so that the toggle button does not jump when expanding/collapsing. Takes inspiration from rust-lang#83337 and should be considered part of rust-lang#83332. Not quite sure if the `.sub-variant` selectors could be further simplified? AFAICS it is only used in that place, and that does not seem to allow any recursion.
2 parents 5b7c986 + 85879fe commit d05e28d

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

src/librustdoc/html/render/print_item.rs

+2
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
963963

964964
use crate::clean::Variant;
965965
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
966+
toggle_open(w, "fields");
966967
let variant_id = cx.derive_id(format!(
967968
"{}.{}.fields",
968969
ItemType::Variant,
@@ -996,6 +997,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
996997
}
997998
}
998999
w.write_str("</div></div>");
1000+
toggle_close(w);
9991001
}
10001002
render_stability_since(w, variant, it, cx.tcx());
10011003
}

src/librustdoc/html/static/main.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ function hideThemeButtonState() {
455455
handleHashes(ev);
456456
}
457457

458+
function openParentDetails(elem) {
459+
while (elem) {
460+
if (elem.tagName === "DETAILS") {
461+
elem.open = true;
462+
}
463+
elem = elem.parentNode;
464+
}
465+
}
466+
458467
function expandSection(id) {
459468
var elem = document.getElementById(id);
460469
if (elem && isHidden(elem)) {
@@ -469,6 +478,8 @@ function hideThemeButtonState() {
469478
// The element is not visible, we need to make it appear!
470479
collapseDocs(collapses[0], "show");
471480
}
481+
// Open all ancestor <details> to make this element visible.
482+
openParentDetails(h3.parentNode);
472483
}
473484
}
474485
}
@@ -1009,7 +1020,7 @@ function hideThemeButtonState() {
10091020
if (hasClass(relatedDoc, "item-info")) {
10101021
relatedDoc = relatedDoc.nextElementSibling;
10111022
}
1012-
if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
1023+
if (hasClass(relatedDoc, "docblock")) {
10131024
if (mode === "toggle") {
10141025
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
10151026
action = "show";
@@ -1318,8 +1329,6 @@ function hideThemeButtonState() {
13181329
if (hasClass(e, "type-decl")) {
13191330
// We do something special for these
13201331
return;
1321-
} else if (hasClass(e, "sub-variant")) {
1322-
otherMessage = "&nbsp;Show&nbsp;fields";
13231332
} else if (hasClass(e, "non-exhaustive")) {
13241333
otherMessage = "&nbsp;This&nbsp;";
13251334
if (hasClass(e, "non-exhaustive-struct")) {
@@ -1351,7 +1360,6 @@ function hideThemeButtonState() {
13511360
}
13521361

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

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

src/librustdoc/html/static/rustdoc.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,11 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
10461046
}
10471047

10481048
.sub-variant, .sub-variant > h3 {
1049-
margin-top: 1px !important;
1049+
margin-top: 0px !important;
1050+
padding-top: 1px;
10501051
}
10511052

1052-
#main > .sub-variant > h3 {
1053+
#main > details > .sub-variant > h3 {
10531054
font-size: 15px;
10541055
margin-left: 25px;
10551056
margin-bottom: 5px;

src/test/rustdoc-gui/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ pub struct Foo;
2929

3030
impl Foo {
3131
#[must_use]
32-
pub fn must_use(&self) -> bool { true }
32+
pub fn must_use(&self) -> bool {
33+
true
34+
}
3335
}
3436

3537
/// Just a normal enum.
@@ -85,3 +87,7 @@ pub trait AnotherOne {
8587
/// let x = 12;
8688
/// ```
8789
pub fn check_list_code_block() {}
90+
91+
pub enum AnEnum {
92+
WithVariants { and: usize, sub: usize, variants: usize },
93+
}

src/test/rustdoc/item-hide-threshold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ pub struct PrivStruct {
6262
}
6363

6464
// @has 'item_hide_threshold/enum.Enum.html'
65-
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 0
65+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
66+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
6667
pub enum Enum {
6768
A, B, C,
6869
D {

0 commit comments

Comments
 (0)