Skip to content

Commit b0c7033

Browse files
committed
print enum variant fields in docs
1 parent dd6e8d4 commit b0c7033

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

src/etc/htmldocck.py

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
from htmlentitydefs import entitydefs
118118
entitydefs['larrb'] = u'\u21e4'
119119
entitydefs['rarrb'] = u'\u21e5'
120+
entitydefs['nbsp'] = ' '
120121

121122
# "void elements" (no closing tag) from the HTML Standard section 12.1.2
122123
VOID_ELEMENTS = set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',

src/librustdoc/html/format.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,27 @@ impl fmt::Display for clean::Generics {
111111

112112
for (i, life) in self.lifetimes.iter().enumerate() {
113113
if i > 0 {
114-
f.write_str(", ")?;
114+
f.write_str(", ")?;
115115
}
116116
write!(f, "{}", *life)?;
117117
}
118118

119119
if !self.type_params.is_empty() {
120120
if !self.lifetimes.is_empty() {
121-
f.write_str(", ")?;
121+
f.write_str(", ")?;
122122
}
123123
for (i, tp) in self.type_params.iter().enumerate() {
124124
if i > 0 {
125-
f.write_str(", ")?
125+
f.write_str(", ")?
126126
}
127127
f.write_str(&tp.name)?;
128128

129129
if !tp.bounds.is_empty() {
130-
write!(f, ": {}", TyParamBounds(&tp.bounds))?;
130+
write!(f, ": {}", TyParamBounds(&tp.bounds))?;
131131
}
132132

133133
match tp.default {
134-
Some(ref ty) => { write!(f, " = {}", ty)?; },
134+
Some(ref ty) => { write!(f, " = {}", ty)?; },
135135
None => {}
136136
};
137137
}
@@ -229,21 +229,21 @@ impl fmt::Display for clean::PathParameters {
229229
let mut comma = false;
230230
for lifetime in lifetimes {
231231
if comma {
232-
f.write_str(", ")?;
232+
f.write_str(", ")?;
233233
}
234234
comma = true;
235235
write!(f, "{}", *lifetime)?;
236236
}
237237
for ty in types {
238238
if comma {
239-
f.write_str(", ")?;
239+
f.write_str(", ")?;
240240
}
241241
comma = true;
242242
write!(f, "{}", *ty)?;
243243
}
244244
for binding in bindings {
245245
if comma {
246-
f.write_str(", ")?;
246+
f.write_str(", ")?;
247247
}
248248
comma = true;
249249
write!(f, "{}", *binding)?;

src/librustdoc/html/render.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -2243,26 +2243,24 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22432243
write!(w, "</pre>")?;
22442244

22452245
document(w, cx, it)?;
2246-
let mut fields = s.fields.iter().filter(|f| {
2246+
let mut fields = s.fields.iter().filter_map(|f| {
22472247
match f.inner {
2248-
clean::StructFieldItem(..) => true,
2249-
_ => false,
2248+
clean::StructFieldItem(ref ty) => Some((f, ty)),
2249+
_ => None,
22502250
}
22512251
}).peekable();
22522252
if let doctree::Plain = s.struct_type {
22532253
if fields.peek().is_some() {
2254-
write!(w, "<h2 class='fields'>Fields</h2>\n<table>")?;
2255-
for field in fields {
2256-
write!(w, "<tr class='stab {stab}'>
2257-
<td id='{shortty}.{name}'>\
2258-
<code>{name}</code></td><td>",
2254+
write!(w, "<h2 class='fields'>Fields</h2>")?;
2255+
for (field, ty) in fields {
2256+
write!(w, "<span id='{shortty}.{name}'><code>{name}: {ty}</code></span>
2257+
<span class='stab {stab}'></span>",
22592258
shortty = ItemType::StructField,
22602259
stab = field.stability_class(),
2261-
name = field.name.as_ref().unwrap())?;
2260+
name = field.name.as_ref().unwrap(),
2261+
ty = ty)?;
22622262
document(w, cx, field)?;
2263-
write!(w, "</td></tr>")?;
22642263
}
2265-
write!(w, "</table>")?;
22662264
}
22672265
}
22682266
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
@@ -2292,7 +2290,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22922290
write!(w, "{}(", name)?;
22932291
for (i, ty) in tys.iter().enumerate() {
22942292
if i > 0 {
2295-
write!(w, ", ")?
2293+
write!(w, ",&nbsp;")?
22962294
}
22972295
write!(w, "{}", *ty)?;
22982296
}
@@ -2324,40 +2322,47 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
23242322

23252323
document(w, cx, it)?;
23262324
if !e.variants.is_empty() {
2327-
write!(w, "<h2 class='variants'>Variants</h2>\n<table class='variants_table'>")?;
2325+
write!(w, "<h2 class='variants'>Variants</h2>\n")?;
23282326
for variant in &e.variants {
2329-
write!(w, "<tr><td id='{shortty}.{name}'><code>{name}</code></td><td>",
2327+
write!(w, "<span id='{shortty}.{name}' class='variant'><code>{name}",
23302328
shortty = ItemType::Variant,
23312329
name = variant.name.as_ref().unwrap())?;
2330+
if let clean::VariantItem(ref var) = variant.inner {
2331+
if let clean::TupleVariant(ref tys) = var.kind {
2332+
write!(w, "(")?;
2333+
for (i, ty) in tys.iter().enumerate() {
2334+
if i > 0 {
2335+
write!(w, ",&nbsp;")?;
2336+
}
2337+
write!(w, "{}", *ty)?;
2338+
}
2339+
write!(w, ")")?;
2340+
}
2341+
}
2342+
write!(w, "</code></span>")?;
23322343
document(w, cx, variant)?;
23332344

23342345
use clean::{Variant, StructVariant};
23352346
if let clean::VariantItem( Variant { kind: StructVariant(ref s) } ) = variant.inner {
2336-
let fields = s.fields.iter().filter(|f| {
2337-
match f.inner {
2338-
clean::StructFieldItem(..) => true,
2339-
_ => false,
2340-
}
2341-
});
23422347
write!(w, "<h3 class='fields'>Fields</h3>\n
23432348
<table>")?;
2344-
for field in fields {
2345-
write!(w, "<tr><td \
2346-
id='{shortty}.{v}.field.{f}'>\
2347-
<code>{f}</code></td><td>",
2348-
shortty = ItemType::Variant,
2349-
v = variant.name.as_ref().unwrap(),
2350-
f = field.name.as_ref().unwrap())?;
2351-
document(w, cx, field)?;
2352-
write!(w, "</td></tr>")?;
2349+
for field in &s.fields {
2350+
use clean::StructFieldItem;
2351+
if let StructFieldItem(ref ty) = field.inner {
2352+
write!(w, "<tr><td \
2353+
id='variant.{v}.field.{f}'>\
2354+
<code>{f}:&nbsp;{t}</code></td><td>",
2355+
v = variant.name.as_ref().unwrap(),
2356+
f = field.name.as_ref().unwrap(),
2357+
t = *ty)?;
2358+
document(w, cx, field)?;
2359+
write!(w, "</td></tr>")?;
2360+
}
23532361
}
23542362
write!(w, "</table>")?;
23552363
}
2356-
write!(w, "</td><td>")?;
23572364
render_stability_since(w, variant, it)?;
2358-
write!(w, "</td></tr>")?;
23592365
}
2360-
write!(w, "</table>")?;
23612366
}
23622367
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)?;
23632368
Ok(())

src/librustdoc/html/static/rustdoc.css

+19
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ nav.sub {
265265
.docblock h2 { font-size: 1.15em; }
266266
.docblock h3, .docblock h4, .docblock h5 { font-size: 1em; }
267267

268+
.docblock {
269+
margin-left: 24px;
270+
}
271+
268272
.content .out-of-band {
269273
font-size: 23px;
270274
margin: 0px;
@@ -640,6 +644,21 @@ span.since {
640644
margin-right: 5px;
641645
}
642646

647+
.enum > .toggle-wrapper > .collapse-toggle, .struct > .toggle-wrapper > .collapse-toggle {
648+
left: 0;
649+
margin-top: 5px;
650+
}
651+
652+
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock {
653+
margin-left: 30px;
654+
margin-bottom: 20px;
655+
margin-top: 5px;
656+
}
657+
658+
.enum > .collapsed, .struct > .collapsed {
659+
margin-bottom: 25px;
660+
}
661+
643662
:target > code {
644663
background: #FDFFD3;
645664
}

0 commit comments

Comments
 (0)