Skip to content

Hide default impls items #54162

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 6 commits into from
Nov 3, 2018
Merged
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
8 changes: 8 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
@@ -552,6 +552,14 @@ impl ItemEnum {
_ => return None,
})
}

pub fn is_associated(&self) -> bool {
match *self {
ItemEnum::TypedefItem(_, _) |
ItemEnum::AssociatedTypeItem(_, _) => true,
_ => false,
}
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
66 changes: 41 additions & 25 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
@@ -2259,8 +2259,8 @@ fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Re
if let Some(ref name) = item.name {
info!("Documenting {}", name);
}
document_stability(w, cx, item)?;
document_full(w, item, cx, "")?;
document_stability(w, cx, item, false)?;
document_full(w, item, cx, "", false)?;
Ok(())
}

@@ -2269,44 +2269,53 @@ fn render_markdown(w: &mut fmt::Formatter,
cx: &Context,
md_text: &str,
links: Vec<(String, String)>,
prefix: &str)
prefix: &str,
is_hidden: bool)
-> fmt::Result {
let mut ids = cx.id_map.borrow_mut();
write!(w, "<div class='docblock'>{}{}</div>",
prefix, Markdown(md_text, &links, RefCell::new(&mut ids), cx.codes))
write!(w, "<div class='docblock{}'>{}{}</div>",
if is_hidden { " hidden" } else { "" },
prefix,
Markdown(md_text, &links, RefCell::new(&mut ids),
cx.codes))
}

fn document_short(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item, link: AssocItemLink,
prefix: &str) -> fmt::Result {
prefix: &str, is_hidden: bool) -> fmt::Result {
if let Some(s) = item.doc_value() {
let markdown = if s.contains('\n') {
format!("{} [Read more]({})",
&plain_summary_line(Some(s)), naive_assoc_href(item, link))
} else {
plain_summary_line(Some(s)).to_string()
};
render_markdown(w, cx, &markdown, item.links(), prefix)?;
render_markdown(w, cx, &markdown, item.links(), prefix, is_hidden)?;
} else if !prefix.is_empty() {
write!(w, "<div class='docblock'>{}</div>", prefix)?;
write!(w, "<div class='docblock{}'>{}</div>",
if is_hidden { " hidden" } else { "" },
prefix)?;
}
Ok(())
}

fn document_full(w: &mut fmt::Formatter, item: &clean::Item,
cx: &Context, prefix: &str) -> fmt::Result {
cx: &Context, prefix: &str, is_hidden: bool) -> fmt::Result {
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
debug!("Doc block: =====\n{}\n=====", s);
render_markdown(w, cx, &*s, item.links(), prefix)?;
render_markdown(w, cx, &*s, item.links(), prefix, is_hidden)?;
} else if !prefix.is_empty() {
write!(w, "<div class='docblock'>{}</div>", prefix)?;
write!(w, "<div class='docblock{}'>{}</div>",
if is_hidden { " hidden" } else { "" },
prefix)?;
}
Ok(())
}

fn document_stability(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result {
fn document_stability(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
is_hidden: bool) -> fmt::Result {
let stabilities = short_stability(item, cx, true);
if !stabilities.is_empty() {
write!(w, "<div class='stability'>")?;
write!(w, "<div class='stability{}'>", if is_hidden { " hidden" } else { "" })?;
for stability in stabilities {
write!(w, "{}", stability)?;
}
@@ -3872,14 +3881,21 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
RenderMode::ForDeref { mut_: deref_mut_ } => should_render_item(&item, deref_mut_),
};

let (is_hidden, extra_class) = if trait_.is_none() ||
item.doc_value().is_some() ||
item.inner.is_associated() {
(false, "")
} else {
(true, " hidden")
};
match item.inner {
clean::MethodItem(clean::Method { ref decl, .. }) |
clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => {
clean::TyMethodItem(clean::TyMethod { ref decl, .. }) => {
// Only render when the method is not static or we allow static methods
if render_method_item {
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "{}", spotlight_decl(decl)?)?;
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
write!(w, "<table class='table-display'><tbody><tr><td><code>")?;
@@ -3901,15 +3917,15 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::TypedefItem(ref tydef, _) => {
let id = cx.derive_id(format!("{}.{}", ItemType::AssociatedType, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?;
}
clean::AssociatedConstItem(ref ty, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
let src = if let Some(l) = (Item { cx, item }).src_href() {
@@ -3923,7 +3939,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
clean::AssociatedTypeItem(ref bounds, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?;
@@ -3940,25 +3956,25 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
if let Some(it) = t.items.iter().find(|i| i.name == item.name) {
// We need the stability of the item from the trait
// because impls can't have a stability.
document_stability(w, cx, it)?;
document_stability(w, cx, it, is_hidden)?;
if item.doc_value().is_some() {
document_full(w, item, cx, "")?;
document_full(w, item, cx, "", is_hidden)?;
} else if show_def_docs {
// In case the item isn't documented,
// provide short documentation from the trait.
document_short(w, cx, it, link, "")?;
document_short(w, cx, it, link, "", is_hidden)?;
}
}
} else {
document_stability(w, cx, item)?;
document_stability(w, cx, item, is_hidden)?;
if show_def_docs {
document_full(w, item, cx, "")?;
document_full(w, item, cx, "", is_hidden)?;
}
}
} else {
document_stability(w, cx, item)?;
document_stability(w, cx, item, is_hidden)?;
if show_def_docs {
document_short(w, cx, item, link, "")?;
document_short(w, cx, item, link, "", is_hidden)?;
}
}
}
44 changes: 44 additions & 0 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
@@ -2057,6 +2057,50 @@
onEach(document.getElementsByClassName('method'), func);
onEach(document.getElementsByClassName('associatedconstant'), func);
onEach(document.getElementsByClassName('impl'), func);
onEach(document.getElementsByClassName('impl-items'), function(e) {
onEach(e.getElementsByClassName('associatedconstant'), func);
var hiddenElems = e.getElementsByClassName('hidden');
var needToggle = false;

for (var i = 0; i < hiddenElems.length; ++i) {
if (hasClass(hiddenElems[i], "content") === false &&
hasClass(hiddenElems[i], "docblock") === false) {
needToggle = true;
break;
}
}
if (needToggle === true) {
var newToggle = document.createElement('a');
newToggle.href = 'javascript:void(0)';
newToggle.className = 'collapse-toggle hidden-default collapsed';
newToggle.innerHTML = "[<span class='inner'>" + labelForToggleButton(true) + "</span>" +
"] Show hidden undocumented items";
newToggle.onclick = function() {
if (hasClass(this, "collapsed")) {
removeClass(this, "collapsed");
onEach(this.parentNode.getElementsByClassName("hidden"), function(x) {
if (hasClass(x, "content") === false) {
removeClass(x, "hidden");
addClass(x, "x");
}
}, true);
this.innerHTML = "[<span class='inner'>" + labelForToggleButton(false) +
"</span>] Hide undocumented items"
} else {
addClass(this, "collapsed");
onEach(this.parentNode.getElementsByClassName("x"), function(x) {
if (hasClass(x, "content") === false) {
addClass(x, "hidden");
removeClass(x, "x");
}
}, true);
this.innerHTML = "[<span class='inner'>" + labelForToggleButton(true) +
"</span>] Show hidden undocumented items";
}
};
e.insertBefore(newToggle, e.firstChild);
}
});

function createToggle(otherMessage, fontSize, extraClass, show) {
var span = document.createElement('span');
31 changes: 19 additions & 12 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
@@ -458,17 +458,6 @@ h4 > code, h3 > code, .invisible > code {
margin-bottom: 15px;
}

.content .impl-items .method, .content .impl-items > .type, .impl-items > .associatedconstant {
margin-left: 20px;
}
.content .impl-items .docblock, .content .impl-items .stability {
margin-bottom: .6em;
}

.content .impl-items > .stability {
margin-left: 40px;
}

.content .docblock > .impl-items {
margin-left: 20px;
margin-top: -34px;
@@ -506,7 +495,20 @@ h4 > code, h3 > code, .invisible > code {
top: -9px;
left: -13px;
}
.methods > .stability {

.content .impl-items .method, .content .impl-items > .type, .impl-items > .associatedconstant {
margin-left: 20px;
}

.content .impl-items .docblock, .content .impl-items .stability {
margin-bottom: .6em;
}

.content .impl-items > .stability {
margin-left: 40px;
}

.methods > .stability, .content .impl-items > .stability {
margin-top: -8px;
}

@@ -814,6 +816,11 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
text-align: center;
}

.collapse-toggle.hidden-default {
position: relative;
margin-left: 20px;
}

.ghost {
display: none;
}
16 changes: 12 additions & 4 deletions src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
@@ -15,11 +15,19 @@ var mainTheme = document.getElementById("mainThemeStyle");

var savedHref = [];

function onEach(arr, func) {
function onEach(arr, func, reversed) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
return true;
if (reversed !== true) {
for (var i = 0; i < arr.length; ++i) {
if (func(arr[i]) === true) {
return true;
}
}
} else {
for (var i = arr.length - 1; i >= 0; --i) {
if (func(arr[i]) === true) {
return true;
}
}
}
}
6 changes: 3 additions & 3 deletions src/test/rustdoc/assoc-consts.rs
Original file line number Diff line number Diff line change
@@ -75,8 +75,8 @@ pub trait Qux {
/// Docs for QUX1 in trait.
const QUX1: i8;
// @has - '//*[@id="associatedconstant.QUX_DEFAULT0"]' 'const QUX_DEFAULT0: u16'
// @has - '//*[@class="docblock"]' "Docs for QUX_DEFAULT0 in trait."
/// Docs for QUX_DEFAULT0 in trait.
// @has - '//*[@class="docblock"]' "Docs for QUX_DEFAULT12 in trait."
/// Docs for QUX_DEFAULT12 in trait.
const QUX_DEFAULT0: u16 = 1;
// @has - '//*[@id="associatedconstant.QUX_DEFAULT1"]' 'const QUX_DEFAULT1: i16'
// @has - '//*[@class="docblock"]' "Docs for QUX_DEFAULT1 in trait."
@@ -99,7 +99,7 @@ impl Qux for Bar {
/// Docs for QUX1 in impl.
const QUX1: i8 = 5;
// @has - '//*[@id="associatedconstant.QUX_DEFAULT0"]' 'const QUX_DEFAULT0: u16'
// @has - '//*[@class="docblock"]' "Docs for QUX_DEFAULT0 in trait."
// @has - '//*[@class="docblock hidden"]' "Docs for QUX_DEFAULT12 in trait."
const QUX_DEFAULT0: u16 = 6;
// @has - '//*[@id="associatedconstant.QUX_DEFAULT1"]' 'const QUX_DEFAULT1: i16'
// @has - '//*[@class="docblock"]' "Docs for QUX_DEFAULT1 in impl."
2 changes: 1 addition & 1 deletion src/test/rustdoc/manual_impl.rs
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ impl T for S2 {
// @has manual_impl/struct.S3.html '//*[@class="trait"]' 'T'
// @has - '//*[@class="docblock"]' 'Docs associated with the S3 trait implementation.'
// @has - '//*[@class="docblock"]' 'Docs associated with the S3 trait b_method implementation.'
// @has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
// @has - '//*[@class="docblock hidden"]' 'Docs associated with the trait a_method definition.'
pub struct S3(usize);

/// Docs associated with the S3 trait implementation.