Skip to content

Commit

Permalink
Rollup merge of rust-lang#39740 - jimmycuadra:rustdoc-empty-stability…
Browse files Browse the repository at this point in the history
…, r=aturon

rustdoc: Only include a stability span if needed.

This patch gets rid of the empty stability boxes in docs by only including the span that creates it when the item actually has a stability class.

Here are images of the issue on `std::process::Output`:

Before:

<img width="340" alt="before" src="https://cloud.githubusercontent.com/assets/122457/22853638/ff88d1b2-f010-11e6-90d6-bf3d10e2fffa.png">

After:

<img width="333" alt="after" src="https://cloud.githubusercontent.com/assets/122457/22853639/06bfe7cc-f011-11e6-9892-f0ea2cc6ec90.png">

This is my first non-trivial patch to Rust, so I'm sure some of my approach is not idiomatic. Let me know how you'd like me to adjust!
  • Loading branch information
GuillaumeGomez authored Feb 12, 2017
2 parents 0095ec2 + 1fa9dbc commit 74204a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
25 changes: 16 additions & 9 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,24 @@ impl Item {
}
}

pub fn stability_class(&self) -> String {
self.stability.as_ref().map(|ref s| {
let mut base = match s.level {
stability::Unstable => "unstable".to_string(),
stability::Stable => String::new(),
};
pub fn stability_class(&self) -> Option<String> {
self.stability.as_ref().and_then(|ref s| {
let mut classes = Vec::with_capacity(2);

if s.level == stability::Unstable {
classes.push("unstable");
}

if !s.deprecated_since.is_empty() {
base.push_str(" deprecated");
classes.push("deprecated");
}
base
}).unwrap_or(String::new())

if classes.len() != 0 {
Some(classes.join(" "))
} else {
None
}
})
}

pub fn stable_since(&self) -> Option<&str> {
Expand Down
16 changes: 11 additions & 5 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
stab_docs = stab_docs,
docs = shorter(Some(&Markdown(doc_value).to_string())),
class = myitem.type_(),
stab = myitem.stability_class(),
stab = myitem.stability_class().unwrap_or("".to_string()),
unsafety_flag = unsafety_flag,
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
title_type = myitem.type_(),
Expand Down Expand Up @@ -2378,13 +2378,16 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, "<span id='{id}' class='{item_type}'>
<span id='{ns_id}' class='invisible'>
<code>{name}: {ty}</code>
</span></span><span class='stab {stab}'></span>",
</span></span>",
item_type = ItemType::StructField,
id = id,
ns_id = ns_id,
stab = field.stability_class(),
name = field.name.as_ref().unwrap(),
ty = ty)?;
if let Some(stability_class) = field.stability_class() {
write!(w, "<span class='stab {stab}'></span>",
stab = stability_class)?;
}
document(w, cx, field)?;
}
}
Expand Down Expand Up @@ -2415,11 +2418,14 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, "<h2 class='fields'>Fields</h2>")?;
for (field, ty) in fields {
write!(w, "<span id='{shortty}.{name}' class='{shortty}'><code>{name}: {ty}</code>
</span><span class='stab {stab}'></span>",
</span>",
shortty = ItemType::StructField,
stab = field.stability_class(),
name = field.name.as_ref().unwrap(),
ty = ty)?;
if let Some(stability_class) = field.stability_class() {
write!(w, "<span class='stab {stab}'></span>",
stab = stability_class)?;
}
document(w, cx, field)?;
}
}
Expand Down

0 comments on commit 74204a0

Please sign in to comment.