Skip to content

Commit b6b98ea

Browse files
authored
Auto merge of #37728 - QuietMisdreavus:rustdoc-enum-struct, r=GuillaumeGomez
rustdoc: fold fields for enum struct variants into a docblock Per discussion in #37500, this PR updates the enum rendering code to wrap variants with named struct fields in a `docblock` span that is hidden automatically upon load of the page. This gives struct variant fields a clean separation from other enum variants, giving a boost to the readability of such documentation. Preview output is available [on the issue page](#37500 (comment)), but for the sake of completeness I'll include the images here again. ![rustdoc struct enum variant 4 part 1](https://cloud.githubusercontent.com/assets/5217170/20231925/96160b7e-a82a-11e6-945b-bbba95c5e4bc.PNG) When you initially load the page, there's a line under variants with struct fields letting you know you can click to expand the listing. ![rustdoc struct enum variant 4 part 2](https://cloud.githubusercontent.com/assets/3050060/20232067/1dc63266-a866-11e6-9555-8fb1c8afdcec.png) If you click to expand, the header and table unfold into a nicely-indented listing. If you want to take a look in your own browser and screen size, [I've got this version hosted on my server](https://shiva.icesoldier.me/doctest/doctest/enum.OldTopicRemoval.html). Fixes #37500 r? @GuillaumeGomez
2 parents 8c2b0e6 + fff9216 commit b6b98ea

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/librustdoc/html/render.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -2488,8 +2488,13 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
24882488
if let clean::VariantItem(Variant {
24892489
kind: VariantKind::Struct(ref s)
24902490
}) = variant.inner {
2491-
write!(w, "<h3 class='fields'>Fields</h3>\n
2492-
<table>")?;
2491+
let variant_id = derive_id(format!("{}.{}.fields",
2492+
ItemType::Variant,
2493+
variant.name.as_ref().unwrap()));
2494+
write!(w, "<span class='docblock autohide sub-variant' id='{id}'>",
2495+
id = variant_id)?;
2496+
write!(w, "<h3 class='fields'>Fields of <code>{name}</code></h3>\n
2497+
<table>", name = variant.name.as_ref().unwrap())?;
24932498
for field in &s.fields {
24942499
use clean::StructFieldItem;
24952500
if let StructFieldItem(ref ty) = field.inner {
@@ -2513,7 +2518,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
25132518
write!(w, "</td></tr>")?;
25142519
}
25152520
}
2516-
write!(w, "</table>")?;
2521+
write!(w, "</table></span>")?;
25172522
}
25182523
render_stability_since(w, variant, it)?;
25192524
}

src/librustdoc/html/static/main.js

+16
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,22 @@
10131013
.html('&nbsp;Expand&nbsp;description'));
10141014
var wrapper = $("<div class='toggle-wrapper'>").append(mainToggle);
10151015
$("#main > .docblock").before(wrapper);
1016+
1017+
$(".docblock.autohide").each(function() {
1018+
var wrap = $(this).prev();
1019+
if (wrap.is(".toggle-wrapper")) {
1020+
var toggle = wrap.children().first();
1021+
if ($(this).children().first().is("h3")) {
1022+
toggle.children(".toggle-label")
1023+
.text(" Show " + $(this).children().first().text());
1024+
}
1025+
$(this).hide();
1026+
wrap.addClass("collapsed");
1027+
toggle.children(".inner").text(labelForToggleButton(true));
1028+
toggle.children(".toggle-label").show();
1029+
}
1030+
});
1031+
10161032
var mainToggle =
10171033
$(toggle).append(
10181034
$('<span/>', {'class': 'toggle-label'})

src/librustdoc/html/static/rustdoc.css

+8
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ h4 > code, h3 > code, .invisible > code {
339339
border-bottom: 1px solid;
340340
}
341341

342+
.fields + table {
343+
margin-bottom: 1em;
344+
}
345+
342346
.content .item-list {
343347
list-style-type: none;
344348
padding: 0;
@@ -671,6 +675,10 @@ span.since {
671675
margin-top: 5px;
672676
}
673677

678+
.sub-variant, .sub-variant > h3 {
679+
margin-top: 0 !important;
680+
}
681+
674682
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock {
675683
margin-left: 30px;
676684
margin-bottom: 20px;

0 commit comments

Comments
 (0)